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/module.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_work(struct work_struct *work)
97 struct snd_rawmidi_runtime *runtime =
98 container_of(work, struct snd_rawmidi_runtime, event_work);
100 runtime->event(runtime->substream);
103 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
105 struct snd_rawmidi_runtime *runtime;
107 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
109 runtime->substream = substream;
110 spin_lock_init(&runtime->lock);
111 init_waitqueue_head(&runtime->sleep);
112 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
113 runtime->event = NULL;
114 runtime->buffer_size = PAGE_SIZE;
115 runtime->avail_min = 1;
116 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
119 runtime->avail = runtime->buffer_size;
120 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
124 runtime->appl_ptr = runtime->hw_ptr = 0;
125 substream->runtime = runtime;
129 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
131 struct snd_rawmidi_runtime *runtime = substream->runtime;
133 kfree(runtime->buffer);
135 substream->runtime = NULL;
139 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
141 if (!substream->opened)
143 substream->ops->trigger(substream, up);
146 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
148 if (!substream->opened)
150 substream->ops->trigger(substream, up);
152 cancel_work_sync(&substream->runtime->event_work);
155 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
158 struct snd_rawmidi_runtime *runtime = substream->runtime;
160 snd_rawmidi_output_trigger(substream, 0);
162 spin_lock_irqsave(&runtime->lock, flags);
163 runtime->appl_ptr = runtime->hw_ptr = 0;
164 runtime->avail = runtime->buffer_size;
165 spin_unlock_irqrestore(&runtime->lock, flags);
169 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
173 struct snd_rawmidi_runtime *runtime = substream->runtime;
177 timeout = wait_event_interruptible_timeout(runtime->sleep,
178 (runtime->avail >= runtime->buffer_size),
180 if (signal_pending(current))
182 if (runtime->avail < runtime->buffer_size && !timeout) {
183 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
187 if (err != -ERESTARTSYS) {
188 /* we need wait a while to make sure that Tx FIFOs are empty */
189 if (substream->ops->drain)
190 substream->ops->drain(substream);
193 snd_rawmidi_drop_output(substream);
198 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
201 struct snd_rawmidi_runtime *runtime = substream->runtime;
203 snd_rawmidi_input_trigger(substream, 0);
205 spin_lock_irqsave(&runtime->lock, flags);
206 runtime->appl_ptr = runtime->hw_ptr = 0;
208 spin_unlock_irqrestore(&runtime->lock, flags);
212 /* look for an available substream for the given stream direction;
213 * if a specific subdevice is given, try to assign it
215 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
216 int stream, int mode,
217 struct snd_rawmidi_substream **sub_ret)
219 struct snd_rawmidi_substream *substream;
220 struct snd_rawmidi_str *s = &rmidi->streams[stream];
221 static unsigned int info_flags[2] = {
222 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
223 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
226 if (!(rmidi->info_flags & info_flags[stream]))
228 if (subdevice >= 0 && subdevice >= s->substream_count)
231 list_for_each_entry(substream, &s->substreams, list) {
232 if (substream->opened) {
233 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
234 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
238 if (subdevice < 0 || subdevice == substream->number) {
239 *sub_ret = substream;
246 /* open and do ref-counting for the given substream */
247 static int open_substream(struct snd_rawmidi *rmidi,
248 struct snd_rawmidi_substream *substream,
253 if (substream->use_count == 0) {
254 err = snd_rawmidi_runtime_create(substream);
257 err = substream->ops->open(substream);
259 snd_rawmidi_runtime_free(substream);
262 substream->opened = 1;
263 substream->active_sensing = 0;
264 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
265 substream->append = 1;
266 substream->pid = get_pid(task_pid(current));
267 rmidi->streams[substream->stream].substream_opened++;
269 substream->use_count++;
273 static void close_substream(struct snd_rawmidi *rmidi,
274 struct snd_rawmidi_substream *substream,
277 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
278 struct snd_rawmidi_file *rfile)
280 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
283 rfile->input = rfile->output = NULL;
284 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
285 err = assign_substream(rmidi, subdevice,
286 SNDRV_RAWMIDI_STREAM_INPUT,
291 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
292 err = assign_substream(rmidi, subdevice,
293 SNDRV_RAWMIDI_STREAM_OUTPUT,
300 err = open_substream(rmidi, sinput, mode);
305 err = open_substream(rmidi, soutput, mode);
308 close_substream(rmidi, sinput, 0);
313 rfile->rmidi = rmidi;
314 rfile->input = sinput;
315 rfile->output = soutput;
319 /* called from sound/core/seq/seq_midi.c */
320 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
321 int mode, struct snd_rawmidi_file * rfile)
323 struct snd_rawmidi *rmidi;
326 if (snd_BUG_ON(!rfile))
329 mutex_lock(®ister_mutex);
330 rmidi = snd_rawmidi_search(card, device);
332 mutex_unlock(®ister_mutex);
335 if (!try_module_get(rmidi->card->module)) {
336 mutex_unlock(®ister_mutex);
339 mutex_unlock(®ister_mutex);
341 mutex_lock(&rmidi->open_mutex);
342 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
343 mutex_unlock(&rmidi->open_mutex);
345 module_put(rmidi->card->module);
349 static int snd_rawmidi_open(struct inode *inode, struct file *file)
351 int maj = imajor(inode);
352 struct snd_card *card;
354 unsigned short fflags;
356 struct snd_rawmidi *rmidi;
357 struct snd_rawmidi_file *rawmidi_file = NULL;
359 struct snd_ctl_file *kctl;
361 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
362 return -EINVAL; /* invalid combination */
364 err = nonseekable_open(inode, file);
368 if (maj == snd_major) {
369 rmidi = snd_lookup_minor_data(iminor(inode),
370 SNDRV_DEVICE_TYPE_RAWMIDI);
371 #ifdef CONFIG_SND_OSSEMUL
372 } else if (maj == SOUND_MAJOR) {
373 rmidi = snd_lookup_oss_minor_data(iminor(inode),
374 SNDRV_OSS_DEVICE_TYPE_MIDI);
382 if (!try_module_get(rmidi->card->module)) {
383 snd_card_unref(rmidi->card);
387 mutex_lock(&rmidi->open_mutex);
389 err = snd_card_file_add(card, file);
392 fflags = snd_rawmidi_file_flags(file);
393 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
394 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
395 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
396 if (rawmidi_file == NULL) {
400 init_waitqueue_entry(&wait, current);
401 add_wait_queue(&rmidi->open_wait, &wait);
404 read_lock(&card->ctl_files_rwlock);
405 list_for_each_entry(kctl, &card->ctl_files, list) {
406 if (kctl->pid == task_pid(current)) {
407 subdevice = kctl->prefer_rawmidi_subdevice;
412 read_unlock(&card->ctl_files_rwlock);
413 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
416 if (err == -EAGAIN) {
417 if (file->f_flags & O_NONBLOCK) {
423 set_current_state(TASK_INTERRUPTIBLE);
424 mutex_unlock(&rmidi->open_mutex);
426 mutex_lock(&rmidi->open_mutex);
427 if (rmidi->card->shutdown) {
431 if (signal_pending(current)) {
436 remove_wait_queue(&rmidi->open_wait, &wait);
441 #ifdef CONFIG_SND_OSSEMUL
442 if (rawmidi_file->input && rawmidi_file->input->runtime)
443 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
444 if (rawmidi_file->output && rawmidi_file->output->runtime)
445 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
447 file->private_data = rawmidi_file;
448 mutex_unlock(&rmidi->open_mutex);
449 snd_card_unref(rmidi->card);
453 snd_card_file_remove(card, file);
455 mutex_unlock(&rmidi->open_mutex);
456 module_put(rmidi->card->module);
457 snd_card_unref(rmidi->card);
461 static void close_substream(struct snd_rawmidi *rmidi,
462 struct snd_rawmidi_substream *substream,
465 if (--substream->use_count)
469 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
470 snd_rawmidi_input_trigger(substream, 0);
472 if (substream->active_sensing) {
473 unsigned char buf = 0xfe;
474 /* sending single active sensing message
475 * to shut the device up
477 snd_rawmidi_kernel_write(substream, &buf, 1);
479 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
480 snd_rawmidi_output_trigger(substream, 0);
483 substream->ops->close(substream);
484 if (substream->runtime->private_free)
485 substream->runtime->private_free(substream);
486 snd_rawmidi_runtime_free(substream);
487 substream->opened = 0;
488 substream->append = 0;
489 put_pid(substream->pid);
490 substream->pid = NULL;
491 rmidi->streams[substream->stream].substream_opened--;
494 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
496 struct snd_rawmidi *rmidi;
498 rmidi = rfile->rmidi;
499 mutex_lock(&rmidi->open_mutex);
501 close_substream(rmidi, rfile->input, 1);
505 close_substream(rmidi, rfile->output, 1);
506 rfile->output = NULL;
509 mutex_unlock(&rmidi->open_mutex);
510 wake_up(&rmidi->open_wait);
513 /* called from sound/core/seq/seq_midi.c */
514 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
516 struct snd_rawmidi *rmidi;
518 if (snd_BUG_ON(!rfile))
521 rmidi = rfile->rmidi;
522 rawmidi_release_priv(rfile);
523 module_put(rmidi->card->module);
527 static int snd_rawmidi_release(struct inode *inode, struct file *file)
529 struct snd_rawmidi_file *rfile;
530 struct snd_rawmidi *rmidi;
531 struct module *module;
533 rfile = file->private_data;
534 rmidi = rfile->rmidi;
535 rawmidi_release_priv(rfile);
537 module = rmidi->card->module;
538 snd_card_file_remove(rmidi->card, file);
543 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
544 struct snd_rawmidi_info *info)
546 struct snd_rawmidi *rmidi;
548 if (substream == NULL)
550 rmidi = substream->rmidi;
551 memset(info, 0, sizeof(*info));
552 info->card = rmidi->card->number;
553 info->device = rmidi->device;
554 info->subdevice = substream->number;
555 info->stream = substream->stream;
556 info->flags = rmidi->info_flags;
557 strcpy(info->id, rmidi->id);
558 strcpy(info->name, rmidi->name);
559 strcpy(info->subname, substream->name);
560 info->subdevices_count = substream->pstr->substream_count;
561 info->subdevices_avail = (substream->pstr->substream_count -
562 substream->pstr->substream_opened);
566 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
567 struct snd_rawmidi_info __user * _info)
569 struct snd_rawmidi_info info;
571 if ((err = snd_rawmidi_info(substream, &info)) < 0)
573 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
578 static int __snd_rawmidi_info_select(struct snd_card *card,
579 struct snd_rawmidi_info *info)
581 struct snd_rawmidi *rmidi;
582 struct snd_rawmidi_str *pstr;
583 struct snd_rawmidi_substream *substream;
585 rmidi = snd_rawmidi_search(card, info->device);
588 if (info->stream < 0 || info->stream > 1)
590 pstr = &rmidi->streams[info->stream];
591 if (pstr->substream_count == 0)
593 if (info->subdevice >= pstr->substream_count)
595 list_for_each_entry(substream, &pstr->substreams, list) {
596 if ((unsigned int)substream->number == info->subdevice)
597 return snd_rawmidi_info(substream, info);
602 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
606 mutex_lock(®ister_mutex);
607 ret = __snd_rawmidi_info_select(card, info);
608 mutex_unlock(®ister_mutex);
612 static int snd_rawmidi_info_select_user(struct snd_card *card,
613 struct snd_rawmidi_info __user *_info)
616 struct snd_rawmidi_info info;
617 if (get_user(info.device, &_info->device))
619 if (get_user(info.stream, &_info->stream))
621 if (get_user(info.subdevice, &_info->subdevice))
623 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
625 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
630 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
631 struct snd_rawmidi_params * params)
634 struct snd_rawmidi_runtime *runtime = substream->runtime;
636 if (substream->append && substream->use_count > 1)
638 snd_rawmidi_drain_output(substream);
639 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
642 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
645 if (params->buffer_size != runtime->buffer_size) {
646 newbuf = krealloc(runtime->buffer, params->buffer_size,
650 runtime->buffer = newbuf;
651 runtime->buffer_size = params->buffer_size;
652 runtime->avail = runtime->buffer_size;
654 runtime->avail_min = params->avail_min;
655 substream->active_sensing = !params->no_active_sensing;
659 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
660 struct snd_rawmidi_params * params)
663 struct snd_rawmidi_runtime *runtime = substream->runtime;
665 snd_rawmidi_drain_input(substream);
666 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
669 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
672 if (params->buffer_size != runtime->buffer_size) {
673 newbuf = krealloc(runtime->buffer, params->buffer_size,
677 runtime->buffer = newbuf;
678 runtime->buffer_size = params->buffer_size;
680 runtime->avail_min = params->avail_min;
684 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
685 struct snd_rawmidi_status * status)
687 struct snd_rawmidi_runtime *runtime = substream->runtime;
689 memset(status, 0, sizeof(*status));
690 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
691 spin_lock_irq(&runtime->lock);
692 status->avail = runtime->avail;
693 spin_unlock_irq(&runtime->lock);
697 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
698 struct snd_rawmidi_status * status)
700 struct snd_rawmidi_runtime *runtime = substream->runtime;
702 memset(status, 0, sizeof(*status));
703 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
704 spin_lock_irq(&runtime->lock);
705 status->avail = runtime->avail;
706 status->xruns = runtime->xruns;
708 spin_unlock_irq(&runtime->lock);
712 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
714 struct snd_rawmidi_file *rfile;
715 void __user *argp = (void __user *)arg;
717 rfile = file->private_data;
718 if (((cmd >> 8) & 0xff) != 'W')
721 case SNDRV_RAWMIDI_IOCTL_PVERSION:
722 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
723 case SNDRV_RAWMIDI_IOCTL_INFO:
726 struct snd_rawmidi_info __user *info = argp;
727 if (get_user(stream, &info->stream))
730 case SNDRV_RAWMIDI_STREAM_INPUT:
731 return snd_rawmidi_info_user(rfile->input, info);
732 case SNDRV_RAWMIDI_STREAM_OUTPUT:
733 return snd_rawmidi_info_user(rfile->output, info);
738 case SNDRV_RAWMIDI_IOCTL_PARAMS:
740 struct snd_rawmidi_params params;
741 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
743 switch (params.stream) {
744 case SNDRV_RAWMIDI_STREAM_OUTPUT:
745 if (rfile->output == NULL)
747 return snd_rawmidi_output_params(rfile->output, ¶ms);
748 case SNDRV_RAWMIDI_STREAM_INPUT:
749 if (rfile->input == NULL)
751 return snd_rawmidi_input_params(rfile->input, ¶ms);
756 case SNDRV_RAWMIDI_IOCTL_STATUS:
759 struct snd_rawmidi_status status;
760 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
762 switch (status.stream) {
763 case SNDRV_RAWMIDI_STREAM_OUTPUT:
764 if (rfile->output == NULL)
766 err = snd_rawmidi_output_status(rfile->output, &status);
768 case SNDRV_RAWMIDI_STREAM_INPUT:
769 if (rfile->input == NULL)
771 err = snd_rawmidi_input_status(rfile->input, &status);
778 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
782 case SNDRV_RAWMIDI_IOCTL_DROP:
785 if (get_user(val, (int __user *) argp))
788 case SNDRV_RAWMIDI_STREAM_OUTPUT:
789 if (rfile->output == NULL)
791 return snd_rawmidi_drop_output(rfile->output);
796 case SNDRV_RAWMIDI_IOCTL_DRAIN:
799 if (get_user(val, (int __user *) argp))
802 case SNDRV_RAWMIDI_STREAM_OUTPUT:
803 if (rfile->output == NULL)
805 return snd_rawmidi_drain_output(rfile->output);
806 case SNDRV_RAWMIDI_STREAM_INPUT:
807 if (rfile->input == NULL)
809 return snd_rawmidi_drain_input(rfile->input);
814 #ifdef CONFIG_SND_DEBUG
816 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
822 static int snd_rawmidi_control_ioctl(struct snd_card *card,
823 struct snd_ctl_file *control,
827 void __user *argp = (void __user *)arg;
830 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
834 if (get_user(device, (int __user *)argp))
836 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
837 device = SNDRV_RAWMIDI_DEVICES - 1;
838 mutex_lock(®ister_mutex);
839 device = device < 0 ? 0 : device + 1;
840 while (device < SNDRV_RAWMIDI_DEVICES) {
841 if (snd_rawmidi_search(card, device))
845 if (device == SNDRV_RAWMIDI_DEVICES)
847 mutex_unlock(®ister_mutex);
848 if (put_user(device, (int __user *)argp))
852 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
856 if (get_user(val, (int __user *)argp))
858 control->prefer_rawmidi_subdevice = val;
861 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
862 return snd_rawmidi_info_select_user(card, argp);
868 * snd_rawmidi_receive - receive the input data from the device
869 * @substream: the rawmidi substream
870 * @buffer: the buffer pointer
871 * @count: the data size to read
873 * Reads the data from the internal buffer.
875 * Returns the size of read data, or a negative error code on failure.
877 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
878 const unsigned char *buffer, int count)
881 int result = 0, count1;
882 struct snd_rawmidi_runtime *runtime = substream->runtime;
884 if (!substream->opened)
886 if (runtime->buffer == NULL) {
887 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
890 spin_lock_irqsave(&runtime->lock, flags);
891 if (count == 1) { /* special case, faster code */
893 if (runtime->avail < runtime->buffer_size) {
894 runtime->buffer[runtime->hw_ptr++] = buffer[0];
895 runtime->hw_ptr %= runtime->buffer_size;
902 substream->bytes += count;
903 count1 = runtime->buffer_size - runtime->hw_ptr;
906 if (count1 > (int)(runtime->buffer_size - runtime->avail))
907 count1 = runtime->buffer_size - runtime->avail;
908 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
909 runtime->hw_ptr += count1;
910 runtime->hw_ptr %= runtime->buffer_size;
911 runtime->avail += count1;
917 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
918 count1 = runtime->buffer_size - runtime->avail;
919 runtime->xruns += count - count1;
922 memcpy(runtime->buffer, buffer, count1);
923 runtime->hw_ptr = count1;
924 runtime->avail += count1;
931 schedule_work(&runtime->event_work);
932 else if (snd_rawmidi_ready(substream))
933 wake_up(&runtime->sleep);
935 spin_unlock_irqrestore(&runtime->lock, flags);
939 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
940 unsigned char __user *userbuf,
941 unsigned char *kernelbuf, long count)
944 long result = 0, count1;
945 struct snd_rawmidi_runtime *runtime = substream->runtime;
946 unsigned long appl_ptr;
948 spin_lock_irqsave(&runtime->lock, flags);
949 while (count > 0 && runtime->avail) {
950 count1 = runtime->buffer_size - runtime->appl_ptr;
953 if (count1 > (int)runtime->avail)
954 count1 = runtime->avail;
956 /* update runtime->appl_ptr before unlocking for userbuf */
957 appl_ptr = runtime->appl_ptr;
958 runtime->appl_ptr += count1;
959 runtime->appl_ptr %= runtime->buffer_size;
960 runtime->avail -= count1;
963 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
965 spin_unlock_irqrestore(&runtime->lock, flags);
966 if (copy_to_user(userbuf + result,
967 runtime->buffer + appl_ptr, count1)) {
968 return result > 0 ? result : -EFAULT;
970 spin_lock_irqsave(&runtime->lock, flags);
975 spin_unlock_irqrestore(&runtime->lock, flags);
979 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
980 unsigned char *buf, long count)
982 snd_rawmidi_input_trigger(substream, 1);
983 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
986 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
991 struct snd_rawmidi_file *rfile;
992 struct snd_rawmidi_substream *substream;
993 struct snd_rawmidi_runtime *runtime;
995 rfile = file->private_data;
996 substream = rfile->input;
997 if (substream == NULL)
999 runtime = substream->runtime;
1000 snd_rawmidi_input_trigger(substream, 1);
1003 spin_lock_irq(&runtime->lock);
1004 while (!snd_rawmidi_ready(substream)) {
1006 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1007 spin_unlock_irq(&runtime->lock);
1008 return result > 0 ? result : -EAGAIN;
1010 init_waitqueue_entry(&wait, current);
1011 add_wait_queue(&runtime->sleep, &wait);
1012 set_current_state(TASK_INTERRUPTIBLE);
1013 spin_unlock_irq(&runtime->lock);
1015 remove_wait_queue(&runtime->sleep, &wait);
1016 if (rfile->rmidi->card->shutdown)
1018 if (signal_pending(current))
1019 return result > 0 ? result : -ERESTARTSYS;
1020 if (!runtime->avail)
1021 return result > 0 ? result : -EIO;
1022 spin_lock_irq(&runtime->lock);
1024 spin_unlock_irq(&runtime->lock);
1025 count1 = snd_rawmidi_kernel_read1(substream,
1026 (unsigned char __user *)buf,
1030 return result > 0 ? result : count1;
1039 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1040 * @substream: the rawmidi substream
1042 * Returns 1 if the internal output buffer is empty, 0 if not.
1044 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1046 struct snd_rawmidi_runtime *runtime = substream->runtime;
1048 unsigned long flags;
1050 if (runtime->buffer == NULL) {
1051 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1054 spin_lock_irqsave(&runtime->lock, flags);
1055 result = runtime->avail >= runtime->buffer_size;
1056 spin_unlock_irqrestore(&runtime->lock, flags);
1061 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1062 * @substream: the rawmidi substream
1063 * @buffer: the buffer pointer
1064 * @count: data size to transfer
1066 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1068 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1069 unsigned char *buffer, int count)
1072 struct snd_rawmidi_runtime *runtime = substream->runtime;
1074 if (runtime->buffer == NULL) {
1075 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1079 if (runtime->avail >= runtime->buffer_size) {
1080 /* warning: lowlevel layer MUST trigger down the hardware */
1083 if (count == 1) { /* special case, faster code */
1084 *buffer = runtime->buffer[runtime->hw_ptr];
1087 count1 = runtime->buffer_size - runtime->hw_ptr;
1090 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1091 count1 = runtime->buffer_size - runtime->avail;
1092 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1096 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1097 count = runtime->buffer_size - runtime->avail - count1;
1098 memcpy(buffer + count1, runtime->buffer, count);
1105 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1108 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1109 * @substream: the rawmidi substream
1110 * @buffer: the buffer pointer
1111 * @count: data size to transfer
1113 * Copies data from the internal output buffer to the given buffer.
1115 * Call this in the interrupt handler when the midi output is ready,
1116 * and call snd_rawmidi_transmit_ack() after the transmission is
1119 * Return: The size of copied data, or a negative error code on failure.
1121 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1122 unsigned char *buffer, int count)
1124 struct snd_rawmidi_runtime *runtime = substream->runtime;
1126 unsigned long flags;
1128 spin_lock_irqsave(&runtime->lock, flags);
1129 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1130 spin_unlock_irqrestore(&runtime->lock, flags);
1135 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1136 * @substream: the rawmidi substream
1137 * @count: the tranferred count
1139 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1141 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1143 struct snd_rawmidi_runtime *runtime = substream->runtime;
1145 if (runtime->buffer == NULL) {
1146 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1149 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1150 runtime->hw_ptr += count;
1151 runtime->hw_ptr %= runtime->buffer_size;
1152 runtime->avail += count;
1153 substream->bytes += count;
1155 if (runtime->drain || snd_rawmidi_ready(substream))
1156 wake_up(&runtime->sleep);
1160 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1163 * snd_rawmidi_transmit_ack - acknowledge the transmission
1164 * @substream: the rawmidi substream
1165 * @count: the transferred count
1167 * Advances the hardware pointer for the internal output buffer with
1168 * the given size and updates the condition.
1169 * Call after the transmission is finished.
1171 * Return: The advanced size if successful, or a negative error code on failure.
1173 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1175 struct snd_rawmidi_runtime *runtime = substream->runtime;
1177 unsigned long flags;
1179 spin_lock_irqsave(&runtime->lock, flags);
1180 result = __snd_rawmidi_transmit_ack(substream, count);
1181 spin_unlock_irqrestore(&runtime->lock, flags);
1186 * snd_rawmidi_transmit - copy from the buffer to the device
1187 * @substream: the rawmidi substream
1188 * @buffer: the buffer pointer
1189 * @count: the data size to transfer
1191 * Copies data from the buffer to the device and advances the pointer.
1193 * Returns the copied size if successful, or a negative error code on failure.
1195 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1196 unsigned char *buffer, int count)
1198 struct snd_rawmidi_runtime *runtime = substream->runtime;
1200 unsigned long flags;
1202 spin_lock_irqsave(&runtime->lock, flags);
1203 if (!substream->opened)
1206 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1210 result = __snd_rawmidi_transmit_ack(substream, count);
1212 spin_unlock_irqrestore(&runtime->lock, flags);
1216 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1217 const unsigned char __user *userbuf,
1218 const unsigned char *kernelbuf,
1221 unsigned long flags;
1222 long count1, result;
1223 struct snd_rawmidi_runtime *runtime = substream->runtime;
1224 unsigned long appl_ptr;
1226 if (!kernelbuf && !userbuf)
1228 if (snd_BUG_ON(!runtime->buffer))
1232 spin_lock_irqsave(&runtime->lock, flags);
1233 if (substream->append) {
1234 if ((long)runtime->avail < count) {
1235 spin_unlock_irqrestore(&runtime->lock, flags);
1239 while (count > 0 && runtime->avail > 0) {
1240 count1 = runtime->buffer_size - runtime->appl_ptr;
1243 if (count1 > (long)runtime->avail)
1244 count1 = runtime->avail;
1246 /* update runtime->appl_ptr before unlocking for userbuf */
1247 appl_ptr = runtime->appl_ptr;
1248 runtime->appl_ptr += count1;
1249 runtime->appl_ptr %= runtime->buffer_size;
1250 runtime->avail -= count1;
1253 memcpy(runtime->buffer + appl_ptr,
1254 kernelbuf + result, count1);
1256 spin_unlock_irqrestore(&runtime->lock, flags);
1257 if (copy_from_user(runtime->buffer + appl_ptr,
1258 userbuf + result, count1)) {
1259 spin_lock_irqsave(&runtime->lock, flags);
1260 result = result > 0 ? result : -EFAULT;
1263 spin_lock_irqsave(&runtime->lock, flags);
1269 count1 = runtime->avail < runtime->buffer_size;
1270 spin_unlock_irqrestore(&runtime->lock, flags);
1272 snd_rawmidi_output_trigger(substream, 1);
1276 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1277 const unsigned char *buf, long count)
1279 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1282 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1283 size_t count, loff_t *offset)
1285 long result, timeout;
1287 struct snd_rawmidi_file *rfile;
1288 struct snd_rawmidi_runtime *runtime;
1289 struct snd_rawmidi_substream *substream;
1291 rfile = file->private_data;
1292 substream = rfile->output;
1293 runtime = substream->runtime;
1294 /* we cannot put an atomic message to our buffer */
1295 if (substream->append && count > runtime->buffer_size)
1299 spin_lock_irq(&runtime->lock);
1300 while (!snd_rawmidi_ready_append(substream, count)) {
1302 if (file->f_flags & O_NONBLOCK) {
1303 spin_unlock_irq(&runtime->lock);
1304 return result > 0 ? result : -EAGAIN;
1306 init_waitqueue_entry(&wait, current);
1307 add_wait_queue(&runtime->sleep, &wait);
1308 set_current_state(TASK_INTERRUPTIBLE);
1309 spin_unlock_irq(&runtime->lock);
1310 timeout = schedule_timeout(30 * HZ);
1311 remove_wait_queue(&runtime->sleep, &wait);
1312 if (rfile->rmidi->card->shutdown)
1314 if (signal_pending(current))
1315 return result > 0 ? result : -ERESTARTSYS;
1316 if (!runtime->avail && !timeout)
1317 return result > 0 ? result : -EIO;
1318 spin_lock_irq(&runtime->lock);
1320 spin_unlock_irq(&runtime->lock);
1321 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1323 return result > 0 ? result : count1;
1326 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1330 if (file->f_flags & O_DSYNC) {
1331 spin_lock_irq(&runtime->lock);
1332 while (runtime->avail != runtime->buffer_size) {
1334 unsigned int last_avail = runtime->avail;
1335 init_waitqueue_entry(&wait, current);
1336 add_wait_queue(&runtime->sleep, &wait);
1337 set_current_state(TASK_INTERRUPTIBLE);
1338 spin_unlock_irq(&runtime->lock);
1339 timeout = schedule_timeout(30 * HZ);
1340 remove_wait_queue(&runtime->sleep, &wait);
1341 if (signal_pending(current))
1342 return result > 0 ? result : -ERESTARTSYS;
1343 if (runtime->avail == last_avail && !timeout)
1344 return result > 0 ? result : -EIO;
1345 spin_lock_irq(&runtime->lock);
1347 spin_unlock_irq(&runtime->lock);
1352 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1354 struct snd_rawmidi_file *rfile;
1355 struct snd_rawmidi_runtime *runtime;
1358 rfile = file->private_data;
1359 if (rfile->input != NULL) {
1360 runtime = rfile->input->runtime;
1361 snd_rawmidi_input_trigger(rfile->input, 1);
1362 poll_wait(file, &runtime->sleep, wait);
1364 if (rfile->output != NULL) {
1365 runtime = rfile->output->runtime;
1366 poll_wait(file, &runtime->sleep, wait);
1369 if (rfile->input != NULL) {
1370 if (snd_rawmidi_ready(rfile->input))
1371 mask |= POLLIN | POLLRDNORM;
1373 if (rfile->output != NULL) {
1374 if (snd_rawmidi_ready(rfile->output))
1375 mask |= POLLOUT | POLLWRNORM;
1382 #ifdef CONFIG_COMPAT
1383 #include "rawmidi_compat.c"
1385 #define snd_rawmidi_ioctl_compat NULL
1392 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1393 struct snd_info_buffer *buffer)
1395 struct snd_rawmidi *rmidi;
1396 struct snd_rawmidi_substream *substream;
1397 struct snd_rawmidi_runtime *runtime;
1399 rmidi = entry->private_data;
1400 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1401 mutex_lock(&rmidi->open_mutex);
1402 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1403 list_for_each_entry(substream,
1404 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1408 " Tx bytes : %lu\n",
1410 (unsigned long) substream->bytes);
1411 if (substream->opened) {
1413 " Owner PID : %d\n",
1414 pid_vnr(substream->pid));
1415 runtime = substream->runtime;
1418 " Buffer size : %lu\n"
1420 runtime->oss ? "OSS compatible" : "native",
1421 (unsigned long) runtime->buffer_size,
1422 (unsigned long) runtime->avail);
1426 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1427 list_for_each_entry(substream,
1428 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1432 " Rx bytes : %lu\n",
1434 (unsigned long) substream->bytes);
1435 if (substream->opened) {
1437 " Owner PID : %d\n",
1438 pid_vnr(substream->pid));
1439 runtime = substream->runtime;
1441 " Buffer size : %lu\n"
1443 " Overruns : %lu\n",
1444 (unsigned long) runtime->buffer_size,
1445 (unsigned long) runtime->avail,
1446 (unsigned long) runtime->xruns);
1450 mutex_unlock(&rmidi->open_mutex);
1454 * Register functions
1457 static const struct file_operations snd_rawmidi_f_ops =
1459 .owner = THIS_MODULE,
1460 .read = snd_rawmidi_read,
1461 .write = snd_rawmidi_write,
1462 .open = snd_rawmidi_open,
1463 .release = snd_rawmidi_release,
1464 .llseek = no_llseek,
1465 .poll = snd_rawmidi_poll,
1466 .unlocked_ioctl = snd_rawmidi_ioctl,
1467 .compat_ioctl = snd_rawmidi_ioctl_compat,
1470 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1471 struct snd_rawmidi_str *stream,
1475 struct snd_rawmidi_substream *substream;
1478 for (idx = 0; idx < count; idx++) {
1479 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1480 if (substream == NULL) {
1481 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1484 substream->stream = direction;
1485 substream->number = idx;
1486 substream->rmidi = rmidi;
1487 substream->pstr = stream;
1488 list_add_tail(&substream->list, &stream->substreams);
1489 stream->substream_count++;
1495 * snd_rawmidi_new - create a rawmidi instance
1496 * @card: the card instance
1497 * @id: the id string
1498 * @device: the device index
1499 * @output_count: the number of output streams
1500 * @input_count: the number of input streams
1501 * @rrawmidi: the pointer to store the new rawmidi instance
1503 * Creates a new rawmidi instance.
1504 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1506 * Returns zero if successful, or a negative error code on failure.
1508 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1509 int output_count, int input_count,
1510 struct snd_rawmidi ** rrawmidi)
1512 struct snd_rawmidi *rmidi;
1514 static struct snd_device_ops ops = {
1515 .dev_free = snd_rawmidi_dev_free,
1516 .dev_register = snd_rawmidi_dev_register,
1517 .dev_disconnect = snd_rawmidi_dev_disconnect,
1520 if (snd_BUG_ON(!card))
1524 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1525 if (rmidi == NULL) {
1526 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1530 rmidi->device = device;
1531 mutex_init(&rmidi->open_mutex);
1532 init_waitqueue_head(&rmidi->open_wait);
1533 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1534 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1537 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1538 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1539 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1540 SNDRV_RAWMIDI_STREAM_INPUT,
1541 input_count)) < 0) {
1542 snd_rawmidi_free(rmidi);
1545 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1546 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1547 SNDRV_RAWMIDI_STREAM_OUTPUT,
1548 output_count)) < 0) {
1549 snd_rawmidi_free(rmidi);
1552 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1553 snd_rawmidi_free(rmidi);
1561 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1563 struct snd_rawmidi_substream *substream;
1565 while (!list_empty(&stream->substreams)) {
1566 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1567 list_del(&substream->list);
1572 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1577 snd_info_free_entry(rmidi->proc_entry);
1578 rmidi->proc_entry = NULL;
1579 mutex_lock(®ister_mutex);
1580 if (rmidi->ops && rmidi->ops->dev_unregister)
1581 rmidi->ops->dev_unregister(rmidi);
1582 mutex_unlock(®ister_mutex);
1584 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1585 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1586 if (rmidi->private_free)
1587 rmidi->private_free(rmidi);
1592 static int snd_rawmidi_dev_free(struct snd_device *device)
1594 struct snd_rawmidi *rmidi = device->device_data;
1595 return snd_rawmidi_free(rmidi);
1598 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1599 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1601 struct snd_rawmidi *rmidi = device->private_data;
1602 rmidi->seq_dev = NULL;
1606 static int snd_rawmidi_dev_register(struct snd_device *device)
1609 struct snd_info_entry *entry;
1611 struct snd_rawmidi *rmidi = device->device_data;
1613 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1615 mutex_lock(®ister_mutex);
1616 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1617 mutex_unlock(®ister_mutex);
1620 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1621 mutex_unlock(®ister_mutex);
1622 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1623 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1624 rmidi->card, rmidi->device,
1625 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1626 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1627 mutex_lock(®ister_mutex);
1628 list_del(&rmidi->list);
1629 mutex_unlock(®ister_mutex);
1632 if (rmidi->ops && rmidi->ops->dev_register &&
1633 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1634 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1635 mutex_lock(®ister_mutex);
1636 list_del(&rmidi->list);
1637 mutex_unlock(®ister_mutex);
1640 #ifdef CONFIG_SND_OSSEMUL
1642 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1643 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1644 rmidi->card, 0, &snd_rawmidi_f_ops,
1646 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1649 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1650 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1654 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1655 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1656 rmidi->card, 1, &snd_rawmidi_f_ops,
1658 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1663 #endif /* CONFIG_SND_OSSEMUL */
1664 sprintf(name, "midi%d", rmidi->device);
1665 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1667 entry->private_data = rmidi;
1668 entry->c.text.read = snd_rawmidi_proc_info_read;
1669 if (snd_info_register(entry) < 0) {
1670 snd_info_free_entry(entry);
1674 rmidi->proc_entry = entry;
1675 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1676 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1677 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1678 rmidi->seq_dev->private_data = rmidi;
1679 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1680 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1681 snd_device_register(rmidi->card, rmidi->seq_dev);
1688 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1690 struct snd_rawmidi *rmidi = device->device_data;
1693 mutex_lock(®ister_mutex);
1694 mutex_lock(&rmidi->open_mutex);
1695 wake_up(&rmidi->open_wait);
1696 list_del_init(&rmidi->list);
1697 for (dir = 0; dir < 2; dir++) {
1698 struct snd_rawmidi_substream *s;
1699 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1701 wake_up(&s->runtime->sleep);
1705 #ifdef CONFIG_SND_OSSEMUL
1706 if (rmidi->ossreg) {
1707 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1708 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1709 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1710 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1713 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1714 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1717 #endif /* CONFIG_SND_OSSEMUL */
1718 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1719 mutex_unlock(&rmidi->open_mutex);
1720 mutex_unlock(®ister_mutex);
1725 * snd_rawmidi_set_ops - set the rawmidi operators
1726 * @rmidi: the rawmidi instance
1727 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1728 * @ops: the operator table
1730 * Sets the rawmidi operators for the given stream direction.
1732 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1733 struct snd_rawmidi_ops *ops)
1735 struct snd_rawmidi_substream *substream;
1737 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1738 substream->ops = ops;
1745 static int __init alsa_rawmidi_init(void)
1748 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1749 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1750 #ifdef CONFIG_SND_OSSEMUL
1752 /* check device map table */
1753 for (i = 0; i < SNDRV_CARDS; i++) {
1754 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1755 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1758 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1759 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1764 #endif /* CONFIG_SND_OSSEMUL */
1768 static void __exit alsa_rawmidi_exit(void)
1770 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1771 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1774 module_init(alsa_rawmidi_init)
1775 module_exit(alsa_rawmidi_exit)
1777 EXPORT_SYMBOL(snd_rawmidi_output_params);
1778 EXPORT_SYMBOL(snd_rawmidi_input_params);
1779 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1780 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1781 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1782 EXPORT_SYMBOL(snd_rawmidi_receive);
1783 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1784 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1785 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1786 EXPORT_SYMBOL(snd_rawmidi_transmit);
1787 EXPORT_SYMBOL(snd_rawmidi_new);
1788 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1789 EXPORT_SYMBOL(snd_rawmidi_info_select);
1790 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1791 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1792 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1793 EXPORT_SYMBOL(snd_rawmidi_kernel_write);