1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Abstract layer for MIDI v1.0 stream
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <sound/core.h>
8 #include <linux/major.h>
9 #include <linux/init.h>
10 #include <linux/sched/signal.h>
11 #include <linux/slab.h>
12 #include <linux/time.h>
13 #include <linux/wait.h>
14 #include <linux/mutex.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
18 #include <linux/nospec.h>
19 #include <sound/rawmidi.h>
20 #include <sound/info.h>
21 #include <sound/control.h>
22 #include <sound/minors.h>
23 #include <sound/initval.h>
25 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
26 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
27 MODULE_LICENSE("GPL");
29 #ifdef CONFIG_SND_OSSEMUL
30 static int midi_map[SNDRV_CARDS];
31 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
32 module_param_array(midi_map, int, NULL, 0444);
33 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
34 module_param_array(amidi_map, int, NULL, 0444);
35 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
36 #endif /* CONFIG_SND_OSSEMUL */
38 static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
39 static int snd_rawmidi_dev_free(struct snd_device *device);
40 static int snd_rawmidi_dev_register(struct snd_device *device);
41 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
43 static LIST_HEAD(snd_rawmidi_devices);
44 static DEFINE_MUTEX(register_mutex);
46 #define rmidi_err(rmidi, fmt, args...) \
47 dev_err(&(rmidi)->dev, fmt, ##args)
48 #define rmidi_warn(rmidi, fmt, args...) \
49 dev_warn(&(rmidi)->dev, fmt, ##args)
50 #define rmidi_dbg(rmidi, fmt, args...) \
51 dev_dbg(&(rmidi)->dev, fmt, ##args)
53 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
55 struct snd_rawmidi *rawmidi;
57 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
58 if (rawmidi->card == card && rawmidi->device == device)
63 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
65 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
67 return SNDRV_RAWMIDI_LFLG_OUTPUT;
69 return SNDRV_RAWMIDI_LFLG_INPUT;
71 return SNDRV_RAWMIDI_LFLG_OPEN;
75 static inline bool __snd_rawmidi_ready(struct snd_rawmidi_runtime *runtime)
77 return runtime->avail >= runtime->avail_min;
80 static bool snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
82 struct snd_rawmidi_runtime *runtime = substream->runtime;
86 spin_lock_irqsave(&runtime->lock, flags);
87 ready = __snd_rawmidi_ready(runtime);
88 spin_unlock_irqrestore(&runtime->lock, flags);
92 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
95 struct snd_rawmidi_runtime *runtime = substream->runtime;
97 return runtime->avail >= runtime->avail_min &&
98 (!substream->append || runtime->avail >= count);
101 static void snd_rawmidi_input_event_work(struct work_struct *work)
103 struct snd_rawmidi_runtime *runtime =
104 container_of(work, struct snd_rawmidi_runtime, event_work);
107 runtime->event(runtime->substream);
110 /* buffer refcount management: call with runtime->lock held */
111 static inline void snd_rawmidi_buffer_ref(struct snd_rawmidi_runtime *runtime)
113 runtime->buffer_ref++;
116 static inline void snd_rawmidi_buffer_unref(struct snd_rawmidi_runtime *runtime)
118 runtime->buffer_ref--;
121 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
123 struct snd_rawmidi_runtime *runtime;
125 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
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 runtime->buffer = kvzalloc(runtime->buffer_size, GFP_KERNEL);
140 if (!runtime->buffer) {
144 runtime->appl_ptr = runtime->hw_ptr = 0;
145 substream->runtime = runtime;
149 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
151 struct snd_rawmidi_runtime *runtime = substream->runtime;
153 kvfree(runtime->buffer);
155 substream->runtime = NULL;
159 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
161 if (!substream->opened)
163 substream->ops->trigger(substream, up);
166 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
168 if (!substream->opened)
170 substream->ops->trigger(substream, up);
172 cancel_work_sync(&substream->runtime->event_work);
175 static void __reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
179 runtime->appl_ptr = runtime->hw_ptr = 0;
180 runtime->avail = is_input ? 0 : runtime->buffer_size;
183 static void reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
188 spin_lock_irqsave(&runtime->lock, flags);
189 __reset_runtime_ptrs(runtime, is_input);
190 spin_unlock_irqrestore(&runtime->lock, flags);
193 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
195 snd_rawmidi_output_trigger(substream, 0);
196 reset_runtime_ptrs(substream->runtime, false);
199 EXPORT_SYMBOL(snd_rawmidi_drop_output);
201 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
205 struct snd_rawmidi_runtime *runtime = substream->runtime;
209 timeout = wait_event_interruptible_timeout(runtime->sleep,
210 (runtime->avail >= runtime->buffer_size),
212 if (signal_pending(current))
214 if (runtime->avail < runtime->buffer_size && !timeout) {
215 rmidi_warn(substream->rmidi,
216 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
217 (long)runtime->avail, (long)runtime->buffer_size);
221 if (err != -ERESTARTSYS) {
222 /* we need wait a while to make sure that Tx FIFOs are empty */
223 if (substream->ops->drain)
224 substream->ops->drain(substream);
227 snd_rawmidi_drop_output(substream);
231 EXPORT_SYMBOL(snd_rawmidi_drain_output);
233 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
235 snd_rawmidi_input_trigger(substream, 0);
236 reset_runtime_ptrs(substream->runtime, true);
239 EXPORT_SYMBOL(snd_rawmidi_drain_input);
241 /* look for an available substream for the given stream direction;
242 * if a specific subdevice is given, try to assign it
244 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
245 int stream, int mode,
246 struct snd_rawmidi_substream **sub_ret)
248 struct snd_rawmidi_substream *substream;
249 struct snd_rawmidi_str *s = &rmidi->streams[stream];
250 static unsigned int info_flags[2] = {
251 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
252 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
255 if (!(rmidi->info_flags & info_flags[stream]))
257 if (subdevice >= 0 && subdevice >= s->substream_count)
260 list_for_each_entry(substream, &s->substreams, list) {
261 if (substream->opened) {
262 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
263 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
267 if (subdevice < 0 || subdevice == substream->number) {
268 *sub_ret = substream;
275 /* open and do ref-counting for the given substream */
276 static int open_substream(struct snd_rawmidi *rmidi,
277 struct snd_rawmidi_substream *substream,
282 if (substream->use_count == 0) {
283 err = snd_rawmidi_runtime_create(substream);
286 err = substream->ops->open(substream);
288 snd_rawmidi_runtime_free(substream);
291 substream->opened = 1;
292 substream->active_sensing = 0;
293 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
294 substream->append = 1;
295 substream->pid = get_pid(task_pid(current));
296 rmidi->streams[substream->stream].substream_opened++;
298 substream->use_count++;
302 static void close_substream(struct snd_rawmidi *rmidi,
303 struct snd_rawmidi_substream *substream,
306 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
307 struct snd_rawmidi_file *rfile)
309 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
312 rfile->input = rfile->output = NULL;
313 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
314 err = assign_substream(rmidi, subdevice,
315 SNDRV_RAWMIDI_STREAM_INPUT,
320 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
321 err = assign_substream(rmidi, subdevice,
322 SNDRV_RAWMIDI_STREAM_OUTPUT,
329 err = open_substream(rmidi, sinput, mode);
334 err = open_substream(rmidi, soutput, mode);
337 close_substream(rmidi, sinput, 0);
342 rfile->rmidi = rmidi;
343 rfile->input = sinput;
344 rfile->output = soutput;
348 /* called from sound/core/seq/seq_midi.c */
349 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
350 int mode, struct snd_rawmidi_file *rfile)
352 struct snd_rawmidi *rmidi;
355 if (snd_BUG_ON(!rfile))
358 mutex_lock(®ister_mutex);
359 rmidi = snd_rawmidi_search(card, device);
362 else if (!try_module_get(rmidi->card->module))
364 mutex_unlock(®ister_mutex);
368 mutex_lock(&rmidi->open_mutex);
369 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
370 mutex_unlock(&rmidi->open_mutex);
372 module_put(rmidi->card->module);
375 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
377 static int snd_rawmidi_open(struct inode *inode, struct file *file)
379 int maj = imajor(inode);
380 struct snd_card *card;
382 unsigned short fflags;
384 struct snd_rawmidi *rmidi;
385 struct snd_rawmidi_file *rawmidi_file = NULL;
386 wait_queue_entry_t wait;
388 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
389 return -EINVAL; /* invalid combination */
391 err = stream_open(inode, file);
395 if (maj == snd_major) {
396 rmidi = snd_lookup_minor_data(iminor(inode),
397 SNDRV_DEVICE_TYPE_RAWMIDI);
398 #ifdef CONFIG_SND_OSSEMUL
399 } else if (maj == SOUND_MAJOR) {
400 rmidi = snd_lookup_oss_minor_data(iminor(inode),
401 SNDRV_OSS_DEVICE_TYPE_MIDI);
409 if (!try_module_get(rmidi->card->module)) {
410 snd_card_unref(rmidi->card);
414 mutex_lock(&rmidi->open_mutex);
416 err = snd_card_file_add(card, file);
419 fflags = snd_rawmidi_file_flags(file);
420 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
421 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
422 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
423 if (rawmidi_file == NULL) {
427 init_waitqueue_entry(&wait, current);
428 add_wait_queue(&rmidi->open_wait, &wait);
430 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
431 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
434 if (err == -EAGAIN) {
435 if (file->f_flags & O_NONBLOCK) {
441 set_current_state(TASK_INTERRUPTIBLE);
442 mutex_unlock(&rmidi->open_mutex);
444 mutex_lock(&rmidi->open_mutex);
445 if (rmidi->card->shutdown) {
449 if (signal_pending(current)) {
454 remove_wait_queue(&rmidi->open_wait, &wait);
459 #ifdef CONFIG_SND_OSSEMUL
460 if (rawmidi_file->input && rawmidi_file->input->runtime)
461 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
462 if (rawmidi_file->output && rawmidi_file->output->runtime)
463 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
465 file->private_data = rawmidi_file;
466 mutex_unlock(&rmidi->open_mutex);
467 snd_card_unref(rmidi->card);
471 snd_card_file_remove(card, file);
473 mutex_unlock(&rmidi->open_mutex);
474 module_put(rmidi->card->module);
475 snd_card_unref(rmidi->card);
479 static void close_substream(struct snd_rawmidi *rmidi,
480 struct snd_rawmidi_substream *substream,
483 if (--substream->use_count)
487 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
488 snd_rawmidi_input_trigger(substream, 0);
490 if (substream->active_sensing) {
491 unsigned char buf = 0xfe;
492 /* sending single active sensing message
493 * to shut the device up
495 snd_rawmidi_kernel_write(substream, &buf, 1);
497 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
498 snd_rawmidi_output_trigger(substream, 0);
501 substream->ops->close(substream);
502 if (substream->runtime->private_free)
503 substream->runtime->private_free(substream);
504 snd_rawmidi_runtime_free(substream);
505 substream->opened = 0;
506 substream->append = 0;
507 put_pid(substream->pid);
508 substream->pid = NULL;
509 rmidi->streams[substream->stream].substream_opened--;
512 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
514 struct snd_rawmidi *rmidi;
516 rmidi = rfile->rmidi;
517 mutex_lock(&rmidi->open_mutex);
519 close_substream(rmidi, rfile->input, 1);
523 close_substream(rmidi, rfile->output, 1);
524 rfile->output = NULL;
527 mutex_unlock(&rmidi->open_mutex);
528 wake_up(&rmidi->open_wait);
531 /* called from sound/core/seq/seq_midi.c */
532 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
534 struct snd_rawmidi *rmidi;
536 if (snd_BUG_ON(!rfile))
539 rmidi = rfile->rmidi;
540 rawmidi_release_priv(rfile);
541 module_put(rmidi->card->module);
544 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
546 static int snd_rawmidi_release(struct inode *inode, struct file *file)
548 struct snd_rawmidi_file *rfile;
549 struct snd_rawmidi *rmidi;
550 struct module *module;
552 rfile = file->private_data;
553 rmidi = rfile->rmidi;
554 rawmidi_release_priv(rfile);
556 module = rmidi->card->module;
557 snd_card_file_remove(rmidi->card, file);
562 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
563 struct snd_rawmidi_info *info)
565 struct snd_rawmidi *rmidi;
567 if (substream == NULL)
569 rmidi = substream->rmidi;
570 memset(info, 0, sizeof(*info));
571 info->card = rmidi->card->number;
572 info->device = rmidi->device;
573 info->subdevice = substream->number;
574 info->stream = substream->stream;
575 info->flags = rmidi->info_flags;
576 strcpy(info->id, rmidi->id);
577 strcpy(info->name, rmidi->name);
578 strcpy(info->subname, substream->name);
579 info->subdevices_count = substream->pstr->substream_count;
580 info->subdevices_avail = (substream->pstr->substream_count -
581 substream->pstr->substream_opened);
585 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
586 struct snd_rawmidi_info __user *_info)
588 struct snd_rawmidi_info info;
591 err = snd_rawmidi_info(substream, &info);
594 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
599 static int __snd_rawmidi_info_select(struct snd_card *card,
600 struct snd_rawmidi_info *info)
602 struct snd_rawmidi *rmidi;
603 struct snd_rawmidi_str *pstr;
604 struct snd_rawmidi_substream *substream;
606 rmidi = snd_rawmidi_search(card, info->device);
609 if (info->stream < 0 || info->stream > 1)
611 info->stream = array_index_nospec(info->stream, 2);
612 pstr = &rmidi->streams[info->stream];
613 if (pstr->substream_count == 0)
615 if (info->subdevice >= pstr->substream_count)
617 list_for_each_entry(substream, &pstr->substreams, list) {
618 if ((unsigned int)substream->number == info->subdevice)
619 return snd_rawmidi_info(substream, info);
624 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
628 mutex_lock(®ister_mutex);
629 ret = __snd_rawmidi_info_select(card, info);
630 mutex_unlock(®ister_mutex);
633 EXPORT_SYMBOL(snd_rawmidi_info_select);
635 static int snd_rawmidi_info_select_user(struct snd_card *card,
636 struct snd_rawmidi_info __user *_info)
639 struct snd_rawmidi_info info;
641 if (get_user(info.device, &_info->device))
643 if (get_user(info.stream, &_info->stream))
645 if (get_user(info.subdevice, &_info->subdevice))
647 err = snd_rawmidi_info_select(card, &info);
650 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
655 static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime,
656 struct snd_rawmidi_params *params,
659 char *newbuf, *oldbuf;
661 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L)
663 if (params->avail_min < 1 || params->avail_min > params->buffer_size)
665 if (params->buffer_size != runtime->buffer_size) {
666 newbuf = kvzalloc(params->buffer_size, GFP_KERNEL);
669 spin_lock_irq(&runtime->lock);
670 if (runtime->buffer_ref) {
671 spin_unlock_irq(&runtime->lock);
675 oldbuf = runtime->buffer;
676 runtime->buffer = newbuf;
677 runtime->buffer_size = params->buffer_size;
678 __reset_runtime_ptrs(runtime, is_input);
679 spin_unlock_irq(&runtime->lock);
682 runtime->avail_min = params->avail_min;
686 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
687 struct snd_rawmidi_params *params)
689 if (substream->append && substream->use_count > 1)
691 snd_rawmidi_drain_output(substream);
692 substream->active_sensing = !params->no_active_sensing;
693 return resize_runtime_buffer(substream->runtime, params, false);
695 EXPORT_SYMBOL(snd_rawmidi_output_params);
697 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
698 struct snd_rawmidi_params *params)
700 snd_rawmidi_drain_input(substream);
701 return resize_runtime_buffer(substream->runtime, params, true);
703 EXPORT_SYMBOL(snd_rawmidi_input_params);
705 static int snd_rawmidi_output_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_OUTPUT;
712 spin_lock_irq(&runtime->lock);
713 status->avail = runtime->avail;
714 spin_unlock_irq(&runtime->lock);
718 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
719 struct snd_rawmidi_status *status)
721 struct snd_rawmidi_runtime *runtime = substream->runtime;
723 memset(status, 0, sizeof(*status));
724 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
725 spin_lock_irq(&runtime->lock);
726 status->avail = runtime->avail;
727 status->xruns = runtime->xruns;
729 spin_unlock_irq(&runtime->lock);
733 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
735 struct snd_rawmidi_file *rfile;
736 void __user *argp = (void __user *)arg;
738 rfile = file->private_data;
739 if (((cmd >> 8) & 0xff) != 'W')
742 case SNDRV_RAWMIDI_IOCTL_PVERSION:
743 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
744 case SNDRV_RAWMIDI_IOCTL_INFO:
747 struct snd_rawmidi_info __user *info = argp;
749 if (get_user(stream, &info->stream))
752 case SNDRV_RAWMIDI_STREAM_INPUT:
753 return snd_rawmidi_info_user(rfile->input, info);
754 case SNDRV_RAWMIDI_STREAM_OUTPUT:
755 return snd_rawmidi_info_user(rfile->output, info);
760 case SNDRV_RAWMIDI_IOCTL_PARAMS:
762 struct snd_rawmidi_params params;
764 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
766 switch (params.stream) {
767 case SNDRV_RAWMIDI_STREAM_OUTPUT:
768 if (rfile->output == NULL)
770 return snd_rawmidi_output_params(rfile->output, ¶ms);
771 case SNDRV_RAWMIDI_STREAM_INPUT:
772 if (rfile->input == NULL)
774 return snd_rawmidi_input_params(rfile->input, ¶ms);
779 case SNDRV_RAWMIDI_IOCTL_STATUS:
782 struct snd_rawmidi_status status;
784 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
786 switch (status.stream) {
787 case SNDRV_RAWMIDI_STREAM_OUTPUT:
788 if (rfile->output == NULL)
790 err = snd_rawmidi_output_status(rfile->output, &status);
792 case SNDRV_RAWMIDI_STREAM_INPUT:
793 if (rfile->input == NULL)
795 err = snd_rawmidi_input_status(rfile->input, &status);
802 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
806 case SNDRV_RAWMIDI_IOCTL_DROP:
810 if (get_user(val, (int __user *) argp))
813 case SNDRV_RAWMIDI_STREAM_OUTPUT:
814 if (rfile->output == NULL)
816 return snd_rawmidi_drop_output(rfile->output);
821 case SNDRV_RAWMIDI_IOCTL_DRAIN:
825 if (get_user(val, (int __user *) argp))
828 case SNDRV_RAWMIDI_STREAM_OUTPUT:
829 if (rfile->output == NULL)
831 return snd_rawmidi_drain_output(rfile->output);
832 case SNDRV_RAWMIDI_STREAM_INPUT:
833 if (rfile->input == NULL)
835 return snd_rawmidi_drain_input(rfile->input);
841 rmidi_dbg(rfile->rmidi,
842 "rawmidi: unknown command = 0x%x\n", cmd);
847 static int snd_rawmidi_control_ioctl(struct snd_card *card,
848 struct snd_ctl_file *control,
852 void __user *argp = (void __user *)arg;
855 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
859 if (get_user(device, (int __user *)argp))
861 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
862 device = SNDRV_RAWMIDI_DEVICES - 1;
863 mutex_lock(®ister_mutex);
864 device = device < 0 ? 0 : device + 1;
865 while (device < SNDRV_RAWMIDI_DEVICES) {
866 if (snd_rawmidi_search(card, device))
870 if (device == SNDRV_RAWMIDI_DEVICES)
872 mutex_unlock(®ister_mutex);
873 if (put_user(device, (int __user *)argp))
877 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
881 if (get_user(val, (int __user *)argp))
883 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
886 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
887 return snd_rawmidi_info_select_user(card, argp);
893 * snd_rawmidi_receive - receive the input data from the device
894 * @substream: the rawmidi substream
895 * @buffer: the buffer pointer
896 * @count: the data size to read
898 * Reads the data from the internal buffer.
900 * Return: The size of read data, or a negative error code on failure.
902 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
903 const unsigned char *buffer, int count)
906 int result = 0, count1;
907 struct snd_rawmidi_runtime *runtime = substream->runtime;
909 if (!substream->opened)
911 if (runtime->buffer == NULL) {
912 rmidi_dbg(substream->rmidi,
913 "snd_rawmidi_receive: input is not active!!!\n");
916 spin_lock_irqsave(&runtime->lock, flags);
917 if (count == 1) { /* special case, faster code */
919 if (runtime->avail < runtime->buffer_size) {
920 runtime->buffer[runtime->hw_ptr++] = buffer[0];
921 runtime->hw_ptr %= runtime->buffer_size;
928 substream->bytes += count;
929 count1 = runtime->buffer_size - runtime->hw_ptr;
932 if (count1 > (int)(runtime->buffer_size - runtime->avail))
933 count1 = runtime->buffer_size - runtime->avail;
934 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
935 runtime->hw_ptr += count1;
936 runtime->hw_ptr %= runtime->buffer_size;
937 runtime->avail += count1;
943 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
944 count1 = runtime->buffer_size - runtime->avail;
945 runtime->xruns += count - count1;
948 memcpy(runtime->buffer, buffer, count1);
949 runtime->hw_ptr = count1;
950 runtime->avail += count1;
957 schedule_work(&runtime->event_work);
958 else if (__snd_rawmidi_ready(runtime))
959 wake_up(&runtime->sleep);
961 spin_unlock_irqrestore(&runtime->lock, flags);
964 EXPORT_SYMBOL(snd_rawmidi_receive);
966 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
967 unsigned char __user *userbuf,
968 unsigned char *kernelbuf, long count)
971 long result = 0, count1;
972 struct snd_rawmidi_runtime *runtime = substream->runtime;
973 unsigned long appl_ptr;
976 spin_lock_irqsave(&runtime->lock, flags);
977 snd_rawmidi_buffer_ref(runtime);
978 while (count > 0 && runtime->avail) {
979 count1 = runtime->buffer_size - runtime->appl_ptr;
982 if (count1 > (int)runtime->avail)
983 count1 = runtime->avail;
985 /* update runtime->appl_ptr before unlocking for userbuf */
986 appl_ptr = runtime->appl_ptr;
987 runtime->appl_ptr += count1;
988 runtime->appl_ptr %= runtime->buffer_size;
989 runtime->avail -= count1;
992 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
994 spin_unlock_irqrestore(&runtime->lock, flags);
995 if (copy_to_user(userbuf + result,
996 runtime->buffer + appl_ptr, count1))
998 spin_lock_irqsave(&runtime->lock, flags);
1006 snd_rawmidi_buffer_unref(runtime);
1007 spin_unlock_irqrestore(&runtime->lock, flags);
1008 return result > 0 ? result : err;
1011 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
1012 unsigned char *buf, long count)
1014 snd_rawmidi_input_trigger(substream, 1);
1015 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
1017 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1019 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1024 struct snd_rawmidi_file *rfile;
1025 struct snd_rawmidi_substream *substream;
1026 struct snd_rawmidi_runtime *runtime;
1028 rfile = file->private_data;
1029 substream = rfile->input;
1030 if (substream == NULL)
1032 runtime = substream->runtime;
1033 snd_rawmidi_input_trigger(substream, 1);
1036 spin_lock_irq(&runtime->lock);
1037 while (!__snd_rawmidi_ready(runtime)) {
1038 wait_queue_entry_t wait;
1040 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1041 spin_unlock_irq(&runtime->lock);
1042 return result > 0 ? result : -EAGAIN;
1044 init_waitqueue_entry(&wait, current);
1045 add_wait_queue(&runtime->sleep, &wait);
1046 set_current_state(TASK_INTERRUPTIBLE);
1047 spin_unlock_irq(&runtime->lock);
1049 remove_wait_queue(&runtime->sleep, &wait);
1050 if (rfile->rmidi->card->shutdown)
1052 if (signal_pending(current))
1053 return result > 0 ? result : -ERESTARTSYS;
1054 spin_lock_irq(&runtime->lock);
1055 if (!runtime->avail) {
1056 spin_unlock_irq(&runtime->lock);
1057 return result > 0 ? result : -EIO;
1060 spin_unlock_irq(&runtime->lock);
1061 count1 = snd_rawmidi_kernel_read1(substream,
1062 (unsigned char __user *)buf,
1066 return result > 0 ? result : count1;
1075 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1076 * @substream: the rawmidi substream
1078 * Return: 1 if the internal output buffer is empty, 0 if not.
1080 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1082 struct snd_rawmidi_runtime *runtime = substream->runtime;
1084 unsigned long flags;
1086 if (runtime->buffer == NULL) {
1087 rmidi_dbg(substream->rmidi,
1088 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1091 spin_lock_irqsave(&runtime->lock, flags);
1092 result = runtime->avail >= runtime->buffer_size;
1093 spin_unlock_irqrestore(&runtime->lock, flags);
1096 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1099 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1100 * @substream: the rawmidi substream
1101 * @buffer: the buffer pointer
1102 * @count: data size to transfer
1104 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1106 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1107 unsigned char *buffer, int count)
1110 struct snd_rawmidi_runtime *runtime = substream->runtime;
1112 if (runtime->buffer == NULL) {
1113 rmidi_dbg(substream->rmidi,
1114 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1118 if (runtime->avail >= runtime->buffer_size) {
1119 /* warning: lowlevel layer MUST trigger down the hardware */
1122 if (count == 1) { /* special case, faster code */
1123 *buffer = runtime->buffer[runtime->hw_ptr];
1126 count1 = runtime->buffer_size - runtime->hw_ptr;
1129 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1130 count1 = runtime->buffer_size - runtime->avail;
1131 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1135 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1136 count = runtime->buffer_size - runtime->avail - count1;
1137 memcpy(buffer + count1, runtime->buffer, count);
1144 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1147 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1148 * @substream: the rawmidi substream
1149 * @buffer: the buffer pointer
1150 * @count: data size to transfer
1152 * Copies data from the internal output buffer to the given buffer.
1154 * Call this in the interrupt handler when the midi output is ready,
1155 * and call snd_rawmidi_transmit_ack() after the transmission is
1158 * Return: The size of copied data, or a negative error code on failure.
1160 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1161 unsigned char *buffer, int count)
1163 struct snd_rawmidi_runtime *runtime = substream->runtime;
1165 unsigned long flags;
1167 spin_lock_irqsave(&runtime->lock, flags);
1168 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1169 spin_unlock_irqrestore(&runtime->lock, flags);
1172 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1175 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1176 * @substream: the rawmidi substream
1177 * @count: the transferred count
1179 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1181 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1183 struct snd_rawmidi_runtime *runtime = substream->runtime;
1185 if (runtime->buffer == NULL) {
1186 rmidi_dbg(substream->rmidi,
1187 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1190 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1191 runtime->hw_ptr += count;
1192 runtime->hw_ptr %= runtime->buffer_size;
1193 runtime->avail += count;
1194 substream->bytes += count;
1196 if (runtime->drain || __snd_rawmidi_ready(runtime))
1197 wake_up(&runtime->sleep);
1201 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1204 * snd_rawmidi_transmit_ack - acknowledge the transmission
1205 * @substream: the rawmidi substream
1206 * @count: the transferred count
1208 * Advances the hardware pointer for the internal output buffer with
1209 * the given size and updates the condition.
1210 * Call after the transmission is finished.
1212 * Return: The advanced size if successful, or a negative error code on failure.
1214 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1216 struct snd_rawmidi_runtime *runtime = substream->runtime;
1218 unsigned long flags;
1220 spin_lock_irqsave(&runtime->lock, flags);
1221 result = __snd_rawmidi_transmit_ack(substream, count);
1222 spin_unlock_irqrestore(&runtime->lock, flags);
1225 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1228 * snd_rawmidi_transmit - copy from the buffer to the device
1229 * @substream: the rawmidi substream
1230 * @buffer: the buffer pointer
1231 * @count: the data size to transfer
1233 * Copies data from the buffer to the device and advances the pointer.
1235 * Return: The copied size if successful, or a negative error code on failure.
1237 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1238 unsigned char *buffer, int count)
1240 struct snd_rawmidi_runtime *runtime = substream->runtime;
1242 unsigned long flags;
1244 spin_lock_irqsave(&runtime->lock, flags);
1245 if (!substream->opened)
1248 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1252 result = __snd_rawmidi_transmit_ack(substream, count);
1254 spin_unlock_irqrestore(&runtime->lock, flags);
1257 EXPORT_SYMBOL(snd_rawmidi_transmit);
1260 * snd_rawmidi_proceed - Discard the all pending bytes and proceed
1261 * @substream: rawmidi substream
1263 * Return: the number of discarded bytes
1265 int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream)
1267 struct snd_rawmidi_runtime *runtime = substream->runtime;
1268 unsigned long flags;
1271 spin_lock_irqsave(&runtime->lock, flags);
1272 if (runtime->avail < runtime->buffer_size) {
1273 count = runtime->buffer_size - runtime->avail;
1274 __snd_rawmidi_transmit_ack(substream, count);
1276 spin_unlock_irqrestore(&runtime->lock, flags);
1279 EXPORT_SYMBOL(snd_rawmidi_proceed);
1281 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1282 const unsigned char __user *userbuf,
1283 const unsigned char *kernelbuf,
1286 unsigned long flags;
1287 long count1, result;
1288 struct snd_rawmidi_runtime *runtime = substream->runtime;
1289 unsigned long appl_ptr;
1291 if (!kernelbuf && !userbuf)
1293 if (snd_BUG_ON(!runtime->buffer))
1297 spin_lock_irqsave(&runtime->lock, flags);
1298 if (substream->append) {
1299 if ((long)runtime->avail < count) {
1300 spin_unlock_irqrestore(&runtime->lock, flags);
1304 snd_rawmidi_buffer_ref(runtime);
1305 while (count > 0 && runtime->avail > 0) {
1306 count1 = runtime->buffer_size - runtime->appl_ptr;
1309 if (count1 > (long)runtime->avail)
1310 count1 = runtime->avail;
1312 /* update runtime->appl_ptr before unlocking for userbuf */
1313 appl_ptr = runtime->appl_ptr;
1314 runtime->appl_ptr += count1;
1315 runtime->appl_ptr %= runtime->buffer_size;
1316 runtime->avail -= count1;
1319 memcpy(runtime->buffer + appl_ptr,
1320 kernelbuf + result, count1);
1322 spin_unlock_irqrestore(&runtime->lock, flags);
1323 if (copy_from_user(runtime->buffer + appl_ptr,
1324 userbuf + result, count1)) {
1325 spin_lock_irqsave(&runtime->lock, flags);
1326 result = result > 0 ? result : -EFAULT;
1329 spin_lock_irqsave(&runtime->lock, flags);
1335 count1 = runtime->avail < runtime->buffer_size;
1336 snd_rawmidi_buffer_unref(runtime);
1337 spin_unlock_irqrestore(&runtime->lock, flags);
1339 snd_rawmidi_output_trigger(substream, 1);
1343 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1344 const unsigned char *buf, long count)
1346 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1348 EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1350 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1351 size_t count, loff_t *offset)
1353 long result, timeout;
1355 struct snd_rawmidi_file *rfile;
1356 struct snd_rawmidi_runtime *runtime;
1357 struct snd_rawmidi_substream *substream;
1359 rfile = file->private_data;
1360 substream = rfile->output;
1361 runtime = substream->runtime;
1362 /* we cannot put an atomic message to our buffer */
1363 if (substream->append && count > runtime->buffer_size)
1367 spin_lock_irq(&runtime->lock);
1368 while (!snd_rawmidi_ready_append(substream, count)) {
1369 wait_queue_entry_t wait;
1371 if (file->f_flags & O_NONBLOCK) {
1372 spin_unlock_irq(&runtime->lock);
1373 return result > 0 ? result : -EAGAIN;
1375 init_waitqueue_entry(&wait, current);
1376 add_wait_queue(&runtime->sleep, &wait);
1377 set_current_state(TASK_INTERRUPTIBLE);
1378 spin_unlock_irq(&runtime->lock);
1379 timeout = schedule_timeout(30 * HZ);
1380 remove_wait_queue(&runtime->sleep, &wait);
1381 if (rfile->rmidi->card->shutdown)
1383 if (signal_pending(current))
1384 return result > 0 ? result : -ERESTARTSYS;
1385 spin_lock_irq(&runtime->lock);
1386 if (!runtime->avail && !timeout) {
1387 spin_unlock_irq(&runtime->lock);
1388 return result > 0 ? result : -EIO;
1391 spin_unlock_irq(&runtime->lock);
1392 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1394 return result > 0 ? result : count1;
1397 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1401 if (file->f_flags & O_DSYNC) {
1402 spin_lock_irq(&runtime->lock);
1403 while (runtime->avail != runtime->buffer_size) {
1404 wait_queue_entry_t wait;
1405 unsigned int last_avail = runtime->avail;
1407 init_waitqueue_entry(&wait, current);
1408 add_wait_queue(&runtime->sleep, &wait);
1409 set_current_state(TASK_INTERRUPTIBLE);
1410 spin_unlock_irq(&runtime->lock);
1411 timeout = schedule_timeout(30 * HZ);
1412 remove_wait_queue(&runtime->sleep, &wait);
1413 if (signal_pending(current))
1414 return result > 0 ? result : -ERESTARTSYS;
1415 if (runtime->avail == last_avail && !timeout)
1416 return result > 0 ? result : -EIO;
1417 spin_lock_irq(&runtime->lock);
1419 spin_unlock_irq(&runtime->lock);
1424 static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait)
1426 struct snd_rawmidi_file *rfile;
1427 struct snd_rawmidi_runtime *runtime;
1430 rfile = file->private_data;
1431 if (rfile->input != NULL) {
1432 runtime = rfile->input->runtime;
1433 snd_rawmidi_input_trigger(rfile->input, 1);
1434 poll_wait(file, &runtime->sleep, wait);
1436 if (rfile->output != NULL) {
1437 runtime = rfile->output->runtime;
1438 poll_wait(file, &runtime->sleep, wait);
1441 if (rfile->input != NULL) {
1442 if (snd_rawmidi_ready(rfile->input))
1443 mask |= EPOLLIN | EPOLLRDNORM;
1445 if (rfile->output != NULL) {
1446 if (snd_rawmidi_ready(rfile->output))
1447 mask |= EPOLLOUT | EPOLLWRNORM;
1454 #ifdef CONFIG_COMPAT
1455 #include "rawmidi_compat.c"
1457 #define snd_rawmidi_ioctl_compat NULL
1463 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1464 struct snd_info_buffer *buffer)
1466 struct snd_rawmidi *rmidi;
1467 struct snd_rawmidi_substream *substream;
1468 struct snd_rawmidi_runtime *runtime;
1469 unsigned long buffer_size, avail, xruns;
1471 rmidi = entry->private_data;
1472 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1473 mutex_lock(&rmidi->open_mutex);
1474 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1475 list_for_each_entry(substream,
1476 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1480 " Tx bytes : %lu\n",
1482 (unsigned long) substream->bytes);
1483 if (substream->opened) {
1485 " Owner PID : %d\n",
1486 pid_vnr(substream->pid));
1487 runtime = substream->runtime;
1488 spin_lock_irq(&runtime->lock);
1489 buffer_size = runtime->buffer_size;
1490 avail = runtime->avail;
1491 spin_unlock_irq(&runtime->lock);
1494 " Buffer size : %lu\n"
1496 runtime->oss ? "OSS compatible" : "native",
1497 buffer_size, avail);
1501 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1502 list_for_each_entry(substream,
1503 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1507 " Rx bytes : %lu\n",
1509 (unsigned long) substream->bytes);
1510 if (substream->opened) {
1512 " Owner PID : %d\n",
1513 pid_vnr(substream->pid));
1514 runtime = substream->runtime;
1515 spin_lock_irq(&runtime->lock);
1516 buffer_size = runtime->buffer_size;
1517 avail = runtime->avail;
1518 xruns = runtime->xruns;
1519 spin_unlock_irq(&runtime->lock);
1521 " Buffer size : %lu\n"
1523 " Overruns : %lu\n",
1524 buffer_size, avail, xruns);
1528 mutex_unlock(&rmidi->open_mutex);
1532 * Register functions
1535 static const struct file_operations snd_rawmidi_f_ops = {
1536 .owner = THIS_MODULE,
1537 .read = snd_rawmidi_read,
1538 .write = snd_rawmidi_write,
1539 .open = snd_rawmidi_open,
1540 .release = snd_rawmidi_release,
1541 .llseek = no_llseek,
1542 .poll = snd_rawmidi_poll,
1543 .unlocked_ioctl = snd_rawmidi_ioctl,
1544 .compat_ioctl = snd_rawmidi_ioctl_compat,
1547 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1548 struct snd_rawmidi_str *stream,
1552 struct snd_rawmidi_substream *substream;
1555 for (idx = 0; idx < count; idx++) {
1556 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1559 substream->stream = direction;
1560 substream->number = idx;
1561 substream->rmidi = rmidi;
1562 substream->pstr = stream;
1563 list_add_tail(&substream->list, &stream->substreams);
1564 stream->substream_count++;
1569 static void release_rawmidi_device(struct device *dev)
1571 kfree(container_of(dev, struct snd_rawmidi, dev));
1575 * snd_rawmidi_new - create a rawmidi instance
1576 * @card: the card instance
1577 * @id: the id string
1578 * @device: the device index
1579 * @output_count: the number of output streams
1580 * @input_count: the number of input streams
1581 * @rrawmidi: the pointer to store the new rawmidi instance
1583 * Creates a new rawmidi instance.
1584 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1586 * Return: Zero if successful, or a negative error code on failure.
1588 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1589 int output_count, int input_count,
1590 struct snd_rawmidi **rrawmidi)
1592 struct snd_rawmidi *rmidi;
1594 static struct snd_device_ops ops = {
1595 .dev_free = snd_rawmidi_dev_free,
1596 .dev_register = snd_rawmidi_dev_register,
1597 .dev_disconnect = snd_rawmidi_dev_disconnect,
1600 if (snd_BUG_ON(!card))
1604 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1608 rmidi->device = device;
1609 mutex_init(&rmidi->open_mutex);
1610 init_waitqueue_head(&rmidi->open_wait);
1611 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1612 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1615 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1617 snd_device_initialize(&rmidi->dev, card);
1618 rmidi->dev.release = release_rawmidi_device;
1619 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1621 err = snd_rawmidi_alloc_substreams(rmidi,
1622 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1623 SNDRV_RAWMIDI_STREAM_INPUT,
1627 err = snd_rawmidi_alloc_substreams(rmidi,
1628 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1629 SNDRV_RAWMIDI_STREAM_OUTPUT,
1633 err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops);
1642 snd_rawmidi_free(rmidi);
1645 EXPORT_SYMBOL(snd_rawmidi_new);
1647 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1649 struct snd_rawmidi_substream *substream;
1651 while (!list_empty(&stream->substreams)) {
1652 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1653 list_del(&substream->list);
1658 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1663 snd_info_free_entry(rmidi->proc_entry);
1664 rmidi->proc_entry = NULL;
1665 if (rmidi->ops && rmidi->ops->dev_unregister)
1666 rmidi->ops->dev_unregister(rmidi);
1668 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1669 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1670 if (rmidi->private_free)
1671 rmidi->private_free(rmidi);
1672 put_device(&rmidi->dev);
1676 static int snd_rawmidi_dev_free(struct snd_device *device)
1678 struct snd_rawmidi *rmidi = device->device_data;
1680 return snd_rawmidi_free(rmidi);
1683 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1684 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1686 struct snd_rawmidi *rmidi = device->private_data;
1688 rmidi->seq_dev = NULL;
1692 static int snd_rawmidi_dev_register(struct snd_device *device)
1695 struct snd_info_entry *entry;
1697 struct snd_rawmidi *rmidi = device->device_data;
1699 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1702 mutex_lock(®ister_mutex);
1703 if (snd_rawmidi_search(rmidi->card, rmidi->device))
1706 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1707 mutex_unlock(®ister_mutex);
1711 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1712 rmidi->card, rmidi->device,
1713 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
1715 rmidi_err(rmidi, "unable to register\n");
1718 if (rmidi->ops && rmidi->ops->dev_register) {
1719 err = rmidi->ops->dev_register(rmidi);
1721 goto error_unregister;
1723 #ifdef CONFIG_SND_OSSEMUL
1725 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1726 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1727 rmidi->card, 0, &snd_rawmidi_f_ops,
1730 "unable to register OSS rawmidi device %i:%i\n",
1731 rmidi->card->number, 0);
1734 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1735 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1739 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1740 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1741 rmidi->card, 1, &snd_rawmidi_f_ops,
1744 "unable to register OSS rawmidi device %i:%i\n",
1745 rmidi->card->number, 1);
1750 #endif /* CONFIG_SND_OSSEMUL */
1751 sprintf(name, "midi%d", rmidi->device);
1752 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1754 entry->private_data = rmidi;
1755 entry->c.text.read = snd_rawmidi_proc_info_read;
1756 if (snd_info_register(entry) < 0) {
1757 snd_info_free_entry(entry);
1761 rmidi->proc_entry = entry;
1762 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1763 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1764 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1765 rmidi->seq_dev->private_data = rmidi;
1766 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1767 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1768 snd_device_register(rmidi->card, rmidi->seq_dev);
1775 snd_unregister_device(&rmidi->dev);
1777 mutex_lock(®ister_mutex);
1778 list_del(&rmidi->list);
1779 mutex_unlock(®ister_mutex);
1783 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1785 struct snd_rawmidi *rmidi = device->device_data;
1788 mutex_lock(®ister_mutex);
1789 mutex_lock(&rmidi->open_mutex);
1790 wake_up(&rmidi->open_wait);
1791 list_del_init(&rmidi->list);
1792 for (dir = 0; dir < 2; dir++) {
1793 struct snd_rawmidi_substream *s;
1795 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1797 wake_up(&s->runtime->sleep);
1801 #ifdef CONFIG_SND_OSSEMUL
1802 if (rmidi->ossreg) {
1803 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1804 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1805 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1806 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1809 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1810 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1813 #endif /* CONFIG_SND_OSSEMUL */
1814 snd_unregister_device(&rmidi->dev);
1815 mutex_unlock(&rmidi->open_mutex);
1816 mutex_unlock(®ister_mutex);
1821 * snd_rawmidi_set_ops - set the rawmidi operators
1822 * @rmidi: the rawmidi instance
1823 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1824 * @ops: the operator table
1826 * Sets the rawmidi operators for the given stream direction.
1828 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1829 const struct snd_rawmidi_ops *ops)
1831 struct snd_rawmidi_substream *substream;
1833 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1834 substream->ops = ops;
1836 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1842 static int __init alsa_rawmidi_init(void)
1845 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1846 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1847 #ifdef CONFIG_SND_OSSEMUL
1849 /* check device map table */
1850 for (i = 0; i < SNDRV_CARDS; i++) {
1851 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1852 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1856 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1857 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1863 #endif /* CONFIG_SND_OSSEMUL */
1867 static void __exit alsa_rawmidi_exit(void)
1869 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1870 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1873 module_init(alsa_rawmidi_init)
1874 module_exit(alsa_rawmidi_exit)