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 * _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 *_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;
683 rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
684 if (((cmd >> 8) & 0xff) != 'W')
687 case SNDRV_RAWMIDI_IOCTL_PVERSION:
688 return put_user(SNDRV_RAWMIDI_VERSION, (int *)arg) ? -EFAULT : 0;
689 case SNDRV_RAWMIDI_IOCTL_INFO:
691 snd_rawmidi_stream_t stream;
692 snd_rawmidi_info_t *info = (snd_rawmidi_info_t *) arg;
693 if (get_user(stream, &info->stream))
696 case SNDRV_RAWMIDI_STREAM_INPUT:
697 return snd_rawmidi_info_user(rfile->input, info);
698 case SNDRV_RAWMIDI_STREAM_OUTPUT:
699 return snd_rawmidi_info_user(rfile->output, info);
704 case SNDRV_RAWMIDI_IOCTL_PARAMS:
706 snd_rawmidi_params_t params;
707 if (copy_from_user(¶ms, (snd_rawmidi_params_t *) arg, sizeof(snd_rawmidi_params_t)))
709 switch (params.stream) {
710 case SNDRV_RAWMIDI_STREAM_OUTPUT:
711 if (rfile->output == NULL)
713 return snd_rawmidi_output_params(rfile->output, ¶ms);
714 case SNDRV_RAWMIDI_STREAM_INPUT:
715 if (rfile->input == NULL)
717 return snd_rawmidi_input_params(rfile->input, ¶ms);
722 case SNDRV_RAWMIDI_IOCTL_STATUS:
725 snd_rawmidi_status_t status;
726 if (copy_from_user(&status, (snd_rawmidi_status_t *) arg, sizeof(snd_rawmidi_status_t)))
728 switch (status.stream) {
729 case SNDRV_RAWMIDI_STREAM_OUTPUT:
730 if (rfile->output == NULL)
732 err = snd_rawmidi_output_status(rfile->output, &status);
734 case SNDRV_RAWMIDI_STREAM_INPUT:
735 if (rfile->input == NULL)
737 err = snd_rawmidi_input_status(rfile->input, &status);
744 if (copy_to_user((snd_rawmidi_status_t *) arg, &status, sizeof(snd_rawmidi_status_t)))
748 case SNDRV_RAWMIDI_IOCTL_DROP:
751 if (get_user(val, (long *) arg))
754 case SNDRV_RAWMIDI_STREAM_OUTPUT:
755 if (rfile->output == NULL)
757 return snd_rawmidi_drop_output(rfile->output);
762 case SNDRV_RAWMIDI_IOCTL_DRAIN:
765 if (get_user(val, (long *) arg))
768 case SNDRV_RAWMIDI_STREAM_OUTPUT:
769 if (rfile->output == NULL)
771 return snd_rawmidi_drain_output(rfile->output);
772 case SNDRV_RAWMIDI_STREAM_INPUT:
773 if (rfile->input == NULL)
775 return snd_rawmidi_drain_input(rfile->input);
780 #ifdef CONFIG_SND_DEBUG
782 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
788 int snd_rawmidi_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
789 unsigned int cmd, unsigned long arg)
793 tmp = card->number * SNDRV_RAWMIDI_DEVICES;
795 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
799 if (get_user(device, (int *)arg))
801 device = device < 0 ? 0 : device + 1;
802 while (device < SNDRV_RAWMIDI_DEVICES) {
803 if (snd_rawmidi_devices[tmp + device])
807 if (device == SNDRV_RAWMIDI_DEVICES)
809 if (put_user(device, (int *)arg))
813 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
817 if (get_user(val, (int *)arg))
819 control->prefer_rawmidi_subdevice = val;
822 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
823 return snd_rawmidi_info_select_user(card, (snd_rawmidi_info_t *)arg);
829 * snd_rawmidi_receive - receive the input data from the device
830 * @substream: the rawmidi substream
831 * @buffer: the buffer pointer
832 * @count: the data size to read
834 * Reads the data from the internal buffer.
836 * Returns the size of read data, or a negative error code on failure.
838 int snd_rawmidi_receive(snd_rawmidi_substream_t * substream, const unsigned char *buffer, int count)
841 int result = 0, count1;
842 snd_rawmidi_runtime_t *runtime = substream->runtime;
844 if (runtime->buffer == NULL) {
845 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
848 spin_lock_irqsave(&runtime->lock, flags);
849 if (count == 1) { /* special case, faster code */
851 if (runtime->avail < runtime->buffer_size) {
852 runtime->buffer[runtime->hw_ptr++] = buffer[0];
853 runtime->hw_ptr %= runtime->buffer_size;
860 substream->bytes += count;
861 count1 = runtime->buffer_size - runtime->hw_ptr;
864 if (count1 > (int)(runtime->buffer_size - runtime->avail))
865 count1 = runtime->buffer_size - runtime->avail;
866 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
867 runtime->hw_ptr += count1;
868 runtime->hw_ptr %= runtime->buffer_size;
869 runtime->avail += count1;
875 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
876 count1 = runtime->buffer_size - runtime->avail;
877 runtime->xruns = count - count1;
880 memcpy(runtime->buffer, buffer, count1);
881 runtime->hw_ptr = count1;
882 runtime->avail += count1;
887 if (result > 0 && runtime->event == NULL) {
888 if (snd_rawmidi_ready(substream))
889 wake_up(&runtime->sleep);
891 spin_unlock_irqrestore(&runtime->lock, flags);
892 if (result > 0 && runtime->event)
893 runtime->event(substream);
897 static long snd_rawmidi_kernel_read1(snd_rawmidi_substream_t *substream,
898 unsigned char *buf, long count, int kernel)
901 long result = 0, count1;
902 snd_rawmidi_runtime_t *runtime = substream->runtime;
904 while (count > 0 && runtime->avail) {
905 count1 = runtime->buffer_size - runtime->appl_ptr;
908 spin_lock_irqsave(&runtime->lock, flags);
909 if (count1 > (int)runtime->avail)
910 count1 = runtime->avail;
912 memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
914 spin_unlock_irqrestore(&runtime->lock, flags);
915 if (copy_to_user(buf + result, runtime->buffer + runtime->appl_ptr, count1)) {
916 return result > 0 ? result : -EFAULT;
918 spin_lock_irqsave(&runtime->lock, flags);
920 runtime->appl_ptr += count1;
921 runtime->appl_ptr %= runtime->buffer_size;
922 runtime->avail -= count1;
923 spin_unlock_irqrestore(&runtime->lock, flags);
930 long snd_rawmidi_kernel_read(snd_rawmidi_substream_t *substream, unsigned char *buf, long count)
932 substream->runtime->trigger = 1;
933 substream->ops->trigger(substream, 1);
934 return snd_rawmidi_kernel_read1(substream, buf, count, 1);
937 static ssize_t snd_rawmidi_read(struct file *file, char *buf, size_t count, loff_t *offset)
941 snd_rawmidi_file_t *rfile;
942 snd_rawmidi_substream_t *substream;
943 snd_rawmidi_runtime_t *runtime;
945 rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
946 substream = rfile->input;
947 if (substream == NULL)
949 runtime = substream->runtime;
950 runtime->trigger = 1;
951 substream->ops->trigger(substream, 1);
954 spin_lock_irq(&runtime->lock);
955 while (!snd_rawmidi_ready(substream)) {
957 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
958 spin_unlock_irq(&runtime->lock);
959 return result > 0 ? result : -EAGAIN;
961 init_waitqueue_entry(&wait, current);
962 add_wait_queue(&runtime->sleep, &wait);
963 set_current_state(TASK_INTERRUPTIBLE);
964 spin_unlock_irq(&runtime->lock);
966 remove_wait_queue(&runtime->sleep, &wait);
967 if (signal_pending(current))
968 return result > 0 ? result : -ERESTARTSYS;
970 return result > 0 ? result : -EIO;
971 spin_lock_irq(&runtime->lock);
973 spin_unlock_irq(&runtime->lock);
974 count1 = snd_rawmidi_kernel_read1(substream, buf, count, 0);
983 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
984 * @substream: the rawmidi substream
986 * Returns 1 if the internal output buffer is empty, 0 if not.
988 int snd_rawmidi_transmit_empty(snd_rawmidi_substream_t * substream)
990 snd_rawmidi_runtime_t *runtime = substream->runtime;
994 if (runtime->buffer == NULL) {
995 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
998 spin_lock_irqsave(&runtime->lock, flags);
999 result = runtime->avail >= runtime->buffer_size;
1001 runtime->trigger = 1;
1002 spin_unlock_irqrestore(&runtime->lock, flags);
1007 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1008 * @substream: the rawmidi substream
1009 * @buffer: the buffer pointer
1010 * @count: data size to transfer
1012 * Copies data from the internal output buffer to the given buffer.
1014 * Call this in the interrupt handler when the midi output is ready,
1015 * and call snd_rawmidi_transmit_ack() after the transmission is
1018 * Returns the size of copied data, or a negative error code on failure.
1020 int snd_rawmidi_transmit_peek(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
1022 unsigned long flags;
1024 snd_rawmidi_runtime_t *runtime = substream->runtime;
1026 if (runtime->buffer == NULL) {
1027 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1031 spin_lock_irqsave(&runtime->lock, flags);
1032 if (runtime->avail >= runtime->buffer_size) {
1033 /* warning: lowlevel layer MUST trigger down the hardware */
1034 runtime->trigger = 0;
1037 if (count == 1) { /* special case, faster code */
1038 *buffer = runtime->buffer[runtime->hw_ptr];
1041 count1 = runtime->buffer_size - runtime->hw_ptr;
1044 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1045 count1 = runtime->buffer_size - runtime->avail;
1046 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1050 memcpy(buffer + count1, runtime->buffer, count);
1053 spin_unlock_irqrestore(&runtime->lock, flags);
1058 * snd_rawmidi_transmit_ack - acknowledge the transmission
1059 * @substream: the rawmidi substream
1060 * @count: the tranferred count
1062 * Advances the hardware pointer for the internal output buffer with
1063 * the given size and updates the condition.
1064 * Call after the transmission is finished.
1066 * Returns the advanced size if successful, or a negative error code on failure.
1068 int snd_rawmidi_transmit_ack(snd_rawmidi_substream_t * substream, int count)
1070 unsigned long flags;
1071 snd_rawmidi_runtime_t *runtime = substream->runtime;
1073 if (runtime->buffer == NULL) {
1074 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1077 spin_lock_irqsave(&runtime->lock, flags);
1078 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1079 runtime->hw_ptr += count;
1080 runtime->hw_ptr %= runtime->buffer_size;
1081 runtime->avail += count;
1082 substream->bytes += count;
1084 wake_up(&runtime->sleep);
1086 if (count > 0 && runtime->event == NULL)
1087 if (snd_rawmidi_ready(substream))
1088 wake_up(&runtime->sleep);
1089 spin_unlock_irqrestore(&runtime->lock, flags);
1090 if (count > 0 && runtime->event)
1091 runtime->event(substream);
1096 * snd_rawmidi_transmit - copy from the buffer to the device
1097 * @substream: the rawmidi substream
1098 * @buf: the buffer pointer
1099 * @count: the data size to transfer
1101 * Copies data from the buffer to the device and advances the pointer.
1103 * Returns the copied size if successful, or a negative error code on failure.
1105 int snd_rawmidi_transmit(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
1107 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1110 return snd_rawmidi_transmit_ack(substream, count);
1113 static long snd_rawmidi_kernel_write1(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count, int kernel)
1115 unsigned long flags;
1116 long count1, result;
1117 snd_rawmidi_runtime_t *runtime = substream->runtime;
1119 snd_assert(buf != NULL, return -EINVAL);
1120 snd_assert(runtime->buffer != NULL, return -EINVAL);
1123 spin_lock_irqsave(&runtime->lock, flags);
1124 if (substream->append) {
1125 if ((long)runtime->avail < count) {
1126 spin_unlock_irqrestore(&runtime->lock, flags);
1130 while (count > 0 && runtime->avail > 0) {
1131 count1 = runtime->buffer_size - runtime->appl_ptr;
1134 if (count1 > (long)runtime->avail)
1135 count1 = runtime->avail;
1137 memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1139 spin_unlock_irqrestore(&runtime->lock, flags);
1140 if (copy_from_user(runtime->buffer + runtime->appl_ptr, buf, count1)) {
1141 spin_lock_irqsave(&runtime->lock, flags);
1142 result = result > 0 ? result : -EFAULT;
1145 spin_lock_irqsave(&runtime->lock, flags);
1147 runtime->appl_ptr += count1;
1148 runtime->appl_ptr %= runtime->buffer_size;
1149 runtime->avail -= count1;
1156 runtime->trigger = 1;
1157 count1 = runtime->avail < runtime->buffer_size;
1158 spin_unlock_irqrestore(&runtime->lock, flags);
1160 substream->ops->trigger(substream, 1);
1164 long snd_rawmidi_kernel_write(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count)
1166 return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1169 static ssize_t snd_rawmidi_write(struct file *file, const char *buf, size_t count, loff_t *offset)
1171 long result, timeout;
1173 snd_rawmidi_file_t *rfile;
1174 snd_rawmidi_runtime_t *runtime;
1175 snd_rawmidi_substream_t *substream;
1177 rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
1178 substream = rfile->output;
1179 runtime = substream->runtime;
1180 /* we cannot put an atomic message to our buffer */
1181 if (substream->append && count > runtime->buffer_size)
1185 spin_lock_irq(&runtime->lock);
1186 while (!snd_rawmidi_ready_append(substream, count)) {
1188 if (file->f_flags & O_NONBLOCK) {
1189 spin_unlock_irq(&runtime->lock);
1190 return result > 0 ? result : -EAGAIN;
1192 init_waitqueue_entry(&wait, current);
1193 add_wait_queue(&runtime->sleep, &wait);
1194 set_current_state(TASK_INTERRUPTIBLE);
1195 spin_unlock_irq(&runtime->lock);
1196 timeout = schedule_timeout(30 * HZ);
1197 remove_wait_queue(&runtime->sleep, &wait);
1198 if (signal_pending(current))
1199 return result > 0 ? result : -ERESTARTSYS;
1200 if (!runtime->avail && !timeout)
1201 return result > 0 ? result : -EIO;
1202 spin_lock_irq(&runtime->lock);
1204 spin_unlock_irq(&runtime->lock);
1205 count1 = snd_rawmidi_kernel_write1(substream, buf, count, 0);
1210 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1214 while (file->f_flags & O_SYNC) {
1215 spin_lock_irq(&runtime->lock);
1216 while (runtime->avail != runtime->buffer_size) {
1218 unsigned int last_avail = runtime->avail;
1219 init_waitqueue_entry(&wait, current);
1220 add_wait_queue(&runtime->sleep, &wait);
1221 set_current_state(TASK_INTERRUPTIBLE);
1222 spin_unlock_irq(&runtime->lock);
1223 timeout = schedule_timeout(30 * HZ);
1224 remove_wait_queue(&runtime->sleep, &wait);
1225 if (signal_pending(current))
1226 return result > 0 ? result : -ERESTARTSYS;
1227 if (runtime->avail == last_avail && !timeout)
1228 return result > 0 ? result : -EIO;
1229 spin_lock_irq(&runtime->lock);
1231 spin_unlock_irq(&runtime->lock);
1236 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1238 snd_rawmidi_file_t *rfile;
1239 snd_rawmidi_runtime_t *runtime;
1242 rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return 0);
1243 if (rfile->input != NULL) {
1244 runtime = rfile->input->runtime;
1245 runtime->trigger = 1;
1246 rfile->input->ops->trigger(rfile->input, 1);
1247 poll_wait(file, &runtime->sleep, wait);
1249 if (rfile->output != NULL) {
1250 runtime = rfile->output->runtime;
1251 poll_wait(file, &runtime->sleep, wait);
1254 if (rfile->input != NULL) {
1255 if (snd_rawmidi_ready(rfile->input))
1256 mask |= POLLIN | POLLRDNORM;
1258 if (rfile->output != NULL) {
1259 if (snd_rawmidi_ready(rfile->output))
1260 mask |= POLLOUT | POLLWRNORM;
1269 static void snd_rawmidi_proc_info_read(snd_info_entry_t *entry,
1270 snd_info_buffer_t * buffer)
1272 snd_rawmidi_t *rmidi;
1273 snd_rawmidi_substream_t *substream;
1274 snd_rawmidi_runtime_t *runtime;
1275 struct list_head *list;
1277 rmidi = snd_magic_cast(snd_rawmidi_t, entry->private_data, return);
1278 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1279 down(&rmidi->open_mutex);
1280 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1281 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
1282 substream = list_entry(list, snd_rawmidi_substream_t, list);
1285 " Tx bytes : %lu\n",
1287 (unsigned long) substream->bytes);
1288 if (substream->opened) {
1289 runtime = substream->runtime;
1292 " Buffer size : %lu\n"
1294 runtime->oss ? "OSS compatible" : "native",
1295 (unsigned long) runtime->buffer_size,
1296 (unsigned long) runtime->avail);
1300 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1301 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
1302 substream = list_entry(list, snd_rawmidi_substream_t, list);
1305 " Rx bytes : %lu\n",
1307 (unsigned long) substream->bytes);
1308 if (substream->opened) {
1309 runtime = substream->runtime;
1311 " Buffer size : %lu\n"
1313 " Overruns : %lu\n",
1314 (unsigned long) runtime->buffer_size,
1315 (unsigned long) runtime->avail,
1316 (unsigned long) runtime->xruns);
1320 up(&rmidi->open_mutex);
1324 * Register functions
1327 static struct file_operations snd_rawmidi_f_ops =
1329 .owner = THIS_MODULE,
1330 .read = snd_rawmidi_read,
1331 .write = snd_rawmidi_write,
1332 .open = snd_rawmidi_open,
1333 .release = snd_rawmidi_release,
1334 .poll = snd_rawmidi_poll,
1335 .ioctl = snd_rawmidi_ioctl,
1338 static snd_minor_t snd_rawmidi_reg =
1340 .comment = "raw midi",
1341 .f_ops = &snd_rawmidi_f_ops,
1344 static int snd_rawmidi_alloc_substreams(snd_rawmidi_t *rmidi,
1345 snd_rawmidi_str_t *stream,
1349 snd_rawmidi_substream_t *substream;
1352 INIT_LIST_HEAD(&stream->substreams);
1353 for (idx = 0; idx < count; idx++) {
1354 substream = snd_kcalloc(sizeof(snd_rawmidi_substream_t), GFP_KERNEL);
1355 if (substream == NULL)
1357 substream->stream = direction;
1358 substream->number = idx;
1359 substream->rmidi = rmidi;
1360 substream->pstr = stream;
1361 list_add_tail(&substream->list, &stream->substreams);
1362 stream->substream_count++;
1368 * snd_rawmidi_new - create a rawmidi instance
1369 * @card: the card instance
1370 * @id: the id string
1371 * @device: the device index
1372 * @output_count: the number of output streams
1373 * @input_count: the number of input streams
1374 * @rrawmidi: the pointer to store the new rawmidi instance
1376 * Creates a new rawmidi instance.
1377 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1379 * Returns zero if successful, or a negative error code on failure.
1381 int snd_rawmidi_new(snd_card_t * card, char *id, int device,
1382 int output_count, int input_count,
1383 snd_rawmidi_t ** rrawmidi)
1385 snd_rawmidi_t *rmidi;
1387 static snd_device_ops_t ops = {
1388 .dev_free = snd_rawmidi_dev_free,
1389 .dev_register = snd_rawmidi_dev_register,
1390 .dev_disconnect = snd_rawmidi_dev_disconnect,
1391 .dev_unregister = snd_rawmidi_dev_unregister
1394 snd_assert(rrawmidi != NULL, return -EINVAL);
1396 snd_assert(card != NULL, return -ENXIO);
1397 rmidi = snd_magic_kcalloc(snd_rawmidi_t, 0, GFP_KERNEL);
1401 rmidi->device = device;
1402 init_MUTEX(&rmidi->open_mutex);
1403 init_waitqueue_head(&rmidi->open_wait);
1405 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1406 if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT], SNDRV_RAWMIDI_STREAM_INPUT, input_count)) < 0) {
1407 snd_rawmidi_free(rmidi);
1410 if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT], SNDRV_RAWMIDI_STREAM_OUTPUT, output_count)) < 0) {
1411 snd_rawmidi_free(rmidi);
1414 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1415 snd_rawmidi_free(rmidi);
1422 static void snd_rawmidi_free_substreams(snd_rawmidi_str_t *stream)
1424 snd_rawmidi_substream_t *substream;
1426 while (!list_empty(&stream->substreams)) {
1427 substream = list_entry(stream->substreams.next, snd_rawmidi_substream_t, list);
1428 list_del(&substream->list);
1433 static int snd_rawmidi_free(snd_rawmidi_t *rmidi)
1435 snd_assert(rmidi != NULL, return -ENXIO);
1436 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1437 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1438 if (rmidi->private_free)
1439 rmidi->private_free(rmidi);
1440 snd_magic_kfree(rmidi);
1444 static int snd_rawmidi_dev_free(snd_device_t *device)
1446 snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
1447 return snd_rawmidi_free(rmidi);
1450 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1451 static void snd_rawmidi_dev_seq_free(snd_seq_device_t *device)
1453 snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->private_data, return);
1454 rmidi->seq_dev = NULL;
1458 static int snd_rawmidi_dev_register(snd_device_t *device)
1461 snd_info_entry_t *entry;
1463 snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
1465 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1467 down(®ister_mutex);
1468 idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1469 if (snd_rawmidi_devices[idx] != NULL) {
1470 up(®ister_mutex);
1473 snd_rawmidi_devices[idx] = rmidi;
1474 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1475 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1476 rmidi->card, rmidi->device,
1477 &snd_rawmidi_reg, name)) < 0) {
1478 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1479 snd_rawmidi_devices[idx] = NULL;
1480 up(®ister_mutex);
1483 if (rmidi->ops && rmidi->ops->dev_register &&
1484 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1485 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1486 snd_rawmidi_devices[idx] = NULL;
1487 up(®ister_mutex);
1490 #ifdef CONFIG_SND_OSSEMUL
1492 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1493 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1494 rmidi->card, 0, &snd_rawmidi_reg, name) < 0) {
1495 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1498 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1499 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1503 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1504 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1505 rmidi->card, 1, &snd_rawmidi_reg, name) < 0) {
1506 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1511 #endif /* CONFIG_SND_OSSEMUL */
1512 up(®ister_mutex);
1513 sprintf(name, "midi%d", rmidi->device);
1514 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1516 entry->private_data = rmidi;
1517 entry->c.text.read_size = 1024;
1518 entry->c.text.read = snd_rawmidi_proc_info_read;
1519 if (snd_info_register(entry) < 0) {
1520 snd_info_free_entry(entry);
1524 rmidi->proc_entry = entry;
1525 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1526 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1527 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1528 rmidi->seq_dev->private_data = rmidi;
1529 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1530 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1531 snd_device_register(rmidi->card, rmidi->seq_dev);
1538 static int snd_rawmidi_dev_disconnect(snd_device_t *device)
1540 snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
1543 down(®ister_mutex);
1544 idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1545 snd_rawmidi_devices[idx] = NULL;
1546 up(®ister_mutex);
1550 static int snd_rawmidi_dev_unregister(snd_device_t *device)
1553 snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
1555 snd_assert(rmidi != NULL, return -ENXIO);
1556 down(®ister_mutex);
1557 idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1558 snd_rawmidi_devices[idx] = NULL;
1559 if (rmidi->proc_entry) {
1560 snd_info_unregister(rmidi->proc_entry);
1561 rmidi->proc_entry = NULL;
1563 #ifdef CONFIG_SND_OSSEMUL
1564 if (rmidi->ossreg) {
1565 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1566 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1567 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1568 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1571 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1572 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1575 #endif /* CONFIG_SND_OSSEMUL */
1576 if (rmidi->ops && rmidi->ops->dev_unregister)
1577 rmidi->ops->dev_unregister(rmidi);
1578 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1579 up(®ister_mutex);
1580 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1581 if (rmidi->seq_dev) {
1582 snd_device_free(rmidi->card, rmidi->seq_dev);
1583 rmidi->seq_dev = NULL;
1586 return snd_rawmidi_free(rmidi);
1590 * snd_rawmidi_set_ops - set the rawmidi operators
1591 * @rmidi: the rawmidi instance
1592 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1593 * @ops: the operator table
1595 * Sets the rawmidi operators for the given stream direction.
1597 void snd_rawmidi_set_ops(snd_rawmidi_t *rmidi, int stream, snd_rawmidi_ops_t *ops)
1599 struct list_head *list;
1600 snd_rawmidi_substream_t *substream;
1602 list_for_each(list, &rmidi->streams[stream].substreams) {
1603 substream = list_entry(list, snd_rawmidi_substream_t, list);
1604 substream->ops = ops;
1612 static int __init alsa_rawmidi_init(void)
1615 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1616 #ifdef CONFIG_SND_OSSEMUL
1618 /* check device map table */
1619 for (i = 0; i < SNDRV_CARDS; i++) {
1620 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1621 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1624 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1625 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1630 #endif /* CONFIG_SND_OSSEMUL */
1634 static void __exit alsa_rawmidi_exit(void)
1636 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1639 module_init(alsa_rawmidi_init)
1640 module_exit(alsa_rawmidi_exit)
1642 EXPORT_SYMBOL(snd_rawmidi_output_params);
1643 EXPORT_SYMBOL(snd_rawmidi_input_params);
1644 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1645 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1646 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1647 EXPORT_SYMBOL(snd_rawmidi_receive);
1648 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1649 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1650 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1651 EXPORT_SYMBOL(snd_rawmidi_transmit);
1652 EXPORT_SYMBOL(snd_rawmidi_new);
1653 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1654 EXPORT_SYMBOL(snd_rawmidi_info);
1655 EXPORT_SYMBOL(snd_rawmidi_info_select);
1656 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1657 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1658 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1659 EXPORT_SYMBOL(snd_rawmidi_kernel_write);