upgrade to linux 2.6.10-1.12_FC2
[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 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, (long __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, (long __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 int snd_rawmidi_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
799                               unsigned int cmd, unsigned long arg)
800 {
801         void __user *argp = (void __user *)arg;
802         unsigned int tmp;
803
804         tmp = card->number * SNDRV_RAWMIDI_DEVICES;
805         switch (cmd) {
806         case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
807         {
808                 int device;
809                 
810                 if (get_user(device, (int __user *)argp))
811                         return -EFAULT;
812                 device = device < 0 ? 0 : device + 1;
813                 while (device < SNDRV_RAWMIDI_DEVICES) {
814                         if (snd_rawmidi_devices[tmp + device])
815                                 break;
816                         device++;
817                 }
818                 if (device == SNDRV_RAWMIDI_DEVICES)
819                         device = -1;
820                 if (put_user(device, (int __user *)argp))
821                         return -EFAULT;
822                 return 0;
823         }
824         case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
825         {
826                 int val;
827                 
828                 if (get_user(val, (int __user *)argp))
829                         return -EFAULT;
830                 control->prefer_rawmidi_subdevice = val;
831                 return 0;
832         }
833         case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
834                 return snd_rawmidi_info_select_user(card, argp);
835         }
836         return -ENOIOCTLCMD;
837 }
838
839 /**
840  * snd_rawmidi_receive - receive the input data from the device
841  * @substream: the rawmidi substream
842  * @buffer: the buffer pointer
843  * @count: the data size to read
844  *
845  * Reads the data from the internal buffer.
846  *
847  * Returns the size of read data, or a negative error code on failure.
848  */
849 int snd_rawmidi_receive(snd_rawmidi_substream_t * substream, const unsigned char *buffer, int count)
850 {
851         unsigned long flags;
852         int result = 0, count1;
853         snd_rawmidi_runtime_t *runtime = substream->runtime;
854
855         if (runtime->buffer == NULL) {
856                 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
857                 return -EINVAL;
858         }
859         spin_lock_irqsave(&runtime->lock, flags);
860         if (count == 1) {       /* special case, faster code */
861                 substream->bytes++;
862                 if (runtime->avail < runtime->buffer_size) {
863                         runtime->buffer[runtime->hw_ptr++] = buffer[0];
864                         runtime->hw_ptr %= runtime->buffer_size;
865                         runtime->avail++;
866                         result++;
867                 } else {
868                         runtime->xruns++;
869                 }
870         } else {
871                 substream->bytes += count;
872                 count1 = runtime->buffer_size - runtime->hw_ptr;
873                 if (count1 > count)
874                         count1 = count;
875                 if (count1 > (int)(runtime->buffer_size - runtime->avail))
876                         count1 = runtime->buffer_size - runtime->avail;
877                 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
878                 runtime->hw_ptr += count1;
879                 runtime->hw_ptr %= runtime->buffer_size;
880                 runtime->avail += count1;
881                 count -= count1;
882                 result += count1;
883                 if (count > 0) {
884                         buffer += count1;
885                         count1 = count;
886                         if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
887                                 count1 = runtime->buffer_size - runtime->avail;
888                                 runtime->xruns = count - count1;
889                         }
890                         if (count1 > 0) {
891                                 memcpy(runtime->buffer, buffer, count1);
892                                 runtime->hw_ptr = count1;
893                                 runtime->avail += count1;
894                                 result += count1;
895                         }
896                 }
897         }
898         if (result > 0 && runtime->event == NULL) {
899                 if (snd_rawmidi_ready(substream))
900                         wake_up(&runtime->sleep);
901         }
902         spin_unlock_irqrestore(&runtime->lock, flags);
903         if (result > 0 && runtime->event)
904                 runtime->event(substream);
905         return result;
906 }
907
908 static long snd_rawmidi_kernel_read1(snd_rawmidi_substream_t *substream,
909                                      unsigned char *buf, long count, int kernel)
910 {
911         unsigned long flags;
912         long result = 0, count1;
913         snd_rawmidi_runtime_t *runtime = substream->runtime;
914
915         while (count > 0 && runtime->avail) {
916                 count1 = runtime->buffer_size - runtime->appl_ptr;
917                 if (count1 > count)
918                         count1 = count;
919                 spin_lock_irqsave(&runtime->lock, flags);
920                 if (count1 > (int)runtime->avail)
921                         count1 = runtime->avail;
922                 if (kernel) {
923                         memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
924                 } else {
925                         spin_unlock_irqrestore(&runtime->lock, flags);
926                         if (copy_to_user(buf + result, runtime->buffer + runtime->appl_ptr, count1)) {
927                                 return result > 0 ? result : -EFAULT;
928                         }
929                         spin_lock_irqsave(&runtime->lock, flags);
930                 }
931                 runtime->appl_ptr += count1;
932                 runtime->appl_ptr %= runtime->buffer_size;
933                 runtime->avail -= count1;
934                 spin_unlock_irqrestore(&runtime->lock, flags);
935                 result += count1;
936                 count -= count1;
937         }
938         return result;
939 }
940
941 long snd_rawmidi_kernel_read(snd_rawmidi_substream_t *substream, unsigned char *buf, long count)
942 {
943         substream->runtime->trigger = 1;
944         substream->ops->trigger(substream, 1);
945         return snd_rawmidi_kernel_read1(substream, buf, count, 1);
946 }
947
948 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
949 {
950         long result;
951         int count1;
952         snd_rawmidi_file_t *rfile;
953         snd_rawmidi_substream_t *substream;
954         snd_rawmidi_runtime_t *runtime;
955
956         rfile = file->private_data;
957         substream = rfile->input;
958         if (substream == NULL)
959                 return -EIO;
960         runtime = substream->runtime;
961         runtime->trigger = 1;
962         substream->ops->trigger(substream, 1);
963         result = 0;
964         while (count > 0) {
965                 spin_lock_irq(&runtime->lock);
966                 while (!snd_rawmidi_ready(substream)) {
967                         wait_queue_t wait;
968                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
969                                 spin_unlock_irq(&runtime->lock);
970                                 return result > 0 ? result : -EAGAIN;
971                         }
972                         init_waitqueue_entry(&wait, current);
973                         add_wait_queue(&runtime->sleep, &wait);
974                         set_current_state(TASK_INTERRUPTIBLE);
975                         spin_unlock_irq(&runtime->lock);
976                         schedule();
977                         remove_wait_queue(&runtime->sleep, &wait);
978                         if (signal_pending(current))
979                                 return result > 0 ? result : -ERESTARTSYS;
980                         if (!runtime->avail)
981                                 return result > 0 ? result : -EIO;
982                         spin_lock_irq(&runtime->lock);
983                 }
984                 spin_unlock_irq(&runtime->lock);
985                 count1 = snd_rawmidi_kernel_read1(substream, buf, count, 0);
986                 if (count1 < 0)
987                         return result > 0 ? result : count1;
988                 result += count1;
989                 buf += count1;
990                 count -= count1;
991         }
992         return result;
993 }
994
995 /**
996  * snd_rawmidi_transmit_empty - check whether the output buffer is empty
997  * @substream: the rawmidi substream
998  * 
999  * Returns 1 if the internal output buffer is empty, 0 if not.
1000  */
1001 int snd_rawmidi_transmit_empty(snd_rawmidi_substream_t * substream)
1002 {
1003         snd_rawmidi_runtime_t *runtime = substream->runtime;
1004         int result;
1005         unsigned long flags;
1006
1007         if (runtime->buffer == NULL) {
1008                 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1009                 return 1;
1010         }
1011         spin_lock_irqsave(&runtime->lock, flags);
1012         result = runtime->avail >= runtime->buffer_size;
1013         if (result)
1014                 runtime->trigger = 1;
1015         spin_unlock_irqrestore(&runtime->lock, flags);
1016         return result;          
1017 }
1018
1019 /**
1020  * snd_rawmidi_transmit_peek - copy data from the internal buffer
1021  * @substream: the rawmidi substream
1022  * @buffer: the buffer pointer
1023  * @count: data size to transfer
1024  *
1025  * Copies data from the internal output buffer to the given buffer.
1026  *
1027  * Call this in the interrupt handler when the midi output is ready,
1028  * and call snd_rawmidi_transmit_ack() after the transmission is
1029  * finished.
1030  *
1031  * Returns the size of copied data, or a negative error code on failure.
1032  */
1033 int snd_rawmidi_transmit_peek(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
1034 {
1035         unsigned long flags;
1036         int result, count1;
1037         snd_rawmidi_runtime_t *runtime = substream->runtime;
1038
1039         if (runtime->buffer == NULL) {
1040                 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1041                 return -EINVAL;
1042         }
1043         result = 0;
1044         spin_lock_irqsave(&runtime->lock, flags);
1045         if (runtime->avail >= runtime->buffer_size) {
1046                 /* warning: lowlevel layer MUST trigger down the hardware */
1047                 runtime->trigger = 0;
1048                 goto __skip;
1049         }
1050         if (count == 1) {       /* special case, faster code */
1051                 *buffer = runtime->buffer[runtime->hw_ptr];
1052                 result++;
1053         } else {
1054                 count1 = runtime->buffer_size - runtime->hw_ptr;
1055                 if (count1 > count)
1056                         count1 = count;
1057                 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1058                         count1 = runtime->buffer_size - runtime->avail;
1059                 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1060                 count -= count1;
1061                 result += count1;
1062                 if (count > 0)
1063                         memcpy(buffer + count1, runtime->buffer, count);
1064         }
1065       __skip:
1066         spin_unlock_irqrestore(&runtime->lock, flags);
1067         return result;
1068 }
1069
1070 /**
1071  * snd_rawmidi_transmit_ack - acknowledge the transmission
1072  * @substream: the rawmidi substream
1073  * @count: the tranferred count
1074  *
1075  * Advances the hardware pointer for the internal output buffer with
1076  * the given size and updates the condition.
1077  * Call after the transmission is finished.
1078  *
1079  * Returns the advanced size if successful, or a negative error code on failure.
1080  */
1081 int snd_rawmidi_transmit_ack(snd_rawmidi_substream_t * substream, int count)
1082 {
1083         unsigned long flags;
1084         snd_rawmidi_runtime_t *runtime = substream->runtime;
1085
1086         if (runtime->buffer == NULL) {
1087                 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1088                 return -EINVAL;
1089         }
1090         spin_lock_irqsave(&runtime->lock, flags);
1091         snd_assert(runtime->avail + count <= runtime->buffer_size, );
1092         runtime->hw_ptr += count;
1093         runtime->hw_ptr %= runtime->buffer_size;
1094         runtime->avail += count;
1095         substream->bytes += count;
1096         if (runtime->drain)
1097                 wake_up(&runtime->sleep);
1098         else
1099                 if (count > 0 && runtime->event == NULL)
1100                         if (snd_rawmidi_ready(substream))
1101                                 wake_up(&runtime->sleep);
1102         spin_unlock_irqrestore(&runtime->lock, flags);
1103         if (count > 0 && runtime->event)
1104                 runtime->event(substream);
1105         return count;
1106 }
1107
1108 /**
1109  * snd_rawmidi_transmit - copy from the buffer to the device
1110  * @substream: the rawmidi substream
1111  * @buf: the buffer pointer
1112  * @count: the data size to transfer
1113  * 
1114  * Copies data from the buffer to the device and advances the pointer.
1115  *
1116  * Returns the copied size if successful, or a negative error code on failure.
1117  */
1118 int snd_rawmidi_transmit(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
1119 {
1120         count = snd_rawmidi_transmit_peek(substream, buffer, count);
1121         if (count < 0)
1122                 return count;
1123         return snd_rawmidi_transmit_ack(substream, count);
1124 }
1125
1126 static long snd_rawmidi_kernel_write1(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count, int kernel)
1127 {
1128         unsigned long flags;
1129         long count1, result;
1130         snd_rawmidi_runtime_t *runtime = substream->runtime;
1131
1132         snd_assert(buf != NULL, return -EINVAL);
1133         snd_assert(runtime->buffer != NULL, return -EINVAL);
1134
1135         result = 0;
1136         spin_lock_irqsave(&runtime->lock, flags);
1137         if (substream->append) {
1138                 if ((long)runtime->avail < count) {
1139                         spin_unlock_irqrestore(&runtime->lock, flags);
1140                         return -EAGAIN;
1141                 }
1142         }
1143         while (count > 0 && runtime->avail > 0) {
1144                 count1 = runtime->buffer_size - runtime->appl_ptr;
1145                 if (count1 > count)
1146                         count1 = count;
1147                 if (count1 > (long)runtime->avail)
1148                         count1 = runtime->avail;
1149                 if (kernel) {
1150                         memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1151                 } else {
1152                         spin_unlock_irqrestore(&runtime->lock, flags);
1153                         if (copy_from_user(runtime->buffer + runtime->appl_ptr, buf, count1)) {
1154                                 spin_lock_irqsave(&runtime->lock, flags);
1155                                 result = result > 0 ? result : -EFAULT;
1156                                 goto __end;
1157                         }
1158                         spin_lock_irqsave(&runtime->lock, flags);
1159                 }
1160                 runtime->appl_ptr += count1;
1161                 runtime->appl_ptr %= runtime->buffer_size;
1162                 runtime->avail -= count1;
1163                 result += count1;
1164                 buf += count1;
1165                 count -= count1;
1166         }
1167       __end:
1168         if (result > 0)
1169                 runtime->trigger = 1;
1170         count1 = runtime->avail < runtime->buffer_size;
1171         spin_unlock_irqrestore(&runtime->lock, flags);
1172         if (count1)
1173                 substream->ops->trigger(substream, 1);
1174         return result;
1175 }
1176
1177 long snd_rawmidi_kernel_write(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count)
1178 {
1179         return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1180 }
1181
1182 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
1183 {
1184         long result, timeout;
1185         int count1;
1186         snd_rawmidi_file_t *rfile;
1187         snd_rawmidi_runtime_t *runtime;
1188         snd_rawmidi_substream_t *substream;
1189
1190         rfile = file->private_data;
1191         substream = rfile->output;
1192         runtime = substream->runtime;
1193         /* we cannot put an atomic message to our buffer */
1194         if (substream->append && count > runtime->buffer_size)
1195                 return -EIO;
1196         result = 0;
1197         while (count > 0) {
1198                 spin_lock_irq(&runtime->lock);
1199                 while (!snd_rawmidi_ready_append(substream, count)) {
1200                         wait_queue_t wait;
1201                         if (file->f_flags & O_NONBLOCK) {
1202                                 spin_unlock_irq(&runtime->lock);
1203                                 return result > 0 ? result : -EAGAIN;
1204                         }
1205                         init_waitqueue_entry(&wait, current);
1206                         add_wait_queue(&runtime->sleep, &wait);
1207                         set_current_state(TASK_INTERRUPTIBLE);
1208                         spin_unlock_irq(&runtime->lock);
1209                         timeout = schedule_timeout(30 * HZ);
1210                         remove_wait_queue(&runtime->sleep, &wait);
1211                         if (signal_pending(current))
1212                                 return result > 0 ? result : -ERESTARTSYS;
1213                         if (!runtime->avail && !timeout)
1214                                 return result > 0 ? result : -EIO;
1215                         spin_lock_irq(&runtime->lock);
1216                 }
1217                 spin_unlock_irq(&runtime->lock);
1218                 count1 = snd_rawmidi_kernel_write1(substream, buf, count, 0);
1219                 if (count1 < 0)
1220                         return result > 0 ? result : count1;
1221                 result += count1;
1222                 buf += count1;
1223                 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1224                         break;
1225                 count -= count1;
1226         }
1227         if (file->f_flags & O_SYNC) {
1228                 spin_lock_irq(&runtime->lock);
1229                 while (runtime->avail != runtime->buffer_size) {
1230                         wait_queue_t wait;
1231                         unsigned int last_avail = runtime->avail;
1232                         init_waitqueue_entry(&wait, current);
1233                         add_wait_queue(&runtime->sleep, &wait);
1234                         set_current_state(TASK_INTERRUPTIBLE);
1235                         spin_unlock_irq(&runtime->lock);
1236                         timeout = schedule_timeout(30 * HZ);
1237                         remove_wait_queue(&runtime->sleep, &wait);
1238                         if (signal_pending(current))
1239                                 return result > 0 ? result : -ERESTARTSYS;
1240                         if (runtime->avail == last_avail && !timeout)
1241                                 return result > 0 ? result : -EIO;
1242                         spin_lock_irq(&runtime->lock);
1243                 }
1244                 spin_unlock_irq(&runtime->lock);
1245         }
1246         return result;
1247 }
1248
1249 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1250 {
1251         snd_rawmidi_file_t *rfile;
1252         snd_rawmidi_runtime_t *runtime;
1253         unsigned int mask;
1254
1255         rfile = file->private_data;
1256         if (rfile->input != NULL) {
1257                 runtime = rfile->input->runtime;
1258                 runtime->trigger = 1;
1259                 rfile->input->ops->trigger(rfile->input, 1);
1260                 poll_wait(file, &runtime->sleep, wait);
1261         }
1262         if (rfile->output != NULL) {
1263                 runtime = rfile->output->runtime;
1264                 poll_wait(file, &runtime->sleep, wait);
1265         }
1266         mask = 0;
1267         if (rfile->input != NULL) {
1268                 if (snd_rawmidi_ready(rfile->input))
1269                         mask |= POLLIN | POLLRDNORM;
1270         }
1271         if (rfile->output != NULL) {
1272                 if (snd_rawmidi_ready(rfile->output))
1273                         mask |= POLLOUT | POLLWRNORM;
1274         }
1275         return mask;
1276 }
1277
1278 /*
1279
1280  */
1281
1282 static void snd_rawmidi_proc_info_read(snd_info_entry_t *entry,
1283                                        snd_info_buffer_t * buffer)
1284 {
1285         snd_rawmidi_t *rmidi;
1286         snd_rawmidi_substream_t *substream;
1287         snd_rawmidi_runtime_t *runtime;
1288         struct list_head *list;
1289
1290         rmidi = entry->private_data;
1291         snd_iprintf(buffer, "%s\n\n", rmidi->name);
1292         down(&rmidi->open_mutex);
1293         if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1294                 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
1295                         substream = list_entry(list, snd_rawmidi_substream_t, list);
1296                         snd_iprintf(buffer,
1297                                     "Output %d\n"
1298                                     "  Tx bytes     : %lu\n",
1299                                     substream->number,
1300                                     (unsigned long) substream->bytes);
1301                         if (substream->opened) {
1302                                 runtime = substream->runtime;
1303                                 snd_iprintf(buffer,
1304                                     "  Mode         : %s\n"
1305                                     "  Buffer size  : %lu\n"
1306                                     "  Avail        : %lu\n",
1307                                     runtime->oss ? "OSS compatible" : "native",
1308                                     (unsigned long) runtime->buffer_size,
1309                                     (unsigned long) runtime->avail);
1310                         }
1311                 }
1312         }
1313         if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1314                 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
1315                         substream = list_entry(list, snd_rawmidi_substream_t, list);
1316                         snd_iprintf(buffer,
1317                                     "Input %d\n"
1318                                     "  Rx bytes     : %lu\n",
1319                                     substream->number,
1320                                     (unsigned long) substream->bytes);
1321                         if (substream->opened) {
1322                                 runtime = substream->runtime;
1323                                 snd_iprintf(buffer,
1324                                             "  Buffer size  : %lu\n"
1325                                             "  Avail        : %lu\n"
1326                                             "  Overruns     : %lu\n",
1327                                             (unsigned long) runtime->buffer_size,
1328                                             (unsigned long) runtime->avail,
1329                                             (unsigned long) runtime->xruns);
1330                         }
1331                 }
1332         }
1333         up(&rmidi->open_mutex);
1334 }
1335
1336 /*
1337  *  Register functions
1338  */
1339
1340 static struct file_operations snd_rawmidi_f_ops =
1341 {
1342         .owner =        THIS_MODULE,
1343         .read =         snd_rawmidi_read,
1344         .write =        snd_rawmidi_write,
1345         .open =         snd_rawmidi_open,
1346         .release =      snd_rawmidi_release,
1347         .poll =         snd_rawmidi_poll,
1348         .ioctl =        snd_rawmidi_ioctl,
1349 };
1350
1351 static snd_minor_t snd_rawmidi_reg =
1352 {
1353         .comment =      "raw midi",
1354         .f_ops =        &snd_rawmidi_f_ops,
1355 };
1356
1357 static int snd_rawmidi_alloc_substreams(snd_rawmidi_t *rmidi,
1358                                         snd_rawmidi_str_t *stream,
1359                                         int direction,
1360                                         int count)
1361 {
1362         snd_rawmidi_substream_t *substream;
1363         int idx;
1364
1365         INIT_LIST_HEAD(&stream->substreams);
1366         for (idx = 0; idx < count; idx++) {
1367                 substream = kcalloc(1, sizeof(*substream), GFP_KERNEL);
1368                 if (substream == NULL)
1369                         return -ENOMEM;
1370                 substream->stream = direction;
1371                 substream->number = idx;
1372                 substream->rmidi = rmidi;
1373                 substream->pstr = stream;
1374                 list_add_tail(&substream->list, &stream->substreams);
1375                 stream->substream_count++;
1376         }
1377         return 0;
1378 }
1379
1380 /**
1381  * snd_rawmidi_new - create a rawmidi instance
1382  * @card: the card instance
1383  * @id: the id string
1384  * @device: the device index
1385  * @output_count: the number of output streams
1386  * @input_count: the number of input streams
1387  * @rrawmidi: the pointer to store the new rawmidi instance
1388  *
1389  * Creates a new rawmidi instance.
1390  * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1391  *
1392  * Returns zero if successful, or a negative error code on failure.
1393  */
1394 int snd_rawmidi_new(snd_card_t * card, char *id, int device,
1395                     int output_count, int input_count,
1396                     snd_rawmidi_t ** rrawmidi)
1397 {
1398         snd_rawmidi_t *rmidi;
1399         int err;
1400         static snd_device_ops_t ops = {
1401                 .dev_free = snd_rawmidi_dev_free,
1402                 .dev_register = snd_rawmidi_dev_register,
1403                 .dev_disconnect = snd_rawmidi_dev_disconnect,
1404                 .dev_unregister = snd_rawmidi_dev_unregister
1405         };
1406
1407         snd_assert(rrawmidi != NULL, return -EINVAL);
1408         *rrawmidi = NULL;
1409         snd_assert(card != NULL, return -ENXIO);
1410         rmidi = kcalloc(1, sizeof(*rmidi), GFP_KERNEL);
1411         if (rmidi == NULL)
1412                 return -ENOMEM;
1413         rmidi->card = card;
1414         rmidi->device = device;
1415         init_MUTEX(&rmidi->open_mutex);
1416         init_waitqueue_head(&rmidi->open_wait);
1417         if (id != NULL)
1418                 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1419         if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT], SNDRV_RAWMIDI_STREAM_INPUT, input_count)) < 0) {
1420                 snd_rawmidi_free(rmidi);
1421                 return err;
1422         }
1423         if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT], SNDRV_RAWMIDI_STREAM_OUTPUT, output_count)) < 0) {
1424                 snd_rawmidi_free(rmidi);
1425                 return err;
1426         }
1427         if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1428                 snd_rawmidi_free(rmidi);
1429                 return err;
1430         }
1431         *rrawmidi = rmidi;
1432         return 0;
1433 }
1434
1435 static void snd_rawmidi_free_substreams(snd_rawmidi_str_t *stream)
1436 {
1437         snd_rawmidi_substream_t *substream;
1438
1439         while (!list_empty(&stream->substreams)) {
1440                 substream = list_entry(stream->substreams.next, snd_rawmidi_substream_t, list);
1441                 list_del(&substream->list);
1442                 kfree(substream);
1443         }
1444 }
1445
1446 static int snd_rawmidi_free(snd_rawmidi_t *rmidi)
1447 {
1448         snd_assert(rmidi != NULL, return -ENXIO);       
1449         snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1450         snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1451         if (rmidi->private_free)
1452                 rmidi->private_free(rmidi);
1453         kfree(rmidi);
1454         return 0;
1455 }
1456
1457 static int snd_rawmidi_dev_free(snd_device_t *device)
1458 {
1459         snd_rawmidi_t *rmidi = device->device_data;
1460         return snd_rawmidi_free(rmidi);
1461 }
1462
1463 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1464 static void snd_rawmidi_dev_seq_free(snd_seq_device_t *device)
1465 {
1466         snd_rawmidi_t *rmidi = device->private_data;
1467         rmidi->seq_dev = NULL;
1468 }
1469 #endif
1470
1471 static int snd_rawmidi_dev_register(snd_device_t *device)
1472 {
1473         int idx, err;
1474         snd_info_entry_t *entry;
1475         char name[16];
1476         snd_rawmidi_t *rmidi = device->device_data;
1477
1478         if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1479                 return -ENOMEM;
1480         down(&register_mutex);
1481         idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1482         if (snd_rawmidi_devices[idx] != NULL) {
1483                 up(&register_mutex);
1484                 return -EBUSY;
1485         }
1486         snd_rawmidi_devices[idx] = rmidi;
1487         sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1488         if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1489                                        rmidi->card, rmidi->device,
1490                                        &snd_rawmidi_reg, name)) < 0) {
1491                 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1492                 snd_rawmidi_devices[idx] = NULL;
1493                 up(&register_mutex);
1494                 return err;
1495         }
1496         if (rmidi->ops && rmidi->ops->dev_register &&
1497             (err = rmidi->ops->dev_register(rmidi)) < 0) {
1498                 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1499                 snd_rawmidi_devices[idx] = NULL;
1500                 up(&register_mutex);
1501                 return err;
1502         }
1503 #ifdef CONFIG_SND_OSSEMUL
1504         rmidi->ossreg = 0;
1505         if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1506                 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1507                                             rmidi->card, 0, &snd_rawmidi_reg, name) < 0) {
1508                         snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1509                 } else {
1510                         rmidi->ossreg++;
1511 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1512                         snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1513 #endif
1514                 }
1515         }
1516         if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1517                 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1518                                             rmidi->card, 1, &snd_rawmidi_reg, name) < 0) {
1519                         snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1520                 } else {
1521                         rmidi->ossreg++;
1522                 }
1523         }
1524 #endif /* CONFIG_SND_OSSEMUL */
1525         up(&register_mutex);
1526         sprintf(name, "midi%d", rmidi->device);
1527         entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1528         if (entry) {
1529                 entry->private_data = rmidi;
1530                 entry->c.text.read_size = 1024;
1531                 entry->c.text.read = snd_rawmidi_proc_info_read;
1532                 if (snd_info_register(entry) < 0) {
1533                         snd_info_free_entry(entry);
1534                         entry = NULL;
1535                 }
1536         }
1537         rmidi->proc_entry = entry;
1538 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1539         if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1540                 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1541                         rmidi->seq_dev->private_data = rmidi;
1542                         rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1543                         sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1544                         snd_device_register(rmidi->card, rmidi->seq_dev);
1545                 }
1546         }
1547 #endif
1548         return 0;
1549 }
1550
1551 static int snd_rawmidi_dev_disconnect(snd_device_t *device)
1552 {
1553         snd_rawmidi_t *rmidi = device->device_data;
1554         int idx;
1555
1556         down(&register_mutex);
1557         idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1558         snd_rawmidi_devices[idx] = NULL;
1559         up(&register_mutex);
1560         return 0;
1561 }
1562
1563 static int snd_rawmidi_dev_unregister(snd_device_t *device)
1564 {
1565         int idx;
1566         snd_rawmidi_t *rmidi = device->device_data;
1567
1568         snd_assert(rmidi != NULL, return -ENXIO);
1569         down(&register_mutex);
1570         idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1571         snd_rawmidi_devices[idx] = NULL;
1572         if (rmidi->proc_entry) {
1573                 snd_info_unregister(rmidi->proc_entry);
1574                 rmidi->proc_entry = NULL;
1575         }
1576 #ifdef CONFIG_SND_OSSEMUL
1577         if (rmidi->ossreg) {
1578                 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1579                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1580 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1581                         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1582 #endif
1583                 }
1584                 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1585                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1586                 rmidi->ossreg = 0;
1587         }
1588 #endif /* CONFIG_SND_OSSEMUL */
1589         if (rmidi->ops && rmidi->ops->dev_unregister)
1590                 rmidi->ops->dev_unregister(rmidi);
1591         snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1592         up(&register_mutex);
1593 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1594         if (rmidi->seq_dev) {
1595                 snd_device_free(rmidi->card, rmidi->seq_dev);
1596                 rmidi->seq_dev = NULL;
1597         }
1598 #endif
1599         return snd_rawmidi_free(rmidi);
1600 }
1601
1602 /**
1603  * snd_rawmidi_set_ops - set the rawmidi operators
1604  * @rmidi: the rawmidi instance
1605  * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1606  * @ops: the operator table
1607  *
1608  * Sets the rawmidi operators for the given stream direction.
1609  */
1610 void snd_rawmidi_set_ops(snd_rawmidi_t *rmidi, int stream, snd_rawmidi_ops_t *ops)
1611 {
1612         struct list_head *list;
1613         snd_rawmidi_substream_t *substream;
1614         
1615         list_for_each(list, &rmidi->streams[stream].substreams) {
1616                 substream = list_entry(list, snd_rawmidi_substream_t, list);
1617                 substream->ops = ops;
1618         }
1619 }
1620
1621 /*
1622  *  ENTRY functions
1623  */
1624
1625 static int __init alsa_rawmidi_init(void)
1626 {
1627
1628         snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1629 #ifdef CONFIG_SND_OSSEMUL
1630         { int i;
1631         /* check device map table */
1632         for (i = 0; i < SNDRV_CARDS; i++) {
1633                 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1634                         snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1635                         midi_map[i] = 0;
1636                 }
1637                 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1638                         snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1639                         amidi_map[i] = 1;
1640                 }
1641         }
1642         }
1643 #endif /* CONFIG_SND_OSSEMUL */
1644         return 0;
1645 }
1646
1647 static void __exit alsa_rawmidi_exit(void)
1648 {
1649         snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1650 }
1651
1652 module_init(alsa_rawmidi_init)
1653 module_exit(alsa_rawmidi_exit)
1654
1655 EXPORT_SYMBOL(snd_rawmidi_output_params);
1656 EXPORT_SYMBOL(snd_rawmidi_input_params);
1657 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1658 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1659 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1660 EXPORT_SYMBOL(snd_rawmidi_receive);
1661 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1662 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1663 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1664 EXPORT_SYMBOL(snd_rawmidi_transmit);
1665 EXPORT_SYMBOL(snd_rawmidi_new);
1666 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1667 EXPORT_SYMBOL(snd_rawmidi_info);
1668 EXPORT_SYMBOL(snd_rawmidi_info_select);
1669 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1670 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1671 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1672 EXPORT_SYMBOL(snd_rawmidi_kernel_write);