vserver 1.9.3
[linux-2.6.git] / sound / core / rawmidi.c
1 /*
2  *  Abstract layer for MIDI v1.0 stream
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <sound/driver.h>
23 #include <sound/core.h>
24 #include <linux/major.h>
25 #include <linux/init.h>
26 #include <linux/smp_lock.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/time.h>
30 #include <linux/wait.h>
31 #include <linux/moduleparam.h>
32 #include <sound/rawmidi.h>
33 #include <sound/info.h>
34 #include <sound/control.h>
35 #include <sound/minors.h>
36 #include <sound/initval.h>
37
38 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
39 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
40 MODULE_LICENSE("GPL");
41
42 #ifdef CONFIG_SND_OSSEMUL
43 static int midi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
44 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
45 static int boot_devs;
46 module_param_array(midi_map, int, boot_devs, 0444);
47 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
48 module_param_array(amidi_map, int, boot_devs, 0444);
49 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
50 #endif /* CONFIG_SND_OSSEMUL */
51
52 static int snd_rawmidi_free(snd_rawmidi_t *rawmidi);
53 static int snd_rawmidi_dev_free(snd_device_t *device);
54 static int snd_rawmidi_dev_register(snd_device_t *device);
55 static int snd_rawmidi_dev_disconnect(snd_device_t *device);
56 static int snd_rawmidi_dev_unregister(snd_device_t *device);
57
58 snd_rawmidi_t *snd_rawmidi_devices[SNDRV_CARDS * SNDRV_RAWMIDI_DEVICES];
59
60 static DECLARE_MUTEX(register_mutex);
61
62 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
63 {
64         switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
65         case FMODE_WRITE:
66                 return SNDRV_RAWMIDI_LFLG_OUTPUT;
67         case FMODE_READ:
68                 return SNDRV_RAWMIDI_LFLG_INPUT;
69         default:
70                 return SNDRV_RAWMIDI_LFLG_OPEN;
71         }
72 }
73
74 static inline int snd_rawmidi_ready(snd_rawmidi_substream_t * substream)
75 {
76         snd_rawmidi_runtime_t *runtime = substream->runtime;
77         return runtime->avail >= runtime->avail_min;
78 }
79
80 static inline int snd_rawmidi_ready_append(snd_rawmidi_substream_t * substream, size_t count)
81 {
82         snd_rawmidi_runtime_t *runtime = substream->runtime;
83         return runtime->avail >= runtime->avail_min &&
84                (!substream->append || runtime->avail >= count);
85 }
86
87 static int snd_rawmidi_init(snd_rawmidi_substream_t *substream)
88 {
89         snd_rawmidi_runtime_t *runtime = substream->runtime;
90         spin_lock_init(&runtime->lock);
91         init_waitqueue_head(&runtime->sleep);
92         runtime->event = NULL;
93         runtime->buffer_size = PAGE_SIZE;
94         runtime->avail_min = 1;
95         if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
96                 runtime->avail = 0;
97         else
98                 runtime->avail = runtime->buffer_size;
99         if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL)
100                 return -ENOMEM;
101         runtime->appl_ptr = runtime->hw_ptr = 0;
102         return 0;
103 }
104
105 static int snd_rawmidi_done_buffer(snd_rawmidi_runtime_t *runtime)
106 {
107         if (runtime->buffer) {
108                 kfree(runtime->buffer);
109                 runtime->buffer = NULL;
110         }
111         return 0;
112 }
113
114 int snd_rawmidi_drop_output(snd_rawmidi_substream_t * substream)
115 {
116         snd_rawmidi_runtime_t *runtime = substream->runtime;
117
118         substream->ops->trigger(substream, 0);
119         runtime->trigger = 0;
120         runtime->drain = 0;
121         /* interrupts are not enabled at this moment,
122            so spinlock is not required */
123         runtime->appl_ptr = runtime->hw_ptr = 0;
124         runtime->avail = runtime->buffer_size;
125         return 0;
126 }
127
128 int snd_rawmidi_drain_output(snd_rawmidi_substream_t * substream)
129 {
130         int err;
131         long timeout;
132         snd_rawmidi_runtime_t *runtime = substream->runtime;
133
134         err = 0;
135         runtime->drain = 1;
136         while (runtime->avail < runtime->buffer_size) {
137                 timeout = interruptible_sleep_on_timeout(&runtime->sleep, 10 * HZ);
138                 if (signal_pending(current)) {
139                         err = -ERESTARTSYS;
140                         break;
141                 }
142                 if (runtime->avail < runtime->buffer_size && !timeout) {
143                         snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
144                         err = -EIO;
145                         break;
146                 }
147         }
148         runtime->drain = 0;
149         if (err != -ERESTARTSYS) {
150                 /* we need wait a while to make sure that Tx FIFOs are empty */
151                 if (substream->ops->drain)
152                         substream->ops->drain(substream);
153                 else {
154                         set_current_state(TASK_UNINTERRUPTIBLE);
155                         schedule_timeout(HZ / 20);
156                 }
157                 snd_rawmidi_drop_output(substream);
158         }
159         return err;
160 }
161
162 int snd_rawmidi_drain_input(snd_rawmidi_substream_t * substream)
163 {
164         snd_rawmidi_runtime_t *runtime = substream->runtime;
165
166         substream->ops->trigger(substream, 0);
167         runtime->trigger = 0;
168         runtime->drain = 0;
169         /* interrupts aren't enabled at this moment, so spinlock isn't needed */
170         runtime->appl_ptr = runtime->hw_ptr = 0;
171         runtime->avail = 0;
172         return 0;
173 }
174
175 int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
176                             int mode, snd_rawmidi_file_t * rfile)
177 {
178         snd_rawmidi_t *rmidi;
179         struct list_head *list1, *list2;
180         snd_rawmidi_substream_t *sinput, *soutput;
181         snd_rawmidi_runtime_t *input = NULL, *output = NULL;
182         int err;
183
184         if (rfile)
185                 rfile->input = rfile->output = NULL;
186         rmidi = snd_rawmidi_devices[(cardnum * SNDRV_RAWMIDI_DEVICES) + device];
187         if (rmidi == NULL) {
188                 err = -ENODEV;
189                 goto __error1;
190         }
191         if (!try_module_get(rmidi->card->module)) {
192                 err = -EFAULT;
193                 goto __error1;
194         }
195         if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
196                 down(&rmidi->open_mutex);
197         if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
198                 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
199                         err = -ENXIO;
200                         goto __error;
201                 }
202                 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
203                         err = -ENODEV;
204                         goto __error;
205                 }
206                 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
207                     rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
208                         err = -EAGAIN;
209                         goto __error;
210                 }
211         }
212         if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
213                 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
214                         err = -ENXIO;
215                         goto __error;
216                 }
217                 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
218                         err = -ENODEV;
219                         goto __error;
220                 }
221                 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
222                     rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
223                         err = -EAGAIN;
224                         goto __error;
225                 }
226         }
227         list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
228         while (1) {
229                 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
230                         sinput = NULL;
231                         if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
232                                 err = -EAGAIN;
233                                 goto __error;
234                         }
235                         break;
236                 }
237                 sinput = list_entry(list1, snd_rawmidi_substream_t, list);
238                 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
239                         goto __nexti;
240                 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
241                         break;
242               __nexti:
243                 list1 = list1->next;
244         }
245         list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
246         while (1) {
247                 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
248                         soutput = NULL;
249                         if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
250                                 err = -EAGAIN;
251                                 goto __error;
252                         }
253                         break;
254                 }
255                 soutput = list_entry(list2, snd_rawmidi_substream_t, list);
256                 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
257                         if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
258                                 if (soutput->opened && !soutput->append)
259                                         goto __nexto;
260                         } else {
261                                 if (soutput->opened)
262                                         goto __nexto;
263                         }
264                 }
265                 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
266                         break;
267               __nexto:
268                 list2 = list2->next;
269         }
270         if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
271                 input = kcalloc(1, sizeof(*input), GFP_KERNEL);
272                 if (input == NULL) {
273                         err = -ENOMEM;
274                         goto __error;
275                 }
276                 sinput->runtime = input;
277                 if (snd_rawmidi_init(sinput) < 0) {
278                         err = -ENOMEM;
279                         goto __error;
280                 }
281                 if ((err = sinput->ops->open(sinput)) < 0) {
282                         sinput->runtime = NULL;
283                         goto __error;
284                 }
285                 sinput->opened = 1;
286                 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
287         } else {
288                 sinput = NULL;
289         }
290         if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
291                 if (soutput->opened)
292                         goto __skip_output;
293                 output = kcalloc(1, sizeof(*output), GFP_KERNEL);
294                 if (output == NULL) {
295                         err = -ENOMEM;
296                         goto __error;
297                 }
298                 soutput->runtime = output;
299                 if (snd_rawmidi_init(soutput) < 0) {
300                         if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
301                                 sinput->ops->close(sinput);
302                                 sinput->runtime = NULL;
303                         }
304                         err = -ENOMEM;
305                         goto __error;
306                 }
307                 if ((err = soutput->ops->open(soutput)) < 0) {
308                         if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
309                                 sinput->ops->close(sinput);
310                                 sinput->runtime = NULL;
311                         }
312                         soutput->runtime = NULL;
313                         goto __error;
314                 }
315               __skip_output:
316                 soutput->opened = 1;
317                 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
318                         soutput->append = 1;
319                 if (soutput->use_count++ == 0)
320                         soutput->active_sensing = 1;
321                 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
322         } else {
323                 soutput = NULL;
324         }
325         if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
326                 up(&rmidi->open_mutex);
327         if (rfile) {
328                 rfile->rmidi = rmidi;
329                 rfile->input = sinput;
330                 rfile->output = soutput;
331         }
332         return 0;
333
334       __error:
335         if (input != NULL) {
336                 snd_rawmidi_done_buffer(input);
337                 kfree(input);
338         }
339         if (output != NULL) {
340                 snd_rawmidi_done_buffer(output);
341                 kfree(output);
342         }
343         module_put(rmidi->card->module);
344         if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
345                 up(&rmidi->open_mutex);
346       __error1:
347         return err;
348 }
349
350 static int snd_rawmidi_open(struct inode *inode, struct file *file)
351 {
352         int maj = imajor(inode);
353         int cardnum;
354         snd_card_t *card;
355         int device, subdevice;
356         unsigned short fflags;
357         int err;
358         snd_rawmidi_t *rmidi;
359         snd_rawmidi_file_t *rawmidi_file;
360         wait_queue_t wait;
361         struct list_head *list;
362         snd_ctl_file_t *kctl;
363
364         switch (maj) {
365         case CONFIG_SND_MAJOR:
366                 cardnum = SNDRV_MINOR_CARD(iminor(inode));
367                 cardnum %= SNDRV_CARDS;
368                 device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_RAWMIDI;
369                 device %= SNDRV_MINOR_RAWMIDIS;
370                 break;
371 #ifdef CONFIG_SND_OSSEMUL
372         case SOUND_MAJOR:
373                 cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
374                 cardnum %= SNDRV_CARDS;
375                 device = SNDRV_MINOR_OSS_DEVICE(iminor(inode)) == SNDRV_MINOR_OSS_MIDI ?
376                         midi_map[cardnum] : amidi_map[cardnum];
377                 break;
378 #endif
379         default:
380                 return -ENXIO;
381         }
382
383         rmidi = snd_rawmidi_devices[(cardnum * SNDRV_RAWMIDI_DEVICES) + device];
384         if (rmidi == NULL)
385                 return -ENODEV;
386 #ifdef CONFIG_SND_OSSEMUL
387         if (maj == SOUND_MAJOR && !rmidi->ossreg)
388                 return -ENXIO;
389 #endif
390         if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK)) 
391                 return -EINVAL;         /* invalid combination */
392         card = rmidi->card;
393         err = snd_card_file_add(card, file);
394         if (err < 0)
395                 return -ENODEV;
396         fflags = snd_rawmidi_file_flags(file);
397         if ((file->f_flags & O_APPEND) || maj != CONFIG_SND_MAJOR) /* OSS emul? */
398                 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
399         fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
400         rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
401         if (rawmidi_file == NULL) {
402                 snd_card_file_remove(card, file);
403                 return -ENOMEM;
404         }
405         init_waitqueue_entry(&wait, current);
406         add_wait_queue(&rmidi->open_wait, &wait);
407         down(&rmidi->open_mutex);
408         while (1) {
409                 subdevice = -1;
410                 down_read(&card->controls_rwsem);
411                 list_for_each(list, &card->ctl_files) {
412                         kctl = snd_ctl_file(list);
413                         if (kctl->pid == current->pid) {
414                                 subdevice = kctl->prefer_rawmidi_subdevice;
415                                 break;
416                         }
417                 }
418                 up_read(&card->controls_rwsem);
419                 err = snd_rawmidi_kernel_open(cardnum, device, subdevice, fflags, rawmidi_file);
420                 if (err >= 0)
421                         break;
422                 if (err == -EAGAIN) {
423                         if (file->f_flags & O_NONBLOCK) {
424                                 err = -EBUSY;
425                                 break;
426                         }
427                 } else
428                         break;
429                 set_current_state(TASK_INTERRUPTIBLE);
430                 up(&rmidi->open_mutex);
431                 schedule();
432                 down(&rmidi->open_mutex);
433                 if (signal_pending(current)) {
434                         err = -ERESTARTSYS;
435                         break;
436                 }
437         }
438 #ifdef CONFIG_SND_OSSEMUL
439         if (rawmidi_file->input && rawmidi_file->input->runtime)
440                 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
441         if (rawmidi_file->output && rawmidi_file->output->runtime)
442                 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
443 #endif
444         remove_wait_queue(&rmidi->open_wait, &wait);
445         if (err >= 0) {
446                 file->private_data = rawmidi_file;
447         } else {
448                 snd_card_file_remove(card, file);
449                 kfree(rawmidi_file);
450         }
451         up(&rmidi->open_mutex);
452         return err;
453 }
454
455 int snd_rawmidi_kernel_release(snd_rawmidi_file_t * rfile)
456 {
457         snd_rawmidi_t *rmidi;
458         snd_rawmidi_substream_t *substream;
459         snd_rawmidi_runtime_t *runtime;
460
461         snd_assert(rfile != NULL, return -ENXIO);
462         snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
463         rmidi = rfile->rmidi;
464         down(&rmidi->open_mutex);
465         if (rfile->input != NULL) {
466                 substream = rfile->input;
467                 rfile->input = NULL;
468                 runtime = substream->runtime;
469                 runtime->trigger = 0;
470                 substream->ops->trigger(substream, 0);
471                 substream->ops->close(substream);
472                 snd_rawmidi_done_buffer(runtime);
473                 if (runtime->private_free != NULL)
474                         runtime->private_free(substream);
475                 kfree(runtime);
476                 substream->runtime = NULL;
477                 substream->opened = 0;
478                 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
479         }
480         if (rfile->output != NULL) {
481                 substream = rfile->output;
482                 rfile->output = NULL;
483                 if (--substream->use_count == 0) {
484                         runtime = substream->runtime;
485                         if (substream->active_sensing) {
486                                 unsigned char buf = 0xfe;
487                                 /* sending single active sensing message to shut the device up */
488                                 snd_rawmidi_kernel_write(substream, &buf, 1);
489                         }
490                         if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
491                                 substream->ops->trigger(substream, 0);
492                         substream->ops->close(substream);
493                         snd_rawmidi_done_buffer(runtime);
494                         if (runtime->private_free != NULL)
495                                 runtime->private_free(substream);
496                         kfree(runtime);
497                         substream->runtime = NULL;
498                         substream->opened = 0;
499                         substream->append = 0;
500                 }
501                 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
502         }
503         up(&rmidi->open_mutex);
504         module_put(rmidi->card->module);
505         return 0;
506 }
507
508 static int snd_rawmidi_release(struct inode *inode, struct file *file)
509 {
510         snd_rawmidi_file_t *rfile;
511         snd_rawmidi_t *rmidi;
512         int err;
513
514         rfile = file->private_data;
515         err = snd_rawmidi_kernel_release(rfile);
516         rmidi = rfile->rmidi;
517         wake_up(&rmidi->open_wait);
518         kfree(rfile);
519         snd_card_file_remove(rmidi->card, file);
520         return err;
521 }
522
523 int snd_rawmidi_info(snd_rawmidi_substream_t *substream, snd_rawmidi_info_t *info)
524 {
525         snd_rawmidi_t *rmidi;
526         
527         if (substream == NULL)
528                 return -ENODEV;
529         rmidi = substream->rmidi;
530         memset(info, 0, sizeof(*info));
531         info->card = rmidi->card->number;
532         info->device = rmidi->device;
533         info->subdevice = substream->number;
534         info->stream = substream->stream;
535         info->flags = rmidi->info_flags;
536         strcpy(info->id, rmidi->id);
537         strcpy(info->name, rmidi->name);
538         strcpy(info->subname, substream->name);
539         info->subdevices_count = substream->pstr->substream_count;
540         info->subdevices_avail = (substream->pstr->substream_count -
541                                   substream->pstr->substream_opened);
542         return 0;
543 }
544
545 static int snd_rawmidi_info_user(snd_rawmidi_substream_t *substream, snd_rawmidi_info_t __user * _info)
546 {
547         snd_rawmidi_info_t info;
548         int err;
549         if ((err = snd_rawmidi_info(substream, &info)) < 0)
550                 return err;
551         if (copy_to_user(_info, &info, sizeof(snd_rawmidi_info_t)))
552                 return -EFAULT;
553         return 0;
554 }
555
556 int snd_rawmidi_info_select(snd_card_t *card, snd_rawmidi_info_t *info)
557 {
558         snd_rawmidi_t *rmidi;
559         snd_rawmidi_str_t *pstr;
560         snd_rawmidi_substream_t *substream;
561         struct list_head *list;
562         if (info->device >= SNDRV_RAWMIDI_DEVICES)
563                 return -ENXIO;
564         rmidi = snd_rawmidi_devices[card->number * SNDRV_RAWMIDI_DEVICES + info->device];
565         if (info->stream < 0 || info->stream > 1)
566                 return -EINVAL;
567         pstr = &rmidi->streams[info->stream];
568         if (pstr->substream_count == 0)
569                 return -ENOENT;
570         if (info->subdevice >= pstr->substream_count)
571                 return -ENXIO;
572         list_for_each(list, &pstr->substreams) {
573                 substream = list_entry(list, snd_rawmidi_substream_t, list);
574                 if ((unsigned int)substream->number == info->subdevice)
575                         return snd_rawmidi_info(substream, info);
576         }
577         return -ENXIO;
578 }
579
580 static int snd_rawmidi_info_select_user(snd_card_t *card,
581                                         snd_rawmidi_info_t __user *_info)
582 {
583         int err;
584         snd_rawmidi_info_t info;
585         if (get_user(info.device, &_info->device))
586                 return -EFAULT;
587         if (get_user(info.stream, &_info->stream))
588                 return -EFAULT;
589         if (get_user(info.subdevice, &_info->subdevice))
590                 return -EFAULT;
591         if ((err = snd_rawmidi_info_select(card, &info)) < 0)
592                 return err;
593         if (copy_to_user(_info, &info, sizeof(snd_rawmidi_info_t)))
594                 return -EFAULT;
595         return 0;
596 }
597
598 int snd_rawmidi_output_params(snd_rawmidi_substream_t * substream,
599                               snd_rawmidi_params_t * params)
600 {
601         char *newbuf;
602         snd_rawmidi_runtime_t *runtime = substream->runtime;
603         
604         if (substream->append && substream->use_count > 1)
605                 return -EBUSY;
606         snd_rawmidi_drain_output(substream);
607         if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
608                 return -EINVAL;
609         }
610         if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
611                 return -EINVAL;
612         }
613         if (params->buffer_size != runtime->buffer_size) {
614                 if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL)
615                         return -ENOMEM;
616                 kfree(runtime->buffer);
617                 runtime->buffer = newbuf;
618                 runtime->buffer_size = params->buffer_size;
619         }
620         runtime->avail_min = params->avail_min;
621         substream->active_sensing = !params->no_active_sensing;
622         return 0;
623 }
624
625 int snd_rawmidi_input_params(snd_rawmidi_substream_t * substream,
626                              snd_rawmidi_params_t * params)
627 {
628         char *newbuf;
629         snd_rawmidi_runtime_t *runtime = substream->runtime;
630
631         snd_rawmidi_drain_input(substream);
632         if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
633                 return -EINVAL;
634         }
635         if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
636                 return -EINVAL;
637         }
638         if (params->buffer_size != runtime->buffer_size) {
639                 if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL)
640                         return -ENOMEM;
641                 kfree(runtime->buffer);
642                 runtime->buffer = newbuf;
643                 runtime->buffer_size = params->buffer_size;
644         }
645         runtime->avail_min = params->avail_min;
646         return 0;
647 }
648
649 static int snd_rawmidi_output_status(snd_rawmidi_substream_t * substream,
650                                      snd_rawmidi_status_t * status)
651 {
652         snd_rawmidi_runtime_t *runtime = substream->runtime;
653
654         memset(status, 0, sizeof(*status));
655         status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
656         spin_lock_irq(&runtime->lock);
657         status->avail = runtime->avail;
658         spin_unlock_irq(&runtime->lock);
659         return 0;
660 }
661
662 static int snd_rawmidi_input_status(snd_rawmidi_substream_t * substream,
663                                     snd_rawmidi_status_t * status)
664 {
665         snd_rawmidi_runtime_t *runtime = substream->runtime;
666
667         memset(status, 0, sizeof(*status));
668         status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
669         spin_lock_irq(&runtime->lock);
670         status->avail = runtime->avail;
671         status->xruns = runtime->xruns;
672         runtime->xruns = 0;
673         spin_unlock_irq(&runtime->lock);
674         return 0;
675 }
676
677 static inline int _snd_rawmidi_ioctl(struct inode *inode, struct file *file,
678                                      unsigned int cmd, unsigned long arg)
679 {
680         snd_rawmidi_file_t *rfile;
681         void __user *argp = (void __user *)arg;
682
683         rfile = file->private_data;
684         if (((cmd >> 8) & 0xff) != 'W')
685                 return -ENOTTY;
686         switch (cmd) {
687         case SNDRV_RAWMIDI_IOCTL_PVERSION:
688                 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
689         case SNDRV_RAWMIDI_IOCTL_INFO:
690         {
691                 snd_rawmidi_stream_t stream;
692                 snd_rawmidi_info_t __user *info = argp;
693                 if (get_user(stream, &info->stream))
694                         return -EFAULT;
695                 switch (stream) {
696                 case SNDRV_RAWMIDI_STREAM_INPUT:
697                         return snd_rawmidi_info_user(rfile->input, info);
698                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
699                         return snd_rawmidi_info_user(rfile->output, info);
700                 default:
701                         return -EINVAL;
702                 }
703         }
704         case SNDRV_RAWMIDI_IOCTL_PARAMS:
705         {
706                 snd_rawmidi_params_t params;
707                 if (copy_from_user(&params, argp, sizeof(snd_rawmidi_params_t)))
708                         return -EFAULT;
709                 switch (params.stream) {
710                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
711                         if (rfile->output == NULL)
712                                 return -EINVAL;
713                         return snd_rawmidi_output_params(rfile->output, &params);
714                 case SNDRV_RAWMIDI_STREAM_INPUT:
715                         if (rfile->input == NULL)
716                                 return -EINVAL;
717                         return snd_rawmidi_input_params(rfile->input, &params);
718                 default:
719                         return -EINVAL;
720                 }
721         }
722         case SNDRV_RAWMIDI_IOCTL_STATUS:
723         {
724                 int err = 0;
725                 snd_rawmidi_status_t status;
726                 if (copy_from_user(&status, argp, sizeof(snd_rawmidi_status_t)))
727                         return -EFAULT;
728                 switch (status.stream) {
729                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
730                         if (rfile->output == NULL)
731                                 return -EINVAL;
732                         err = snd_rawmidi_output_status(rfile->output, &status);
733                         break;
734                 case SNDRV_RAWMIDI_STREAM_INPUT:
735                         if (rfile->input == NULL)
736                                 return -EINVAL;
737                         err = snd_rawmidi_input_status(rfile->input, &status);
738                         break;
739                 default:
740                         return -EINVAL;
741                 }
742                 if (err < 0)
743                         return err;
744                 if (copy_to_user(argp, &status, sizeof(snd_rawmidi_status_t)))
745                         return -EFAULT;
746                 return 0;
747         }
748         case SNDRV_RAWMIDI_IOCTL_DROP:
749         {
750                 int val;
751                 if (get_user(val, (long __user *) argp))
752                         return -EFAULT;
753                 switch (val) {
754                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
755                         if (rfile->output == NULL)
756                                 return -EINVAL;
757                         return snd_rawmidi_drop_output(rfile->output);
758                 default:
759                         return -EINVAL;
760                 }
761         }
762         case SNDRV_RAWMIDI_IOCTL_DRAIN:
763         {
764                 int val;
765                 if (get_user(val, (long __user *) argp))
766                         return -EFAULT;
767                 switch (val) {
768                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
769                         if (rfile->output == NULL)
770                                 return -EINVAL;
771                         return snd_rawmidi_drain_output(rfile->output);
772                 case SNDRV_RAWMIDI_STREAM_INPUT:
773                         if (rfile->input == NULL)
774                                 return -EINVAL;
775                         return snd_rawmidi_drain_input(rfile->input);
776                 default:
777                         return -EINVAL;
778                 }
779         }
780 #ifdef CONFIG_SND_DEBUG
781         default:
782                 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
783 #endif
784         }
785         return -ENOTTY;
786 }
787
788 /* FIXME: need to unlock BKL to allow preemption */
789 static int snd_rawmidi_ioctl(struct inode *inode, struct file *file,
790                              unsigned int cmd, unsigned long arg)
791 {
792         int err;
793         unlock_kernel();
794         err = _snd_rawmidi_ioctl(inode, file, cmd, arg);
795         lock_kernel();
796         return err;
797 }
798
799 int snd_rawmidi_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
800                               unsigned int cmd, unsigned long arg)
801 {
802         void __user *argp = (void __user *)arg;
803         unsigned int tmp;
804
805         tmp = card->number * SNDRV_RAWMIDI_DEVICES;
806         switch (cmd) {
807         case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
808         {
809                 int device;
810                 
811                 if (get_user(device, (int __user *)argp))
812                         return -EFAULT;
813                 device = device < 0 ? 0 : device + 1;
814                 while (device < SNDRV_RAWMIDI_DEVICES) {
815                         if (snd_rawmidi_devices[tmp + device])
816                                 break;
817                         device++;
818                 }
819                 if (device == SNDRV_RAWMIDI_DEVICES)
820                         device = -1;
821                 if (put_user(device, (int __user *)argp))
822                         return -EFAULT;
823                 return 0;
824         }
825         case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
826         {
827                 int val;
828                 
829                 if (get_user(val, (int __user *)argp))
830                         return -EFAULT;
831                 control->prefer_rawmidi_subdevice = val;
832                 return 0;
833         }
834         case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
835                 return snd_rawmidi_info_select_user(card, argp);
836         }
837         return -ENOIOCTLCMD;
838 }
839
840 /**
841  * snd_rawmidi_receive - receive the input data from the device
842  * @substream: the rawmidi substream
843  * @buffer: the buffer pointer
844  * @count: the data size to read
845  *
846  * Reads the data from the internal buffer.
847  *
848  * Returns the size of read data, or a negative error code on failure.
849  */
850 int snd_rawmidi_receive(snd_rawmidi_substream_t * substream, const unsigned char *buffer, int count)
851 {
852         unsigned long flags;
853         int result = 0, count1;
854         snd_rawmidi_runtime_t *runtime = substream->runtime;
855
856         if (runtime->buffer == NULL) {
857                 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
858                 return -EINVAL;
859         }
860         spin_lock_irqsave(&runtime->lock, flags);
861         if (count == 1) {       /* special case, faster code */
862                 substream->bytes++;
863                 if (runtime->avail < runtime->buffer_size) {
864                         runtime->buffer[runtime->hw_ptr++] = buffer[0];
865                         runtime->hw_ptr %= runtime->buffer_size;
866                         runtime->avail++;
867                         result++;
868                 } else {
869                         runtime->xruns++;
870                 }
871         } else {
872                 substream->bytes += count;
873                 count1 = runtime->buffer_size - runtime->hw_ptr;
874                 if (count1 > count)
875                         count1 = count;
876                 if (count1 > (int)(runtime->buffer_size - runtime->avail))
877                         count1 = runtime->buffer_size - runtime->avail;
878                 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
879                 runtime->hw_ptr += count1;
880                 runtime->hw_ptr %= runtime->buffer_size;
881                 runtime->avail += count1;
882                 count -= count1;
883                 result += count1;
884                 if (count > 0) {
885                         buffer += count1;
886                         count1 = count;
887                         if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
888                                 count1 = runtime->buffer_size - runtime->avail;
889                                 runtime->xruns = count - count1;
890                         }
891                         if (count1 > 0) {
892                                 memcpy(runtime->buffer, buffer, count1);
893                                 runtime->hw_ptr = count1;
894                                 runtime->avail += count1;
895                                 result += count1;
896                         }
897                 }
898         }
899         if (result > 0 && runtime->event == NULL) {
900                 if (snd_rawmidi_ready(substream))
901                         wake_up(&runtime->sleep);
902         }
903         spin_unlock_irqrestore(&runtime->lock, flags);
904         if (result > 0 && runtime->event)
905                 runtime->event(substream);
906         return result;
907 }
908
909 static long snd_rawmidi_kernel_read1(snd_rawmidi_substream_t *substream,
910                                      unsigned char *buf, long count, int kernel)
911 {
912         unsigned long flags;
913         long result = 0, count1;
914         snd_rawmidi_runtime_t *runtime = substream->runtime;
915
916         while (count > 0 && runtime->avail) {
917                 count1 = runtime->buffer_size - runtime->appl_ptr;
918                 if (count1 > count)
919                         count1 = count;
920                 spin_lock_irqsave(&runtime->lock, flags);
921                 if (count1 > (int)runtime->avail)
922                         count1 = runtime->avail;
923                 if (kernel) {
924                         memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
925                 } else {
926                         spin_unlock_irqrestore(&runtime->lock, flags);
927                         if (copy_to_user(buf + result, runtime->buffer + runtime->appl_ptr, count1)) {
928                                 return result > 0 ? result : -EFAULT;
929                         }
930                         spin_lock_irqsave(&runtime->lock, flags);
931                 }
932                 runtime->appl_ptr += count1;
933                 runtime->appl_ptr %= runtime->buffer_size;
934                 runtime->avail -= count1;
935                 spin_unlock_irqrestore(&runtime->lock, flags);
936                 result += count1;
937                 count -= count1;
938         }
939         return result;
940 }
941
942 long snd_rawmidi_kernel_read(snd_rawmidi_substream_t *substream, unsigned char *buf, long count)
943 {
944         substream->runtime->trigger = 1;
945         substream->ops->trigger(substream, 1);
946         return snd_rawmidi_kernel_read1(substream, buf, count, 1);
947 }
948
949 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
950 {
951         long result;
952         int count1;
953         snd_rawmidi_file_t *rfile;
954         snd_rawmidi_substream_t *substream;
955         snd_rawmidi_runtime_t *runtime;
956
957         rfile = file->private_data;
958         substream = rfile->input;
959         if (substream == NULL)
960                 return -EIO;
961         runtime = substream->runtime;
962         runtime->trigger = 1;
963         substream->ops->trigger(substream, 1);
964         result = 0;
965         while (count > 0) {
966                 spin_lock_irq(&runtime->lock);
967                 while (!snd_rawmidi_ready(substream)) {
968                         wait_queue_t wait;
969                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
970                                 spin_unlock_irq(&runtime->lock);
971                                 return result > 0 ? result : -EAGAIN;
972                         }
973                         init_waitqueue_entry(&wait, current);
974                         add_wait_queue(&runtime->sleep, &wait);
975                         set_current_state(TASK_INTERRUPTIBLE);
976                         spin_unlock_irq(&runtime->lock);
977                         schedule();
978                         remove_wait_queue(&runtime->sleep, &wait);
979                         if (signal_pending(current))
980                                 return result > 0 ? result : -ERESTARTSYS;
981                         if (!runtime->avail)
982                                 return result > 0 ? result : -EIO;
983                         spin_lock_irq(&runtime->lock);
984                 }
985                 spin_unlock_irq(&runtime->lock);
986                 count1 = snd_rawmidi_kernel_read1(substream, buf, count, 0);
987                 if (count1 < 0)
988                         return result > 0 ? result : count1;
989                 result += count1;
990                 buf += count1;
991                 count -= count1;
992         }
993         return result;
994 }
995
996 /**
997  * snd_rawmidi_transmit_empty - check whether the output buffer is empty
998  * @substream: the rawmidi substream
999  * 
1000  * Returns 1 if the internal output buffer is empty, 0 if not.
1001  */
1002 int snd_rawmidi_transmit_empty(snd_rawmidi_substream_t * substream)
1003 {
1004         snd_rawmidi_runtime_t *runtime = substream->runtime;
1005         int result;
1006         unsigned long flags;
1007
1008         if (runtime->buffer == NULL) {
1009                 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1010                 return 1;
1011         }
1012         spin_lock_irqsave(&runtime->lock, flags);
1013         result = runtime->avail >= runtime->buffer_size;
1014         if (result)
1015                 runtime->trigger = 1;
1016         spin_unlock_irqrestore(&runtime->lock, flags);
1017         return result;          
1018 }
1019
1020 /**
1021  * snd_rawmidi_transmit_peek - copy data from the internal buffer
1022  * @substream: the rawmidi substream
1023  * @buffer: the buffer pointer
1024  * @count: data size to transfer
1025  *
1026  * Copies data from the internal output buffer to the given buffer.
1027  *
1028  * Call this in the interrupt handler when the midi output is ready,
1029  * and call snd_rawmidi_transmit_ack() after the transmission is
1030  * finished.
1031  *
1032  * Returns the size of copied data, or a negative error code on failure.
1033  */
1034 int snd_rawmidi_transmit_peek(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
1035 {
1036         unsigned long flags;
1037         int result, count1;
1038         snd_rawmidi_runtime_t *runtime = substream->runtime;
1039
1040         if (runtime->buffer == NULL) {
1041                 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1042                 return -EINVAL;
1043         }
1044         result = 0;
1045         spin_lock_irqsave(&runtime->lock, flags);
1046         if (runtime->avail >= runtime->buffer_size) {
1047                 /* warning: lowlevel layer MUST trigger down the hardware */
1048                 runtime->trigger = 0;
1049                 goto __skip;
1050         }
1051         if (count == 1) {       /* special case, faster code */
1052                 *buffer = runtime->buffer[runtime->hw_ptr];
1053                 result++;
1054         } else {
1055                 count1 = runtime->buffer_size - runtime->hw_ptr;
1056                 if (count1 > count)
1057                         count1 = count;
1058                 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1059                         count1 = runtime->buffer_size - runtime->avail;
1060                 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1061                 count -= count1;
1062                 result += count1;
1063                 if (count > 0)
1064                         memcpy(buffer + count1, runtime->buffer, count);
1065         }
1066       __skip:
1067         spin_unlock_irqrestore(&runtime->lock, flags);
1068         return result;
1069 }
1070
1071 /**
1072  * snd_rawmidi_transmit_ack - acknowledge the transmission
1073  * @substream: the rawmidi substream
1074  * @count: the tranferred count
1075  *
1076  * Advances the hardware pointer for the internal output buffer with
1077  * the given size and updates the condition.
1078  * Call after the transmission is finished.
1079  *
1080  * Returns the advanced size if successful, or a negative error code on failure.
1081  */
1082 int snd_rawmidi_transmit_ack(snd_rawmidi_substream_t * substream, int count)
1083 {
1084         unsigned long flags;
1085         snd_rawmidi_runtime_t *runtime = substream->runtime;
1086
1087         if (runtime->buffer == NULL) {
1088                 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1089                 return -EINVAL;
1090         }
1091         spin_lock_irqsave(&runtime->lock, flags);
1092         snd_assert(runtime->avail + count <= runtime->buffer_size, );
1093         runtime->hw_ptr += count;
1094         runtime->hw_ptr %= runtime->buffer_size;
1095         runtime->avail += count;
1096         substream->bytes += count;
1097         if (runtime->drain)
1098                 wake_up(&runtime->sleep);
1099         else
1100                 if (count > 0 && runtime->event == NULL)
1101                         if (snd_rawmidi_ready(substream))
1102                                 wake_up(&runtime->sleep);
1103         spin_unlock_irqrestore(&runtime->lock, flags);
1104         if (count > 0 && runtime->event)
1105                 runtime->event(substream);
1106         return count;
1107 }
1108
1109 /**
1110  * snd_rawmidi_transmit - copy from the buffer to the device
1111  * @substream: the rawmidi substream
1112  * @buf: the buffer pointer
1113  * @count: the data size to transfer
1114  * 
1115  * Copies data from the buffer to the device and advances the pointer.
1116  *
1117  * Returns the copied size if successful, or a negative error code on failure.
1118  */
1119 int snd_rawmidi_transmit(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
1120 {
1121         count = snd_rawmidi_transmit_peek(substream, buffer, count);
1122         if (count < 0)
1123                 return count;
1124         return snd_rawmidi_transmit_ack(substream, count);
1125 }
1126
1127 static long snd_rawmidi_kernel_write1(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count, int kernel)
1128 {
1129         unsigned long flags;
1130         long count1, result;
1131         snd_rawmidi_runtime_t *runtime = substream->runtime;
1132
1133         snd_assert(buf != NULL, return -EINVAL);
1134         snd_assert(runtime->buffer != NULL, return -EINVAL);
1135
1136         result = 0;
1137         spin_lock_irqsave(&runtime->lock, flags);
1138         if (substream->append) {
1139                 if ((long)runtime->avail < count) {
1140                         spin_unlock_irqrestore(&runtime->lock, flags);
1141                         return -EAGAIN;
1142                 }
1143         }
1144         while (count > 0 && runtime->avail > 0) {
1145                 count1 = runtime->buffer_size - runtime->appl_ptr;
1146                 if (count1 > count)
1147                         count1 = count;
1148                 if (count1 > (long)runtime->avail)
1149                         count1 = runtime->avail;
1150                 if (kernel) {
1151                         memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1152                 } else {
1153                         spin_unlock_irqrestore(&runtime->lock, flags);
1154                         if (copy_from_user(runtime->buffer + runtime->appl_ptr, buf, count1)) {
1155                                 spin_lock_irqsave(&runtime->lock, flags);
1156                                 result = result > 0 ? result : -EFAULT;
1157                                 goto __end;
1158                         }
1159                         spin_lock_irqsave(&runtime->lock, flags);
1160                 }
1161                 runtime->appl_ptr += count1;
1162                 runtime->appl_ptr %= runtime->buffer_size;
1163                 runtime->avail -= count1;
1164                 result += count1;
1165                 buf += count1;
1166                 count -= count1;
1167         }
1168       __end:
1169         if (result > 0)
1170                 runtime->trigger = 1;
1171         count1 = runtime->avail < runtime->buffer_size;
1172         spin_unlock_irqrestore(&runtime->lock, flags);
1173         if (count1)
1174                 substream->ops->trigger(substream, 1);
1175         return result;
1176 }
1177
1178 long snd_rawmidi_kernel_write(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count)
1179 {
1180         return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1181 }
1182
1183 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
1184 {
1185         long result, timeout;
1186         int count1;
1187         snd_rawmidi_file_t *rfile;
1188         snd_rawmidi_runtime_t *runtime;
1189         snd_rawmidi_substream_t *substream;
1190
1191         rfile = file->private_data;
1192         substream = rfile->output;
1193         runtime = substream->runtime;
1194         /* we cannot put an atomic message to our buffer */
1195         if (substream->append && count > runtime->buffer_size)
1196                 return -EIO;
1197         result = 0;
1198         while (count > 0) {
1199                 spin_lock_irq(&runtime->lock);
1200                 while (!snd_rawmidi_ready_append(substream, count)) {
1201                         wait_queue_t wait;
1202                         if (file->f_flags & O_NONBLOCK) {
1203                                 spin_unlock_irq(&runtime->lock);
1204                                 return result > 0 ? result : -EAGAIN;
1205                         }
1206                         init_waitqueue_entry(&wait, current);
1207                         add_wait_queue(&runtime->sleep, &wait);
1208                         set_current_state(TASK_INTERRUPTIBLE);
1209                         spin_unlock_irq(&runtime->lock);
1210                         timeout = schedule_timeout(30 * HZ);
1211                         remove_wait_queue(&runtime->sleep, &wait);
1212                         if (signal_pending(current))
1213                                 return result > 0 ? result : -ERESTARTSYS;
1214                         if (!runtime->avail && !timeout)
1215                                 return result > 0 ? result : -EIO;
1216                         spin_lock_irq(&runtime->lock);
1217                 }
1218                 spin_unlock_irq(&runtime->lock);
1219                 count1 = snd_rawmidi_kernel_write1(substream, buf, count, 0);
1220                 if (count1 < 0)
1221                         return result > 0 ? result : count1;
1222                 result += count1;
1223                 buf += count1;
1224                 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1225                         break;
1226                 count -= count1;
1227         }
1228         if (file->f_flags & O_SYNC) {
1229                 spin_lock_irq(&runtime->lock);
1230                 while (runtime->avail != runtime->buffer_size) {
1231                         wait_queue_t wait;
1232                         unsigned int last_avail = runtime->avail;
1233                         init_waitqueue_entry(&wait, current);
1234                         add_wait_queue(&runtime->sleep, &wait);
1235                         set_current_state(TASK_INTERRUPTIBLE);
1236                         spin_unlock_irq(&runtime->lock);
1237                         timeout = schedule_timeout(30 * HZ);
1238                         remove_wait_queue(&runtime->sleep, &wait);
1239                         if (signal_pending(current))
1240                                 return result > 0 ? result : -ERESTARTSYS;
1241                         if (runtime->avail == last_avail && !timeout)
1242                                 return result > 0 ? result : -EIO;
1243                         spin_lock_irq(&runtime->lock);
1244                 }
1245                 spin_unlock_irq(&runtime->lock);
1246         }
1247         return result;
1248 }
1249
1250 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1251 {
1252         snd_rawmidi_file_t *rfile;
1253         snd_rawmidi_runtime_t *runtime;
1254         unsigned int mask;
1255
1256         rfile = file->private_data;
1257         if (rfile->input != NULL) {
1258                 runtime = rfile->input->runtime;
1259                 runtime->trigger = 1;
1260                 rfile->input->ops->trigger(rfile->input, 1);
1261                 poll_wait(file, &runtime->sleep, wait);
1262         }
1263         if (rfile->output != NULL) {
1264                 runtime = rfile->output->runtime;
1265                 poll_wait(file, &runtime->sleep, wait);
1266         }
1267         mask = 0;
1268         if (rfile->input != NULL) {
1269                 if (snd_rawmidi_ready(rfile->input))
1270                         mask |= POLLIN | POLLRDNORM;
1271         }
1272         if (rfile->output != NULL) {
1273                 if (snd_rawmidi_ready(rfile->output))
1274                         mask |= POLLOUT | POLLWRNORM;
1275         }
1276         return mask;
1277 }
1278
1279 /*
1280
1281  */
1282
1283 static void snd_rawmidi_proc_info_read(snd_info_entry_t *entry,
1284                                        snd_info_buffer_t * buffer)
1285 {
1286         snd_rawmidi_t *rmidi;
1287         snd_rawmidi_substream_t *substream;
1288         snd_rawmidi_runtime_t *runtime;
1289         struct list_head *list;
1290
1291         rmidi = entry->private_data;
1292         snd_iprintf(buffer, "%s\n\n", rmidi->name);
1293         down(&rmidi->open_mutex);
1294         if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1295                 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
1296                         substream = list_entry(list, snd_rawmidi_substream_t, list);
1297                         snd_iprintf(buffer,
1298                                     "Output %d\n"
1299                                     "  Tx bytes     : %lu\n",
1300                                     substream->number,
1301                                     (unsigned long) substream->bytes);
1302                         if (substream->opened) {
1303                                 runtime = substream->runtime;
1304                                 snd_iprintf(buffer,
1305                                     "  Mode         : %s\n"
1306                                     "  Buffer size  : %lu\n"
1307                                     "  Avail        : %lu\n",
1308                                     runtime->oss ? "OSS compatible" : "native",
1309                                     (unsigned long) runtime->buffer_size,
1310                                     (unsigned long) runtime->avail);
1311                         }
1312                 }
1313         }
1314         if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1315                 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
1316                         substream = list_entry(list, snd_rawmidi_substream_t, list);
1317                         snd_iprintf(buffer,
1318                                     "Input %d\n"
1319                                     "  Rx bytes     : %lu\n",
1320                                     substream->number,
1321                                     (unsigned long) substream->bytes);
1322                         if (substream->opened) {
1323                                 runtime = substream->runtime;
1324                                 snd_iprintf(buffer,
1325                                             "  Buffer size  : %lu\n"
1326                                             "  Avail        : %lu\n"
1327                                             "  Overruns     : %lu\n",
1328                                             (unsigned long) runtime->buffer_size,
1329                                             (unsigned long) runtime->avail,
1330                                             (unsigned long) runtime->xruns);
1331                         }
1332                 }
1333         }
1334         up(&rmidi->open_mutex);
1335 }
1336
1337 /*
1338  *  Register functions
1339  */
1340
1341 static struct file_operations snd_rawmidi_f_ops =
1342 {
1343         .owner =        THIS_MODULE,
1344         .read =         snd_rawmidi_read,
1345         .write =        snd_rawmidi_write,
1346         .open =         snd_rawmidi_open,
1347         .release =      snd_rawmidi_release,
1348         .poll =         snd_rawmidi_poll,
1349         .ioctl =        snd_rawmidi_ioctl,
1350 };
1351
1352 static snd_minor_t snd_rawmidi_reg =
1353 {
1354         .comment =      "raw midi",
1355         .f_ops =        &snd_rawmidi_f_ops,
1356 };
1357
1358 static int snd_rawmidi_alloc_substreams(snd_rawmidi_t *rmidi,
1359                                         snd_rawmidi_str_t *stream,
1360                                         int direction,
1361                                         int count)
1362 {
1363         snd_rawmidi_substream_t *substream;
1364         int idx;
1365
1366         INIT_LIST_HEAD(&stream->substreams);
1367         for (idx = 0; idx < count; idx++) {
1368                 substream = kcalloc(1, sizeof(*substream), GFP_KERNEL);
1369                 if (substream == NULL)
1370                         return -ENOMEM;
1371                 substream->stream = direction;
1372                 substream->number = idx;
1373                 substream->rmidi = rmidi;
1374                 substream->pstr = stream;
1375                 list_add_tail(&substream->list, &stream->substreams);
1376                 stream->substream_count++;
1377         }
1378         return 0;
1379 }
1380
1381 /**
1382  * snd_rawmidi_new - create a rawmidi instance
1383  * @card: the card instance
1384  * @id: the id string
1385  * @device: the device index
1386  * @output_count: the number of output streams
1387  * @input_count: the number of input streams
1388  * @rrawmidi: the pointer to store the new rawmidi instance
1389  *
1390  * Creates a new rawmidi instance.
1391  * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1392  *
1393  * Returns zero if successful, or a negative error code on failure.
1394  */
1395 int snd_rawmidi_new(snd_card_t * card, char *id, int device,
1396                     int output_count, int input_count,
1397                     snd_rawmidi_t ** rrawmidi)
1398 {
1399         snd_rawmidi_t *rmidi;
1400         int err;
1401         static snd_device_ops_t ops = {
1402                 .dev_free = snd_rawmidi_dev_free,
1403                 .dev_register = snd_rawmidi_dev_register,
1404                 .dev_disconnect = snd_rawmidi_dev_disconnect,
1405                 .dev_unregister = snd_rawmidi_dev_unregister
1406         };
1407
1408         snd_assert(rrawmidi != NULL, return -EINVAL);
1409         *rrawmidi = NULL;
1410         snd_assert(card != NULL, return -ENXIO);
1411         rmidi = kcalloc(1, sizeof(*rmidi), GFP_KERNEL);
1412         if (rmidi == NULL)
1413                 return -ENOMEM;
1414         rmidi->card = card;
1415         rmidi->device = device;
1416         init_MUTEX(&rmidi->open_mutex);
1417         init_waitqueue_head(&rmidi->open_wait);
1418         if (id != NULL)
1419                 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1420         if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT], SNDRV_RAWMIDI_STREAM_INPUT, input_count)) < 0) {
1421                 snd_rawmidi_free(rmidi);
1422                 return err;
1423         }
1424         if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT], SNDRV_RAWMIDI_STREAM_OUTPUT, output_count)) < 0) {
1425                 snd_rawmidi_free(rmidi);
1426                 return err;
1427         }
1428         if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1429                 snd_rawmidi_free(rmidi);
1430                 return err;
1431         }
1432         *rrawmidi = rmidi;
1433         return 0;
1434 }
1435
1436 static void snd_rawmidi_free_substreams(snd_rawmidi_str_t *stream)
1437 {
1438         snd_rawmidi_substream_t *substream;
1439
1440         while (!list_empty(&stream->substreams)) {
1441                 substream = list_entry(stream->substreams.next, snd_rawmidi_substream_t, list);
1442                 list_del(&substream->list);
1443                 kfree(substream);
1444         }
1445 }
1446
1447 static int snd_rawmidi_free(snd_rawmidi_t *rmidi)
1448 {
1449         snd_assert(rmidi != NULL, return -ENXIO);       
1450         snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1451         snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1452         if (rmidi->private_free)
1453                 rmidi->private_free(rmidi);
1454         kfree(rmidi);
1455         return 0;
1456 }
1457
1458 static int snd_rawmidi_dev_free(snd_device_t *device)
1459 {
1460         snd_rawmidi_t *rmidi = device->device_data;
1461         return snd_rawmidi_free(rmidi);
1462 }
1463
1464 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1465 static void snd_rawmidi_dev_seq_free(snd_seq_device_t *device)
1466 {
1467         snd_rawmidi_t *rmidi = device->private_data;
1468         rmidi->seq_dev = NULL;
1469 }
1470 #endif
1471
1472 static int snd_rawmidi_dev_register(snd_device_t *device)
1473 {
1474         int idx, err;
1475         snd_info_entry_t *entry;
1476         char name[16];
1477         snd_rawmidi_t *rmidi = device->device_data;
1478
1479         if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1480                 return -ENOMEM;
1481         down(&register_mutex);
1482         idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1483         if (snd_rawmidi_devices[idx] != NULL) {
1484                 up(&register_mutex);
1485                 return -EBUSY;
1486         }
1487         snd_rawmidi_devices[idx] = rmidi;
1488         sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1489         if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1490                                        rmidi->card, rmidi->device,
1491                                        &snd_rawmidi_reg, name)) < 0) {
1492                 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1493                 snd_rawmidi_devices[idx] = NULL;
1494                 up(&register_mutex);
1495                 return err;
1496         }
1497         if (rmidi->ops && rmidi->ops->dev_register &&
1498             (err = rmidi->ops->dev_register(rmidi)) < 0) {
1499                 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1500                 snd_rawmidi_devices[idx] = NULL;
1501                 up(&register_mutex);
1502                 return err;
1503         }
1504 #ifdef CONFIG_SND_OSSEMUL
1505         rmidi->ossreg = 0;
1506         if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1507                 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1508                                             rmidi->card, 0, &snd_rawmidi_reg, name) < 0) {
1509                         snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1510                 } else {
1511                         rmidi->ossreg++;
1512 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1513                         snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1514 #endif
1515                 }
1516         }
1517         if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1518                 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1519                                             rmidi->card, 1, &snd_rawmidi_reg, name) < 0) {
1520                         snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1521                 } else {
1522                         rmidi->ossreg++;
1523                 }
1524         }
1525 #endif /* CONFIG_SND_OSSEMUL */
1526         up(&register_mutex);
1527         sprintf(name, "midi%d", rmidi->device);
1528         entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1529         if (entry) {
1530                 entry->private_data = rmidi;
1531                 entry->c.text.read_size = 1024;
1532                 entry->c.text.read = snd_rawmidi_proc_info_read;
1533                 if (snd_info_register(entry) < 0) {
1534                         snd_info_free_entry(entry);
1535                         entry = NULL;
1536                 }
1537         }
1538         rmidi->proc_entry = entry;
1539 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1540         if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1541                 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1542                         rmidi->seq_dev->private_data = rmidi;
1543                         rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1544                         sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1545                         snd_device_register(rmidi->card, rmidi->seq_dev);
1546                 }
1547         }
1548 #endif
1549         return 0;
1550 }
1551
1552 static int snd_rawmidi_dev_disconnect(snd_device_t *device)
1553 {
1554         snd_rawmidi_t *rmidi = device->device_data;
1555         int idx;
1556
1557         down(&register_mutex);
1558         idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1559         snd_rawmidi_devices[idx] = NULL;
1560         up(&register_mutex);
1561         return 0;
1562 }
1563
1564 static int snd_rawmidi_dev_unregister(snd_device_t *device)
1565 {
1566         int idx;
1567         snd_rawmidi_t *rmidi = device->device_data;
1568
1569         snd_assert(rmidi != NULL, return -ENXIO);
1570         down(&register_mutex);
1571         idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1572         snd_rawmidi_devices[idx] = NULL;
1573         if (rmidi->proc_entry) {
1574                 snd_info_unregister(rmidi->proc_entry);
1575                 rmidi->proc_entry = NULL;
1576         }
1577 #ifdef CONFIG_SND_OSSEMUL
1578         if (rmidi->ossreg) {
1579                 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1580                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1581 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1582                         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1583 #endif
1584                 }
1585                 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1586                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1587                 rmidi->ossreg = 0;
1588         }
1589 #endif /* CONFIG_SND_OSSEMUL */
1590         if (rmidi->ops && rmidi->ops->dev_unregister)
1591                 rmidi->ops->dev_unregister(rmidi);
1592         snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1593         up(&register_mutex);
1594 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1595         if (rmidi->seq_dev) {
1596                 snd_device_free(rmidi->card, rmidi->seq_dev);
1597                 rmidi->seq_dev = NULL;
1598         }
1599 #endif
1600         return snd_rawmidi_free(rmidi);
1601 }
1602
1603 /**
1604  * snd_rawmidi_set_ops - set the rawmidi operators
1605  * @rmidi: the rawmidi instance
1606  * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1607  * @ops: the operator table
1608  *
1609  * Sets the rawmidi operators for the given stream direction.
1610  */
1611 void snd_rawmidi_set_ops(snd_rawmidi_t *rmidi, int stream, snd_rawmidi_ops_t *ops)
1612 {
1613         struct list_head *list;
1614         snd_rawmidi_substream_t *substream;
1615         
1616         list_for_each(list, &rmidi->streams[stream].substreams) {
1617                 substream = list_entry(list, snd_rawmidi_substream_t, list);
1618                 substream->ops = ops;
1619         }
1620 }
1621
1622 /*
1623  *  ENTRY functions
1624  */
1625
1626 static int __init alsa_rawmidi_init(void)
1627 {
1628
1629         snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1630 #ifdef CONFIG_SND_OSSEMUL
1631         { int i;
1632         /* check device map table */
1633         for (i = 0; i < SNDRV_CARDS; i++) {
1634                 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1635                         snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1636                         midi_map[i] = 0;
1637                 }
1638                 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1639                         snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1640                         amidi_map[i] = 1;
1641                 }
1642         }
1643         }
1644 #endif /* CONFIG_SND_OSSEMUL */
1645         return 0;
1646 }
1647
1648 static void __exit alsa_rawmidi_exit(void)
1649 {
1650         snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1651 }
1652
1653 module_init(alsa_rawmidi_init)
1654 module_exit(alsa_rawmidi_exit)
1655
1656 EXPORT_SYMBOL(snd_rawmidi_output_params);
1657 EXPORT_SYMBOL(snd_rawmidi_input_params);
1658 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1659 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1660 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1661 EXPORT_SYMBOL(snd_rawmidi_receive);
1662 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1663 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1664 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1665 EXPORT_SYMBOL(snd_rawmidi_transmit);
1666 EXPORT_SYMBOL(snd_rawmidi_new);
1667 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1668 EXPORT_SYMBOL(snd_rawmidi_info);
1669 EXPORT_SYMBOL(snd_rawmidi_info_select);
1670 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1671 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1672 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1673 EXPORT_SYMBOL(snd_rawmidi_kernel_write);