Linux 3.12.70
[linux/fpc-iii.git] / sound / core / rawmidi.c
blob33c9666b34af7b1787ccc4c29f213166c0b79e5b
1 /*
2 * Abstract layer for MIDI v1.0 stream
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
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.
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.
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
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/module.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>
38 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
39 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
40 MODULE_LICENSE("GPL");
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 */
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);
56 static LIST_HEAD(snd_rawmidi_devices);
57 static DEFINE_MUTEX(register_mutex);
59 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
61 struct snd_rawmidi *rawmidi;
63 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
64 if (rawmidi->card == card && rawmidi->device == device)
65 return rawmidi;
66 return NULL;
69 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
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;
81 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
83 struct snd_rawmidi_runtime *runtime = substream->runtime;
84 return runtime->avail >= runtime->avail_min;
87 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
88 size_t count)
90 struct snd_rawmidi_runtime *runtime = substream->runtime;
91 return runtime->avail >= runtime->avail_min &&
92 (!substream->append || runtime->avail >= count);
95 static void snd_rawmidi_input_event_work(struct work_struct *work)
97 struct snd_rawmidi_runtime *runtime =
98 container_of(work, struct snd_rawmidi_runtime, event_work);
99 if (runtime->event)
100 runtime->event(runtime->substream);
103 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
105 struct snd_rawmidi_runtime *runtime;
107 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
108 return -ENOMEM;
109 runtime->substream = substream;
110 spin_lock_init(&runtime->lock);
111 init_waitqueue_head(&runtime->sleep);
112 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
113 runtime->event = NULL;
114 runtime->buffer_size = PAGE_SIZE;
115 runtime->avail_min = 1;
116 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
117 runtime->avail = 0;
118 else
119 runtime->avail = runtime->buffer_size;
120 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
121 kfree(runtime);
122 return -ENOMEM;
124 runtime->appl_ptr = runtime->hw_ptr = 0;
125 substream->runtime = runtime;
126 return 0;
129 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
131 struct snd_rawmidi_runtime *runtime = substream->runtime;
133 kfree(runtime->buffer);
134 kfree(runtime);
135 substream->runtime = NULL;
136 return 0;
139 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
141 if (!substream->opened)
142 return;
143 substream->ops->trigger(substream, up);
146 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
148 if (!substream->opened)
149 return;
150 substream->ops->trigger(substream, up);
151 if (!up)
152 cancel_work_sync(&substream->runtime->event_work);
155 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
157 unsigned long flags;
158 struct snd_rawmidi_runtime *runtime = substream->runtime;
160 snd_rawmidi_output_trigger(substream, 0);
161 runtime->drain = 0;
162 spin_lock_irqsave(&runtime->lock, flags);
163 runtime->appl_ptr = runtime->hw_ptr = 0;
164 runtime->avail = runtime->buffer_size;
165 spin_unlock_irqrestore(&runtime->lock, flags);
166 return 0;
169 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
171 int err;
172 long timeout;
173 struct snd_rawmidi_runtime *runtime = substream->runtime;
175 err = 0;
176 runtime->drain = 1;
177 timeout = wait_event_interruptible_timeout(runtime->sleep,
178 (runtime->avail >= runtime->buffer_size),
179 10*HZ);
180 if (signal_pending(current))
181 err = -ERESTARTSYS;
182 if (runtime->avail < runtime->buffer_size && !timeout) {
183 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
184 err = -EIO;
186 runtime->drain = 0;
187 if (err != -ERESTARTSYS) {
188 /* we need wait a while to make sure that Tx FIFOs are empty */
189 if (substream->ops->drain)
190 substream->ops->drain(substream);
191 else
192 msleep(50);
193 snd_rawmidi_drop_output(substream);
195 return err;
198 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
200 unsigned long flags;
201 struct snd_rawmidi_runtime *runtime = substream->runtime;
203 snd_rawmidi_input_trigger(substream, 0);
204 runtime->drain = 0;
205 spin_lock_irqsave(&runtime->lock, flags);
206 runtime->appl_ptr = runtime->hw_ptr = 0;
207 runtime->avail = 0;
208 spin_unlock_irqrestore(&runtime->lock, flags);
209 return 0;
212 /* look for an available substream for the given stream direction;
213 * if a specific subdevice is given, try to assign it
215 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
216 int stream, int mode,
217 struct snd_rawmidi_substream **sub_ret)
219 struct snd_rawmidi_substream *substream;
220 struct snd_rawmidi_str *s = &rmidi->streams[stream];
221 static unsigned int info_flags[2] = {
222 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
223 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
226 if (!(rmidi->info_flags & info_flags[stream]))
227 return -ENXIO;
228 if (subdevice >= 0 && subdevice >= s->substream_count)
229 return -ENODEV;
231 list_for_each_entry(substream, &s->substreams, list) {
232 if (substream->opened) {
233 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
234 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
235 !substream->append)
236 continue;
238 if (subdevice < 0 || subdevice == substream->number) {
239 *sub_ret = substream;
240 return 0;
243 return -EAGAIN;
246 /* open and do ref-counting for the given substream */
247 static int open_substream(struct snd_rawmidi *rmidi,
248 struct snd_rawmidi_substream *substream,
249 int mode)
251 int err;
253 if (substream->use_count == 0) {
254 err = snd_rawmidi_runtime_create(substream);
255 if (err < 0)
256 return err;
257 err = substream->ops->open(substream);
258 if (err < 0) {
259 snd_rawmidi_runtime_free(substream);
260 return err;
262 substream->opened = 1;
263 substream->active_sensing = 0;
264 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
265 substream->append = 1;
266 substream->pid = get_pid(task_pid(current));
267 rmidi->streams[substream->stream].substream_opened++;
269 substream->use_count++;
270 return 0;
273 static void close_substream(struct snd_rawmidi *rmidi,
274 struct snd_rawmidi_substream *substream,
275 int cleanup);
277 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
278 struct snd_rawmidi_file *rfile)
280 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
281 int err;
283 rfile->input = rfile->output = NULL;
284 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
285 err = assign_substream(rmidi, subdevice,
286 SNDRV_RAWMIDI_STREAM_INPUT,
287 mode, &sinput);
288 if (err < 0)
289 return err;
291 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
292 err = assign_substream(rmidi, subdevice,
293 SNDRV_RAWMIDI_STREAM_OUTPUT,
294 mode, &soutput);
295 if (err < 0)
296 return err;
299 if (sinput) {
300 err = open_substream(rmidi, sinput, mode);
301 if (err < 0)
302 return err;
304 if (soutput) {
305 err = open_substream(rmidi, soutput, mode);
306 if (err < 0) {
307 if (sinput)
308 close_substream(rmidi, sinput, 0);
309 return err;
313 rfile->rmidi = rmidi;
314 rfile->input = sinput;
315 rfile->output = soutput;
316 return 0;
319 /* called from sound/core/seq/seq_midi.c */
320 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
321 int mode, struct snd_rawmidi_file * rfile)
323 struct snd_rawmidi *rmidi;
324 int err;
326 if (snd_BUG_ON(!rfile))
327 return -EINVAL;
329 mutex_lock(&register_mutex);
330 rmidi = snd_rawmidi_search(card, device);
331 if (rmidi == NULL) {
332 mutex_unlock(&register_mutex);
333 return -ENODEV;
335 if (!try_module_get(rmidi->card->module)) {
336 mutex_unlock(&register_mutex);
337 return -ENXIO;
339 mutex_unlock(&register_mutex);
341 mutex_lock(&rmidi->open_mutex);
342 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
343 mutex_unlock(&rmidi->open_mutex);
344 if (err < 0)
345 module_put(rmidi->card->module);
346 return err;
349 static int snd_rawmidi_open(struct inode *inode, struct file *file)
351 int maj = imajor(inode);
352 struct snd_card *card;
353 int subdevice;
354 unsigned short fflags;
355 int err;
356 struct snd_rawmidi *rmidi;
357 struct snd_rawmidi_file *rawmidi_file = NULL;
358 wait_queue_t wait;
359 struct snd_ctl_file *kctl;
361 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
362 return -EINVAL; /* invalid combination */
364 err = nonseekable_open(inode, file);
365 if (err < 0)
366 return err;
368 if (maj == snd_major) {
369 rmidi = snd_lookup_minor_data(iminor(inode),
370 SNDRV_DEVICE_TYPE_RAWMIDI);
371 #ifdef CONFIG_SND_OSSEMUL
372 } else if (maj == SOUND_MAJOR) {
373 rmidi = snd_lookup_oss_minor_data(iminor(inode),
374 SNDRV_OSS_DEVICE_TYPE_MIDI);
375 #endif
376 } else
377 return -ENXIO;
379 if (rmidi == NULL)
380 return -ENODEV;
382 if (!try_module_get(rmidi->card->module)) {
383 snd_card_unref(rmidi->card);
384 return -ENXIO;
387 mutex_lock(&rmidi->open_mutex);
388 card = rmidi->card;
389 err = snd_card_file_add(card, file);
390 if (err < 0)
391 goto __error_card;
392 fflags = snd_rawmidi_file_flags(file);
393 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
394 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
395 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
396 if (rawmidi_file == NULL) {
397 err = -ENOMEM;
398 goto __error;
400 init_waitqueue_entry(&wait, current);
401 add_wait_queue(&rmidi->open_wait, &wait);
402 while (1) {
403 subdevice = -1;
404 read_lock(&card->ctl_files_rwlock);
405 list_for_each_entry(kctl, &card->ctl_files, list) {
406 if (kctl->pid == task_pid(current)) {
407 subdevice = kctl->prefer_rawmidi_subdevice;
408 if (subdevice != -1)
409 break;
412 read_unlock(&card->ctl_files_rwlock);
413 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
414 if (err >= 0)
415 break;
416 if (err == -EAGAIN) {
417 if (file->f_flags & O_NONBLOCK) {
418 err = -EBUSY;
419 break;
421 } else
422 break;
423 set_current_state(TASK_INTERRUPTIBLE);
424 mutex_unlock(&rmidi->open_mutex);
425 schedule();
426 mutex_lock(&rmidi->open_mutex);
427 if (rmidi->card->shutdown) {
428 err = -ENODEV;
429 break;
431 if (signal_pending(current)) {
432 err = -ERESTARTSYS;
433 break;
436 remove_wait_queue(&rmidi->open_wait, &wait);
437 if (err < 0) {
438 kfree(rawmidi_file);
439 goto __error;
441 #ifdef CONFIG_SND_OSSEMUL
442 if (rawmidi_file->input && rawmidi_file->input->runtime)
443 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
444 if (rawmidi_file->output && rawmidi_file->output->runtime)
445 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
446 #endif
447 file->private_data = rawmidi_file;
448 mutex_unlock(&rmidi->open_mutex);
449 snd_card_unref(rmidi->card);
450 return 0;
452 __error:
453 snd_card_file_remove(card, file);
454 __error_card:
455 mutex_unlock(&rmidi->open_mutex);
456 module_put(rmidi->card->module);
457 snd_card_unref(rmidi->card);
458 return err;
461 static void close_substream(struct snd_rawmidi *rmidi,
462 struct snd_rawmidi_substream *substream,
463 int cleanup)
465 if (--substream->use_count)
466 return;
468 if (cleanup) {
469 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
470 snd_rawmidi_input_trigger(substream, 0);
471 else {
472 if (substream->active_sensing) {
473 unsigned char buf = 0xfe;
474 /* sending single active sensing message
475 * to shut the device up
477 snd_rawmidi_kernel_write(substream, &buf, 1);
479 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
480 snd_rawmidi_output_trigger(substream, 0);
483 substream->ops->close(substream);
484 if (substream->runtime->private_free)
485 substream->runtime->private_free(substream);
486 snd_rawmidi_runtime_free(substream);
487 substream->opened = 0;
488 substream->append = 0;
489 put_pid(substream->pid);
490 substream->pid = NULL;
491 rmidi->streams[substream->stream].substream_opened--;
494 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
496 struct snd_rawmidi *rmidi;
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;
504 if (rfile->output) {
505 close_substream(rmidi, rfile->output, 1);
506 rfile->output = NULL;
508 rfile->rmidi = NULL;
509 mutex_unlock(&rmidi->open_mutex);
510 wake_up(&rmidi->open_wait);
513 /* called from sound/core/seq/seq_midi.c */
514 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
516 struct snd_rawmidi *rmidi;
518 if (snd_BUG_ON(!rfile))
519 return -ENXIO;
521 rmidi = rfile->rmidi;
522 rawmidi_release_priv(rfile);
523 module_put(rmidi->card->module);
524 return 0;
527 static int snd_rawmidi_release(struct inode *inode, struct file *file)
529 struct snd_rawmidi_file *rfile;
530 struct snd_rawmidi *rmidi;
531 struct module *module;
533 rfile = file->private_data;
534 rmidi = rfile->rmidi;
535 rawmidi_release_priv(rfile);
536 kfree(rfile);
537 module = rmidi->card->module;
538 snd_card_file_remove(rmidi->card, file);
539 module_put(module);
540 return 0;
543 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
544 struct snd_rawmidi_info *info)
546 struct snd_rawmidi *rmidi;
548 if (substream == NULL)
549 return -ENODEV;
550 rmidi = substream->rmidi;
551 memset(info, 0, sizeof(*info));
552 info->card = rmidi->card->number;
553 info->device = rmidi->device;
554 info->subdevice = substream->number;
555 info->stream = substream->stream;
556 info->flags = rmidi->info_flags;
557 strcpy(info->id, rmidi->id);
558 strcpy(info->name, rmidi->name);
559 strcpy(info->subname, substream->name);
560 info->subdevices_count = substream->pstr->substream_count;
561 info->subdevices_avail = (substream->pstr->substream_count -
562 substream->pstr->substream_opened);
563 return 0;
566 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
567 struct snd_rawmidi_info __user * _info)
569 struct snd_rawmidi_info info;
570 int err;
571 if ((err = snd_rawmidi_info(substream, &info)) < 0)
572 return err;
573 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
574 return -EFAULT;
575 return 0;
578 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
580 struct snd_rawmidi *rmidi;
581 struct snd_rawmidi_str *pstr;
582 struct snd_rawmidi_substream *substream;
584 mutex_lock(&register_mutex);
585 rmidi = snd_rawmidi_search(card, info->device);
586 mutex_unlock(&register_mutex);
587 if (!rmidi)
588 return -ENXIO;
589 if (info->stream < 0 || info->stream > 1)
590 return -EINVAL;
591 pstr = &rmidi->streams[info->stream];
592 if (pstr->substream_count == 0)
593 return -ENOENT;
594 if (info->subdevice >= pstr->substream_count)
595 return -ENXIO;
596 list_for_each_entry(substream, &pstr->substreams, list) {
597 if ((unsigned int)substream->number == info->subdevice)
598 return snd_rawmidi_info(substream, info);
600 return -ENXIO;
603 static int snd_rawmidi_info_select_user(struct snd_card *card,
604 struct snd_rawmidi_info __user *_info)
606 int err;
607 struct snd_rawmidi_info info;
608 if (get_user(info.device, &_info->device))
609 return -EFAULT;
610 if (get_user(info.stream, &_info->stream))
611 return -EFAULT;
612 if (get_user(info.subdevice, &_info->subdevice))
613 return -EFAULT;
614 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
615 return err;
616 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
617 return -EFAULT;
618 return 0;
621 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
622 struct snd_rawmidi_params * params)
624 char *newbuf;
625 struct snd_rawmidi_runtime *runtime = substream->runtime;
627 if (substream->append && substream->use_count > 1)
628 return -EBUSY;
629 snd_rawmidi_drain_output(substream);
630 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
631 return -EINVAL;
633 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
634 return -EINVAL;
636 if (params->buffer_size != runtime->buffer_size) {
637 newbuf = krealloc(runtime->buffer, params->buffer_size,
638 GFP_KERNEL);
639 if (!newbuf)
640 return -ENOMEM;
641 runtime->buffer = newbuf;
642 runtime->buffer_size = params->buffer_size;
643 runtime->avail = runtime->buffer_size;
645 runtime->avail_min = params->avail_min;
646 substream->active_sensing = !params->no_active_sensing;
647 return 0;
650 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
651 struct snd_rawmidi_params * params)
653 char *newbuf;
654 struct snd_rawmidi_runtime *runtime = substream->runtime;
656 snd_rawmidi_drain_input(substream);
657 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
658 return -EINVAL;
660 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
661 return -EINVAL;
663 if (params->buffer_size != runtime->buffer_size) {
664 newbuf = krealloc(runtime->buffer, params->buffer_size,
665 GFP_KERNEL);
666 if (!newbuf)
667 return -ENOMEM;
668 runtime->buffer = newbuf;
669 runtime->buffer_size = params->buffer_size;
671 runtime->avail_min = params->avail_min;
672 return 0;
675 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
676 struct snd_rawmidi_status * status)
678 struct snd_rawmidi_runtime *runtime = substream->runtime;
680 memset(status, 0, sizeof(*status));
681 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
682 spin_lock_irq(&runtime->lock);
683 status->avail = runtime->avail;
684 spin_unlock_irq(&runtime->lock);
685 return 0;
688 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
689 struct snd_rawmidi_status * status)
691 struct snd_rawmidi_runtime *runtime = substream->runtime;
693 memset(status, 0, sizeof(*status));
694 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
695 spin_lock_irq(&runtime->lock);
696 status->avail = runtime->avail;
697 status->xruns = runtime->xruns;
698 runtime->xruns = 0;
699 spin_unlock_irq(&runtime->lock);
700 return 0;
703 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
705 struct snd_rawmidi_file *rfile;
706 void __user *argp = (void __user *)arg;
708 rfile = file->private_data;
709 if (((cmd >> 8) & 0xff) != 'W')
710 return -ENOTTY;
711 switch (cmd) {
712 case SNDRV_RAWMIDI_IOCTL_PVERSION:
713 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
714 case SNDRV_RAWMIDI_IOCTL_INFO:
716 int stream;
717 struct snd_rawmidi_info __user *info = argp;
718 if (get_user(stream, &info->stream))
719 return -EFAULT;
720 switch (stream) {
721 case SNDRV_RAWMIDI_STREAM_INPUT:
722 return snd_rawmidi_info_user(rfile->input, info);
723 case SNDRV_RAWMIDI_STREAM_OUTPUT:
724 return snd_rawmidi_info_user(rfile->output, info);
725 default:
726 return -EINVAL;
729 case SNDRV_RAWMIDI_IOCTL_PARAMS:
731 struct snd_rawmidi_params params;
732 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
733 return -EFAULT;
734 switch (params.stream) {
735 case SNDRV_RAWMIDI_STREAM_OUTPUT:
736 if (rfile->output == NULL)
737 return -EINVAL;
738 return snd_rawmidi_output_params(rfile->output, &params);
739 case SNDRV_RAWMIDI_STREAM_INPUT:
740 if (rfile->input == NULL)
741 return -EINVAL;
742 return snd_rawmidi_input_params(rfile->input, &params);
743 default:
744 return -EINVAL;
747 case SNDRV_RAWMIDI_IOCTL_STATUS:
749 int err = 0;
750 struct snd_rawmidi_status status;
751 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
752 return -EFAULT;
753 switch (status.stream) {
754 case SNDRV_RAWMIDI_STREAM_OUTPUT:
755 if (rfile->output == NULL)
756 return -EINVAL;
757 err = snd_rawmidi_output_status(rfile->output, &status);
758 break;
759 case SNDRV_RAWMIDI_STREAM_INPUT:
760 if (rfile->input == NULL)
761 return -EINVAL;
762 err = snd_rawmidi_input_status(rfile->input, &status);
763 break;
764 default:
765 return -EINVAL;
767 if (err < 0)
768 return err;
769 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
770 return -EFAULT;
771 return 0;
773 case SNDRV_RAWMIDI_IOCTL_DROP:
775 int val;
776 if (get_user(val, (int __user *) argp))
777 return -EFAULT;
778 switch (val) {
779 case SNDRV_RAWMIDI_STREAM_OUTPUT:
780 if (rfile->output == NULL)
781 return -EINVAL;
782 return snd_rawmidi_drop_output(rfile->output);
783 default:
784 return -EINVAL;
787 case SNDRV_RAWMIDI_IOCTL_DRAIN:
789 int val;
790 if (get_user(val, (int __user *) argp))
791 return -EFAULT;
792 switch (val) {
793 case SNDRV_RAWMIDI_STREAM_OUTPUT:
794 if (rfile->output == NULL)
795 return -EINVAL;
796 return snd_rawmidi_drain_output(rfile->output);
797 case SNDRV_RAWMIDI_STREAM_INPUT:
798 if (rfile->input == NULL)
799 return -EINVAL;
800 return snd_rawmidi_drain_input(rfile->input);
801 default:
802 return -EINVAL;
805 #ifdef CONFIG_SND_DEBUG
806 default:
807 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
808 #endif
810 return -ENOTTY;
813 static int snd_rawmidi_control_ioctl(struct snd_card *card,
814 struct snd_ctl_file *control,
815 unsigned int cmd,
816 unsigned long arg)
818 void __user *argp = (void __user *)arg;
820 switch (cmd) {
821 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
823 int device;
825 if (get_user(device, (int __user *)argp))
826 return -EFAULT;
827 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
828 device = SNDRV_RAWMIDI_DEVICES - 1;
829 mutex_lock(&register_mutex);
830 device = device < 0 ? 0 : device + 1;
831 while (device < SNDRV_RAWMIDI_DEVICES) {
832 if (snd_rawmidi_search(card, device))
833 break;
834 device++;
836 if (device == SNDRV_RAWMIDI_DEVICES)
837 device = -1;
838 mutex_unlock(&register_mutex);
839 if (put_user(device, (int __user *)argp))
840 return -EFAULT;
841 return 0;
843 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
845 int val;
847 if (get_user(val, (int __user *)argp))
848 return -EFAULT;
849 control->prefer_rawmidi_subdevice = val;
850 return 0;
852 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
853 return snd_rawmidi_info_select_user(card, argp);
855 return -ENOIOCTLCMD;
859 * snd_rawmidi_receive - receive the input data from the device
860 * @substream: the rawmidi substream
861 * @buffer: the buffer pointer
862 * @count: the data size to read
864 * Reads the data from the internal buffer.
866 * Return: The size of read data, or a negative error code on failure.
868 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
869 const unsigned char *buffer, int count)
871 unsigned long flags;
872 int result = 0, count1;
873 struct snd_rawmidi_runtime *runtime = substream->runtime;
875 if (!substream->opened)
876 return -EBADFD;
877 if (runtime->buffer == NULL) {
878 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
879 return -EINVAL;
881 spin_lock_irqsave(&runtime->lock, flags);
882 if (count == 1) { /* special case, faster code */
883 substream->bytes++;
884 if (runtime->avail < runtime->buffer_size) {
885 runtime->buffer[runtime->hw_ptr++] = buffer[0];
886 runtime->hw_ptr %= runtime->buffer_size;
887 runtime->avail++;
888 result++;
889 } else {
890 runtime->xruns++;
892 } else {
893 substream->bytes += count;
894 count1 = runtime->buffer_size - runtime->hw_ptr;
895 if (count1 > count)
896 count1 = count;
897 if (count1 > (int)(runtime->buffer_size - runtime->avail))
898 count1 = runtime->buffer_size - runtime->avail;
899 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
900 runtime->hw_ptr += count1;
901 runtime->hw_ptr %= runtime->buffer_size;
902 runtime->avail += count1;
903 count -= count1;
904 result += count1;
905 if (count > 0) {
906 buffer += count1;
907 count1 = count;
908 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
909 count1 = runtime->buffer_size - runtime->avail;
910 runtime->xruns += count - count1;
912 if (count1 > 0) {
913 memcpy(runtime->buffer, buffer, count1);
914 runtime->hw_ptr = count1;
915 runtime->avail += count1;
916 result += count1;
920 if (result > 0) {
921 if (runtime->event)
922 schedule_work(&runtime->event_work);
923 else if (snd_rawmidi_ready(substream))
924 wake_up(&runtime->sleep);
926 spin_unlock_irqrestore(&runtime->lock, flags);
927 return result;
930 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
931 unsigned char __user *userbuf,
932 unsigned char *kernelbuf, long count)
934 unsigned long flags;
935 long result = 0, count1;
936 struct snd_rawmidi_runtime *runtime = substream->runtime;
937 unsigned long appl_ptr;
939 spin_lock_irqsave(&runtime->lock, flags);
940 while (count > 0 && runtime->avail) {
941 count1 = runtime->buffer_size - runtime->appl_ptr;
942 if (count1 > count)
943 count1 = count;
944 if (count1 > (int)runtime->avail)
945 count1 = runtime->avail;
947 /* update runtime->appl_ptr before unlocking for userbuf */
948 appl_ptr = runtime->appl_ptr;
949 runtime->appl_ptr += count1;
950 runtime->appl_ptr %= runtime->buffer_size;
951 runtime->avail -= count1;
953 if (kernelbuf)
954 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
955 if (userbuf) {
956 spin_unlock_irqrestore(&runtime->lock, flags);
957 if (copy_to_user(userbuf + result,
958 runtime->buffer + appl_ptr, count1)) {
959 return result > 0 ? result : -EFAULT;
961 spin_lock_irqsave(&runtime->lock, flags);
963 result += count1;
964 count -= count1;
966 spin_unlock_irqrestore(&runtime->lock, flags);
967 return result;
970 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
971 unsigned char *buf, long count)
973 snd_rawmidi_input_trigger(substream, 1);
974 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
977 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
978 loff_t *offset)
980 long result;
981 int count1;
982 struct snd_rawmidi_file *rfile;
983 struct snd_rawmidi_substream *substream;
984 struct snd_rawmidi_runtime *runtime;
986 rfile = file->private_data;
987 substream = rfile->input;
988 if (substream == NULL)
989 return -EIO;
990 runtime = substream->runtime;
991 snd_rawmidi_input_trigger(substream, 1);
992 result = 0;
993 while (count > 0) {
994 spin_lock_irq(&runtime->lock);
995 while (!snd_rawmidi_ready(substream)) {
996 wait_queue_t wait;
997 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
998 spin_unlock_irq(&runtime->lock);
999 return result > 0 ? result : -EAGAIN;
1001 init_waitqueue_entry(&wait, current);
1002 add_wait_queue(&runtime->sleep, &wait);
1003 set_current_state(TASK_INTERRUPTIBLE);
1004 spin_unlock_irq(&runtime->lock);
1005 schedule();
1006 remove_wait_queue(&runtime->sleep, &wait);
1007 if (rfile->rmidi->card->shutdown)
1008 return -ENODEV;
1009 if (signal_pending(current))
1010 return result > 0 ? result : -ERESTARTSYS;
1011 if (!runtime->avail)
1012 return result > 0 ? result : -EIO;
1013 spin_lock_irq(&runtime->lock);
1015 spin_unlock_irq(&runtime->lock);
1016 count1 = snd_rawmidi_kernel_read1(substream,
1017 (unsigned char __user *)buf,
1018 NULL/*kernelbuf*/,
1019 count);
1020 if (count1 < 0)
1021 return result > 0 ? result : count1;
1022 result += count1;
1023 buf += count1;
1024 count -= count1;
1026 return result;
1030 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1031 * @substream: the rawmidi substream
1033 * Return: 1 if the internal output buffer is empty, 0 if not.
1035 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1037 struct snd_rawmidi_runtime *runtime = substream->runtime;
1038 int result;
1039 unsigned long flags;
1041 if (runtime->buffer == NULL) {
1042 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1043 return 1;
1045 spin_lock_irqsave(&runtime->lock, flags);
1046 result = runtime->avail >= runtime->buffer_size;
1047 spin_unlock_irqrestore(&runtime->lock, flags);
1048 return result;
1052 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1053 * @substream: the rawmidi substream
1054 * @buffer: the buffer pointer
1055 * @count: data size to transfer
1057 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1059 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1060 unsigned char *buffer, int count)
1062 int result, count1;
1063 struct snd_rawmidi_runtime *runtime = substream->runtime;
1065 if (runtime->buffer == NULL) {
1066 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1067 return -EINVAL;
1069 result = 0;
1070 if (runtime->avail >= runtime->buffer_size) {
1071 /* warning: lowlevel layer MUST trigger down the hardware */
1072 goto __skip;
1074 if (count == 1) { /* special case, faster code */
1075 *buffer = runtime->buffer[runtime->hw_ptr];
1076 result++;
1077 } else {
1078 count1 = runtime->buffer_size - runtime->hw_ptr;
1079 if (count1 > count)
1080 count1 = count;
1081 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1082 count1 = runtime->buffer_size - runtime->avail;
1083 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1084 count -= count1;
1085 result += count1;
1086 if (count > 0) {
1087 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1088 count = runtime->buffer_size - runtime->avail - count1;
1089 memcpy(buffer + count1, runtime->buffer, count);
1090 result += count;
1093 __skip:
1094 return result;
1098 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1099 * @substream: the rawmidi substream
1100 * @buffer: the buffer pointer
1101 * @count: data size to transfer
1103 * Copies data from the internal output buffer to the given buffer.
1105 * Call this in the interrupt handler when the midi output is ready,
1106 * and call snd_rawmidi_transmit_ack() after the transmission is
1107 * finished.
1109 * Return: The size of copied data, or a negative error code on failure.
1111 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1112 unsigned char *buffer, int count)
1114 struct snd_rawmidi_runtime *runtime = substream->runtime;
1115 int result;
1116 unsigned long flags;
1118 spin_lock_irqsave(&runtime->lock, flags);
1119 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1120 spin_unlock_irqrestore(&runtime->lock, flags);
1121 return result;
1125 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1126 * @substream: the rawmidi substream
1127 * @count: the tranferred count
1129 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1131 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1133 struct snd_rawmidi_runtime *runtime = substream->runtime;
1135 if (runtime->buffer == NULL) {
1136 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1137 return -EINVAL;
1139 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1140 runtime->hw_ptr += count;
1141 runtime->hw_ptr %= runtime->buffer_size;
1142 runtime->avail += count;
1143 substream->bytes += count;
1144 if (count > 0) {
1145 if (runtime->drain || snd_rawmidi_ready(substream))
1146 wake_up(&runtime->sleep);
1148 return count;
1152 * snd_rawmidi_transmit_ack - acknowledge the transmission
1153 * @substream: the rawmidi substream
1154 * @count: the transferred count
1156 * Advances the hardware pointer for the internal output buffer with
1157 * the given size and updates the condition.
1158 * Call after the transmission is finished.
1160 * Return: The advanced size if successful, or a negative error code on failure.
1162 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1164 struct snd_rawmidi_runtime *runtime = substream->runtime;
1165 int result;
1166 unsigned long flags;
1168 spin_lock_irqsave(&runtime->lock, flags);
1169 result = __snd_rawmidi_transmit_ack(substream, count);
1170 spin_unlock_irqrestore(&runtime->lock, flags);
1171 return result;
1175 * snd_rawmidi_transmit - copy from the buffer to the device
1176 * @substream: the rawmidi substream
1177 * @buffer: the buffer pointer
1178 * @count: the data size to transfer
1180 * Copies data from the buffer to the device and advances the pointer.
1182 * Return: The copied size if successful, or a negative error code on failure.
1184 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1185 unsigned char *buffer, int count)
1187 struct snd_rawmidi_runtime *runtime = substream->runtime;
1188 int result;
1189 unsigned long flags;
1191 spin_lock_irqsave(&runtime->lock, flags);
1192 if (!substream->opened)
1193 result = -EBADFD;
1194 else {
1195 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1196 if (count <= 0)
1197 result = count;
1198 else
1199 result = __snd_rawmidi_transmit_ack(substream, count);
1201 spin_unlock_irqrestore(&runtime->lock, flags);
1202 return result;
1205 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1206 const unsigned char __user *userbuf,
1207 const unsigned char *kernelbuf,
1208 long count)
1210 unsigned long flags;
1211 long count1, result;
1212 struct snd_rawmidi_runtime *runtime = substream->runtime;
1213 unsigned long appl_ptr;
1215 if (!kernelbuf && !userbuf)
1216 return -EINVAL;
1217 if (snd_BUG_ON(!runtime->buffer))
1218 return -EINVAL;
1220 result = 0;
1221 spin_lock_irqsave(&runtime->lock, flags);
1222 if (substream->append) {
1223 if ((long)runtime->avail < count) {
1224 spin_unlock_irqrestore(&runtime->lock, flags);
1225 return -EAGAIN;
1228 while (count > 0 && runtime->avail > 0) {
1229 count1 = runtime->buffer_size - runtime->appl_ptr;
1230 if (count1 > count)
1231 count1 = count;
1232 if (count1 > (long)runtime->avail)
1233 count1 = runtime->avail;
1235 /* update runtime->appl_ptr before unlocking for userbuf */
1236 appl_ptr = runtime->appl_ptr;
1237 runtime->appl_ptr += count1;
1238 runtime->appl_ptr %= runtime->buffer_size;
1239 runtime->avail -= count1;
1241 if (kernelbuf)
1242 memcpy(runtime->buffer + appl_ptr,
1243 kernelbuf + result, count1);
1244 else if (userbuf) {
1245 spin_unlock_irqrestore(&runtime->lock, flags);
1246 if (copy_from_user(runtime->buffer + appl_ptr,
1247 userbuf + result, count1)) {
1248 spin_lock_irqsave(&runtime->lock, flags);
1249 result = result > 0 ? result : -EFAULT;
1250 goto __end;
1252 spin_lock_irqsave(&runtime->lock, flags);
1254 result += count1;
1255 count -= count1;
1257 __end:
1258 count1 = runtime->avail < runtime->buffer_size;
1259 spin_unlock_irqrestore(&runtime->lock, flags);
1260 if (count1)
1261 snd_rawmidi_output_trigger(substream, 1);
1262 return result;
1265 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1266 const unsigned char *buf, long count)
1268 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1271 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1272 size_t count, loff_t *offset)
1274 long result, timeout;
1275 int count1;
1276 struct snd_rawmidi_file *rfile;
1277 struct snd_rawmidi_runtime *runtime;
1278 struct snd_rawmidi_substream *substream;
1280 rfile = file->private_data;
1281 substream = rfile->output;
1282 runtime = substream->runtime;
1283 /* we cannot put an atomic message to our buffer */
1284 if (substream->append && count > runtime->buffer_size)
1285 return -EIO;
1286 result = 0;
1287 while (count > 0) {
1288 spin_lock_irq(&runtime->lock);
1289 while (!snd_rawmidi_ready_append(substream, count)) {
1290 wait_queue_t wait;
1291 if (file->f_flags & O_NONBLOCK) {
1292 spin_unlock_irq(&runtime->lock);
1293 return result > 0 ? result : -EAGAIN;
1295 init_waitqueue_entry(&wait, current);
1296 add_wait_queue(&runtime->sleep, &wait);
1297 set_current_state(TASK_INTERRUPTIBLE);
1298 spin_unlock_irq(&runtime->lock);
1299 timeout = schedule_timeout(30 * HZ);
1300 remove_wait_queue(&runtime->sleep, &wait);
1301 if (rfile->rmidi->card->shutdown)
1302 return -ENODEV;
1303 if (signal_pending(current))
1304 return result > 0 ? result : -ERESTARTSYS;
1305 if (!runtime->avail && !timeout)
1306 return result > 0 ? result : -EIO;
1307 spin_lock_irq(&runtime->lock);
1309 spin_unlock_irq(&runtime->lock);
1310 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1311 if (count1 < 0)
1312 return result > 0 ? result : count1;
1313 result += count1;
1314 buf += count1;
1315 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1316 break;
1317 count -= count1;
1319 if (file->f_flags & O_DSYNC) {
1320 spin_lock_irq(&runtime->lock);
1321 while (runtime->avail != runtime->buffer_size) {
1322 wait_queue_t wait;
1323 unsigned int last_avail = runtime->avail;
1324 init_waitqueue_entry(&wait, current);
1325 add_wait_queue(&runtime->sleep, &wait);
1326 set_current_state(TASK_INTERRUPTIBLE);
1327 spin_unlock_irq(&runtime->lock);
1328 timeout = schedule_timeout(30 * HZ);
1329 remove_wait_queue(&runtime->sleep, &wait);
1330 if (signal_pending(current))
1331 return result > 0 ? result : -ERESTARTSYS;
1332 if (runtime->avail == last_avail && !timeout)
1333 return result > 0 ? result : -EIO;
1334 spin_lock_irq(&runtime->lock);
1336 spin_unlock_irq(&runtime->lock);
1338 return result;
1341 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1343 struct snd_rawmidi_file *rfile;
1344 struct snd_rawmidi_runtime *runtime;
1345 unsigned int mask;
1347 rfile = file->private_data;
1348 if (rfile->input != NULL) {
1349 runtime = rfile->input->runtime;
1350 snd_rawmidi_input_trigger(rfile->input, 1);
1351 poll_wait(file, &runtime->sleep, wait);
1353 if (rfile->output != NULL) {
1354 runtime = rfile->output->runtime;
1355 poll_wait(file, &runtime->sleep, wait);
1357 mask = 0;
1358 if (rfile->input != NULL) {
1359 if (snd_rawmidi_ready(rfile->input))
1360 mask |= POLLIN | POLLRDNORM;
1362 if (rfile->output != NULL) {
1363 if (snd_rawmidi_ready(rfile->output))
1364 mask |= POLLOUT | POLLWRNORM;
1366 return mask;
1371 #ifdef CONFIG_COMPAT
1372 #include "rawmidi_compat.c"
1373 #else
1374 #define snd_rawmidi_ioctl_compat NULL
1375 #endif
1381 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1382 struct snd_info_buffer *buffer)
1384 struct snd_rawmidi *rmidi;
1385 struct snd_rawmidi_substream *substream;
1386 struct snd_rawmidi_runtime *runtime;
1388 rmidi = entry->private_data;
1389 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1390 mutex_lock(&rmidi->open_mutex);
1391 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1392 list_for_each_entry(substream,
1393 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1394 list) {
1395 snd_iprintf(buffer,
1396 "Output %d\n"
1397 " Tx bytes : %lu\n",
1398 substream->number,
1399 (unsigned long) substream->bytes);
1400 if (substream->opened) {
1401 snd_iprintf(buffer,
1402 " Owner PID : %d\n",
1403 pid_vnr(substream->pid));
1404 runtime = substream->runtime;
1405 snd_iprintf(buffer,
1406 " Mode : %s\n"
1407 " Buffer size : %lu\n"
1408 " Avail : %lu\n",
1409 runtime->oss ? "OSS compatible" : "native",
1410 (unsigned long) runtime->buffer_size,
1411 (unsigned long) runtime->avail);
1415 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1416 list_for_each_entry(substream,
1417 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1418 list) {
1419 snd_iprintf(buffer,
1420 "Input %d\n"
1421 " Rx bytes : %lu\n",
1422 substream->number,
1423 (unsigned long) substream->bytes);
1424 if (substream->opened) {
1425 snd_iprintf(buffer,
1426 " Owner PID : %d\n",
1427 pid_vnr(substream->pid));
1428 runtime = substream->runtime;
1429 snd_iprintf(buffer,
1430 " Buffer size : %lu\n"
1431 " Avail : %lu\n"
1432 " Overruns : %lu\n",
1433 (unsigned long) runtime->buffer_size,
1434 (unsigned long) runtime->avail,
1435 (unsigned long) runtime->xruns);
1439 mutex_unlock(&rmidi->open_mutex);
1443 * Register functions
1446 static const struct file_operations snd_rawmidi_f_ops =
1448 .owner = THIS_MODULE,
1449 .read = snd_rawmidi_read,
1450 .write = snd_rawmidi_write,
1451 .open = snd_rawmidi_open,
1452 .release = snd_rawmidi_release,
1453 .llseek = no_llseek,
1454 .poll = snd_rawmidi_poll,
1455 .unlocked_ioctl = snd_rawmidi_ioctl,
1456 .compat_ioctl = snd_rawmidi_ioctl_compat,
1459 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1460 struct snd_rawmidi_str *stream,
1461 int direction,
1462 int count)
1464 struct snd_rawmidi_substream *substream;
1465 int idx;
1467 for (idx = 0; idx < count; idx++) {
1468 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1469 if (substream == NULL) {
1470 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1471 return -ENOMEM;
1473 substream->stream = direction;
1474 substream->number = idx;
1475 substream->rmidi = rmidi;
1476 substream->pstr = stream;
1477 list_add_tail(&substream->list, &stream->substreams);
1478 stream->substream_count++;
1480 return 0;
1484 * snd_rawmidi_new - create a rawmidi instance
1485 * @card: the card instance
1486 * @id: the id string
1487 * @device: the device index
1488 * @output_count: the number of output streams
1489 * @input_count: the number of input streams
1490 * @rrawmidi: the pointer to store the new rawmidi instance
1492 * Creates a new rawmidi instance.
1493 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1495 * Return: Zero if successful, or a negative error code on failure.
1497 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1498 int output_count, int input_count,
1499 struct snd_rawmidi ** rrawmidi)
1501 struct snd_rawmidi *rmidi;
1502 int err;
1503 static struct snd_device_ops ops = {
1504 .dev_free = snd_rawmidi_dev_free,
1505 .dev_register = snd_rawmidi_dev_register,
1506 .dev_disconnect = snd_rawmidi_dev_disconnect,
1509 if (snd_BUG_ON(!card))
1510 return -ENXIO;
1511 if (rrawmidi)
1512 *rrawmidi = NULL;
1513 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1514 if (rmidi == NULL) {
1515 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1516 return -ENOMEM;
1518 rmidi->card = card;
1519 rmidi->device = device;
1520 mutex_init(&rmidi->open_mutex);
1521 init_waitqueue_head(&rmidi->open_wait);
1522 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1523 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1525 if (id != NULL)
1526 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1527 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1528 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1529 SNDRV_RAWMIDI_STREAM_INPUT,
1530 input_count)) < 0) {
1531 snd_rawmidi_free(rmidi);
1532 return err;
1534 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1535 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1536 SNDRV_RAWMIDI_STREAM_OUTPUT,
1537 output_count)) < 0) {
1538 snd_rawmidi_free(rmidi);
1539 return err;
1541 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1542 snd_rawmidi_free(rmidi);
1543 return err;
1545 if (rrawmidi)
1546 *rrawmidi = rmidi;
1547 return 0;
1550 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1552 struct snd_rawmidi_substream *substream;
1554 while (!list_empty(&stream->substreams)) {
1555 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1556 list_del(&substream->list);
1557 kfree(substream);
1561 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1563 if (!rmidi)
1564 return 0;
1566 snd_info_free_entry(rmidi->proc_entry);
1567 rmidi->proc_entry = NULL;
1568 mutex_lock(&register_mutex);
1569 if (rmidi->ops && rmidi->ops->dev_unregister)
1570 rmidi->ops->dev_unregister(rmidi);
1571 mutex_unlock(&register_mutex);
1573 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1574 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1575 if (rmidi->private_free)
1576 rmidi->private_free(rmidi);
1577 kfree(rmidi);
1578 return 0;
1581 static int snd_rawmidi_dev_free(struct snd_device *device)
1583 struct snd_rawmidi *rmidi = device->device_data;
1584 return snd_rawmidi_free(rmidi);
1587 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1588 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1590 struct snd_rawmidi *rmidi = device->private_data;
1591 rmidi->seq_dev = NULL;
1593 #endif
1595 static int snd_rawmidi_dev_register(struct snd_device *device)
1597 int err;
1598 struct snd_info_entry *entry;
1599 char name[16];
1600 struct snd_rawmidi *rmidi = device->device_data;
1602 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1603 return -ENOMEM;
1604 mutex_lock(&register_mutex);
1605 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1606 mutex_unlock(&register_mutex);
1607 return -EBUSY;
1609 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1610 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1611 mutex_unlock(&register_mutex);
1612 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1613 rmidi->card, rmidi->device,
1614 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1615 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1616 mutex_lock(&register_mutex);
1617 list_del(&rmidi->list);
1618 mutex_unlock(&register_mutex);
1619 return err;
1621 if (rmidi->ops && rmidi->ops->dev_register &&
1622 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1623 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1624 mutex_lock(&register_mutex);
1625 list_del(&rmidi->list);
1626 mutex_unlock(&register_mutex);
1627 return err;
1629 #ifdef CONFIG_SND_OSSEMUL
1630 rmidi->ossreg = 0;
1631 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1632 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1633 rmidi->card, 0, &snd_rawmidi_f_ops,
1634 rmidi, name) < 0) {
1635 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1636 } else {
1637 rmidi->ossreg++;
1638 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1639 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1640 #endif
1643 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1644 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1645 rmidi->card, 1, &snd_rawmidi_f_ops,
1646 rmidi, name) < 0) {
1647 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1648 } else {
1649 rmidi->ossreg++;
1652 #endif /* CONFIG_SND_OSSEMUL */
1653 sprintf(name, "midi%d", rmidi->device);
1654 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1655 if (entry) {
1656 entry->private_data = rmidi;
1657 entry->c.text.read = snd_rawmidi_proc_info_read;
1658 if (snd_info_register(entry) < 0) {
1659 snd_info_free_entry(entry);
1660 entry = NULL;
1663 rmidi->proc_entry = entry;
1664 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1665 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1666 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1667 rmidi->seq_dev->private_data = rmidi;
1668 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1669 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1670 snd_device_register(rmidi->card, rmidi->seq_dev);
1673 #endif
1674 return 0;
1677 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1679 struct snd_rawmidi *rmidi = device->device_data;
1680 int dir;
1682 mutex_lock(&register_mutex);
1683 mutex_lock(&rmidi->open_mutex);
1684 wake_up(&rmidi->open_wait);
1685 list_del_init(&rmidi->list);
1686 for (dir = 0; dir < 2; dir++) {
1687 struct snd_rawmidi_substream *s;
1688 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1689 if (s->runtime)
1690 wake_up(&s->runtime->sleep);
1694 #ifdef CONFIG_SND_OSSEMUL
1695 if (rmidi->ossreg) {
1696 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1697 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1698 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1699 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1700 #endif
1702 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1703 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1704 rmidi->ossreg = 0;
1706 #endif /* CONFIG_SND_OSSEMUL */
1707 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1708 mutex_unlock(&rmidi->open_mutex);
1709 mutex_unlock(&register_mutex);
1710 return 0;
1714 * snd_rawmidi_set_ops - set the rawmidi operators
1715 * @rmidi: the rawmidi instance
1716 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1717 * @ops: the operator table
1719 * Sets the rawmidi operators for the given stream direction.
1721 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1722 struct snd_rawmidi_ops *ops)
1724 struct snd_rawmidi_substream *substream;
1726 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1727 substream->ops = ops;
1731 * ENTRY functions
1734 static int __init alsa_rawmidi_init(void)
1737 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1738 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1739 #ifdef CONFIG_SND_OSSEMUL
1740 { int i;
1741 /* check device map table */
1742 for (i = 0; i < SNDRV_CARDS; i++) {
1743 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1744 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1745 midi_map[i] = 0;
1747 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1748 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1749 amidi_map[i] = 1;
1753 #endif /* CONFIG_SND_OSSEMUL */
1754 return 0;
1757 static void __exit alsa_rawmidi_exit(void)
1759 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1760 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1763 module_init(alsa_rawmidi_init)
1764 module_exit(alsa_rawmidi_exit)
1766 EXPORT_SYMBOL(snd_rawmidi_output_params);
1767 EXPORT_SYMBOL(snd_rawmidi_input_params);
1768 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1769 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1770 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1771 EXPORT_SYMBOL(snd_rawmidi_receive);
1772 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1773 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1774 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1775 EXPORT_SYMBOL(snd_rawmidi_transmit);
1776 EXPORT_SYMBOL(snd_rawmidi_new);
1777 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1778 EXPORT_SYMBOL(snd_rawmidi_info_select);
1779 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1780 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1781 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1782 EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1783 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1784 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);