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