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 (signal_pending(current)) {
432 remove_wait_queue(&rmidi->open_wait, &wait);
437 #ifdef CONFIG_SND_OSSEMUL
438 if (rawmidi_file->input && rawmidi_file->input->runtime)
439 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
440 if (rawmidi_file->output && rawmidi_file->output->runtime)
441 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
443 file->private_data = rawmidi_file;
444 mutex_unlock(&rmidi->open_mutex);
445 snd_card_unref(rmidi->card);
449 snd_card_file_remove(card, file);
451 mutex_unlock(&rmidi->open_mutex);
452 module_put(rmidi->card->module);
453 snd_card_unref(rmidi->card);
457 static void close_substream(struct snd_rawmidi *rmidi,
458 struct snd_rawmidi_substream *substream,
461 if (--substream->use_count)
465 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
466 snd_rawmidi_input_trigger(substream, 0);
468 if (substream->active_sensing) {
469 unsigned char buf = 0xfe;
470 /* sending single active sensing message
471 * to shut the device up
473 snd_rawmidi_kernel_write(substream, &buf, 1);
475 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
476 snd_rawmidi_output_trigger(substream, 0);
479 substream->ops->close(substream);
480 if (substream->runtime->private_free)
481 substream->runtime->private_free(substream);
482 snd_rawmidi_runtime_free(substream);
483 substream->opened = 0;
484 substream->append = 0;
485 put_pid(substream->pid);
486 substream->pid = NULL;
487 rmidi->streams[substream->stream].substream_opened--;
490 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
492 struct snd_rawmidi *rmidi;
494 rmidi = rfile->rmidi;
495 mutex_lock(&rmidi->open_mutex);
497 close_substream(rmidi, rfile->input, 1);
501 close_substream(rmidi, rfile->output, 1);
502 rfile->output = NULL;
505 mutex_unlock(&rmidi->open_mutex);
506 wake_up(&rmidi->open_wait);
509 /* called from sound/core/seq/seq_midi.c */
510 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
512 struct snd_rawmidi *rmidi;
514 if (snd_BUG_ON(!rfile))
517 rmidi = rfile->rmidi;
518 rawmidi_release_priv(rfile);
519 module_put(rmidi->card->module);
523 static int snd_rawmidi_release(struct inode *inode, struct file *file)
525 struct snd_rawmidi_file *rfile;
526 struct snd_rawmidi *rmidi;
527 struct module *module;
529 rfile = file->private_data;
530 rmidi = rfile->rmidi;
531 rawmidi_release_priv(rfile);
533 module = rmidi->card->module;
534 snd_card_file_remove(rmidi->card, file);
539 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
540 struct snd_rawmidi_info *info)
542 struct snd_rawmidi *rmidi;
544 if (substream == NULL)
546 rmidi = substream->rmidi;
547 memset(info, 0, sizeof(*info));
548 info->card = rmidi->card->number;
549 info->device = rmidi->device;
550 info->subdevice = substream->number;
551 info->stream = substream->stream;
552 info->flags = rmidi->info_flags;
553 strcpy(info->id, rmidi->id);
554 strcpy(info->name, rmidi->name);
555 strcpy(info->subname, substream->name);
556 info->subdevices_count = substream->pstr->substream_count;
557 info->subdevices_avail = (substream->pstr->substream_count -
558 substream->pstr->substream_opened);
562 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
563 struct snd_rawmidi_info __user * _info)
565 struct snd_rawmidi_info info;
567 if ((err = snd_rawmidi_info(substream, &info)) < 0)
569 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
574 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
576 struct snd_rawmidi *rmidi;
577 struct snd_rawmidi_str *pstr;
578 struct snd_rawmidi_substream *substream;
580 mutex_lock(®ister_mutex);
581 rmidi = snd_rawmidi_search(card, info->device);
582 mutex_unlock(®ister_mutex);
585 if (info->stream < 0 || info->stream > 1)
587 pstr = &rmidi->streams[info->stream];
588 if (pstr->substream_count == 0)
590 if (info->subdevice >= pstr->substream_count)
592 list_for_each_entry(substream, &pstr->substreams, list) {
593 if ((unsigned int)substream->number == info->subdevice)
594 return snd_rawmidi_info(substream, info);
599 static int snd_rawmidi_info_select_user(struct snd_card *card,
600 struct snd_rawmidi_info __user *_info)
603 struct snd_rawmidi_info info;
604 if (get_user(info.device, &_info->device))
606 if (get_user(info.stream, &_info->stream))
608 if (get_user(info.subdevice, &_info->subdevice))
610 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
612 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
617 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
618 struct snd_rawmidi_params * params)
621 struct snd_rawmidi_runtime *runtime = substream->runtime;
623 if (substream->append && substream->use_count > 1)
625 snd_rawmidi_drain_output(substream);
626 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
629 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
632 if (params->buffer_size != runtime->buffer_size) {
633 newbuf = krealloc(runtime->buffer, params->buffer_size,
637 runtime->buffer = newbuf;
638 runtime->buffer_size = params->buffer_size;
639 runtime->avail = runtime->buffer_size;
641 runtime->avail_min = params->avail_min;
642 substream->active_sensing = !params->no_active_sensing;
646 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
647 struct snd_rawmidi_params * params)
650 struct snd_rawmidi_runtime *runtime = substream->runtime;
652 snd_rawmidi_drain_input(substream);
653 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
656 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
659 if (params->buffer_size != runtime->buffer_size) {
660 newbuf = krealloc(runtime->buffer, params->buffer_size,
664 runtime->buffer = newbuf;
665 runtime->buffer_size = params->buffer_size;
667 runtime->avail_min = params->avail_min;
671 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
672 struct snd_rawmidi_status * status)
674 struct snd_rawmidi_runtime *runtime = substream->runtime;
676 memset(status, 0, sizeof(*status));
677 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
678 spin_lock_irq(&runtime->lock);
679 status->avail = runtime->avail;
680 spin_unlock_irq(&runtime->lock);
684 static int snd_rawmidi_input_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_INPUT;
691 spin_lock_irq(&runtime->lock);
692 status->avail = runtime->avail;
693 status->xruns = runtime->xruns;
695 spin_unlock_irq(&runtime->lock);
699 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
701 struct snd_rawmidi_file *rfile;
702 void __user *argp = (void __user *)arg;
704 rfile = file->private_data;
705 if (((cmd >> 8) & 0xff) != 'W')
708 case SNDRV_RAWMIDI_IOCTL_PVERSION:
709 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
710 case SNDRV_RAWMIDI_IOCTL_INFO:
713 struct snd_rawmidi_info __user *info = argp;
714 if (get_user(stream, &info->stream))
717 case SNDRV_RAWMIDI_STREAM_INPUT:
718 return snd_rawmidi_info_user(rfile->input, info);
719 case SNDRV_RAWMIDI_STREAM_OUTPUT:
720 return snd_rawmidi_info_user(rfile->output, info);
725 case SNDRV_RAWMIDI_IOCTL_PARAMS:
727 struct snd_rawmidi_params params;
728 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
730 switch (params.stream) {
731 case SNDRV_RAWMIDI_STREAM_OUTPUT:
732 if (rfile->output == NULL)
734 return snd_rawmidi_output_params(rfile->output, ¶ms);
735 case SNDRV_RAWMIDI_STREAM_INPUT:
736 if (rfile->input == NULL)
738 return snd_rawmidi_input_params(rfile->input, ¶ms);
743 case SNDRV_RAWMIDI_IOCTL_STATUS:
746 struct snd_rawmidi_status status;
747 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
749 switch (status.stream) {
750 case SNDRV_RAWMIDI_STREAM_OUTPUT:
751 if (rfile->output == NULL)
753 err = snd_rawmidi_output_status(rfile->output, &status);
755 case SNDRV_RAWMIDI_STREAM_INPUT:
756 if (rfile->input == NULL)
758 err = snd_rawmidi_input_status(rfile->input, &status);
765 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
769 case SNDRV_RAWMIDI_IOCTL_DROP:
772 if (get_user(val, (int __user *) argp))
775 case SNDRV_RAWMIDI_STREAM_OUTPUT:
776 if (rfile->output == NULL)
778 return snd_rawmidi_drop_output(rfile->output);
783 case SNDRV_RAWMIDI_IOCTL_DRAIN:
786 if (get_user(val, (int __user *) argp))
789 case SNDRV_RAWMIDI_STREAM_OUTPUT:
790 if (rfile->output == NULL)
792 return snd_rawmidi_drain_output(rfile->output);
793 case SNDRV_RAWMIDI_STREAM_INPUT:
794 if (rfile->input == NULL)
796 return snd_rawmidi_drain_input(rfile->input);
801 #ifdef CONFIG_SND_DEBUG
803 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
809 static int snd_rawmidi_control_ioctl(struct snd_card *card,
810 struct snd_ctl_file *control,
814 void __user *argp = (void __user *)arg;
817 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
821 if (get_user(device, (int __user *)argp))
823 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
824 device = SNDRV_RAWMIDI_DEVICES - 1;
825 mutex_lock(®ister_mutex);
826 device = device < 0 ? 0 : device + 1;
827 while (device < SNDRV_RAWMIDI_DEVICES) {
828 if (snd_rawmidi_search(card, device))
832 if (device == SNDRV_RAWMIDI_DEVICES)
834 mutex_unlock(®ister_mutex);
835 if (put_user(device, (int __user *)argp))
839 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
843 if (get_user(val, (int __user *)argp))
845 control->prefer_rawmidi_subdevice = val;
848 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
849 return snd_rawmidi_info_select_user(card, argp);
855 * snd_rawmidi_receive - receive the input data from the device
856 * @substream: the rawmidi substream
857 * @buffer: the buffer pointer
858 * @count: the data size to read
860 * Reads the data from the internal buffer.
862 * Returns the size of read data, or a negative error code on failure.
864 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
865 const unsigned char *buffer, int count)
868 int result = 0, count1;
869 struct snd_rawmidi_runtime *runtime = substream->runtime;
871 if (!substream->opened)
873 if (runtime->buffer == NULL) {
874 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
877 spin_lock_irqsave(&runtime->lock, flags);
878 if (count == 1) { /* special case, faster code */
880 if (runtime->avail < runtime->buffer_size) {
881 runtime->buffer[runtime->hw_ptr++] = buffer[0];
882 runtime->hw_ptr %= runtime->buffer_size;
889 substream->bytes += count;
890 count1 = runtime->buffer_size - runtime->hw_ptr;
893 if (count1 > (int)(runtime->buffer_size - runtime->avail))
894 count1 = runtime->buffer_size - runtime->avail;
895 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
896 runtime->hw_ptr += count1;
897 runtime->hw_ptr %= runtime->buffer_size;
898 runtime->avail += count1;
904 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
905 count1 = runtime->buffer_size - runtime->avail;
906 runtime->xruns += count - count1;
909 memcpy(runtime->buffer, buffer, count1);
910 runtime->hw_ptr = count1;
911 runtime->avail += count1;
918 schedule_work(&runtime->event_work);
919 else if (snd_rawmidi_ready(substream))
920 wake_up(&runtime->sleep);
922 spin_unlock_irqrestore(&runtime->lock, flags);
926 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
927 unsigned char __user *userbuf,
928 unsigned char *kernelbuf, long count)
931 long result = 0, count1;
932 struct snd_rawmidi_runtime *runtime = substream->runtime;
934 while (count > 0 && runtime->avail) {
935 count1 = runtime->buffer_size - runtime->appl_ptr;
938 spin_lock_irqsave(&runtime->lock, flags);
939 if (count1 > (int)runtime->avail)
940 count1 = runtime->avail;
942 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
944 spin_unlock_irqrestore(&runtime->lock, flags);
945 if (copy_to_user(userbuf + result,
946 runtime->buffer + runtime->appl_ptr, count1)) {
947 return result > 0 ? result : -EFAULT;
949 spin_lock_irqsave(&runtime->lock, flags);
951 runtime->appl_ptr += count1;
952 runtime->appl_ptr %= runtime->buffer_size;
953 runtime->avail -= count1;
954 spin_unlock_irqrestore(&runtime->lock, flags);
961 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
962 unsigned char *buf, long count)
964 snd_rawmidi_input_trigger(substream, 1);
965 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
968 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
973 struct snd_rawmidi_file *rfile;
974 struct snd_rawmidi_substream *substream;
975 struct snd_rawmidi_runtime *runtime;
977 rfile = file->private_data;
978 substream = rfile->input;
979 if (substream == NULL)
981 runtime = substream->runtime;
982 snd_rawmidi_input_trigger(substream, 1);
985 spin_lock_irq(&runtime->lock);
986 while (!snd_rawmidi_ready(substream)) {
988 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
989 spin_unlock_irq(&runtime->lock);
990 return result > 0 ? result : -EAGAIN;
992 init_waitqueue_entry(&wait, current);
993 add_wait_queue(&runtime->sleep, &wait);
994 set_current_state(TASK_INTERRUPTIBLE);
995 spin_unlock_irq(&runtime->lock);
997 remove_wait_queue(&runtime->sleep, &wait);
998 if (signal_pending(current))
999 return result > 0 ? result : -ERESTARTSYS;
1000 if (!runtime->avail)
1001 return result > 0 ? result : -EIO;
1002 spin_lock_irq(&runtime->lock);
1004 spin_unlock_irq(&runtime->lock);
1005 count1 = snd_rawmidi_kernel_read1(substream,
1006 (unsigned char __user *)buf,
1010 return result > 0 ? result : count1;
1019 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1020 * @substream: the rawmidi substream
1022 * Returns 1 if the internal output buffer is empty, 0 if not.
1024 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1026 struct snd_rawmidi_runtime *runtime = substream->runtime;
1028 unsigned long flags;
1030 if (runtime->buffer == NULL) {
1031 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1034 spin_lock_irqsave(&runtime->lock, flags);
1035 result = runtime->avail >= runtime->buffer_size;
1036 spin_unlock_irqrestore(&runtime->lock, flags);
1041 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1042 * @substream: the rawmidi substream
1043 * @buffer: the buffer pointer
1044 * @count: data size to transfer
1046 * Copies data from the internal output buffer to the given buffer.
1048 * Call this in the interrupt handler when the midi output is ready,
1049 * and call snd_rawmidi_transmit_ack() after the transmission is
1052 * Returns the size of copied data, or a negative error code on failure.
1054 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1055 unsigned char *buffer, int count)
1057 unsigned long flags;
1059 struct snd_rawmidi_runtime *runtime = substream->runtime;
1061 if (runtime->buffer == NULL) {
1062 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1066 spin_lock_irqsave(&runtime->lock, flags);
1067 if (runtime->avail >= runtime->buffer_size) {
1068 /* warning: lowlevel layer MUST trigger down the hardware */
1071 if (count == 1) { /* special case, faster code */
1072 *buffer = runtime->buffer[runtime->hw_ptr];
1075 count1 = runtime->buffer_size - runtime->hw_ptr;
1078 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1079 count1 = runtime->buffer_size - runtime->avail;
1080 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1084 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1085 count = runtime->buffer_size - runtime->avail - count1;
1086 memcpy(buffer + count1, runtime->buffer, count);
1091 spin_unlock_irqrestore(&runtime->lock, flags);
1096 * snd_rawmidi_transmit_ack - acknowledge the transmission
1097 * @substream: the rawmidi substream
1098 * @count: the tranferred count
1100 * Advances the hardware pointer for the internal output buffer with
1101 * the given size and updates the condition.
1102 * Call after the transmission is finished.
1104 * Returns the advanced size if successful, or a negative error code on failure.
1106 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1108 unsigned long flags;
1109 struct snd_rawmidi_runtime *runtime = substream->runtime;
1111 if (runtime->buffer == NULL) {
1112 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1115 spin_lock_irqsave(&runtime->lock, flags);
1116 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1117 runtime->hw_ptr += count;
1118 runtime->hw_ptr %= runtime->buffer_size;
1119 runtime->avail += count;
1120 substream->bytes += count;
1122 if (runtime->drain || snd_rawmidi_ready(substream))
1123 wake_up(&runtime->sleep);
1125 spin_unlock_irqrestore(&runtime->lock, flags);
1130 * snd_rawmidi_transmit - copy from the buffer to the device
1131 * @substream: the rawmidi substream
1132 * @buffer: the buffer pointer
1133 * @count: the data size to transfer
1135 * Copies data from the buffer to the device and advances the pointer.
1137 * Returns the copied size if successful, or a negative error code on failure.
1139 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1140 unsigned char *buffer, int count)
1142 if (!substream->opened)
1144 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1147 return snd_rawmidi_transmit_ack(substream, count);
1150 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1151 const unsigned char __user *userbuf,
1152 const unsigned char *kernelbuf,
1155 unsigned long flags;
1156 long count1, result;
1157 struct snd_rawmidi_runtime *runtime = substream->runtime;
1159 if (snd_BUG_ON(!kernelbuf && !userbuf))
1161 if (snd_BUG_ON(!runtime->buffer))
1165 spin_lock_irqsave(&runtime->lock, flags);
1166 if (substream->append) {
1167 if ((long)runtime->avail < count) {
1168 spin_unlock_irqrestore(&runtime->lock, flags);
1172 while (count > 0 && runtime->avail > 0) {
1173 count1 = runtime->buffer_size - runtime->appl_ptr;
1176 if (count1 > (long)runtime->avail)
1177 count1 = runtime->avail;
1179 memcpy(runtime->buffer + runtime->appl_ptr,
1180 kernelbuf + result, count1);
1182 spin_unlock_irqrestore(&runtime->lock, flags);
1183 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1184 userbuf + result, count1)) {
1185 spin_lock_irqsave(&runtime->lock, flags);
1186 result = result > 0 ? result : -EFAULT;
1189 spin_lock_irqsave(&runtime->lock, flags);
1191 runtime->appl_ptr += count1;
1192 runtime->appl_ptr %= runtime->buffer_size;
1193 runtime->avail -= count1;
1198 count1 = runtime->avail < runtime->buffer_size;
1199 spin_unlock_irqrestore(&runtime->lock, flags);
1201 snd_rawmidi_output_trigger(substream, 1);
1205 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1206 const unsigned char *buf, long count)
1208 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1211 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1212 size_t count, loff_t *offset)
1214 long result, timeout;
1216 struct snd_rawmidi_file *rfile;
1217 struct snd_rawmidi_runtime *runtime;
1218 struct snd_rawmidi_substream *substream;
1220 rfile = file->private_data;
1221 substream = rfile->output;
1222 runtime = substream->runtime;
1223 /* we cannot put an atomic message to our buffer */
1224 if (substream->append && count > runtime->buffer_size)
1228 spin_lock_irq(&runtime->lock);
1229 while (!snd_rawmidi_ready_append(substream, count)) {
1231 if (file->f_flags & O_NONBLOCK) {
1232 spin_unlock_irq(&runtime->lock);
1233 return result > 0 ? result : -EAGAIN;
1235 init_waitqueue_entry(&wait, current);
1236 add_wait_queue(&runtime->sleep, &wait);
1237 set_current_state(TASK_INTERRUPTIBLE);
1238 spin_unlock_irq(&runtime->lock);
1239 timeout = schedule_timeout(30 * HZ);
1240 remove_wait_queue(&runtime->sleep, &wait);
1241 if (signal_pending(current))
1242 return result > 0 ? result : -ERESTARTSYS;
1243 if (!runtime->avail && !timeout)
1244 return result > 0 ? result : -EIO;
1245 spin_lock_irq(&runtime->lock);
1247 spin_unlock_irq(&runtime->lock);
1248 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1250 return result > 0 ? result : count1;
1253 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1257 if (file->f_flags & O_DSYNC) {
1258 spin_lock_irq(&runtime->lock);
1259 while (runtime->avail != runtime->buffer_size) {
1261 unsigned int last_avail = runtime->avail;
1262 init_waitqueue_entry(&wait, current);
1263 add_wait_queue(&runtime->sleep, &wait);
1264 set_current_state(TASK_INTERRUPTIBLE);
1265 spin_unlock_irq(&runtime->lock);
1266 timeout = schedule_timeout(30 * HZ);
1267 remove_wait_queue(&runtime->sleep, &wait);
1268 if (signal_pending(current))
1269 return result > 0 ? result : -ERESTARTSYS;
1270 if (runtime->avail == last_avail && !timeout)
1271 return result > 0 ? result : -EIO;
1272 spin_lock_irq(&runtime->lock);
1274 spin_unlock_irq(&runtime->lock);
1279 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1281 struct snd_rawmidi_file *rfile;
1282 struct snd_rawmidi_runtime *runtime;
1285 rfile = file->private_data;
1286 if (rfile->input != NULL) {
1287 runtime = rfile->input->runtime;
1288 snd_rawmidi_input_trigger(rfile->input, 1);
1289 poll_wait(file, &runtime->sleep, wait);
1291 if (rfile->output != NULL) {
1292 runtime = rfile->output->runtime;
1293 poll_wait(file, &runtime->sleep, wait);
1296 if (rfile->input != NULL) {
1297 if (snd_rawmidi_ready(rfile->input))
1298 mask |= POLLIN | POLLRDNORM;
1300 if (rfile->output != NULL) {
1301 if (snd_rawmidi_ready(rfile->output))
1302 mask |= POLLOUT | POLLWRNORM;
1309 #ifdef CONFIG_COMPAT
1310 #include "rawmidi_compat.c"
1312 #define snd_rawmidi_ioctl_compat NULL
1319 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1320 struct snd_info_buffer *buffer)
1322 struct snd_rawmidi *rmidi;
1323 struct snd_rawmidi_substream *substream;
1324 struct snd_rawmidi_runtime *runtime;
1326 rmidi = entry->private_data;
1327 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1328 mutex_lock(&rmidi->open_mutex);
1329 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1330 list_for_each_entry(substream,
1331 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1335 " Tx bytes : %lu\n",
1337 (unsigned long) substream->bytes);
1338 if (substream->opened) {
1340 " Owner PID : %d\n",
1341 pid_vnr(substream->pid));
1342 runtime = substream->runtime;
1345 " Buffer size : %lu\n"
1347 runtime->oss ? "OSS compatible" : "native",
1348 (unsigned long) runtime->buffer_size,
1349 (unsigned long) runtime->avail);
1353 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1354 list_for_each_entry(substream,
1355 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1359 " Rx bytes : %lu\n",
1361 (unsigned long) substream->bytes);
1362 if (substream->opened) {
1364 " Owner PID : %d\n",
1365 pid_vnr(substream->pid));
1366 runtime = substream->runtime;
1368 " Buffer size : %lu\n"
1370 " Overruns : %lu\n",
1371 (unsigned long) runtime->buffer_size,
1372 (unsigned long) runtime->avail,
1373 (unsigned long) runtime->xruns);
1377 mutex_unlock(&rmidi->open_mutex);
1381 * Register functions
1384 static const struct file_operations snd_rawmidi_f_ops =
1386 .owner = THIS_MODULE,
1387 .read = snd_rawmidi_read,
1388 .write = snd_rawmidi_write,
1389 .open = snd_rawmidi_open,
1390 .release = snd_rawmidi_release,
1391 .llseek = no_llseek,
1392 .poll = snd_rawmidi_poll,
1393 .unlocked_ioctl = snd_rawmidi_ioctl,
1394 .compat_ioctl = snd_rawmidi_ioctl_compat,
1397 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1398 struct snd_rawmidi_str *stream,
1402 struct snd_rawmidi_substream *substream;
1405 for (idx = 0; idx < count; idx++) {
1406 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1407 if (substream == NULL) {
1408 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1411 substream->stream = direction;
1412 substream->number = idx;
1413 substream->rmidi = rmidi;
1414 substream->pstr = stream;
1415 list_add_tail(&substream->list, &stream->substreams);
1416 stream->substream_count++;
1422 * snd_rawmidi_new - create a rawmidi instance
1423 * @card: the card instance
1424 * @id: the id string
1425 * @device: the device index
1426 * @output_count: the number of output streams
1427 * @input_count: the number of input streams
1428 * @rrawmidi: the pointer to store the new rawmidi instance
1430 * Creates a new rawmidi instance.
1431 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1433 * Returns zero if successful, or a negative error code on failure.
1435 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1436 int output_count, int input_count,
1437 struct snd_rawmidi ** rrawmidi)
1439 struct snd_rawmidi *rmidi;
1441 static struct snd_device_ops ops = {
1442 .dev_free = snd_rawmidi_dev_free,
1443 .dev_register = snd_rawmidi_dev_register,
1444 .dev_disconnect = snd_rawmidi_dev_disconnect,
1447 if (snd_BUG_ON(!card))
1451 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1452 if (rmidi == NULL) {
1453 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1457 rmidi->device = device;
1458 mutex_init(&rmidi->open_mutex);
1459 init_waitqueue_head(&rmidi->open_wait);
1460 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1461 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1464 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1465 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1466 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1467 SNDRV_RAWMIDI_STREAM_INPUT,
1468 input_count)) < 0) {
1469 snd_rawmidi_free(rmidi);
1472 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1473 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1474 SNDRV_RAWMIDI_STREAM_OUTPUT,
1475 output_count)) < 0) {
1476 snd_rawmidi_free(rmidi);
1479 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1480 snd_rawmidi_free(rmidi);
1488 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1490 struct snd_rawmidi_substream *substream;
1492 while (!list_empty(&stream->substreams)) {
1493 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1494 list_del(&substream->list);
1499 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1504 snd_info_free_entry(rmidi->proc_entry);
1505 rmidi->proc_entry = NULL;
1506 mutex_lock(®ister_mutex);
1507 if (rmidi->ops && rmidi->ops->dev_unregister)
1508 rmidi->ops->dev_unregister(rmidi);
1509 mutex_unlock(®ister_mutex);
1511 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1512 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1513 if (rmidi->private_free)
1514 rmidi->private_free(rmidi);
1519 static int snd_rawmidi_dev_free(struct snd_device *device)
1521 struct snd_rawmidi *rmidi = device->device_data;
1522 return snd_rawmidi_free(rmidi);
1525 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1526 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1528 struct snd_rawmidi *rmidi = device->private_data;
1529 rmidi->seq_dev = NULL;
1533 static int snd_rawmidi_dev_register(struct snd_device *device)
1536 struct snd_info_entry *entry;
1538 struct snd_rawmidi *rmidi = device->device_data;
1540 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1542 mutex_lock(®ister_mutex);
1543 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1544 mutex_unlock(®ister_mutex);
1547 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1548 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1549 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1550 rmidi->card, rmidi->device,
1551 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1552 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1553 list_del(&rmidi->list);
1554 mutex_unlock(®ister_mutex);
1557 if (rmidi->ops && rmidi->ops->dev_register &&
1558 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1559 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1560 list_del(&rmidi->list);
1561 mutex_unlock(®ister_mutex);
1564 #ifdef CONFIG_SND_OSSEMUL
1566 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1567 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1568 rmidi->card, 0, &snd_rawmidi_f_ops,
1570 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1573 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1574 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1578 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1579 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1580 rmidi->card, 1, &snd_rawmidi_f_ops,
1582 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1587 #endif /* CONFIG_SND_OSSEMUL */
1588 mutex_unlock(®ister_mutex);
1589 sprintf(name, "midi%d", rmidi->device);
1590 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1592 entry->private_data = rmidi;
1593 entry->c.text.read = snd_rawmidi_proc_info_read;
1594 if (snd_info_register(entry) < 0) {
1595 snd_info_free_entry(entry);
1599 rmidi->proc_entry = entry;
1600 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1601 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1602 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1603 rmidi->seq_dev->private_data = rmidi;
1604 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1605 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1606 snd_device_register(rmidi->card, rmidi->seq_dev);
1613 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1615 struct snd_rawmidi *rmidi = device->device_data;
1617 mutex_lock(®ister_mutex);
1618 list_del_init(&rmidi->list);
1619 #ifdef CONFIG_SND_OSSEMUL
1620 if (rmidi->ossreg) {
1621 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1622 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1623 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1624 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1627 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1628 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1631 #endif /* CONFIG_SND_OSSEMUL */
1632 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1633 mutex_unlock(®ister_mutex);
1638 * snd_rawmidi_set_ops - set the rawmidi operators
1639 * @rmidi: the rawmidi instance
1640 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1641 * @ops: the operator table
1643 * Sets the rawmidi operators for the given stream direction.
1645 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1646 struct snd_rawmidi_ops *ops)
1648 struct snd_rawmidi_substream *substream;
1650 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1651 substream->ops = ops;
1658 static int __init alsa_rawmidi_init(void)
1661 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1662 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1663 #ifdef CONFIG_SND_OSSEMUL
1665 /* check device map table */
1666 for (i = 0; i < SNDRV_CARDS; i++) {
1667 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1668 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1671 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1672 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1677 #endif /* CONFIG_SND_OSSEMUL */
1681 static void __exit alsa_rawmidi_exit(void)
1683 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1684 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1687 module_init(alsa_rawmidi_init)
1688 module_exit(alsa_rawmidi_exit)
1690 EXPORT_SYMBOL(snd_rawmidi_output_params);
1691 EXPORT_SYMBOL(snd_rawmidi_input_params);
1692 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1693 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1694 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1695 EXPORT_SYMBOL(snd_rawmidi_receive);
1696 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1697 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1698 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1699 EXPORT_SYMBOL(snd_rawmidi_transmit);
1700 EXPORT_SYMBOL(snd_rawmidi_new);
1701 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1702 EXPORT_SYMBOL(snd_rawmidi_info_select);
1703 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1704 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1705 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1706 EXPORT_SYMBOL(snd_rawmidi_kernel_write);