patch-2_6_7-vs1_9_1_12
[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 __user * _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 __user *_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         void __user *argp = (void __user *)arg;
683
684         rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
685         if (((cmd >> 8) & 0xff) != 'W')
686                 return -ENOTTY;
687         switch (cmd) {
688         case SNDRV_RAWMIDI_IOCTL_PVERSION:
689                 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
690         case SNDRV_RAWMIDI_IOCTL_INFO:
691         {
692                 snd_rawmidi_stream_t stream;
693                 snd_rawmidi_info_t __user *info = argp;
694                 if (get_user(stream, &info->stream))
695                         return -EFAULT;
696                 switch (stream) {
697                 case SNDRV_RAWMIDI_STREAM_INPUT:
698                         return snd_rawmidi_info_user(rfile->input, info);
699                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
700                         return snd_rawmidi_info_user(rfile->output, info);
701                 default:
702                         return -EINVAL;
703                 }
704         }
705         case SNDRV_RAWMIDI_IOCTL_PARAMS:
706         {
707                 snd_rawmidi_params_t params;
708                 if (copy_from_user(&params, argp, sizeof(snd_rawmidi_params_t)))
709                         return -EFAULT;
710                 switch (params.stream) {
711                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
712                         if (rfile->output == NULL)
713                                 return -EINVAL;
714                         return snd_rawmidi_output_params(rfile->output, &params);
715                 case SNDRV_RAWMIDI_STREAM_INPUT:
716                         if (rfile->input == NULL)
717                                 return -EINVAL;
718                         return snd_rawmidi_input_params(rfile->input, &params);
719                 default:
720                         return -EINVAL;
721                 }
722         }
723         case SNDRV_RAWMIDI_IOCTL_STATUS:
724         {
725                 int err = 0;
726                 snd_rawmidi_status_t status;
727                 if (copy_from_user(&status, argp, sizeof(snd_rawmidi_status_t)))
728                         return -EFAULT;
729                 switch (status.stream) {
730                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
731                         if (rfile->output == NULL)
732                                 return -EINVAL;
733                         err = snd_rawmidi_output_status(rfile->output, &status);
734                         break;
735                 case SNDRV_RAWMIDI_STREAM_INPUT:
736                         if (rfile->input == NULL)
737                                 return -EINVAL;
738                         err = snd_rawmidi_input_status(rfile->input, &status);
739                         break;
740                 default:
741                         return -EINVAL;
742                 }
743                 if (err < 0)
744                         return err;
745                 if (copy_to_user(argp, &status, sizeof(snd_rawmidi_status_t)))
746                         return -EFAULT;
747                 return 0;
748         }
749         case SNDRV_RAWMIDI_IOCTL_DROP:
750         {
751                 int val;
752                 if (get_user(val, (long __user *) argp))
753                         return -EFAULT;
754                 switch (val) {
755                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
756                         if (rfile->output == NULL)
757                                 return -EINVAL;
758                         return snd_rawmidi_drop_output(rfile->output);
759                 default:
760                         return -EINVAL;
761                 }
762         }
763         case SNDRV_RAWMIDI_IOCTL_DRAIN:
764         {
765                 int val;
766                 if (get_user(val, (long __user *) argp))
767                         return -EFAULT;
768                 switch (val) {
769                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
770                         if (rfile->output == NULL)
771                                 return -EINVAL;
772                         return snd_rawmidi_drain_output(rfile->output);
773                 case SNDRV_RAWMIDI_STREAM_INPUT:
774                         if (rfile->input == NULL)
775                                 return -EINVAL;
776                         return snd_rawmidi_drain_input(rfile->input);
777                 default:
778                         return -EINVAL;
779                 }
780         }
781 #ifdef CONFIG_SND_DEBUG
782         default:
783                 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
784 #endif
785         }
786         return -ENOTTY;
787 }
788
789 int snd_rawmidi_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
790                               unsigned int cmd, unsigned long arg)
791 {
792         void __user *argp = (void __user *)arg;
793         unsigned int tmp;
794
795         tmp = card->number * SNDRV_RAWMIDI_DEVICES;
796         switch (cmd) {
797         case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
798         {
799                 int device;
800                 
801                 if (get_user(device, (int __user *)argp))
802                         return -EFAULT;
803                 device = device < 0 ? 0 : device + 1;
804                 while (device < SNDRV_RAWMIDI_DEVICES) {
805                         if (snd_rawmidi_devices[tmp + device])
806                                 break;
807                         device++;
808                 }
809                 if (device == SNDRV_RAWMIDI_DEVICES)
810                         device = -1;
811                 if (put_user(device, (int __user *)argp))
812                         return -EFAULT;
813                 return 0;
814         }
815         case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
816         {
817                 int val;
818                 
819                 if (get_user(val, (int __user *)argp))
820                         return -EFAULT;
821                 control->prefer_rawmidi_subdevice = val;
822                 return 0;
823         }
824         case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
825                 return snd_rawmidi_info_select_user(card, argp);
826         }
827         return -ENOIOCTLCMD;
828 }
829
830 /**
831  * snd_rawmidi_receive - receive the input data from the device
832  * @substream: the rawmidi substream
833  * @buffer: the buffer pointer
834  * @count: the data size to read
835  *
836  * Reads the data from the internal buffer.
837  *
838  * Returns the size of read data, or a negative error code on failure.
839  */
840 int snd_rawmidi_receive(snd_rawmidi_substream_t * substream, const unsigned char *buffer, int count)
841 {
842         unsigned long flags;
843         int result = 0, count1;
844         snd_rawmidi_runtime_t *runtime = substream->runtime;
845
846         if (runtime->buffer == NULL) {
847                 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
848                 return -EINVAL;
849         }
850         spin_lock_irqsave(&runtime->lock, flags);
851         if (count == 1) {       /* special case, faster code */
852                 substream->bytes++;
853                 if (runtime->avail < runtime->buffer_size) {
854                         runtime->buffer[runtime->hw_ptr++] = buffer[0];
855                         runtime->hw_ptr %= runtime->buffer_size;
856                         runtime->avail++;
857                         result++;
858                 } else {
859                         runtime->xruns++;
860                 }
861         } else {
862                 substream->bytes += count;
863                 count1 = runtime->buffer_size - runtime->hw_ptr;
864                 if (count1 > count)
865                         count1 = count;
866                 if (count1 > (int)(runtime->buffer_size - runtime->avail))
867                         count1 = runtime->buffer_size - runtime->avail;
868                 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
869                 runtime->hw_ptr += count1;
870                 runtime->hw_ptr %= runtime->buffer_size;
871                 runtime->avail += count1;
872                 count -= count1;
873                 result += count1;
874                 if (count > 0) {
875                         buffer += count1;
876                         count1 = count;
877                         if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
878                                 count1 = runtime->buffer_size - runtime->avail;
879                                 runtime->xruns = count - count1;
880                         }
881                         if (count1 > 0) {
882                                 memcpy(runtime->buffer, buffer, count1);
883                                 runtime->hw_ptr = count1;
884                                 runtime->avail += count1;
885                                 result += count1;
886                         }
887                 }
888         }
889         if (result > 0 && runtime->event == NULL) {
890                 if (snd_rawmidi_ready(substream))
891                         wake_up(&runtime->sleep);
892         }
893         spin_unlock_irqrestore(&runtime->lock, flags);
894         if (result > 0 && runtime->event)
895                 runtime->event(substream);
896         return result;
897 }
898
899 static long snd_rawmidi_kernel_read1(snd_rawmidi_substream_t *substream,
900                                      unsigned char *buf, long count, int kernel)
901 {
902         unsigned long flags;
903         long result = 0, count1;
904         snd_rawmidi_runtime_t *runtime = substream->runtime;
905
906         while (count > 0 && runtime->avail) {
907                 count1 = runtime->buffer_size - runtime->appl_ptr;
908                 if (count1 > count)
909                         count1 = count;
910                 spin_lock_irqsave(&runtime->lock, flags);
911                 if (count1 > (int)runtime->avail)
912                         count1 = runtime->avail;
913                 if (kernel) {
914                         memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
915                 } else {
916                         spin_unlock_irqrestore(&runtime->lock, flags);
917                         if (copy_to_user(buf + result, runtime->buffer + runtime->appl_ptr, count1)) {
918                                 return result > 0 ? result : -EFAULT;
919                         }
920                         spin_lock_irqsave(&runtime->lock, flags);
921                 }
922                 runtime->appl_ptr += count1;
923                 runtime->appl_ptr %= runtime->buffer_size;
924                 runtime->avail -= count1;
925                 spin_unlock_irqrestore(&runtime->lock, flags);
926                 result += count1;
927                 count -= count1;
928         }
929         return result;
930 }
931
932 long snd_rawmidi_kernel_read(snd_rawmidi_substream_t *substream, unsigned char *buf, long count)
933 {
934         substream->runtime->trigger = 1;
935         substream->ops->trigger(substream, 1);
936         return snd_rawmidi_kernel_read1(substream, buf, count, 1);
937 }
938
939 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
940 {
941         long result;
942         int count1;
943         snd_rawmidi_file_t *rfile;
944         snd_rawmidi_substream_t *substream;
945         snd_rawmidi_runtime_t *runtime;
946
947         rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
948         substream = rfile->input;
949         if (substream == NULL)
950                 return -EIO;
951         runtime = substream->runtime;
952         runtime->trigger = 1;
953         substream->ops->trigger(substream, 1);
954         result = 0;
955         while (count > 0) {
956                 spin_lock_irq(&runtime->lock);
957                 while (!snd_rawmidi_ready(substream)) {
958                         wait_queue_t wait;
959                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
960                                 spin_unlock_irq(&runtime->lock);
961                                 return result > 0 ? result : -EAGAIN;
962                         }
963                         init_waitqueue_entry(&wait, current);
964                         add_wait_queue(&runtime->sleep, &wait);
965                         set_current_state(TASK_INTERRUPTIBLE);
966                         spin_unlock_irq(&runtime->lock);
967                         schedule();
968                         remove_wait_queue(&runtime->sleep, &wait);
969                         if (signal_pending(current))
970                                 return result > 0 ? result : -ERESTARTSYS;
971                         if (!runtime->avail)
972                                 return result > 0 ? result : -EIO;
973                         spin_lock_irq(&runtime->lock);
974                 }
975                 spin_unlock_irq(&runtime->lock);
976                 count1 = snd_rawmidi_kernel_read1(substream, buf, count, 0);
977                 result += count1;
978                 buf += count1;
979                 count -= count1;
980         }
981         return result;
982 }
983
984 /**
985  * snd_rawmidi_transmit_empty - check whether the output buffer is empty
986  * @substream: the rawmidi substream
987  * 
988  * Returns 1 if the internal output buffer is empty, 0 if not.
989  */
990 int snd_rawmidi_transmit_empty(snd_rawmidi_substream_t * substream)
991 {
992         snd_rawmidi_runtime_t *runtime = substream->runtime;
993         int result;
994         unsigned long flags;
995
996         if (runtime->buffer == NULL) {
997                 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
998                 return 1;
999         }
1000         spin_lock_irqsave(&runtime->lock, flags);
1001         result = runtime->avail >= runtime->buffer_size;
1002         if (result)
1003                 runtime->trigger = 1;
1004         spin_unlock_irqrestore(&runtime->lock, flags);
1005         return result;          
1006 }
1007
1008 /**
1009  * snd_rawmidi_transmit_peek - copy data from the internal buffer
1010  * @substream: the rawmidi substream
1011  * @buffer: the buffer pointer
1012  * @count: data size to transfer
1013  *
1014  * Copies data from the internal output buffer to the given buffer.
1015  *
1016  * Call this in the interrupt handler when the midi output is ready,
1017  * and call snd_rawmidi_transmit_ack() after the transmission is
1018  * finished.
1019  *
1020  * Returns the size of copied data, or a negative error code on failure.
1021  */
1022 int snd_rawmidi_transmit_peek(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
1023 {
1024         unsigned long flags;
1025         int result, count1;
1026         snd_rawmidi_runtime_t *runtime = substream->runtime;
1027
1028         if (runtime->buffer == NULL) {
1029                 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1030                 return -EINVAL;
1031         }
1032         result = 0;
1033         spin_lock_irqsave(&runtime->lock, flags);
1034         if (runtime->avail >= runtime->buffer_size) {
1035                 /* warning: lowlevel layer MUST trigger down the hardware */
1036                 runtime->trigger = 0;
1037                 goto __skip;
1038         }
1039         if (count == 1) {       /* special case, faster code */
1040                 *buffer = runtime->buffer[runtime->hw_ptr];
1041                 result++;
1042         } else {
1043                 count1 = runtime->buffer_size - runtime->hw_ptr;
1044                 if (count1 > count)
1045                         count1 = count;
1046                 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1047                         count1 = runtime->buffer_size - runtime->avail;
1048                 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1049                 count -= count1;
1050                 result += count1;
1051                 if (count > 0)
1052                         memcpy(buffer + count1, runtime->buffer, count);
1053         }
1054       __skip:
1055         spin_unlock_irqrestore(&runtime->lock, flags);
1056         return result;
1057 }
1058
1059 /**
1060  * snd_rawmidi_transmit_ack - acknowledge the transmission
1061  * @substream: the rawmidi substream
1062  * @count: the tranferred count
1063  *
1064  * Advances the hardware pointer for the internal output buffer with
1065  * the given size and updates the condition.
1066  * Call after the transmission is finished.
1067  *
1068  * Returns the advanced size if successful, or a negative error code on failure.
1069  */
1070 int snd_rawmidi_transmit_ack(snd_rawmidi_substream_t * substream, int count)
1071 {
1072         unsigned long flags;
1073         snd_rawmidi_runtime_t *runtime = substream->runtime;
1074
1075         if (runtime->buffer == NULL) {
1076                 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1077                 return -EINVAL;
1078         }
1079         spin_lock_irqsave(&runtime->lock, flags);
1080         snd_assert(runtime->avail + count <= runtime->buffer_size, );
1081         runtime->hw_ptr += count;
1082         runtime->hw_ptr %= runtime->buffer_size;
1083         runtime->avail += count;
1084         substream->bytes += count;
1085         if (runtime->drain)
1086                 wake_up(&runtime->sleep);
1087         else
1088                 if (count > 0 && runtime->event == NULL)
1089                         if (snd_rawmidi_ready(substream))
1090                                 wake_up(&runtime->sleep);
1091         spin_unlock_irqrestore(&runtime->lock, flags);
1092         if (count > 0 && runtime->event)
1093                 runtime->event(substream);
1094         return count;
1095 }
1096
1097 /**
1098  * snd_rawmidi_transmit - copy from the buffer to the device
1099  * @substream: the rawmidi substream
1100  * @buf: the buffer pointer
1101  * @count: the data size to transfer
1102  * 
1103  * Copies data from the buffer to the device and advances the pointer.
1104  *
1105  * Returns the copied size if successful, or a negative error code on failure.
1106  */
1107 int snd_rawmidi_transmit(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
1108 {
1109         count = snd_rawmidi_transmit_peek(substream, buffer, count);
1110         if (count < 0)
1111                 return count;
1112         return snd_rawmidi_transmit_ack(substream, count);
1113 }
1114
1115 static long snd_rawmidi_kernel_write1(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count, int kernel)
1116 {
1117         unsigned long flags;
1118         long count1, result;
1119         snd_rawmidi_runtime_t *runtime = substream->runtime;
1120
1121         snd_assert(buf != NULL, return -EINVAL);
1122         snd_assert(runtime->buffer != NULL, return -EINVAL);
1123
1124         result = 0;
1125         spin_lock_irqsave(&runtime->lock, flags);
1126         if (substream->append) {
1127                 if ((long)runtime->avail < count) {
1128                         spin_unlock_irqrestore(&runtime->lock, flags);
1129                         return -EAGAIN;
1130                 }
1131         }
1132         while (count > 0 && runtime->avail > 0) {
1133                 count1 = runtime->buffer_size - runtime->appl_ptr;
1134                 if (count1 > count)
1135                         count1 = count;
1136                 if (count1 > (long)runtime->avail)
1137                         count1 = runtime->avail;
1138                 if (kernel) {
1139                         memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1140                 } else {
1141                         spin_unlock_irqrestore(&runtime->lock, flags);
1142                         if (copy_from_user(runtime->buffer + runtime->appl_ptr, buf, count1)) {
1143                                 spin_lock_irqsave(&runtime->lock, flags);
1144                                 result = result > 0 ? result : -EFAULT;
1145                                 goto __end;
1146                         }
1147                         spin_lock_irqsave(&runtime->lock, flags);
1148                 }
1149                 runtime->appl_ptr += count1;
1150                 runtime->appl_ptr %= runtime->buffer_size;
1151                 runtime->avail -= count1;
1152                 result += count1;
1153                 buf += count1;
1154                 count -= count1;
1155         }
1156       __end:
1157         if (result > 0)
1158                 runtime->trigger = 1;
1159         count1 = runtime->avail < runtime->buffer_size;
1160         spin_unlock_irqrestore(&runtime->lock, flags);
1161         if (count1)
1162                 substream->ops->trigger(substream, 1);
1163         return result;
1164 }
1165
1166 long snd_rawmidi_kernel_write(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count)
1167 {
1168         return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1169 }
1170
1171 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
1172 {
1173         long result, timeout;
1174         int count1;
1175         snd_rawmidi_file_t *rfile;
1176         snd_rawmidi_runtime_t *runtime;
1177         snd_rawmidi_substream_t *substream;
1178
1179         rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
1180         substream = rfile->output;
1181         runtime = substream->runtime;
1182         /* we cannot put an atomic message to our buffer */
1183         if (substream->append && count > runtime->buffer_size)
1184                 return -EIO;
1185         result = 0;
1186         while (count > 0) {
1187                 spin_lock_irq(&runtime->lock);
1188                 while (!snd_rawmidi_ready_append(substream, count)) {
1189                         wait_queue_t wait;
1190                         if (file->f_flags & O_NONBLOCK) {
1191                                 spin_unlock_irq(&runtime->lock);
1192                                 return result > 0 ? result : -EAGAIN;
1193                         }
1194                         init_waitqueue_entry(&wait, current);
1195                         add_wait_queue(&runtime->sleep, &wait);
1196                         set_current_state(TASK_INTERRUPTIBLE);
1197                         spin_unlock_irq(&runtime->lock);
1198                         timeout = schedule_timeout(30 * HZ);
1199                         remove_wait_queue(&runtime->sleep, &wait);
1200                         if (signal_pending(current))
1201                                 return result > 0 ? result : -ERESTARTSYS;
1202                         if (!runtime->avail && !timeout)
1203                                 return result > 0 ? result : -EIO;
1204                         spin_lock_irq(&runtime->lock);
1205                 }
1206                 spin_unlock_irq(&runtime->lock);
1207                 count1 = snd_rawmidi_kernel_write1(substream, buf, count, 0);
1208                 if (count1 < 0)
1209                         continue;
1210                 result += count1;
1211                 buf += count1;
1212                 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1213                         break;
1214                 count -= count1;
1215         }
1216         while (file->f_flags & O_SYNC) {
1217                 spin_lock_irq(&runtime->lock);
1218                 while (runtime->avail != runtime->buffer_size) {
1219                         wait_queue_t wait;
1220                         unsigned int last_avail = runtime->avail;
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 == last_avail && !timeout)
1230                                 return result > 0 ? result : -EIO;
1231                         spin_lock_irq(&runtime->lock);
1232                 }
1233                 spin_unlock_irq(&runtime->lock);
1234         }
1235         return result;
1236 }
1237
1238 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1239 {
1240         snd_rawmidi_file_t *rfile;
1241         snd_rawmidi_runtime_t *runtime;
1242         unsigned int mask;
1243
1244         rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return 0);
1245         if (rfile->input != NULL) {
1246                 runtime = rfile->input->runtime;
1247                 runtime->trigger = 1;
1248                 rfile->input->ops->trigger(rfile->input, 1);
1249                 poll_wait(file, &runtime->sleep, wait);
1250         }
1251         if (rfile->output != NULL) {
1252                 runtime = rfile->output->runtime;
1253                 poll_wait(file, &runtime->sleep, wait);
1254         }
1255         mask = 0;
1256         if (rfile->input != NULL) {
1257                 if (snd_rawmidi_ready(rfile->input))
1258                         mask |= POLLIN | POLLRDNORM;
1259         }
1260         if (rfile->output != NULL) {
1261                 if (snd_rawmidi_ready(rfile->output))
1262                         mask |= POLLOUT | POLLWRNORM;
1263         }
1264         return mask;
1265 }
1266
1267 /*
1268
1269  */
1270
1271 static void snd_rawmidi_proc_info_read(snd_info_entry_t *entry,
1272                                        snd_info_buffer_t * buffer)
1273 {
1274         snd_rawmidi_t *rmidi;
1275         snd_rawmidi_substream_t *substream;
1276         snd_rawmidi_runtime_t *runtime;
1277         struct list_head *list;
1278
1279         rmidi = snd_magic_cast(snd_rawmidi_t, entry->private_data, return);
1280         snd_iprintf(buffer, "%s\n\n", rmidi->name);
1281         down(&rmidi->open_mutex);
1282         if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1283                 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
1284                         substream = list_entry(list, snd_rawmidi_substream_t, list);
1285                         snd_iprintf(buffer,
1286                                     "Output %d\n"
1287                                     "  Tx bytes     : %lu\n",
1288                                     substream->number,
1289                                     (unsigned long) substream->bytes);
1290                         if (substream->opened) {
1291                                 runtime = substream->runtime;
1292                                 snd_iprintf(buffer,
1293                                     "  Mode         : %s\n"
1294                                     "  Buffer size  : %lu\n"
1295                                     "  Avail        : %lu\n",
1296                                     runtime->oss ? "OSS compatible" : "native",
1297                                     (unsigned long) runtime->buffer_size,
1298                                     (unsigned long) runtime->avail);
1299                         }
1300                 }
1301         }
1302         if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1303                 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
1304                         substream = list_entry(list, snd_rawmidi_substream_t, list);
1305                         snd_iprintf(buffer,
1306                                     "Input %d\n"
1307                                     "  Rx bytes     : %lu\n",
1308                                     substream->number,
1309                                     (unsigned long) substream->bytes);
1310                         if (substream->opened) {
1311                                 runtime = substream->runtime;
1312                                 snd_iprintf(buffer,
1313                                             "  Buffer size  : %lu\n"
1314                                             "  Avail        : %lu\n"
1315                                             "  Overruns     : %lu\n",
1316                                             (unsigned long) runtime->buffer_size,
1317                                             (unsigned long) runtime->avail,
1318                                             (unsigned long) runtime->xruns);
1319                         }
1320                 }
1321         }
1322         up(&rmidi->open_mutex);
1323 }
1324
1325 /*
1326  *  Register functions
1327  */
1328
1329 static struct file_operations snd_rawmidi_f_ops =
1330 {
1331         .owner =        THIS_MODULE,
1332         .read =         snd_rawmidi_read,
1333         .write =        snd_rawmidi_write,
1334         .open =         snd_rawmidi_open,
1335         .release =      snd_rawmidi_release,
1336         .poll =         snd_rawmidi_poll,
1337         .ioctl =        snd_rawmidi_ioctl,
1338 };
1339
1340 static snd_minor_t snd_rawmidi_reg =
1341 {
1342         .comment =      "raw midi",
1343         .f_ops =        &snd_rawmidi_f_ops,
1344 };
1345
1346 static int snd_rawmidi_alloc_substreams(snd_rawmidi_t *rmidi,
1347                                         snd_rawmidi_str_t *stream,
1348                                         int direction,
1349                                         int count)
1350 {
1351         snd_rawmidi_substream_t *substream;
1352         int idx;
1353
1354         INIT_LIST_HEAD(&stream->substreams);
1355         for (idx = 0; idx < count; idx++) {
1356                 substream = snd_kcalloc(sizeof(snd_rawmidi_substream_t), GFP_KERNEL);
1357                 if (substream == NULL)
1358                         return -ENOMEM;
1359                 substream->stream = direction;
1360                 substream->number = idx;
1361                 substream->rmidi = rmidi;
1362                 substream->pstr = stream;
1363                 list_add_tail(&substream->list, &stream->substreams);
1364                 stream->substream_count++;
1365         }
1366         return 0;
1367 }
1368
1369 /**
1370  * snd_rawmidi_new - create a rawmidi instance
1371  * @card: the card instance
1372  * @id: the id string
1373  * @device: the device index
1374  * @output_count: the number of output streams
1375  * @input_count: the number of input streams
1376  * @rrawmidi: the pointer to store the new rawmidi instance
1377  *
1378  * Creates a new rawmidi instance.
1379  * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1380  *
1381  * Returns zero if successful, or a negative error code on failure.
1382  */
1383 int snd_rawmidi_new(snd_card_t * card, char *id, int device,
1384                     int output_count, int input_count,
1385                     snd_rawmidi_t ** rrawmidi)
1386 {
1387         snd_rawmidi_t *rmidi;
1388         int err;
1389         static snd_device_ops_t ops = {
1390                 .dev_free = snd_rawmidi_dev_free,
1391                 .dev_register = snd_rawmidi_dev_register,
1392                 .dev_disconnect = snd_rawmidi_dev_disconnect,
1393                 .dev_unregister = snd_rawmidi_dev_unregister
1394         };
1395
1396         snd_assert(rrawmidi != NULL, return -EINVAL);
1397         *rrawmidi = NULL;
1398         snd_assert(card != NULL, return -ENXIO);
1399         rmidi = snd_magic_kcalloc(snd_rawmidi_t, 0, GFP_KERNEL);
1400         if (rmidi == NULL)
1401                 return -ENOMEM;
1402         rmidi->card = card;
1403         rmidi->device = device;
1404         init_MUTEX(&rmidi->open_mutex);
1405         init_waitqueue_head(&rmidi->open_wait);
1406         if (id != NULL)
1407                 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1408         if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT], SNDRV_RAWMIDI_STREAM_INPUT, input_count)) < 0) {
1409                 snd_rawmidi_free(rmidi);
1410                 return err;
1411         }
1412         if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT], SNDRV_RAWMIDI_STREAM_OUTPUT, output_count)) < 0) {
1413                 snd_rawmidi_free(rmidi);
1414                 return err;
1415         }
1416         if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1417                 snd_rawmidi_free(rmidi);
1418                 return err;
1419         }
1420         *rrawmidi = rmidi;
1421         return 0;
1422 }
1423
1424 static void snd_rawmidi_free_substreams(snd_rawmidi_str_t *stream)
1425 {
1426         snd_rawmidi_substream_t *substream;
1427
1428         while (!list_empty(&stream->substreams)) {
1429                 substream = list_entry(stream->substreams.next, snd_rawmidi_substream_t, list);
1430                 list_del(&substream->list);
1431                 kfree(substream);
1432         }
1433 }
1434
1435 static int snd_rawmidi_free(snd_rawmidi_t *rmidi)
1436 {
1437         snd_assert(rmidi != NULL, return -ENXIO);       
1438         snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1439         snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1440         if (rmidi->private_free)
1441                 rmidi->private_free(rmidi);
1442         snd_magic_kfree(rmidi);
1443         return 0;
1444 }
1445
1446 static int snd_rawmidi_dev_free(snd_device_t *device)
1447 {
1448         snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
1449         return snd_rawmidi_free(rmidi);
1450 }
1451
1452 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1453 static void snd_rawmidi_dev_seq_free(snd_seq_device_t *device)
1454 {
1455         snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->private_data, return);
1456         rmidi->seq_dev = NULL;
1457 }
1458 #endif
1459
1460 static int snd_rawmidi_dev_register(snd_device_t *device)
1461 {
1462         int idx, err;
1463         snd_info_entry_t *entry;
1464         char name[16];
1465         snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
1466
1467         if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1468                 return -ENOMEM;
1469         down(&register_mutex);
1470         idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1471         if (snd_rawmidi_devices[idx] != NULL) {
1472                 up(&register_mutex);
1473                 return -EBUSY;
1474         }
1475         snd_rawmidi_devices[idx] = rmidi;
1476         sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1477         if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1478                                        rmidi->card, rmidi->device,
1479                                        &snd_rawmidi_reg, name)) < 0) {
1480                 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1481                 snd_rawmidi_devices[idx] = NULL;
1482                 up(&register_mutex);
1483                 return err;
1484         }
1485         if (rmidi->ops && rmidi->ops->dev_register &&
1486             (err = rmidi->ops->dev_register(rmidi)) < 0) {
1487                 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1488                 snd_rawmidi_devices[idx] = NULL;
1489                 up(&register_mutex);
1490                 return err;
1491         }
1492 #ifdef CONFIG_SND_OSSEMUL
1493         rmidi->ossreg = 0;
1494         if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1495                 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1496                                             rmidi->card, 0, &snd_rawmidi_reg, name) < 0) {
1497                         snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1498                 } else {
1499                         rmidi->ossreg++;
1500 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1501                         snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1502 #endif
1503                 }
1504         }
1505         if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1506                 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1507                                             rmidi->card, 1, &snd_rawmidi_reg, name) < 0) {
1508                         snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1509                 } else {
1510                         rmidi->ossreg++;
1511                 }
1512         }
1513 #endif /* CONFIG_SND_OSSEMUL */
1514         up(&register_mutex);
1515         sprintf(name, "midi%d", rmidi->device);
1516         entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1517         if (entry) {
1518                 entry->private_data = rmidi;
1519                 entry->c.text.read_size = 1024;
1520                 entry->c.text.read = snd_rawmidi_proc_info_read;
1521                 if (snd_info_register(entry) < 0) {
1522                         snd_info_free_entry(entry);
1523                         entry = NULL;
1524                 }
1525         }
1526         rmidi->proc_entry = entry;
1527 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1528         if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1529                 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1530                         rmidi->seq_dev->private_data = rmidi;
1531                         rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1532                         sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1533                         snd_device_register(rmidi->card, rmidi->seq_dev);
1534                 }
1535         }
1536 #endif
1537         return 0;
1538 }
1539
1540 static int snd_rawmidi_dev_disconnect(snd_device_t *device)
1541 {
1542         snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
1543         int idx;
1544
1545         down(&register_mutex);
1546         idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1547         snd_rawmidi_devices[idx] = NULL;
1548         up(&register_mutex);
1549         return 0;
1550 }
1551
1552 static int snd_rawmidi_dev_unregister(snd_device_t *device)
1553 {
1554         int idx;
1555         snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
1556
1557         snd_assert(rmidi != NULL, return -ENXIO);
1558         down(&register_mutex);
1559         idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1560         snd_rawmidi_devices[idx] = NULL;
1561         if (rmidi->proc_entry) {
1562                 snd_info_unregister(rmidi->proc_entry);
1563                 rmidi->proc_entry = NULL;
1564         }
1565 #ifdef CONFIG_SND_OSSEMUL
1566         if (rmidi->ossreg) {
1567                 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1568                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1569 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1570                         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1571 #endif
1572                 }
1573                 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1574                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1575                 rmidi->ossreg = 0;
1576         }
1577 #endif /* CONFIG_SND_OSSEMUL */
1578         if (rmidi->ops && rmidi->ops->dev_unregister)
1579                 rmidi->ops->dev_unregister(rmidi);
1580         snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1581         up(&register_mutex);
1582 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1583         if (rmidi->seq_dev) {
1584                 snd_device_free(rmidi->card, rmidi->seq_dev);
1585                 rmidi->seq_dev = NULL;
1586         }
1587 #endif
1588         return snd_rawmidi_free(rmidi);
1589 }
1590
1591 /**
1592  * snd_rawmidi_set_ops - set the rawmidi operators
1593  * @rmidi: the rawmidi instance
1594  * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1595  * @ops: the operator table
1596  *
1597  * Sets the rawmidi operators for the given stream direction.
1598  */
1599 void snd_rawmidi_set_ops(snd_rawmidi_t *rmidi, int stream, snd_rawmidi_ops_t *ops)
1600 {
1601         struct list_head *list;
1602         snd_rawmidi_substream_t *substream;
1603         
1604         list_for_each(list, &rmidi->streams[stream].substreams) {
1605                 substream = list_entry(list, snd_rawmidi_substream_t, list);
1606                 substream->ops = ops;
1607         }
1608 }
1609
1610 /*
1611  *  ENTRY functions
1612  */
1613
1614 static int __init alsa_rawmidi_init(void)
1615 {
1616
1617         snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1618 #ifdef CONFIG_SND_OSSEMUL
1619         { int i;
1620         /* check device map table */
1621         for (i = 0; i < SNDRV_CARDS; i++) {
1622                 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1623                         snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1624                         midi_map[i] = 0;
1625                 }
1626                 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1627                         snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1628                         amidi_map[i] = 1;
1629                 }
1630         }
1631         }
1632 #endif /* CONFIG_SND_OSSEMUL */
1633         return 0;
1634 }
1635
1636 static void __exit alsa_rawmidi_exit(void)
1637 {
1638         snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1639 }
1640
1641 module_init(alsa_rawmidi_init)
1642 module_exit(alsa_rawmidi_exit)
1643
1644 EXPORT_SYMBOL(snd_rawmidi_output_params);
1645 EXPORT_SYMBOL(snd_rawmidi_input_params);
1646 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1647 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1648 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1649 EXPORT_SYMBOL(snd_rawmidi_receive);
1650 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1651 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1652 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1653 EXPORT_SYMBOL(snd_rawmidi_transmit);
1654 EXPORT_SYMBOL(snd_rawmidi_new);
1655 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1656 EXPORT_SYMBOL(snd_rawmidi_info);
1657 EXPORT_SYMBOL(snd_rawmidi_info_select);
1658 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1659 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1660 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1661 EXPORT_SYMBOL(snd_rawmidi_kernel_write);