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