2 * Abstract layer for MIDI v1.0 stream
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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/driver.h>
23 #include <sound/core.h>
24 #include <linux/major.h>
25 #include <linux/init.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/time.h>
29 #include <linux/wait.h>
30 #include <linux/moduleparam.h>
31 #include <sound/rawmidi.h>
32 #include <sound/info.h>
33 #include <sound/control.h>
34 #include <sound/minors.h>
35 #include <sound/initval.h>
37 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
38 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
39 MODULE_LICENSE("GPL");
41 #ifdef CONFIG_SND_OSSEMUL
42 static int midi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
43 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
45 module_param_array(midi_map, int, boot_devs, 0444);
46 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
47 MODULE_PARM_SYNTAX(midi_map, "default:0,skill:advanced");
48 module_param_array(amidi_map, int, boot_devs, 0444);
49 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
50 MODULE_PARM_SYNTAX(amidi_map, "default:1,skill:advanced");
51 #endif /* CONFIG_SND_OSSEMUL */
53 static int snd_rawmidi_free(snd_rawmidi_t *rawmidi);
54 static int snd_rawmidi_dev_free(snd_device_t *device);
55 static int snd_rawmidi_dev_register(snd_device_t *device);
56 static int snd_rawmidi_dev_disconnect(snd_device_t *device);
57 static int snd_rawmidi_dev_unregister(snd_device_t *device);
59 snd_rawmidi_t *snd_rawmidi_devices[SNDRV_CARDS * SNDRV_RAWMIDI_DEVICES];
61 static DECLARE_MUTEX(register_mutex);
63 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
65 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
67 return SNDRV_RAWMIDI_LFLG_OUTPUT;
69 return SNDRV_RAWMIDI_LFLG_INPUT;
71 return SNDRV_RAWMIDI_LFLG_OPEN;
75 static inline int snd_rawmidi_ready(snd_rawmidi_substream_t * substream)
77 snd_rawmidi_runtime_t *runtime = substream->runtime;
78 return runtime->avail >= runtime->avail_min;
81 static inline int snd_rawmidi_ready_append(snd_rawmidi_substream_t * substream, size_t count)
83 snd_rawmidi_runtime_t *runtime = substream->runtime;
84 return runtime->avail >= runtime->avail_min &&
85 (!substream->append || runtime->avail >= count);
88 static int snd_rawmidi_init(snd_rawmidi_substream_t *substream)
90 snd_rawmidi_runtime_t *runtime = substream->runtime;
91 spin_lock_init(&runtime->lock);
92 init_waitqueue_head(&runtime->sleep);
93 runtime->event = NULL;
94 runtime->buffer_size = PAGE_SIZE;
95 runtime->avail_min = 1;
96 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
99 runtime->avail = runtime->buffer_size;
100 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL)
102 runtime->appl_ptr = runtime->hw_ptr = 0;
106 static int snd_rawmidi_done_buffer(snd_rawmidi_runtime_t *runtime)
108 if (runtime->buffer) {
109 kfree(runtime->buffer);
110 runtime->buffer = NULL;
115 int snd_rawmidi_drop_output(snd_rawmidi_substream_t * substream)
117 snd_rawmidi_runtime_t *runtime = substream->runtime;
119 substream->ops->trigger(substream, 0);
120 runtime->trigger = 0;
122 /* interrupts are not enabled at this moment,
123 so spinlock is not required */
124 runtime->appl_ptr = runtime->hw_ptr = 0;
125 runtime->avail = runtime->buffer_size;
129 int snd_rawmidi_drain_output(snd_rawmidi_substream_t * substream)
133 snd_rawmidi_runtime_t *runtime = substream->runtime;
137 while (runtime->avail < runtime->buffer_size) {
138 timeout = interruptible_sleep_on_timeout(&runtime->sleep, 10 * HZ);
139 if (signal_pending(current)) {
143 if (runtime->avail < runtime->buffer_size && !timeout) {
144 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
150 if (err != -ERESTARTSYS) {
151 /* we need wait a while to make sure that Tx FIFOs are empty */
152 if (substream->ops->drain)
153 substream->ops->drain(substream);
155 set_current_state(TASK_UNINTERRUPTIBLE);
156 schedule_timeout(HZ / 20);
158 snd_rawmidi_drop_output(substream);
163 int snd_rawmidi_drain_input(snd_rawmidi_substream_t * substream)
165 snd_rawmidi_runtime_t *runtime = substream->runtime;
167 substream->ops->trigger(substream, 0);
168 runtime->trigger = 0;
170 /* interrupts aren't enabled at this moment, so spinlock isn't needed */
171 runtime->appl_ptr = runtime->hw_ptr = 0;
176 int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
177 int mode, snd_rawmidi_file_t * rfile)
179 snd_rawmidi_t *rmidi;
180 struct list_head *list1, *list2;
181 snd_rawmidi_substream_t *sinput, *soutput;
182 snd_rawmidi_runtime_t *input = NULL, *output = NULL;
186 rfile->input = rfile->output = NULL;
187 rmidi = snd_rawmidi_devices[(cardnum * SNDRV_RAWMIDI_DEVICES) + device];
192 if (!try_module_get(rmidi->card->module)) {
196 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
197 down(&rmidi->open_mutex);
198 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
199 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
203 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
207 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
208 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
213 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
214 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
218 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
222 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
223 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
228 list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
230 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
232 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
238 sinput = list_entry(list1, snd_rawmidi_substream_t, list);
239 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
241 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
246 list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
248 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
250 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
256 soutput = list_entry(list2, snd_rawmidi_substream_t, list);
257 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
258 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
259 if (soutput->opened && !soutput->append)
266 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
271 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
272 input = snd_kcalloc(sizeof(snd_rawmidi_runtime_t), GFP_KERNEL);
277 sinput->runtime = input;
278 if (snd_rawmidi_init(sinput) < 0) {
282 if ((err = sinput->ops->open(sinput)) < 0) {
283 sinput->runtime = NULL;
287 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
291 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
294 output = snd_kcalloc(sizeof(snd_rawmidi_runtime_t), GFP_KERNEL);
295 if (output == NULL) {
299 soutput->runtime = output;
300 if (snd_rawmidi_init(soutput) < 0) {
301 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
302 sinput->ops->close(sinput);
303 sinput->runtime = NULL;
308 if ((err = soutput->ops->open(soutput)) < 0) {
309 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
310 sinput->ops->close(sinput);
311 sinput->runtime = NULL;
313 soutput->runtime = NULL;
318 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
320 if (soutput->use_count++ == 0)
321 soutput->active_sensing = 1;
322 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
326 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
327 up(&rmidi->open_mutex);
329 rfile->rmidi = rmidi;
330 rfile->input = sinput;
331 rfile->output = soutput;
337 snd_rawmidi_done_buffer(input);
340 if (output != NULL) {
341 snd_rawmidi_done_buffer(output);
344 module_put(rmidi->card->module);
345 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
346 up(&rmidi->open_mutex);
351 static int snd_rawmidi_open(struct inode *inode, struct file *file)
353 int maj = imajor(inode);
356 int device, subdevice;
357 unsigned short fflags;
359 snd_rawmidi_t *rmidi;
360 snd_rawmidi_file_t *rawmidi_file;
362 struct list_head *list;
363 snd_ctl_file_t *kctl;
366 case CONFIG_SND_MAJOR:
367 cardnum = SNDRV_MINOR_CARD(iminor(inode));
368 cardnum %= SNDRV_CARDS;
369 device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_RAWMIDI;
370 device %= SNDRV_MINOR_RAWMIDIS;
372 #ifdef CONFIG_SND_OSSEMUL
374 cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
375 cardnum %= SNDRV_CARDS;
376 device = SNDRV_MINOR_OSS_DEVICE(iminor(inode)) == SNDRV_MINOR_OSS_MIDI ?
377 midi_map[cardnum] : amidi_map[cardnum];
384 rmidi = snd_rawmidi_devices[(cardnum * SNDRV_RAWMIDI_DEVICES) + device];
387 #ifdef CONFIG_SND_OSSEMUL
388 if (maj == SOUND_MAJOR && !rmidi->ossreg)
391 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
392 return -EINVAL; /* invalid combination */
394 err = snd_card_file_add(card, file);
397 fflags = snd_rawmidi_file_flags(file);
398 if ((file->f_flags & O_APPEND) || maj != CONFIG_SND_MAJOR) /* OSS emul? */
399 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
400 fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
401 rawmidi_file = snd_magic_kmalloc(snd_rawmidi_file_t, 0, GFP_KERNEL);
402 if (rawmidi_file == NULL) {
403 snd_card_file_remove(card, file);
406 init_waitqueue_entry(&wait, current);
407 add_wait_queue(&rmidi->open_wait, &wait);
408 down(&rmidi->open_mutex);
411 down_read(&card->controls_rwsem);
412 list_for_each(list, &card->ctl_files) {
413 kctl = snd_ctl_file(list);
414 if (kctl->pid == current->pid) {
415 subdevice = kctl->prefer_rawmidi_subdevice;
419 up_read(&card->controls_rwsem);
420 err = snd_rawmidi_kernel_open(cardnum, device, subdevice, fflags, rawmidi_file);
423 if (err == -EAGAIN) {
424 if (file->f_flags & O_NONBLOCK) {
430 set_current_state(TASK_INTERRUPTIBLE);
431 up(&rmidi->open_mutex);
433 down(&rmidi->open_mutex);
434 if (signal_pending(current)) {
439 #ifdef CONFIG_SND_OSSEMUL
440 if (rawmidi_file->input && rawmidi_file->input->runtime)
441 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
442 if (rawmidi_file->output && rawmidi_file->output->runtime)
443 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
445 remove_wait_queue(&rmidi->open_wait, &wait);
447 file->private_data = rawmidi_file;
449 snd_card_file_remove(card, file);
450 snd_magic_kfree(rawmidi_file);
452 up(&rmidi->open_mutex);
456 int snd_rawmidi_kernel_release(snd_rawmidi_file_t * rfile)
458 snd_rawmidi_t *rmidi;
459 snd_rawmidi_substream_t *substream;
460 snd_rawmidi_runtime_t *runtime;
462 snd_assert(rfile != NULL, return -ENXIO);
463 snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
464 rmidi = rfile->rmidi;
465 down(&rmidi->open_mutex);
466 if (rfile->input != NULL) {
467 substream = rfile->input;
469 runtime = substream->runtime;
470 runtime->trigger = 0;
471 substream->ops->trigger(substream, 0);
472 substream->ops->close(substream);
473 snd_rawmidi_done_buffer(runtime);
474 if (runtime->private_free != NULL)
475 runtime->private_free(substream);
477 substream->runtime = NULL;
478 substream->opened = 0;
479 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
481 if (rfile->output != NULL) {
482 substream = rfile->output;
483 rfile->output = NULL;
484 if (--substream->use_count == 0) {
485 runtime = substream->runtime;
486 if (substream->active_sensing) {
487 unsigned char buf = 0xfe;
488 /* sending single active sensing message to shut the device up */
489 snd_rawmidi_kernel_write(substream, &buf, 1);
491 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
492 substream->ops->trigger(substream, 0);
493 substream->ops->close(substream);
494 snd_rawmidi_done_buffer(runtime);
495 if (runtime->private_free != NULL)
496 runtime->private_free(substream);
498 substream->runtime = NULL;
499 substream->opened = 0;
500 substream->append = 0;
502 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
504 up(&rmidi->open_mutex);
505 module_put(rmidi->card->module);
509 static int snd_rawmidi_release(struct inode *inode, struct file *file)
511 snd_rawmidi_file_t *rfile;
512 snd_rawmidi_t *rmidi;
515 rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
516 err = snd_rawmidi_kernel_release(rfile);
517 rmidi = rfile->rmidi;
518 wake_up(&rmidi->open_wait);
519 snd_magic_kfree(rfile);
520 snd_card_file_remove(rmidi->card, file);
524 int snd_rawmidi_info(snd_rawmidi_substream_t *substream, snd_rawmidi_info_t *info)
526 snd_rawmidi_t *rmidi;
528 if (substream == NULL)
530 rmidi = substream->rmidi;
531 memset(info, 0, sizeof(*info));
532 info->card = rmidi->card->number;
533 info->device = rmidi->device;
534 info->subdevice = substream->number;
535 info->stream = substream->stream;
536 info->flags = rmidi->info_flags;
537 strcpy(info->id, rmidi->id);
538 strcpy(info->name, rmidi->name);
539 strcpy(info->subname, substream->name);
540 info->subdevices_count = substream->pstr->substream_count;
541 info->subdevices_avail = (substream->pstr->substream_count -
542 substream->pstr->substream_opened);
546 static int snd_rawmidi_info_user(snd_rawmidi_substream_t *substream, snd_rawmidi_info_t __user * _info)
548 snd_rawmidi_info_t info;
550 if ((err = snd_rawmidi_info(substream, &info)) < 0)
552 if (copy_to_user(_info, &info, sizeof(snd_rawmidi_info_t)))
557 int snd_rawmidi_info_select(snd_card_t *card, snd_rawmidi_info_t *info)
559 snd_rawmidi_t *rmidi;
560 snd_rawmidi_str_t *pstr;
561 snd_rawmidi_substream_t *substream;
562 struct list_head *list;
563 if (info->device >= SNDRV_RAWMIDI_DEVICES)
565 rmidi = snd_rawmidi_devices[card->number * SNDRV_RAWMIDI_DEVICES + info->device];
566 if (info->stream < 0 || info->stream > 1)
568 pstr = &rmidi->streams[info->stream];
569 if (pstr->substream_count == 0)
571 if (info->subdevice >= pstr->substream_count)
573 list_for_each(list, &pstr->substreams) {
574 substream = list_entry(list, snd_rawmidi_substream_t, list);
575 if ((unsigned int)substream->number == info->subdevice)
576 return snd_rawmidi_info(substream, info);
581 static int snd_rawmidi_info_select_user(snd_card_t *card,
582 snd_rawmidi_info_t __user *_info)
585 snd_rawmidi_info_t info;
586 if (get_user(info.device, &_info->device))
588 if (get_user(info.stream, &_info->stream))
590 if (get_user(info.subdevice, &_info->subdevice))
592 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
594 if (copy_to_user(_info, &info, sizeof(snd_rawmidi_info_t)))
599 int snd_rawmidi_output_params(snd_rawmidi_substream_t * substream,
600 snd_rawmidi_params_t * params)
603 snd_rawmidi_runtime_t *runtime = substream->runtime;
605 if (substream->append && substream->use_count > 1)
607 snd_rawmidi_drain_output(substream);
608 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
611 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
614 if (params->buffer_size != runtime->buffer_size) {
615 if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL)
617 kfree(runtime->buffer);
618 runtime->buffer = newbuf;
619 runtime->buffer_size = params->buffer_size;
621 runtime->avail_min = params->avail_min;
622 substream->active_sensing = !params->no_active_sensing;
626 int snd_rawmidi_input_params(snd_rawmidi_substream_t * substream,
627 snd_rawmidi_params_t * params)
630 snd_rawmidi_runtime_t *runtime = substream->runtime;
632 snd_rawmidi_drain_input(substream);
633 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
636 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
639 if (params->buffer_size != runtime->buffer_size) {
640 if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL)
642 kfree(runtime->buffer);
643 runtime->buffer = newbuf;
644 runtime->buffer_size = params->buffer_size;
646 runtime->avail_min = params->avail_min;
650 static int snd_rawmidi_output_status(snd_rawmidi_substream_t * substream,
651 snd_rawmidi_status_t * status)
653 snd_rawmidi_runtime_t *runtime = substream->runtime;
655 memset(status, 0, sizeof(*status));
656 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
657 spin_lock_irq(&runtime->lock);
658 status->avail = runtime->avail;
659 spin_unlock_irq(&runtime->lock);
663 static int snd_rawmidi_input_status(snd_rawmidi_substream_t * substream,
664 snd_rawmidi_status_t * status)
666 snd_rawmidi_runtime_t *runtime = substream->runtime;
668 memset(status, 0, sizeof(*status));
669 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
670 spin_lock_irq(&runtime->lock);
671 status->avail = runtime->avail;
672 status->xruns = runtime->xruns;
674 spin_unlock_irq(&runtime->lock);
678 static int snd_rawmidi_ioctl(struct inode *inode, struct file *file,
679 unsigned int cmd, unsigned long arg)
681 snd_rawmidi_file_t *rfile;
682 void __user *argp = (void __user *)arg;
684 rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
685 if (((cmd >> 8) & 0xff) != 'W')
688 case SNDRV_RAWMIDI_IOCTL_PVERSION:
689 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
690 case SNDRV_RAWMIDI_IOCTL_INFO:
692 snd_rawmidi_stream_t stream;
693 snd_rawmidi_info_t __user *info = argp;
694 if (get_user(stream, &info->stream))
697 case SNDRV_RAWMIDI_STREAM_INPUT:
698 return snd_rawmidi_info_user(rfile->input, info);
699 case SNDRV_RAWMIDI_STREAM_OUTPUT:
700 return snd_rawmidi_info_user(rfile->output, info);
705 case SNDRV_RAWMIDI_IOCTL_PARAMS:
707 snd_rawmidi_params_t params;
708 if (copy_from_user(¶ms, argp, sizeof(snd_rawmidi_params_t)))
710 switch (params.stream) {
711 case SNDRV_RAWMIDI_STREAM_OUTPUT:
712 if (rfile->output == NULL)
714 return snd_rawmidi_output_params(rfile->output, ¶ms);
715 case SNDRV_RAWMIDI_STREAM_INPUT:
716 if (rfile->input == NULL)
718 return snd_rawmidi_input_params(rfile->input, ¶ms);
723 case SNDRV_RAWMIDI_IOCTL_STATUS:
726 snd_rawmidi_status_t status;
727 if (copy_from_user(&status, argp, sizeof(snd_rawmidi_status_t)))
729 switch (status.stream) {
730 case SNDRV_RAWMIDI_STREAM_OUTPUT:
731 if (rfile->output == NULL)
733 err = snd_rawmidi_output_status(rfile->output, &status);
735 case SNDRV_RAWMIDI_STREAM_INPUT:
736 if (rfile->input == NULL)
738 err = snd_rawmidi_input_status(rfile->input, &status);
745 if (copy_to_user(argp, &status, sizeof(snd_rawmidi_status_t)))
749 case SNDRV_RAWMIDI_IOCTL_DROP:
752 if (get_user(val, (long __user *) argp))
755 case SNDRV_RAWMIDI_STREAM_OUTPUT:
756 if (rfile->output == NULL)
758 return snd_rawmidi_drop_output(rfile->output);
763 case SNDRV_RAWMIDI_IOCTL_DRAIN:
766 if (get_user(val, (long __user *) argp))
769 case SNDRV_RAWMIDI_STREAM_OUTPUT:
770 if (rfile->output == NULL)
772 return snd_rawmidi_drain_output(rfile->output);
773 case SNDRV_RAWMIDI_STREAM_INPUT:
774 if (rfile->input == NULL)
776 return snd_rawmidi_drain_input(rfile->input);
781 #ifdef CONFIG_SND_DEBUG
783 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
789 int snd_rawmidi_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
790 unsigned int cmd, unsigned long arg)
792 void __user *argp = (void __user *)arg;
795 tmp = card->number * SNDRV_RAWMIDI_DEVICES;
797 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
801 if (get_user(device, (int __user *)argp))
803 device = device < 0 ? 0 : device + 1;
804 while (device < SNDRV_RAWMIDI_DEVICES) {
805 if (snd_rawmidi_devices[tmp + device])
809 if (device == SNDRV_RAWMIDI_DEVICES)
811 if (put_user(device, (int __user *)argp))
815 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
819 if (get_user(val, (int __user *)argp))
821 control->prefer_rawmidi_subdevice = val;
824 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
825 return snd_rawmidi_info_select_user(card, argp);
831 * snd_rawmidi_receive - receive the input data from the device
832 * @substream: the rawmidi substream
833 * @buffer: the buffer pointer
834 * @count: the data size to read
836 * Reads the data from the internal buffer.
838 * Returns the size of read data, or a negative error code on failure.
840 int snd_rawmidi_receive(snd_rawmidi_substream_t * substream, const unsigned char *buffer, int count)
843 int result = 0, count1;
844 snd_rawmidi_runtime_t *runtime = substream->runtime;
846 if (runtime->buffer == NULL) {
847 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
850 spin_lock_irqsave(&runtime->lock, flags);
851 if (count == 1) { /* special case, faster code */
853 if (runtime->avail < runtime->buffer_size) {
854 runtime->buffer[runtime->hw_ptr++] = buffer[0];
855 runtime->hw_ptr %= runtime->buffer_size;
862 substream->bytes += count;
863 count1 = runtime->buffer_size - runtime->hw_ptr;
866 if (count1 > (int)(runtime->buffer_size - runtime->avail))
867 count1 = runtime->buffer_size - runtime->avail;
868 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
869 runtime->hw_ptr += count1;
870 runtime->hw_ptr %= runtime->buffer_size;
871 runtime->avail += count1;
877 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
878 count1 = runtime->buffer_size - runtime->avail;
879 runtime->xruns = count - count1;
882 memcpy(runtime->buffer, buffer, count1);
883 runtime->hw_ptr = count1;
884 runtime->avail += count1;
889 if (result > 0 && runtime->event == NULL) {
890 if (snd_rawmidi_ready(substream))
891 wake_up(&runtime->sleep);
893 spin_unlock_irqrestore(&runtime->lock, flags);
894 if (result > 0 && runtime->event)
895 runtime->event(substream);
899 static long snd_rawmidi_kernel_read1(snd_rawmidi_substream_t *substream,
900 unsigned char *buf, long count, int kernel)
903 long result = 0, count1;
904 snd_rawmidi_runtime_t *runtime = substream->runtime;
906 while (count > 0 && runtime->avail) {
907 count1 = runtime->buffer_size - runtime->appl_ptr;
910 spin_lock_irqsave(&runtime->lock, flags);
911 if (count1 > (int)runtime->avail)
912 count1 = runtime->avail;
914 memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
916 spin_unlock_irqrestore(&runtime->lock, flags);
917 if (copy_to_user(buf + result, runtime->buffer + runtime->appl_ptr, count1)) {
918 return result > 0 ? result : -EFAULT;
920 spin_lock_irqsave(&runtime->lock, flags);
922 runtime->appl_ptr += count1;
923 runtime->appl_ptr %= runtime->buffer_size;
924 runtime->avail -= count1;
925 spin_unlock_irqrestore(&runtime->lock, flags);
932 long snd_rawmidi_kernel_read(snd_rawmidi_substream_t *substream, unsigned char *buf, long count)
934 substream->runtime->trigger = 1;
935 substream->ops->trigger(substream, 1);
936 return snd_rawmidi_kernel_read1(substream, buf, count, 1);
939 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
943 snd_rawmidi_file_t *rfile;
944 snd_rawmidi_substream_t *substream;
945 snd_rawmidi_runtime_t *runtime;
947 rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
948 substream = rfile->input;
949 if (substream == NULL)
951 runtime = substream->runtime;
952 runtime->trigger = 1;
953 substream->ops->trigger(substream, 1);
956 spin_lock_irq(&runtime->lock);
957 while (!snd_rawmidi_ready(substream)) {
959 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
960 spin_unlock_irq(&runtime->lock);
961 return result > 0 ? result : -EAGAIN;
963 init_waitqueue_entry(&wait, current);
964 add_wait_queue(&runtime->sleep, &wait);
965 set_current_state(TASK_INTERRUPTIBLE);
966 spin_unlock_irq(&runtime->lock);
968 remove_wait_queue(&runtime->sleep, &wait);
969 if (signal_pending(current))
970 return result > 0 ? result : -ERESTARTSYS;
972 return result > 0 ? result : -EIO;
973 spin_lock_irq(&runtime->lock);
975 spin_unlock_irq(&runtime->lock);
976 count1 = snd_rawmidi_kernel_read1(substream, buf, count, 0);
985 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
986 * @substream: the rawmidi substream
988 * Returns 1 if the internal output buffer is empty, 0 if not.
990 int snd_rawmidi_transmit_empty(snd_rawmidi_substream_t * substream)
992 snd_rawmidi_runtime_t *runtime = substream->runtime;
996 if (runtime->buffer == NULL) {
997 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1000 spin_lock_irqsave(&runtime->lock, flags);
1001 result = runtime->avail >= runtime->buffer_size;
1003 runtime->trigger = 1;
1004 spin_unlock_irqrestore(&runtime->lock, flags);
1009 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1010 * @substream: the rawmidi substream
1011 * @buffer: the buffer pointer
1012 * @count: data size to transfer
1014 * Copies data from the internal output buffer to the given buffer.
1016 * Call this in the interrupt handler when the midi output is ready,
1017 * and call snd_rawmidi_transmit_ack() after the transmission is
1020 * Returns the size of copied data, or a negative error code on failure.
1022 int snd_rawmidi_transmit_peek(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
1024 unsigned long flags;
1026 snd_rawmidi_runtime_t *runtime = substream->runtime;
1028 if (runtime->buffer == NULL) {
1029 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1033 spin_lock_irqsave(&runtime->lock, flags);
1034 if (runtime->avail >= runtime->buffer_size) {
1035 /* warning: lowlevel layer MUST trigger down the hardware */
1036 runtime->trigger = 0;
1039 if (count == 1) { /* special case, faster code */
1040 *buffer = runtime->buffer[runtime->hw_ptr];
1043 count1 = runtime->buffer_size - runtime->hw_ptr;
1046 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1047 count1 = runtime->buffer_size - runtime->avail;
1048 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1052 memcpy(buffer + count1, runtime->buffer, count);
1055 spin_unlock_irqrestore(&runtime->lock, flags);
1060 * snd_rawmidi_transmit_ack - acknowledge the transmission
1061 * @substream: the rawmidi substream
1062 * @count: the tranferred count
1064 * Advances the hardware pointer for the internal output buffer with
1065 * the given size and updates the condition.
1066 * Call after the transmission is finished.
1068 * Returns the advanced size if successful, or a negative error code on failure.
1070 int snd_rawmidi_transmit_ack(snd_rawmidi_substream_t * substream, int count)
1072 unsigned long flags;
1073 snd_rawmidi_runtime_t *runtime = substream->runtime;
1075 if (runtime->buffer == NULL) {
1076 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1079 spin_lock_irqsave(&runtime->lock, flags);
1080 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1081 runtime->hw_ptr += count;
1082 runtime->hw_ptr %= runtime->buffer_size;
1083 runtime->avail += count;
1084 substream->bytes += count;
1086 wake_up(&runtime->sleep);
1088 if (count > 0 && runtime->event == NULL)
1089 if (snd_rawmidi_ready(substream))
1090 wake_up(&runtime->sleep);
1091 spin_unlock_irqrestore(&runtime->lock, flags);
1092 if (count > 0 && runtime->event)
1093 runtime->event(substream);
1098 * snd_rawmidi_transmit - copy from the buffer to the device
1099 * @substream: the rawmidi substream
1100 * @buf: the buffer pointer
1101 * @count: the data size to transfer
1103 * Copies data from the buffer to the device and advances the pointer.
1105 * Returns the copied size if successful, or a negative error code on failure.
1107 int snd_rawmidi_transmit(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
1109 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1112 return snd_rawmidi_transmit_ack(substream, count);
1115 static long snd_rawmidi_kernel_write1(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count, int kernel)
1117 unsigned long flags;
1118 long count1, result;
1119 snd_rawmidi_runtime_t *runtime = substream->runtime;
1121 snd_assert(buf != NULL, return -EINVAL);
1122 snd_assert(runtime->buffer != NULL, return -EINVAL);
1125 spin_lock_irqsave(&runtime->lock, flags);
1126 if (substream->append) {
1127 if ((long)runtime->avail < count) {
1128 spin_unlock_irqrestore(&runtime->lock, flags);
1132 while (count > 0 && runtime->avail > 0) {
1133 count1 = runtime->buffer_size - runtime->appl_ptr;
1136 if (count1 > (long)runtime->avail)
1137 count1 = runtime->avail;
1139 memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1141 spin_unlock_irqrestore(&runtime->lock, flags);
1142 if (copy_from_user(runtime->buffer + runtime->appl_ptr, buf, count1)) {
1143 spin_lock_irqsave(&runtime->lock, flags);
1144 result = result > 0 ? result : -EFAULT;
1147 spin_lock_irqsave(&runtime->lock, flags);
1149 runtime->appl_ptr += count1;
1150 runtime->appl_ptr %= runtime->buffer_size;
1151 runtime->avail -= count1;
1158 runtime->trigger = 1;
1159 count1 = runtime->avail < runtime->buffer_size;
1160 spin_unlock_irqrestore(&runtime->lock, flags);
1162 substream->ops->trigger(substream, 1);
1166 long snd_rawmidi_kernel_write(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count)
1168 return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1171 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
1173 long result, timeout;
1175 snd_rawmidi_file_t *rfile;
1176 snd_rawmidi_runtime_t *runtime;
1177 snd_rawmidi_substream_t *substream;
1179 rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
1180 substream = rfile->output;
1181 runtime = substream->runtime;
1182 /* we cannot put an atomic message to our buffer */
1183 if (substream->append && count > runtime->buffer_size)
1187 spin_lock_irq(&runtime->lock);
1188 while (!snd_rawmidi_ready_append(substream, count)) {
1190 if (file->f_flags & O_NONBLOCK) {
1191 spin_unlock_irq(&runtime->lock);
1192 return result > 0 ? result : -EAGAIN;
1194 init_waitqueue_entry(&wait, current);
1195 add_wait_queue(&runtime->sleep, &wait);
1196 set_current_state(TASK_INTERRUPTIBLE);
1197 spin_unlock_irq(&runtime->lock);
1198 timeout = schedule_timeout(30 * HZ);
1199 remove_wait_queue(&runtime->sleep, &wait);
1200 if (signal_pending(current))
1201 return result > 0 ? result : -ERESTARTSYS;
1202 if (!runtime->avail && !timeout)
1203 return result > 0 ? result : -EIO;
1204 spin_lock_irq(&runtime->lock);
1206 spin_unlock_irq(&runtime->lock);
1207 count1 = snd_rawmidi_kernel_write1(substream, buf, count, 0);
1212 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1216 while (file->f_flags & O_SYNC) {
1217 spin_lock_irq(&runtime->lock);
1218 while (runtime->avail != runtime->buffer_size) {
1220 unsigned int last_avail = runtime->avail;
1221 init_waitqueue_entry(&wait, current);
1222 add_wait_queue(&runtime->sleep, &wait);
1223 set_current_state(TASK_INTERRUPTIBLE);
1224 spin_unlock_irq(&runtime->lock);
1225 timeout = schedule_timeout(30 * HZ);
1226 remove_wait_queue(&runtime->sleep, &wait);
1227 if (signal_pending(current))
1228 return result > 0 ? result : -ERESTARTSYS;
1229 if (runtime->avail == last_avail && !timeout)
1230 return result > 0 ? result : -EIO;
1231 spin_lock_irq(&runtime->lock);
1233 spin_unlock_irq(&runtime->lock);
1238 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1240 snd_rawmidi_file_t *rfile;
1241 snd_rawmidi_runtime_t *runtime;
1244 rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return 0);
1245 if (rfile->input != NULL) {
1246 runtime = rfile->input->runtime;
1247 runtime->trigger = 1;
1248 rfile->input->ops->trigger(rfile->input, 1);
1249 poll_wait(file, &runtime->sleep, wait);
1251 if (rfile->output != NULL) {
1252 runtime = rfile->output->runtime;
1253 poll_wait(file, &runtime->sleep, wait);
1256 if (rfile->input != NULL) {
1257 if (snd_rawmidi_ready(rfile->input))
1258 mask |= POLLIN | POLLRDNORM;
1260 if (rfile->output != NULL) {
1261 if (snd_rawmidi_ready(rfile->output))
1262 mask |= POLLOUT | POLLWRNORM;
1271 static void snd_rawmidi_proc_info_read(snd_info_entry_t *entry,
1272 snd_info_buffer_t * buffer)
1274 snd_rawmidi_t *rmidi;
1275 snd_rawmidi_substream_t *substream;
1276 snd_rawmidi_runtime_t *runtime;
1277 struct list_head *list;
1279 rmidi = snd_magic_cast(snd_rawmidi_t, entry->private_data, return);
1280 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1281 down(&rmidi->open_mutex);
1282 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1283 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
1284 substream = list_entry(list, snd_rawmidi_substream_t, list);
1287 " Tx bytes : %lu\n",
1289 (unsigned long) substream->bytes);
1290 if (substream->opened) {
1291 runtime = substream->runtime;
1294 " Buffer size : %lu\n"
1296 runtime->oss ? "OSS compatible" : "native",
1297 (unsigned long) runtime->buffer_size,
1298 (unsigned long) runtime->avail);
1302 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1303 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
1304 substream = list_entry(list, snd_rawmidi_substream_t, list);
1307 " Rx bytes : %lu\n",
1309 (unsigned long) substream->bytes);
1310 if (substream->opened) {
1311 runtime = substream->runtime;
1313 " Buffer size : %lu\n"
1315 " Overruns : %lu\n",
1316 (unsigned long) runtime->buffer_size,
1317 (unsigned long) runtime->avail,
1318 (unsigned long) runtime->xruns);
1322 up(&rmidi->open_mutex);
1326 * Register functions
1329 static struct file_operations snd_rawmidi_f_ops =
1331 .owner = THIS_MODULE,
1332 .read = snd_rawmidi_read,
1333 .write = snd_rawmidi_write,
1334 .open = snd_rawmidi_open,
1335 .release = snd_rawmidi_release,
1336 .poll = snd_rawmidi_poll,
1337 .ioctl = snd_rawmidi_ioctl,
1340 static snd_minor_t snd_rawmidi_reg =
1342 .comment = "raw midi",
1343 .f_ops = &snd_rawmidi_f_ops,
1346 static int snd_rawmidi_alloc_substreams(snd_rawmidi_t *rmidi,
1347 snd_rawmidi_str_t *stream,
1351 snd_rawmidi_substream_t *substream;
1354 INIT_LIST_HEAD(&stream->substreams);
1355 for (idx = 0; idx < count; idx++) {
1356 substream = snd_kcalloc(sizeof(snd_rawmidi_substream_t), GFP_KERNEL);
1357 if (substream == NULL)
1359 substream->stream = direction;
1360 substream->number = idx;
1361 substream->rmidi = rmidi;
1362 substream->pstr = stream;
1363 list_add_tail(&substream->list, &stream->substreams);
1364 stream->substream_count++;
1370 * snd_rawmidi_new - create a rawmidi instance
1371 * @card: the card instance
1372 * @id: the id string
1373 * @device: the device index
1374 * @output_count: the number of output streams
1375 * @input_count: the number of input streams
1376 * @rrawmidi: the pointer to store the new rawmidi instance
1378 * Creates a new rawmidi instance.
1379 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1381 * Returns zero if successful, or a negative error code on failure.
1383 int snd_rawmidi_new(snd_card_t * card, char *id, int device,
1384 int output_count, int input_count,
1385 snd_rawmidi_t ** rrawmidi)
1387 snd_rawmidi_t *rmidi;
1389 static snd_device_ops_t ops = {
1390 .dev_free = snd_rawmidi_dev_free,
1391 .dev_register = snd_rawmidi_dev_register,
1392 .dev_disconnect = snd_rawmidi_dev_disconnect,
1393 .dev_unregister = snd_rawmidi_dev_unregister
1396 snd_assert(rrawmidi != NULL, return -EINVAL);
1398 snd_assert(card != NULL, return -ENXIO);
1399 rmidi = snd_magic_kcalloc(snd_rawmidi_t, 0, GFP_KERNEL);
1403 rmidi->device = device;
1404 init_MUTEX(&rmidi->open_mutex);
1405 init_waitqueue_head(&rmidi->open_wait);
1407 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1408 if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT], SNDRV_RAWMIDI_STREAM_INPUT, input_count)) < 0) {
1409 snd_rawmidi_free(rmidi);
1412 if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT], SNDRV_RAWMIDI_STREAM_OUTPUT, output_count)) < 0) {
1413 snd_rawmidi_free(rmidi);
1416 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1417 snd_rawmidi_free(rmidi);
1424 static void snd_rawmidi_free_substreams(snd_rawmidi_str_t *stream)
1426 snd_rawmidi_substream_t *substream;
1428 while (!list_empty(&stream->substreams)) {
1429 substream = list_entry(stream->substreams.next, snd_rawmidi_substream_t, list);
1430 list_del(&substream->list);
1435 static int snd_rawmidi_free(snd_rawmidi_t *rmidi)
1437 snd_assert(rmidi != NULL, return -ENXIO);
1438 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1439 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1440 if (rmidi->private_free)
1441 rmidi->private_free(rmidi);
1442 snd_magic_kfree(rmidi);
1446 static int snd_rawmidi_dev_free(snd_device_t *device)
1448 snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
1449 return snd_rawmidi_free(rmidi);
1452 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1453 static void snd_rawmidi_dev_seq_free(snd_seq_device_t *device)
1455 snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->private_data, return);
1456 rmidi->seq_dev = NULL;
1460 static int snd_rawmidi_dev_register(snd_device_t *device)
1463 snd_info_entry_t *entry;
1465 snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
1467 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1469 down(®ister_mutex);
1470 idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1471 if (snd_rawmidi_devices[idx] != NULL) {
1472 up(®ister_mutex);
1475 snd_rawmidi_devices[idx] = rmidi;
1476 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1477 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1478 rmidi->card, rmidi->device,
1479 &snd_rawmidi_reg, name)) < 0) {
1480 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1481 snd_rawmidi_devices[idx] = NULL;
1482 up(®ister_mutex);
1485 if (rmidi->ops && rmidi->ops->dev_register &&
1486 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1487 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1488 snd_rawmidi_devices[idx] = NULL;
1489 up(®ister_mutex);
1492 #ifdef CONFIG_SND_OSSEMUL
1494 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1495 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1496 rmidi->card, 0, &snd_rawmidi_reg, name) < 0) {
1497 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1500 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1501 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1505 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1506 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1507 rmidi->card, 1, &snd_rawmidi_reg, name) < 0) {
1508 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1513 #endif /* CONFIG_SND_OSSEMUL */
1514 up(®ister_mutex);
1515 sprintf(name, "midi%d", rmidi->device);
1516 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1518 entry->private_data = rmidi;
1519 entry->c.text.read_size = 1024;
1520 entry->c.text.read = snd_rawmidi_proc_info_read;
1521 if (snd_info_register(entry) < 0) {
1522 snd_info_free_entry(entry);
1526 rmidi->proc_entry = entry;
1527 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1528 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1529 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1530 rmidi->seq_dev->private_data = rmidi;
1531 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1532 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1533 snd_device_register(rmidi->card, rmidi->seq_dev);
1540 static int snd_rawmidi_dev_disconnect(snd_device_t *device)
1542 snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
1545 down(®ister_mutex);
1546 idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1547 snd_rawmidi_devices[idx] = NULL;
1548 up(®ister_mutex);
1552 static int snd_rawmidi_dev_unregister(snd_device_t *device)
1555 snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
1557 snd_assert(rmidi != NULL, return -ENXIO);
1558 down(®ister_mutex);
1559 idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1560 snd_rawmidi_devices[idx] = NULL;
1561 if (rmidi->proc_entry) {
1562 snd_info_unregister(rmidi->proc_entry);
1563 rmidi->proc_entry = NULL;
1565 #ifdef CONFIG_SND_OSSEMUL
1566 if (rmidi->ossreg) {
1567 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1568 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1569 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1570 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1573 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1574 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1577 #endif /* CONFIG_SND_OSSEMUL */
1578 if (rmidi->ops && rmidi->ops->dev_unregister)
1579 rmidi->ops->dev_unregister(rmidi);
1580 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1581 up(®ister_mutex);
1582 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1583 if (rmidi->seq_dev) {
1584 snd_device_free(rmidi->card, rmidi->seq_dev);
1585 rmidi->seq_dev = NULL;
1588 return snd_rawmidi_free(rmidi);
1592 * snd_rawmidi_set_ops - set the rawmidi operators
1593 * @rmidi: the rawmidi instance
1594 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1595 * @ops: the operator table
1597 * Sets the rawmidi operators for the given stream direction.
1599 void snd_rawmidi_set_ops(snd_rawmidi_t *rmidi, int stream, snd_rawmidi_ops_t *ops)
1601 struct list_head *list;
1602 snd_rawmidi_substream_t *substream;
1604 list_for_each(list, &rmidi->streams[stream].substreams) {
1605 substream = list_entry(list, snd_rawmidi_substream_t, list);
1606 substream->ops = ops;
1614 static int __init alsa_rawmidi_init(void)
1617 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1618 #ifdef CONFIG_SND_OSSEMUL
1620 /* check device map table */
1621 for (i = 0; i < SNDRV_CARDS; i++) {
1622 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1623 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1626 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1627 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1632 #endif /* CONFIG_SND_OSSEMUL */
1636 static void __exit alsa_rawmidi_exit(void)
1638 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1641 module_init(alsa_rawmidi_init)
1642 module_exit(alsa_rawmidi_exit)
1644 EXPORT_SYMBOL(snd_rawmidi_output_params);
1645 EXPORT_SYMBOL(snd_rawmidi_input_params);
1646 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1647 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1648 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1649 EXPORT_SYMBOL(snd_rawmidi_receive);
1650 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1651 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1652 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1653 EXPORT_SYMBOL(snd_rawmidi_transmit);
1654 EXPORT_SYMBOL(snd_rawmidi_new);
1655 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1656 EXPORT_SYMBOL(snd_rawmidi_info);
1657 EXPORT_SYMBOL(snd_rawmidi_info_select);
1658 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1659 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1660 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1661 EXPORT_SYMBOL(snd_rawmidi_kernel_write);