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