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 <linux/nospec.h>
34 #include <sound/rawmidi.h>
35 #include <sound/info.h>
36 #include <sound/control.h>
37 #include <sound/minors.h>
38 #include <sound/initval.h>
40 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
41 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
42 MODULE_LICENSE("GPL");
44 #ifdef CONFIG_SND_OSSEMUL
45 static int midi_map[SNDRV_CARDS];
46 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
47 module_param_array(midi_map, int, NULL, 0444);
48 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
49 module_param_array(amidi_map, int, NULL, 0444);
50 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
51 #endif /* CONFIG_SND_OSSEMUL */
53 static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
54 static int snd_rawmidi_dev_free(struct snd_device *device);
55 static int snd_rawmidi_dev_register(struct snd_device *device);
56 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
58 static LIST_HEAD(snd_rawmidi_devices);
59 static DEFINE_MUTEX(register_mutex);
61 #define rmidi_err(rmidi, fmt, args...) \
62 dev_err(&(rmidi)->dev, fmt, ##args)
63 #define rmidi_warn(rmidi, fmt, args...) \
64 dev_warn(&(rmidi)->dev, fmt, ##args)
65 #define rmidi_dbg(rmidi, fmt, args...) \
66 dev_dbg(&(rmidi)->dev, fmt, ##args)
68 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
70 struct snd_rawmidi *rawmidi;
72 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
73 if (rawmidi->card == card && rawmidi->device == device)
78 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
80 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
82 return SNDRV_RAWMIDI_LFLG_OUTPUT;
84 return SNDRV_RAWMIDI_LFLG_INPUT;
86 return SNDRV_RAWMIDI_LFLG_OPEN;
90 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
92 struct snd_rawmidi_runtime *runtime = substream->runtime;
94 return runtime->avail >= runtime->avail_min;
97 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
100 struct snd_rawmidi_runtime *runtime = substream->runtime;
102 return runtime->avail >= runtime->avail_min &&
103 (!substream->append || runtime->avail >= count);
106 static void snd_rawmidi_input_event_work(struct work_struct *work)
108 struct snd_rawmidi_runtime *runtime =
109 container_of(work, struct snd_rawmidi_runtime, event_work);
112 runtime->event(runtime->substream);
115 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
117 struct snd_rawmidi_runtime *runtime;
119 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
122 runtime->substream = substream;
123 spin_lock_init(&runtime->lock);
124 init_waitqueue_head(&runtime->sleep);
125 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
126 runtime->event = NULL;
127 runtime->buffer_size = PAGE_SIZE;
128 runtime->avail_min = 1;
129 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
132 runtime->avail = runtime->buffer_size;
133 runtime->buffer = kvzalloc(runtime->buffer_size, GFP_KERNEL);
134 if (!runtime->buffer) {
138 runtime->appl_ptr = runtime->hw_ptr = 0;
139 substream->runtime = runtime;
143 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
145 struct snd_rawmidi_runtime *runtime = substream->runtime;
147 kvfree(runtime->buffer);
149 substream->runtime = NULL;
153 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
155 if (!substream->opened)
157 substream->ops->trigger(substream, up);
160 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
162 if (!substream->opened)
164 substream->ops->trigger(substream, up);
166 cancel_work_sync(&substream->runtime->event_work);
169 static void __reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
173 runtime->appl_ptr = runtime->hw_ptr = 0;
174 runtime->avail = is_input ? 0 : runtime->buffer_size;
177 static void reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
182 spin_lock_irqsave(&runtime->lock, flags);
183 __reset_runtime_ptrs(runtime, is_input);
184 spin_unlock_irqrestore(&runtime->lock, flags);
187 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
189 snd_rawmidi_output_trigger(substream, 0);
190 reset_runtime_ptrs(substream->runtime, false);
193 EXPORT_SYMBOL(snd_rawmidi_drop_output);
195 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
199 struct snd_rawmidi_runtime *runtime = substream->runtime;
203 timeout = wait_event_interruptible_timeout(runtime->sleep,
204 (runtime->avail >= runtime->buffer_size),
206 if (signal_pending(current))
208 if (runtime->avail < runtime->buffer_size && !timeout) {
209 rmidi_warn(substream->rmidi,
210 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
211 (long)runtime->avail, (long)runtime->buffer_size);
215 if (err != -ERESTARTSYS) {
216 /* we need wait a while to make sure that Tx FIFOs are empty */
217 if (substream->ops->drain)
218 substream->ops->drain(substream);
221 snd_rawmidi_drop_output(substream);
225 EXPORT_SYMBOL(snd_rawmidi_drain_output);
227 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
229 snd_rawmidi_input_trigger(substream, 0);
230 reset_runtime_ptrs(substream->runtime, true);
233 EXPORT_SYMBOL(snd_rawmidi_drain_input);
235 /* look for an available substream for the given stream direction;
236 * if a specific subdevice is given, try to assign it
238 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
239 int stream, int mode,
240 struct snd_rawmidi_substream **sub_ret)
242 struct snd_rawmidi_substream *substream;
243 struct snd_rawmidi_str *s = &rmidi->streams[stream];
244 static unsigned int info_flags[2] = {
245 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
246 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
249 if (!(rmidi->info_flags & info_flags[stream]))
251 if (subdevice >= 0 && subdevice >= s->substream_count)
254 list_for_each_entry(substream, &s->substreams, list) {
255 if (substream->opened) {
256 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
257 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
261 if (subdevice < 0 || subdevice == substream->number) {
262 *sub_ret = substream;
269 /* open and do ref-counting for the given substream */
270 static int open_substream(struct snd_rawmidi *rmidi,
271 struct snd_rawmidi_substream *substream,
276 if (substream->use_count == 0) {
277 err = snd_rawmidi_runtime_create(substream);
280 err = substream->ops->open(substream);
282 snd_rawmidi_runtime_free(substream);
285 substream->opened = 1;
286 substream->active_sensing = 0;
287 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
288 substream->append = 1;
289 substream->pid = get_pid(task_pid(current));
290 rmidi->streams[substream->stream].substream_opened++;
292 substream->use_count++;
296 static void close_substream(struct snd_rawmidi *rmidi,
297 struct snd_rawmidi_substream *substream,
300 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
301 struct snd_rawmidi_file *rfile)
303 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
306 rfile->input = rfile->output = NULL;
307 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
308 err = assign_substream(rmidi, subdevice,
309 SNDRV_RAWMIDI_STREAM_INPUT,
314 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
315 err = assign_substream(rmidi, subdevice,
316 SNDRV_RAWMIDI_STREAM_OUTPUT,
323 err = open_substream(rmidi, sinput, mode);
328 err = open_substream(rmidi, soutput, mode);
331 close_substream(rmidi, sinput, 0);
336 rfile->rmidi = rmidi;
337 rfile->input = sinput;
338 rfile->output = soutput;
342 /* called from sound/core/seq/seq_midi.c */
343 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
344 int mode, struct snd_rawmidi_file *rfile)
346 struct snd_rawmidi *rmidi;
349 if (snd_BUG_ON(!rfile))
352 mutex_lock(®ister_mutex);
353 rmidi = snd_rawmidi_search(card, device);
356 else if (!try_module_get(rmidi->card->module))
358 mutex_unlock(®ister_mutex);
362 mutex_lock(&rmidi->open_mutex);
363 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
364 mutex_unlock(&rmidi->open_mutex);
366 module_put(rmidi->card->module);
369 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
371 static int snd_rawmidi_open(struct inode *inode, struct file *file)
373 int maj = imajor(inode);
374 struct snd_card *card;
376 unsigned short fflags;
378 struct snd_rawmidi *rmidi;
379 struct snd_rawmidi_file *rawmidi_file = NULL;
380 wait_queue_entry_t wait;
382 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
383 return -EINVAL; /* invalid combination */
385 err = nonseekable_open(inode, file);
389 if (maj == snd_major) {
390 rmidi = snd_lookup_minor_data(iminor(inode),
391 SNDRV_DEVICE_TYPE_RAWMIDI);
392 #ifdef CONFIG_SND_OSSEMUL
393 } else if (maj == SOUND_MAJOR) {
394 rmidi = snd_lookup_oss_minor_data(iminor(inode),
395 SNDRV_OSS_DEVICE_TYPE_MIDI);
403 if (!try_module_get(rmidi->card->module)) {
404 snd_card_unref(rmidi->card);
408 mutex_lock(&rmidi->open_mutex);
410 err = snd_card_file_add(card, file);
413 fflags = snd_rawmidi_file_flags(file);
414 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
415 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
416 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
417 if (rawmidi_file == NULL) {
421 init_waitqueue_entry(&wait, current);
422 add_wait_queue(&rmidi->open_wait, &wait);
424 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
425 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
428 if (err == -EAGAIN) {
429 if (file->f_flags & O_NONBLOCK) {
435 set_current_state(TASK_INTERRUPTIBLE);
436 mutex_unlock(&rmidi->open_mutex);
438 mutex_lock(&rmidi->open_mutex);
439 if (rmidi->card->shutdown) {
443 if (signal_pending(current)) {
448 remove_wait_queue(&rmidi->open_wait, &wait);
453 #ifdef CONFIG_SND_OSSEMUL
454 if (rawmidi_file->input && rawmidi_file->input->runtime)
455 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
456 if (rawmidi_file->output && rawmidi_file->output->runtime)
457 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
459 file->private_data = rawmidi_file;
460 mutex_unlock(&rmidi->open_mutex);
461 snd_card_unref(rmidi->card);
465 snd_card_file_remove(card, file);
467 mutex_unlock(&rmidi->open_mutex);
468 module_put(rmidi->card->module);
469 snd_card_unref(rmidi->card);
473 static void close_substream(struct snd_rawmidi *rmidi,
474 struct snd_rawmidi_substream *substream,
477 if (--substream->use_count)
481 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
482 snd_rawmidi_input_trigger(substream, 0);
484 if (substream->active_sensing) {
485 unsigned char buf = 0xfe;
486 /* sending single active sensing message
487 * to shut the device up
489 snd_rawmidi_kernel_write(substream, &buf, 1);
491 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
492 snd_rawmidi_output_trigger(substream, 0);
495 substream->ops->close(substream);
496 if (substream->runtime->private_free)
497 substream->runtime->private_free(substream);
498 snd_rawmidi_runtime_free(substream);
499 substream->opened = 0;
500 substream->append = 0;
501 put_pid(substream->pid);
502 substream->pid = NULL;
503 rmidi->streams[substream->stream].substream_opened--;
506 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
508 struct snd_rawmidi *rmidi;
510 rmidi = rfile->rmidi;
511 mutex_lock(&rmidi->open_mutex);
513 close_substream(rmidi, rfile->input, 1);
517 close_substream(rmidi, rfile->output, 1);
518 rfile->output = NULL;
521 mutex_unlock(&rmidi->open_mutex);
522 wake_up(&rmidi->open_wait);
525 /* called from sound/core/seq/seq_midi.c */
526 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
528 struct snd_rawmidi *rmidi;
530 if (snd_BUG_ON(!rfile))
533 rmidi = rfile->rmidi;
534 rawmidi_release_priv(rfile);
535 module_put(rmidi->card->module);
538 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
540 static int snd_rawmidi_release(struct inode *inode, struct file *file)
542 struct snd_rawmidi_file *rfile;
543 struct snd_rawmidi *rmidi;
544 struct module *module;
546 rfile = file->private_data;
547 rmidi = rfile->rmidi;
548 rawmidi_release_priv(rfile);
550 module = rmidi->card->module;
551 snd_card_file_remove(rmidi->card, file);
556 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
557 struct snd_rawmidi_info *info)
559 struct snd_rawmidi *rmidi;
561 if (substream == NULL)
563 rmidi = substream->rmidi;
564 memset(info, 0, sizeof(*info));
565 info->card = rmidi->card->number;
566 info->device = rmidi->device;
567 info->subdevice = substream->number;
568 info->stream = substream->stream;
569 info->flags = rmidi->info_flags;
570 strcpy(info->id, rmidi->id);
571 strcpy(info->name, rmidi->name);
572 strcpy(info->subname, substream->name);
573 info->subdevices_count = substream->pstr->substream_count;
574 info->subdevices_avail = (substream->pstr->substream_count -
575 substream->pstr->substream_opened);
579 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
580 struct snd_rawmidi_info __user *_info)
582 struct snd_rawmidi_info info;
585 err = snd_rawmidi_info(substream, &info);
588 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
593 static int __snd_rawmidi_info_select(struct snd_card *card,
594 struct snd_rawmidi_info *info)
596 struct snd_rawmidi *rmidi;
597 struct snd_rawmidi_str *pstr;
598 struct snd_rawmidi_substream *substream;
600 rmidi = snd_rawmidi_search(card, info->device);
603 if (info->stream < 0 || info->stream > 1)
605 info->stream = array_index_nospec(info->stream, 2);
606 pstr = &rmidi->streams[info->stream];
607 if (pstr->substream_count == 0)
609 if (info->subdevice >= pstr->substream_count)
611 list_for_each_entry(substream, &pstr->substreams, list) {
612 if ((unsigned int)substream->number == info->subdevice)
613 return snd_rawmidi_info(substream, info);
618 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
622 mutex_lock(®ister_mutex);
623 ret = __snd_rawmidi_info_select(card, info);
624 mutex_unlock(®ister_mutex);
627 EXPORT_SYMBOL(snd_rawmidi_info_select);
629 static int snd_rawmidi_info_select_user(struct snd_card *card,
630 struct snd_rawmidi_info __user *_info)
633 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 err = snd_rawmidi_info_select(card, &info);
644 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
649 static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime,
650 struct snd_rawmidi_params *params,
653 char *newbuf, *oldbuf;
655 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L)
657 if (params->avail_min < 1 || params->avail_min > params->buffer_size)
659 if (params->buffer_size != runtime->buffer_size) {
660 newbuf = kvzalloc(params->buffer_size, GFP_KERNEL);
663 spin_lock_irq(&runtime->lock);
664 oldbuf = runtime->buffer;
665 runtime->buffer = newbuf;
666 runtime->buffer_size = params->buffer_size;
667 __reset_runtime_ptrs(runtime, is_input);
668 spin_unlock_irq(&runtime->lock);
671 runtime->avail_min = params->avail_min;
675 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
676 struct snd_rawmidi_params *params)
678 if (substream->append && substream->use_count > 1)
680 snd_rawmidi_drain_output(substream);
681 substream->active_sensing = !params->no_active_sensing;
682 return resize_runtime_buffer(substream->runtime, params, false);
684 EXPORT_SYMBOL(snd_rawmidi_output_params);
686 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
687 struct snd_rawmidi_params *params)
689 snd_rawmidi_drain_input(substream);
690 return resize_runtime_buffer(substream->runtime, params, true);
692 EXPORT_SYMBOL(snd_rawmidi_input_params);
694 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
695 struct snd_rawmidi_status *status)
697 struct snd_rawmidi_runtime *runtime = substream->runtime;
699 memset(status, 0, sizeof(*status));
700 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
701 spin_lock_irq(&runtime->lock);
702 status->avail = runtime->avail;
703 spin_unlock_irq(&runtime->lock);
707 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
708 struct snd_rawmidi_status *status)
710 struct snd_rawmidi_runtime *runtime = substream->runtime;
712 memset(status, 0, sizeof(*status));
713 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
714 spin_lock_irq(&runtime->lock);
715 status->avail = runtime->avail;
716 status->xruns = runtime->xruns;
718 spin_unlock_irq(&runtime->lock);
722 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
724 struct snd_rawmidi_file *rfile;
725 void __user *argp = (void __user *)arg;
727 rfile = file->private_data;
728 if (((cmd >> 8) & 0xff) != 'W')
731 case SNDRV_RAWMIDI_IOCTL_PVERSION:
732 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
733 case SNDRV_RAWMIDI_IOCTL_INFO:
736 struct snd_rawmidi_info __user *info = argp;
738 if (get_user(stream, &info->stream))
741 case SNDRV_RAWMIDI_STREAM_INPUT:
742 return snd_rawmidi_info_user(rfile->input, info);
743 case SNDRV_RAWMIDI_STREAM_OUTPUT:
744 return snd_rawmidi_info_user(rfile->output, info);
749 case SNDRV_RAWMIDI_IOCTL_PARAMS:
751 struct snd_rawmidi_params params;
753 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
755 switch (params.stream) {
756 case SNDRV_RAWMIDI_STREAM_OUTPUT:
757 if (rfile->output == NULL)
759 return snd_rawmidi_output_params(rfile->output, ¶ms);
760 case SNDRV_RAWMIDI_STREAM_INPUT:
761 if (rfile->input == NULL)
763 return snd_rawmidi_input_params(rfile->input, ¶ms);
768 case SNDRV_RAWMIDI_IOCTL_STATUS:
771 struct snd_rawmidi_status status;
773 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
775 switch (status.stream) {
776 case SNDRV_RAWMIDI_STREAM_OUTPUT:
777 if (rfile->output == NULL)
779 err = snd_rawmidi_output_status(rfile->output, &status);
781 case SNDRV_RAWMIDI_STREAM_INPUT:
782 if (rfile->input == NULL)
784 err = snd_rawmidi_input_status(rfile->input, &status);
791 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
795 case SNDRV_RAWMIDI_IOCTL_DROP:
799 if (get_user(val, (int __user *) argp))
802 case SNDRV_RAWMIDI_STREAM_OUTPUT:
803 if (rfile->output == NULL)
805 return snd_rawmidi_drop_output(rfile->output);
810 case SNDRV_RAWMIDI_IOCTL_DRAIN:
814 if (get_user(val, (int __user *) argp))
817 case SNDRV_RAWMIDI_STREAM_OUTPUT:
818 if (rfile->output == NULL)
820 return snd_rawmidi_drain_output(rfile->output);
821 case SNDRV_RAWMIDI_STREAM_INPUT:
822 if (rfile->input == NULL)
824 return snd_rawmidi_drain_input(rfile->input);
830 rmidi_dbg(rfile->rmidi,
831 "rawmidi: unknown command = 0x%x\n", cmd);
836 static int snd_rawmidi_control_ioctl(struct snd_card *card,
837 struct snd_ctl_file *control,
841 void __user *argp = (void __user *)arg;
844 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
848 if (get_user(device, (int __user *)argp))
850 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
851 device = SNDRV_RAWMIDI_DEVICES - 1;
852 mutex_lock(®ister_mutex);
853 device = device < 0 ? 0 : device + 1;
854 while (device < SNDRV_RAWMIDI_DEVICES) {
855 if (snd_rawmidi_search(card, device))
859 if (device == SNDRV_RAWMIDI_DEVICES)
861 mutex_unlock(®ister_mutex);
862 if (put_user(device, (int __user *)argp))
866 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
870 if (get_user(val, (int __user *)argp))
872 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
875 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
876 return snd_rawmidi_info_select_user(card, argp);
882 * snd_rawmidi_receive - receive the input data from the device
883 * @substream: the rawmidi substream
884 * @buffer: the buffer pointer
885 * @count: the data size to read
887 * Reads the data from the internal buffer.
889 * Return: The size of read data, or a negative error code on failure.
891 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
892 const unsigned char *buffer, int count)
895 int result = 0, count1;
896 struct snd_rawmidi_runtime *runtime = substream->runtime;
898 if (!substream->opened)
900 if (runtime->buffer == NULL) {
901 rmidi_dbg(substream->rmidi,
902 "snd_rawmidi_receive: input is not active!!!\n");
905 spin_lock_irqsave(&runtime->lock, flags);
906 if (count == 1) { /* special case, faster code */
908 if (runtime->avail < runtime->buffer_size) {
909 runtime->buffer[runtime->hw_ptr++] = buffer[0];
910 runtime->hw_ptr %= runtime->buffer_size;
917 substream->bytes += count;
918 count1 = runtime->buffer_size - runtime->hw_ptr;
921 if (count1 > (int)(runtime->buffer_size - runtime->avail))
922 count1 = runtime->buffer_size - runtime->avail;
923 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
924 runtime->hw_ptr += count1;
925 runtime->hw_ptr %= runtime->buffer_size;
926 runtime->avail += count1;
932 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
933 count1 = runtime->buffer_size - runtime->avail;
934 runtime->xruns += count - count1;
937 memcpy(runtime->buffer, buffer, count1);
938 runtime->hw_ptr = count1;
939 runtime->avail += count1;
946 schedule_work(&runtime->event_work);
947 else if (snd_rawmidi_ready(substream))
948 wake_up(&runtime->sleep);
950 spin_unlock_irqrestore(&runtime->lock, flags);
953 EXPORT_SYMBOL(snd_rawmidi_receive);
955 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
956 unsigned char __user *userbuf,
957 unsigned char *kernelbuf, long count)
960 long result = 0, count1;
961 struct snd_rawmidi_runtime *runtime = substream->runtime;
962 unsigned long appl_ptr;
964 spin_lock_irqsave(&runtime->lock, flags);
965 while (count > 0 && runtime->avail) {
966 count1 = runtime->buffer_size - runtime->appl_ptr;
969 if (count1 > (int)runtime->avail)
970 count1 = runtime->avail;
972 /* update runtime->appl_ptr before unlocking for userbuf */
973 appl_ptr = runtime->appl_ptr;
974 runtime->appl_ptr += count1;
975 runtime->appl_ptr %= runtime->buffer_size;
976 runtime->avail -= count1;
979 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
981 spin_unlock_irqrestore(&runtime->lock, flags);
982 if (copy_to_user(userbuf + result,
983 runtime->buffer + appl_ptr, count1)) {
984 return result > 0 ? result : -EFAULT;
986 spin_lock_irqsave(&runtime->lock, flags);
991 spin_unlock_irqrestore(&runtime->lock, flags);
995 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
996 unsigned char *buf, long count)
998 snd_rawmidi_input_trigger(substream, 1);
999 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
1001 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1003 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1008 struct snd_rawmidi_file *rfile;
1009 struct snd_rawmidi_substream *substream;
1010 struct snd_rawmidi_runtime *runtime;
1012 rfile = file->private_data;
1013 substream = rfile->input;
1014 if (substream == NULL)
1016 runtime = substream->runtime;
1017 snd_rawmidi_input_trigger(substream, 1);
1020 spin_lock_irq(&runtime->lock);
1021 while (!snd_rawmidi_ready(substream)) {
1022 wait_queue_entry_t wait;
1024 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1025 spin_unlock_irq(&runtime->lock);
1026 return result > 0 ? result : -EAGAIN;
1028 init_waitqueue_entry(&wait, current);
1029 add_wait_queue(&runtime->sleep, &wait);
1030 set_current_state(TASK_INTERRUPTIBLE);
1031 spin_unlock_irq(&runtime->lock);
1033 remove_wait_queue(&runtime->sleep, &wait);
1034 if (rfile->rmidi->card->shutdown)
1036 if (signal_pending(current))
1037 return result > 0 ? result : -ERESTARTSYS;
1038 if (!runtime->avail)
1039 return result > 0 ? result : -EIO;
1040 spin_lock_irq(&runtime->lock);
1042 spin_unlock_irq(&runtime->lock);
1043 count1 = snd_rawmidi_kernel_read1(substream,
1044 (unsigned char __user *)buf,
1048 return result > 0 ? result : count1;
1057 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1058 * @substream: the rawmidi substream
1060 * Return: 1 if the internal output buffer is empty, 0 if not.
1062 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1064 struct snd_rawmidi_runtime *runtime = substream->runtime;
1066 unsigned long flags;
1068 if (runtime->buffer == NULL) {
1069 rmidi_dbg(substream->rmidi,
1070 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1073 spin_lock_irqsave(&runtime->lock, flags);
1074 result = runtime->avail >= runtime->buffer_size;
1075 spin_unlock_irqrestore(&runtime->lock, flags);
1078 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1081 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1082 * @substream: the rawmidi substream
1083 * @buffer: the buffer pointer
1084 * @count: data size to transfer
1086 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1088 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1089 unsigned char *buffer, int count)
1092 struct snd_rawmidi_runtime *runtime = substream->runtime;
1094 if (runtime->buffer == NULL) {
1095 rmidi_dbg(substream->rmidi,
1096 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1100 if (runtime->avail >= runtime->buffer_size) {
1101 /* warning: lowlevel layer MUST trigger down the hardware */
1104 if (count == 1) { /* special case, faster code */
1105 *buffer = runtime->buffer[runtime->hw_ptr];
1108 count1 = runtime->buffer_size - runtime->hw_ptr;
1111 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1112 count1 = runtime->buffer_size - runtime->avail;
1113 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1117 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1118 count = runtime->buffer_size - runtime->avail - count1;
1119 memcpy(buffer + count1, runtime->buffer, count);
1126 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1129 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1130 * @substream: the rawmidi substream
1131 * @buffer: the buffer pointer
1132 * @count: data size to transfer
1134 * Copies data from the internal output buffer to the given buffer.
1136 * Call this in the interrupt handler when the midi output is ready,
1137 * and call snd_rawmidi_transmit_ack() after the transmission is
1140 * Return: The size of copied data, or a negative error code on failure.
1142 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1143 unsigned char *buffer, int count)
1145 struct snd_rawmidi_runtime *runtime = substream->runtime;
1147 unsigned long flags;
1149 spin_lock_irqsave(&runtime->lock, flags);
1150 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1151 spin_unlock_irqrestore(&runtime->lock, flags);
1154 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1157 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1158 * @substream: the rawmidi substream
1159 * @count: the transferred count
1161 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1163 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1165 struct snd_rawmidi_runtime *runtime = substream->runtime;
1167 if (runtime->buffer == NULL) {
1168 rmidi_dbg(substream->rmidi,
1169 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1172 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1173 runtime->hw_ptr += count;
1174 runtime->hw_ptr %= runtime->buffer_size;
1175 runtime->avail += count;
1176 substream->bytes += count;
1178 if (runtime->drain || snd_rawmidi_ready(substream))
1179 wake_up(&runtime->sleep);
1183 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1186 * snd_rawmidi_transmit_ack - acknowledge the transmission
1187 * @substream: the rawmidi substream
1188 * @count: the transferred count
1190 * Advances the hardware pointer for the internal output buffer with
1191 * the given size and updates the condition.
1192 * Call after the transmission is finished.
1194 * Return: The advanced size if successful, or a negative error code on failure.
1196 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1198 struct snd_rawmidi_runtime *runtime = substream->runtime;
1200 unsigned long flags;
1202 spin_lock_irqsave(&runtime->lock, flags);
1203 result = __snd_rawmidi_transmit_ack(substream, count);
1204 spin_unlock_irqrestore(&runtime->lock, flags);
1207 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1210 * snd_rawmidi_transmit - copy from the buffer to the device
1211 * @substream: the rawmidi substream
1212 * @buffer: the buffer pointer
1213 * @count: the data size to transfer
1215 * Copies data from the buffer to the device and advances the pointer.
1217 * Return: The copied size if successful, or a negative error code on failure.
1219 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1220 unsigned char *buffer, int count)
1222 struct snd_rawmidi_runtime *runtime = substream->runtime;
1224 unsigned long flags;
1226 spin_lock_irqsave(&runtime->lock, flags);
1227 if (!substream->opened)
1230 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1234 result = __snd_rawmidi_transmit_ack(substream, count);
1236 spin_unlock_irqrestore(&runtime->lock, flags);
1239 EXPORT_SYMBOL(snd_rawmidi_transmit);
1242 * snd_rawmidi_proceed - Discard the all pending bytes and proceed
1243 * @substream: rawmidi substream
1245 * Return: the number of discarded bytes
1247 int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream)
1249 struct snd_rawmidi_runtime *runtime = substream->runtime;
1250 unsigned long flags;
1253 spin_lock_irqsave(&runtime->lock, flags);
1254 if (runtime->avail < runtime->buffer_size) {
1255 count = runtime->buffer_size - runtime->avail;
1256 __snd_rawmidi_transmit_ack(substream, count);
1258 spin_unlock_irqrestore(&runtime->lock, flags);
1261 EXPORT_SYMBOL(snd_rawmidi_proceed);
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 while (count > 0 && runtime->avail > 0) {
1287 count1 = runtime->buffer_size - runtime->appl_ptr;
1290 if (count1 > (long)runtime->avail)
1291 count1 = runtime->avail;
1293 /* update runtime->appl_ptr before unlocking for userbuf */
1294 appl_ptr = runtime->appl_ptr;
1295 runtime->appl_ptr += count1;
1296 runtime->appl_ptr %= runtime->buffer_size;
1297 runtime->avail -= count1;
1300 memcpy(runtime->buffer + appl_ptr,
1301 kernelbuf + result, count1);
1303 spin_unlock_irqrestore(&runtime->lock, flags);
1304 if (copy_from_user(runtime->buffer + appl_ptr,
1305 userbuf + result, count1)) {
1306 spin_lock_irqsave(&runtime->lock, flags);
1307 result = result > 0 ? result : -EFAULT;
1310 spin_lock_irqsave(&runtime->lock, flags);
1316 count1 = runtime->avail < runtime->buffer_size;
1317 spin_unlock_irqrestore(&runtime->lock, flags);
1319 snd_rawmidi_output_trigger(substream, 1);
1323 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1324 const unsigned char *buf, long count)
1326 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1328 EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1330 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1331 size_t count, loff_t *offset)
1333 long result, timeout;
1335 struct snd_rawmidi_file *rfile;
1336 struct snd_rawmidi_runtime *runtime;
1337 struct snd_rawmidi_substream *substream;
1339 rfile = file->private_data;
1340 substream = rfile->output;
1341 runtime = substream->runtime;
1342 /* we cannot put an atomic message to our buffer */
1343 if (substream->append && count > runtime->buffer_size)
1347 spin_lock_irq(&runtime->lock);
1348 while (!snd_rawmidi_ready_append(substream, count)) {
1349 wait_queue_entry_t wait;
1351 if (file->f_flags & O_NONBLOCK) {
1352 spin_unlock_irq(&runtime->lock);
1353 return result > 0 ? result : -EAGAIN;
1355 init_waitqueue_entry(&wait, current);
1356 add_wait_queue(&runtime->sleep, &wait);
1357 set_current_state(TASK_INTERRUPTIBLE);
1358 spin_unlock_irq(&runtime->lock);
1359 timeout = schedule_timeout(30 * HZ);
1360 remove_wait_queue(&runtime->sleep, &wait);
1361 if (rfile->rmidi->card->shutdown)
1363 if (signal_pending(current))
1364 return result > 0 ? result : -ERESTARTSYS;
1365 if (!runtime->avail && !timeout)
1366 return result > 0 ? result : -EIO;
1367 spin_lock_irq(&runtime->lock);
1369 spin_unlock_irq(&runtime->lock);
1370 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1372 return result > 0 ? result : count1;
1375 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1379 if (file->f_flags & O_DSYNC) {
1380 spin_lock_irq(&runtime->lock);
1381 while (runtime->avail != runtime->buffer_size) {
1382 wait_queue_entry_t wait;
1383 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 __poll_t 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 |= EPOLLIN | EPOLLRDNORM;
1423 if (rfile->output != NULL) {
1424 if (snd_rawmidi_ready(rfile->output))
1425 mask |= EPOLLOUT | EPOLLWRNORM;
1432 #ifdef CONFIG_COMPAT
1433 #include "rawmidi_compat.c"
1435 #define snd_rawmidi_ioctl_compat NULL
1441 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1442 struct snd_info_buffer *buffer)
1444 struct snd_rawmidi *rmidi;
1445 struct snd_rawmidi_substream *substream;
1446 struct snd_rawmidi_runtime *runtime;
1448 rmidi = entry->private_data;
1449 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1450 mutex_lock(&rmidi->open_mutex);
1451 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1452 list_for_each_entry(substream,
1453 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1457 " Tx bytes : %lu\n",
1459 (unsigned long) substream->bytes);
1460 if (substream->opened) {
1462 " Owner PID : %d\n",
1463 pid_vnr(substream->pid));
1464 runtime = substream->runtime;
1467 " Buffer size : %lu\n"
1469 runtime->oss ? "OSS compatible" : "native",
1470 (unsigned long) runtime->buffer_size,
1471 (unsigned long) runtime->avail);
1475 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1476 list_for_each_entry(substream,
1477 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1481 " Rx bytes : %lu\n",
1483 (unsigned long) substream->bytes);
1484 if (substream->opened) {
1486 " Owner PID : %d\n",
1487 pid_vnr(substream->pid));
1488 runtime = substream->runtime;
1490 " Buffer size : %lu\n"
1492 " Overruns : %lu\n",
1493 (unsigned long) runtime->buffer_size,
1494 (unsigned long) runtime->avail,
1495 (unsigned long) runtime->xruns);
1499 mutex_unlock(&rmidi->open_mutex);
1503 * Register functions
1506 static const struct file_operations snd_rawmidi_f_ops = {
1507 .owner = THIS_MODULE,
1508 .read = snd_rawmidi_read,
1509 .write = snd_rawmidi_write,
1510 .open = snd_rawmidi_open,
1511 .release = snd_rawmidi_release,
1512 .llseek = no_llseek,
1513 .poll = snd_rawmidi_poll,
1514 .unlocked_ioctl = snd_rawmidi_ioctl,
1515 .compat_ioctl = snd_rawmidi_ioctl_compat,
1518 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1519 struct snd_rawmidi_str *stream,
1523 struct snd_rawmidi_substream *substream;
1526 for (idx = 0; idx < count; idx++) {
1527 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1530 substream->stream = direction;
1531 substream->number = idx;
1532 substream->rmidi = rmidi;
1533 substream->pstr = stream;
1534 list_add_tail(&substream->list, &stream->substreams);
1535 stream->substream_count++;
1540 static void release_rawmidi_device(struct device *dev)
1542 kfree(container_of(dev, struct snd_rawmidi, dev));
1546 * snd_rawmidi_new - create a rawmidi instance
1547 * @card: the card instance
1548 * @id: the id string
1549 * @device: the device index
1550 * @output_count: the number of output streams
1551 * @input_count: the number of input streams
1552 * @rrawmidi: the pointer to store the new rawmidi instance
1554 * Creates a new rawmidi instance.
1555 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1557 * Return: Zero if successful, or a negative error code on failure.
1559 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1560 int output_count, int input_count,
1561 struct snd_rawmidi **rrawmidi)
1563 struct snd_rawmidi *rmidi;
1565 static struct snd_device_ops ops = {
1566 .dev_free = snd_rawmidi_dev_free,
1567 .dev_register = snd_rawmidi_dev_register,
1568 .dev_disconnect = snd_rawmidi_dev_disconnect,
1571 if (snd_BUG_ON(!card))
1575 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1579 rmidi->device = device;
1580 mutex_init(&rmidi->open_mutex);
1581 init_waitqueue_head(&rmidi->open_wait);
1582 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1583 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1586 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1588 snd_device_initialize(&rmidi->dev, card);
1589 rmidi->dev.release = release_rawmidi_device;
1590 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1592 err = snd_rawmidi_alloc_substreams(rmidi,
1593 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1594 SNDRV_RAWMIDI_STREAM_INPUT,
1598 err = snd_rawmidi_alloc_substreams(rmidi,
1599 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1600 SNDRV_RAWMIDI_STREAM_OUTPUT,
1604 err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops);
1613 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;
1653 return snd_rawmidi_free(rmidi);
1656 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1657 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1659 struct snd_rawmidi *rmidi = device->private_data;
1661 rmidi->seq_dev = NULL;
1665 static int snd_rawmidi_dev_register(struct snd_device *device)
1668 struct snd_info_entry *entry;
1670 struct snd_rawmidi *rmidi = device->device_data;
1672 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1675 mutex_lock(®ister_mutex);
1676 if (snd_rawmidi_search(rmidi->card, rmidi->device))
1679 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1680 mutex_unlock(®ister_mutex);
1684 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1685 rmidi->card, rmidi->device,
1686 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
1688 rmidi_err(rmidi, "unable to register\n");
1691 if (rmidi->ops && rmidi->ops->dev_register) {
1692 err = rmidi->ops->dev_register(rmidi);
1694 goto error_unregister;
1696 #ifdef CONFIG_SND_OSSEMUL
1698 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1699 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1700 rmidi->card, 0, &snd_rawmidi_f_ops,
1703 "unable to register OSS rawmidi device %i:%i\n",
1704 rmidi->card->number, 0);
1707 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1708 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1712 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1713 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1714 rmidi->card, 1, &snd_rawmidi_f_ops,
1717 "unable to register OSS rawmidi device %i:%i\n",
1718 rmidi->card->number, 1);
1723 #endif /* CONFIG_SND_OSSEMUL */
1724 sprintf(name, "midi%d", rmidi->device);
1725 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1727 entry->private_data = rmidi;
1728 entry->c.text.read = snd_rawmidi_proc_info_read;
1729 if (snd_info_register(entry) < 0) {
1730 snd_info_free_entry(entry);
1734 rmidi->proc_entry = entry;
1735 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1736 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1737 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1738 rmidi->seq_dev->private_data = rmidi;
1739 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1740 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1741 snd_device_register(rmidi->card, rmidi->seq_dev);
1748 snd_unregister_device(&rmidi->dev);
1750 mutex_lock(®ister_mutex);
1751 list_del(&rmidi->list);
1752 mutex_unlock(®ister_mutex);
1756 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1758 struct snd_rawmidi *rmidi = device->device_data;
1761 mutex_lock(®ister_mutex);
1762 mutex_lock(&rmidi->open_mutex);
1763 wake_up(&rmidi->open_wait);
1764 list_del_init(&rmidi->list);
1765 for (dir = 0; dir < 2; dir++) {
1766 struct snd_rawmidi_substream *s;
1768 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1770 wake_up(&s->runtime->sleep);
1774 #ifdef CONFIG_SND_OSSEMUL
1775 if (rmidi->ossreg) {
1776 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1777 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1778 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1779 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1782 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1783 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1786 #endif /* CONFIG_SND_OSSEMUL */
1787 snd_unregister_device(&rmidi->dev);
1788 mutex_unlock(&rmidi->open_mutex);
1789 mutex_unlock(®ister_mutex);
1794 * snd_rawmidi_set_ops - set the rawmidi operators
1795 * @rmidi: the rawmidi instance
1796 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1797 * @ops: the operator table
1799 * Sets the rawmidi operators for the given stream direction.
1801 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1802 const struct snd_rawmidi_ops *ops)
1804 struct snd_rawmidi_substream *substream;
1806 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1807 substream->ops = ops;
1809 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1815 static int __init alsa_rawmidi_init(void)
1818 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1819 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1820 #ifdef CONFIG_SND_OSSEMUL
1822 /* check device map table */
1823 for (i = 0; i < SNDRV_CARDS; i++) {
1824 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1825 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1829 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1830 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1836 #endif /* CONFIG_SND_OSSEMUL */
1840 static void __exit alsa_rawmidi_exit(void)
1842 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1843 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1846 module_init(alsa_rawmidi_init)
1847 module_exit(alsa_rawmidi_exit)