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