~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/core/rawmidi.c

Version: ~ [ linux-5.1-rc5 ] ~ [ linux-5.0.7 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.34 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.111 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.168 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.178 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.138 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.65 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp