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