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 bool __snd_rawmidi_ready(struct snd_rawmidi_runtime *runtime)
92 return runtime->avail >= runtime->avail_min;
95 static bool snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
97 struct snd_rawmidi_runtime *runtime = substream->runtime;
101 spin_lock_irqsave(&runtime->lock, flags);
102 ready = __snd_rawmidi_ready(runtime);
103 spin_unlock_irqrestore(&runtime->lock, flags);
107 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
110 struct snd_rawmidi_runtime *runtime = substream->runtime;
112 return runtime->avail >= runtime->avail_min &&
113 (!substream->append || runtime->avail >= count);
116 static void snd_rawmidi_input_event_work(struct work_struct *work)
118 struct snd_rawmidi_runtime *runtime =
119 container_of(work, struct snd_rawmidi_runtime, event_work);
122 runtime->event(runtime->substream);
125 /* buffer refcount management: call with runtime->lock held */
126 static inline void snd_rawmidi_buffer_ref(struct snd_rawmidi_runtime *runtime)
128 runtime->buffer_ref++;
131 static inline void snd_rawmidi_buffer_unref(struct snd_rawmidi_runtime *runtime)
133 runtime->buffer_ref--;
136 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
138 struct snd_rawmidi_runtime *runtime;
140 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
143 runtime->substream = substream;
144 spin_lock_init(&runtime->lock);
145 init_waitqueue_head(&runtime->sleep);
146 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
147 runtime->event = NULL;
148 runtime->buffer_size = PAGE_SIZE;
149 runtime->avail_min = 1;
150 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
153 runtime->avail = runtime->buffer_size;
154 runtime->buffer = kvzalloc(runtime->buffer_size, GFP_KERNEL);
155 if (!runtime->buffer) {
159 runtime->appl_ptr = runtime->hw_ptr = 0;
160 substream->runtime = runtime;
164 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
166 struct snd_rawmidi_runtime *runtime = substream->runtime;
168 kvfree(runtime->buffer);
170 substream->runtime = NULL;
174 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
176 if (!substream->opened)
178 substream->ops->trigger(substream, up);
181 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
183 if (!substream->opened)
185 substream->ops->trigger(substream, up);
187 cancel_work_sync(&substream->runtime->event_work);
190 static void __reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
194 runtime->appl_ptr = runtime->hw_ptr = 0;
195 runtime->avail = is_input ? 0 : runtime->buffer_size;
198 static void reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
203 spin_lock_irqsave(&runtime->lock, flags);
204 __reset_runtime_ptrs(runtime, is_input);
205 spin_unlock_irqrestore(&runtime->lock, flags);
208 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
210 snd_rawmidi_output_trigger(substream, 0);
211 reset_runtime_ptrs(substream->runtime, false);
214 EXPORT_SYMBOL(snd_rawmidi_drop_output);
216 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
220 struct snd_rawmidi_runtime *runtime = substream->runtime;
224 timeout = wait_event_interruptible_timeout(runtime->sleep,
225 (runtime->avail >= runtime->buffer_size),
227 if (signal_pending(current))
229 if (runtime->avail < runtime->buffer_size && !timeout) {
230 rmidi_warn(substream->rmidi,
231 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
232 (long)runtime->avail, (long)runtime->buffer_size);
236 if (err != -ERESTARTSYS) {
237 /* we need wait a while to make sure that Tx FIFOs are empty */
238 if (substream->ops->drain)
239 substream->ops->drain(substream);
242 snd_rawmidi_drop_output(substream);
246 EXPORT_SYMBOL(snd_rawmidi_drain_output);
248 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
250 snd_rawmidi_input_trigger(substream, 0);
251 reset_runtime_ptrs(substream->runtime, true);
254 EXPORT_SYMBOL(snd_rawmidi_drain_input);
256 /* look for an available substream for the given stream direction;
257 * if a specific subdevice is given, try to assign it
259 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
260 int stream, int mode,
261 struct snd_rawmidi_substream **sub_ret)
263 struct snd_rawmidi_substream *substream;
264 struct snd_rawmidi_str *s = &rmidi->streams[stream];
265 static unsigned int info_flags[2] = {
266 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
267 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
270 if (!(rmidi->info_flags & info_flags[stream]))
272 if (subdevice >= 0 && subdevice >= s->substream_count)
275 list_for_each_entry(substream, &s->substreams, list) {
276 if (substream->opened) {
277 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
278 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
282 if (subdevice < 0 || subdevice == substream->number) {
283 *sub_ret = substream;
290 /* open and do ref-counting for the given substream */
291 static int open_substream(struct snd_rawmidi *rmidi,
292 struct snd_rawmidi_substream *substream,
297 if (substream->use_count == 0) {
298 err = snd_rawmidi_runtime_create(substream);
301 err = substream->ops->open(substream);
303 snd_rawmidi_runtime_free(substream);
306 substream->opened = 1;
307 substream->active_sensing = 0;
308 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
309 substream->append = 1;
310 substream->pid = get_pid(task_pid(current));
311 rmidi->streams[substream->stream].substream_opened++;
313 substream->use_count++;
317 static void close_substream(struct snd_rawmidi *rmidi,
318 struct snd_rawmidi_substream *substream,
321 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
322 struct snd_rawmidi_file *rfile)
324 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
327 rfile->input = rfile->output = NULL;
328 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
329 err = assign_substream(rmidi, subdevice,
330 SNDRV_RAWMIDI_STREAM_INPUT,
335 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
336 err = assign_substream(rmidi, subdevice,
337 SNDRV_RAWMIDI_STREAM_OUTPUT,
344 err = open_substream(rmidi, sinput, mode);
349 err = open_substream(rmidi, soutput, mode);
352 close_substream(rmidi, sinput, 0);
357 rfile->rmidi = rmidi;
358 rfile->input = sinput;
359 rfile->output = soutput;
363 /* called from sound/core/seq/seq_midi.c */
364 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
365 int mode, struct snd_rawmidi_file *rfile)
367 struct snd_rawmidi *rmidi;
370 if (snd_BUG_ON(!rfile))
373 mutex_lock(®ister_mutex);
374 rmidi = snd_rawmidi_search(card, device);
377 else if (!try_module_get(rmidi->card->module))
379 mutex_unlock(®ister_mutex);
383 mutex_lock(&rmidi->open_mutex);
384 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
385 mutex_unlock(&rmidi->open_mutex);
387 module_put(rmidi->card->module);
390 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
392 static int snd_rawmidi_open(struct inode *inode, struct file *file)
394 int maj = imajor(inode);
395 struct snd_card *card;
397 unsigned short fflags;
399 struct snd_rawmidi *rmidi;
400 struct snd_rawmidi_file *rawmidi_file = NULL;
401 wait_queue_entry_t wait;
403 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
404 return -EINVAL; /* invalid combination */
406 err = nonseekable_open(inode, file);
410 if (maj == snd_major) {
411 rmidi = snd_lookup_minor_data(iminor(inode),
412 SNDRV_DEVICE_TYPE_RAWMIDI);
413 #ifdef CONFIG_SND_OSSEMUL
414 } else if (maj == SOUND_MAJOR) {
415 rmidi = snd_lookup_oss_minor_data(iminor(inode),
416 SNDRV_OSS_DEVICE_TYPE_MIDI);
424 if (!try_module_get(rmidi->card->module)) {
425 snd_card_unref(rmidi->card);
429 mutex_lock(&rmidi->open_mutex);
431 err = snd_card_file_add(card, file);
434 fflags = snd_rawmidi_file_flags(file);
435 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
436 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
437 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
438 if (rawmidi_file == NULL) {
442 init_waitqueue_entry(&wait, current);
443 add_wait_queue(&rmidi->open_wait, &wait);
445 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
446 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
449 if (err == -EAGAIN) {
450 if (file->f_flags & O_NONBLOCK) {
456 set_current_state(TASK_INTERRUPTIBLE);
457 mutex_unlock(&rmidi->open_mutex);
459 mutex_lock(&rmidi->open_mutex);
460 if (rmidi->card->shutdown) {
464 if (signal_pending(current)) {
469 remove_wait_queue(&rmidi->open_wait, &wait);
474 #ifdef CONFIG_SND_OSSEMUL
475 if (rawmidi_file->input && rawmidi_file->input->runtime)
476 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
477 if (rawmidi_file->output && rawmidi_file->output->runtime)
478 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
480 file->private_data = rawmidi_file;
481 mutex_unlock(&rmidi->open_mutex);
482 snd_card_unref(rmidi->card);
486 snd_card_file_remove(card, file);
488 mutex_unlock(&rmidi->open_mutex);
489 module_put(rmidi->card->module);
490 snd_card_unref(rmidi->card);
494 static void close_substream(struct snd_rawmidi *rmidi,
495 struct snd_rawmidi_substream *substream,
498 if (--substream->use_count)
502 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
503 snd_rawmidi_input_trigger(substream, 0);
505 if (substream->active_sensing) {
506 unsigned char buf = 0xfe;
507 /* sending single active sensing message
508 * to shut the device up
510 snd_rawmidi_kernel_write(substream, &buf, 1);
512 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
513 snd_rawmidi_output_trigger(substream, 0);
516 substream->ops->close(substream);
517 if (substream->runtime->private_free)
518 substream->runtime->private_free(substream);
519 snd_rawmidi_runtime_free(substream);
520 substream->opened = 0;
521 substream->append = 0;
522 put_pid(substream->pid);
523 substream->pid = NULL;
524 rmidi->streams[substream->stream].substream_opened--;
527 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
529 struct snd_rawmidi *rmidi;
531 rmidi = rfile->rmidi;
532 mutex_lock(&rmidi->open_mutex);
534 close_substream(rmidi, rfile->input, 1);
538 close_substream(rmidi, rfile->output, 1);
539 rfile->output = NULL;
542 mutex_unlock(&rmidi->open_mutex);
543 wake_up(&rmidi->open_wait);
546 /* called from sound/core/seq/seq_midi.c */
547 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
549 struct snd_rawmidi *rmidi;
551 if (snd_BUG_ON(!rfile))
554 rmidi = rfile->rmidi;
555 rawmidi_release_priv(rfile);
556 module_put(rmidi->card->module);
559 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
561 static int snd_rawmidi_release(struct inode *inode, struct file *file)
563 struct snd_rawmidi_file *rfile;
564 struct snd_rawmidi *rmidi;
565 struct module *module;
567 rfile = file->private_data;
568 rmidi = rfile->rmidi;
569 rawmidi_release_priv(rfile);
571 module = rmidi->card->module;
572 snd_card_file_remove(rmidi->card, file);
577 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
578 struct snd_rawmidi_info *info)
580 struct snd_rawmidi *rmidi;
582 if (substream == NULL)
584 rmidi = substream->rmidi;
585 memset(info, 0, sizeof(*info));
586 info->card = rmidi->card->number;
587 info->device = rmidi->device;
588 info->subdevice = substream->number;
589 info->stream = substream->stream;
590 info->flags = rmidi->info_flags;
591 strcpy(info->id, rmidi->id);
592 strcpy(info->name, rmidi->name);
593 strcpy(info->subname, substream->name);
594 info->subdevices_count = substream->pstr->substream_count;
595 info->subdevices_avail = (substream->pstr->substream_count -
596 substream->pstr->substream_opened);
600 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
601 struct snd_rawmidi_info __user *_info)
603 struct snd_rawmidi_info info;
606 err = snd_rawmidi_info(substream, &info);
609 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
614 static int __snd_rawmidi_info_select(struct snd_card *card,
615 struct snd_rawmidi_info *info)
617 struct snd_rawmidi *rmidi;
618 struct snd_rawmidi_str *pstr;
619 struct snd_rawmidi_substream *substream;
621 rmidi = snd_rawmidi_search(card, info->device);
624 if (info->stream < 0 || info->stream > 1)
626 info->stream = array_index_nospec(info->stream, 2);
627 pstr = &rmidi->streams[info->stream];
628 if (pstr->substream_count == 0)
630 if (info->subdevice >= pstr->substream_count)
632 list_for_each_entry(substream, &pstr->substreams, list) {
633 if ((unsigned int)substream->number == info->subdevice)
634 return snd_rawmidi_info(substream, info);
639 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
643 mutex_lock(®ister_mutex);
644 ret = __snd_rawmidi_info_select(card, info);
645 mutex_unlock(®ister_mutex);
648 EXPORT_SYMBOL(snd_rawmidi_info_select);
650 static int snd_rawmidi_info_select_user(struct snd_card *card,
651 struct snd_rawmidi_info __user *_info)
654 struct snd_rawmidi_info info;
656 if (get_user(info.device, &_info->device))
658 if (get_user(info.stream, &_info->stream))
660 if (get_user(info.subdevice, &_info->subdevice))
662 err = snd_rawmidi_info_select(card, &info);
665 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
670 static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime,
671 struct snd_rawmidi_params *params,
674 char *newbuf, *oldbuf;
676 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L)
678 if (params->avail_min < 1 || params->avail_min > params->buffer_size)
680 if (params->buffer_size != runtime->buffer_size) {
681 newbuf = kvzalloc(params->buffer_size, GFP_KERNEL);
684 spin_lock_irq(&runtime->lock);
685 if (runtime->buffer_ref) {
686 spin_unlock_irq(&runtime->lock);
690 oldbuf = runtime->buffer;
691 runtime->buffer = newbuf;
692 runtime->buffer_size = params->buffer_size;
693 __reset_runtime_ptrs(runtime, is_input);
694 spin_unlock_irq(&runtime->lock);
697 runtime->avail_min = params->avail_min;
701 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
702 struct snd_rawmidi_params *params)
704 if (substream->append && substream->use_count > 1)
706 snd_rawmidi_drain_output(substream);
707 substream->active_sensing = !params->no_active_sensing;
708 return resize_runtime_buffer(substream->runtime, params, false);
710 EXPORT_SYMBOL(snd_rawmidi_output_params);
712 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
713 struct snd_rawmidi_params *params)
715 snd_rawmidi_drain_input(substream);
716 return resize_runtime_buffer(substream->runtime, params, true);
718 EXPORT_SYMBOL(snd_rawmidi_input_params);
720 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
721 struct snd_rawmidi_status *status)
723 struct snd_rawmidi_runtime *runtime = substream->runtime;
725 memset(status, 0, sizeof(*status));
726 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
727 spin_lock_irq(&runtime->lock);
728 status->avail = runtime->avail;
729 spin_unlock_irq(&runtime->lock);
733 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
734 struct snd_rawmidi_status *status)
736 struct snd_rawmidi_runtime *runtime = substream->runtime;
738 memset(status, 0, sizeof(*status));
739 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
740 spin_lock_irq(&runtime->lock);
741 status->avail = runtime->avail;
742 status->xruns = runtime->xruns;
744 spin_unlock_irq(&runtime->lock);
748 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
750 struct snd_rawmidi_file *rfile;
751 void __user *argp = (void __user *)arg;
753 rfile = file->private_data;
754 if (((cmd >> 8) & 0xff) != 'W')
757 case SNDRV_RAWMIDI_IOCTL_PVERSION:
758 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
759 case SNDRV_RAWMIDI_IOCTL_INFO:
762 struct snd_rawmidi_info __user *info = argp;
764 if (get_user(stream, &info->stream))
767 case SNDRV_RAWMIDI_STREAM_INPUT:
768 return snd_rawmidi_info_user(rfile->input, info);
769 case SNDRV_RAWMIDI_STREAM_OUTPUT:
770 return snd_rawmidi_info_user(rfile->output, info);
775 case SNDRV_RAWMIDI_IOCTL_PARAMS:
777 struct snd_rawmidi_params params;
779 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
781 switch (params.stream) {
782 case SNDRV_RAWMIDI_STREAM_OUTPUT:
783 if (rfile->output == NULL)
785 return snd_rawmidi_output_params(rfile->output, ¶ms);
786 case SNDRV_RAWMIDI_STREAM_INPUT:
787 if (rfile->input == NULL)
789 return snd_rawmidi_input_params(rfile->input, ¶ms);
794 case SNDRV_RAWMIDI_IOCTL_STATUS:
797 struct snd_rawmidi_status status;
799 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
801 switch (status.stream) {
802 case SNDRV_RAWMIDI_STREAM_OUTPUT:
803 if (rfile->output == NULL)
805 err = snd_rawmidi_output_status(rfile->output, &status);
807 case SNDRV_RAWMIDI_STREAM_INPUT:
808 if (rfile->input == NULL)
810 err = snd_rawmidi_input_status(rfile->input, &status);
817 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
821 case SNDRV_RAWMIDI_IOCTL_DROP:
825 if (get_user(val, (int __user *) argp))
828 case SNDRV_RAWMIDI_STREAM_OUTPUT:
829 if (rfile->output == NULL)
831 return snd_rawmidi_drop_output(rfile->output);
836 case SNDRV_RAWMIDI_IOCTL_DRAIN:
840 if (get_user(val, (int __user *) argp))
843 case SNDRV_RAWMIDI_STREAM_OUTPUT:
844 if (rfile->output == NULL)
846 return snd_rawmidi_drain_output(rfile->output);
847 case SNDRV_RAWMIDI_STREAM_INPUT:
848 if (rfile->input == NULL)
850 return snd_rawmidi_drain_input(rfile->input);
856 rmidi_dbg(rfile->rmidi,
857 "rawmidi: unknown command = 0x%x\n", cmd);
862 static int snd_rawmidi_control_ioctl(struct snd_card *card,
863 struct snd_ctl_file *control,
867 void __user *argp = (void __user *)arg;
870 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
874 if (get_user(device, (int __user *)argp))
876 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
877 device = SNDRV_RAWMIDI_DEVICES - 1;
878 mutex_lock(®ister_mutex);
879 device = device < 0 ? 0 : device + 1;
880 while (device < SNDRV_RAWMIDI_DEVICES) {
881 if (snd_rawmidi_search(card, device))
885 if (device == SNDRV_RAWMIDI_DEVICES)
887 mutex_unlock(®ister_mutex);
888 if (put_user(device, (int __user *)argp))
892 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
896 if (get_user(val, (int __user *)argp))
898 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
901 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
902 return snd_rawmidi_info_select_user(card, argp);
908 * snd_rawmidi_receive - receive the input data from the device
909 * @substream: the rawmidi substream
910 * @buffer: the buffer pointer
911 * @count: the data size to read
913 * Reads the data from the internal buffer.
915 * Return: The size of read data, or a negative error code on failure.
917 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
918 const unsigned char *buffer, int count)
921 int result = 0, count1;
922 struct snd_rawmidi_runtime *runtime = substream->runtime;
924 if (!substream->opened)
926 if (runtime->buffer == NULL) {
927 rmidi_dbg(substream->rmidi,
928 "snd_rawmidi_receive: input is not active!!!\n");
931 spin_lock_irqsave(&runtime->lock, flags);
932 if (count == 1) { /* special case, faster code */
934 if (runtime->avail < runtime->buffer_size) {
935 runtime->buffer[runtime->hw_ptr++] = buffer[0];
936 runtime->hw_ptr %= runtime->buffer_size;
943 substream->bytes += count;
944 count1 = runtime->buffer_size - runtime->hw_ptr;
947 if (count1 > (int)(runtime->buffer_size - runtime->avail))
948 count1 = runtime->buffer_size - runtime->avail;
949 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
950 runtime->hw_ptr += count1;
951 runtime->hw_ptr %= runtime->buffer_size;
952 runtime->avail += count1;
958 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
959 count1 = runtime->buffer_size - runtime->avail;
960 runtime->xruns += count - count1;
963 memcpy(runtime->buffer, buffer, count1);
964 runtime->hw_ptr = count1;
965 runtime->avail += count1;
972 schedule_work(&runtime->event_work);
973 else if (__snd_rawmidi_ready(runtime))
974 wake_up(&runtime->sleep);
976 spin_unlock_irqrestore(&runtime->lock, flags);
979 EXPORT_SYMBOL(snd_rawmidi_receive);
981 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
982 unsigned char __user *userbuf,
983 unsigned char *kernelbuf, long count)
986 long result = 0, count1;
987 struct snd_rawmidi_runtime *runtime = substream->runtime;
988 unsigned long appl_ptr;
991 spin_lock_irqsave(&runtime->lock, flags);
992 snd_rawmidi_buffer_ref(runtime);
993 while (count > 0 && runtime->avail) {
994 count1 = runtime->buffer_size - runtime->appl_ptr;
997 if (count1 > (int)runtime->avail)
998 count1 = runtime->avail;
1000 /* update runtime->appl_ptr before unlocking for userbuf */
1001 appl_ptr = runtime->appl_ptr;
1002 runtime->appl_ptr += count1;
1003 runtime->appl_ptr %= runtime->buffer_size;
1004 runtime->avail -= count1;
1007 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
1009 spin_unlock_irqrestore(&runtime->lock, flags);
1010 if (copy_to_user(userbuf + result,
1011 runtime->buffer + appl_ptr, count1))
1013 spin_lock_irqsave(&runtime->lock, flags);
1021 snd_rawmidi_buffer_unref(runtime);
1022 spin_unlock_irqrestore(&runtime->lock, flags);
1023 return result > 0 ? result : err;
1026 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
1027 unsigned char *buf, long count)
1029 snd_rawmidi_input_trigger(substream, 1);
1030 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
1032 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1034 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1039 struct snd_rawmidi_file *rfile;
1040 struct snd_rawmidi_substream *substream;
1041 struct snd_rawmidi_runtime *runtime;
1043 rfile = file->private_data;
1044 substream = rfile->input;
1045 if (substream == NULL)
1047 runtime = substream->runtime;
1048 snd_rawmidi_input_trigger(substream, 1);
1051 spin_lock_irq(&runtime->lock);
1052 while (!__snd_rawmidi_ready(runtime)) {
1053 wait_queue_entry_t wait;
1055 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1056 spin_unlock_irq(&runtime->lock);
1057 return result > 0 ? result : -EAGAIN;
1059 init_waitqueue_entry(&wait, current);
1060 add_wait_queue(&runtime->sleep, &wait);
1061 set_current_state(TASK_INTERRUPTIBLE);
1062 spin_unlock_irq(&runtime->lock);
1064 remove_wait_queue(&runtime->sleep, &wait);
1065 if (rfile->rmidi->card->shutdown)
1067 if (signal_pending(current))
1068 return result > 0 ? result : -ERESTARTSYS;
1069 spin_lock_irq(&runtime->lock);
1070 if (!runtime->avail) {
1071 spin_unlock_irq(&runtime->lock);
1072 return result > 0 ? result : -EIO;
1075 spin_unlock_irq(&runtime->lock);
1076 count1 = snd_rawmidi_kernel_read1(substream,
1077 (unsigned char __user *)buf,
1081 return result > 0 ? result : count1;
1090 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1091 * @substream: the rawmidi substream
1093 * Return: 1 if the internal output buffer is empty, 0 if not.
1095 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1097 struct snd_rawmidi_runtime *runtime = substream->runtime;
1099 unsigned long flags;
1101 if (runtime->buffer == NULL) {
1102 rmidi_dbg(substream->rmidi,
1103 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1106 spin_lock_irqsave(&runtime->lock, flags);
1107 result = runtime->avail >= runtime->buffer_size;
1108 spin_unlock_irqrestore(&runtime->lock, flags);
1111 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1114 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1115 * @substream: the rawmidi substream
1116 * @buffer: the buffer pointer
1117 * @count: data size to transfer
1119 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1121 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1122 unsigned char *buffer, int count)
1125 struct snd_rawmidi_runtime *runtime = substream->runtime;
1127 if (runtime->buffer == NULL) {
1128 rmidi_dbg(substream->rmidi,
1129 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1133 if (runtime->avail >= runtime->buffer_size) {
1134 /* warning: lowlevel layer MUST trigger down the hardware */
1137 if (count == 1) { /* special case, faster code */
1138 *buffer = runtime->buffer[runtime->hw_ptr];
1141 count1 = runtime->buffer_size - runtime->hw_ptr;
1144 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1145 count1 = runtime->buffer_size - runtime->avail;
1146 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1150 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1151 count = runtime->buffer_size - runtime->avail - count1;
1152 memcpy(buffer + count1, runtime->buffer, count);
1159 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1162 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1163 * @substream: the rawmidi substream
1164 * @buffer: the buffer pointer
1165 * @count: data size to transfer
1167 * Copies data from the internal output buffer to the given buffer.
1169 * Call this in the interrupt handler when the midi output is ready,
1170 * and call snd_rawmidi_transmit_ack() after the transmission is
1173 * Return: The size of copied data, or a negative error code on failure.
1175 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1176 unsigned char *buffer, int count)
1178 struct snd_rawmidi_runtime *runtime = substream->runtime;
1180 unsigned long flags;
1182 spin_lock_irqsave(&runtime->lock, flags);
1183 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1184 spin_unlock_irqrestore(&runtime->lock, flags);
1187 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1190 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1191 * @substream: the rawmidi substream
1192 * @count: the transferred count
1194 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1196 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1198 struct snd_rawmidi_runtime *runtime = substream->runtime;
1200 if (runtime->buffer == NULL) {
1201 rmidi_dbg(substream->rmidi,
1202 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1205 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1206 runtime->hw_ptr += count;
1207 runtime->hw_ptr %= runtime->buffer_size;
1208 runtime->avail += count;
1209 substream->bytes += count;
1211 if (runtime->drain || __snd_rawmidi_ready(runtime))
1212 wake_up(&runtime->sleep);
1216 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1219 * snd_rawmidi_transmit_ack - acknowledge the transmission
1220 * @substream: the rawmidi substream
1221 * @count: the transferred count
1223 * Advances the hardware pointer for the internal output buffer with
1224 * the given size and updates the condition.
1225 * Call after the transmission is finished.
1227 * Return: The advanced size if successful, or a negative error code on failure.
1229 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1231 struct snd_rawmidi_runtime *runtime = substream->runtime;
1233 unsigned long flags;
1235 spin_lock_irqsave(&runtime->lock, flags);
1236 result = __snd_rawmidi_transmit_ack(substream, count);
1237 spin_unlock_irqrestore(&runtime->lock, flags);
1240 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1243 * snd_rawmidi_transmit - copy from the buffer to the device
1244 * @substream: the rawmidi substream
1245 * @buffer: the buffer pointer
1246 * @count: the data size to transfer
1248 * Copies data from the buffer to the device and advances the pointer.
1250 * Return: The copied size if successful, or a negative error code on failure.
1252 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1253 unsigned char *buffer, int count)
1255 struct snd_rawmidi_runtime *runtime = substream->runtime;
1257 unsigned long flags;
1259 spin_lock_irqsave(&runtime->lock, flags);
1260 if (!substream->opened)
1263 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1267 result = __snd_rawmidi_transmit_ack(substream, count);
1269 spin_unlock_irqrestore(&runtime->lock, flags);
1272 EXPORT_SYMBOL(snd_rawmidi_transmit);
1274 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1275 const unsigned char __user *userbuf,
1276 const unsigned char *kernelbuf,
1279 unsigned long flags;
1280 long count1, result;
1281 struct snd_rawmidi_runtime *runtime = substream->runtime;
1282 unsigned long appl_ptr;
1284 if (!kernelbuf && !userbuf)
1286 if (snd_BUG_ON(!runtime->buffer))
1290 spin_lock_irqsave(&runtime->lock, flags);
1291 if (substream->append) {
1292 if ((long)runtime->avail < count) {
1293 spin_unlock_irqrestore(&runtime->lock, flags);
1297 snd_rawmidi_buffer_ref(runtime);
1298 while (count > 0 && runtime->avail > 0) {
1299 count1 = runtime->buffer_size - runtime->appl_ptr;
1302 if (count1 > (long)runtime->avail)
1303 count1 = runtime->avail;
1305 /* update runtime->appl_ptr before unlocking for userbuf */
1306 appl_ptr = runtime->appl_ptr;
1307 runtime->appl_ptr += count1;
1308 runtime->appl_ptr %= runtime->buffer_size;
1309 runtime->avail -= count1;
1312 memcpy(runtime->buffer + appl_ptr,
1313 kernelbuf + result, count1);
1315 spin_unlock_irqrestore(&runtime->lock, flags);
1316 if (copy_from_user(runtime->buffer + appl_ptr,
1317 userbuf + result, count1)) {
1318 spin_lock_irqsave(&runtime->lock, flags);
1319 result = result > 0 ? result : -EFAULT;
1322 spin_lock_irqsave(&runtime->lock, flags);
1328 count1 = runtime->avail < runtime->buffer_size;
1329 snd_rawmidi_buffer_unref(runtime);
1330 spin_unlock_irqrestore(&runtime->lock, flags);
1332 snd_rawmidi_output_trigger(substream, 1);
1336 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1337 const unsigned char *buf, long count)
1339 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1341 EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1343 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1344 size_t count, loff_t *offset)
1346 long result, timeout;
1348 struct snd_rawmidi_file *rfile;
1349 struct snd_rawmidi_runtime *runtime;
1350 struct snd_rawmidi_substream *substream;
1352 rfile = file->private_data;
1353 substream = rfile->output;
1354 runtime = substream->runtime;
1355 /* we cannot put an atomic message to our buffer */
1356 if (substream->append && count > runtime->buffer_size)
1360 spin_lock_irq(&runtime->lock);
1361 while (!snd_rawmidi_ready_append(substream, count)) {
1362 wait_queue_entry_t wait;
1364 if (file->f_flags & O_NONBLOCK) {
1365 spin_unlock_irq(&runtime->lock);
1366 return result > 0 ? result : -EAGAIN;
1368 init_waitqueue_entry(&wait, current);
1369 add_wait_queue(&runtime->sleep, &wait);
1370 set_current_state(TASK_INTERRUPTIBLE);
1371 spin_unlock_irq(&runtime->lock);
1372 timeout = schedule_timeout(30 * HZ);
1373 remove_wait_queue(&runtime->sleep, &wait);
1374 if (rfile->rmidi->card->shutdown)
1376 if (signal_pending(current))
1377 return result > 0 ? result : -ERESTARTSYS;
1378 spin_lock_irq(&runtime->lock);
1379 if (!runtime->avail && !timeout) {
1380 spin_unlock_irq(&runtime->lock);
1381 return result > 0 ? result : -EIO;
1384 spin_unlock_irq(&runtime->lock);
1385 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1387 return result > 0 ? result : count1;
1390 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1394 if (file->f_flags & O_DSYNC) {
1395 spin_lock_irq(&runtime->lock);
1396 while (runtime->avail != runtime->buffer_size) {
1397 wait_queue_entry_t wait;
1398 unsigned int last_avail = runtime->avail;
1400 init_waitqueue_entry(&wait, current);
1401 add_wait_queue(&runtime->sleep, &wait);
1402 set_current_state(TASK_INTERRUPTIBLE);
1403 spin_unlock_irq(&runtime->lock);
1404 timeout = schedule_timeout(30 * HZ);
1405 remove_wait_queue(&runtime->sleep, &wait);
1406 if (signal_pending(current))
1407 return result > 0 ? result : -ERESTARTSYS;
1408 if (runtime->avail == last_avail && !timeout)
1409 return result > 0 ? result : -EIO;
1410 spin_lock_irq(&runtime->lock);
1412 spin_unlock_irq(&runtime->lock);
1417 static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait)
1419 struct snd_rawmidi_file *rfile;
1420 struct snd_rawmidi_runtime *runtime;
1423 rfile = file->private_data;
1424 if (rfile->input != NULL) {
1425 runtime = rfile->input->runtime;
1426 snd_rawmidi_input_trigger(rfile->input, 1);
1427 poll_wait(file, &runtime->sleep, wait);
1429 if (rfile->output != NULL) {
1430 runtime = rfile->output->runtime;
1431 poll_wait(file, &runtime->sleep, wait);
1434 if (rfile->input != NULL) {
1435 if (snd_rawmidi_ready(rfile->input))
1436 mask |= EPOLLIN | EPOLLRDNORM;
1438 if (rfile->output != NULL) {
1439 if (snd_rawmidi_ready(rfile->output))
1440 mask |= EPOLLOUT | EPOLLWRNORM;
1447 #ifdef CONFIG_COMPAT
1448 #include "rawmidi_compat.c"
1450 #define snd_rawmidi_ioctl_compat NULL
1456 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1457 struct snd_info_buffer *buffer)
1459 struct snd_rawmidi *rmidi;
1460 struct snd_rawmidi_substream *substream;
1461 struct snd_rawmidi_runtime *runtime;
1462 unsigned long buffer_size, avail, xruns;
1464 rmidi = entry->private_data;
1465 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1466 mutex_lock(&rmidi->open_mutex);
1467 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1468 list_for_each_entry(substream,
1469 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1473 " Tx bytes : %lu\n",
1475 (unsigned long) substream->bytes);
1476 if (substream->opened) {
1478 " Owner PID : %d\n",
1479 pid_vnr(substream->pid));
1480 runtime = substream->runtime;
1481 spin_lock_irq(&runtime->lock);
1482 buffer_size = runtime->buffer_size;
1483 avail = runtime->avail;
1484 spin_unlock_irq(&runtime->lock);
1487 " Buffer size : %lu\n"
1489 runtime->oss ? "OSS compatible" : "native",
1490 buffer_size, avail);
1494 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1495 list_for_each_entry(substream,
1496 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1500 " Rx bytes : %lu\n",
1502 (unsigned long) substream->bytes);
1503 if (substream->opened) {
1505 " Owner PID : %d\n",
1506 pid_vnr(substream->pid));
1507 runtime = substream->runtime;
1508 spin_lock_irq(&runtime->lock);
1509 buffer_size = runtime->buffer_size;
1510 avail = runtime->avail;
1511 xruns = runtime->xruns;
1512 spin_unlock_irq(&runtime->lock);
1514 " Buffer size : %lu\n"
1516 " Overruns : %lu\n",
1517 buffer_size, avail, xruns);
1521 mutex_unlock(&rmidi->open_mutex);
1525 * Register functions
1528 static const struct file_operations snd_rawmidi_f_ops = {
1529 .owner = THIS_MODULE,
1530 .read = snd_rawmidi_read,
1531 .write = snd_rawmidi_write,
1532 .open = snd_rawmidi_open,
1533 .release = snd_rawmidi_release,
1534 .llseek = no_llseek,
1535 .poll = snd_rawmidi_poll,
1536 .unlocked_ioctl = snd_rawmidi_ioctl,
1537 .compat_ioctl = snd_rawmidi_ioctl_compat,
1540 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1541 struct snd_rawmidi_str *stream,
1545 struct snd_rawmidi_substream *substream;
1548 for (idx = 0; idx < count; idx++) {
1549 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1552 substream->stream = direction;
1553 substream->number = idx;
1554 substream->rmidi = rmidi;
1555 substream->pstr = stream;
1556 list_add_tail(&substream->list, &stream->substreams);
1557 stream->substream_count++;
1562 static void release_rawmidi_device(struct device *dev)
1564 kfree(container_of(dev, struct snd_rawmidi, dev));
1568 * snd_rawmidi_new - create a rawmidi instance
1569 * @card: the card instance
1570 * @id: the id string
1571 * @device: the device index
1572 * @output_count: the number of output streams
1573 * @input_count: the number of input streams
1574 * @rrawmidi: the pointer to store the new rawmidi instance
1576 * Creates a new rawmidi instance.
1577 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1579 * Return: Zero if successful, or a negative error code on failure.
1581 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1582 int output_count, int input_count,
1583 struct snd_rawmidi **rrawmidi)
1585 struct snd_rawmidi *rmidi;
1587 static struct snd_device_ops ops = {
1588 .dev_free = snd_rawmidi_dev_free,
1589 .dev_register = snd_rawmidi_dev_register,
1590 .dev_disconnect = snd_rawmidi_dev_disconnect,
1593 if (snd_BUG_ON(!card))
1597 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1601 rmidi->device = device;
1602 mutex_init(&rmidi->open_mutex);
1603 init_waitqueue_head(&rmidi->open_wait);
1604 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1605 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1608 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1610 snd_device_initialize(&rmidi->dev, card);
1611 rmidi->dev.release = release_rawmidi_device;
1612 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1614 err = snd_rawmidi_alloc_substreams(rmidi,
1615 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1616 SNDRV_RAWMIDI_STREAM_INPUT,
1620 err = snd_rawmidi_alloc_substreams(rmidi,
1621 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1622 SNDRV_RAWMIDI_STREAM_OUTPUT,
1626 err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops);
1635 snd_rawmidi_free(rmidi);
1638 EXPORT_SYMBOL(snd_rawmidi_new);
1640 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1642 struct snd_rawmidi_substream *substream;
1644 while (!list_empty(&stream->substreams)) {
1645 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1646 list_del(&substream->list);
1651 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1656 snd_info_free_entry(rmidi->proc_entry);
1657 rmidi->proc_entry = NULL;
1658 mutex_lock(®ister_mutex);
1659 if (rmidi->ops && rmidi->ops->dev_unregister)
1660 rmidi->ops->dev_unregister(rmidi);
1661 mutex_unlock(®ister_mutex);
1663 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1664 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1665 if (rmidi->private_free)
1666 rmidi->private_free(rmidi);
1667 put_device(&rmidi->dev);
1671 static int snd_rawmidi_dev_free(struct snd_device *device)
1673 struct snd_rawmidi *rmidi = device->device_data;
1675 return snd_rawmidi_free(rmidi);
1678 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1679 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1681 struct snd_rawmidi *rmidi = device->private_data;
1683 rmidi->seq_dev = NULL;
1687 static int snd_rawmidi_dev_register(struct snd_device *device)
1690 struct snd_info_entry *entry;
1692 struct snd_rawmidi *rmidi = device->device_data;
1694 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1697 mutex_lock(®ister_mutex);
1698 if (snd_rawmidi_search(rmidi->card, rmidi->device))
1701 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1702 mutex_unlock(®ister_mutex);
1706 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1707 rmidi->card, rmidi->device,
1708 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
1710 rmidi_err(rmidi, "unable to register\n");
1713 if (rmidi->ops && rmidi->ops->dev_register) {
1714 err = rmidi->ops->dev_register(rmidi);
1716 goto error_unregister;
1718 #ifdef CONFIG_SND_OSSEMUL
1720 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1721 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1722 rmidi->card, 0, &snd_rawmidi_f_ops,
1725 "unable to register OSS rawmidi device %i:%i\n",
1726 rmidi->card->number, 0);
1729 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1730 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1734 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1735 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1736 rmidi->card, 1, &snd_rawmidi_f_ops,
1739 "unable to register OSS rawmidi device %i:%i\n",
1740 rmidi->card->number, 1);
1745 #endif /* CONFIG_SND_OSSEMUL */
1746 sprintf(name, "midi%d", rmidi->device);
1747 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1749 entry->private_data = rmidi;
1750 entry->c.text.read = snd_rawmidi_proc_info_read;
1751 if (snd_info_register(entry) < 0) {
1752 snd_info_free_entry(entry);
1756 rmidi->proc_entry = entry;
1757 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1758 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1759 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1760 rmidi->seq_dev->private_data = rmidi;
1761 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1762 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1763 snd_device_register(rmidi->card, rmidi->seq_dev);
1770 snd_unregister_device(&rmidi->dev);
1772 mutex_lock(®ister_mutex);
1773 list_del(&rmidi->list);
1774 mutex_unlock(®ister_mutex);
1778 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1780 struct snd_rawmidi *rmidi = device->device_data;
1783 mutex_lock(®ister_mutex);
1784 mutex_lock(&rmidi->open_mutex);
1785 wake_up(&rmidi->open_wait);
1786 list_del_init(&rmidi->list);
1787 for (dir = 0; dir < 2; dir++) {
1788 struct snd_rawmidi_substream *s;
1790 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1792 wake_up(&s->runtime->sleep);
1796 #ifdef CONFIG_SND_OSSEMUL
1797 if (rmidi->ossreg) {
1798 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1799 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1800 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1801 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1804 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1805 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1808 #endif /* CONFIG_SND_OSSEMUL */
1809 snd_unregister_device(&rmidi->dev);
1810 mutex_unlock(&rmidi->open_mutex);
1811 mutex_unlock(®ister_mutex);
1816 * snd_rawmidi_set_ops - set the rawmidi operators
1817 * @rmidi: the rawmidi instance
1818 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1819 * @ops: the operator table
1821 * Sets the rawmidi operators for the given stream direction.
1823 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1824 const struct snd_rawmidi_ops *ops)
1826 struct snd_rawmidi_substream *substream;
1828 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1829 substream->ops = ops;
1831 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1837 static int __init alsa_rawmidi_init(void)
1840 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1841 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1842 #ifdef CONFIG_SND_OSSEMUL
1844 /* check device map table */
1845 for (i = 0; i < SNDRV_CARDS; i++) {
1846 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1847 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1851 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1852 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1858 #endif /* CONFIG_SND_OSSEMUL */
1862 static void __exit alsa_rawmidi_exit(void)
1864 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1865 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1868 module_init(alsa_rawmidi_init)
1869 module_exit(alsa_rawmidi_exit)