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/smp_lock.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/time.h>
30 #include <linux/wait.h>
31 #include <linux/moduleparam.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@suse.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] = {[0 ... (SNDRV_CARDS-1)] = 0};
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(snd_rawmidi_t *rawmidi);
52 static int snd_rawmidi_dev_free(snd_device_t *device);
53 static int snd_rawmidi_dev_register(snd_device_t *device);
54 static int snd_rawmidi_dev_disconnect(snd_device_t *device);
55 static int snd_rawmidi_dev_unregister(snd_device_t *device);
57 snd_rawmidi_t *snd_rawmidi_devices[SNDRV_CARDS * SNDRV_RAWMIDI_DEVICES];
59 static DECLARE_MUTEX(register_mutex);
61 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
63 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
65 return SNDRV_RAWMIDI_LFLG_OUTPUT;
67 return SNDRV_RAWMIDI_LFLG_INPUT;
69 return SNDRV_RAWMIDI_LFLG_OPEN;
73 static inline int snd_rawmidi_ready(snd_rawmidi_substream_t * substream)
75 snd_rawmidi_runtime_t *runtime = substream->runtime;
76 return runtime->avail >= runtime->avail_min;
79 static inline int snd_rawmidi_ready_append(snd_rawmidi_substream_t * substream, size_t count)
81 snd_rawmidi_runtime_t *runtime = substream->runtime;
82 return runtime->avail >= runtime->avail_min &&
83 (!substream->append || runtime->avail >= count);
86 static int snd_rawmidi_init(snd_rawmidi_substream_t *substream)
88 snd_rawmidi_runtime_t *runtime = substream->runtime;
89 spin_lock_init(&runtime->lock);
90 init_waitqueue_head(&runtime->sleep);
91 runtime->event = NULL;
92 runtime->buffer_size = PAGE_SIZE;
93 runtime->avail_min = 1;
94 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
97 runtime->avail = runtime->buffer_size;
98 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL)
100 runtime->appl_ptr = runtime->hw_ptr = 0;
104 static int snd_rawmidi_done_buffer(snd_rawmidi_runtime_t *runtime)
106 if (runtime->buffer) {
107 kfree(runtime->buffer);
108 runtime->buffer = NULL;
113 int snd_rawmidi_drop_output(snd_rawmidi_substream_t * substream)
115 snd_rawmidi_runtime_t *runtime = substream->runtime;
117 substream->ops->trigger(substream, 0);
118 runtime->trigger = 0;
120 /* interrupts are not enabled at this moment,
121 so spinlock is not required */
122 runtime->appl_ptr = runtime->hw_ptr = 0;
123 runtime->avail = runtime->buffer_size;
127 int snd_rawmidi_drain_output(snd_rawmidi_substream_t * substream)
131 snd_rawmidi_runtime_t *runtime = substream->runtime;
135 while (runtime->avail < runtime->buffer_size) {
136 timeout = interruptible_sleep_on_timeout(&runtime->sleep, 10 * HZ);
137 if (signal_pending(current)) {
141 if (runtime->avail < runtime->buffer_size && !timeout) {
142 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
148 if (err != -ERESTARTSYS) {
149 /* we need wait a while to make sure that Tx FIFOs are empty */
150 if (substream->ops->drain)
151 substream->ops->drain(substream);
153 set_current_state(TASK_UNINTERRUPTIBLE);
154 schedule_timeout(HZ / 20);
156 snd_rawmidi_drop_output(substream);
161 int snd_rawmidi_drain_input(snd_rawmidi_substream_t * substream)
163 snd_rawmidi_runtime_t *runtime = substream->runtime;
165 substream->ops->trigger(substream, 0);
166 runtime->trigger = 0;
168 /* interrupts aren't enabled at this moment, so spinlock isn't needed */
169 runtime->appl_ptr = runtime->hw_ptr = 0;
174 int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
175 int mode, snd_rawmidi_file_t * rfile)
177 snd_rawmidi_t *rmidi;
178 struct list_head *list1, *list2;
179 snd_rawmidi_substream_t *sinput, *soutput;
180 snd_rawmidi_runtime_t *input = NULL, *output = NULL;
184 rfile->input = rfile->output = NULL;
185 rmidi = snd_rawmidi_devices[(cardnum * SNDRV_RAWMIDI_DEVICES) + device];
190 if (!try_module_get(rmidi->card->module)) {
194 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
195 down(&rmidi->open_mutex);
196 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
197 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
201 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
205 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
206 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
211 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
212 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
216 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
220 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
221 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
226 list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
228 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
230 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
236 sinput = list_entry(list1, snd_rawmidi_substream_t, list);
237 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
239 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
244 list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
246 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
248 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
254 soutput = list_entry(list2, snd_rawmidi_substream_t, list);
255 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
256 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
257 if (soutput->opened && !soutput->append)
264 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
269 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
270 input = kcalloc(1, sizeof(*input), GFP_KERNEL);
275 sinput->runtime = input;
276 if (snd_rawmidi_init(sinput) < 0) {
280 if ((err = sinput->ops->open(sinput)) < 0) {
281 sinput->runtime = NULL;
285 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
289 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
292 output = kcalloc(1, sizeof(*output), GFP_KERNEL);
293 if (output == NULL) {
297 soutput->runtime = output;
298 if (snd_rawmidi_init(soutput) < 0) {
299 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
300 sinput->ops->close(sinput);
301 sinput->runtime = NULL;
306 if ((err = soutput->ops->open(soutput)) < 0) {
307 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
308 sinput->ops->close(sinput);
309 sinput->runtime = NULL;
311 soutput->runtime = NULL;
316 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
318 if (soutput->use_count++ == 0)
319 soutput->active_sensing = 1;
320 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
324 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
325 up(&rmidi->open_mutex);
327 rfile->rmidi = rmidi;
328 rfile->input = sinput;
329 rfile->output = soutput;
335 snd_rawmidi_done_buffer(input);
338 if (output != NULL) {
339 snd_rawmidi_done_buffer(output);
342 module_put(rmidi->card->module);
343 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
344 up(&rmidi->open_mutex);
349 static int snd_rawmidi_open(struct inode *inode, struct file *file)
351 int maj = imajor(inode);
354 int device, subdevice;
355 unsigned short fflags;
357 snd_rawmidi_t *rmidi;
358 snd_rawmidi_file_t *rawmidi_file;
360 struct list_head *list;
361 snd_ctl_file_t *kctl;
364 case CONFIG_SND_MAJOR:
365 cardnum = SNDRV_MINOR_CARD(iminor(inode));
366 cardnum %= SNDRV_CARDS;
367 device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_RAWMIDI;
368 device %= SNDRV_MINOR_RAWMIDIS;
370 #ifdef CONFIG_SND_OSSEMUL
372 cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
373 cardnum %= SNDRV_CARDS;
374 device = SNDRV_MINOR_OSS_DEVICE(iminor(inode)) == SNDRV_MINOR_OSS_MIDI ?
375 midi_map[cardnum] : amidi_map[cardnum];
382 rmidi = snd_rawmidi_devices[(cardnum * SNDRV_RAWMIDI_DEVICES) + device];
385 #ifdef CONFIG_SND_OSSEMUL
386 if (maj == SOUND_MAJOR && !rmidi->ossreg)
389 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
390 return -EINVAL; /* invalid combination */
392 err = snd_card_file_add(card, file);
395 fflags = snd_rawmidi_file_flags(file);
396 if ((file->f_flags & O_APPEND) || maj != CONFIG_SND_MAJOR) /* OSS emul? */
397 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
398 fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
399 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
400 if (rawmidi_file == NULL) {
401 snd_card_file_remove(card, file);
404 init_waitqueue_entry(&wait, current);
405 add_wait_queue(&rmidi->open_wait, &wait);
406 down(&rmidi->open_mutex);
409 down_read(&card->controls_rwsem);
410 list_for_each(list, &card->ctl_files) {
411 kctl = snd_ctl_file(list);
412 if (kctl->pid == current->pid) {
413 subdevice = kctl->prefer_rawmidi_subdevice;
417 up_read(&card->controls_rwsem);
418 err = snd_rawmidi_kernel_open(cardnum, device, subdevice, fflags, rawmidi_file);
421 if (err == -EAGAIN) {
422 if (file->f_flags & O_NONBLOCK) {
428 set_current_state(TASK_INTERRUPTIBLE);
429 up(&rmidi->open_mutex);
431 down(&rmidi->open_mutex);
432 if (signal_pending(current)) {
437 #ifdef CONFIG_SND_OSSEMUL
438 if (rawmidi_file->input && rawmidi_file->input->runtime)
439 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
440 if (rawmidi_file->output && rawmidi_file->output->runtime)
441 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
443 remove_wait_queue(&rmidi->open_wait, &wait);
445 file->private_data = rawmidi_file;
447 snd_card_file_remove(card, file);
450 up(&rmidi->open_mutex);
454 int snd_rawmidi_kernel_release(snd_rawmidi_file_t * rfile)
456 snd_rawmidi_t *rmidi;
457 snd_rawmidi_substream_t *substream;
458 snd_rawmidi_runtime_t *runtime;
460 snd_assert(rfile != NULL, return -ENXIO);
461 snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
462 rmidi = rfile->rmidi;
463 down(&rmidi->open_mutex);
464 if (rfile->input != NULL) {
465 substream = rfile->input;
467 runtime = substream->runtime;
468 runtime->trigger = 0;
469 substream->ops->trigger(substream, 0);
470 substream->ops->close(substream);
471 snd_rawmidi_done_buffer(runtime);
472 if (runtime->private_free != NULL)
473 runtime->private_free(substream);
475 substream->runtime = NULL;
476 substream->opened = 0;
477 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
479 if (rfile->output != NULL) {
480 substream = rfile->output;
481 rfile->output = NULL;
482 if (--substream->use_count == 0) {
483 runtime = substream->runtime;
484 if (substream->active_sensing) {
485 unsigned char buf = 0xfe;
486 /* sending single active sensing message to shut the device up */
487 snd_rawmidi_kernel_write(substream, &buf, 1);
489 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
490 substream->ops->trigger(substream, 0);
491 substream->ops->close(substream);
492 snd_rawmidi_done_buffer(runtime);
493 if (runtime->private_free != NULL)
494 runtime->private_free(substream);
496 substream->runtime = NULL;
497 substream->opened = 0;
498 substream->append = 0;
500 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
502 up(&rmidi->open_mutex);
503 module_put(rmidi->card->module);
507 static int snd_rawmidi_release(struct inode *inode, struct file *file)
509 snd_rawmidi_file_t *rfile;
510 snd_rawmidi_t *rmidi;
513 rfile = file->private_data;
514 err = snd_rawmidi_kernel_release(rfile);
515 rmidi = rfile->rmidi;
516 wake_up(&rmidi->open_wait);
518 snd_card_file_remove(rmidi->card, file);
522 int snd_rawmidi_info(snd_rawmidi_substream_t *substream, snd_rawmidi_info_t *info)
524 snd_rawmidi_t *rmidi;
526 if (substream == NULL)
528 rmidi = substream->rmidi;
529 memset(info, 0, sizeof(*info));
530 info->card = rmidi->card->number;
531 info->device = rmidi->device;
532 info->subdevice = substream->number;
533 info->stream = substream->stream;
534 info->flags = rmidi->info_flags;
535 strcpy(info->id, rmidi->id);
536 strcpy(info->name, rmidi->name);
537 strcpy(info->subname, substream->name);
538 info->subdevices_count = substream->pstr->substream_count;
539 info->subdevices_avail = (substream->pstr->substream_count -
540 substream->pstr->substream_opened);
544 static int snd_rawmidi_info_user(snd_rawmidi_substream_t *substream, snd_rawmidi_info_t __user * _info)
546 snd_rawmidi_info_t info;
548 if ((err = snd_rawmidi_info(substream, &info)) < 0)
550 if (copy_to_user(_info, &info, sizeof(snd_rawmidi_info_t)))
555 int snd_rawmidi_info_select(snd_card_t *card, snd_rawmidi_info_t *info)
557 snd_rawmidi_t *rmidi;
558 snd_rawmidi_str_t *pstr;
559 snd_rawmidi_substream_t *substream;
560 struct list_head *list;
561 if (info->device >= SNDRV_RAWMIDI_DEVICES)
563 rmidi = snd_rawmidi_devices[card->number * SNDRV_RAWMIDI_DEVICES + info->device];
564 if (info->stream < 0 || info->stream > 1)
566 pstr = &rmidi->streams[info->stream];
567 if (pstr->substream_count == 0)
569 if (info->subdevice >= pstr->substream_count)
571 list_for_each(list, &pstr->substreams) {
572 substream = list_entry(list, snd_rawmidi_substream_t, list);
573 if ((unsigned int)substream->number == info->subdevice)
574 return snd_rawmidi_info(substream, info);
579 static int snd_rawmidi_info_select_user(snd_card_t *card,
580 snd_rawmidi_info_t __user *_info)
583 snd_rawmidi_info_t info;
584 if (get_user(info.device, &_info->device))
586 if (get_user(info.stream, &_info->stream))
588 if (get_user(info.subdevice, &_info->subdevice))
590 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
592 if (copy_to_user(_info, &info, sizeof(snd_rawmidi_info_t)))
597 int snd_rawmidi_output_params(snd_rawmidi_substream_t * substream,
598 snd_rawmidi_params_t * params)
601 snd_rawmidi_runtime_t *runtime = substream->runtime;
603 if (substream->append && substream->use_count > 1)
605 snd_rawmidi_drain_output(substream);
606 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
609 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
612 if (params->buffer_size != runtime->buffer_size) {
613 if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL)
615 kfree(runtime->buffer);
616 runtime->buffer = newbuf;
617 runtime->buffer_size = params->buffer_size;
619 runtime->avail_min = params->avail_min;
620 substream->active_sensing = !params->no_active_sensing;
624 int snd_rawmidi_input_params(snd_rawmidi_substream_t * substream,
625 snd_rawmidi_params_t * params)
628 snd_rawmidi_runtime_t *runtime = substream->runtime;
630 snd_rawmidi_drain_input(substream);
631 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
634 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
637 if (params->buffer_size != runtime->buffer_size) {
638 if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL)
640 kfree(runtime->buffer);
641 runtime->buffer = newbuf;
642 runtime->buffer_size = params->buffer_size;
644 runtime->avail_min = params->avail_min;
648 static int snd_rawmidi_output_status(snd_rawmidi_substream_t * substream,
649 snd_rawmidi_status_t * status)
651 snd_rawmidi_runtime_t *runtime = substream->runtime;
653 memset(status, 0, sizeof(*status));
654 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
655 spin_lock_irq(&runtime->lock);
656 status->avail = runtime->avail;
657 spin_unlock_irq(&runtime->lock);
661 static int snd_rawmidi_input_status(snd_rawmidi_substream_t * substream,
662 snd_rawmidi_status_t * status)
664 snd_rawmidi_runtime_t *runtime = substream->runtime;
666 memset(status, 0, sizeof(*status));
667 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
668 spin_lock_irq(&runtime->lock);
669 status->avail = runtime->avail;
670 status->xruns = runtime->xruns;
672 spin_unlock_irq(&runtime->lock);
676 static inline int _snd_rawmidi_ioctl(struct inode *inode, struct file *file,
677 unsigned int cmd, unsigned long arg)
679 snd_rawmidi_file_t *rfile;
680 void __user *argp = (void __user *)arg;
682 rfile = file->private_data;
683 if (((cmd >> 8) & 0xff) != 'W')
686 case SNDRV_RAWMIDI_IOCTL_PVERSION:
687 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
688 case SNDRV_RAWMIDI_IOCTL_INFO:
690 snd_rawmidi_stream_t stream;
691 snd_rawmidi_info_t __user *info = argp;
692 if (get_user(stream, &info->stream))
695 case SNDRV_RAWMIDI_STREAM_INPUT:
696 return snd_rawmidi_info_user(rfile->input, info);
697 case SNDRV_RAWMIDI_STREAM_OUTPUT:
698 return snd_rawmidi_info_user(rfile->output, info);
703 case SNDRV_RAWMIDI_IOCTL_PARAMS:
705 snd_rawmidi_params_t params;
706 if (copy_from_user(¶ms, argp, sizeof(snd_rawmidi_params_t)))
708 switch (params.stream) {
709 case SNDRV_RAWMIDI_STREAM_OUTPUT:
710 if (rfile->output == NULL)
712 return snd_rawmidi_output_params(rfile->output, ¶ms);
713 case SNDRV_RAWMIDI_STREAM_INPUT:
714 if (rfile->input == NULL)
716 return snd_rawmidi_input_params(rfile->input, ¶ms);
721 case SNDRV_RAWMIDI_IOCTL_STATUS:
724 snd_rawmidi_status_t status;
725 if (copy_from_user(&status, argp, sizeof(snd_rawmidi_status_t)))
727 switch (status.stream) {
728 case SNDRV_RAWMIDI_STREAM_OUTPUT:
729 if (rfile->output == NULL)
731 err = snd_rawmidi_output_status(rfile->output, &status);
733 case SNDRV_RAWMIDI_STREAM_INPUT:
734 if (rfile->input == NULL)
736 err = snd_rawmidi_input_status(rfile->input, &status);
743 if (copy_to_user(argp, &status, sizeof(snd_rawmidi_status_t)))
747 case SNDRV_RAWMIDI_IOCTL_DROP:
750 if (get_user(val, (long __user *) argp))
753 case SNDRV_RAWMIDI_STREAM_OUTPUT:
754 if (rfile->output == NULL)
756 return snd_rawmidi_drop_output(rfile->output);
761 case SNDRV_RAWMIDI_IOCTL_DRAIN:
764 if (get_user(val, (long __user *) argp))
767 case SNDRV_RAWMIDI_STREAM_OUTPUT:
768 if (rfile->output == NULL)
770 return snd_rawmidi_drain_output(rfile->output);
771 case SNDRV_RAWMIDI_STREAM_INPUT:
772 if (rfile->input == NULL)
774 return snd_rawmidi_drain_input(rfile->input);
779 #ifdef CONFIG_SND_DEBUG
781 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
787 /* FIXME: need to unlock BKL to allow preemption */
788 static int snd_rawmidi_ioctl(struct inode *inode, struct file *file,
789 unsigned int cmd, unsigned long arg)
793 err = _snd_rawmidi_ioctl(inode, file, cmd, arg);
798 int snd_rawmidi_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
799 unsigned int cmd, unsigned long arg)
801 void __user *argp = (void __user *)arg;
804 tmp = card->number * SNDRV_RAWMIDI_DEVICES;
806 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
810 if (get_user(device, (int __user *)argp))
812 device = device < 0 ? 0 : device + 1;
813 while (device < SNDRV_RAWMIDI_DEVICES) {
814 if (snd_rawmidi_devices[tmp + device])
818 if (device == SNDRV_RAWMIDI_DEVICES)
820 if (put_user(device, (int __user *)argp))
824 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
828 if (get_user(val, (int __user *)argp))
830 control->prefer_rawmidi_subdevice = val;
833 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
834 return snd_rawmidi_info_select_user(card, argp);
840 * snd_rawmidi_receive - receive the input data from the device
841 * @substream: the rawmidi substream
842 * @buffer: the buffer pointer
843 * @count: the data size to read
845 * Reads the data from the internal buffer.
847 * Returns the size of read data, or a negative error code on failure.
849 int snd_rawmidi_receive(snd_rawmidi_substream_t * substream, const unsigned char *buffer, int count)
852 int result = 0, count1;
853 snd_rawmidi_runtime_t *runtime = substream->runtime;
855 if (runtime->buffer == NULL) {
856 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
859 spin_lock_irqsave(&runtime->lock, flags);
860 if (count == 1) { /* special case, faster code */
862 if (runtime->avail < runtime->buffer_size) {
863 runtime->buffer[runtime->hw_ptr++] = buffer[0];
864 runtime->hw_ptr %= runtime->buffer_size;
871 substream->bytes += count;
872 count1 = runtime->buffer_size - runtime->hw_ptr;
875 if (count1 > (int)(runtime->buffer_size - runtime->avail))
876 count1 = runtime->buffer_size - runtime->avail;
877 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
878 runtime->hw_ptr += count1;
879 runtime->hw_ptr %= runtime->buffer_size;
880 runtime->avail += count1;
886 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
887 count1 = runtime->buffer_size - runtime->avail;
888 runtime->xruns = count - count1;
891 memcpy(runtime->buffer, buffer, count1);
892 runtime->hw_ptr = count1;
893 runtime->avail += count1;
898 if (result > 0 && runtime->event == NULL) {
899 if (snd_rawmidi_ready(substream))
900 wake_up(&runtime->sleep);
902 spin_unlock_irqrestore(&runtime->lock, flags);
903 if (result > 0 && runtime->event)
904 runtime->event(substream);
908 static long snd_rawmidi_kernel_read1(snd_rawmidi_substream_t *substream,
909 unsigned char *buf, long count, int kernel)
912 long result = 0, count1;
913 snd_rawmidi_runtime_t *runtime = substream->runtime;
915 while (count > 0 && runtime->avail) {
916 count1 = runtime->buffer_size - runtime->appl_ptr;
919 spin_lock_irqsave(&runtime->lock, flags);
920 if (count1 > (int)runtime->avail)
921 count1 = runtime->avail;
923 memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
925 spin_unlock_irqrestore(&runtime->lock, flags);
926 if (copy_to_user(buf + result, runtime->buffer + runtime->appl_ptr, count1)) {
927 return result > 0 ? result : -EFAULT;
929 spin_lock_irqsave(&runtime->lock, flags);
931 runtime->appl_ptr += count1;
932 runtime->appl_ptr %= runtime->buffer_size;
933 runtime->avail -= count1;
934 spin_unlock_irqrestore(&runtime->lock, flags);
941 long snd_rawmidi_kernel_read(snd_rawmidi_substream_t *substream, unsigned char *buf, long count)
943 substream->runtime->trigger = 1;
944 substream->ops->trigger(substream, 1);
945 return snd_rawmidi_kernel_read1(substream, buf, count, 1);
948 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
952 snd_rawmidi_file_t *rfile;
953 snd_rawmidi_substream_t *substream;
954 snd_rawmidi_runtime_t *runtime;
956 rfile = file->private_data;
957 substream = rfile->input;
958 if (substream == NULL)
960 runtime = substream->runtime;
961 runtime->trigger = 1;
962 substream->ops->trigger(substream, 1);
965 spin_lock_irq(&runtime->lock);
966 while (!snd_rawmidi_ready(substream)) {
968 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
969 spin_unlock_irq(&runtime->lock);
970 return result > 0 ? result : -EAGAIN;
972 init_waitqueue_entry(&wait, current);
973 add_wait_queue(&runtime->sleep, &wait);
974 set_current_state(TASK_INTERRUPTIBLE);
975 spin_unlock_irq(&runtime->lock);
977 remove_wait_queue(&runtime->sleep, &wait);
978 if (signal_pending(current))
979 return result > 0 ? result : -ERESTARTSYS;
981 return result > 0 ? result : -EIO;
982 spin_lock_irq(&runtime->lock);
984 spin_unlock_irq(&runtime->lock);
985 count1 = snd_rawmidi_kernel_read1(substream, buf, count, 0);
987 return result > 0 ? result : count1;
996 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
997 * @substream: the rawmidi substream
999 * Returns 1 if the internal output buffer is empty, 0 if not.
1001 int snd_rawmidi_transmit_empty(snd_rawmidi_substream_t * substream)
1003 snd_rawmidi_runtime_t *runtime = substream->runtime;
1005 unsigned long flags;
1007 if (runtime->buffer == NULL) {
1008 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1011 spin_lock_irqsave(&runtime->lock, flags);
1012 result = runtime->avail >= runtime->buffer_size;
1014 runtime->trigger = 1;
1015 spin_unlock_irqrestore(&runtime->lock, flags);
1020 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1021 * @substream: the rawmidi substream
1022 * @buffer: the buffer pointer
1023 * @count: data size to transfer
1025 * Copies data from the internal output buffer to the given buffer.
1027 * Call this in the interrupt handler when the midi output is ready,
1028 * and call snd_rawmidi_transmit_ack() after the transmission is
1031 * Returns the size of copied data, or a negative error code on failure.
1033 int snd_rawmidi_transmit_peek(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
1035 unsigned long flags;
1037 snd_rawmidi_runtime_t *runtime = substream->runtime;
1039 if (runtime->buffer == NULL) {
1040 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1044 spin_lock_irqsave(&runtime->lock, flags);
1045 if (runtime->avail >= runtime->buffer_size) {
1046 /* warning: lowlevel layer MUST trigger down the hardware */
1047 runtime->trigger = 0;
1050 if (count == 1) { /* special case, faster code */
1051 *buffer = runtime->buffer[runtime->hw_ptr];
1054 count1 = runtime->buffer_size - runtime->hw_ptr;
1057 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1058 count1 = runtime->buffer_size - runtime->avail;
1059 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1063 memcpy(buffer + count1, runtime->buffer, count);
1066 spin_unlock_irqrestore(&runtime->lock, flags);
1071 * snd_rawmidi_transmit_ack - acknowledge the transmission
1072 * @substream: the rawmidi substream
1073 * @count: the tranferred count
1075 * Advances the hardware pointer for the internal output buffer with
1076 * the given size and updates the condition.
1077 * Call after the transmission is finished.
1079 * Returns the advanced size if successful, or a negative error code on failure.
1081 int snd_rawmidi_transmit_ack(snd_rawmidi_substream_t * substream, int count)
1083 unsigned long flags;
1084 snd_rawmidi_runtime_t *runtime = substream->runtime;
1086 if (runtime->buffer == NULL) {
1087 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1090 spin_lock_irqsave(&runtime->lock, flags);
1091 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1092 runtime->hw_ptr += count;
1093 runtime->hw_ptr %= runtime->buffer_size;
1094 runtime->avail += count;
1095 substream->bytes += count;
1097 wake_up(&runtime->sleep);
1099 if (count > 0 && runtime->event == NULL)
1100 if (snd_rawmidi_ready(substream))
1101 wake_up(&runtime->sleep);
1102 spin_unlock_irqrestore(&runtime->lock, flags);
1103 if (count > 0 && runtime->event)
1104 runtime->event(substream);
1109 * snd_rawmidi_transmit - copy from the buffer to the device
1110 * @substream: the rawmidi substream
1111 * @buf: the buffer pointer
1112 * @count: the data size to transfer
1114 * Copies data from the buffer to the device and advances the pointer.
1116 * Returns the copied size if successful, or a negative error code on failure.
1118 int snd_rawmidi_transmit(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
1120 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1123 return snd_rawmidi_transmit_ack(substream, count);
1126 static long snd_rawmidi_kernel_write1(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count, int kernel)
1128 unsigned long flags;
1129 long count1, result;
1130 snd_rawmidi_runtime_t *runtime = substream->runtime;
1132 snd_assert(buf != NULL, return -EINVAL);
1133 snd_assert(runtime->buffer != NULL, return -EINVAL);
1136 spin_lock_irqsave(&runtime->lock, flags);
1137 if (substream->append) {
1138 if ((long)runtime->avail < count) {
1139 spin_unlock_irqrestore(&runtime->lock, flags);
1143 while (count > 0 && runtime->avail > 0) {
1144 count1 = runtime->buffer_size - runtime->appl_ptr;
1147 if (count1 > (long)runtime->avail)
1148 count1 = runtime->avail;
1150 memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1152 spin_unlock_irqrestore(&runtime->lock, flags);
1153 if (copy_from_user(runtime->buffer + runtime->appl_ptr, buf, count1)) {
1154 spin_lock_irqsave(&runtime->lock, flags);
1155 result = result > 0 ? result : -EFAULT;
1158 spin_lock_irqsave(&runtime->lock, flags);
1160 runtime->appl_ptr += count1;
1161 runtime->appl_ptr %= runtime->buffer_size;
1162 runtime->avail -= count1;
1169 runtime->trigger = 1;
1170 count1 = runtime->avail < runtime->buffer_size;
1171 spin_unlock_irqrestore(&runtime->lock, flags);
1173 substream->ops->trigger(substream, 1);
1177 long snd_rawmidi_kernel_write(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count)
1179 return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1182 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
1184 long result, timeout;
1186 snd_rawmidi_file_t *rfile;
1187 snd_rawmidi_runtime_t *runtime;
1188 snd_rawmidi_substream_t *substream;
1190 rfile = file->private_data;
1191 substream = rfile->output;
1192 runtime = substream->runtime;
1193 /* we cannot put an atomic message to our buffer */
1194 if (substream->append && count > runtime->buffer_size)
1198 spin_lock_irq(&runtime->lock);
1199 while (!snd_rawmidi_ready_append(substream, count)) {
1201 if (file->f_flags & O_NONBLOCK) {
1202 spin_unlock_irq(&runtime->lock);
1203 return result > 0 ? result : -EAGAIN;
1205 init_waitqueue_entry(&wait, current);
1206 add_wait_queue(&runtime->sleep, &wait);
1207 set_current_state(TASK_INTERRUPTIBLE);
1208 spin_unlock_irq(&runtime->lock);
1209 timeout = schedule_timeout(30 * HZ);
1210 remove_wait_queue(&runtime->sleep, &wait);
1211 if (signal_pending(current))
1212 return result > 0 ? result : -ERESTARTSYS;
1213 if (!runtime->avail && !timeout)
1214 return result > 0 ? result : -EIO;
1215 spin_lock_irq(&runtime->lock);
1217 spin_unlock_irq(&runtime->lock);
1218 count1 = snd_rawmidi_kernel_write1(substream, buf, count, 0);
1220 return result > 0 ? result : count1;
1223 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1227 if (file->f_flags & O_SYNC) {
1228 spin_lock_irq(&runtime->lock);
1229 while (runtime->avail != runtime->buffer_size) {
1231 unsigned int last_avail = runtime->avail;
1232 init_waitqueue_entry(&wait, current);
1233 add_wait_queue(&runtime->sleep, &wait);
1234 set_current_state(TASK_INTERRUPTIBLE);
1235 spin_unlock_irq(&runtime->lock);
1236 timeout = schedule_timeout(30 * HZ);
1237 remove_wait_queue(&runtime->sleep, &wait);
1238 if (signal_pending(current))
1239 return result > 0 ? result : -ERESTARTSYS;
1240 if (runtime->avail == last_avail && !timeout)
1241 return result > 0 ? result : -EIO;
1242 spin_lock_irq(&runtime->lock);
1244 spin_unlock_irq(&runtime->lock);
1249 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1251 snd_rawmidi_file_t *rfile;
1252 snd_rawmidi_runtime_t *runtime;
1255 rfile = file->private_data;
1256 if (rfile->input != NULL) {
1257 runtime = rfile->input->runtime;
1258 runtime->trigger = 1;
1259 rfile->input->ops->trigger(rfile->input, 1);
1260 poll_wait(file, &runtime->sleep, wait);
1262 if (rfile->output != NULL) {
1263 runtime = rfile->output->runtime;
1264 poll_wait(file, &runtime->sleep, wait);
1267 if (rfile->input != NULL) {
1268 if (snd_rawmidi_ready(rfile->input))
1269 mask |= POLLIN | POLLRDNORM;
1271 if (rfile->output != NULL) {
1272 if (snd_rawmidi_ready(rfile->output))
1273 mask |= POLLOUT | POLLWRNORM;
1282 static void snd_rawmidi_proc_info_read(snd_info_entry_t *entry,
1283 snd_info_buffer_t * buffer)
1285 snd_rawmidi_t *rmidi;
1286 snd_rawmidi_substream_t *substream;
1287 snd_rawmidi_runtime_t *runtime;
1288 struct list_head *list;
1290 rmidi = entry->private_data;
1291 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1292 down(&rmidi->open_mutex);
1293 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1294 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
1295 substream = list_entry(list, snd_rawmidi_substream_t, list);
1298 " Tx bytes : %lu\n",
1300 (unsigned long) substream->bytes);
1301 if (substream->opened) {
1302 runtime = substream->runtime;
1305 " Buffer size : %lu\n"
1307 runtime->oss ? "OSS compatible" : "native",
1308 (unsigned long) runtime->buffer_size,
1309 (unsigned long) runtime->avail);
1313 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1314 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
1315 substream = list_entry(list, snd_rawmidi_substream_t, list);
1318 " Rx bytes : %lu\n",
1320 (unsigned long) substream->bytes);
1321 if (substream->opened) {
1322 runtime = substream->runtime;
1324 " Buffer size : %lu\n"
1326 " Overruns : %lu\n",
1327 (unsigned long) runtime->buffer_size,
1328 (unsigned long) runtime->avail,
1329 (unsigned long) runtime->xruns);
1333 up(&rmidi->open_mutex);
1337 * Register functions
1340 static struct file_operations snd_rawmidi_f_ops =
1342 .owner = THIS_MODULE,
1343 .read = snd_rawmidi_read,
1344 .write = snd_rawmidi_write,
1345 .open = snd_rawmidi_open,
1346 .release = snd_rawmidi_release,
1347 .poll = snd_rawmidi_poll,
1348 .ioctl = snd_rawmidi_ioctl,
1351 static snd_minor_t snd_rawmidi_reg =
1353 .comment = "raw midi",
1354 .f_ops = &snd_rawmidi_f_ops,
1357 static int snd_rawmidi_alloc_substreams(snd_rawmidi_t *rmidi,
1358 snd_rawmidi_str_t *stream,
1362 snd_rawmidi_substream_t *substream;
1365 INIT_LIST_HEAD(&stream->substreams);
1366 for (idx = 0; idx < count; idx++) {
1367 substream = kcalloc(1, sizeof(*substream), GFP_KERNEL);
1368 if (substream == NULL)
1370 substream->stream = direction;
1371 substream->number = idx;
1372 substream->rmidi = rmidi;
1373 substream->pstr = stream;
1374 list_add_tail(&substream->list, &stream->substreams);
1375 stream->substream_count++;
1381 * snd_rawmidi_new - create a rawmidi instance
1382 * @card: the card instance
1383 * @id: the id string
1384 * @device: the device index
1385 * @output_count: the number of output streams
1386 * @input_count: the number of input streams
1387 * @rrawmidi: the pointer to store the new rawmidi instance
1389 * Creates a new rawmidi instance.
1390 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1392 * Returns zero if successful, or a negative error code on failure.
1394 int snd_rawmidi_new(snd_card_t * card, char *id, int device,
1395 int output_count, int input_count,
1396 snd_rawmidi_t ** rrawmidi)
1398 snd_rawmidi_t *rmidi;
1400 static snd_device_ops_t ops = {
1401 .dev_free = snd_rawmidi_dev_free,
1402 .dev_register = snd_rawmidi_dev_register,
1403 .dev_disconnect = snd_rawmidi_dev_disconnect,
1404 .dev_unregister = snd_rawmidi_dev_unregister
1407 snd_assert(rrawmidi != NULL, return -EINVAL);
1409 snd_assert(card != NULL, return -ENXIO);
1410 rmidi = kcalloc(1, sizeof(*rmidi), GFP_KERNEL);
1414 rmidi->device = device;
1415 init_MUTEX(&rmidi->open_mutex);
1416 init_waitqueue_head(&rmidi->open_wait);
1418 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1419 if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT], SNDRV_RAWMIDI_STREAM_INPUT, input_count)) < 0) {
1420 snd_rawmidi_free(rmidi);
1423 if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT], SNDRV_RAWMIDI_STREAM_OUTPUT, output_count)) < 0) {
1424 snd_rawmidi_free(rmidi);
1427 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1428 snd_rawmidi_free(rmidi);
1435 static void snd_rawmidi_free_substreams(snd_rawmidi_str_t *stream)
1437 snd_rawmidi_substream_t *substream;
1439 while (!list_empty(&stream->substreams)) {
1440 substream = list_entry(stream->substreams.next, snd_rawmidi_substream_t, list);
1441 list_del(&substream->list);
1446 static int snd_rawmidi_free(snd_rawmidi_t *rmidi)
1448 snd_assert(rmidi != NULL, return -ENXIO);
1449 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1450 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1451 if (rmidi->private_free)
1452 rmidi->private_free(rmidi);
1457 static int snd_rawmidi_dev_free(snd_device_t *device)
1459 snd_rawmidi_t *rmidi = device->device_data;
1460 return snd_rawmidi_free(rmidi);
1463 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1464 static void snd_rawmidi_dev_seq_free(snd_seq_device_t *device)
1466 snd_rawmidi_t *rmidi = device->private_data;
1467 rmidi->seq_dev = NULL;
1471 static int snd_rawmidi_dev_register(snd_device_t *device)
1474 snd_info_entry_t *entry;
1476 snd_rawmidi_t *rmidi = device->device_data;
1478 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1480 down(®ister_mutex);
1481 idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1482 if (snd_rawmidi_devices[idx] != NULL) {
1483 up(®ister_mutex);
1486 snd_rawmidi_devices[idx] = rmidi;
1487 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1488 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1489 rmidi->card, rmidi->device,
1490 &snd_rawmidi_reg, name)) < 0) {
1491 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1492 snd_rawmidi_devices[idx] = NULL;
1493 up(®ister_mutex);
1496 if (rmidi->ops && rmidi->ops->dev_register &&
1497 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1498 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1499 snd_rawmidi_devices[idx] = NULL;
1500 up(®ister_mutex);
1503 #ifdef CONFIG_SND_OSSEMUL
1505 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1506 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1507 rmidi->card, 0, &snd_rawmidi_reg, name) < 0) {
1508 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1511 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1512 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1516 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1517 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1518 rmidi->card, 1, &snd_rawmidi_reg, name) < 0) {
1519 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1524 #endif /* CONFIG_SND_OSSEMUL */
1525 up(®ister_mutex);
1526 sprintf(name, "midi%d", rmidi->device);
1527 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1529 entry->private_data = rmidi;
1530 entry->c.text.read_size = 1024;
1531 entry->c.text.read = snd_rawmidi_proc_info_read;
1532 if (snd_info_register(entry) < 0) {
1533 snd_info_free_entry(entry);
1537 rmidi->proc_entry = entry;
1538 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1539 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1540 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1541 rmidi->seq_dev->private_data = rmidi;
1542 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1543 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1544 snd_device_register(rmidi->card, rmidi->seq_dev);
1551 static int snd_rawmidi_dev_disconnect(snd_device_t *device)
1553 snd_rawmidi_t *rmidi = device->device_data;
1556 down(®ister_mutex);
1557 idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1558 snd_rawmidi_devices[idx] = NULL;
1559 up(®ister_mutex);
1563 static int snd_rawmidi_dev_unregister(snd_device_t *device)
1566 snd_rawmidi_t *rmidi = device->device_data;
1568 snd_assert(rmidi != NULL, return -ENXIO);
1569 down(®ister_mutex);
1570 idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
1571 snd_rawmidi_devices[idx] = NULL;
1572 if (rmidi->proc_entry) {
1573 snd_info_unregister(rmidi->proc_entry);
1574 rmidi->proc_entry = NULL;
1576 #ifdef CONFIG_SND_OSSEMUL
1577 if (rmidi->ossreg) {
1578 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1579 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1580 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1581 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1584 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1585 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1588 #endif /* CONFIG_SND_OSSEMUL */
1589 if (rmidi->ops && rmidi->ops->dev_unregister)
1590 rmidi->ops->dev_unregister(rmidi);
1591 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1592 up(®ister_mutex);
1593 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1594 if (rmidi->seq_dev) {
1595 snd_device_free(rmidi->card, rmidi->seq_dev);
1596 rmidi->seq_dev = NULL;
1599 return snd_rawmidi_free(rmidi);
1603 * snd_rawmidi_set_ops - set the rawmidi operators
1604 * @rmidi: the rawmidi instance
1605 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1606 * @ops: the operator table
1608 * Sets the rawmidi operators for the given stream direction.
1610 void snd_rawmidi_set_ops(snd_rawmidi_t *rmidi, int stream, snd_rawmidi_ops_t *ops)
1612 struct list_head *list;
1613 snd_rawmidi_substream_t *substream;
1615 list_for_each(list, &rmidi->streams[stream].substreams) {
1616 substream = list_entry(list, snd_rawmidi_substream_t, list);
1617 substream->ops = ops;
1625 static int __init alsa_rawmidi_init(void)
1628 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1629 #ifdef CONFIG_SND_OSSEMUL
1631 /* check device map table */
1632 for (i = 0; i < SNDRV_CARDS; i++) {
1633 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1634 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1637 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1638 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1643 #endif /* CONFIG_SND_OSSEMUL */
1647 static void __exit alsa_rawmidi_exit(void)
1649 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1652 module_init(alsa_rawmidi_init)
1653 module_exit(alsa_rawmidi_exit)
1655 EXPORT_SYMBOL(snd_rawmidi_output_params);
1656 EXPORT_SYMBOL(snd_rawmidi_input_params);
1657 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1658 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1659 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1660 EXPORT_SYMBOL(snd_rawmidi_receive);
1661 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1662 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1663 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1664 EXPORT_SYMBOL(snd_rawmidi_transmit);
1665 EXPORT_SYMBOL(snd_rawmidi_new);
1666 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1667 EXPORT_SYMBOL(snd_rawmidi_info);
1668 EXPORT_SYMBOL(snd_rawmidi_info_select);
1669 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1670 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1671 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1672 EXPORT_SYMBOL(snd_rawmidi_kernel_write);