2 * Abstract layer for MIDI v1.0 stream
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/core.h>
23 #include <linux/major.h>
24 #include <linux/init.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/wait.h>
29 #include <linux/mutex.h>
30 #include <linux/moduleparam.h>
31 #include <linux/delay.h>
32 #include <sound/rawmidi.h>
33 #include <sound/info.h>
34 #include <sound/control.h>
35 #include <sound/minors.h>
36 #include <sound/initval.h>
38 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
39 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
40 MODULE_LICENSE("GPL");
42 #ifdef CONFIG_SND_OSSEMUL
43 static int midi_map[SNDRV_CARDS];
44 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
45 module_param_array(midi_map, int, NULL, 0444);
46 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
47 module_param_array(amidi_map, int, NULL, 0444);
48 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
49 #endif /* CONFIG_SND_OSSEMUL */
51 static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
52 static int snd_rawmidi_dev_free(struct snd_device *device);
53 static int snd_rawmidi_dev_register(struct snd_device *device);
54 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
56 static LIST_HEAD(snd_rawmidi_devices);
57 static DEFINE_MUTEX(register_mutex);
59 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
61 struct snd_rawmidi *rawmidi;
63 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
64 if (rawmidi->card == card && rawmidi->device == device)
69 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
71 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
73 return SNDRV_RAWMIDI_LFLG_OUTPUT;
75 return SNDRV_RAWMIDI_LFLG_INPUT;
77 return SNDRV_RAWMIDI_LFLG_OPEN;
81 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
83 struct snd_rawmidi_runtime *runtime = substream->runtime;
84 return runtime->avail >= runtime->avail_min;
87 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
90 struct snd_rawmidi_runtime *runtime = substream->runtime;
91 return runtime->avail >= runtime->avail_min &&
92 (!substream->append || runtime->avail >= count);
95 static void snd_rawmidi_input_event_tasklet(unsigned long data)
97 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
98 substream->runtime->event(substream);
101 static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
103 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
104 substream->ops->trigger(substream, 1);
107 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
109 struct snd_rawmidi_runtime *runtime;
111 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
113 spin_lock_init(&runtime->lock);
114 init_waitqueue_head(&runtime->sleep);
115 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
116 tasklet_init(&runtime->tasklet,
117 snd_rawmidi_input_event_tasklet,
118 (unsigned long)substream);
120 tasklet_init(&runtime->tasklet,
121 snd_rawmidi_output_trigger_tasklet,
122 (unsigned long)substream);
123 runtime->event = NULL;
124 runtime->buffer_size = PAGE_SIZE;
125 runtime->avail_min = 1;
126 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
129 runtime->avail = runtime->buffer_size;
130 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
134 runtime->appl_ptr = runtime->hw_ptr = 0;
135 substream->runtime = runtime;
139 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
141 struct snd_rawmidi_runtime *runtime = substream->runtime;
143 kfree(runtime->buffer);
145 substream->runtime = NULL;
149 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
152 tasklet_hi_schedule(&substream->runtime->tasklet);
154 tasklet_kill(&substream->runtime->tasklet);
155 substream->ops->trigger(substream, 0);
159 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
161 substream->ops->trigger(substream, up);
162 if (!up && substream->runtime->event)
163 tasklet_kill(&substream->runtime->tasklet);
166 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
169 struct snd_rawmidi_runtime *runtime = substream->runtime;
171 snd_rawmidi_output_trigger(substream, 0);
173 spin_lock_irqsave(&runtime->lock, flags);
174 runtime->appl_ptr = runtime->hw_ptr = 0;
175 runtime->avail = runtime->buffer_size;
176 spin_unlock_irqrestore(&runtime->lock, flags);
180 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
184 struct snd_rawmidi_runtime *runtime = substream->runtime;
188 timeout = wait_event_interruptible_timeout(runtime->sleep,
189 (runtime->avail >= runtime->buffer_size),
191 if (signal_pending(current))
193 if (runtime->avail < runtime->buffer_size && !timeout) {
194 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
198 if (err != -ERESTARTSYS) {
199 /* we need wait a while to make sure that Tx FIFOs are empty */
200 if (substream->ops->drain)
201 substream->ops->drain(substream);
204 snd_rawmidi_drop_output(substream);
209 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
212 struct snd_rawmidi_runtime *runtime = substream->runtime;
214 snd_rawmidi_input_trigger(substream, 0);
216 spin_lock_irqsave(&runtime->lock, flags);
217 runtime->appl_ptr = runtime->hw_ptr = 0;
219 spin_unlock_irqrestore(&runtime->lock, flags);
223 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
224 int mode, struct snd_rawmidi_file * rfile)
226 struct snd_rawmidi *rmidi;
227 struct list_head *list1, *list2;
228 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
229 struct snd_rawmidi_runtime *input = NULL, *output = NULL;
233 rfile->input = rfile->output = NULL;
234 mutex_lock(®ister_mutex);
235 rmidi = snd_rawmidi_search(card, device);
236 mutex_unlock(®ister_mutex);
241 if (!try_module_get(rmidi->card->module)) {
245 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
246 mutex_lock(&rmidi->open_mutex);
247 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
248 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
252 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
256 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
257 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
262 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
263 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
267 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
271 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
272 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
277 list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
279 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
281 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
287 sinput = list_entry(list1, struct snd_rawmidi_substream, list);
288 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
290 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
295 list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
297 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
299 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
305 soutput = list_entry(list2, struct snd_rawmidi_substream, list);
306 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
307 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
308 if (soutput->opened && !soutput->append)
315 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
320 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
321 if ((err = snd_rawmidi_runtime_create(sinput)) < 0)
323 input = sinput->runtime;
324 if ((err = sinput->ops->open(sinput)) < 0)
327 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
331 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
334 if ((err = snd_rawmidi_runtime_create(soutput)) < 0) {
335 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
336 sinput->ops->close(sinput);
339 output = soutput->runtime;
340 if ((err = soutput->ops->open(soutput)) < 0) {
341 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
342 sinput->ops->close(sinput);
347 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
349 if (soutput->use_count++ == 0)
350 soutput->active_sensing = 1;
351 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
355 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
356 mutex_unlock(&rmidi->open_mutex);
358 rfile->rmidi = rmidi;
359 rfile->input = sinput;
360 rfile->output = soutput;
366 snd_rawmidi_runtime_free(sinput);
368 snd_rawmidi_runtime_free(soutput);
369 module_put(rmidi->card->module);
370 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
371 mutex_unlock(&rmidi->open_mutex);
376 static int snd_rawmidi_open(struct inode *inode, struct file *file)
378 int maj = imajor(inode);
379 struct snd_card *card;
381 unsigned short fflags;
383 struct snd_rawmidi *rmidi;
384 struct snd_rawmidi_file *rawmidi_file;
386 struct snd_ctl_file *kctl;
388 if (maj == snd_major) {
389 rmidi = snd_lookup_minor_data(iminor(inode),
390 SNDRV_DEVICE_TYPE_RAWMIDI);
391 #ifdef CONFIG_SND_OSSEMUL
392 } else if (maj == SOUND_MAJOR) {
393 rmidi = snd_lookup_oss_minor_data(iminor(inode),
394 SNDRV_OSS_DEVICE_TYPE_MIDI);
401 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
402 return -EINVAL; /* invalid combination */
404 err = snd_card_file_add(card, file);
407 fflags = snd_rawmidi_file_flags(file);
408 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
409 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
410 fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
411 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
412 if (rawmidi_file == NULL) {
413 snd_card_file_remove(card, file);
416 init_waitqueue_entry(&wait, current);
417 add_wait_queue(&rmidi->open_wait, &wait);
418 mutex_lock(&rmidi->open_mutex);
421 down_read(&card->controls_rwsem);
422 list_for_each_entry(kctl, &card->ctl_files, list) {
423 if (kctl->pid == current->pid) {
424 subdevice = kctl->prefer_rawmidi_subdevice;
429 up_read(&card->controls_rwsem);
430 err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
431 subdevice, fflags, rawmidi_file);
434 if (err == -EAGAIN) {
435 if (file->f_flags & O_NONBLOCK) {
441 set_current_state(TASK_INTERRUPTIBLE);
442 mutex_unlock(&rmidi->open_mutex);
444 mutex_lock(&rmidi->open_mutex);
445 if (signal_pending(current)) {
450 #ifdef CONFIG_SND_OSSEMUL
451 if (rawmidi_file->input && rawmidi_file->input->runtime)
452 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
453 if (rawmidi_file->output && rawmidi_file->output->runtime)
454 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
456 remove_wait_queue(&rmidi->open_wait, &wait);
458 file->private_data = rawmidi_file;
460 snd_card_file_remove(card, file);
463 mutex_unlock(&rmidi->open_mutex);
467 int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
469 struct snd_rawmidi *rmidi;
470 struct snd_rawmidi_substream *substream;
471 struct snd_rawmidi_runtime *runtime;
473 snd_assert(rfile != NULL, return -ENXIO);
474 snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
475 rmidi = rfile->rmidi;
476 mutex_lock(&rmidi->open_mutex);
477 if (rfile->input != NULL) {
478 substream = rfile->input;
480 runtime = substream->runtime;
481 snd_rawmidi_input_trigger(substream, 0);
482 substream->ops->close(substream);
483 if (runtime->private_free != NULL)
484 runtime->private_free(substream);
485 snd_rawmidi_runtime_free(substream);
486 substream->opened = 0;
487 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
489 if (rfile->output != NULL) {
490 substream = rfile->output;
491 rfile->output = NULL;
492 if (--substream->use_count == 0) {
493 runtime = substream->runtime;
494 if (substream->active_sensing) {
495 unsigned char buf = 0xfe;
496 /* sending single active sensing message to shut the device up */
497 snd_rawmidi_kernel_write(substream, &buf, 1);
499 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
500 snd_rawmidi_output_trigger(substream, 0);
501 substream->ops->close(substream);
502 if (runtime->private_free != NULL)
503 runtime->private_free(substream);
504 snd_rawmidi_runtime_free(substream);
505 substream->opened = 0;
506 substream->append = 0;
508 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
510 mutex_unlock(&rmidi->open_mutex);
511 module_put(rmidi->card->module);
515 static int snd_rawmidi_release(struct inode *inode, struct file *file)
517 struct snd_rawmidi_file *rfile;
518 struct snd_rawmidi *rmidi;
521 rfile = file->private_data;
522 err = snd_rawmidi_kernel_release(rfile);
523 rmidi = rfile->rmidi;
524 wake_up(&rmidi->open_wait);
526 snd_card_file_remove(rmidi->card, file);
530 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
531 struct snd_rawmidi_info *info)
533 struct snd_rawmidi *rmidi;
535 if (substream == NULL)
537 rmidi = substream->rmidi;
538 memset(info, 0, sizeof(*info));
539 info->card = rmidi->card->number;
540 info->device = rmidi->device;
541 info->subdevice = substream->number;
542 info->stream = substream->stream;
543 info->flags = rmidi->info_flags;
544 strcpy(info->id, rmidi->id);
545 strcpy(info->name, rmidi->name);
546 strcpy(info->subname, substream->name);
547 info->subdevices_count = substream->pstr->substream_count;
548 info->subdevices_avail = (substream->pstr->substream_count -
549 substream->pstr->substream_opened);
553 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
554 struct snd_rawmidi_info __user * _info)
556 struct snd_rawmidi_info info;
558 if ((err = snd_rawmidi_info(substream, &info)) < 0)
560 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
565 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
567 struct snd_rawmidi *rmidi;
568 struct snd_rawmidi_str *pstr;
569 struct snd_rawmidi_substream *substream;
571 mutex_lock(®ister_mutex);
572 rmidi = snd_rawmidi_search(card, info->device);
573 mutex_unlock(®ister_mutex);
576 if (info->stream < 0 || info->stream > 1)
578 pstr = &rmidi->streams[info->stream];
579 if (pstr->substream_count == 0)
581 if (info->subdevice >= pstr->substream_count)
583 list_for_each_entry(substream, &pstr->substreams, list) {
584 if ((unsigned int)substream->number == info->subdevice)
585 return snd_rawmidi_info(substream, info);
590 static int snd_rawmidi_info_select_user(struct snd_card *card,
591 struct snd_rawmidi_info __user *_info)
594 struct snd_rawmidi_info info;
595 if (get_user(info.device, &_info->device))
597 if (get_user(info.stream, &_info->stream))
599 if (get_user(info.subdevice, &_info->subdevice))
601 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
603 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
608 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
609 struct snd_rawmidi_params * params)
612 struct snd_rawmidi_runtime *runtime = substream->runtime;
614 if (substream->append && substream->use_count > 1)
616 snd_rawmidi_drain_output(substream);
617 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
620 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
623 if (params->buffer_size != runtime->buffer_size) {
624 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
627 kfree(runtime->buffer);
628 runtime->buffer = newbuf;
629 runtime->buffer_size = params->buffer_size;
630 runtime->avail = runtime->buffer_size;
632 runtime->avail_min = params->avail_min;
633 substream->active_sensing = !params->no_active_sensing;
637 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
638 struct snd_rawmidi_params * params)
641 struct snd_rawmidi_runtime *runtime = substream->runtime;
643 snd_rawmidi_drain_input(substream);
644 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
647 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
650 if (params->buffer_size != runtime->buffer_size) {
651 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
654 kfree(runtime->buffer);
655 runtime->buffer = newbuf;
656 runtime->buffer_size = params->buffer_size;
658 runtime->avail_min = params->avail_min;
662 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
663 struct snd_rawmidi_status * status)
665 struct snd_rawmidi_runtime *runtime = substream->runtime;
667 memset(status, 0, sizeof(*status));
668 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
669 spin_lock_irq(&runtime->lock);
670 status->avail = runtime->avail;
671 spin_unlock_irq(&runtime->lock);
675 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
676 struct snd_rawmidi_status * status)
678 struct snd_rawmidi_runtime *runtime = substream->runtime;
680 memset(status, 0, sizeof(*status));
681 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
682 spin_lock_irq(&runtime->lock);
683 status->avail = runtime->avail;
684 status->xruns = runtime->xruns;
686 spin_unlock_irq(&runtime->lock);
690 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
692 struct snd_rawmidi_file *rfile;
693 void __user *argp = (void __user *)arg;
695 rfile = file->private_data;
696 if (((cmd >> 8) & 0xff) != 'W')
699 case SNDRV_RAWMIDI_IOCTL_PVERSION:
700 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
701 case SNDRV_RAWMIDI_IOCTL_INFO:
704 struct snd_rawmidi_info __user *info = argp;
705 if (get_user(stream, &info->stream))
708 case SNDRV_RAWMIDI_STREAM_INPUT:
709 return snd_rawmidi_info_user(rfile->input, info);
710 case SNDRV_RAWMIDI_STREAM_OUTPUT:
711 return snd_rawmidi_info_user(rfile->output, info);
716 case SNDRV_RAWMIDI_IOCTL_PARAMS:
718 struct snd_rawmidi_params params;
719 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
721 switch (params.stream) {
722 case SNDRV_RAWMIDI_STREAM_OUTPUT:
723 if (rfile->output == NULL)
725 return snd_rawmidi_output_params(rfile->output, ¶ms);
726 case SNDRV_RAWMIDI_STREAM_INPUT:
727 if (rfile->input == NULL)
729 return snd_rawmidi_input_params(rfile->input, ¶ms);
734 case SNDRV_RAWMIDI_IOCTL_STATUS:
737 struct snd_rawmidi_status status;
738 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
740 switch (status.stream) {
741 case SNDRV_RAWMIDI_STREAM_OUTPUT:
742 if (rfile->output == NULL)
744 err = snd_rawmidi_output_status(rfile->output, &status);
746 case SNDRV_RAWMIDI_STREAM_INPUT:
747 if (rfile->input == NULL)
749 err = snd_rawmidi_input_status(rfile->input, &status);
756 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
760 case SNDRV_RAWMIDI_IOCTL_DROP:
763 if (get_user(val, (int __user *) argp))
766 case SNDRV_RAWMIDI_STREAM_OUTPUT:
767 if (rfile->output == NULL)
769 return snd_rawmidi_drop_output(rfile->output);
774 case SNDRV_RAWMIDI_IOCTL_DRAIN:
777 if (get_user(val, (int __user *) argp))
780 case SNDRV_RAWMIDI_STREAM_OUTPUT:
781 if (rfile->output == NULL)
783 return snd_rawmidi_drain_output(rfile->output);
784 case SNDRV_RAWMIDI_STREAM_INPUT:
785 if (rfile->input == NULL)
787 return snd_rawmidi_drain_input(rfile->input);
792 #ifdef CONFIG_SND_DEBUG
794 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
800 static int snd_rawmidi_control_ioctl(struct snd_card *card,
801 struct snd_ctl_file *control,
805 void __user *argp = (void __user *)arg;
808 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
812 if (get_user(device, (int __user *)argp))
814 mutex_lock(®ister_mutex);
815 device = device < 0 ? 0 : device + 1;
816 while (device < SNDRV_RAWMIDI_DEVICES) {
817 if (snd_rawmidi_search(card, device))
821 if (device == SNDRV_RAWMIDI_DEVICES)
823 mutex_unlock(®ister_mutex);
824 if (put_user(device, (int __user *)argp))
828 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
832 if (get_user(val, (int __user *)argp))
834 control->prefer_rawmidi_subdevice = val;
837 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
838 return snd_rawmidi_info_select_user(card, argp);
844 * snd_rawmidi_receive - receive the input data from the device
845 * @substream: the rawmidi substream
846 * @buffer: the buffer pointer
847 * @count: the data size to read
849 * Reads the data from the internal buffer.
851 * Returns the size of read data, or a negative error code on failure.
853 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
854 const unsigned char *buffer, int count)
857 int result = 0, count1;
858 struct snd_rawmidi_runtime *runtime = substream->runtime;
860 if (runtime->buffer == NULL) {
861 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
864 spin_lock_irqsave(&runtime->lock, flags);
865 if (count == 1) { /* special case, faster code */
867 if (runtime->avail < runtime->buffer_size) {
868 runtime->buffer[runtime->hw_ptr++] = buffer[0];
869 runtime->hw_ptr %= runtime->buffer_size;
876 substream->bytes += count;
877 count1 = runtime->buffer_size - runtime->hw_ptr;
880 if (count1 > (int)(runtime->buffer_size - runtime->avail))
881 count1 = runtime->buffer_size - runtime->avail;
882 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
883 runtime->hw_ptr += count1;
884 runtime->hw_ptr %= runtime->buffer_size;
885 runtime->avail += count1;
891 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
892 count1 = runtime->buffer_size - runtime->avail;
893 runtime->xruns += count - count1;
896 memcpy(runtime->buffer, buffer, count1);
897 runtime->hw_ptr = count1;
898 runtime->avail += count1;
905 tasklet_hi_schedule(&runtime->tasklet);
906 else if (snd_rawmidi_ready(substream))
907 wake_up(&runtime->sleep);
909 spin_unlock_irqrestore(&runtime->lock, flags);
913 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
914 unsigned char __user *userbuf,
915 unsigned char *kernelbuf, long count)
918 long result = 0, count1;
919 struct snd_rawmidi_runtime *runtime = substream->runtime;
921 while (count > 0 && runtime->avail) {
922 count1 = runtime->buffer_size - runtime->appl_ptr;
925 spin_lock_irqsave(&runtime->lock, flags);
926 if (count1 > (int)runtime->avail)
927 count1 = runtime->avail;
929 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
931 spin_unlock_irqrestore(&runtime->lock, flags);
932 if (copy_to_user(userbuf + result,
933 runtime->buffer + runtime->appl_ptr, count1)) {
934 return result > 0 ? result : -EFAULT;
936 spin_lock_irqsave(&runtime->lock, flags);
938 runtime->appl_ptr += count1;
939 runtime->appl_ptr %= runtime->buffer_size;
940 runtime->avail -= count1;
941 spin_unlock_irqrestore(&runtime->lock, flags);
948 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
949 unsigned char *buf, long count)
951 snd_rawmidi_input_trigger(substream, 1);
952 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
955 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
960 struct snd_rawmidi_file *rfile;
961 struct snd_rawmidi_substream *substream;
962 struct snd_rawmidi_runtime *runtime;
964 rfile = file->private_data;
965 substream = rfile->input;
966 if (substream == NULL)
968 runtime = substream->runtime;
969 snd_rawmidi_input_trigger(substream, 1);
972 spin_lock_irq(&runtime->lock);
973 while (!snd_rawmidi_ready(substream)) {
975 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
976 spin_unlock_irq(&runtime->lock);
977 return result > 0 ? result : -EAGAIN;
979 init_waitqueue_entry(&wait, current);
980 add_wait_queue(&runtime->sleep, &wait);
981 set_current_state(TASK_INTERRUPTIBLE);
982 spin_unlock_irq(&runtime->lock);
984 remove_wait_queue(&runtime->sleep, &wait);
985 if (signal_pending(current))
986 return result > 0 ? result : -ERESTARTSYS;
988 return result > 0 ? result : -EIO;
989 spin_lock_irq(&runtime->lock);
991 spin_unlock_irq(&runtime->lock);
992 count1 = snd_rawmidi_kernel_read1(substream,
993 (unsigned char __user *)buf,
997 return result > 0 ? result : count1;
1006 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1007 * @substream: the rawmidi substream
1009 * Returns 1 if the internal output buffer is empty, 0 if not.
1011 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1013 struct snd_rawmidi_runtime *runtime = substream->runtime;
1015 unsigned long flags;
1017 if (runtime->buffer == NULL) {
1018 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1021 spin_lock_irqsave(&runtime->lock, flags);
1022 result = runtime->avail >= runtime->buffer_size;
1023 spin_unlock_irqrestore(&runtime->lock, flags);
1028 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1029 * @substream: the rawmidi substream
1030 * @buffer: the buffer pointer
1031 * @count: data size to transfer
1033 * Copies data from the internal output buffer to the given buffer.
1035 * Call this in the interrupt handler when the midi output is ready,
1036 * and call snd_rawmidi_transmit_ack() after the transmission is
1039 * Returns the size of copied data, or a negative error code on failure.
1041 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1042 unsigned char *buffer, int count)
1044 unsigned long flags;
1046 struct snd_rawmidi_runtime *runtime = substream->runtime;
1048 if (runtime->buffer == NULL) {
1049 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1053 spin_lock_irqsave(&runtime->lock, flags);
1054 if (runtime->avail >= runtime->buffer_size) {
1055 /* warning: lowlevel layer MUST trigger down the hardware */
1058 if (count == 1) { /* special case, faster code */
1059 *buffer = runtime->buffer[runtime->hw_ptr];
1062 count1 = runtime->buffer_size - runtime->hw_ptr;
1065 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1066 count1 = runtime->buffer_size - runtime->avail;
1067 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1071 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1072 count = runtime->buffer_size - runtime->avail - count1;
1073 memcpy(buffer + count1, runtime->buffer, count);
1078 spin_unlock_irqrestore(&runtime->lock, flags);
1083 * snd_rawmidi_transmit_ack - acknowledge the transmission
1084 * @substream: the rawmidi substream
1085 * @count: the tranferred count
1087 * Advances the hardware pointer for the internal output buffer with
1088 * the given size and updates the condition.
1089 * Call after the transmission is finished.
1091 * Returns the advanced size if successful, or a negative error code on failure.
1093 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1095 unsigned long flags;
1096 struct snd_rawmidi_runtime *runtime = substream->runtime;
1098 if (runtime->buffer == NULL) {
1099 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1102 spin_lock_irqsave(&runtime->lock, flags);
1103 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1104 runtime->hw_ptr += count;
1105 runtime->hw_ptr %= runtime->buffer_size;
1106 runtime->avail += count;
1107 substream->bytes += count;
1109 if (runtime->drain || snd_rawmidi_ready(substream))
1110 wake_up(&runtime->sleep);
1112 spin_unlock_irqrestore(&runtime->lock, flags);
1117 * snd_rawmidi_transmit - copy from the buffer to the device
1118 * @substream: the rawmidi substream
1119 * @buffer: the buffer pointer
1120 * @count: the data size to transfer
1122 * Copies data from the buffer to the device and advances the pointer.
1124 * Returns the copied size if successful, or a negative error code on failure.
1126 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1127 unsigned char *buffer, int count)
1129 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1132 return snd_rawmidi_transmit_ack(substream, count);
1135 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1136 const unsigned char __user *userbuf,
1137 const unsigned char *kernelbuf,
1140 unsigned long flags;
1141 long count1, result;
1142 struct snd_rawmidi_runtime *runtime = substream->runtime;
1144 snd_assert(kernelbuf != NULL || userbuf != NULL, return -EINVAL);
1145 snd_assert(runtime->buffer != NULL, return -EINVAL);
1148 spin_lock_irqsave(&runtime->lock, flags);
1149 if (substream->append) {
1150 if ((long)runtime->avail < count) {
1151 spin_unlock_irqrestore(&runtime->lock, flags);
1155 while (count > 0 && runtime->avail > 0) {
1156 count1 = runtime->buffer_size - runtime->appl_ptr;
1159 if (count1 > (long)runtime->avail)
1160 count1 = runtime->avail;
1162 memcpy(runtime->buffer + runtime->appl_ptr,
1163 kernelbuf + result, count1);
1165 spin_unlock_irqrestore(&runtime->lock, flags);
1166 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1167 userbuf + result, count1)) {
1168 spin_lock_irqsave(&runtime->lock, flags);
1169 result = result > 0 ? result : -EFAULT;
1172 spin_lock_irqsave(&runtime->lock, flags);
1174 runtime->appl_ptr += count1;
1175 runtime->appl_ptr %= runtime->buffer_size;
1176 runtime->avail -= count1;
1181 count1 = runtime->avail < runtime->buffer_size;
1182 spin_unlock_irqrestore(&runtime->lock, flags);
1184 snd_rawmidi_output_trigger(substream, 1);
1188 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1189 const unsigned char *buf, long count)
1191 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1194 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1195 size_t count, loff_t *offset)
1197 long result, timeout;
1199 struct snd_rawmidi_file *rfile;
1200 struct snd_rawmidi_runtime *runtime;
1201 struct snd_rawmidi_substream *substream;
1203 rfile = file->private_data;
1204 substream = rfile->output;
1205 runtime = substream->runtime;
1206 /* we cannot put an atomic message to our buffer */
1207 if (substream->append && count > runtime->buffer_size)
1211 spin_lock_irq(&runtime->lock);
1212 while (!snd_rawmidi_ready_append(substream, count)) {
1214 if (file->f_flags & O_NONBLOCK) {
1215 spin_unlock_irq(&runtime->lock);
1216 return result > 0 ? result : -EAGAIN;
1218 init_waitqueue_entry(&wait, current);
1219 add_wait_queue(&runtime->sleep, &wait);
1220 set_current_state(TASK_INTERRUPTIBLE);
1221 spin_unlock_irq(&runtime->lock);
1222 timeout = schedule_timeout(30 * HZ);
1223 remove_wait_queue(&runtime->sleep, &wait);
1224 if (signal_pending(current))
1225 return result > 0 ? result : -ERESTARTSYS;
1226 if (!runtime->avail && !timeout)
1227 return result > 0 ? result : -EIO;
1228 spin_lock_irq(&runtime->lock);
1230 spin_unlock_irq(&runtime->lock);
1231 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1233 return result > 0 ? result : count1;
1236 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1240 if (file->f_flags & O_SYNC) {
1241 spin_lock_irq(&runtime->lock);
1242 while (runtime->avail != runtime->buffer_size) {
1244 unsigned int last_avail = runtime->avail;
1245 init_waitqueue_entry(&wait, current);
1246 add_wait_queue(&runtime->sleep, &wait);
1247 set_current_state(TASK_INTERRUPTIBLE);
1248 spin_unlock_irq(&runtime->lock);
1249 timeout = schedule_timeout(30 * HZ);
1250 remove_wait_queue(&runtime->sleep, &wait);
1251 if (signal_pending(current))
1252 return result > 0 ? result : -ERESTARTSYS;
1253 if (runtime->avail == last_avail && !timeout)
1254 return result > 0 ? result : -EIO;
1255 spin_lock_irq(&runtime->lock);
1257 spin_unlock_irq(&runtime->lock);
1262 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1264 struct snd_rawmidi_file *rfile;
1265 struct snd_rawmidi_runtime *runtime;
1268 rfile = file->private_data;
1269 if (rfile->input != NULL) {
1270 runtime = rfile->input->runtime;
1271 snd_rawmidi_input_trigger(rfile->input, 1);
1272 poll_wait(file, &runtime->sleep, wait);
1274 if (rfile->output != NULL) {
1275 runtime = rfile->output->runtime;
1276 poll_wait(file, &runtime->sleep, wait);
1279 if (rfile->input != NULL) {
1280 if (snd_rawmidi_ready(rfile->input))
1281 mask |= POLLIN | POLLRDNORM;
1283 if (rfile->output != NULL) {
1284 if (snd_rawmidi_ready(rfile->output))
1285 mask |= POLLOUT | POLLWRNORM;
1292 #ifdef CONFIG_COMPAT
1293 #include "rawmidi_compat.c"
1295 #define snd_rawmidi_ioctl_compat NULL
1302 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1303 struct snd_info_buffer *buffer)
1305 struct snd_rawmidi *rmidi;
1306 struct snd_rawmidi_substream *substream;
1307 struct snd_rawmidi_runtime *runtime;
1309 rmidi = entry->private_data;
1310 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1311 mutex_lock(&rmidi->open_mutex);
1312 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1313 list_for_each_entry(substream,
1314 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1318 " Tx bytes : %lu\n",
1320 (unsigned long) substream->bytes);
1321 if (substream->opened) {
1322 runtime = substream->runtime;
1325 " Buffer size : %lu\n"
1327 runtime->oss ? "OSS compatible" : "native",
1328 (unsigned long) runtime->buffer_size,
1329 (unsigned long) runtime->avail);
1333 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1334 list_for_each_entry(substream,
1335 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1339 " Rx bytes : %lu\n",
1341 (unsigned long) substream->bytes);
1342 if (substream->opened) {
1343 runtime = substream->runtime;
1345 " Buffer size : %lu\n"
1347 " Overruns : %lu\n",
1348 (unsigned long) runtime->buffer_size,
1349 (unsigned long) runtime->avail,
1350 (unsigned long) runtime->xruns);
1354 mutex_unlock(&rmidi->open_mutex);
1358 * Register functions
1361 static const struct file_operations snd_rawmidi_f_ops =
1363 .owner = THIS_MODULE,
1364 .read = snd_rawmidi_read,
1365 .write = snd_rawmidi_write,
1366 .open = snd_rawmidi_open,
1367 .release = snd_rawmidi_release,
1368 .poll = snd_rawmidi_poll,
1369 .unlocked_ioctl = snd_rawmidi_ioctl,
1370 .compat_ioctl = snd_rawmidi_ioctl_compat,
1373 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1374 struct snd_rawmidi_str *stream,
1378 struct snd_rawmidi_substream *substream;
1381 for (idx = 0; idx < count; idx++) {
1382 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1383 if (substream == NULL) {
1384 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1387 substream->stream = direction;
1388 substream->number = idx;
1389 substream->rmidi = rmidi;
1390 substream->pstr = stream;
1391 list_add_tail(&substream->list, &stream->substreams);
1392 stream->substream_count++;
1398 * snd_rawmidi_new - create a rawmidi instance
1399 * @card: the card instance
1400 * @id: the id string
1401 * @device: the device index
1402 * @output_count: the number of output streams
1403 * @input_count: the number of input streams
1404 * @rrawmidi: the pointer to store the new rawmidi instance
1406 * Creates a new rawmidi instance.
1407 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1409 * Returns zero if successful, or a negative error code on failure.
1411 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1412 int output_count, int input_count,
1413 struct snd_rawmidi ** rrawmidi)
1415 struct snd_rawmidi *rmidi;
1417 static struct snd_device_ops ops = {
1418 .dev_free = snd_rawmidi_dev_free,
1419 .dev_register = snd_rawmidi_dev_register,
1420 .dev_disconnect = snd_rawmidi_dev_disconnect,
1423 snd_assert(rrawmidi != NULL, return -EINVAL);
1425 snd_assert(card != NULL, return -ENXIO);
1426 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1427 if (rmidi == NULL) {
1428 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1432 rmidi->device = device;
1433 mutex_init(&rmidi->open_mutex);
1434 init_waitqueue_head(&rmidi->open_wait);
1435 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1436 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1439 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1440 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1441 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1442 SNDRV_RAWMIDI_STREAM_INPUT,
1443 input_count)) < 0) {
1444 snd_rawmidi_free(rmidi);
1447 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1448 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1449 SNDRV_RAWMIDI_STREAM_OUTPUT,
1450 output_count)) < 0) {
1451 snd_rawmidi_free(rmidi);
1454 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1455 snd_rawmidi_free(rmidi);
1462 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1464 struct snd_rawmidi_substream *substream;
1466 while (!list_empty(&stream->substreams)) {
1467 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1468 list_del(&substream->list);
1473 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1475 snd_assert(rmidi != NULL, return -ENXIO);
1477 snd_info_free_entry(rmidi->proc_entry);
1478 rmidi->proc_entry = NULL;
1479 mutex_lock(®ister_mutex);
1480 if (rmidi->ops && rmidi->ops->dev_unregister)
1481 rmidi->ops->dev_unregister(rmidi);
1482 mutex_unlock(®ister_mutex);
1484 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1485 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1486 if (rmidi->private_free)
1487 rmidi->private_free(rmidi);
1492 static int snd_rawmidi_dev_free(struct snd_device *device)
1494 struct snd_rawmidi *rmidi = device->device_data;
1495 return snd_rawmidi_free(rmidi);
1498 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1499 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1501 struct snd_rawmidi *rmidi = device->private_data;
1502 rmidi->seq_dev = NULL;
1506 static int snd_rawmidi_dev_register(struct snd_device *device)
1509 struct snd_info_entry *entry;
1511 struct snd_rawmidi *rmidi = device->device_data;
1513 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1515 mutex_lock(®ister_mutex);
1516 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1517 mutex_unlock(®ister_mutex);
1520 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1521 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1522 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1523 rmidi->card, rmidi->device,
1524 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1525 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1526 list_del(&rmidi->list);
1527 mutex_unlock(®ister_mutex);
1530 if (rmidi->ops && rmidi->ops->dev_register &&
1531 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1532 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1533 list_del(&rmidi->list);
1534 mutex_unlock(®ister_mutex);
1537 #ifdef CONFIG_SND_OSSEMUL
1539 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1540 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1541 rmidi->card, 0, &snd_rawmidi_f_ops,
1543 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1546 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1547 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1551 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1552 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1553 rmidi->card, 1, &snd_rawmidi_f_ops,
1555 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1560 #endif /* CONFIG_SND_OSSEMUL */
1561 mutex_unlock(®ister_mutex);
1562 sprintf(name, "midi%d", rmidi->device);
1563 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1565 entry->private_data = rmidi;
1566 entry->c.text.read = snd_rawmidi_proc_info_read;
1567 if (snd_info_register(entry) < 0) {
1568 snd_info_free_entry(entry);
1572 rmidi->proc_entry = entry;
1573 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1574 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1575 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1576 rmidi->seq_dev->private_data = rmidi;
1577 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1578 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1579 snd_device_register(rmidi->card, rmidi->seq_dev);
1586 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1588 struct snd_rawmidi *rmidi = device->device_data;
1590 mutex_lock(®ister_mutex);
1591 list_del_init(&rmidi->list);
1592 #ifdef CONFIG_SND_OSSEMUL
1593 if (rmidi->ossreg) {
1594 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1595 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1596 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1597 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1600 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1601 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1604 #endif /* CONFIG_SND_OSSEMUL */
1605 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1606 mutex_unlock(®ister_mutex);
1611 * snd_rawmidi_set_ops - set the rawmidi operators
1612 * @rmidi: the rawmidi instance
1613 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1614 * @ops: the operator table
1616 * Sets the rawmidi operators for the given stream direction.
1618 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1619 struct snd_rawmidi_ops *ops)
1621 struct snd_rawmidi_substream *substream;
1623 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1624 substream->ops = ops;
1631 static int __init alsa_rawmidi_init(void)
1634 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1635 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1636 #ifdef CONFIG_SND_OSSEMUL
1638 /* check device map table */
1639 for (i = 0; i < SNDRV_CARDS; i++) {
1640 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1641 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1644 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1645 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1650 #endif /* CONFIG_SND_OSSEMUL */
1654 static void __exit alsa_rawmidi_exit(void)
1656 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1657 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1660 module_init(alsa_rawmidi_init)
1661 module_exit(alsa_rawmidi_exit)
1663 EXPORT_SYMBOL(snd_rawmidi_output_params);
1664 EXPORT_SYMBOL(snd_rawmidi_input_params);
1665 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1666 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1667 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1668 EXPORT_SYMBOL(snd_rawmidi_receive);
1669 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1670 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1671 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1672 EXPORT_SYMBOL(snd_rawmidi_transmit);
1673 EXPORT_SYMBOL(snd_rawmidi_new);
1674 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1675 EXPORT_SYMBOL(snd_rawmidi_info_select);
1676 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1677 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1678 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1679 EXPORT_SYMBOL(snd_rawmidi_kernel_write);