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