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/signal.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>
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;
93 return runtime->avail >= runtime->avail_min;
96 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
99 struct snd_rawmidi_runtime *runtime = substream->runtime;
101 return runtime->avail >= runtime->avail_min &&
102 (!substream->append || runtime->avail >= count);
105 static void snd_rawmidi_input_event_work(struct work_struct *work)
107 struct snd_rawmidi_runtime *runtime =
108 container_of(work, struct snd_rawmidi_runtime, event_work);
111 runtime->event(runtime->substream);
114 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
116 struct snd_rawmidi_runtime *runtime;
118 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
121 runtime->substream = substream;
122 spin_lock_init(&runtime->lock);
123 init_waitqueue_head(&runtime->sleep);
124 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
125 runtime->event = NULL;
126 runtime->buffer_size = PAGE_SIZE;
127 runtime->avail_min = 1;
128 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
131 runtime->avail = runtime->buffer_size;
132 runtime->buffer = kvzalloc(runtime->buffer_size, GFP_KERNEL);
133 if (!runtime->buffer) {
137 runtime->appl_ptr = runtime->hw_ptr = 0;
138 substream->runtime = runtime;
142 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
144 struct snd_rawmidi_runtime *runtime = substream->runtime;
146 kvfree(runtime->buffer);
148 substream->runtime = NULL;
152 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
154 if (!substream->opened)
156 substream->ops->trigger(substream, up);
159 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
161 if (!substream->opened)
163 substream->ops->trigger(substream, up);
165 cancel_work_sync(&substream->runtime->event_work);
168 static void __reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
172 runtime->appl_ptr = runtime->hw_ptr = 0;
173 runtime->avail = is_input ? 0 : runtime->buffer_size;
176 static void reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
181 spin_lock_irqsave(&runtime->lock, flags);
182 __reset_runtime_ptrs(runtime, is_input);
183 spin_unlock_irqrestore(&runtime->lock, flags);
186 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
188 snd_rawmidi_output_trigger(substream, 0);
189 reset_runtime_ptrs(substream->runtime, false);
192 EXPORT_SYMBOL(snd_rawmidi_drop_output);
194 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
198 struct snd_rawmidi_runtime *runtime = substream->runtime;
202 timeout = wait_event_interruptible_timeout(runtime->sleep,
203 (runtime->avail >= runtime->buffer_size),
205 if (signal_pending(current))
207 if (runtime->avail < runtime->buffer_size && !timeout) {
208 rmidi_warn(substream->rmidi,
209 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
210 (long)runtime->avail, (long)runtime->buffer_size);
214 if (err != -ERESTARTSYS) {
215 /* we need wait a while to make sure that Tx FIFOs are empty */
216 if (substream->ops->drain)
217 substream->ops->drain(substream);
220 snd_rawmidi_drop_output(substream);
224 EXPORT_SYMBOL(snd_rawmidi_drain_output);
226 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
228 snd_rawmidi_input_trigger(substream, 0);
229 reset_runtime_ptrs(substream->runtime, true);
232 EXPORT_SYMBOL(snd_rawmidi_drain_input);
234 /* look for an available substream for the given stream direction;
235 * if a specific subdevice is given, try to assign it
237 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
238 int stream, int mode,
239 struct snd_rawmidi_substream **sub_ret)
241 struct snd_rawmidi_substream *substream;
242 struct snd_rawmidi_str *s = &rmidi->streams[stream];
243 static unsigned int info_flags[2] = {
244 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
245 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
248 if (!(rmidi->info_flags & info_flags[stream]))
250 if (subdevice >= 0 && subdevice >= s->substream_count)
253 list_for_each_entry(substream, &s->substreams, list) {
254 if (substream->opened) {
255 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
256 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
260 if (subdevice < 0 || subdevice == substream->number) {
261 *sub_ret = substream;
268 /* open and do ref-counting for the given substream */
269 static int open_substream(struct snd_rawmidi *rmidi,
270 struct snd_rawmidi_substream *substream,
275 if (substream->use_count == 0) {
276 err = snd_rawmidi_runtime_create(substream);
279 err = substream->ops->open(substream);
281 snd_rawmidi_runtime_free(substream);
284 substream->opened = 1;
285 substream->active_sensing = 0;
286 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
287 substream->append = 1;
288 substream->pid = get_pid(task_pid(current));
289 rmidi->streams[substream->stream].substream_opened++;
291 substream->use_count++;
295 static void close_substream(struct snd_rawmidi *rmidi,
296 struct snd_rawmidi_substream *substream,
299 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
300 struct snd_rawmidi_file *rfile)
302 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
305 rfile->input = rfile->output = NULL;
306 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
307 err = assign_substream(rmidi, subdevice,
308 SNDRV_RAWMIDI_STREAM_INPUT,
313 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
314 err = assign_substream(rmidi, subdevice,
315 SNDRV_RAWMIDI_STREAM_OUTPUT,
322 err = open_substream(rmidi, sinput, mode);
327 err = open_substream(rmidi, soutput, mode);
330 close_substream(rmidi, sinput, 0);
335 rfile->rmidi = rmidi;
336 rfile->input = sinput;
337 rfile->output = soutput;
341 /* called from sound/core/seq/seq_midi.c */
342 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
343 int mode, struct snd_rawmidi_file *rfile)
345 struct snd_rawmidi *rmidi;
348 if (snd_BUG_ON(!rfile))
351 mutex_lock(®ister_mutex);
352 rmidi = snd_rawmidi_search(card, device);
355 else if (!try_module_get(rmidi->card->module))
357 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);
368 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
370 static int snd_rawmidi_open(struct inode *inode, struct file *file)
372 int maj = imajor(inode);
373 struct snd_card *card;
375 unsigned short fflags;
377 struct snd_rawmidi *rmidi;
378 struct snd_rawmidi_file *rawmidi_file = NULL;
379 wait_queue_entry_t wait;
381 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
382 return -EINVAL; /* invalid combination */
384 err = nonseekable_open(inode, file);
388 if (maj == snd_major) {
389 rmidi = snd_lookup_minor_data(iminor(inode),
390 SNDRV_DEVICE_TYPE_RAWMIDI);
391 #ifdef CONFIG_SND_OSSEMUL
392 } else if (maj == SOUND_MAJOR) {
393 rmidi = snd_lookup_oss_minor_data(iminor(inode),
394 SNDRV_OSS_DEVICE_TYPE_MIDI);
402 if (!try_module_get(rmidi->card->module)) {
403 snd_card_unref(rmidi->card);
407 mutex_lock(&rmidi->open_mutex);
409 err = snd_card_file_add(card, file);
412 fflags = snd_rawmidi_file_flags(file);
413 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
414 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
415 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
416 if (rawmidi_file == NULL) {
420 init_waitqueue_entry(&wait, current);
421 add_wait_queue(&rmidi->open_wait, &wait);
423 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
424 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
427 if (err == -EAGAIN) {
428 if (file->f_flags & O_NONBLOCK) {
434 set_current_state(TASK_INTERRUPTIBLE);
435 mutex_unlock(&rmidi->open_mutex);
437 mutex_lock(&rmidi->open_mutex);
438 if (rmidi->card->shutdown) {
442 if (signal_pending(current)) {
447 remove_wait_queue(&rmidi->open_wait, &wait);
452 #ifdef CONFIG_SND_OSSEMUL
453 if (rawmidi_file->input && rawmidi_file->input->runtime)
454 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
455 if (rawmidi_file->output && rawmidi_file->output->runtime)
456 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
458 file->private_data = rawmidi_file;
459 mutex_unlock(&rmidi->open_mutex);
460 snd_card_unref(rmidi->card);
464 snd_card_file_remove(card, file);
466 mutex_unlock(&rmidi->open_mutex);
467 module_put(rmidi->card->module);
468 snd_card_unref(rmidi->card);
472 static void close_substream(struct snd_rawmidi *rmidi,
473 struct snd_rawmidi_substream *substream,
476 if (--substream->use_count)
480 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
481 snd_rawmidi_input_trigger(substream, 0);
483 if (substream->active_sensing) {
484 unsigned char buf = 0xfe;
485 /* sending single active sensing message
486 * to shut the device up
488 snd_rawmidi_kernel_write(substream, &buf, 1);
490 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
491 snd_rawmidi_output_trigger(substream, 0);
494 substream->ops->close(substream);
495 if (substream->runtime->private_free)
496 substream->runtime->private_free(substream);
497 snd_rawmidi_runtime_free(substream);
498 substream->opened = 0;
499 substream->append = 0;
500 put_pid(substream->pid);
501 substream->pid = NULL;
502 rmidi->streams[substream->stream].substream_opened--;
505 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
507 struct snd_rawmidi *rmidi;
509 rmidi = rfile->rmidi;
510 mutex_lock(&rmidi->open_mutex);
512 close_substream(rmidi, rfile->input, 1);
516 close_substream(rmidi, rfile->output, 1);
517 rfile->output = NULL;
520 mutex_unlock(&rmidi->open_mutex);
521 wake_up(&rmidi->open_wait);
524 /* called from sound/core/seq/seq_midi.c */
525 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
527 struct snd_rawmidi *rmidi;
529 if (snd_BUG_ON(!rfile))
532 rmidi = rfile->rmidi;
533 rawmidi_release_priv(rfile);
534 module_put(rmidi->card->module);
537 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
539 static int snd_rawmidi_release(struct inode *inode, struct file *file)
541 struct snd_rawmidi_file *rfile;
542 struct snd_rawmidi *rmidi;
543 struct module *module;
545 rfile = file->private_data;
546 rmidi = rfile->rmidi;
547 rawmidi_release_priv(rfile);
549 module = rmidi->card->module;
550 snd_card_file_remove(rmidi->card, file);
555 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
556 struct snd_rawmidi_info *info)
558 struct snd_rawmidi *rmidi;
560 if (substream == NULL)
562 rmidi = substream->rmidi;
563 memset(info, 0, sizeof(*info));
564 info->card = rmidi->card->number;
565 info->device = rmidi->device;
566 info->subdevice = substream->number;
567 info->stream = substream->stream;
568 info->flags = rmidi->info_flags;
569 strcpy(info->id, rmidi->id);
570 strcpy(info->name, rmidi->name);
571 strcpy(info->subname, substream->name);
572 info->subdevices_count = substream->pstr->substream_count;
573 info->subdevices_avail = (substream->pstr->substream_count -
574 substream->pstr->substream_opened);
578 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
579 struct snd_rawmidi_info __user *_info)
581 struct snd_rawmidi_info info;
584 err = snd_rawmidi_info(substream, &info);
587 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
592 static int __snd_rawmidi_info_select(struct snd_card *card,
593 struct snd_rawmidi_info *info)
595 struct snd_rawmidi *rmidi;
596 struct snd_rawmidi_str *pstr;
597 struct snd_rawmidi_substream *substream;
599 rmidi = snd_rawmidi_search(card, info->device);
602 if (info->stream < 0 || info->stream > 1)
604 pstr = &rmidi->streams[info->stream];
605 if (pstr->substream_count == 0)
607 if (info->subdevice >= pstr->substream_count)
609 list_for_each_entry(substream, &pstr->substreams, list) {
610 if ((unsigned int)substream->number == info->subdevice)
611 return snd_rawmidi_info(substream, info);
616 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
620 mutex_lock(®ister_mutex);
621 ret = __snd_rawmidi_info_select(card, info);
622 mutex_unlock(®ister_mutex);
625 EXPORT_SYMBOL(snd_rawmidi_info_select);
627 static int snd_rawmidi_info_select_user(struct snd_card *card,
628 struct snd_rawmidi_info __user *_info)
631 struct snd_rawmidi_info info;
633 if (get_user(info.device, &_info->device))
635 if (get_user(info.stream, &_info->stream))
637 if (get_user(info.subdevice, &_info->subdevice))
639 err = snd_rawmidi_info_select(card, &info);
642 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
647 static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime,
648 struct snd_rawmidi_params *params,
651 char *newbuf, *oldbuf;
653 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L)
655 if (params->avail_min < 1 || params->avail_min > params->buffer_size)
657 if (params->buffer_size != runtime->buffer_size) {
658 newbuf = kvzalloc(params->buffer_size, GFP_KERNEL);
661 spin_lock_irq(&runtime->lock);
662 oldbuf = runtime->buffer;
663 runtime->buffer = newbuf;
664 runtime->buffer_size = params->buffer_size;
665 __reset_runtime_ptrs(runtime, is_input);
666 spin_unlock_irq(&runtime->lock);
669 runtime->avail_min = params->avail_min;
673 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
674 struct snd_rawmidi_params *params)
676 if (substream->append && substream->use_count > 1)
678 snd_rawmidi_drain_output(substream);
679 substream->active_sensing = !params->no_active_sensing;
680 return resize_runtime_buffer(substream->runtime, params, false);
682 EXPORT_SYMBOL(snd_rawmidi_output_params);
684 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
685 struct snd_rawmidi_params *params)
687 snd_rawmidi_drain_input(substream);
688 return resize_runtime_buffer(substream->runtime, params, true);
690 EXPORT_SYMBOL(snd_rawmidi_input_params);
692 static int snd_rawmidi_output_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_OUTPUT;
699 spin_lock_irq(&runtime->lock);
700 status->avail = runtime->avail;
701 spin_unlock_irq(&runtime->lock);
705 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
706 struct snd_rawmidi_status *status)
708 struct snd_rawmidi_runtime *runtime = substream->runtime;
710 memset(status, 0, sizeof(*status));
711 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
712 spin_lock_irq(&runtime->lock);
713 status->avail = runtime->avail;
714 status->xruns = runtime->xruns;
716 spin_unlock_irq(&runtime->lock);
720 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
722 struct snd_rawmidi_file *rfile;
723 void __user *argp = (void __user *)arg;
725 rfile = file->private_data;
726 if (((cmd >> 8) & 0xff) != 'W')
729 case SNDRV_RAWMIDI_IOCTL_PVERSION:
730 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
731 case SNDRV_RAWMIDI_IOCTL_INFO:
734 struct snd_rawmidi_info __user *info = argp;
736 if (get_user(stream, &info->stream))
739 case SNDRV_RAWMIDI_STREAM_INPUT:
740 return snd_rawmidi_info_user(rfile->input, info);
741 case SNDRV_RAWMIDI_STREAM_OUTPUT:
742 return snd_rawmidi_info_user(rfile->output, info);
747 case SNDRV_RAWMIDI_IOCTL_PARAMS:
749 struct snd_rawmidi_params params;
751 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
753 switch (params.stream) {
754 case SNDRV_RAWMIDI_STREAM_OUTPUT:
755 if (rfile->output == NULL)
757 return snd_rawmidi_output_params(rfile->output, ¶ms);
758 case SNDRV_RAWMIDI_STREAM_INPUT:
759 if (rfile->input == NULL)
761 return snd_rawmidi_input_params(rfile->input, ¶ms);
766 case SNDRV_RAWMIDI_IOCTL_STATUS:
769 struct snd_rawmidi_status status;
771 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
773 switch (status.stream) {
774 case SNDRV_RAWMIDI_STREAM_OUTPUT:
775 if (rfile->output == NULL)
777 err = snd_rawmidi_output_status(rfile->output, &status);
779 case SNDRV_RAWMIDI_STREAM_INPUT:
780 if (rfile->input == NULL)
782 err = snd_rawmidi_input_status(rfile->input, &status);
789 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
793 case SNDRV_RAWMIDI_IOCTL_DROP:
797 if (get_user(val, (int __user *) argp))
800 case SNDRV_RAWMIDI_STREAM_OUTPUT:
801 if (rfile->output == NULL)
803 return snd_rawmidi_drop_output(rfile->output);
808 case SNDRV_RAWMIDI_IOCTL_DRAIN:
812 if (get_user(val, (int __user *) argp))
815 case SNDRV_RAWMIDI_STREAM_OUTPUT:
816 if (rfile->output == NULL)
818 return snd_rawmidi_drain_output(rfile->output);
819 case SNDRV_RAWMIDI_STREAM_INPUT:
820 if (rfile->input == NULL)
822 return snd_rawmidi_drain_input(rfile->input);
828 rmidi_dbg(rfile->rmidi,
829 "rawmidi: unknown command = 0x%x\n", cmd);
834 static int snd_rawmidi_control_ioctl(struct snd_card *card,
835 struct snd_ctl_file *control,
839 void __user *argp = (void __user *)arg;
842 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
846 if (get_user(device, (int __user *)argp))
848 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
849 device = SNDRV_RAWMIDI_DEVICES - 1;
850 mutex_lock(®ister_mutex);
851 device = device < 0 ? 0 : device + 1;
852 while (device < SNDRV_RAWMIDI_DEVICES) {
853 if (snd_rawmidi_search(card, device))
857 if (device == SNDRV_RAWMIDI_DEVICES)
859 mutex_unlock(®ister_mutex);
860 if (put_user(device, (int __user *)argp))
864 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
868 if (get_user(val, (int __user *)argp))
870 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
873 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
874 return snd_rawmidi_info_select_user(card, argp);
880 * snd_rawmidi_receive - receive the input data from the device
881 * @substream: the rawmidi substream
882 * @buffer: the buffer pointer
883 * @count: the data size to read
885 * Reads the data from the internal buffer.
887 * Return: The size of read data, or a negative error code on failure.
889 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
890 const unsigned char *buffer, int count)
893 int result = 0, count1;
894 struct snd_rawmidi_runtime *runtime = substream->runtime;
896 if (!substream->opened)
898 if (runtime->buffer == NULL) {
899 rmidi_dbg(substream->rmidi,
900 "snd_rawmidi_receive: input is not active!!!\n");
903 spin_lock_irqsave(&runtime->lock, flags);
904 if (count == 1) { /* special case, faster code */
906 if (runtime->avail < runtime->buffer_size) {
907 runtime->buffer[runtime->hw_ptr++] = buffer[0];
908 runtime->hw_ptr %= runtime->buffer_size;
915 substream->bytes += count;
916 count1 = runtime->buffer_size - runtime->hw_ptr;
919 if (count1 > (int)(runtime->buffer_size - runtime->avail))
920 count1 = runtime->buffer_size - runtime->avail;
921 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
922 runtime->hw_ptr += count1;
923 runtime->hw_ptr %= runtime->buffer_size;
924 runtime->avail += count1;
930 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
931 count1 = runtime->buffer_size - runtime->avail;
932 runtime->xruns += count - count1;
935 memcpy(runtime->buffer, buffer, count1);
936 runtime->hw_ptr = count1;
937 runtime->avail += count1;
944 schedule_work(&runtime->event_work);
945 else if (snd_rawmidi_ready(substream))
946 wake_up(&runtime->sleep);
948 spin_unlock_irqrestore(&runtime->lock, flags);
951 EXPORT_SYMBOL(snd_rawmidi_receive);
953 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
954 unsigned char __user *userbuf,
955 unsigned char *kernelbuf, long count)
958 long result = 0, count1;
959 struct snd_rawmidi_runtime *runtime = substream->runtime;
960 unsigned long appl_ptr;
962 spin_lock_irqsave(&runtime->lock, flags);
963 while (count > 0 && runtime->avail) {
964 count1 = runtime->buffer_size - runtime->appl_ptr;
967 if (count1 > (int)runtime->avail)
968 count1 = runtime->avail;
970 /* update runtime->appl_ptr before unlocking for userbuf */
971 appl_ptr = runtime->appl_ptr;
972 runtime->appl_ptr += count1;
973 runtime->appl_ptr %= runtime->buffer_size;
974 runtime->avail -= count1;
977 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
979 spin_unlock_irqrestore(&runtime->lock, flags);
980 if (copy_to_user(userbuf + result,
981 runtime->buffer + appl_ptr, count1)) {
982 return result > 0 ? result : -EFAULT;
984 spin_lock_irqsave(&runtime->lock, flags);
989 spin_unlock_irqrestore(&runtime->lock, flags);
993 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
994 unsigned char *buf, long count)
996 snd_rawmidi_input_trigger(substream, 1);
997 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
999 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1001 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1006 struct snd_rawmidi_file *rfile;
1007 struct snd_rawmidi_substream *substream;
1008 struct snd_rawmidi_runtime *runtime;
1010 rfile = file->private_data;
1011 substream = rfile->input;
1012 if (substream == NULL)
1014 runtime = substream->runtime;
1015 snd_rawmidi_input_trigger(substream, 1);
1018 spin_lock_irq(&runtime->lock);
1019 while (!snd_rawmidi_ready(substream)) {
1020 wait_queue_entry_t wait;
1022 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1023 spin_unlock_irq(&runtime->lock);
1024 return result > 0 ? result : -EAGAIN;
1026 init_waitqueue_entry(&wait, current);
1027 add_wait_queue(&runtime->sleep, &wait);
1028 set_current_state(TASK_INTERRUPTIBLE);
1029 spin_unlock_irq(&runtime->lock);
1031 remove_wait_queue(&runtime->sleep, &wait);
1032 if (rfile->rmidi->card->shutdown)
1034 if (signal_pending(current))
1035 return result > 0 ? result : -ERESTARTSYS;
1036 if (!runtime->avail)
1037 return result > 0 ? result : -EIO;
1038 spin_lock_irq(&runtime->lock);
1040 spin_unlock_irq(&runtime->lock);
1041 count1 = snd_rawmidi_kernel_read1(substream,
1042 (unsigned char __user *)buf,
1046 return result > 0 ? result : count1;
1055 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1056 * @substream: the rawmidi substream
1058 * Return: 1 if the internal output buffer is empty, 0 if not.
1060 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1062 struct snd_rawmidi_runtime *runtime = substream->runtime;
1064 unsigned long flags;
1066 if (runtime->buffer == NULL) {
1067 rmidi_dbg(substream->rmidi,
1068 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1071 spin_lock_irqsave(&runtime->lock, flags);
1072 result = runtime->avail >= runtime->buffer_size;
1073 spin_unlock_irqrestore(&runtime->lock, flags);
1076 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1079 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1080 * @substream: the rawmidi substream
1081 * @buffer: the buffer pointer
1082 * @count: data size to transfer
1084 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1086 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1087 unsigned char *buffer, int count)
1090 struct snd_rawmidi_runtime *runtime = substream->runtime;
1092 if (runtime->buffer == NULL) {
1093 rmidi_dbg(substream->rmidi,
1094 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1098 if (runtime->avail >= runtime->buffer_size) {
1099 /* warning: lowlevel layer MUST trigger down the hardware */
1102 if (count == 1) { /* special case, faster code */
1103 *buffer = runtime->buffer[runtime->hw_ptr];
1106 count1 = runtime->buffer_size - runtime->hw_ptr;
1109 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1110 count1 = runtime->buffer_size - runtime->avail;
1111 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1115 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1116 count = runtime->buffer_size - runtime->avail - count1;
1117 memcpy(buffer + count1, runtime->buffer, count);
1124 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1127 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1128 * @substream: the rawmidi substream
1129 * @buffer: the buffer pointer
1130 * @count: data size to transfer
1132 * Copies data from the internal output buffer to the given buffer.
1134 * Call this in the interrupt handler when the midi output is ready,
1135 * and call snd_rawmidi_transmit_ack() after the transmission is
1138 * Return: The size of copied data, or a negative error code on failure.
1140 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1141 unsigned char *buffer, int count)
1143 struct snd_rawmidi_runtime *runtime = substream->runtime;
1145 unsigned long flags;
1147 spin_lock_irqsave(&runtime->lock, flags);
1148 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1149 spin_unlock_irqrestore(&runtime->lock, flags);
1152 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1155 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1156 * @substream: the rawmidi substream
1157 * @count: the transferred count
1159 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1161 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1163 struct snd_rawmidi_runtime *runtime = substream->runtime;
1165 if (runtime->buffer == NULL) {
1166 rmidi_dbg(substream->rmidi,
1167 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1170 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1171 runtime->hw_ptr += count;
1172 runtime->hw_ptr %= runtime->buffer_size;
1173 runtime->avail += count;
1174 substream->bytes += count;
1176 if (runtime->drain || snd_rawmidi_ready(substream))
1177 wake_up(&runtime->sleep);
1181 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1184 * snd_rawmidi_transmit_ack - acknowledge the transmission
1185 * @substream: the rawmidi substream
1186 * @count: the transferred count
1188 * Advances the hardware pointer for the internal output buffer with
1189 * the given size and updates the condition.
1190 * Call after the transmission is finished.
1192 * Return: The advanced size if successful, or a negative error code on failure.
1194 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1196 struct snd_rawmidi_runtime *runtime = substream->runtime;
1198 unsigned long flags;
1200 spin_lock_irqsave(&runtime->lock, flags);
1201 result = __snd_rawmidi_transmit_ack(substream, count);
1202 spin_unlock_irqrestore(&runtime->lock, flags);
1205 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1208 * snd_rawmidi_transmit - copy from the buffer to the device
1209 * @substream: the rawmidi substream
1210 * @buffer: the buffer pointer
1211 * @count: the data size to transfer
1213 * Copies data from the buffer to the device and advances the pointer.
1215 * Return: The copied size if successful, or a negative error code on failure.
1217 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1218 unsigned char *buffer, int count)
1220 struct snd_rawmidi_runtime *runtime = substream->runtime;
1222 unsigned long flags;
1224 spin_lock_irqsave(&runtime->lock, flags);
1225 if (!substream->opened)
1228 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1232 result = __snd_rawmidi_transmit_ack(substream, count);
1234 spin_unlock_irqrestore(&runtime->lock, flags);
1237 EXPORT_SYMBOL(snd_rawmidi_transmit);
1240 * snd_rawmidi_proceed - Discard the all pending bytes and proceed
1241 * @substream: rawmidi substream
1243 * Return: the number of discarded bytes
1245 int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream)
1247 struct snd_rawmidi_runtime *runtime = substream->runtime;
1248 unsigned long flags;
1251 spin_lock_irqsave(&runtime->lock, flags);
1252 if (runtime->avail < runtime->buffer_size) {
1253 count = runtime->buffer_size - runtime->avail;
1254 __snd_rawmidi_transmit_ack(substream, count);
1256 spin_unlock_irqrestore(&runtime->lock, flags);
1259 EXPORT_SYMBOL(snd_rawmidi_proceed);
1261 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1262 const unsigned char __user *userbuf,
1263 const unsigned char *kernelbuf,
1266 unsigned long flags;
1267 long count1, result;
1268 struct snd_rawmidi_runtime *runtime = substream->runtime;
1269 unsigned long appl_ptr;
1271 if (!kernelbuf && !userbuf)
1273 if (snd_BUG_ON(!runtime->buffer))
1277 spin_lock_irqsave(&runtime->lock, flags);
1278 if (substream->append) {
1279 if ((long)runtime->avail < count) {
1280 spin_unlock_irqrestore(&runtime->lock, flags);
1284 while (count > 0 && runtime->avail > 0) {
1285 count1 = runtime->buffer_size - runtime->appl_ptr;
1288 if (count1 > (long)runtime->avail)
1289 count1 = runtime->avail;
1291 /* update runtime->appl_ptr before unlocking for userbuf */
1292 appl_ptr = runtime->appl_ptr;
1293 runtime->appl_ptr += count1;
1294 runtime->appl_ptr %= runtime->buffer_size;
1295 runtime->avail -= count1;
1298 memcpy(runtime->buffer + appl_ptr,
1299 kernelbuf + result, count1);
1301 spin_unlock_irqrestore(&runtime->lock, flags);
1302 if (copy_from_user(runtime->buffer + appl_ptr,
1303 userbuf + result, count1)) {
1304 spin_lock_irqsave(&runtime->lock, flags);
1305 result = result > 0 ? result : -EFAULT;
1308 spin_lock_irqsave(&runtime->lock, flags);
1314 count1 = runtime->avail < runtime->buffer_size;
1315 spin_unlock_irqrestore(&runtime->lock, flags);
1317 snd_rawmidi_output_trigger(substream, 1);
1321 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1322 const unsigned char *buf, long count)
1324 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1326 EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1328 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1329 size_t count, loff_t *offset)
1331 long result, timeout;
1333 struct snd_rawmidi_file *rfile;
1334 struct snd_rawmidi_runtime *runtime;
1335 struct snd_rawmidi_substream *substream;
1337 rfile = file->private_data;
1338 substream = rfile->output;
1339 runtime = substream->runtime;
1340 /* we cannot put an atomic message to our buffer */
1341 if (substream->append && count > runtime->buffer_size)
1345 spin_lock_irq(&runtime->lock);
1346 while (!snd_rawmidi_ready_append(substream, count)) {
1347 wait_queue_entry_t wait;
1349 if (file->f_flags & O_NONBLOCK) {
1350 spin_unlock_irq(&runtime->lock);
1351 return result > 0 ? result : -EAGAIN;
1353 init_waitqueue_entry(&wait, current);
1354 add_wait_queue(&runtime->sleep, &wait);
1355 set_current_state(TASK_INTERRUPTIBLE);
1356 spin_unlock_irq(&runtime->lock);
1357 timeout = schedule_timeout(30 * HZ);
1358 remove_wait_queue(&runtime->sleep, &wait);
1359 if (rfile->rmidi->card->shutdown)
1361 if (signal_pending(current))
1362 return result > 0 ? result : -ERESTARTSYS;
1363 if (!runtime->avail && !timeout)
1364 return result > 0 ? result : -EIO;
1365 spin_lock_irq(&runtime->lock);
1367 spin_unlock_irq(&runtime->lock);
1368 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1370 return result > 0 ? result : count1;
1373 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1377 if (file->f_flags & O_DSYNC) {
1378 spin_lock_irq(&runtime->lock);
1379 while (runtime->avail != runtime->buffer_size) {
1380 wait_queue_entry_t wait;
1381 unsigned int last_avail = runtime->avail;
1383 init_waitqueue_entry(&wait, current);
1384 add_wait_queue(&runtime->sleep, &wait);
1385 set_current_state(TASK_INTERRUPTIBLE);
1386 spin_unlock_irq(&runtime->lock);
1387 timeout = schedule_timeout(30 * HZ);
1388 remove_wait_queue(&runtime->sleep, &wait);
1389 if (signal_pending(current))
1390 return result > 0 ? result : -ERESTARTSYS;
1391 if (runtime->avail == last_avail && !timeout)
1392 return result > 0 ? result : -EIO;
1393 spin_lock_irq(&runtime->lock);
1395 spin_unlock_irq(&runtime->lock);
1400 static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait)
1402 struct snd_rawmidi_file *rfile;
1403 struct snd_rawmidi_runtime *runtime;
1406 rfile = file->private_data;
1407 if (rfile->input != NULL) {
1408 runtime = rfile->input->runtime;
1409 snd_rawmidi_input_trigger(rfile->input, 1);
1410 poll_wait(file, &runtime->sleep, wait);
1412 if (rfile->output != NULL) {
1413 runtime = rfile->output->runtime;
1414 poll_wait(file, &runtime->sleep, wait);
1417 if (rfile->input != NULL) {
1418 if (snd_rawmidi_ready(rfile->input))
1419 mask |= EPOLLIN | EPOLLRDNORM;
1421 if (rfile->output != NULL) {
1422 if (snd_rawmidi_ready(rfile->output))
1423 mask |= EPOLLOUT | EPOLLWRNORM;
1430 #ifdef CONFIG_COMPAT
1431 #include "rawmidi_compat.c"
1433 #define snd_rawmidi_ioctl_compat NULL
1439 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1440 struct snd_info_buffer *buffer)
1442 struct snd_rawmidi *rmidi;
1443 struct snd_rawmidi_substream *substream;
1444 struct snd_rawmidi_runtime *runtime;
1446 rmidi = entry->private_data;
1447 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1448 mutex_lock(&rmidi->open_mutex);
1449 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1450 list_for_each_entry(substream,
1451 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1455 " Tx bytes : %lu\n",
1457 (unsigned long) substream->bytes);
1458 if (substream->opened) {
1460 " Owner PID : %d\n",
1461 pid_vnr(substream->pid));
1462 runtime = substream->runtime;
1465 " Buffer size : %lu\n"
1467 runtime->oss ? "OSS compatible" : "native",
1468 (unsigned long) runtime->buffer_size,
1469 (unsigned long) runtime->avail);
1473 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1474 list_for_each_entry(substream,
1475 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1479 " Rx bytes : %lu\n",
1481 (unsigned long) substream->bytes);
1482 if (substream->opened) {
1484 " Owner PID : %d\n",
1485 pid_vnr(substream->pid));
1486 runtime = substream->runtime;
1488 " Buffer size : %lu\n"
1490 " Overruns : %lu\n",
1491 (unsigned long) runtime->buffer_size,
1492 (unsigned long) runtime->avail,
1493 (unsigned long) runtime->xruns);
1497 mutex_unlock(&rmidi->open_mutex);
1501 * Register functions
1504 static const struct file_operations snd_rawmidi_f_ops = {
1505 .owner = THIS_MODULE,
1506 .read = snd_rawmidi_read,
1507 .write = snd_rawmidi_write,
1508 .open = snd_rawmidi_open,
1509 .release = snd_rawmidi_release,
1510 .llseek = no_llseek,
1511 .poll = snd_rawmidi_poll,
1512 .unlocked_ioctl = snd_rawmidi_ioctl,
1513 .compat_ioctl = snd_rawmidi_ioctl_compat,
1516 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1517 struct snd_rawmidi_str *stream,
1521 struct snd_rawmidi_substream *substream;
1524 for (idx = 0; idx < count; idx++) {
1525 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1528 substream->stream = direction;
1529 substream->number = idx;
1530 substream->rmidi = rmidi;
1531 substream->pstr = stream;
1532 list_add_tail(&substream->list, &stream->substreams);
1533 stream->substream_count++;
1538 static void release_rawmidi_device(struct device *dev)
1540 kfree(container_of(dev, struct snd_rawmidi, dev));
1544 * snd_rawmidi_new - create a rawmidi instance
1545 * @card: the card instance
1546 * @id: the id string
1547 * @device: the device index
1548 * @output_count: the number of output streams
1549 * @input_count: the number of input streams
1550 * @rrawmidi: the pointer to store the new rawmidi instance
1552 * Creates a new rawmidi instance.
1553 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1555 * Return: Zero if successful, or a negative error code on failure.
1557 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1558 int output_count, int input_count,
1559 struct snd_rawmidi **rrawmidi)
1561 struct snd_rawmidi *rmidi;
1563 static struct snd_device_ops ops = {
1564 .dev_free = snd_rawmidi_dev_free,
1565 .dev_register = snd_rawmidi_dev_register,
1566 .dev_disconnect = snd_rawmidi_dev_disconnect,
1569 if (snd_BUG_ON(!card))
1573 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1577 rmidi->device = device;
1578 mutex_init(&rmidi->open_mutex);
1579 init_waitqueue_head(&rmidi->open_wait);
1580 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1581 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1584 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1586 snd_device_initialize(&rmidi->dev, card);
1587 rmidi->dev.release = release_rawmidi_device;
1588 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1590 err = snd_rawmidi_alloc_substreams(rmidi,
1591 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1592 SNDRV_RAWMIDI_STREAM_INPUT,
1596 err = snd_rawmidi_alloc_substreams(rmidi,
1597 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1598 SNDRV_RAWMIDI_STREAM_OUTPUT,
1602 err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops);
1611 snd_rawmidi_free(rmidi);
1614 EXPORT_SYMBOL(snd_rawmidi_new);
1616 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1618 struct snd_rawmidi_substream *substream;
1620 while (!list_empty(&stream->substreams)) {
1621 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1622 list_del(&substream->list);
1627 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1632 snd_info_free_entry(rmidi->proc_entry);
1633 rmidi->proc_entry = NULL;
1634 mutex_lock(®ister_mutex);
1635 if (rmidi->ops && rmidi->ops->dev_unregister)
1636 rmidi->ops->dev_unregister(rmidi);
1637 mutex_unlock(®ister_mutex);
1639 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1640 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1641 if (rmidi->private_free)
1642 rmidi->private_free(rmidi);
1643 put_device(&rmidi->dev);
1647 static int snd_rawmidi_dev_free(struct snd_device *device)
1649 struct snd_rawmidi *rmidi = device->device_data;
1651 return snd_rawmidi_free(rmidi);
1654 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1655 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1657 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)
1673 mutex_lock(®ister_mutex);
1674 if (snd_rawmidi_search(rmidi->card, rmidi->device))
1677 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1678 mutex_unlock(®ister_mutex);
1682 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1683 rmidi->card, rmidi->device,
1684 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
1686 rmidi_err(rmidi, "unable to register\n");
1689 if (rmidi->ops && rmidi->ops->dev_register) {
1690 err = rmidi->ops->dev_register(rmidi);
1692 goto error_unregister;
1694 #ifdef CONFIG_SND_OSSEMUL
1696 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1697 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1698 rmidi->card, 0, &snd_rawmidi_f_ops,
1701 "unable to register OSS rawmidi device %i:%i\n",
1702 rmidi->card->number, 0);
1705 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1706 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1710 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1711 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1712 rmidi->card, 1, &snd_rawmidi_f_ops,
1715 "unable to register OSS rawmidi device %i:%i\n",
1716 rmidi->card->number, 1);
1721 #endif /* CONFIG_SND_OSSEMUL */
1722 sprintf(name, "midi%d", rmidi->device);
1723 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1725 entry->private_data = rmidi;
1726 entry->c.text.read = snd_rawmidi_proc_info_read;
1727 if (snd_info_register(entry) < 0) {
1728 snd_info_free_entry(entry);
1732 rmidi->proc_entry = entry;
1733 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1734 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1735 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1736 rmidi->seq_dev->private_data = rmidi;
1737 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1738 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1739 snd_device_register(rmidi->card, rmidi->seq_dev);
1746 snd_unregister_device(&rmidi->dev);
1748 mutex_lock(®ister_mutex);
1749 list_del(&rmidi->list);
1750 mutex_unlock(®ister_mutex);
1754 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1756 struct snd_rawmidi *rmidi = device->device_data;
1759 mutex_lock(®ister_mutex);
1760 mutex_lock(&rmidi->open_mutex);
1761 wake_up(&rmidi->open_wait);
1762 list_del_init(&rmidi->list);
1763 for (dir = 0; dir < 2; dir++) {
1764 struct snd_rawmidi_substream *s;
1766 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1768 wake_up(&s->runtime->sleep);
1772 #ifdef CONFIG_SND_OSSEMUL
1773 if (rmidi->ossreg) {
1774 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1775 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1776 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1777 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1780 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1781 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1784 #endif /* CONFIG_SND_OSSEMUL */
1785 snd_unregister_device(&rmidi->dev);
1786 mutex_unlock(&rmidi->open_mutex);
1787 mutex_unlock(®ister_mutex);
1792 * snd_rawmidi_set_ops - set the rawmidi operators
1793 * @rmidi: the rawmidi instance
1794 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1795 * @ops: the operator table
1797 * Sets the rawmidi operators for the given stream direction.
1799 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1800 const struct snd_rawmidi_ops *ops)
1802 struct snd_rawmidi_substream *substream;
1804 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1805 substream->ops = ops;
1807 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1813 static int __init alsa_rawmidi_init(void)
1816 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1817 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1818 #ifdef CONFIG_SND_OSSEMUL
1820 /* check device map table */
1821 for (i = 0; i < SNDRV_CARDS; i++) {
1822 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1823 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1827 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1828 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1834 #endif /* CONFIG_SND_OSSEMUL */
1838 static void __exit alsa_rawmidi_exit(void)
1840 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1841 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1844 module_init(alsa_rawmidi_init)
1845 module_exit(alsa_rawmidi_exit)