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