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 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
580 struct snd_rawmidi *rmidi;
581 struct snd_rawmidi_str *pstr;
582 struct snd_rawmidi_substream *substream;
584 mutex_lock(®ister_mutex);
585 rmidi = snd_rawmidi_search(card, info->device);
586 mutex_unlock(®ister_mutex);
589 if (info->stream < 0 || info->stream > 1)
591 pstr = &rmidi->streams[info->stream];
592 if (pstr->substream_count == 0)
594 if (info->subdevice >= pstr->substream_count)
596 list_for_each_entry(substream, &pstr->substreams, list) {
597 if ((unsigned int)substream->number == info->subdevice)
598 return snd_rawmidi_info(substream, info);
603 static int snd_rawmidi_info_select_user(struct snd_card *card,
604 struct snd_rawmidi_info __user *_info)
607 struct snd_rawmidi_info info;
608 if (get_user(info.device, &_info->device))
610 if (get_user(info.stream, &_info->stream))
612 if (get_user(info.subdevice, &_info->subdevice))
614 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
616 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
621 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
622 struct snd_rawmidi_params * params)
625 struct snd_rawmidi_runtime *runtime = substream->runtime;
627 if (substream->append && substream->use_count > 1)
629 snd_rawmidi_drain_output(substream);
630 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
633 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
636 if (params->buffer_size != runtime->buffer_size) {
637 newbuf = krealloc(runtime->buffer, params->buffer_size,
641 runtime->buffer = newbuf;
642 runtime->buffer_size = params->buffer_size;
643 runtime->avail = runtime->buffer_size;
645 runtime->avail_min = params->avail_min;
646 substream->active_sensing = !params->no_active_sensing;
650 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
651 struct snd_rawmidi_params * params)
654 struct snd_rawmidi_runtime *runtime = substream->runtime;
656 snd_rawmidi_drain_input(substream);
657 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
660 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
663 if (params->buffer_size != runtime->buffer_size) {
664 newbuf = krealloc(runtime->buffer, params->buffer_size,
668 runtime->buffer = newbuf;
669 runtime->buffer_size = params->buffer_size;
671 runtime->avail_min = params->avail_min;
675 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
676 struct snd_rawmidi_status * status)
678 struct snd_rawmidi_runtime *runtime = substream->runtime;
680 memset(status, 0, sizeof(*status));
681 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
682 spin_lock_irq(&runtime->lock);
683 status->avail = runtime->avail;
684 spin_unlock_irq(&runtime->lock);
688 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
689 struct snd_rawmidi_status * status)
691 struct snd_rawmidi_runtime *runtime = substream->runtime;
693 memset(status, 0, sizeof(*status));
694 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
695 spin_lock_irq(&runtime->lock);
696 status->avail = runtime->avail;
697 status->xruns = runtime->xruns;
699 spin_unlock_irq(&runtime->lock);
703 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
705 struct snd_rawmidi_file *rfile;
706 void __user *argp = (void __user *)arg;
708 rfile = file->private_data;
709 if (((cmd >> 8) & 0xff) != 'W')
712 case SNDRV_RAWMIDI_IOCTL_PVERSION:
713 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
714 case SNDRV_RAWMIDI_IOCTL_INFO:
717 struct snd_rawmidi_info __user *info = argp;
718 if (get_user(stream, &info->stream))
721 case SNDRV_RAWMIDI_STREAM_INPUT:
722 return snd_rawmidi_info_user(rfile->input, info);
723 case SNDRV_RAWMIDI_STREAM_OUTPUT:
724 return snd_rawmidi_info_user(rfile->output, info);
729 case SNDRV_RAWMIDI_IOCTL_PARAMS:
731 struct snd_rawmidi_params params;
732 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
734 switch (params.stream) {
735 case SNDRV_RAWMIDI_STREAM_OUTPUT:
736 if (rfile->output == NULL)
738 return snd_rawmidi_output_params(rfile->output, ¶ms);
739 case SNDRV_RAWMIDI_STREAM_INPUT:
740 if (rfile->input == NULL)
742 return snd_rawmidi_input_params(rfile->input, ¶ms);
747 case SNDRV_RAWMIDI_IOCTL_STATUS:
750 struct snd_rawmidi_status status;
751 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
753 switch (status.stream) {
754 case SNDRV_RAWMIDI_STREAM_OUTPUT:
755 if (rfile->output == NULL)
757 err = snd_rawmidi_output_status(rfile->output, &status);
759 case SNDRV_RAWMIDI_STREAM_INPUT:
760 if (rfile->input == NULL)
762 err = snd_rawmidi_input_status(rfile->input, &status);
769 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
773 case SNDRV_RAWMIDI_IOCTL_DROP:
776 if (get_user(val, (int __user *) argp))
779 case SNDRV_RAWMIDI_STREAM_OUTPUT:
780 if (rfile->output == NULL)
782 return snd_rawmidi_drop_output(rfile->output);
787 case SNDRV_RAWMIDI_IOCTL_DRAIN:
790 if (get_user(val, (int __user *) argp))
793 case SNDRV_RAWMIDI_STREAM_OUTPUT:
794 if (rfile->output == NULL)
796 return snd_rawmidi_drain_output(rfile->output);
797 case SNDRV_RAWMIDI_STREAM_INPUT:
798 if (rfile->input == NULL)
800 return snd_rawmidi_drain_input(rfile->input);
805 #ifdef CONFIG_SND_DEBUG
807 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
813 static int snd_rawmidi_control_ioctl(struct snd_card *card,
814 struct snd_ctl_file *control,
818 void __user *argp = (void __user *)arg;
821 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
825 if (get_user(device, (int __user *)argp))
827 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
828 device = SNDRV_RAWMIDI_DEVICES - 1;
829 mutex_lock(®ister_mutex);
830 device = device < 0 ? 0 : device + 1;
831 while (device < SNDRV_RAWMIDI_DEVICES) {
832 if (snd_rawmidi_search(card, device))
836 if (device == SNDRV_RAWMIDI_DEVICES)
838 mutex_unlock(®ister_mutex);
839 if (put_user(device, (int __user *)argp))
843 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
847 if (get_user(val, (int __user *)argp))
849 control->prefer_rawmidi_subdevice = val;
852 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
853 return snd_rawmidi_info_select_user(card, argp);
859 * snd_rawmidi_receive - receive the input data from the device
860 * @substream: the rawmidi substream
861 * @buffer: the buffer pointer
862 * @count: the data size to read
864 * Reads the data from the internal buffer.
866 * Returns the size of read data, or a negative error code on failure.
868 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
869 const unsigned char *buffer, int count)
872 int result = 0, count1;
873 struct snd_rawmidi_runtime *runtime = substream->runtime;
875 if (!substream->opened)
877 if (runtime->buffer == NULL) {
878 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
881 spin_lock_irqsave(&runtime->lock, flags);
882 if (count == 1) { /* special case, faster code */
884 if (runtime->avail < runtime->buffer_size) {
885 runtime->buffer[runtime->hw_ptr++] = buffer[0];
886 runtime->hw_ptr %= runtime->buffer_size;
893 substream->bytes += count;
894 count1 = runtime->buffer_size - runtime->hw_ptr;
897 if (count1 > (int)(runtime->buffer_size - runtime->avail))
898 count1 = runtime->buffer_size - runtime->avail;
899 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
900 runtime->hw_ptr += count1;
901 runtime->hw_ptr %= runtime->buffer_size;
902 runtime->avail += count1;
908 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
909 count1 = runtime->buffer_size - runtime->avail;
910 runtime->xruns += count - count1;
913 memcpy(runtime->buffer, buffer, count1);
914 runtime->hw_ptr = count1;
915 runtime->avail += count1;
922 schedule_work(&runtime->event_work);
923 else if (snd_rawmidi_ready(substream))
924 wake_up(&runtime->sleep);
926 spin_unlock_irqrestore(&runtime->lock, flags);
930 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
931 unsigned char __user *userbuf,
932 unsigned char *kernelbuf, long count)
935 long result = 0, count1;
936 struct snd_rawmidi_runtime *runtime = substream->runtime;
937 unsigned long appl_ptr;
939 spin_lock_irqsave(&runtime->lock, flags);
940 while (count > 0 && runtime->avail) {
941 count1 = runtime->buffer_size - runtime->appl_ptr;
944 if (count1 > (int)runtime->avail)
945 count1 = runtime->avail;
947 /* update runtime->appl_ptr before unlocking for userbuf */
948 appl_ptr = runtime->appl_ptr;
949 runtime->appl_ptr += count1;
950 runtime->appl_ptr %= runtime->buffer_size;
951 runtime->avail -= count1;
954 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
956 spin_unlock_irqrestore(&runtime->lock, flags);
957 if (copy_to_user(userbuf + result,
958 runtime->buffer + appl_ptr, count1)) {
959 return result > 0 ? result : -EFAULT;
961 spin_lock_irqsave(&runtime->lock, flags);
966 spin_unlock_irqrestore(&runtime->lock, flags);
970 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
971 unsigned char *buf, long count)
973 snd_rawmidi_input_trigger(substream, 1);
974 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
977 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
982 struct snd_rawmidi_file *rfile;
983 struct snd_rawmidi_substream *substream;
984 struct snd_rawmidi_runtime *runtime;
986 rfile = file->private_data;
987 substream = rfile->input;
988 if (substream == NULL)
990 runtime = substream->runtime;
991 snd_rawmidi_input_trigger(substream, 1);
994 spin_lock_irq(&runtime->lock);
995 while (!snd_rawmidi_ready(substream)) {
997 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
998 spin_unlock_irq(&runtime->lock);
999 return result > 0 ? result : -EAGAIN;
1001 init_waitqueue_entry(&wait, current);
1002 add_wait_queue(&runtime->sleep, &wait);
1003 set_current_state(TASK_INTERRUPTIBLE);
1004 spin_unlock_irq(&runtime->lock);
1006 remove_wait_queue(&runtime->sleep, &wait);
1007 if (rfile->rmidi->card->shutdown)
1009 if (signal_pending(current))
1010 return result > 0 ? result : -ERESTARTSYS;
1011 if (!runtime->avail)
1012 return result > 0 ? result : -EIO;
1013 spin_lock_irq(&runtime->lock);
1015 spin_unlock_irq(&runtime->lock);
1016 count1 = snd_rawmidi_kernel_read1(substream,
1017 (unsigned char __user *)buf,
1021 return result > 0 ? result : count1;
1030 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1031 * @substream: the rawmidi substream
1033 * Returns 1 if the internal output buffer is empty, 0 if not.
1035 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1037 struct snd_rawmidi_runtime *runtime = substream->runtime;
1039 unsigned long flags;
1041 if (runtime->buffer == NULL) {
1042 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1045 spin_lock_irqsave(&runtime->lock, flags);
1046 result = runtime->avail >= runtime->buffer_size;
1047 spin_unlock_irqrestore(&runtime->lock, flags);
1052 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1053 * @substream: the rawmidi substream
1054 * @buffer: the buffer pointer
1055 * @count: data size to transfer
1057 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1059 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1060 unsigned char *buffer, int count)
1063 struct snd_rawmidi_runtime *runtime = substream->runtime;
1065 if (runtime->buffer == NULL) {
1066 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1070 if (runtime->avail >= runtime->buffer_size) {
1071 /* warning: lowlevel layer MUST trigger down the hardware */
1074 if (count == 1) { /* special case, faster code */
1075 *buffer = runtime->buffer[runtime->hw_ptr];
1078 count1 = runtime->buffer_size - runtime->hw_ptr;
1081 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1082 count1 = runtime->buffer_size - runtime->avail;
1083 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1087 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1088 count = runtime->buffer_size - runtime->avail - count1;
1089 memcpy(buffer + count1, runtime->buffer, count);
1096 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1099 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1100 * @substream: the rawmidi substream
1101 * @buffer: the buffer pointer
1102 * @count: data size to transfer
1104 * Copies data from the internal output buffer to the given buffer.
1106 * Call this in the interrupt handler when the midi output is ready,
1107 * and call snd_rawmidi_transmit_ack() after the transmission is
1110 * Return: The size of copied data, or a negative error code on failure.
1112 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1113 unsigned char *buffer, int count)
1115 struct snd_rawmidi_runtime *runtime = substream->runtime;
1117 unsigned long flags;
1119 spin_lock_irqsave(&runtime->lock, flags);
1120 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1121 spin_unlock_irqrestore(&runtime->lock, flags);
1126 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1127 * @substream: the rawmidi substream
1128 * @count: the tranferred count
1130 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1132 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1134 struct snd_rawmidi_runtime *runtime = substream->runtime;
1136 if (runtime->buffer == NULL) {
1137 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1140 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1141 runtime->hw_ptr += count;
1142 runtime->hw_ptr %= runtime->buffer_size;
1143 runtime->avail += count;
1144 substream->bytes += count;
1146 if (runtime->drain || snd_rawmidi_ready(substream))
1147 wake_up(&runtime->sleep);
1151 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1154 * snd_rawmidi_transmit_ack - acknowledge the transmission
1155 * @substream: the rawmidi substream
1156 * @count: the transferred count
1158 * Advances the hardware pointer for the internal output buffer with
1159 * the given size and updates the condition.
1160 * Call after the transmission is finished.
1162 * Return: The advanced size if successful, or a negative error code on failure.
1164 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1166 struct snd_rawmidi_runtime *runtime = substream->runtime;
1168 unsigned long flags;
1170 spin_lock_irqsave(&runtime->lock, flags);
1171 result = __snd_rawmidi_transmit_ack(substream, count);
1172 spin_unlock_irqrestore(&runtime->lock, flags);
1177 * snd_rawmidi_transmit - copy from the buffer to the device
1178 * @substream: the rawmidi substream
1179 * @buffer: the buffer pointer
1180 * @count: the data size to transfer
1182 * Copies data from the buffer to the device and advances the pointer.
1184 * Returns the copied size if successful, or a negative error code on failure.
1186 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1187 unsigned char *buffer, int count)
1189 struct snd_rawmidi_runtime *runtime = substream->runtime;
1191 unsigned long flags;
1193 spin_lock_irqsave(&runtime->lock, flags);
1194 if (!substream->opened)
1197 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1201 result = __snd_rawmidi_transmit_ack(substream, count);
1203 spin_unlock_irqrestore(&runtime->lock, flags);
1207 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1208 const unsigned char __user *userbuf,
1209 const unsigned char *kernelbuf,
1212 unsigned long flags;
1213 long count1, result;
1214 struct snd_rawmidi_runtime *runtime = substream->runtime;
1215 unsigned long appl_ptr;
1217 if (!kernelbuf && !userbuf)
1219 if (snd_BUG_ON(!runtime->buffer))
1223 spin_lock_irqsave(&runtime->lock, flags);
1224 if (substream->append) {
1225 if ((long)runtime->avail < count) {
1226 spin_unlock_irqrestore(&runtime->lock, flags);
1230 while (count > 0 && runtime->avail > 0) {
1231 count1 = runtime->buffer_size - runtime->appl_ptr;
1234 if (count1 > (long)runtime->avail)
1235 count1 = runtime->avail;
1237 /* update runtime->appl_ptr before unlocking for userbuf */
1238 appl_ptr = runtime->appl_ptr;
1239 runtime->appl_ptr += count1;
1240 runtime->appl_ptr %= runtime->buffer_size;
1241 runtime->avail -= count1;
1244 memcpy(runtime->buffer + appl_ptr,
1245 kernelbuf + result, count1);
1247 spin_unlock_irqrestore(&runtime->lock, flags);
1248 if (copy_from_user(runtime->buffer + appl_ptr,
1249 userbuf + result, count1)) {
1250 spin_lock_irqsave(&runtime->lock, flags);
1251 result = result > 0 ? result : -EFAULT;
1254 spin_lock_irqsave(&runtime->lock, flags);
1260 count1 = runtime->avail < runtime->buffer_size;
1261 spin_unlock_irqrestore(&runtime->lock, flags);
1263 snd_rawmidi_output_trigger(substream, 1);
1267 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1268 const unsigned char *buf, long count)
1270 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1273 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1274 size_t count, loff_t *offset)
1276 long result, timeout;
1278 struct snd_rawmidi_file *rfile;
1279 struct snd_rawmidi_runtime *runtime;
1280 struct snd_rawmidi_substream *substream;
1282 rfile = file->private_data;
1283 substream = rfile->output;
1284 runtime = substream->runtime;
1285 /* we cannot put an atomic message to our buffer */
1286 if (substream->append && count > runtime->buffer_size)
1290 spin_lock_irq(&runtime->lock);
1291 while (!snd_rawmidi_ready_append(substream, count)) {
1293 if (file->f_flags & O_NONBLOCK) {
1294 spin_unlock_irq(&runtime->lock);
1295 return result > 0 ? result : -EAGAIN;
1297 init_waitqueue_entry(&wait, current);
1298 add_wait_queue(&runtime->sleep, &wait);
1299 set_current_state(TASK_INTERRUPTIBLE);
1300 spin_unlock_irq(&runtime->lock);
1301 timeout = schedule_timeout(30 * HZ);
1302 remove_wait_queue(&runtime->sleep, &wait);
1303 if (rfile->rmidi->card->shutdown)
1305 if (signal_pending(current))
1306 return result > 0 ? result : -ERESTARTSYS;
1307 if (!runtime->avail && !timeout)
1308 return result > 0 ? result : -EIO;
1309 spin_lock_irq(&runtime->lock);
1311 spin_unlock_irq(&runtime->lock);
1312 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1314 return result > 0 ? result : count1;
1317 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1321 if (file->f_flags & O_DSYNC) {
1322 spin_lock_irq(&runtime->lock);
1323 while (runtime->avail != runtime->buffer_size) {
1325 unsigned int last_avail = runtime->avail;
1326 init_waitqueue_entry(&wait, current);
1327 add_wait_queue(&runtime->sleep, &wait);
1328 set_current_state(TASK_INTERRUPTIBLE);
1329 spin_unlock_irq(&runtime->lock);
1330 timeout = schedule_timeout(30 * HZ);
1331 remove_wait_queue(&runtime->sleep, &wait);
1332 if (signal_pending(current))
1333 return result > 0 ? result : -ERESTARTSYS;
1334 if (runtime->avail == last_avail && !timeout)
1335 return result > 0 ? result : -EIO;
1336 spin_lock_irq(&runtime->lock);
1338 spin_unlock_irq(&runtime->lock);
1343 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1345 struct snd_rawmidi_file *rfile;
1346 struct snd_rawmidi_runtime *runtime;
1349 rfile = file->private_data;
1350 if (rfile->input != NULL) {
1351 runtime = rfile->input->runtime;
1352 snd_rawmidi_input_trigger(rfile->input, 1);
1353 poll_wait(file, &runtime->sleep, wait);
1355 if (rfile->output != NULL) {
1356 runtime = rfile->output->runtime;
1357 poll_wait(file, &runtime->sleep, wait);
1360 if (rfile->input != NULL) {
1361 if (snd_rawmidi_ready(rfile->input))
1362 mask |= POLLIN | POLLRDNORM;
1364 if (rfile->output != NULL) {
1365 if (snd_rawmidi_ready(rfile->output))
1366 mask |= POLLOUT | POLLWRNORM;
1373 #ifdef CONFIG_COMPAT
1374 #include "rawmidi_compat.c"
1376 #define snd_rawmidi_ioctl_compat NULL
1383 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1384 struct snd_info_buffer *buffer)
1386 struct snd_rawmidi *rmidi;
1387 struct snd_rawmidi_substream *substream;
1388 struct snd_rawmidi_runtime *runtime;
1390 rmidi = entry->private_data;
1391 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1392 mutex_lock(&rmidi->open_mutex);
1393 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1394 list_for_each_entry(substream,
1395 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1399 " Tx bytes : %lu\n",
1401 (unsigned long) substream->bytes);
1402 if (substream->opened) {
1404 " Owner PID : %d\n",
1405 pid_vnr(substream->pid));
1406 runtime = substream->runtime;
1409 " Buffer size : %lu\n"
1411 runtime->oss ? "OSS compatible" : "native",
1412 (unsigned long) runtime->buffer_size,
1413 (unsigned long) runtime->avail);
1417 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1418 list_for_each_entry(substream,
1419 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1423 " Rx bytes : %lu\n",
1425 (unsigned long) substream->bytes);
1426 if (substream->opened) {
1428 " Owner PID : %d\n",
1429 pid_vnr(substream->pid));
1430 runtime = substream->runtime;
1432 " Buffer size : %lu\n"
1434 " Overruns : %lu\n",
1435 (unsigned long) runtime->buffer_size,
1436 (unsigned long) runtime->avail,
1437 (unsigned long) runtime->xruns);
1441 mutex_unlock(&rmidi->open_mutex);
1445 * Register functions
1448 static const struct file_operations snd_rawmidi_f_ops =
1450 .owner = THIS_MODULE,
1451 .read = snd_rawmidi_read,
1452 .write = snd_rawmidi_write,
1453 .open = snd_rawmidi_open,
1454 .release = snd_rawmidi_release,
1455 .llseek = no_llseek,
1456 .poll = snd_rawmidi_poll,
1457 .unlocked_ioctl = snd_rawmidi_ioctl,
1458 .compat_ioctl = snd_rawmidi_ioctl_compat,
1461 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1462 struct snd_rawmidi_str *stream,
1466 struct snd_rawmidi_substream *substream;
1469 for (idx = 0; idx < count; idx++) {
1470 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1471 if (substream == NULL) {
1472 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1475 substream->stream = direction;
1476 substream->number = idx;
1477 substream->rmidi = rmidi;
1478 substream->pstr = stream;
1479 list_add_tail(&substream->list, &stream->substreams);
1480 stream->substream_count++;
1486 * snd_rawmidi_new - create a rawmidi instance
1487 * @card: the card instance
1488 * @id: the id string
1489 * @device: the device index
1490 * @output_count: the number of output streams
1491 * @input_count: the number of input streams
1492 * @rrawmidi: the pointer to store the new rawmidi instance
1494 * Creates a new rawmidi instance.
1495 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1497 * Returns zero if successful, or a negative error code on failure.
1499 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1500 int output_count, int input_count,
1501 struct snd_rawmidi ** rrawmidi)
1503 struct snd_rawmidi *rmidi;
1505 static struct snd_device_ops ops = {
1506 .dev_free = snd_rawmidi_dev_free,
1507 .dev_register = snd_rawmidi_dev_register,
1508 .dev_disconnect = snd_rawmidi_dev_disconnect,
1511 if (snd_BUG_ON(!card))
1515 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1516 if (rmidi == NULL) {
1517 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1521 rmidi->device = device;
1522 mutex_init(&rmidi->open_mutex);
1523 init_waitqueue_head(&rmidi->open_wait);
1524 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1525 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1528 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1529 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1530 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1531 SNDRV_RAWMIDI_STREAM_INPUT,
1532 input_count)) < 0) {
1533 snd_rawmidi_free(rmidi);
1536 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1537 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1538 SNDRV_RAWMIDI_STREAM_OUTPUT,
1539 output_count)) < 0) {
1540 snd_rawmidi_free(rmidi);
1543 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1544 snd_rawmidi_free(rmidi);
1552 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1554 struct snd_rawmidi_substream *substream;
1556 while (!list_empty(&stream->substreams)) {
1557 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1558 list_del(&substream->list);
1563 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1568 snd_info_free_entry(rmidi->proc_entry);
1569 rmidi->proc_entry = NULL;
1570 mutex_lock(®ister_mutex);
1571 if (rmidi->ops && rmidi->ops->dev_unregister)
1572 rmidi->ops->dev_unregister(rmidi);
1573 mutex_unlock(®ister_mutex);
1575 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1576 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1577 if (rmidi->private_free)
1578 rmidi->private_free(rmidi);
1583 static int snd_rawmidi_dev_free(struct snd_device *device)
1585 struct snd_rawmidi *rmidi = device->device_data;
1586 return snd_rawmidi_free(rmidi);
1589 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1590 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1592 struct snd_rawmidi *rmidi = device->private_data;
1593 rmidi->seq_dev = NULL;
1597 static int snd_rawmidi_dev_register(struct snd_device *device)
1600 struct snd_info_entry *entry;
1602 struct snd_rawmidi *rmidi = device->device_data;
1604 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1606 mutex_lock(®ister_mutex);
1607 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1608 mutex_unlock(®ister_mutex);
1611 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1612 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1613 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1614 rmidi->card, rmidi->device,
1615 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1616 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1617 list_del(&rmidi->list);
1618 mutex_unlock(®ister_mutex);
1621 if (rmidi->ops && rmidi->ops->dev_register &&
1622 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1623 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1624 list_del(&rmidi->list);
1625 mutex_unlock(®ister_mutex);
1628 #ifdef CONFIG_SND_OSSEMUL
1630 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1631 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1632 rmidi->card, 0, &snd_rawmidi_f_ops,
1634 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1637 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1638 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1642 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1643 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1644 rmidi->card, 1, &snd_rawmidi_f_ops,
1646 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1651 #endif /* CONFIG_SND_OSSEMUL */
1652 mutex_unlock(®ister_mutex);
1653 sprintf(name, "midi%d", rmidi->device);
1654 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1656 entry->private_data = rmidi;
1657 entry->c.text.read = snd_rawmidi_proc_info_read;
1658 if (snd_info_register(entry) < 0) {
1659 snd_info_free_entry(entry);
1663 rmidi->proc_entry = entry;
1664 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1665 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1666 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1667 rmidi->seq_dev->private_data = rmidi;
1668 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1669 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1670 snd_device_register(rmidi->card, rmidi->seq_dev);
1677 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1679 struct snd_rawmidi *rmidi = device->device_data;
1682 mutex_lock(®ister_mutex);
1683 mutex_lock(&rmidi->open_mutex);
1684 wake_up(&rmidi->open_wait);
1685 list_del_init(&rmidi->list);
1686 for (dir = 0; dir < 2; dir++) {
1687 struct snd_rawmidi_substream *s;
1688 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1690 wake_up(&s->runtime->sleep);
1694 #ifdef CONFIG_SND_OSSEMUL
1695 if (rmidi->ossreg) {
1696 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1697 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1698 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1699 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1702 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1703 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1706 #endif /* CONFIG_SND_OSSEMUL */
1707 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1708 mutex_unlock(&rmidi->open_mutex);
1709 mutex_unlock(®ister_mutex);
1714 * snd_rawmidi_set_ops - set the rawmidi operators
1715 * @rmidi: the rawmidi instance
1716 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1717 * @ops: the operator table
1719 * Sets the rawmidi operators for the given stream direction.
1721 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1722 struct snd_rawmidi_ops *ops)
1724 struct snd_rawmidi_substream *substream;
1726 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1727 substream->ops = ops;
1734 static int __init alsa_rawmidi_init(void)
1737 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1738 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1739 #ifdef CONFIG_SND_OSSEMUL
1741 /* check device map table */
1742 for (i = 0; i < SNDRV_CARDS; i++) {
1743 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1744 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1747 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1748 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1753 #endif /* CONFIG_SND_OSSEMUL */
1757 static void __exit alsa_rawmidi_exit(void)
1759 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1760 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1763 module_init(alsa_rawmidi_init)
1764 module_exit(alsa_rawmidi_exit)
1766 EXPORT_SYMBOL(snd_rawmidi_output_params);
1767 EXPORT_SYMBOL(snd_rawmidi_input_params);
1768 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1769 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1770 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1771 EXPORT_SYMBOL(snd_rawmidi_receive);
1772 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1773 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1774 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1775 EXPORT_SYMBOL(snd_rawmidi_transmit);
1776 EXPORT_SYMBOL(snd_rawmidi_new);
1777 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1778 EXPORT_SYMBOL(snd_rawmidi_info_select);
1779 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1780 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1781 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1782 EXPORT_SYMBOL(snd_rawmidi_kernel_write);