2 * Abstract layer for MIDI v1.0 stream
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/core.h>
23 #include <linux/major.h>
24 #include <linux/init.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/wait.h>
29 #include <linux/mutex.h>
30 #include <linux/moduleparam.h>
31 #include <linux/delay.h>
32 #include <sound/rawmidi.h>
33 #include <sound/info.h>
34 #include <sound/control.h>
35 #include <sound/minors.h>
36 #include <sound/initval.h>
38 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
39 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
40 MODULE_LICENSE("GPL");
42 #ifdef CONFIG_SND_OSSEMUL
43 static int midi_map[SNDRV_CARDS];
44 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
45 module_param_array(midi_map, int, NULL, 0444);
46 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
47 module_param_array(amidi_map, int, NULL, 0444);
48 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
49 #endif /* CONFIG_SND_OSSEMUL */
51 static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
52 static int snd_rawmidi_dev_free(struct snd_device *device);
53 static int snd_rawmidi_dev_register(struct snd_device *device);
54 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
56 static LIST_HEAD(snd_rawmidi_devices);
57 static DEFINE_MUTEX(register_mutex);
59 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
61 struct snd_rawmidi *rawmidi;
63 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
64 if (rawmidi->card == card && rawmidi->device == device)
69 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
71 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
73 return SNDRV_RAWMIDI_LFLG_OUTPUT;
75 return SNDRV_RAWMIDI_LFLG_INPUT;
77 return SNDRV_RAWMIDI_LFLG_OPEN;
81 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
83 struct snd_rawmidi_runtime *runtime = substream->runtime;
84 return runtime->avail >= runtime->avail_min;
87 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
90 struct snd_rawmidi_runtime *runtime = substream->runtime;
91 return runtime->avail >= runtime->avail_min &&
92 (!substream->append || runtime->avail >= count);
95 static void snd_rawmidi_input_event_tasklet(unsigned long data)
97 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
98 substream->runtime->event(substream);
101 static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
103 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
104 substream->ops->trigger(substream, 1);
107 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
109 struct snd_rawmidi_runtime *runtime;
111 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
113 spin_lock_init(&runtime->lock);
114 init_waitqueue_head(&runtime->sleep);
115 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
116 tasklet_init(&runtime->tasklet,
117 snd_rawmidi_input_event_tasklet,
118 (unsigned long)substream);
120 tasklet_init(&runtime->tasklet,
121 snd_rawmidi_output_trigger_tasklet,
122 (unsigned long)substream);
123 runtime->event = NULL;
124 runtime->buffer_size = PAGE_SIZE;
125 runtime->avail_min = 1;
126 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
129 runtime->avail = runtime->buffer_size;
130 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
134 runtime->appl_ptr = runtime->hw_ptr = 0;
135 substream->runtime = runtime;
139 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
141 struct snd_rawmidi_runtime *runtime = substream->runtime;
143 kfree(runtime->buffer);
145 substream->runtime = NULL;
149 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
151 if (!substream->opened)
154 tasklet_schedule(&substream->runtime->tasklet);
156 tasklet_kill(&substream->runtime->tasklet);
157 substream->ops->trigger(substream, 0);
161 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
163 if (!substream->opened)
165 substream->ops->trigger(substream, up);
166 if (!up && substream->runtime->event)
167 tasklet_kill(&substream->runtime->tasklet);
170 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
173 struct snd_rawmidi_runtime *runtime = substream->runtime;
175 snd_rawmidi_output_trigger(substream, 0);
177 spin_lock_irqsave(&runtime->lock, flags);
178 runtime->appl_ptr = runtime->hw_ptr = 0;
179 runtime->avail = runtime->buffer_size;
180 spin_unlock_irqrestore(&runtime->lock, flags);
184 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
188 struct snd_rawmidi_runtime *runtime = substream->runtime;
192 timeout = wait_event_interruptible_timeout(runtime->sleep,
193 (runtime->avail >= runtime->buffer_size),
195 if (signal_pending(current))
197 if (runtime->avail < runtime->buffer_size && !timeout) {
198 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
202 if (err != -ERESTARTSYS) {
203 /* we need wait a while to make sure that Tx FIFOs are empty */
204 if (substream->ops->drain)
205 substream->ops->drain(substream);
208 snd_rawmidi_drop_output(substream);
213 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
216 struct snd_rawmidi_runtime *runtime = substream->runtime;
218 snd_rawmidi_input_trigger(substream, 0);
220 spin_lock_irqsave(&runtime->lock, flags);
221 runtime->appl_ptr = runtime->hw_ptr = 0;
223 spin_unlock_irqrestore(&runtime->lock, flags);
227 /* look for an available substream for the given stream direction;
228 * if a specific subdevice is given, try to assign it
230 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
231 int stream, int mode,
232 struct snd_rawmidi_substream **sub_ret)
234 struct snd_rawmidi_substream *substream;
235 struct snd_rawmidi_str *s = &rmidi->streams[stream];
236 static unsigned int info_flags[2] = {
237 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
238 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
241 if (!(rmidi->info_flags & info_flags[stream]))
243 if (subdevice >= 0 && subdevice >= s->substream_count)
245 if (s->substream_opened >= s->substream_count)
248 list_for_each_entry(substream, &s->substreams, list) {
249 if (substream->opened) {
250 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
251 !(mode & SNDRV_RAWMIDI_LFLG_APPEND))
254 if (subdevice < 0 || subdevice == substream->number) {
255 *sub_ret = substream;
262 /* open and do ref-counting for the given substream */
263 static int open_substream(struct snd_rawmidi *rmidi,
264 struct snd_rawmidi_substream *substream,
269 if (substream->use_count == 0) {
270 err = snd_rawmidi_runtime_create(substream);
273 err = substream->ops->open(substream);
276 substream->opened = 1;
277 substream->active_sensing = 0;
278 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
279 substream->append = 1;
281 substream->use_count++;
282 rmidi->streams[substream->stream].substream_opened++;
286 static void close_substream(struct snd_rawmidi *rmidi,
287 struct snd_rawmidi_substream *substream,
290 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
291 struct snd_rawmidi_file *rfile)
293 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
296 rfile->input = rfile->output = NULL;
297 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
298 err = assign_substream(rmidi, subdevice,
299 SNDRV_RAWMIDI_STREAM_INPUT,
304 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
305 err = assign_substream(rmidi, subdevice,
306 SNDRV_RAWMIDI_STREAM_OUTPUT,
313 err = open_substream(rmidi, sinput, mode);
318 err = open_substream(rmidi, soutput, mode);
321 close_substream(rmidi, sinput, 0);
326 rfile->rmidi = rmidi;
327 rfile->input = sinput;
328 rfile->output = soutput;
332 if (sinput && sinput->runtime)
333 snd_rawmidi_runtime_free(sinput);
334 if (soutput && soutput->runtime)
335 snd_rawmidi_runtime_free(soutput);
339 /* called from sound/core/seq/seq_midi.c */
340 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
341 int mode, struct snd_rawmidi_file * rfile)
343 struct snd_rawmidi *rmidi;
346 if (snd_BUG_ON(!rfile))
349 mutex_lock(®ister_mutex);
350 rmidi = snd_rawmidi_search(card, device);
352 mutex_unlock(®ister_mutex);
355 if (!try_module_get(rmidi->card->module)) {
356 mutex_unlock(®ister_mutex);
359 mutex_unlock(®ister_mutex);
361 mutex_lock(&rmidi->open_mutex);
362 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
363 mutex_unlock(&rmidi->open_mutex);
365 module_put(rmidi->card->module);
369 static int snd_rawmidi_open(struct inode *inode, struct file *file)
371 int maj = imajor(inode);
372 struct snd_card *card;
374 unsigned short fflags;
376 struct snd_rawmidi *rmidi;
377 struct snd_rawmidi_file *rawmidi_file = NULL;
379 struct snd_ctl_file *kctl;
381 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
382 return -EINVAL; /* invalid combination */
384 if (maj == snd_major) {
385 rmidi = snd_lookup_minor_data(iminor(inode),
386 SNDRV_DEVICE_TYPE_RAWMIDI);
387 #ifdef CONFIG_SND_OSSEMUL
388 } else if (maj == SOUND_MAJOR) {
389 rmidi = snd_lookup_oss_minor_data(iminor(inode),
390 SNDRV_OSS_DEVICE_TYPE_MIDI);
398 if (!try_module_get(rmidi->card->module))
401 mutex_lock(&rmidi->open_mutex);
403 err = snd_card_file_add(card, file);
406 fflags = snd_rawmidi_file_flags(file);
407 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
408 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
409 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
410 if (rawmidi_file == NULL) {
414 init_waitqueue_entry(&wait, current);
415 add_wait_queue(&rmidi->open_wait, &wait);
418 read_lock(&card->ctl_files_rwlock);
419 list_for_each_entry(kctl, &card->ctl_files, list) {
420 if (kctl->pid == current->pid) {
421 subdevice = kctl->prefer_rawmidi_subdevice;
426 read_unlock(&card->ctl_files_rwlock);
427 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
430 if (err == -EAGAIN) {
431 if (file->f_flags & O_NONBLOCK) {
437 set_current_state(TASK_INTERRUPTIBLE);
438 mutex_unlock(&rmidi->open_mutex);
440 mutex_lock(&rmidi->open_mutex);
441 if (signal_pending(current)) {
446 remove_wait_queue(&rmidi->open_wait, &wait);
451 #ifdef CONFIG_SND_OSSEMUL
452 if (rawmidi_file->input && rawmidi_file->input->runtime)
453 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
454 if (rawmidi_file->output && rawmidi_file->output->runtime)
455 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
457 file->private_data = rawmidi_file;
458 mutex_unlock(&rmidi->open_mutex);
462 snd_card_file_remove(card, file);
464 mutex_unlock(&rmidi->open_mutex);
465 module_put(rmidi->card->module);
469 static void close_substream(struct snd_rawmidi *rmidi,
470 struct snd_rawmidi_substream *substream,
473 rmidi->streams[substream->stream].substream_opened--;
474 if (--substream->use_count)
478 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
479 snd_rawmidi_input_trigger(substream, 0);
481 if (substream->active_sensing) {
482 unsigned char buf = 0xfe;
483 /* sending single active sensing message
484 * to shut the device up
486 snd_rawmidi_kernel_write(substream, &buf, 1);
488 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
489 snd_rawmidi_output_trigger(substream, 0);
492 substream->ops->close(substream);
493 if (substream->runtime->private_free)
494 substream->runtime->private_free(substream);
495 snd_rawmidi_runtime_free(substream);
496 substream->opened = 0;
497 substream->append = 0;
500 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
502 struct snd_rawmidi *rmidi;
504 rmidi = rfile->rmidi;
505 mutex_lock(&rmidi->open_mutex);
507 close_substream(rmidi, rfile->input, 1);
511 close_substream(rmidi, rfile->output, 1);
512 rfile->output = NULL;
515 mutex_unlock(&rmidi->open_mutex);
516 wake_up(&rmidi->open_wait);
519 /* called from sound/core/seq/seq_midi.c */
520 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
522 struct snd_rawmidi *rmidi;
524 if (snd_BUG_ON(!rfile))
527 rmidi = rfile->rmidi;
528 rawmidi_release_priv(rfile);
529 module_put(rmidi->card->module);
533 static int snd_rawmidi_release(struct inode *inode, struct file *file)
535 struct snd_rawmidi_file *rfile;
536 struct snd_rawmidi *rmidi;
538 rfile = file->private_data;
539 rmidi = rfile->rmidi;
540 rawmidi_release_priv(rfile);
542 snd_card_file_remove(rmidi->card, file);
543 module_put(rmidi->card->module);
547 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
548 struct snd_rawmidi_info *info)
550 struct snd_rawmidi *rmidi;
552 if (substream == NULL)
554 rmidi = substream->rmidi;
555 memset(info, 0, sizeof(*info));
556 info->card = rmidi->card->number;
557 info->device = rmidi->device;
558 info->subdevice = substream->number;
559 info->stream = substream->stream;
560 info->flags = rmidi->info_flags;
561 strcpy(info->id, rmidi->id);
562 strcpy(info->name, rmidi->name);
563 strcpy(info->subname, substream->name);
564 info->subdevices_count = substream->pstr->substream_count;
565 info->subdevices_avail = (substream->pstr->substream_count -
566 substream->pstr->substream_opened);
570 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
571 struct snd_rawmidi_info __user * _info)
573 struct snd_rawmidi_info info;
575 if ((err = snd_rawmidi_info(substream, &info)) < 0)
577 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
582 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
584 struct snd_rawmidi *rmidi;
585 struct snd_rawmidi_str *pstr;
586 struct snd_rawmidi_substream *substream;
588 mutex_lock(®ister_mutex);
589 rmidi = snd_rawmidi_search(card, info->device);
590 mutex_unlock(®ister_mutex);
593 if (info->stream < 0 || info->stream > 1)
595 pstr = &rmidi->streams[info->stream];
596 if (pstr->substream_count == 0)
598 if (info->subdevice >= pstr->substream_count)
600 list_for_each_entry(substream, &pstr->substreams, list) {
601 if ((unsigned int)substream->number == info->subdevice)
602 return snd_rawmidi_info(substream, info);
607 static int snd_rawmidi_info_select_user(struct snd_card *card,
608 struct snd_rawmidi_info __user *_info)
611 struct snd_rawmidi_info info;
612 if (get_user(info.device, &_info->device))
614 if (get_user(info.stream, &_info->stream))
616 if (get_user(info.subdevice, &_info->subdevice))
618 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
620 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
625 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
626 struct snd_rawmidi_params * params)
629 struct snd_rawmidi_runtime *runtime = substream->runtime;
631 if (substream->append && substream->use_count > 1)
633 snd_rawmidi_drain_output(substream);
634 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
637 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
640 if (params->buffer_size != runtime->buffer_size) {
641 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
644 kfree(runtime->buffer);
645 runtime->buffer = newbuf;
646 runtime->buffer_size = params->buffer_size;
647 runtime->avail = runtime->buffer_size;
649 runtime->avail_min = params->avail_min;
650 substream->active_sensing = !params->no_active_sensing;
654 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
655 struct snd_rawmidi_params * params)
658 struct snd_rawmidi_runtime *runtime = substream->runtime;
660 snd_rawmidi_drain_input(substream);
661 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
664 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
667 if (params->buffer_size != runtime->buffer_size) {
668 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
671 kfree(runtime->buffer);
672 runtime->buffer = newbuf;
673 runtime->buffer_size = params->buffer_size;
675 runtime->avail_min = params->avail_min;
679 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
680 struct snd_rawmidi_status * status)
682 struct snd_rawmidi_runtime *runtime = substream->runtime;
684 memset(status, 0, sizeof(*status));
685 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
686 spin_lock_irq(&runtime->lock);
687 status->avail = runtime->avail;
688 spin_unlock_irq(&runtime->lock);
692 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
693 struct snd_rawmidi_status * status)
695 struct snd_rawmidi_runtime *runtime = substream->runtime;
697 memset(status, 0, sizeof(*status));
698 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
699 spin_lock_irq(&runtime->lock);
700 status->avail = runtime->avail;
701 status->xruns = runtime->xruns;
703 spin_unlock_irq(&runtime->lock);
707 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
709 struct snd_rawmidi_file *rfile;
710 void __user *argp = (void __user *)arg;
712 rfile = file->private_data;
713 if (((cmd >> 8) & 0xff) != 'W')
716 case SNDRV_RAWMIDI_IOCTL_PVERSION:
717 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
718 case SNDRV_RAWMIDI_IOCTL_INFO:
721 struct snd_rawmidi_info __user *info = argp;
722 if (get_user(stream, &info->stream))
725 case SNDRV_RAWMIDI_STREAM_INPUT:
726 return snd_rawmidi_info_user(rfile->input, info);
727 case SNDRV_RAWMIDI_STREAM_OUTPUT:
728 return snd_rawmidi_info_user(rfile->output, info);
733 case SNDRV_RAWMIDI_IOCTL_PARAMS:
735 struct snd_rawmidi_params params;
736 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
738 switch (params.stream) {
739 case SNDRV_RAWMIDI_STREAM_OUTPUT:
740 if (rfile->output == NULL)
742 return snd_rawmidi_output_params(rfile->output, ¶ms);
743 case SNDRV_RAWMIDI_STREAM_INPUT:
744 if (rfile->input == NULL)
746 return snd_rawmidi_input_params(rfile->input, ¶ms);
751 case SNDRV_RAWMIDI_IOCTL_STATUS:
754 struct snd_rawmidi_status status;
755 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
757 switch (status.stream) {
758 case SNDRV_RAWMIDI_STREAM_OUTPUT:
759 if (rfile->output == NULL)
761 err = snd_rawmidi_output_status(rfile->output, &status);
763 case SNDRV_RAWMIDI_STREAM_INPUT:
764 if (rfile->input == NULL)
766 err = snd_rawmidi_input_status(rfile->input, &status);
773 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
777 case SNDRV_RAWMIDI_IOCTL_DROP:
780 if (get_user(val, (int __user *) argp))
783 case SNDRV_RAWMIDI_STREAM_OUTPUT:
784 if (rfile->output == NULL)
786 return snd_rawmidi_drop_output(rfile->output);
791 case SNDRV_RAWMIDI_IOCTL_DRAIN:
794 if (get_user(val, (int __user *) argp))
797 case SNDRV_RAWMIDI_STREAM_OUTPUT:
798 if (rfile->output == NULL)
800 return snd_rawmidi_drain_output(rfile->output);
801 case SNDRV_RAWMIDI_STREAM_INPUT:
802 if (rfile->input == NULL)
804 return snd_rawmidi_drain_input(rfile->input);
809 #ifdef CONFIG_SND_DEBUG
811 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
817 static int snd_rawmidi_control_ioctl(struct snd_card *card,
818 struct snd_ctl_file *control,
822 void __user *argp = (void __user *)arg;
825 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
829 if (get_user(device, (int __user *)argp))
831 mutex_lock(®ister_mutex);
832 device = device < 0 ? 0 : device + 1;
833 while (device < SNDRV_RAWMIDI_DEVICES) {
834 if (snd_rawmidi_search(card, device))
838 if (device == SNDRV_RAWMIDI_DEVICES)
840 mutex_unlock(®ister_mutex);
841 if (put_user(device, (int __user *)argp))
845 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
849 if (get_user(val, (int __user *)argp))
851 control->prefer_rawmidi_subdevice = val;
854 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
855 return snd_rawmidi_info_select_user(card, argp);
861 * snd_rawmidi_receive - receive the input data from the device
862 * @substream: the rawmidi substream
863 * @buffer: the buffer pointer
864 * @count: the data size to read
866 * Reads the data from the internal buffer.
868 * Returns the size of read data, or a negative error code on failure.
870 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
871 const unsigned char *buffer, int count)
874 int result = 0, count1;
875 struct snd_rawmidi_runtime *runtime = substream->runtime;
877 if (!substream->opened)
879 if (runtime->buffer == NULL) {
880 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
883 spin_lock_irqsave(&runtime->lock, flags);
884 if (count == 1) { /* special case, faster code */
886 if (runtime->avail < runtime->buffer_size) {
887 runtime->buffer[runtime->hw_ptr++] = buffer[0];
888 runtime->hw_ptr %= runtime->buffer_size;
895 substream->bytes += count;
896 count1 = runtime->buffer_size - runtime->hw_ptr;
899 if (count1 > (int)(runtime->buffer_size - runtime->avail))
900 count1 = runtime->buffer_size - runtime->avail;
901 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
902 runtime->hw_ptr += count1;
903 runtime->hw_ptr %= runtime->buffer_size;
904 runtime->avail += count1;
910 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
911 count1 = runtime->buffer_size - runtime->avail;
912 runtime->xruns += count - count1;
915 memcpy(runtime->buffer, buffer, count1);
916 runtime->hw_ptr = count1;
917 runtime->avail += count1;
924 tasklet_schedule(&runtime->tasklet);
925 else if (snd_rawmidi_ready(substream))
926 wake_up(&runtime->sleep);
928 spin_unlock_irqrestore(&runtime->lock, flags);
932 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
933 unsigned char __user *userbuf,
934 unsigned char *kernelbuf, long count)
937 long result = 0, count1;
938 struct snd_rawmidi_runtime *runtime = substream->runtime;
940 while (count > 0 && runtime->avail) {
941 count1 = runtime->buffer_size - runtime->appl_ptr;
944 spin_lock_irqsave(&runtime->lock, flags);
945 if (count1 > (int)runtime->avail)
946 count1 = runtime->avail;
948 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
950 spin_unlock_irqrestore(&runtime->lock, flags);
951 if (copy_to_user(userbuf + result,
952 runtime->buffer + runtime->appl_ptr, count1)) {
953 return result > 0 ? result : -EFAULT;
955 spin_lock_irqsave(&runtime->lock, flags);
957 runtime->appl_ptr += count1;
958 runtime->appl_ptr %= runtime->buffer_size;
959 runtime->avail -= count1;
960 spin_unlock_irqrestore(&runtime->lock, flags);
967 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
968 unsigned char *buf, long count)
970 snd_rawmidi_input_trigger(substream, 1);
971 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
974 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
979 struct snd_rawmidi_file *rfile;
980 struct snd_rawmidi_substream *substream;
981 struct snd_rawmidi_runtime *runtime;
983 rfile = file->private_data;
984 substream = rfile->input;
985 if (substream == NULL)
987 runtime = substream->runtime;
988 snd_rawmidi_input_trigger(substream, 1);
991 spin_lock_irq(&runtime->lock);
992 while (!snd_rawmidi_ready(substream)) {
994 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
995 spin_unlock_irq(&runtime->lock);
996 return result > 0 ? result : -EAGAIN;
998 init_waitqueue_entry(&wait, current);
999 add_wait_queue(&runtime->sleep, &wait);
1000 set_current_state(TASK_INTERRUPTIBLE);
1001 spin_unlock_irq(&runtime->lock);
1003 remove_wait_queue(&runtime->sleep, &wait);
1004 if (signal_pending(current))
1005 return result > 0 ? result : -ERESTARTSYS;
1006 if (!runtime->avail)
1007 return result > 0 ? result : -EIO;
1008 spin_lock_irq(&runtime->lock);
1010 spin_unlock_irq(&runtime->lock);
1011 count1 = snd_rawmidi_kernel_read1(substream,
1012 (unsigned char __user *)buf,
1016 return result > 0 ? result : count1;
1025 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1026 * @substream: the rawmidi substream
1028 * Returns 1 if the internal output buffer is empty, 0 if not.
1030 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1032 struct snd_rawmidi_runtime *runtime = substream->runtime;
1034 unsigned long flags;
1036 if (runtime->buffer == NULL) {
1037 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1040 spin_lock_irqsave(&runtime->lock, flags);
1041 result = runtime->avail >= runtime->buffer_size;
1042 spin_unlock_irqrestore(&runtime->lock, flags);
1047 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1048 * @substream: the rawmidi substream
1049 * @buffer: the buffer pointer
1050 * @count: data size to transfer
1052 * Copies data from the internal output buffer to the given buffer.
1054 * Call this in the interrupt handler when the midi output is ready,
1055 * and call snd_rawmidi_transmit_ack() after the transmission is
1058 * Returns the size of copied data, or a negative error code on failure.
1060 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1061 unsigned char *buffer, int count)
1063 unsigned long flags;
1065 struct snd_rawmidi_runtime *runtime = substream->runtime;
1067 if (runtime->buffer == NULL) {
1068 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1072 spin_lock_irqsave(&runtime->lock, flags);
1073 if (runtime->avail >= runtime->buffer_size) {
1074 /* warning: lowlevel layer MUST trigger down the hardware */
1077 if (count == 1) { /* special case, faster code */
1078 *buffer = runtime->buffer[runtime->hw_ptr];
1081 count1 = runtime->buffer_size - runtime->hw_ptr;
1084 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1085 count1 = runtime->buffer_size - runtime->avail;
1086 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1090 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1091 count = runtime->buffer_size - runtime->avail - count1;
1092 memcpy(buffer + count1, runtime->buffer, count);
1097 spin_unlock_irqrestore(&runtime->lock, flags);
1102 * snd_rawmidi_transmit_ack - acknowledge the transmission
1103 * @substream: the rawmidi substream
1104 * @count: the tranferred count
1106 * Advances the hardware pointer for the internal output buffer with
1107 * the given size and updates the condition.
1108 * Call after the transmission is finished.
1110 * Returns the advanced size if successful, or a negative error code on failure.
1112 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1114 unsigned long flags;
1115 struct snd_rawmidi_runtime *runtime = substream->runtime;
1117 if (runtime->buffer == NULL) {
1118 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1121 spin_lock_irqsave(&runtime->lock, flags);
1122 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1123 runtime->hw_ptr += count;
1124 runtime->hw_ptr %= runtime->buffer_size;
1125 runtime->avail += count;
1126 substream->bytes += count;
1128 if (runtime->drain || snd_rawmidi_ready(substream))
1129 wake_up(&runtime->sleep);
1131 spin_unlock_irqrestore(&runtime->lock, flags);
1136 * snd_rawmidi_transmit - copy from the buffer to the device
1137 * @substream: the rawmidi substream
1138 * @buffer: the buffer pointer
1139 * @count: the data size to transfer
1141 * Copies data from the buffer to the device and advances the pointer.
1143 * Returns the copied size if successful, or a negative error code on failure.
1145 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1146 unsigned char *buffer, int count)
1148 if (!substream->opened)
1150 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1153 return snd_rawmidi_transmit_ack(substream, count);
1156 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1157 const unsigned char __user *userbuf,
1158 const unsigned char *kernelbuf,
1161 unsigned long flags;
1162 long count1, result;
1163 struct snd_rawmidi_runtime *runtime = substream->runtime;
1165 if (snd_BUG_ON(!kernelbuf && !userbuf))
1167 if (snd_BUG_ON(!runtime->buffer))
1171 spin_lock_irqsave(&runtime->lock, flags);
1172 if (substream->append) {
1173 if ((long)runtime->avail < count) {
1174 spin_unlock_irqrestore(&runtime->lock, flags);
1178 while (count > 0 && runtime->avail > 0) {
1179 count1 = runtime->buffer_size - runtime->appl_ptr;
1182 if (count1 > (long)runtime->avail)
1183 count1 = runtime->avail;
1185 memcpy(runtime->buffer + runtime->appl_ptr,
1186 kernelbuf + result, count1);
1188 spin_unlock_irqrestore(&runtime->lock, flags);
1189 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1190 userbuf + result, count1)) {
1191 spin_lock_irqsave(&runtime->lock, flags);
1192 result = result > 0 ? result : -EFAULT;
1195 spin_lock_irqsave(&runtime->lock, flags);
1197 runtime->appl_ptr += count1;
1198 runtime->appl_ptr %= runtime->buffer_size;
1199 runtime->avail -= count1;
1204 count1 = runtime->avail < runtime->buffer_size;
1205 spin_unlock_irqrestore(&runtime->lock, flags);
1207 snd_rawmidi_output_trigger(substream, 1);
1211 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1212 const unsigned char *buf, long count)
1214 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1217 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1218 size_t count, loff_t *offset)
1220 long result, timeout;
1222 struct snd_rawmidi_file *rfile;
1223 struct snd_rawmidi_runtime *runtime;
1224 struct snd_rawmidi_substream *substream;
1226 rfile = file->private_data;
1227 substream = rfile->output;
1228 runtime = substream->runtime;
1229 /* we cannot put an atomic message to our buffer */
1230 if (substream->append && count > runtime->buffer_size)
1234 spin_lock_irq(&runtime->lock);
1235 while (!snd_rawmidi_ready_append(substream, count)) {
1237 if (file->f_flags & O_NONBLOCK) {
1238 spin_unlock_irq(&runtime->lock);
1239 return result > 0 ? result : -EAGAIN;
1241 init_waitqueue_entry(&wait, current);
1242 add_wait_queue(&runtime->sleep, &wait);
1243 set_current_state(TASK_INTERRUPTIBLE);
1244 spin_unlock_irq(&runtime->lock);
1245 timeout = schedule_timeout(30 * HZ);
1246 remove_wait_queue(&runtime->sleep, &wait);
1247 if (signal_pending(current))
1248 return result > 0 ? result : -ERESTARTSYS;
1249 if (!runtime->avail && !timeout)
1250 return result > 0 ? result : -EIO;
1251 spin_lock_irq(&runtime->lock);
1253 spin_unlock_irq(&runtime->lock);
1254 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1256 return result > 0 ? result : count1;
1259 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1263 if (file->f_flags & O_SYNC) {
1264 spin_lock_irq(&runtime->lock);
1265 while (runtime->avail != runtime->buffer_size) {
1267 unsigned int last_avail = runtime->avail;
1268 init_waitqueue_entry(&wait, current);
1269 add_wait_queue(&runtime->sleep, &wait);
1270 set_current_state(TASK_INTERRUPTIBLE);
1271 spin_unlock_irq(&runtime->lock);
1272 timeout = schedule_timeout(30 * HZ);
1273 remove_wait_queue(&runtime->sleep, &wait);
1274 if (signal_pending(current))
1275 return result > 0 ? result : -ERESTARTSYS;
1276 if (runtime->avail == last_avail && !timeout)
1277 return result > 0 ? result : -EIO;
1278 spin_lock_irq(&runtime->lock);
1280 spin_unlock_irq(&runtime->lock);
1285 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1287 struct snd_rawmidi_file *rfile;
1288 struct snd_rawmidi_runtime *runtime;
1291 rfile = file->private_data;
1292 if (rfile->input != NULL) {
1293 runtime = rfile->input->runtime;
1294 snd_rawmidi_input_trigger(rfile->input, 1);
1295 poll_wait(file, &runtime->sleep, wait);
1297 if (rfile->output != NULL) {
1298 runtime = rfile->output->runtime;
1299 poll_wait(file, &runtime->sleep, wait);
1302 if (rfile->input != NULL) {
1303 if (snd_rawmidi_ready(rfile->input))
1304 mask |= POLLIN | POLLRDNORM;
1306 if (rfile->output != NULL) {
1307 if (snd_rawmidi_ready(rfile->output))
1308 mask |= POLLOUT | POLLWRNORM;
1315 #ifdef CONFIG_COMPAT
1316 #include "rawmidi_compat.c"
1318 #define snd_rawmidi_ioctl_compat NULL
1325 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1326 struct snd_info_buffer *buffer)
1328 struct snd_rawmidi *rmidi;
1329 struct snd_rawmidi_substream *substream;
1330 struct snd_rawmidi_runtime *runtime;
1332 rmidi = entry->private_data;
1333 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1334 mutex_lock(&rmidi->open_mutex);
1335 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1336 list_for_each_entry(substream,
1337 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1341 " Tx bytes : %lu\n",
1343 (unsigned long) substream->bytes);
1344 if (substream->opened) {
1345 runtime = substream->runtime;
1348 " Buffer size : %lu\n"
1350 runtime->oss ? "OSS compatible" : "native",
1351 (unsigned long) runtime->buffer_size,
1352 (unsigned long) runtime->avail);
1356 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1357 list_for_each_entry(substream,
1358 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1362 " Rx bytes : %lu\n",
1364 (unsigned long) substream->bytes);
1365 if (substream->opened) {
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 .poll = snd_rawmidi_poll,
1392 .unlocked_ioctl = snd_rawmidi_ioctl,
1393 .compat_ioctl = snd_rawmidi_ioctl_compat,
1396 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1397 struct snd_rawmidi_str *stream,
1401 struct snd_rawmidi_substream *substream;
1404 for (idx = 0; idx < count; idx++) {
1405 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1406 if (substream == NULL) {
1407 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1410 substream->stream = direction;
1411 substream->number = idx;
1412 substream->rmidi = rmidi;
1413 substream->pstr = stream;
1414 list_add_tail(&substream->list, &stream->substreams);
1415 stream->substream_count++;
1421 * snd_rawmidi_new - create a rawmidi instance
1422 * @card: the card instance
1423 * @id: the id string
1424 * @device: the device index
1425 * @output_count: the number of output streams
1426 * @input_count: the number of input streams
1427 * @rrawmidi: the pointer to store the new rawmidi instance
1429 * Creates a new rawmidi instance.
1430 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1432 * Returns zero if successful, or a negative error code on failure.
1434 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1435 int output_count, int input_count,
1436 struct snd_rawmidi ** rrawmidi)
1438 struct snd_rawmidi *rmidi;
1440 static struct snd_device_ops ops = {
1441 .dev_free = snd_rawmidi_dev_free,
1442 .dev_register = snd_rawmidi_dev_register,
1443 .dev_disconnect = snd_rawmidi_dev_disconnect,
1446 if (snd_BUG_ON(!card))
1450 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1451 if (rmidi == NULL) {
1452 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1456 rmidi->device = device;
1457 mutex_init(&rmidi->open_mutex);
1458 init_waitqueue_head(&rmidi->open_wait);
1459 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1460 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1463 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1464 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1465 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1466 SNDRV_RAWMIDI_STREAM_INPUT,
1467 input_count)) < 0) {
1468 snd_rawmidi_free(rmidi);
1471 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1472 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1473 SNDRV_RAWMIDI_STREAM_OUTPUT,
1474 output_count)) < 0) {
1475 snd_rawmidi_free(rmidi);
1478 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1479 snd_rawmidi_free(rmidi);
1487 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1489 struct snd_rawmidi_substream *substream;
1491 while (!list_empty(&stream->substreams)) {
1492 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1493 list_del(&substream->list);
1498 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1503 snd_info_free_entry(rmidi->proc_entry);
1504 rmidi->proc_entry = NULL;
1505 mutex_lock(®ister_mutex);
1506 if (rmidi->ops && rmidi->ops->dev_unregister)
1507 rmidi->ops->dev_unregister(rmidi);
1508 mutex_unlock(®ister_mutex);
1510 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1511 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1512 if (rmidi->private_free)
1513 rmidi->private_free(rmidi);
1518 static int snd_rawmidi_dev_free(struct snd_device *device)
1520 struct snd_rawmidi *rmidi = device->device_data;
1521 return snd_rawmidi_free(rmidi);
1524 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1525 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1527 struct snd_rawmidi *rmidi = device->private_data;
1528 rmidi->seq_dev = NULL;
1532 static int snd_rawmidi_dev_register(struct snd_device *device)
1535 struct snd_info_entry *entry;
1537 struct snd_rawmidi *rmidi = device->device_data;
1539 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1541 mutex_lock(®ister_mutex);
1542 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1543 mutex_unlock(®ister_mutex);
1546 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1547 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1548 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1549 rmidi->card, rmidi->device,
1550 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1551 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1552 list_del(&rmidi->list);
1553 mutex_unlock(®ister_mutex);
1556 if (rmidi->ops && rmidi->ops->dev_register &&
1557 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1558 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1559 list_del(&rmidi->list);
1560 mutex_unlock(®ister_mutex);
1563 #ifdef CONFIG_SND_OSSEMUL
1565 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1566 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1567 rmidi->card, 0, &snd_rawmidi_f_ops,
1569 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1572 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1573 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1577 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1578 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1579 rmidi->card, 1, &snd_rawmidi_f_ops,
1581 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1586 #endif /* CONFIG_SND_OSSEMUL */
1587 mutex_unlock(®ister_mutex);
1588 sprintf(name, "midi%d", rmidi->device);
1589 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1591 entry->private_data = rmidi;
1592 entry->c.text.read = snd_rawmidi_proc_info_read;
1593 if (snd_info_register(entry) < 0) {
1594 snd_info_free_entry(entry);
1598 rmidi->proc_entry = entry;
1599 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1600 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1601 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1602 rmidi->seq_dev->private_data = rmidi;
1603 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1604 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1605 snd_device_register(rmidi->card, rmidi->seq_dev);
1612 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1614 struct snd_rawmidi *rmidi = device->device_data;
1616 mutex_lock(®ister_mutex);
1617 list_del_init(&rmidi->list);
1618 #ifdef CONFIG_SND_OSSEMUL
1619 if (rmidi->ossreg) {
1620 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1621 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1622 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1623 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1626 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1627 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1630 #endif /* CONFIG_SND_OSSEMUL */
1631 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1632 mutex_unlock(®ister_mutex);
1637 * snd_rawmidi_set_ops - set the rawmidi operators
1638 * @rmidi: the rawmidi instance
1639 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1640 * @ops: the operator table
1642 * Sets the rawmidi operators for the given stream direction.
1644 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1645 struct snd_rawmidi_ops *ops)
1647 struct snd_rawmidi_substream *substream;
1649 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1650 substream->ops = ops;
1657 static int __init alsa_rawmidi_init(void)
1660 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1661 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1662 #ifdef CONFIG_SND_OSSEMUL
1664 /* check device map table */
1665 for (i = 0; i < SNDRV_CARDS; i++) {
1666 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1667 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1670 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1671 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1676 #endif /* CONFIG_SND_OSSEMUL */
1680 static void __exit alsa_rawmidi_exit(void)
1682 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1683 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1686 module_init(alsa_rawmidi_init)
1687 module_exit(alsa_rawmidi_exit)
1689 EXPORT_SYMBOL(snd_rawmidi_output_params);
1690 EXPORT_SYMBOL(snd_rawmidi_input_params);
1691 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1692 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1693 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1694 EXPORT_SYMBOL(snd_rawmidi_receive);
1695 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1696 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1697 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1698 EXPORT_SYMBOL(snd_rawmidi_transmit);
1699 EXPORT_SYMBOL(snd_rawmidi_new);
1700 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1701 EXPORT_SYMBOL(snd_rawmidi_info_select);
1702 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1703 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1704 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1705 EXPORT_SYMBOL(snd_rawmidi_kernel_write);