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