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/mutex.h>
32 #include <linux/moduleparam.h>
33 #include <linux/delay.h>
34 #include <linux/wait.h>
35 #include <sound/rawmidi.h>
36 #include <sound/info.h>
37 #include <sound/control.h>
38 #include <sound/minors.h>
39 #include <sound/initval.h>
41 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
42 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
43 MODULE_LICENSE("GPL");
45 #ifdef CONFIG_SND_OSSEMUL
46 static int midi_map[SNDRV_CARDS];
47 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
48 module_param_array(midi_map, int, NULL, 0444);
49 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
50 module_param_array(amidi_map, int, NULL, 0444);
51 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
52 #endif /* CONFIG_SND_OSSEMUL */
54 static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
55 static int snd_rawmidi_dev_free(struct snd_device *device);
56 static int snd_rawmidi_dev_register(struct snd_device *device);
57 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
59 static LIST_HEAD(snd_rawmidi_devices);
60 static DEFINE_MUTEX(register_mutex);
62 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
64 struct snd_rawmidi *rawmidi;
66 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
67 if (rawmidi->card == card && rawmidi->device == device)
72 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
74 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
76 return SNDRV_RAWMIDI_LFLG_OUTPUT;
78 return SNDRV_RAWMIDI_LFLG_INPUT;
80 return SNDRV_RAWMIDI_LFLG_OPEN;
84 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
86 struct snd_rawmidi_runtime *runtime = substream->runtime;
87 return runtime->avail >= runtime->avail_min;
90 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
93 struct snd_rawmidi_runtime *runtime = substream->runtime;
94 return runtime->avail >= runtime->avail_min &&
95 (!substream->append || runtime->avail >= count);
98 static void snd_rawmidi_input_event_tasklet(unsigned long data)
100 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
101 substream->runtime->event(substream);
104 static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
106 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
107 substream->ops->trigger(substream, 1);
110 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
112 struct snd_rawmidi_runtime *runtime;
114 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
116 spin_lock_init(&runtime->lock);
117 init_waitqueue_head(&runtime->sleep);
118 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
119 tasklet_init(&runtime->tasklet,
120 snd_rawmidi_input_event_tasklet,
121 (unsigned long)substream);
123 tasklet_init(&runtime->tasklet,
124 snd_rawmidi_output_trigger_tasklet,
125 (unsigned long)substream);
126 runtime->event = NULL;
127 runtime->buffer_size = PAGE_SIZE;
128 runtime->avail_min = 1;
129 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
132 runtime->avail = runtime->buffer_size;
133 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
137 runtime->appl_ptr = runtime->hw_ptr = 0;
138 substream->runtime = runtime;
142 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
144 struct snd_rawmidi_runtime *runtime = substream->runtime;
146 kfree(runtime->buffer);
148 substream->runtime = NULL;
152 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
155 tasklet_hi_schedule(&substream->runtime->tasklet);
157 tasklet_kill(&substream->runtime->tasklet);
158 substream->ops->trigger(substream, 0);
162 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
164 substream->ops->trigger(substream, up);
165 if (!up && substream->runtime->event)
166 tasklet_kill(&substream->runtime->tasklet);
169 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
172 struct snd_rawmidi_runtime *runtime = substream->runtime;
174 snd_rawmidi_output_trigger(substream, 0);
176 spin_lock_irqsave(&runtime->lock, flags);
177 runtime->appl_ptr = runtime->hw_ptr = 0;
178 runtime->avail = runtime->buffer_size;
179 spin_unlock_irqrestore(&runtime->lock, flags);
183 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
187 struct snd_rawmidi_runtime *runtime = substream->runtime;
191 timeout = wait_event_interruptible_timeout(runtime->sleep,
192 (runtime->avail >= runtime->buffer_size),
194 if (signal_pending(current))
196 if (runtime->avail < runtime->buffer_size && !timeout) {
197 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
201 if (err != -ERESTARTSYS) {
202 /* we need wait a while to make sure that Tx FIFOs are empty */
203 if (substream->ops->drain)
204 substream->ops->drain(substream);
207 snd_rawmidi_drop_output(substream);
212 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
215 struct snd_rawmidi_runtime *runtime = substream->runtime;
217 snd_rawmidi_input_trigger(substream, 0);
219 spin_lock_irqsave(&runtime->lock, flags);
220 runtime->appl_ptr = runtime->hw_ptr = 0;
222 spin_unlock_irqrestore(&runtime->lock, flags);
226 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
227 int mode, struct snd_rawmidi_file * rfile)
229 struct snd_rawmidi *rmidi;
230 struct list_head *list1, *list2;
231 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
232 struct snd_rawmidi_runtime *input = NULL, *output = NULL;
236 rfile->input = rfile->output = NULL;
237 mutex_lock(®ister_mutex);
238 rmidi = snd_rawmidi_search(card, device);
239 mutex_unlock(®ister_mutex);
244 if (!try_module_get(rmidi->card->module)) {
248 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
249 mutex_lock(&rmidi->open_mutex);
250 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
251 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
255 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
259 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
260 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
265 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
266 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
270 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
274 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
275 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
280 list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
282 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
284 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
290 sinput = list_entry(list1, struct snd_rawmidi_substream, list);
291 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
293 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
298 list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
300 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
302 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
308 soutput = list_entry(list2, struct snd_rawmidi_substream, list);
309 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
310 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
311 if (soutput->opened && !soutput->append)
318 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
323 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
324 if ((err = snd_rawmidi_runtime_create(sinput)) < 0)
326 input = sinput->runtime;
327 if ((err = sinput->ops->open(sinput)) < 0)
330 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
334 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
337 if ((err = snd_rawmidi_runtime_create(soutput)) < 0) {
338 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
339 sinput->ops->close(sinput);
342 output = soutput->runtime;
343 if ((err = soutput->ops->open(soutput)) < 0) {
344 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
345 sinput->ops->close(sinput);
350 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
352 if (soutput->use_count++ == 0)
353 soutput->active_sensing = 1;
354 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
358 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
359 mutex_unlock(&rmidi->open_mutex);
361 rfile->rmidi = rmidi;
362 rfile->input = sinput;
363 rfile->output = soutput;
369 snd_rawmidi_runtime_free(sinput);
371 snd_rawmidi_runtime_free(soutput);
372 module_put(rmidi->card->module);
373 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
374 mutex_unlock(&rmidi->open_mutex);
379 static int snd_rawmidi_open(struct inode *inode, struct file *file)
381 int maj = imajor(inode);
382 struct snd_card *card;
384 unsigned short fflags;
386 struct snd_rawmidi *rmidi;
387 struct snd_rawmidi_file *rawmidi_file;
389 struct snd_ctl_file *kctl;
391 if (maj == snd_major) {
392 rmidi = snd_lookup_minor_data(iminor(inode),
393 SNDRV_DEVICE_TYPE_RAWMIDI);
394 #ifdef CONFIG_SND_OSSEMUL
395 } else if (maj == SOUND_MAJOR) {
396 rmidi = snd_lookup_oss_minor_data(iminor(inode),
397 SNDRV_OSS_DEVICE_TYPE_MIDI);
404 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
405 return -EINVAL; /* invalid combination */
407 err = snd_card_file_add(card, file);
410 fflags = snd_rawmidi_file_flags(file);
411 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
412 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
413 fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
414 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
415 if (rawmidi_file == NULL) {
416 snd_card_file_remove(card, file);
419 init_waitqueue_entry(&wait, current);
420 add_wait_queue(&rmidi->open_wait, &wait);
421 mutex_lock(&rmidi->open_mutex);
424 down_read(&card->controls_rwsem);
425 list_for_each_entry(kctl, &card->ctl_files, list) {
426 if (kctl->pid == current->pid) {
427 subdevice = kctl->prefer_rawmidi_subdevice;
432 up_read(&card->controls_rwsem);
433 err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
434 subdevice, fflags, rawmidi_file);
437 if (err == -EAGAIN) {
438 if (file->f_flags & O_NONBLOCK) {
444 set_current_state(TASK_INTERRUPTIBLE);
445 mutex_unlock(&rmidi->open_mutex);
447 mutex_lock(&rmidi->open_mutex);
448 if (signal_pending(current)) {
453 #ifdef CONFIG_SND_OSSEMUL
454 if (rawmidi_file->input && rawmidi_file->input->runtime)
455 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
456 if (rawmidi_file->output && rawmidi_file->output->runtime)
457 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
459 remove_wait_queue(&rmidi->open_wait, &wait);
461 file->private_data = rawmidi_file;
463 snd_card_file_remove(card, file);
466 mutex_unlock(&rmidi->open_mutex);
470 int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
472 struct snd_rawmidi *rmidi;
473 struct snd_rawmidi_substream *substream;
474 struct snd_rawmidi_runtime *runtime;
476 snd_assert(rfile != NULL, return -ENXIO);
477 snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
478 rmidi = rfile->rmidi;
479 mutex_lock(&rmidi->open_mutex);
480 if (rfile->input != NULL) {
481 substream = rfile->input;
483 runtime = substream->runtime;
484 snd_rawmidi_input_trigger(substream, 0);
485 substream->ops->close(substream);
486 if (runtime->private_free != NULL)
487 runtime->private_free(substream);
488 snd_rawmidi_runtime_free(substream);
489 substream->opened = 0;
490 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
492 if (rfile->output != NULL) {
493 substream = rfile->output;
494 rfile->output = NULL;
495 if (--substream->use_count == 0) {
496 runtime = substream->runtime;
497 if (substream->active_sensing) {
498 unsigned char buf = 0xfe;
499 /* sending single active sensing message to shut the device up */
500 snd_rawmidi_kernel_write(substream, &buf, 1);
502 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
503 snd_rawmidi_output_trigger(substream, 0);
504 substream->ops->close(substream);
505 if (runtime->private_free != NULL)
506 runtime->private_free(substream);
507 snd_rawmidi_runtime_free(substream);
508 substream->opened = 0;
509 substream->append = 0;
511 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
513 mutex_unlock(&rmidi->open_mutex);
514 module_put(rmidi->card->module);
518 static int snd_rawmidi_release(struct inode *inode, struct file *file)
520 struct snd_rawmidi_file *rfile;
521 struct snd_rawmidi *rmidi;
524 rfile = file->private_data;
525 err = snd_rawmidi_kernel_release(rfile);
526 rmidi = rfile->rmidi;
527 wake_up(&rmidi->open_wait);
529 snd_card_file_remove(rmidi->card, file);
533 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
534 struct snd_rawmidi_info *info)
536 struct snd_rawmidi *rmidi;
538 if (substream == NULL)
540 rmidi = substream->rmidi;
541 memset(info, 0, sizeof(*info));
542 info->card = rmidi->card->number;
543 info->device = rmidi->device;
544 info->subdevice = substream->number;
545 info->stream = substream->stream;
546 info->flags = rmidi->info_flags;
547 strcpy(info->id, rmidi->id);
548 strcpy(info->name, rmidi->name);
549 strcpy(info->subname, substream->name);
550 info->subdevices_count = substream->pstr->substream_count;
551 info->subdevices_avail = (substream->pstr->substream_count -
552 substream->pstr->substream_opened);
556 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
557 struct snd_rawmidi_info __user * _info)
559 struct snd_rawmidi_info info;
561 if ((err = snd_rawmidi_info(substream, &info)) < 0)
563 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
568 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
570 struct snd_rawmidi *rmidi;
571 struct snd_rawmidi_str *pstr;
572 struct snd_rawmidi_substream *substream;
574 mutex_lock(®ister_mutex);
575 rmidi = snd_rawmidi_search(card, info->device);
576 mutex_unlock(®ister_mutex);
579 if (info->stream < 0 || info->stream > 1)
581 pstr = &rmidi->streams[info->stream];
582 if (pstr->substream_count == 0)
584 if (info->subdevice >= pstr->substream_count)
586 list_for_each_entry(substream, &pstr->substreams, list) {
587 if ((unsigned int)substream->number == info->subdevice)
588 return snd_rawmidi_info(substream, info);
593 static int snd_rawmidi_info_select_user(struct snd_card *card,
594 struct snd_rawmidi_info __user *_info)
597 struct snd_rawmidi_info info;
598 if (get_user(info.device, &_info->device))
600 if (get_user(info.stream, &_info->stream))
602 if (get_user(info.subdevice, &_info->subdevice))
604 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
606 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
611 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
612 struct snd_rawmidi_params * params)
615 struct snd_rawmidi_runtime *runtime = substream->runtime;
617 if (substream->append && substream->use_count > 1)
619 snd_rawmidi_drain_output(substream);
620 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
623 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
626 if (params->buffer_size != runtime->buffer_size) {
627 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
630 kfree(runtime->buffer);
631 runtime->buffer = newbuf;
632 runtime->buffer_size = params->buffer_size;
633 runtime->avail = runtime->buffer_size;
635 runtime->avail_min = params->avail_min;
636 substream->active_sensing = !params->no_active_sensing;
640 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
641 struct snd_rawmidi_params * params)
644 struct snd_rawmidi_runtime *runtime = substream->runtime;
646 snd_rawmidi_drain_input(substream);
647 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
650 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
653 if (params->buffer_size != runtime->buffer_size) {
654 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
657 kfree(runtime->buffer);
658 runtime->buffer = newbuf;
659 runtime->buffer_size = params->buffer_size;
661 runtime->avail_min = params->avail_min;
665 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
666 struct snd_rawmidi_status * status)
668 struct snd_rawmidi_runtime *runtime = substream->runtime;
670 memset(status, 0, sizeof(*status));
671 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
672 spin_lock_irq(&runtime->lock);
673 status->avail = runtime->avail;
674 spin_unlock_irq(&runtime->lock);
678 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
679 struct snd_rawmidi_status * status)
681 struct snd_rawmidi_runtime *runtime = substream->runtime;
683 memset(status, 0, sizeof(*status));
684 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
685 spin_lock_irq(&runtime->lock);
686 status->avail = runtime->avail;
687 status->xruns = runtime->xruns;
689 spin_unlock_irq(&runtime->lock);
693 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
695 struct snd_rawmidi_file *rfile;
696 void __user *argp = (void __user *)arg;
698 rfile = file->private_data;
699 if (((cmd >> 8) & 0xff) != 'W')
702 case SNDRV_RAWMIDI_IOCTL_PVERSION:
703 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
704 case SNDRV_RAWMIDI_IOCTL_INFO:
707 struct snd_rawmidi_info __user *info = argp;
708 if (get_user(stream, &info->stream))
711 case SNDRV_RAWMIDI_STREAM_INPUT:
712 return snd_rawmidi_info_user(rfile->input, info);
713 case SNDRV_RAWMIDI_STREAM_OUTPUT:
714 return snd_rawmidi_info_user(rfile->output, info);
719 case SNDRV_RAWMIDI_IOCTL_PARAMS:
721 struct snd_rawmidi_params params;
722 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
724 switch (params.stream) {
725 case SNDRV_RAWMIDI_STREAM_OUTPUT:
726 if (rfile->output == NULL)
728 return snd_rawmidi_output_params(rfile->output, ¶ms);
729 case SNDRV_RAWMIDI_STREAM_INPUT:
730 if (rfile->input == NULL)
732 return snd_rawmidi_input_params(rfile->input, ¶ms);
737 case SNDRV_RAWMIDI_IOCTL_STATUS:
740 struct snd_rawmidi_status status;
741 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
743 switch (status.stream) {
744 case SNDRV_RAWMIDI_STREAM_OUTPUT:
745 if (rfile->output == NULL)
747 err = snd_rawmidi_output_status(rfile->output, &status);
749 case SNDRV_RAWMIDI_STREAM_INPUT:
750 if (rfile->input == NULL)
752 err = snd_rawmidi_input_status(rfile->input, &status);
759 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
763 case SNDRV_RAWMIDI_IOCTL_DROP:
766 if (get_user(val, (int __user *) argp))
769 case SNDRV_RAWMIDI_STREAM_OUTPUT:
770 if (rfile->output == NULL)
772 return snd_rawmidi_drop_output(rfile->output);
777 case SNDRV_RAWMIDI_IOCTL_DRAIN:
780 if (get_user(val, (int __user *) argp))
783 case SNDRV_RAWMIDI_STREAM_OUTPUT:
784 if (rfile->output == NULL)
786 return snd_rawmidi_drain_output(rfile->output);
787 case SNDRV_RAWMIDI_STREAM_INPUT:
788 if (rfile->input == NULL)
790 return snd_rawmidi_drain_input(rfile->input);
795 #ifdef CONFIG_SND_DEBUG
797 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
803 static int snd_rawmidi_control_ioctl(struct snd_card *card,
804 struct snd_ctl_file *control,
808 void __user *argp = (void __user *)arg;
811 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
815 if (get_user(device, (int __user *)argp))
817 mutex_lock(®ister_mutex);
818 device = device < 0 ? 0 : device + 1;
819 while (device < SNDRV_RAWMIDI_DEVICES) {
820 if (snd_rawmidi_search(card, device))
824 if (device == SNDRV_RAWMIDI_DEVICES)
826 mutex_unlock(®ister_mutex);
827 if (put_user(device, (int __user *)argp))
831 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
835 if (get_user(val, (int __user *)argp))
837 control->prefer_rawmidi_subdevice = val;
840 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
841 return snd_rawmidi_info_select_user(card, argp);
847 * snd_rawmidi_receive - receive the input data from the device
848 * @substream: the rawmidi substream
849 * @buffer: the buffer pointer
850 * @count: the data size to read
852 * Reads the data from the internal buffer.
854 * Returns the size of read data, or a negative error code on failure.
856 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
857 const unsigned char *buffer, int count)
860 int result = 0, count1;
861 struct snd_rawmidi_runtime *runtime = substream->runtime;
863 if (runtime->buffer == NULL) {
864 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
867 spin_lock_irqsave(&runtime->lock, flags);
868 if (count == 1) { /* special case, faster code */
870 if (runtime->avail < runtime->buffer_size) {
871 runtime->buffer[runtime->hw_ptr++] = buffer[0];
872 runtime->hw_ptr %= runtime->buffer_size;
879 substream->bytes += count;
880 count1 = runtime->buffer_size - runtime->hw_ptr;
883 if (count1 > (int)(runtime->buffer_size - runtime->avail))
884 count1 = runtime->buffer_size - runtime->avail;
885 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
886 runtime->hw_ptr += count1;
887 runtime->hw_ptr %= runtime->buffer_size;
888 runtime->avail += count1;
894 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
895 count1 = runtime->buffer_size - runtime->avail;
896 runtime->xruns += count - count1;
899 memcpy(runtime->buffer, buffer, count1);
900 runtime->hw_ptr = count1;
901 runtime->avail += count1;
908 tasklet_hi_schedule(&runtime->tasklet);
909 else if (snd_rawmidi_ready(substream))
910 wake_up(&runtime->sleep);
912 spin_unlock_irqrestore(&runtime->lock, flags);
916 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
917 unsigned char *buf, long count, int kernel)
920 long result = 0, count1;
921 struct snd_rawmidi_runtime *runtime = substream->runtime;
923 while (count > 0 && runtime->avail) {
924 count1 = runtime->buffer_size - runtime->appl_ptr;
927 spin_lock_irqsave(&runtime->lock, flags);
928 if (count1 > (int)runtime->avail)
929 count1 = runtime->avail;
931 memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
933 spin_unlock_irqrestore(&runtime->lock, flags);
934 if (copy_to_user((char __user *)buf + result,
935 runtime->buffer + runtime->appl_ptr, count1)) {
936 return result > 0 ? result : -EFAULT;
938 spin_lock_irqsave(&runtime->lock, flags);
940 runtime->appl_ptr += count1;
941 runtime->appl_ptr %= runtime->buffer_size;
942 runtime->avail -= count1;
943 spin_unlock_irqrestore(&runtime->lock, flags);
950 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
951 unsigned char *buf, long count)
953 snd_rawmidi_input_trigger(substream, 1);
954 return snd_rawmidi_kernel_read1(substream, buf, count, 1);
957 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
962 struct snd_rawmidi_file *rfile;
963 struct snd_rawmidi_substream *substream;
964 struct snd_rawmidi_runtime *runtime;
966 rfile = file->private_data;
967 substream = rfile->input;
968 if (substream == NULL)
970 runtime = substream->runtime;
971 snd_rawmidi_input_trigger(substream, 1);
974 spin_lock_irq(&runtime->lock);
975 while (!snd_rawmidi_ready(substream)) {
977 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
978 spin_unlock_irq(&runtime->lock);
979 return result > 0 ? result : -EAGAIN;
981 init_waitqueue_entry(&wait, current);
982 add_wait_queue(&runtime->sleep, &wait);
983 set_current_state(TASK_INTERRUPTIBLE);
984 spin_unlock_irq(&runtime->lock);
986 remove_wait_queue(&runtime->sleep, &wait);
987 if (signal_pending(current))
988 return result > 0 ? result : -ERESTARTSYS;
990 return result > 0 ? result : -EIO;
991 spin_lock_irq(&runtime->lock);
993 spin_unlock_irq(&runtime->lock);
994 count1 = snd_rawmidi_kernel_read1(substream,
995 (unsigned char __force *)buf,
998 return result > 0 ? result : count1;
1007 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1008 * @substream: the rawmidi substream
1010 * Returns 1 if the internal output buffer is empty, 0 if not.
1012 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1014 struct snd_rawmidi_runtime *runtime = substream->runtime;
1016 unsigned long flags;
1018 if (runtime->buffer == NULL) {
1019 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1022 spin_lock_irqsave(&runtime->lock, flags);
1023 result = runtime->avail >= runtime->buffer_size;
1024 spin_unlock_irqrestore(&runtime->lock, flags);
1029 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1030 * @substream: the rawmidi substream
1031 * @buffer: the buffer pointer
1032 * @count: data size to transfer
1034 * Copies data from the internal output buffer to the given buffer.
1036 * Call this in the interrupt handler when the midi output is ready,
1037 * and call snd_rawmidi_transmit_ack() after the transmission is
1040 * Returns the size of copied data, or a negative error code on failure.
1042 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1043 unsigned char *buffer, int count)
1045 unsigned long flags;
1047 struct snd_rawmidi_runtime *runtime = substream->runtime;
1049 if (runtime->buffer == NULL) {
1050 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1054 spin_lock_irqsave(&runtime->lock, flags);
1055 if (runtime->avail >= runtime->buffer_size) {
1056 /* warning: lowlevel layer MUST trigger down the hardware */
1059 if (count == 1) { /* special case, faster code */
1060 *buffer = runtime->buffer[runtime->hw_ptr];
1063 count1 = runtime->buffer_size - runtime->hw_ptr;
1066 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1067 count1 = runtime->buffer_size - runtime->avail;
1068 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1072 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1073 count = runtime->buffer_size - runtime->avail - count1;
1074 memcpy(buffer + count1, runtime->buffer, count);
1079 spin_unlock_irqrestore(&runtime->lock, flags);
1084 * snd_rawmidi_transmit_ack - acknowledge the transmission
1085 * @substream: the rawmidi substream
1086 * @count: the tranferred count
1088 * Advances the hardware pointer for the internal output buffer with
1089 * the given size and updates the condition.
1090 * Call after the transmission is finished.
1092 * Returns the advanced size if successful, or a negative error code on failure.
1094 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1096 unsigned long flags;
1097 struct snd_rawmidi_runtime *runtime = substream->runtime;
1099 if (runtime->buffer == NULL) {
1100 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1103 spin_lock_irqsave(&runtime->lock, flags);
1104 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1105 runtime->hw_ptr += count;
1106 runtime->hw_ptr %= runtime->buffer_size;
1107 runtime->avail += count;
1108 substream->bytes += count;
1110 if (runtime->drain || snd_rawmidi_ready(substream))
1111 wake_up(&runtime->sleep);
1113 spin_unlock_irqrestore(&runtime->lock, flags);
1118 * snd_rawmidi_transmit - copy from the buffer to the device
1119 * @substream: the rawmidi substream
1120 * @buffer: the buffer pointer
1121 * @count: the data size to transfer
1123 * Copies data from the buffer to the device and advances the pointer.
1125 * Returns the copied size if successful, or a negative error code on failure.
1127 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1128 unsigned char *buffer, int count)
1130 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1133 return snd_rawmidi_transmit_ack(substream, count);
1136 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1137 const unsigned char *buf, long count, int kernel)
1139 unsigned long flags;
1140 long count1, result;
1141 struct snd_rawmidi_runtime *runtime = substream->runtime;
1143 snd_assert(buf != NULL, return -EINVAL);
1144 snd_assert(runtime->buffer != NULL, return -EINVAL);
1147 spin_lock_irqsave(&runtime->lock, flags);
1148 if (substream->append) {
1149 if ((long)runtime->avail < count) {
1150 spin_unlock_irqrestore(&runtime->lock, flags);
1154 while (count > 0 && runtime->avail > 0) {
1155 count1 = runtime->buffer_size - runtime->appl_ptr;
1158 if (count1 > (long)runtime->avail)
1159 count1 = runtime->avail;
1161 memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1163 spin_unlock_irqrestore(&runtime->lock, flags);
1164 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1165 (char __user *)buf, count1)) {
1166 spin_lock_irqsave(&runtime->lock, flags);
1167 result = result > 0 ? result : -EFAULT;
1170 spin_lock_irqsave(&runtime->lock, flags);
1172 runtime->appl_ptr += count1;
1173 runtime->appl_ptr %= runtime->buffer_size;
1174 runtime->avail -= count1;
1180 count1 = runtime->avail < runtime->buffer_size;
1181 spin_unlock_irqrestore(&runtime->lock, flags);
1183 snd_rawmidi_output_trigger(substream, 1);
1187 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1188 const unsigned char *buf, long count)
1190 return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1193 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1194 size_t count, loff_t *offset)
1196 long result, timeout;
1198 struct snd_rawmidi_file *rfile;
1199 struct snd_rawmidi_runtime *runtime;
1200 struct snd_rawmidi_substream *substream;
1202 rfile = file->private_data;
1203 substream = rfile->output;
1204 runtime = substream->runtime;
1205 /* we cannot put an atomic message to our buffer */
1206 if (substream->append && count > runtime->buffer_size)
1210 spin_lock_irq(&runtime->lock);
1211 while (!snd_rawmidi_ready_append(substream, count)) {
1213 if (file->f_flags & O_NONBLOCK) {
1214 spin_unlock_irq(&runtime->lock);
1215 return result > 0 ? result : -EAGAIN;
1217 init_waitqueue_entry(&wait, current);
1218 add_wait_queue(&runtime->sleep, &wait);
1219 set_current_state(TASK_INTERRUPTIBLE);
1220 spin_unlock_irq(&runtime->lock);
1221 timeout = schedule_timeout(30 * HZ);
1222 remove_wait_queue(&runtime->sleep, &wait);
1223 if (signal_pending(current))
1224 return result > 0 ? result : -ERESTARTSYS;
1225 if (!runtime->avail && !timeout)
1226 return result > 0 ? result : -EIO;
1227 spin_lock_irq(&runtime->lock);
1229 spin_unlock_irq(&runtime->lock);
1230 count1 = snd_rawmidi_kernel_write1(substream,
1231 (unsigned char __force *)buf,
1234 return result > 0 ? result : count1;
1237 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1241 if (file->f_flags & O_SYNC) {
1242 spin_lock_irq(&runtime->lock);
1243 while (runtime->avail != runtime->buffer_size) {
1245 unsigned int last_avail = runtime->avail;
1246 init_waitqueue_entry(&wait, current);
1247 add_wait_queue(&runtime->sleep, &wait);
1248 set_current_state(TASK_INTERRUPTIBLE);
1249 spin_unlock_irq(&runtime->lock);
1250 timeout = schedule_timeout(30 * HZ);
1251 remove_wait_queue(&runtime->sleep, &wait);
1252 if (signal_pending(current))
1253 return result > 0 ? result : -ERESTARTSYS;
1254 if (runtime->avail == last_avail && !timeout)
1255 return result > 0 ? result : -EIO;
1256 spin_lock_irq(&runtime->lock);
1258 spin_unlock_irq(&runtime->lock);
1263 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1265 struct snd_rawmidi_file *rfile;
1266 struct snd_rawmidi_runtime *runtime;
1269 rfile = file->private_data;
1270 if (rfile->input != NULL) {
1271 runtime = rfile->input->runtime;
1272 snd_rawmidi_input_trigger(rfile->input, 1);
1273 poll_wait(file, &runtime->sleep, wait);
1275 if (rfile->output != NULL) {
1276 runtime = rfile->output->runtime;
1277 poll_wait(file, &runtime->sleep, wait);
1280 if (rfile->input != NULL) {
1281 if (snd_rawmidi_ready(rfile->input))
1282 mask |= POLLIN | POLLRDNORM;
1284 if (rfile->output != NULL) {
1285 if (snd_rawmidi_ready(rfile->output))
1286 mask |= POLLOUT | POLLWRNORM;
1293 #ifdef CONFIG_COMPAT
1294 #include "rawmidi_compat.c"
1296 #define snd_rawmidi_ioctl_compat NULL
1303 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1304 struct snd_info_buffer *buffer)
1306 struct snd_rawmidi *rmidi;
1307 struct snd_rawmidi_substream *substream;
1308 struct snd_rawmidi_runtime *runtime;
1310 rmidi = entry->private_data;
1311 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1312 mutex_lock(&rmidi->open_mutex);
1313 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1314 list_for_each_entry(substream,
1315 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1319 " Tx bytes : %lu\n",
1321 (unsigned long) substream->bytes);
1322 if (substream->opened) {
1323 runtime = substream->runtime;
1326 " Buffer size : %lu\n"
1328 runtime->oss ? "OSS compatible" : "native",
1329 (unsigned long) runtime->buffer_size,
1330 (unsigned long) runtime->avail);
1334 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1335 list_for_each_entry(substream,
1336 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1340 " Rx bytes : %lu\n",
1342 (unsigned long) substream->bytes);
1343 if (substream->opened) {
1344 runtime = substream->runtime;
1346 " Buffer size : %lu\n"
1348 " Overruns : %lu\n",
1349 (unsigned long) runtime->buffer_size,
1350 (unsigned long) runtime->avail,
1351 (unsigned long) runtime->xruns);
1355 mutex_unlock(&rmidi->open_mutex);
1359 * Register functions
1362 static struct file_operations snd_rawmidi_f_ops =
1364 .owner = THIS_MODULE,
1365 .read = snd_rawmidi_read,
1366 .write = snd_rawmidi_write,
1367 .open = snd_rawmidi_open,
1368 .release = snd_rawmidi_release,
1369 .poll = snd_rawmidi_poll,
1370 .unlocked_ioctl = snd_rawmidi_ioctl,
1371 .compat_ioctl = snd_rawmidi_ioctl_compat,
1374 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1375 struct snd_rawmidi_str *stream,
1379 struct snd_rawmidi_substream *substream;
1382 for (idx = 0; idx < count; idx++) {
1383 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1384 if (substream == NULL) {
1385 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1388 substream->stream = direction;
1389 substream->number = idx;
1390 substream->rmidi = rmidi;
1391 substream->pstr = stream;
1392 list_add_tail(&substream->list, &stream->substreams);
1393 stream->substream_count++;
1399 * snd_rawmidi_new - create a rawmidi instance
1400 * @card: the card instance
1401 * @id: the id string
1402 * @device: the device index
1403 * @output_count: the number of output streams
1404 * @input_count: the number of input streams
1405 * @rrawmidi: the pointer to store the new rawmidi instance
1407 * Creates a new rawmidi instance.
1408 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1410 * Returns zero if successful, or a negative error code on failure.
1412 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1413 int output_count, int input_count,
1414 struct snd_rawmidi ** rrawmidi)
1416 struct snd_rawmidi *rmidi;
1418 static struct snd_device_ops ops = {
1419 .dev_free = snd_rawmidi_dev_free,
1420 .dev_register = snd_rawmidi_dev_register,
1421 .dev_disconnect = snd_rawmidi_dev_disconnect,
1424 snd_assert(rrawmidi != NULL, return -EINVAL);
1426 snd_assert(card != NULL, return -ENXIO);
1427 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1428 if (rmidi == NULL) {
1429 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1433 rmidi->device = device;
1434 mutex_init(&rmidi->open_mutex);
1435 init_waitqueue_head(&rmidi->open_wait);
1436 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1437 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1440 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1441 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1442 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1443 SNDRV_RAWMIDI_STREAM_INPUT,
1444 input_count)) < 0) {
1445 snd_rawmidi_free(rmidi);
1448 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1449 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1450 SNDRV_RAWMIDI_STREAM_OUTPUT,
1451 output_count)) < 0) {
1452 snd_rawmidi_free(rmidi);
1455 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1456 snd_rawmidi_free(rmidi);
1463 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1465 struct snd_rawmidi_substream *substream;
1467 while (!list_empty(&stream->substreams)) {
1468 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1469 list_del(&substream->list);
1474 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1476 snd_assert(rmidi != NULL, return -ENXIO);
1478 snd_info_free_entry(rmidi->proc_entry);
1479 rmidi->proc_entry = NULL;
1480 mutex_lock(®ister_mutex);
1481 if (rmidi->ops && rmidi->ops->dev_unregister)
1482 rmidi->ops->dev_unregister(rmidi);
1483 mutex_unlock(®ister_mutex);
1485 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1486 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1487 if (rmidi->private_free)
1488 rmidi->private_free(rmidi);
1493 static int snd_rawmidi_dev_free(struct snd_device *device)
1495 struct snd_rawmidi *rmidi = device->device_data;
1496 return snd_rawmidi_free(rmidi);
1499 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1500 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1502 struct snd_rawmidi *rmidi = device->private_data;
1503 rmidi->seq_dev = NULL;
1507 static int snd_rawmidi_dev_register(struct snd_device *device)
1510 struct snd_info_entry *entry;
1512 struct snd_rawmidi *rmidi = device->device_data;
1514 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1516 mutex_lock(®ister_mutex);
1517 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1518 mutex_unlock(®ister_mutex);
1521 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1522 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1523 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1524 rmidi->card, rmidi->device,
1525 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1526 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1527 list_del(&rmidi->list);
1528 mutex_unlock(®ister_mutex);
1531 if (rmidi->ops && rmidi->ops->dev_register &&
1532 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1533 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1534 list_del(&rmidi->list);
1535 mutex_unlock(®ister_mutex);
1538 #ifdef CONFIG_SND_OSSEMUL
1540 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1541 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1542 rmidi->card, 0, &snd_rawmidi_f_ops,
1544 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1547 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1548 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1552 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1553 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1554 rmidi->card, 1, &snd_rawmidi_f_ops,
1556 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1561 #endif /* CONFIG_SND_OSSEMUL */
1562 mutex_unlock(®ister_mutex);
1563 sprintf(name, "midi%d", rmidi->device);
1564 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1566 entry->private_data = rmidi;
1567 entry->c.text.read = snd_rawmidi_proc_info_read;
1568 if (snd_info_register(entry) < 0) {
1569 snd_info_free_entry(entry);
1573 rmidi->proc_entry = entry;
1574 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1575 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1576 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1577 rmidi->seq_dev->private_data = rmidi;
1578 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1579 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1580 snd_device_register(rmidi->card, rmidi->seq_dev);
1587 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1589 struct snd_rawmidi *rmidi = device->device_data;
1591 mutex_lock(®ister_mutex);
1592 list_del_init(&rmidi->list);
1593 #ifdef CONFIG_SND_OSSEMUL
1594 if (rmidi->ossreg) {
1595 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1596 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1597 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1598 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1601 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1602 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1605 #endif /* CONFIG_SND_OSSEMUL */
1606 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1607 mutex_unlock(®ister_mutex);
1612 * snd_rawmidi_set_ops - set the rawmidi operators
1613 * @rmidi: the rawmidi instance
1614 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1615 * @ops: the operator table
1617 * Sets the rawmidi operators for the given stream direction.
1619 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1620 struct snd_rawmidi_ops *ops)
1622 struct snd_rawmidi_substream *substream;
1624 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1625 substream->ops = ops;
1632 static int __init alsa_rawmidi_init(void)
1635 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1636 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1637 #ifdef CONFIG_SND_OSSEMUL
1639 /* check device map table */
1640 for (i = 0; i < SNDRV_CARDS; i++) {
1641 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1642 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1645 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1646 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1651 #endif /* CONFIG_SND_OSSEMUL */
1655 static void __exit alsa_rawmidi_exit(void)
1657 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1658 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1661 module_init(alsa_rawmidi_init)
1662 module_exit(alsa_rawmidi_exit)
1664 EXPORT_SYMBOL(snd_rawmidi_output_params);
1665 EXPORT_SYMBOL(snd_rawmidi_input_params);
1666 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1667 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1668 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1669 EXPORT_SYMBOL(snd_rawmidi_receive);
1670 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1671 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1672 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1673 EXPORT_SYMBOL(snd_rawmidi_transmit);
1674 EXPORT_SYMBOL(snd_rawmidi_new);
1675 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1676 EXPORT_SYMBOL(snd_rawmidi_info_select);
1677 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1678 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1679 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1680 EXPORT_SYMBOL(snd_rawmidi_kernel_write);