pandora: defconfig: update
[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 (rmidi->card->shutdown) {
428                         err = -ENODEV;
429                         break;
430                 }
431                 if (signal_pending(current)) {
432                         err = -ERESTARTSYS;
433                         break;
434                 }
435         }
436         remove_wait_queue(&rmidi->open_wait, &wait);
437         if (err < 0) {
438                 kfree(rawmidi_file);
439                 goto __error;
440         }
441 #ifdef CONFIG_SND_OSSEMUL
442         if (rawmidi_file->input && rawmidi_file->input->runtime)
443                 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
444         if (rawmidi_file->output && rawmidi_file->output->runtime)
445                 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
446 #endif
447         file->private_data = rawmidi_file;
448         mutex_unlock(&rmidi->open_mutex);
449         snd_card_unref(rmidi->card);
450         return 0;
451
452  __error:
453         snd_card_file_remove(card, file);
454  __error_card:
455         mutex_unlock(&rmidi->open_mutex);
456         module_put(rmidi->card->module);
457         snd_card_unref(rmidi->card);
458         return err;
459 }
460
461 static void close_substream(struct snd_rawmidi *rmidi,
462                             struct snd_rawmidi_substream *substream,
463                             int cleanup)
464 {
465         if (--substream->use_count)
466                 return;
467
468         if (cleanup) {
469                 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
470                         snd_rawmidi_input_trigger(substream, 0);
471                 else {
472                         if (substream->active_sensing) {
473                                 unsigned char buf = 0xfe;
474                                 /* sending single active sensing message
475                                  * to shut the device up
476                                  */
477                                 snd_rawmidi_kernel_write(substream, &buf, 1);
478                         }
479                         if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
480                                 snd_rawmidi_output_trigger(substream, 0);
481                 }
482         }
483         substream->ops->close(substream);
484         if (substream->runtime->private_free)
485                 substream->runtime->private_free(substream);
486         snd_rawmidi_runtime_free(substream);
487         substream->opened = 0;
488         substream->append = 0;
489         put_pid(substream->pid);
490         substream->pid = NULL;
491         rmidi->streams[substream->stream].substream_opened--;
492 }
493
494 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
495 {
496         struct snd_rawmidi *rmidi;
497
498         rmidi = rfile->rmidi;
499         mutex_lock(&rmidi->open_mutex);
500         if (rfile->input) {
501                 close_substream(rmidi, rfile->input, 1);
502                 rfile->input = NULL;
503         }
504         if (rfile->output) {
505                 close_substream(rmidi, rfile->output, 1);
506                 rfile->output = NULL;
507         }
508         rfile->rmidi = NULL;
509         mutex_unlock(&rmidi->open_mutex);
510         wake_up(&rmidi->open_wait);
511 }
512
513 /* called from sound/core/seq/seq_midi.c */
514 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
515 {
516         struct snd_rawmidi *rmidi;
517
518         if (snd_BUG_ON(!rfile))
519                 return -ENXIO;
520         
521         rmidi = rfile->rmidi;
522         rawmidi_release_priv(rfile);
523         module_put(rmidi->card->module);
524         return 0;
525 }
526
527 static int snd_rawmidi_release(struct inode *inode, struct file *file)
528 {
529         struct snd_rawmidi_file *rfile;
530         struct snd_rawmidi *rmidi;
531         struct module *module;
532
533         rfile = file->private_data;
534         rmidi = rfile->rmidi;
535         rawmidi_release_priv(rfile);
536         kfree(rfile);
537         module = rmidi->card->module;
538         snd_card_file_remove(rmidi->card, file);
539         module_put(module);
540         return 0;
541 }
542
543 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
544                             struct snd_rawmidi_info *info)
545 {
546         struct snd_rawmidi *rmidi;
547         
548         if (substream == NULL)
549                 return -ENODEV;
550         rmidi = substream->rmidi;
551         memset(info, 0, sizeof(*info));
552         info->card = rmidi->card->number;
553         info->device = rmidi->device;
554         info->subdevice = substream->number;
555         info->stream = substream->stream;
556         info->flags = rmidi->info_flags;
557         strcpy(info->id, rmidi->id);
558         strcpy(info->name, rmidi->name);
559         strcpy(info->subname, substream->name);
560         info->subdevices_count = substream->pstr->substream_count;
561         info->subdevices_avail = (substream->pstr->substream_count -
562                                   substream->pstr->substream_opened);
563         return 0;
564 }
565
566 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
567                                  struct snd_rawmidi_info __user * _info)
568 {
569         struct snd_rawmidi_info info;
570         int err;
571         if ((err = snd_rawmidi_info(substream, &info)) < 0)
572                 return err;
573         if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
574                 return -EFAULT;
575         return 0;
576 }
577
578 static int __snd_rawmidi_info_select(struct snd_card *card,
579                                      struct snd_rawmidi_info *info)
580 {
581         struct snd_rawmidi *rmidi;
582         struct snd_rawmidi_str *pstr;
583         struct snd_rawmidi_substream *substream;
584
585         rmidi = snd_rawmidi_search(card, info->device);
586         if (!rmidi)
587                 return -ENXIO;
588         if (info->stream < 0 || info->stream > 1)
589                 return -EINVAL;
590         pstr = &rmidi->streams[info->stream];
591         if (pstr->substream_count == 0)
592                 return -ENOENT;
593         if (info->subdevice >= pstr->substream_count)
594                 return -ENXIO;
595         list_for_each_entry(substream, &pstr->substreams, list) {
596                 if ((unsigned int)substream->number == info->subdevice)
597                         return snd_rawmidi_info(substream, info);
598         }
599         return -ENXIO;
600 }
601
602 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
603 {
604         int ret;
605
606         mutex_lock(&register_mutex);
607         ret = __snd_rawmidi_info_select(card, info);
608         mutex_unlock(&register_mutex);
609         return ret;
610 }
611
612 static int snd_rawmidi_info_select_user(struct snd_card *card,
613                                         struct snd_rawmidi_info __user *_info)
614 {
615         int err;
616         struct snd_rawmidi_info info;
617         if (get_user(info.device, &_info->device))
618                 return -EFAULT;
619         if (get_user(info.stream, &_info->stream))
620                 return -EFAULT;
621         if (get_user(info.subdevice, &_info->subdevice))
622                 return -EFAULT;
623         if ((err = snd_rawmidi_info_select(card, &info)) < 0)
624                 return err;
625         if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
626                 return -EFAULT;
627         return 0;
628 }
629
630 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
631                               struct snd_rawmidi_params * params)
632 {
633         char *newbuf;
634         struct snd_rawmidi_runtime *runtime = substream->runtime;
635         
636         if (substream->append && substream->use_count > 1)
637                 return -EBUSY;
638         snd_rawmidi_drain_output(substream);
639         if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
640                 return -EINVAL;
641         }
642         if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
643                 return -EINVAL;
644         }
645         if (params->buffer_size != runtime->buffer_size) {
646                 newbuf = krealloc(runtime->buffer, params->buffer_size,
647                                   GFP_KERNEL);
648                 if (!newbuf)
649                         return -ENOMEM;
650                 runtime->buffer = newbuf;
651                 runtime->buffer_size = params->buffer_size;
652                 runtime->avail = runtime->buffer_size;
653         }
654         runtime->avail_min = params->avail_min;
655         substream->active_sensing = !params->no_active_sensing;
656         return 0;
657 }
658
659 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
660                              struct snd_rawmidi_params * params)
661 {
662         char *newbuf;
663         struct snd_rawmidi_runtime *runtime = substream->runtime;
664
665         snd_rawmidi_drain_input(substream);
666         if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
667                 return -EINVAL;
668         }
669         if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
670                 return -EINVAL;
671         }
672         if (params->buffer_size != runtime->buffer_size) {
673                 newbuf = krealloc(runtime->buffer, params->buffer_size,
674                                   GFP_KERNEL);
675                 if (!newbuf)
676                         return -ENOMEM;
677                 runtime->buffer = newbuf;
678                 runtime->buffer_size = params->buffer_size;
679         }
680         runtime->avail_min = params->avail_min;
681         return 0;
682 }
683
684 static int snd_rawmidi_output_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_OUTPUT;
691         spin_lock_irq(&runtime->lock);
692         status->avail = runtime->avail;
693         spin_unlock_irq(&runtime->lock);
694         return 0;
695 }
696
697 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
698                                     struct snd_rawmidi_status * status)
699 {
700         struct snd_rawmidi_runtime *runtime = substream->runtime;
701
702         memset(status, 0, sizeof(*status));
703         status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
704         spin_lock_irq(&runtime->lock);
705         status->avail = runtime->avail;
706         status->xruns = runtime->xruns;
707         runtime->xruns = 0;
708         spin_unlock_irq(&runtime->lock);
709         return 0;
710 }
711
712 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
713 {
714         struct snd_rawmidi_file *rfile;
715         void __user *argp = (void __user *)arg;
716
717         rfile = file->private_data;
718         if (((cmd >> 8) & 0xff) != 'W')
719                 return -ENOTTY;
720         switch (cmd) {
721         case SNDRV_RAWMIDI_IOCTL_PVERSION:
722                 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
723         case SNDRV_RAWMIDI_IOCTL_INFO:
724         {
725                 int stream;
726                 struct snd_rawmidi_info __user *info = argp;
727                 if (get_user(stream, &info->stream))
728                         return -EFAULT;
729                 switch (stream) {
730                 case SNDRV_RAWMIDI_STREAM_INPUT:
731                         return snd_rawmidi_info_user(rfile->input, info);
732                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
733                         return snd_rawmidi_info_user(rfile->output, info);
734                 default:
735                         return -EINVAL;
736                 }
737         }
738         case SNDRV_RAWMIDI_IOCTL_PARAMS:
739         {
740                 struct snd_rawmidi_params params;
741                 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
742                         return -EFAULT;
743                 switch (params.stream) {
744                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
745                         if (rfile->output == NULL)
746                                 return -EINVAL;
747                         return snd_rawmidi_output_params(rfile->output, &params);
748                 case SNDRV_RAWMIDI_STREAM_INPUT:
749                         if (rfile->input == NULL)
750                                 return -EINVAL;
751                         return snd_rawmidi_input_params(rfile->input, &params);
752                 default:
753                         return -EINVAL;
754                 }
755         }
756         case SNDRV_RAWMIDI_IOCTL_STATUS:
757         {
758                 int err = 0;
759                 struct snd_rawmidi_status status;
760                 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
761                         return -EFAULT;
762                 switch (status.stream) {
763                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
764                         if (rfile->output == NULL)
765                                 return -EINVAL;
766                         err = snd_rawmidi_output_status(rfile->output, &status);
767                         break;
768                 case SNDRV_RAWMIDI_STREAM_INPUT:
769                         if (rfile->input == NULL)
770                                 return -EINVAL;
771                         err = snd_rawmidi_input_status(rfile->input, &status);
772                         break;
773                 default:
774                         return -EINVAL;
775                 }
776                 if (err < 0)
777                         return err;
778                 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
779                         return -EFAULT;
780                 return 0;
781         }
782         case SNDRV_RAWMIDI_IOCTL_DROP:
783         {
784                 int val;
785                 if (get_user(val, (int __user *) argp))
786                         return -EFAULT;
787                 switch (val) {
788                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
789                         if (rfile->output == NULL)
790                                 return -EINVAL;
791                         return snd_rawmidi_drop_output(rfile->output);
792                 default:
793                         return -EINVAL;
794                 }
795         }
796         case SNDRV_RAWMIDI_IOCTL_DRAIN:
797         {
798                 int val;
799                 if (get_user(val, (int __user *) argp))
800                         return -EFAULT;
801                 switch (val) {
802                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
803                         if (rfile->output == NULL)
804                                 return -EINVAL;
805                         return snd_rawmidi_drain_output(rfile->output);
806                 case SNDRV_RAWMIDI_STREAM_INPUT:
807                         if (rfile->input == NULL)
808                                 return -EINVAL;
809                         return snd_rawmidi_drain_input(rfile->input);
810                 default:
811                         return -EINVAL;
812                 }
813         }
814 #ifdef CONFIG_SND_DEBUG
815         default:
816                 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
817 #endif
818         }
819         return -ENOTTY;
820 }
821
822 static int snd_rawmidi_control_ioctl(struct snd_card *card,
823                                      struct snd_ctl_file *control,
824                                      unsigned int cmd,
825                                      unsigned long arg)
826 {
827         void __user *argp = (void __user *)arg;
828
829         switch (cmd) {
830         case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
831         {
832                 int device;
833                 
834                 if (get_user(device, (int __user *)argp))
835                         return -EFAULT;
836                 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
837                         device = SNDRV_RAWMIDI_DEVICES - 1;
838                 mutex_lock(&register_mutex);
839                 device = device < 0 ? 0 : device + 1;
840                 while (device < SNDRV_RAWMIDI_DEVICES) {
841                         if (snd_rawmidi_search(card, device))
842                                 break;
843                         device++;
844                 }
845                 if (device == SNDRV_RAWMIDI_DEVICES)
846                         device = -1;
847                 mutex_unlock(&register_mutex);
848                 if (put_user(device, (int __user *)argp))
849                         return -EFAULT;
850                 return 0;
851         }
852         case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
853         {
854                 int val;
855                 
856                 if (get_user(val, (int __user *)argp))
857                         return -EFAULT;
858                 control->prefer_rawmidi_subdevice = val;
859                 return 0;
860         }
861         case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
862                 return snd_rawmidi_info_select_user(card, argp);
863         }
864         return -ENOIOCTLCMD;
865 }
866
867 /**
868  * snd_rawmidi_receive - receive the input data from the device
869  * @substream: the rawmidi substream
870  * @buffer: the buffer pointer
871  * @count: the data size to read
872  *
873  * Reads the data from the internal buffer.
874  *
875  * Returns the size of read data, or a negative error code on failure.
876  */
877 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
878                         const unsigned char *buffer, int count)
879 {
880         unsigned long flags;
881         int result = 0, count1;
882         struct snd_rawmidi_runtime *runtime = substream->runtime;
883
884         if (!substream->opened)
885                 return -EBADFD;
886         if (runtime->buffer == NULL) {
887                 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
888                 return -EINVAL;
889         }
890         spin_lock_irqsave(&runtime->lock, flags);
891         if (count == 1) {       /* special case, faster code */
892                 substream->bytes++;
893                 if (runtime->avail < runtime->buffer_size) {
894                         runtime->buffer[runtime->hw_ptr++] = buffer[0];
895                         runtime->hw_ptr %= runtime->buffer_size;
896                         runtime->avail++;
897                         result++;
898                 } else {
899                         runtime->xruns++;
900                 }
901         } else {
902                 substream->bytes += count;
903                 count1 = runtime->buffer_size - runtime->hw_ptr;
904                 if (count1 > count)
905                         count1 = count;
906                 if (count1 > (int)(runtime->buffer_size - runtime->avail))
907                         count1 = runtime->buffer_size - runtime->avail;
908                 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
909                 runtime->hw_ptr += count1;
910                 runtime->hw_ptr %= runtime->buffer_size;
911                 runtime->avail += count1;
912                 count -= count1;
913                 result += count1;
914                 if (count > 0) {
915                         buffer += count1;
916                         count1 = count;
917                         if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
918                                 count1 = runtime->buffer_size - runtime->avail;
919                                 runtime->xruns += count - count1;
920                         }
921                         if (count1 > 0) {
922                                 memcpy(runtime->buffer, buffer, count1);
923                                 runtime->hw_ptr = count1;
924                                 runtime->avail += count1;
925                                 result += count1;
926                         }
927                 }
928         }
929         if (result > 0) {
930                 if (runtime->event)
931                         schedule_work(&runtime->event_work);
932                 else if (snd_rawmidi_ready(substream))
933                         wake_up(&runtime->sleep);
934         }
935         spin_unlock_irqrestore(&runtime->lock, flags);
936         return result;
937 }
938
939 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
940                                      unsigned char __user *userbuf,
941                                      unsigned char *kernelbuf, long count)
942 {
943         unsigned long flags;
944         long result = 0, count1;
945         struct snd_rawmidi_runtime *runtime = substream->runtime;
946         unsigned long appl_ptr;
947
948         spin_lock_irqsave(&runtime->lock, flags);
949         while (count > 0 && runtime->avail) {
950                 count1 = runtime->buffer_size - runtime->appl_ptr;
951                 if (count1 > count)
952                         count1 = count;
953                 if (count1 > (int)runtime->avail)
954                         count1 = runtime->avail;
955
956                 /* update runtime->appl_ptr before unlocking for userbuf */
957                 appl_ptr = runtime->appl_ptr;
958                 runtime->appl_ptr += count1;
959                 runtime->appl_ptr %= runtime->buffer_size;
960                 runtime->avail -= count1;
961
962                 if (kernelbuf)
963                         memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
964                 if (userbuf) {
965                         spin_unlock_irqrestore(&runtime->lock, flags);
966                         if (copy_to_user(userbuf + result,
967                                          runtime->buffer + appl_ptr, count1)) {
968                                 return result > 0 ? result : -EFAULT;
969                         }
970                         spin_lock_irqsave(&runtime->lock, flags);
971                 }
972                 result += count1;
973                 count -= count1;
974         }
975         spin_unlock_irqrestore(&runtime->lock, flags);
976         return result;
977 }
978
979 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
980                              unsigned char *buf, long count)
981 {
982         snd_rawmidi_input_trigger(substream, 1);
983         return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
984 }
985
986 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
987                                 loff_t *offset)
988 {
989         long result;
990         int count1;
991         struct snd_rawmidi_file *rfile;
992         struct snd_rawmidi_substream *substream;
993         struct snd_rawmidi_runtime *runtime;
994
995         rfile = file->private_data;
996         substream = rfile->input;
997         if (substream == NULL)
998                 return -EIO;
999         runtime = substream->runtime;
1000         snd_rawmidi_input_trigger(substream, 1);
1001         result = 0;
1002         while (count > 0) {
1003                 spin_lock_irq(&runtime->lock);
1004                 while (!snd_rawmidi_ready(substream)) {
1005                         wait_queue_t wait;
1006                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1007                                 spin_unlock_irq(&runtime->lock);
1008                                 return result > 0 ? result : -EAGAIN;
1009                         }
1010                         init_waitqueue_entry(&wait, current);
1011                         add_wait_queue(&runtime->sleep, &wait);
1012                         set_current_state(TASK_INTERRUPTIBLE);
1013                         spin_unlock_irq(&runtime->lock);
1014                         schedule();
1015                         remove_wait_queue(&runtime->sleep, &wait);
1016                         if (rfile->rmidi->card->shutdown)
1017                                 return -ENODEV;
1018                         if (signal_pending(current))
1019                                 return result > 0 ? result : -ERESTARTSYS;
1020                         if (!runtime->avail)
1021                                 return result > 0 ? result : -EIO;
1022                         spin_lock_irq(&runtime->lock);
1023                 }
1024                 spin_unlock_irq(&runtime->lock);
1025                 count1 = snd_rawmidi_kernel_read1(substream,
1026                                                   (unsigned char __user *)buf,
1027                                                   NULL/*kernelbuf*/,
1028                                                   count);
1029                 if (count1 < 0)
1030                         return result > 0 ? result : count1;
1031                 result += count1;
1032                 buf += count1;
1033                 count -= count1;
1034         }
1035         return result;
1036 }
1037
1038 /**
1039  * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1040  * @substream: the rawmidi substream
1041  * 
1042  * Returns 1 if the internal output buffer is empty, 0 if not.
1043  */
1044 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1045 {
1046         struct snd_rawmidi_runtime *runtime = substream->runtime;
1047         int result;
1048         unsigned long flags;
1049
1050         if (runtime->buffer == NULL) {
1051                 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1052                 return 1;
1053         }
1054         spin_lock_irqsave(&runtime->lock, flags);
1055         result = runtime->avail >= runtime->buffer_size;
1056         spin_unlock_irqrestore(&runtime->lock, flags);
1057         return result;          
1058 }
1059
1060 /**
1061  * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1062  * @substream: the rawmidi substream
1063  * @buffer: the buffer pointer
1064  * @count: data size to transfer
1065  *
1066  * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1067  */
1068 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1069                               unsigned char *buffer, int count)
1070 {
1071         int result, count1;
1072         struct snd_rawmidi_runtime *runtime = substream->runtime;
1073
1074         if (runtime->buffer == NULL) {
1075                 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1076                 return -EINVAL;
1077         }
1078         result = 0;
1079         if (runtime->avail >= runtime->buffer_size) {
1080                 /* warning: lowlevel layer MUST trigger down the hardware */
1081                 goto __skip;
1082         }
1083         if (count == 1) {       /* special case, faster code */
1084                 *buffer = runtime->buffer[runtime->hw_ptr];
1085                 result++;
1086         } else {
1087                 count1 = runtime->buffer_size - runtime->hw_ptr;
1088                 if (count1 > count)
1089                         count1 = count;
1090                 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1091                         count1 = runtime->buffer_size - runtime->avail;
1092                 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1093                 count -= count1;
1094                 result += count1;
1095                 if (count > 0) {
1096                         if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1097                                 count = runtime->buffer_size - runtime->avail - count1;
1098                         memcpy(buffer + count1, runtime->buffer, count);
1099                         result += count;
1100                 }
1101         }
1102       __skip:
1103         return result;
1104 }
1105 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1106
1107 /**
1108  * snd_rawmidi_transmit_peek - copy data from the internal buffer
1109  * @substream: the rawmidi substream
1110  * @buffer: the buffer pointer
1111  * @count: data size to transfer
1112  *
1113  * Copies data from the internal output buffer to the given buffer.
1114  *
1115  * Call this in the interrupt handler when the midi output is ready,
1116  * and call snd_rawmidi_transmit_ack() after the transmission is
1117  * finished.
1118  *
1119  * Return: The size of copied data, or a negative error code on failure.
1120  */
1121 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1122                               unsigned char *buffer, int count)
1123 {
1124         struct snd_rawmidi_runtime *runtime = substream->runtime;
1125         int result;
1126         unsigned long flags;
1127
1128         spin_lock_irqsave(&runtime->lock, flags);
1129         result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1130         spin_unlock_irqrestore(&runtime->lock, flags);
1131         return result;
1132 }
1133
1134 /**
1135  * __snd_rawmidi_transmit_ack - acknowledge the transmission
1136  * @substream: the rawmidi substream
1137  * @count: the tranferred count
1138  *
1139  * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1140  */
1141 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1142 {
1143         struct snd_rawmidi_runtime *runtime = substream->runtime;
1144
1145         if (runtime->buffer == NULL) {
1146                 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1147                 return -EINVAL;
1148         }
1149         snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1150         runtime->hw_ptr += count;
1151         runtime->hw_ptr %= runtime->buffer_size;
1152         runtime->avail += count;
1153         substream->bytes += count;
1154         if (count > 0) {
1155                 if (runtime->drain || snd_rawmidi_ready(substream))
1156                         wake_up(&runtime->sleep);
1157         }
1158         return count;
1159 }
1160 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1161
1162 /**
1163  * snd_rawmidi_transmit_ack - acknowledge the transmission
1164  * @substream: the rawmidi substream
1165  * @count: the transferred count
1166  *
1167  * Advances the hardware pointer for the internal output buffer with
1168  * the given size and updates the condition.
1169  * Call after the transmission is finished.
1170  *
1171  * Return: The advanced size if successful, or a negative error code on failure.
1172  */
1173 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1174 {
1175         struct snd_rawmidi_runtime *runtime = substream->runtime;
1176         int result;
1177         unsigned long flags;
1178
1179         spin_lock_irqsave(&runtime->lock, flags);
1180         result = __snd_rawmidi_transmit_ack(substream, count);
1181         spin_unlock_irqrestore(&runtime->lock, flags);
1182         return result;
1183 }
1184
1185 /**
1186  * snd_rawmidi_transmit - copy from the buffer to the device
1187  * @substream: the rawmidi substream
1188  * @buffer: the buffer pointer
1189  * @count: the data size to transfer
1190  * 
1191  * Copies data from the buffer to the device and advances the pointer.
1192  *
1193  * Returns the copied size if successful, or a negative error code on failure.
1194  */
1195 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1196                          unsigned char *buffer, int count)
1197 {
1198         struct snd_rawmidi_runtime *runtime = substream->runtime;
1199         int result;
1200         unsigned long flags;
1201
1202         spin_lock_irqsave(&runtime->lock, flags);
1203         if (!substream->opened)
1204                 result = -EBADFD;
1205         else {
1206                 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1207                 if (count <= 0)
1208                         result = count;
1209                 else
1210                         result = __snd_rawmidi_transmit_ack(substream, count);
1211         }
1212         spin_unlock_irqrestore(&runtime->lock, flags);
1213         return result;
1214 }
1215
1216 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1217                                       const unsigned char __user *userbuf,
1218                                       const unsigned char *kernelbuf,
1219                                       long count)
1220 {
1221         unsigned long flags;
1222         long count1, result;
1223         struct snd_rawmidi_runtime *runtime = substream->runtime;
1224         unsigned long appl_ptr;
1225
1226         if (!kernelbuf && !userbuf)
1227                 return -EINVAL;
1228         if (snd_BUG_ON(!runtime->buffer))
1229                 return -EINVAL;
1230
1231         result = 0;
1232         spin_lock_irqsave(&runtime->lock, flags);
1233         if (substream->append) {
1234                 if ((long)runtime->avail < count) {
1235                         spin_unlock_irqrestore(&runtime->lock, flags);
1236                         return -EAGAIN;
1237                 }
1238         }
1239         while (count > 0 && runtime->avail > 0) {
1240                 count1 = runtime->buffer_size - runtime->appl_ptr;
1241                 if (count1 > count)
1242                         count1 = count;
1243                 if (count1 > (long)runtime->avail)
1244                         count1 = runtime->avail;
1245
1246                 /* update runtime->appl_ptr before unlocking for userbuf */
1247                 appl_ptr = runtime->appl_ptr;
1248                 runtime->appl_ptr += count1;
1249                 runtime->appl_ptr %= runtime->buffer_size;
1250                 runtime->avail -= count1;
1251
1252                 if (kernelbuf)
1253                         memcpy(runtime->buffer + appl_ptr,
1254                                kernelbuf + result, count1);
1255                 else if (userbuf) {
1256                         spin_unlock_irqrestore(&runtime->lock, flags);
1257                         if (copy_from_user(runtime->buffer + appl_ptr,
1258                                            userbuf + result, count1)) {
1259                                 spin_lock_irqsave(&runtime->lock, flags);
1260                                 result = result > 0 ? result : -EFAULT;
1261                                 goto __end;
1262                         }
1263                         spin_lock_irqsave(&runtime->lock, flags);
1264                 }
1265                 result += count1;
1266                 count -= count1;
1267         }
1268       __end:
1269         count1 = runtime->avail < runtime->buffer_size;
1270         spin_unlock_irqrestore(&runtime->lock, flags);
1271         if (count1)
1272                 snd_rawmidi_output_trigger(substream, 1);
1273         return result;
1274 }
1275
1276 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1277                               const unsigned char *buf, long count)
1278 {
1279         return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1280 }
1281
1282 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1283                                  size_t count, loff_t *offset)
1284 {
1285         long result, timeout;
1286         int count1;
1287         struct snd_rawmidi_file *rfile;
1288         struct snd_rawmidi_runtime *runtime;
1289         struct snd_rawmidi_substream *substream;
1290
1291         rfile = file->private_data;
1292         substream = rfile->output;
1293         runtime = substream->runtime;
1294         /* we cannot put an atomic message to our buffer */
1295         if (substream->append && count > runtime->buffer_size)
1296                 return -EIO;
1297         result = 0;
1298         while (count > 0) {
1299                 spin_lock_irq(&runtime->lock);
1300                 while (!snd_rawmidi_ready_append(substream, count)) {
1301                         wait_queue_t wait;
1302                         if (file->f_flags & O_NONBLOCK) {
1303                                 spin_unlock_irq(&runtime->lock);
1304                                 return result > 0 ? result : -EAGAIN;
1305                         }
1306                         init_waitqueue_entry(&wait, current);
1307                         add_wait_queue(&runtime->sleep, &wait);
1308                         set_current_state(TASK_INTERRUPTIBLE);
1309                         spin_unlock_irq(&runtime->lock);
1310                         timeout = schedule_timeout(30 * HZ);
1311                         remove_wait_queue(&runtime->sleep, &wait);
1312                         if (rfile->rmidi->card->shutdown)
1313                                 return -ENODEV;
1314                         if (signal_pending(current))
1315                                 return result > 0 ? result : -ERESTARTSYS;
1316                         if (!runtime->avail && !timeout)
1317                                 return result > 0 ? result : -EIO;
1318                         spin_lock_irq(&runtime->lock);
1319                 }
1320                 spin_unlock_irq(&runtime->lock);
1321                 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1322                 if (count1 < 0)
1323                         return result > 0 ? result : count1;
1324                 result += count1;
1325                 buf += count1;
1326                 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1327                         break;
1328                 count -= count1;
1329         }
1330         if (file->f_flags & O_DSYNC) {
1331                 spin_lock_irq(&runtime->lock);
1332                 while (runtime->avail != runtime->buffer_size) {
1333                         wait_queue_t wait;
1334                         unsigned int last_avail = runtime->avail;
1335                         init_waitqueue_entry(&wait, current);
1336                         add_wait_queue(&runtime->sleep, &wait);
1337                         set_current_state(TASK_INTERRUPTIBLE);
1338                         spin_unlock_irq(&runtime->lock);
1339                         timeout = schedule_timeout(30 * HZ);
1340                         remove_wait_queue(&runtime->sleep, &wait);
1341                         if (signal_pending(current))
1342                                 return result > 0 ? result : -ERESTARTSYS;
1343                         if (runtime->avail == last_avail && !timeout)
1344                                 return result > 0 ? result : -EIO;
1345                         spin_lock_irq(&runtime->lock);
1346                 }
1347                 spin_unlock_irq(&runtime->lock);
1348         }
1349         return result;
1350 }
1351
1352 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1353 {
1354         struct snd_rawmidi_file *rfile;
1355         struct snd_rawmidi_runtime *runtime;
1356         unsigned int mask;
1357
1358         rfile = file->private_data;
1359         if (rfile->input != NULL) {
1360                 runtime = rfile->input->runtime;
1361                 snd_rawmidi_input_trigger(rfile->input, 1);
1362                 poll_wait(file, &runtime->sleep, wait);
1363         }
1364         if (rfile->output != NULL) {
1365                 runtime = rfile->output->runtime;
1366                 poll_wait(file, &runtime->sleep, wait);
1367         }
1368         mask = 0;
1369         if (rfile->input != NULL) {
1370                 if (snd_rawmidi_ready(rfile->input))
1371                         mask |= POLLIN | POLLRDNORM;
1372         }
1373         if (rfile->output != NULL) {
1374                 if (snd_rawmidi_ready(rfile->output))
1375                         mask |= POLLOUT | POLLWRNORM;
1376         }
1377         return mask;
1378 }
1379
1380 /*
1381  */
1382 #ifdef CONFIG_COMPAT
1383 #include "rawmidi_compat.c"
1384 #else
1385 #define snd_rawmidi_ioctl_compat        NULL
1386 #endif
1387
1388 /*
1389
1390  */
1391
1392 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1393                                        struct snd_info_buffer *buffer)
1394 {
1395         struct snd_rawmidi *rmidi;
1396         struct snd_rawmidi_substream *substream;
1397         struct snd_rawmidi_runtime *runtime;
1398
1399         rmidi = entry->private_data;
1400         snd_iprintf(buffer, "%s\n\n", rmidi->name);
1401         mutex_lock(&rmidi->open_mutex);
1402         if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1403                 list_for_each_entry(substream,
1404                                     &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1405                                     list) {
1406                         snd_iprintf(buffer,
1407                                     "Output %d\n"
1408                                     "  Tx bytes     : %lu\n",
1409                                     substream->number,
1410                                     (unsigned long) substream->bytes);
1411                         if (substream->opened) {
1412                                 snd_iprintf(buffer,
1413                                     "  Owner PID    : %d\n",
1414                                     pid_vnr(substream->pid));
1415                                 runtime = substream->runtime;
1416                                 snd_iprintf(buffer,
1417                                     "  Mode         : %s\n"
1418                                     "  Buffer size  : %lu\n"
1419                                     "  Avail        : %lu\n",
1420                                     runtime->oss ? "OSS compatible" : "native",
1421                                     (unsigned long) runtime->buffer_size,
1422                                     (unsigned long) runtime->avail);
1423                         }
1424                 }
1425         }
1426         if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1427                 list_for_each_entry(substream,
1428                                     &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1429                                     list) {
1430                         snd_iprintf(buffer,
1431                                     "Input %d\n"
1432                                     "  Rx bytes     : %lu\n",
1433                                     substream->number,
1434                                     (unsigned long) substream->bytes);
1435                         if (substream->opened) {
1436                                 snd_iprintf(buffer,
1437                                             "  Owner PID    : %d\n",
1438                                             pid_vnr(substream->pid));
1439                                 runtime = substream->runtime;
1440                                 snd_iprintf(buffer,
1441                                             "  Buffer size  : %lu\n"
1442                                             "  Avail        : %lu\n"
1443                                             "  Overruns     : %lu\n",
1444                                             (unsigned long) runtime->buffer_size,
1445                                             (unsigned long) runtime->avail,
1446                                             (unsigned long) runtime->xruns);
1447                         }
1448                 }
1449         }
1450         mutex_unlock(&rmidi->open_mutex);
1451 }
1452
1453 /*
1454  *  Register functions
1455  */
1456
1457 static const struct file_operations snd_rawmidi_f_ops =
1458 {
1459         .owner =        THIS_MODULE,
1460         .read =         snd_rawmidi_read,
1461         .write =        snd_rawmidi_write,
1462         .open =         snd_rawmidi_open,
1463         .release =      snd_rawmidi_release,
1464         .llseek =       no_llseek,
1465         .poll =         snd_rawmidi_poll,
1466         .unlocked_ioctl =       snd_rawmidi_ioctl,
1467         .compat_ioctl = snd_rawmidi_ioctl_compat,
1468 };
1469
1470 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1471                                         struct snd_rawmidi_str *stream,
1472                                         int direction,
1473                                         int count)
1474 {
1475         struct snd_rawmidi_substream *substream;
1476         int idx;
1477
1478         for (idx = 0; idx < count; idx++) {
1479                 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1480                 if (substream == NULL) {
1481                         snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1482                         return -ENOMEM;
1483                 }
1484                 substream->stream = direction;
1485                 substream->number = idx;
1486                 substream->rmidi = rmidi;
1487                 substream->pstr = stream;
1488                 list_add_tail(&substream->list, &stream->substreams);
1489                 stream->substream_count++;
1490         }
1491         return 0;
1492 }
1493
1494 /**
1495  * snd_rawmidi_new - create a rawmidi instance
1496  * @card: the card instance
1497  * @id: the id string
1498  * @device: the device index
1499  * @output_count: the number of output streams
1500  * @input_count: the number of input streams
1501  * @rrawmidi: the pointer to store the new rawmidi instance
1502  *
1503  * Creates a new rawmidi instance.
1504  * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1505  *
1506  * Returns zero if successful, or a negative error code on failure.
1507  */
1508 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1509                     int output_count, int input_count,
1510                     struct snd_rawmidi ** rrawmidi)
1511 {
1512         struct snd_rawmidi *rmidi;
1513         int err;
1514         static struct snd_device_ops ops = {
1515                 .dev_free = snd_rawmidi_dev_free,
1516                 .dev_register = snd_rawmidi_dev_register,
1517                 .dev_disconnect = snd_rawmidi_dev_disconnect,
1518         };
1519
1520         if (snd_BUG_ON(!card))
1521                 return -ENXIO;
1522         if (rrawmidi)
1523                 *rrawmidi = NULL;
1524         rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1525         if (rmidi == NULL) {
1526                 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1527                 return -ENOMEM;
1528         }
1529         rmidi->card = card;
1530         rmidi->device = device;
1531         mutex_init(&rmidi->open_mutex);
1532         init_waitqueue_head(&rmidi->open_wait);
1533         INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1534         INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1535
1536         if (id != NULL)
1537                 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1538         if ((err = snd_rawmidi_alloc_substreams(rmidi,
1539                                                 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1540                                                 SNDRV_RAWMIDI_STREAM_INPUT,
1541                                                 input_count)) < 0) {
1542                 snd_rawmidi_free(rmidi);
1543                 return err;
1544         }
1545         if ((err = snd_rawmidi_alloc_substreams(rmidi,
1546                                                 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1547                                                 SNDRV_RAWMIDI_STREAM_OUTPUT,
1548                                                 output_count)) < 0) {
1549                 snd_rawmidi_free(rmidi);
1550                 return err;
1551         }
1552         if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1553                 snd_rawmidi_free(rmidi);
1554                 return err;
1555         }
1556         if (rrawmidi)
1557                 *rrawmidi = rmidi;
1558         return 0;
1559 }
1560
1561 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1562 {
1563         struct snd_rawmidi_substream *substream;
1564
1565         while (!list_empty(&stream->substreams)) {
1566                 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1567                 list_del(&substream->list);
1568                 kfree(substream);
1569         }
1570 }
1571
1572 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1573 {
1574         if (!rmidi)
1575                 return 0;
1576
1577         snd_info_free_entry(rmidi->proc_entry);
1578         rmidi->proc_entry = NULL;
1579         mutex_lock(&register_mutex);
1580         if (rmidi->ops && rmidi->ops->dev_unregister)
1581                 rmidi->ops->dev_unregister(rmidi);
1582         mutex_unlock(&register_mutex);
1583
1584         snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1585         snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1586         if (rmidi->private_free)
1587                 rmidi->private_free(rmidi);
1588         kfree(rmidi);
1589         return 0;
1590 }
1591
1592 static int snd_rawmidi_dev_free(struct snd_device *device)
1593 {
1594         struct snd_rawmidi *rmidi = device->device_data;
1595         return snd_rawmidi_free(rmidi);
1596 }
1597
1598 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1599 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1600 {
1601         struct snd_rawmidi *rmidi = device->private_data;
1602         rmidi->seq_dev = NULL;
1603 }
1604 #endif
1605
1606 static int snd_rawmidi_dev_register(struct snd_device *device)
1607 {
1608         int err;
1609         struct snd_info_entry *entry;
1610         char name[16];
1611         struct snd_rawmidi *rmidi = device->device_data;
1612
1613         if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1614                 return -ENOMEM;
1615         mutex_lock(&register_mutex);
1616         if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1617                 mutex_unlock(&register_mutex);
1618                 return -EBUSY;
1619         }
1620         list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1621         mutex_unlock(&register_mutex);
1622         sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1623         if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1624                                        rmidi->card, rmidi->device,
1625                                        &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1626                 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1627                 mutex_lock(&register_mutex);
1628                 list_del(&rmidi->list);
1629                 mutex_unlock(&register_mutex);
1630                 return err;
1631         }
1632         if (rmidi->ops && rmidi->ops->dev_register &&
1633             (err = rmidi->ops->dev_register(rmidi)) < 0) {
1634                 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1635                 mutex_lock(&register_mutex);
1636                 list_del(&rmidi->list);
1637                 mutex_unlock(&register_mutex);
1638                 return err;
1639         }
1640 #ifdef CONFIG_SND_OSSEMUL
1641         rmidi->ossreg = 0;
1642         if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1643                 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1644                                             rmidi->card, 0, &snd_rawmidi_f_ops,
1645                                             rmidi, name) < 0) {
1646                         snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1647                 } else {
1648                         rmidi->ossreg++;
1649 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1650                         snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1651 #endif
1652                 }
1653         }
1654         if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1655                 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1656                                             rmidi->card, 1, &snd_rawmidi_f_ops,
1657                                             rmidi, name) < 0) {
1658                         snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1659                 } else {
1660                         rmidi->ossreg++;
1661                 }
1662         }
1663 #endif /* CONFIG_SND_OSSEMUL */
1664         sprintf(name, "midi%d", rmidi->device);
1665         entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1666         if (entry) {
1667                 entry->private_data = rmidi;
1668                 entry->c.text.read = snd_rawmidi_proc_info_read;
1669                 if (snd_info_register(entry) < 0) {
1670                         snd_info_free_entry(entry);
1671                         entry = NULL;
1672                 }
1673         }
1674         rmidi->proc_entry = entry;
1675 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1676         if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1677                 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1678                         rmidi->seq_dev->private_data = rmidi;
1679                         rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1680                         sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1681                         snd_device_register(rmidi->card, rmidi->seq_dev);
1682                 }
1683         }
1684 #endif
1685         return 0;
1686 }
1687
1688 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1689 {
1690         struct snd_rawmidi *rmidi = device->device_data;
1691         int dir;
1692
1693         mutex_lock(&register_mutex);
1694         mutex_lock(&rmidi->open_mutex);
1695         wake_up(&rmidi->open_wait);
1696         list_del_init(&rmidi->list);
1697         for (dir = 0; dir < 2; dir++) {
1698                 struct snd_rawmidi_substream *s;
1699                 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1700                         if (s->runtime)
1701                                 wake_up(&s->runtime->sleep);
1702                 }
1703         }
1704
1705 #ifdef CONFIG_SND_OSSEMUL
1706         if (rmidi->ossreg) {
1707                 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1708                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1709 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1710                         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1711 #endif
1712                 }
1713                 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1714                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1715                 rmidi->ossreg = 0;
1716         }
1717 #endif /* CONFIG_SND_OSSEMUL */
1718         snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1719         mutex_unlock(&rmidi->open_mutex);
1720         mutex_unlock(&register_mutex);
1721         return 0;
1722 }
1723
1724 /**
1725  * snd_rawmidi_set_ops - set the rawmidi operators
1726  * @rmidi: the rawmidi instance
1727  * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1728  * @ops: the operator table
1729  *
1730  * Sets the rawmidi operators for the given stream direction.
1731  */
1732 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1733                          struct snd_rawmidi_ops *ops)
1734 {
1735         struct snd_rawmidi_substream *substream;
1736         
1737         list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1738                 substream->ops = ops;
1739 }
1740
1741 /*
1742  *  ENTRY functions
1743  */
1744
1745 static int __init alsa_rawmidi_init(void)
1746 {
1747
1748         snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1749         snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1750 #ifdef CONFIG_SND_OSSEMUL
1751         { int i;
1752         /* check device map table */
1753         for (i = 0; i < SNDRV_CARDS; i++) {
1754                 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1755                         snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1756                         midi_map[i] = 0;
1757                 }
1758                 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1759                         snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1760                         amidi_map[i] = 1;
1761                 }
1762         }
1763         }
1764 #endif /* CONFIG_SND_OSSEMUL */
1765         return 0;
1766 }
1767
1768 static void __exit alsa_rawmidi_exit(void)
1769 {
1770         snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1771         snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1772 }
1773
1774 module_init(alsa_rawmidi_init)
1775 module_exit(alsa_rawmidi_exit)
1776
1777 EXPORT_SYMBOL(snd_rawmidi_output_params);
1778 EXPORT_SYMBOL(snd_rawmidi_input_params);
1779 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1780 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1781 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1782 EXPORT_SYMBOL(snd_rawmidi_receive);
1783 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1784 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1785 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1786 EXPORT_SYMBOL(snd_rawmidi_transmit);
1787 EXPORT_SYMBOL(snd_rawmidi_new);
1788 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1789 EXPORT_SYMBOL(snd_rawmidi_info_select);
1790 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1791 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1792 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1793 EXPORT_SYMBOL(snd_rawmidi_kernel_write);