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