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 <linux/nospec.h>
33 #include <sound/rawmidi.h>
34 #include <sound/info.h>
35 #include <sound/control.h>
36 #include <sound/minors.h>
37 #include <sound/initval.h>
39 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
40 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
41 MODULE_LICENSE("GPL");
43 #ifdef CONFIG_SND_OSSEMUL
44 static int midi_map[SNDRV_CARDS];
45 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
46 module_param_array(midi_map, int, NULL, 0444);
47 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
48 module_param_array(amidi_map, int, NULL, 0444);
49 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
50 #endif /* CONFIG_SND_OSSEMUL */
52 static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
53 static int snd_rawmidi_dev_free(struct snd_device *device);
54 static int snd_rawmidi_dev_register(struct snd_device *device);
55 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
57 static LIST_HEAD(snd_rawmidi_devices);
58 static DEFINE_MUTEX(register_mutex);
60 #define rmidi_err(rmidi, fmt, args...) \
61 dev_err(&(rmidi)->dev, fmt, ##args)
62 #define rmidi_warn(rmidi, fmt, args...) \
63 dev_warn(&(rmidi)->dev, fmt, ##args)
64 #define rmidi_dbg(rmidi, fmt, args...) \
65 dev_dbg(&(rmidi)->dev, fmt, ##args)
67 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
69 struct snd_rawmidi *rawmidi;
71 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
72 if (rawmidi->card == card && rawmidi->device == device)
77 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
79 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
81 return SNDRV_RAWMIDI_LFLG_OUTPUT;
83 return SNDRV_RAWMIDI_LFLG_INPUT;
85 return SNDRV_RAWMIDI_LFLG_OPEN;
89 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
91 struct snd_rawmidi_runtime *runtime = substream->runtime;
92 return runtime->avail >= runtime->avail_min;
95 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
98 struct snd_rawmidi_runtime *runtime = substream->runtime;
99 return runtime->avail >= runtime->avail_min &&
100 (!substream->append || runtime->avail >= count);
103 static void snd_rawmidi_input_event_work(struct work_struct *work)
105 struct snd_rawmidi_runtime *runtime =
106 container_of(work, struct snd_rawmidi_runtime, event_work);
108 runtime->event(runtime->substream);
111 /* buffer refcount management: call with runtime->lock held */
112 static inline void snd_rawmidi_buffer_ref(struct snd_rawmidi_runtime *runtime)
114 runtime->buffer_ref++;
117 static inline void snd_rawmidi_buffer_unref(struct snd_rawmidi_runtime *runtime)
119 runtime->buffer_ref--;
122 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
124 struct snd_rawmidi_runtime *runtime;
126 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
128 runtime->substream = substream;
129 spin_lock_init(&runtime->lock);
130 init_waitqueue_head(&runtime->sleep);
131 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
132 runtime->event = NULL;
133 runtime->buffer_size = PAGE_SIZE;
134 runtime->avail_min = 1;
135 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
138 runtime->avail = runtime->buffer_size;
139 if ((runtime->buffer = kzalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
143 runtime->appl_ptr = runtime->hw_ptr = 0;
144 substream->runtime = runtime;
148 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
150 struct snd_rawmidi_runtime *runtime = substream->runtime;
152 kfree(runtime->buffer);
154 substream->runtime = NULL;
158 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
160 if (!substream->opened)
162 substream->ops->trigger(substream, up);
165 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
167 if (!substream->opened)
169 substream->ops->trigger(substream, up);
171 cancel_work_sync(&substream->runtime->event_work);
174 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
177 struct snd_rawmidi_runtime *runtime = substream->runtime;
179 snd_rawmidi_output_trigger(substream, 0);
181 spin_lock_irqsave(&runtime->lock, flags);
182 runtime->appl_ptr = runtime->hw_ptr = 0;
183 runtime->avail = runtime->buffer_size;
184 spin_unlock_irqrestore(&runtime->lock, flags);
187 EXPORT_SYMBOL(snd_rawmidi_drop_output);
189 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
193 struct snd_rawmidi_runtime *runtime = substream->runtime;
197 timeout = wait_event_interruptible_timeout(runtime->sleep,
198 (runtime->avail >= runtime->buffer_size),
200 if (signal_pending(current))
202 if (runtime->avail < runtime->buffer_size && !timeout) {
203 rmidi_warn(substream->rmidi,
204 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
205 (long)runtime->avail, (long)runtime->buffer_size);
209 if (err != -ERESTARTSYS) {
210 /* we need wait a while to make sure that Tx FIFOs are empty */
211 if (substream->ops->drain)
212 substream->ops->drain(substream);
215 snd_rawmidi_drop_output(substream);
219 EXPORT_SYMBOL(snd_rawmidi_drain_output);
221 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
224 struct snd_rawmidi_runtime *runtime = substream->runtime;
226 snd_rawmidi_input_trigger(substream, 0);
228 spin_lock_irqsave(&runtime->lock, flags);
229 runtime->appl_ptr = runtime->hw_ptr = 0;
231 spin_unlock_irqrestore(&runtime->lock, flags);
234 EXPORT_SYMBOL(snd_rawmidi_drain_input);
236 /* look for an available substream for the given stream direction;
237 * if a specific subdevice is given, try to assign it
239 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
240 int stream, int mode,
241 struct snd_rawmidi_substream **sub_ret)
243 struct snd_rawmidi_substream *substream;
244 struct snd_rawmidi_str *s = &rmidi->streams[stream];
245 static unsigned int info_flags[2] = {
246 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
247 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
250 if (!(rmidi->info_flags & info_flags[stream]))
252 if (subdevice >= 0 && subdevice >= s->substream_count)
255 list_for_each_entry(substream, &s->substreams, list) {
256 if (substream->opened) {
257 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
258 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
262 if (subdevice < 0 || subdevice == substream->number) {
263 *sub_ret = substream;
270 /* open and do ref-counting for the given substream */
271 static int open_substream(struct snd_rawmidi *rmidi,
272 struct snd_rawmidi_substream *substream,
277 if (substream->use_count == 0) {
278 err = snd_rawmidi_runtime_create(substream);
281 err = substream->ops->open(substream);
283 snd_rawmidi_runtime_free(substream);
286 substream->opened = 1;
287 substream->active_sensing = 0;
288 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
289 substream->append = 1;
290 substream->pid = get_pid(task_pid(current));
291 rmidi->streams[substream->stream].substream_opened++;
293 substream->use_count++;
297 static void close_substream(struct snd_rawmidi *rmidi,
298 struct snd_rawmidi_substream *substream,
301 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
302 struct snd_rawmidi_file *rfile)
304 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
307 rfile->input = rfile->output = NULL;
308 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
309 err = assign_substream(rmidi, subdevice,
310 SNDRV_RAWMIDI_STREAM_INPUT,
315 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
316 err = assign_substream(rmidi, subdevice,
317 SNDRV_RAWMIDI_STREAM_OUTPUT,
324 err = open_substream(rmidi, sinput, mode);
329 err = open_substream(rmidi, soutput, mode);
332 close_substream(rmidi, sinput, 0);
337 rfile->rmidi = rmidi;
338 rfile->input = sinput;
339 rfile->output = soutput;
343 /* called from sound/core/seq/seq_midi.c */
344 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
345 int mode, struct snd_rawmidi_file * rfile)
347 struct snd_rawmidi *rmidi;
350 if (snd_BUG_ON(!rfile))
353 mutex_lock(®ister_mutex);
354 rmidi = snd_rawmidi_search(card, device);
356 mutex_unlock(®ister_mutex);
359 if (!try_module_get(rmidi->card->module)) {
360 mutex_unlock(®ister_mutex);
363 mutex_unlock(®ister_mutex);
365 mutex_lock(&rmidi->open_mutex);
366 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
367 mutex_unlock(&rmidi->open_mutex);
369 module_put(rmidi->card->module);
372 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
374 static int snd_rawmidi_open(struct inode *inode, struct file *file)
376 int maj = imajor(inode);
377 struct snd_card *card;
379 unsigned short fflags;
381 struct snd_rawmidi *rmidi;
382 struct snd_rawmidi_file *rawmidi_file = NULL;
385 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
386 return -EINVAL; /* invalid combination */
388 err = nonseekable_open(inode, file);
392 if (maj == snd_major) {
393 rmidi = snd_lookup_minor_data(iminor(inode),
394 SNDRV_DEVICE_TYPE_RAWMIDI);
395 #ifdef CONFIG_SND_OSSEMUL
396 } else if (maj == SOUND_MAJOR) {
397 rmidi = snd_lookup_oss_minor_data(iminor(inode),
398 SNDRV_OSS_DEVICE_TYPE_MIDI);
406 if (!try_module_get(rmidi->card->module)) {
407 snd_card_unref(rmidi->card);
411 mutex_lock(&rmidi->open_mutex);
413 err = snd_card_file_add(card, file);
416 fflags = snd_rawmidi_file_flags(file);
417 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
418 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
419 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
420 if (rawmidi_file == NULL) {
424 init_waitqueue_entry(&wait, current);
425 add_wait_queue(&rmidi->open_wait, &wait);
427 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
428 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
431 if (err == -EAGAIN) {
432 if (file->f_flags & O_NONBLOCK) {
438 set_current_state(TASK_INTERRUPTIBLE);
439 mutex_unlock(&rmidi->open_mutex);
441 mutex_lock(&rmidi->open_mutex);
442 if (rmidi->card->shutdown) {
446 if (signal_pending(current)) {
451 remove_wait_queue(&rmidi->open_wait, &wait);
456 #ifdef CONFIG_SND_OSSEMUL
457 if (rawmidi_file->input && rawmidi_file->input->runtime)
458 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
459 if (rawmidi_file->output && rawmidi_file->output->runtime)
460 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
462 file->private_data = rawmidi_file;
463 mutex_unlock(&rmidi->open_mutex);
464 snd_card_unref(rmidi->card);
468 snd_card_file_remove(card, file);
470 mutex_unlock(&rmidi->open_mutex);
471 module_put(rmidi->card->module);
472 snd_card_unref(rmidi->card);
476 static void close_substream(struct snd_rawmidi *rmidi,
477 struct snd_rawmidi_substream *substream,
480 if (--substream->use_count)
484 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
485 snd_rawmidi_input_trigger(substream, 0);
487 if (substream->active_sensing) {
488 unsigned char buf = 0xfe;
489 /* sending single active sensing message
490 * to shut the device up
492 snd_rawmidi_kernel_write(substream, &buf, 1);
494 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
495 snd_rawmidi_output_trigger(substream, 0);
498 substream->ops->close(substream);
499 if (substream->runtime->private_free)
500 substream->runtime->private_free(substream);
501 snd_rawmidi_runtime_free(substream);
502 substream->opened = 0;
503 substream->append = 0;
504 put_pid(substream->pid);
505 substream->pid = NULL;
506 rmidi->streams[substream->stream].substream_opened--;
509 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
511 struct snd_rawmidi *rmidi;
513 rmidi = rfile->rmidi;
514 mutex_lock(&rmidi->open_mutex);
516 close_substream(rmidi, rfile->input, 1);
520 close_substream(rmidi, rfile->output, 1);
521 rfile->output = NULL;
524 mutex_unlock(&rmidi->open_mutex);
525 wake_up(&rmidi->open_wait);
528 /* called from sound/core/seq/seq_midi.c */
529 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
531 struct snd_rawmidi *rmidi;
533 if (snd_BUG_ON(!rfile))
536 rmidi = rfile->rmidi;
537 rawmidi_release_priv(rfile);
538 module_put(rmidi->card->module);
541 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
543 static int snd_rawmidi_release(struct inode *inode, struct file *file)
545 struct snd_rawmidi_file *rfile;
546 struct snd_rawmidi *rmidi;
547 struct module *module;
549 rfile = file->private_data;
550 rmidi = rfile->rmidi;
551 rawmidi_release_priv(rfile);
553 module = rmidi->card->module;
554 snd_card_file_remove(rmidi->card, file);
559 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
560 struct snd_rawmidi_info *info)
562 struct snd_rawmidi *rmidi;
564 if (substream == NULL)
566 rmidi = substream->rmidi;
567 memset(info, 0, sizeof(*info));
568 info->card = rmidi->card->number;
569 info->device = rmidi->device;
570 info->subdevice = substream->number;
571 info->stream = substream->stream;
572 info->flags = rmidi->info_flags;
573 strcpy(info->id, rmidi->id);
574 strcpy(info->name, rmidi->name);
575 strcpy(info->subname, substream->name);
576 info->subdevices_count = substream->pstr->substream_count;
577 info->subdevices_avail = (substream->pstr->substream_count -
578 substream->pstr->substream_opened);
582 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
583 struct snd_rawmidi_info __user * _info)
585 struct snd_rawmidi_info info;
587 if ((err = snd_rawmidi_info(substream, &info)) < 0)
589 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
594 static int __snd_rawmidi_info_select(struct snd_card *card,
595 struct snd_rawmidi_info *info)
597 struct snd_rawmidi *rmidi;
598 struct snd_rawmidi_str *pstr;
599 struct snd_rawmidi_substream *substream;
601 rmidi = snd_rawmidi_search(card, info->device);
604 if (info->stream < 0 || info->stream > 1)
606 info->stream = array_index_nospec(info->stream, 2);
607 pstr = &rmidi->streams[info->stream];
608 if (pstr->substream_count == 0)
610 if (info->subdevice >= pstr->substream_count)
612 list_for_each_entry(substream, &pstr->substreams, list) {
613 if ((unsigned int)substream->number == info->subdevice)
614 return snd_rawmidi_info(substream, info);
619 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
623 mutex_lock(®ister_mutex);
624 ret = __snd_rawmidi_info_select(card, info);
625 mutex_unlock(®ister_mutex);
628 EXPORT_SYMBOL(snd_rawmidi_info_select);
630 static int snd_rawmidi_info_select_user(struct snd_card *card,
631 struct snd_rawmidi_info __user *_info)
634 struct snd_rawmidi_info info;
635 if (get_user(info.device, &_info->device))
637 if (get_user(info.stream, &_info->stream))
639 if (get_user(info.subdevice, &_info->subdevice))
641 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
643 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
648 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
649 struct snd_rawmidi_params * params)
651 char *newbuf, *oldbuf;
652 struct snd_rawmidi_runtime *runtime = substream->runtime;
654 if (substream->append && substream->use_count > 1)
656 snd_rawmidi_drain_output(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 = kzalloc(params->buffer_size, GFP_KERNEL);
667 spin_lock_irq(&runtime->lock);
668 if (runtime->buffer_ref) {
669 spin_unlock_irq(&runtime->lock);
673 oldbuf = runtime->buffer;
674 runtime->buffer = newbuf;
675 runtime->buffer_size = params->buffer_size;
676 runtime->avail = runtime->buffer_size;
677 runtime->appl_ptr = runtime->hw_ptr = 0;
678 spin_unlock_irq(&runtime->lock);
681 runtime->avail_min = params->avail_min;
682 substream->active_sensing = !params->no_active_sensing;
685 EXPORT_SYMBOL(snd_rawmidi_output_params);
687 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
688 struct snd_rawmidi_params * params)
690 char *newbuf, *oldbuf;
691 struct snd_rawmidi_runtime *runtime = substream->runtime;
693 snd_rawmidi_drain_input(substream);
694 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
697 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
700 if (params->buffer_size != runtime->buffer_size) {
701 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
704 spin_lock_irq(&runtime->lock);
705 oldbuf = runtime->buffer;
706 runtime->buffer = newbuf;
707 runtime->buffer_size = params->buffer_size;
708 runtime->appl_ptr = runtime->hw_ptr = 0;
709 spin_unlock_irq(&runtime->lock);
712 runtime->avail_min = params->avail_min;
715 EXPORT_SYMBOL(snd_rawmidi_input_params);
717 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
718 struct snd_rawmidi_status * status)
720 struct snd_rawmidi_runtime *runtime = substream->runtime;
722 memset(status, 0, sizeof(*status));
723 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
724 spin_lock_irq(&runtime->lock);
725 status->avail = runtime->avail;
726 spin_unlock_irq(&runtime->lock);
730 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
731 struct snd_rawmidi_status * status)
733 struct snd_rawmidi_runtime *runtime = substream->runtime;
735 memset(status, 0, sizeof(*status));
736 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
737 spin_lock_irq(&runtime->lock);
738 status->avail = runtime->avail;
739 status->xruns = runtime->xruns;
741 spin_unlock_irq(&runtime->lock);
745 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
747 struct snd_rawmidi_file *rfile;
748 void __user *argp = (void __user *)arg;
750 rfile = file->private_data;
751 if (((cmd >> 8) & 0xff) != 'W')
754 case SNDRV_RAWMIDI_IOCTL_PVERSION:
755 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
756 case SNDRV_RAWMIDI_IOCTL_INFO:
759 struct snd_rawmidi_info __user *info = argp;
760 if (get_user(stream, &info->stream))
763 case SNDRV_RAWMIDI_STREAM_INPUT:
764 return snd_rawmidi_info_user(rfile->input, info);
765 case SNDRV_RAWMIDI_STREAM_OUTPUT:
766 return snd_rawmidi_info_user(rfile->output, info);
771 case SNDRV_RAWMIDI_IOCTL_PARAMS:
773 struct snd_rawmidi_params params;
774 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
776 switch (params.stream) {
777 case SNDRV_RAWMIDI_STREAM_OUTPUT:
778 if (rfile->output == NULL)
780 return snd_rawmidi_output_params(rfile->output, ¶ms);
781 case SNDRV_RAWMIDI_STREAM_INPUT:
782 if (rfile->input == NULL)
784 return snd_rawmidi_input_params(rfile->input, ¶ms);
789 case SNDRV_RAWMIDI_IOCTL_STATUS:
792 struct snd_rawmidi_status status;
793 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
795 switch (status.stream) {
796 case SNDRV_RAWMIDI_STREAM_OUTPUT:
797 if (rfile->output == NULL)
799 err = snd_rawmidi_output_status(rfile->output, &status);
801 case SNDRV_RAWMIDI_STREAM_INPUT:
802 if (rfile->input == NULL)
804 err = snd_rawmidi_input_status(rfile->input, &status);
811 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
815 case SNDRV_RAWMIDI_IOCTL_DROP:
818 if (get_user(val, (int __user *) argp))
821 case SNDRV_RAWMIDI_STREAM_OUTPUT:
822 if (rfile->output == NULL)
824 return snd_rawmidi_drop_output(rfile->output);
829 case SNDRV_RAWMIDI_IOCTL_DRAIN:
832 if (get_user(val, (int __user *) argp))
835 case SNDRV_RAWMIDI_STREAM_OUTPUT:
836 if (rfile->output == NULL)
838 return snd_rawmidi_drain_output(rfile->output);
839 case SNDRV_RAWMIDI_STREAM_INPUT:
840 if (rfile->input == NULL)
842 return snd_rawmidi_drain_input(rfile->input);
848 rmidi_dbg(rfile->rmidi,
849 "rawmidi: unknown command = 0x%x\n", cmd);
854 static int snd_rawmidi_control_ioctl(struct snd_card *card,
855 struct snd_ctl_file *control,
859 void __user *argp = (void __user *)arg;
862 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
866 if (get_user(device, (int __user *)argp))
868 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
869 device = SNDRV_RAWMIDI_DEVICES - 1;
870 mutex_lock(®ister_mutex);
871 device = device < 0 ? 0 : device + 1;
872 while (device < SNDRV_RAWMIDI_DEVICES) {
873 if (snd_rawmidi_search(card, device))
877 if (device == SNDRV_RAWMIDI_DEVICES)
879 mutex_unlock(®ister_mutex);
880 if (put_user(device, (int __user *)argp))
884 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
888 if (get_user(val, (int __user *)argp))
890 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
893 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
894 return snd_rawmidi_info_select_user(card, argp);
900 * snd_rawmidi_receive - receive the input data from the device
901 * @substream: the rawmidi substream
902 * @buffer: the buffer pointer
903 * @count: the data size to read
905 * Reads the data from the internal buffer.
907 * Return: The size of read data, or a negative error code on failure.
909 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
910 const unsigned char *buffer, int count)
913 int result = 0, count1;
914 struct snd_rawmidi_runtime *runtime = substream->runtime;
916 if (!substream->opened)
918 if (runtime->buffer == NULL) {
919 rmidi_dbg(substream->rmidi,
920 "snd_rawmidi_receive: input is not active!!!\n");
923 spin_lock_irqsave(&runtime->lock, flags);
924 if (count == 1) { /* special case, faster code */
926 if (runtime->avail < runtime->buffer_size) {
927 runtime->buffer[runtime->hw_ptr++] = buffer[0];
928 runtime->hw_ptr %= runtime->buffer_size;
935 substream->bytes += count;
936 count1 = runtime->buffer_size - runtime->hw_ptr;
939 if (count1 > (int)(runtime->buffer_size - runtime->avail))
940 count1 = runtime->buffer_size - runtime->avail;
941 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
942 runtime->hw_ptr += count1;
943 runtime->hw_ptr %= runtime->buffer_size;
944 runtime->avail += count1;
950 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
951 count1 = runtime->buffer_size - runtime->avail;
952 runtime->xruns += count - count1;
955 memcpy(runtime->buffer, buffer, count1);
956 runtime->hw_ptr = count1;
957 runtime->avail += count1;
964 schedule_work(&runtime->event_work);
965 else if (snd_rawmidi_ready(substream))
966 wake_up(&runtime->sleep);
968 spin_unlock_irqrestore(&runtime->lock, flags);
971 EXPORT_SYMBOL(snd_rawmidi_receive);
973 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
974 unsigned char __user *userbuf,
975 unsigned char *kernelbuf, long count)
978 long result = 0, count1;
979 struct snd_rawmidi_runtime *runtime = substream->runtime;
980 unsigned long appl_ptr;
983 spin_lock_irqsave(&runtime->lock, flags);
984 snd_rawmidi_buffer_ref(runtime);
985 while (count > 0 && runtime->avail) {
986 count1 = runtime->buffer_size - runtime->appl_ptr;
989 if (count1 > (int)runtime->avail)
990 count1 = runtime->avail;
992 /* update runtime->appl_ptr before unlocking for userbuf */
993 appl_ptr = runtime->appl_ptr;
994 runtime->appl_ptr += count1;
995 runtime->appl_ptr %= runtime->buffer_size;
996 runtime->avail -= count1;
999 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
1001 spin_unlock_irqrestore(&runtime->lock, flags);
1002 if (copy_to_user(userbuf + result,
1003 runtime->buffer + appl_ptr, count1))
1005 spin_lock_irqsave(&runtime->lock, flags);
1013 snd_rawmidi_buffer_unref(runtime);
1014 spin_unlock_irqrestore(&runtime->lock, flags);
1015 return result > 0 ? result : err;
1018 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
1019 unsigned char *buf, long count)
1021 snd_rawmidi_input_trigger(substream, 1);
1022 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
1024 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1026 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1031 struct snd_rawmidi_file *rfile;
1032 struct snd_rawmidi_substream *substream;
1033 struct snd_rawmidi_runtime *runtime;
1035 rfile = file->private_data;
1036 substream = rfile->input;
1037 if (substream == NULL)
1039 runtime = substream->runtime;
1040 snd_rawmidi_input_trigger(substream, 1);
1043 spin_lock_irq(&runtime->lock);
1044 while (!snd_rawmidi_ready(substream)) {
1046 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1047 spin_unlock_irq(&runtime->lock);
1048 return result > 0 ? result : -EAGAIN;
1050 init_waitqueue_entry(&wait, current);
1051 add_wait_queue(&runtime->sleep, &wait);
1052 set_current_state(TASK_INTERRUPTIBLE);
1053 spin_unlock_irq(&runtime->lock);
1055 remove_wait_queue(&runtime->sleep, &wait);
1056 if (rfile->rmidi->card->shutdown)
1058 if (signal_pending(current))
1059 return result > 0 ? result : -ERESTARTSYS;
1060 if (!runtime->avail)
1061 return result > 0 ? result : -EIO;
1062 spin_lock_irq(&runtime->lock);
1064 spin_unlock_irq(&runtime->lock);
1065 count1 = snd_rawmidi_kernel_read1(substream,
1066 (unsigned char __user *)buf,
1070 return result > 0 ? result : count1;
1079 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1080 * @substream: the rawmidi substream
1082 * Return: 1 if the internal output buffer is empty, 0 if not.
1084 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1086 struct snd_rawmidi_runtime *runtime = substream->runtime;
1088 unsigned long flags;
1090 if (runtime->buffer == NULL) {
1091 rmidi_dbg(substream->rmidi,
1092 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1095 spin_lock_irqsave(&runtime->lock, flags);
1096 result = runtime->avail >= runtime->buffer_size;
1097 spin_unlock_irqrestore(&runtime->lock, flags);
1100 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1103 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1104 * @substream: the rawmidi substream
1105 * @buffer: the buffer pointer
1106 * @count: data size to transfer
1108 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1110 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1111 unsigned char *buffer, int count)
1114 struct snd_rawmidi_runtime *runtime = substream->runtime;
1116 if (runtime->buffer == NULL) {
1117 rmidi_dbg(substream->rmidi,
1118 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1122 if (runtime->avail >= runtime->buffer_size) {
1123 /* warning: lowlevel layer MUST trigger down the hardware */
1126 if (count == 1) { /* special case, faster code */
1127 *buffer = runtime->buffer[runtime->hw_ptr];
1130 count1 = runtime->buffer_size - runtime->hw_ptr;
1133 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1134 count1 = runtime->buffer_size - runtime->avail;
1135 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1139 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1140 count = runtime->buffer_size - runtime->avail - count1;
1141 memcpy(buffer + count1, runtime->buffer, count);
1148 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1151 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1152 * @substream: the rawmidi substream
1153 * @buffer: the buffer pointer
1154 * @count: data size to transfer
1156 * Copies data from the internal output buffer to the given buffer.
1158 * Call this in the interrupt handler when the midi output is ready,
1159 * and call snd_rawmidi_transmit_ack() after the transmission is
1162 * Return: The size of copied data, or a negative error code on failure.
1164 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1165 unsigned char *buffer, int count)
1167 struct snd_rawmidi_runtime *runtime = substream->runtime;
1169 unsigned long flags;
1171 spin_lock_irqsave(&runtime->lock, flags);
1172 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1173 spin_unlock_irqrestore(&runtime->lock, flags);
1176 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1179 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1180 * @substream: the rawmidi substream
1181 * @count: the transferred count
1183 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1185 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1187 struct snd_rawmidi_runtime *runtime = substream->runtime;
1189 if (runtime->buffer == NULL) {
1190 rmidi_dbg(substream->rmidi,
1191 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1194 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1195 runtime->hw_ptr += count;
1196 runtime->hw_ptr %= runtime->buffer_size;
1197 runtime->avail += count;
1198 substream->bytes += count;
1200 if (runtime->drain || snd_rawmidi_ready(substream))
1201 wake_up(&runtime->sleep);
1205 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1208 * snd_rawmidi_transmit_ack - acknowledge the transmission
1209 * @substream: the rawmidi substream
1210 * @count: the transferred count
1212 * Advances the hardware pointer for the internal output buffer with
1213 * the given size and updates the condition.
1214 * Call after the transmission is finished.
1216 * Return: The advanced size if successful, or a negative error code on failure.
1218 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1220 struct snd_rawmidi_runtime *runtime = substream->runtime;
1222 unsigned long flags;
1224 spin_lock_irqsave(&runtime->lock, flags);
1225 result = __snd_rawmidi_transmit_ack(substream, count);
1226 spin_unlock_irqrestore(&runtime->lock, flags);
1229 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1232 * snd_rawmidi_transmit - copy from the buffer to the device
1233 * @substream: the rawmidi substream
1234 * @buffer: the buffer pointer
1235 * @count: the data size to transfer
1237 * Copies data from the buffer to the device and advances the pointer.
1239 * Return: The copied size if successful, or a negative error code on failure.
1241 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1242 unsigned char *buffer, int count)
1244 struct snd_rawmidi_runtime *runtime = substream->runtime;
1246 unsigned long flags;
1248 spin_lock_irqsave(&runtime->lock, flags);
1249 if (!substream->opened)
1252 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1256 result = __snd_rawmidi_transmit_ack(substream, count);
1258 spin_unlock_irqrestore(&runtime->lock, flags);
1261 EXPORT_SYMBOL(snd_rawmidi_transmit);
1263 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1264 const unsigned char __user *userbuf,
1265 const unsigned char *kernelbuf,
1268 unsigned long flags;
1269 long count1, result;
1270 struct snd_rawmidi_runtime *runtime = substream->runtime;
1271 unsigned long appl_ptr;
1273 if (!kernelbuf && !userbuf)
1275 if (snd_BUG_ON(!runtime->buffer))
1279 spin_lock_irqsave(&runtime->lock, flags);
1280 if (substream->append) {
1281 if ((long)runtime->avail < count) {
1282 spin_unlock_irqrestore(&runtime->lock, flags);
1286 snd_rawmidi_buffer_ref(runtime);
1287 while (count > 0 && runtime->avail > 0) {
1288 count1 = runtime->buffer_size - runtime->appl_ptr;
1291 if (count1 > (long)runtime->avail)
1292 count1 = runtime->avail;
1294 /* update runtime->appl_ptr before unlocking for userbuf */
1295 appl_ptr = runtime->appl_ptr;
1296 runtime->appl_ptr += count1;
1297 runtime->appl_ptr %= runtime->buffer_size;
1298 runtime->avail -= count1;
1301 memcpy(runtime->buffer + appl_ptr,
1302 kernelbuf + result, count1);
1304 spin_unlock_irqrestore(&runtime->lock, flags);
1305 if (copy_from_user(runtime->buffer + appl_ptr,
1306 userbuf + result, count1)) {
1307 spin_lock_irqsave(&runtime->lock, flags);
1308 result = result > 0 ? result : -EFAULT;
1311 spin_lock_irqsave(&runtime->lock, flags);
1317 count1 = runtime->avail < runtime->buffer_size;
1318 snd_rawmidi_buffer_unref(runtime);
1319 spin_unlock_irqrestore(&runtime->lock, flags);
1321 snd_rawmidi_output_trigger(substream, 1);
1325 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1326 const unsigned char *buf, long count)
1328 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1330 EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1332 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1333 size_t count, loff_t *offset)
1335 long result, timeout;
1337 struct snd_rawmidi_file *rfile;
1338 struct snd_rawmidi_runtime *runtime;
1339 struct snd_rawmidi_substream *substream;
1341 rfile = file->private_data;
1342 substream = rfile->output;
1343 runtime = substream->runtime;
1344 /* we cannot put an atomic message to our buffer */
1345 if (substream->append && count > runtime->buffer_size)
1349 spin_lock_irq(&runtime->lock);
1350 while (!snd_rawmidi_ready_append(substream, count)) {
1352 if (file->f_flags & O_NONBLOCK) {
1353 spin_unlock_irq(&runtime->lock);
1354 return result > 0 ? result : -EAGAIN;
1356 init_waitqueue_entry(&wait, current);
1357 add_wait_queue(&runtime->sleep, &wait);
1358 set_current_state(TASK_INTERRUPTIBLE);
1359 spin_unlock_irq(&runtime->lock);
1360 timeout = schedule_timeout(30 * HZ);
1361 remove_wait_queue(&runtime->sleep, &wait);
1362 if (rfile->rmidi->card->shutdown)
1364 if (signal_pending(current))
1365 return result > 0 ? result : -ERESTARTSYS;
1366 if (!runtime->avail && !timeout)
1367 return result > 0 ? result : -EIO;
1368 spin_lock_irq(&runtime->lock);
1370 spin_unlock_irq(&runtime->lock);
1371 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1373 return result > 0 ? result : count1;
1376 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1380 if (file->f_flags & O_DSYNC) {
1381 spin_lock_irq(&runtime->lock);
1382 while (runtime->avail != runtime->buffer_size) {
1384 unsigned int last_avail = runtime->avail;
1385 init_waitqueue_entry(&wait, current);
1386 add_wait_queue(&runtime->sleep, &wait);
1387 set_current_state(TASK_INTERRUPTIBLE);
1388 spin_unlock_irq(&runtime->lock);
1389 timeout = schedule_timeout(30 * HZ);
1390 remove_wait_queue(&runtime->sleep, &wait);
1391 if (signal_pending(current))
1392 return result > 0 ? result : -ERESTARTSYS;
1393 if (runtime->avail == last_avail && !timeout)
1394 return result > 0 ? result : -EIO;
1395 spin_lock_irq(&runtime->lock);
1397 spin_unlock_irq(&runtime->lock);
1402 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1404 struct snd_rawmidi_file *rfile;
1405 struct snd_rawmidi_runtime *runtime;
1408 rfile = file->private_data;
1409 if (rfile->input != NULL) {
1410 runtime = rfile->input->runtime;
1411 snd_rawmidi_input_trigger(rfile->input, 1);
1412 poll_wait(file, &runtime->sleep, wait);
1414 if (rfile->output != NULL) {
1415 runtime = rfile->output->runtime;
1416 poll_wait(file, &runtime->sleep, wait);
1419 if (rfile->input != NULL) {
1420 if (snd_rawmidi_ready(rfile->input))
1421 mask |= POLLIN | POLLRDNORM;
1423 if (rfile->output != NULL) {
1424 if (snd_rawmidi_ready(rfile->output))
1425 mask |= POLLOUT | POLLWRNORM;
1432 #ifdef CONFIG_COMPAT
1433 #include "rawmidi_compat.c"
1435 #define snd_rawmidi_ioctl_compat NULL
1442 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1443 struct snd_info_buffer *buffer)
1445 struct snd_rawmidi *rmidi;
1446 struct snd_rawmidi_substream *substream;
1447 struct snd_rawmidi_runtime *runtime;
1449 rmidi = entry->private_data;
1450 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1451 mutex_lock(&rmidi->open_mutex);
1452 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1453 list_for_each_entry(substream,
1454 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1458 " Tx bytes : %lu\n",
1460 (unsigned long) substream->bytes);
1461 if (substream->opened) {
1463 " Owner PID : %d\n",
1464 pid_vnr(substream->pid));
1465 runtime = substream->runtime;
1468 " Buffer size : %lu\n"
1470 runtime->oss ? "OSS compatible" : "native",
1471 (unsigned long) runtime->buffer_size,
1472 (unsigned long) runtime->avail);
1476 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1477 list_for_each_entry(substream,
1478 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1482 " Rx bytes : %lu\n",
1484 (unsigned long) substream->bytes);
1485 if (substream->opened) {
1487 " Owner PID : %d\n",
1488 pid_vnr(substream->pid));
1489 runtime = substream->runtime;
1491 " Buffer size : %lu\n"
1493 " Overruns : %lu\n",
1494 (unsigned long) runtime->buffer_size,
1495 (unsigned long) runtime->avail,
1496 (unsigned long) runtime->xruns);
1500 mutex_unlock(&rmidi->open_mutex);
1504 * Register functions
1507 static const struct file_operations snd_rawmidi_f_ops =
1509 .owner = THIS_MODULE,
1510 .read = snd_rawmidi_read,
1511 .write = snd_rawmidi_write,
1512 .open = snd_rawmidi_open,
1513 .release = snd_rawmidi_release,
1514 .llseek = no_llseek,
1515 .poll = snd_rawmidi_poll,
1516 .unlocked_ioctl = snd_rawmidi_ioctl,
1517 .compat_ioctl = snd_rawmidi_ioctl_compat,
1520 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1521 struct snd_rawmidi_str *stream,
1525 struct snd_rawmidi_substream *substream;
1528 for (idx = 0; idx < count; idx++) {
1529 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1532 substream->stream = direction;
1533 substream->number = idx;
1534 substream->rmidi = rmidi;
1535 substream->pstr = stream;
1536 list_add_tail(&substream->list, &stream->substreams);
1537 stream->substream_count++;
1542 static void release_rawmidi_device(struct device *dev)
1544 kfree(container_of(dev, struct snd_rawmidi, dev));
1548 * snd_rawmidi_new - create a rawmidi instance
1549 * @card: the card instance
1550 * @id: the id string
1551 * @device: the device index
1552 * @output_count: the number of output streams
1553 * @input_count: the number of input streams
1554 * @rrawmidi: the pointer to store the new rawmidi instance
1556 * Creates a new rawmidi instance.
1557 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1559 * Return: Zero if successful, or a negative error code on failure.
1561 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1562 int output_count, int input_count,
1563 struct snd_rawmidi ** rrawmidi)
1565 struct snd_rawmidi *rmidi;
1567 static struct snd_device_ops ops = {
1568 .dev_free = snd_rawmidi_dev_free,
1569 .dev_register = snd_rawmidi_dev_register,
1570 .dev_disconnect = snd_rawmidi_dev_disconnect,
1573 if (snd_BUG_ON(!card))
1577 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1581 rmidi->device = device;
1582 mutex_init(&rmidi->open_mutex);
1583 init_waitqueue_head(&rmidi->open_wait);
1584 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1585 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1588 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1590 snd_device_initialize(&rmidi->dev, card);
1591 rmidi->dev.release = release_rawmidi_device;
1592 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1594 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1595 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1596 SNDRV_RAWMIDI_STREAM_INPUT,
1597 input_count)) < 0) {
1598 snd_rawmidi_free(rmidi);
1601 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1602 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1603 SNDRV_RAWMIDI_STREAM_OUTPUT,
1604 output_count)) < 0) {
1605 snd_rawmidi_free(rmidi);
1608 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1609 snd_rawmidi_free(rmidi);
1616 EXPORT_SYMBOL(snd_rawmidi_new);
1618 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1620 struct snd_rawmidi_substream *substream;
1622 while (!list_empty(&stream->substreams)) {
1623 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1624 list_del(&substream->list);
1629 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1634 snd_info_free_entry(rmidi->proc_entry);
1635 rmidi->proc_entry = NULL;
1636 mutex_lock(®ister_mutex);
1637 if (rmidi->ops && rmidi->ops->dev_unregister)
1638 rmidi->ops->dev_unregister(rmidi);
1639 mutex_unlock(®ister_mutex);
1641 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1642 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1643 if (rmidi->private_free)
1644 rmidi->private_free(rmidi);
1645 put_device(&rmidi->dev);
1649 static int snd_rawmidi_dev_free(struct snd_device *device)
1651 struct snd_rawmidi *rmidi = device->device_data;
1652 return snd_rawmidi_free(rmidi);
1655 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1656 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1658 struct snd_rawmidi *rmidi = device->private_data;
1659 rmidi->seq_dev = NULL;
1663 static int snd_rawmidi_dev_register(struct snd_device *device)
1666 struct snd_info_entry *entry;
1668 struct snd_rawmidi *rmidi = device->device_data;
1670 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1672 mutex_lock(®ister_mutex);
1673 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1674 mutex_unlock(®ister_mutex);
1677 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1678 mutex_unlock(®ister_mutex);
1679 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1680 rmidi->card, rmidi->device,
1681 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
1683 rmidi_err(rmidi, "unable to register\n");
1684 mutex_lock(®ister_mutex);
1685 list_del(&rmidi->list);
1686 mutex_unlock(®ister_mutex);
1689 if (rmidi->ops && rmidi->ops->dev_register &&
1690 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1691 snd_unregister_device(&rmidi->dev);
1692 mutex_lock(®ister_mutex);
1693 list_del(&rmidi->list);
1694 mutex_unlock(®ister_mutex);
1697 #ifdef CONFIG_SND_OSSEMUL
1699 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1700 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1701 rmidi->card, 0, &snd_rawmidi_f_ops,
1704 "unable to register OSS rawmidi device %i:%i\n",
1705 rmidi->card->number, 0);
1708 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1709 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1713 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1714 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1715 rmidi->card, 1, &snd_rawmidi_f_ops,
1718 "unable to register OSS rawmidi device %i:%i\n",
1719 rmidi->card->number, 1);
1724 #endif /* CONFIG_SND_OSSEMUL */
1725 sprintf(name, "midi%d", rmidi->device);
1726 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1728 entry->private_data = rmidi;
1729 entry->c.text.read = snd_rawmidi_proc_info_read;
1730 if (snd_info_register(entry) < 0) {
1731 snd_info_free_entry(entry);
1735 rmidi->proc_entry = entry;
1736 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1737 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1738 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1739 rmidi->seq_dev->private_data = rmidi;
1740 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1741 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1742 snd_device_register(rmidi->card, rmidi->seq_dev);
1749 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1751 struct snd_rawmidi *rmidi = device->device_data;
1754 mutex_lock(®ister_mutex);
1755 mutex_lock(&rmidi->open_mutex);
1756 wake_up(&rmidi->open_wait);
1757 list_del_init(&rmidi->list);
1758 for (dir = 0; dir < 2; dir++) {
1759 struct snd_rawmidi_substream *s;
1760 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1762 wake_up(&s->runtime->sleep);
1766 #ifdef CONFIG_SND_OSSEMUL
1767 if (rmidi->ossreg) {
1768 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1769 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1770 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1771 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1774 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1775 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1778 #endif /* CONFIG_SND_OSSEMUL */
1779 snd_unregister_device(&rmidi->dev);
1780 mutex_unlock(&rmidi->open_mutex);
1781 mutex_unlock(®ister_mutex);
1786 * snd_rawmidi_set_ops - set the rawmidi operators
1787 * @rmidi: the rawmidi instance
1788 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1789 * @ops: the operator table
1791 * Sets the rawmidi operators for the given stream direction.
1793 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1794 struct snd_rawmidi_ops *ops)
1796 struct snd_rawmidi_substream *substream;
1798 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1799 substream->ops = ops;
1801 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1807 static int __init alsa_rawmidi_init(void)
1810 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1811 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1812 #ifdef CONFIG_SND_OSSEMUL
1814 /* check device map table */
1815 for (i = 0; i < SNDRV_CARDS; i++) {
1816 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1817 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1821 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1822 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1828 #endif /* CONFIG_SND_OSSEMUL */
1832 static void __exit alsa_rawmidi_exit(void)
1834 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1835 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1838 module_init(alsa_rawmidi_init)
1839 module_exit(alsa_rawmidi_exit)