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