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