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 *rmidi);
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 struct snd_rawmidi_status32 {
55 s32 tstamp_sec; /* Timestamp */
57 u32 avail; /* available bytes */
58 u32 xruns; /* count of overruns since last status (in bytes) */
59 unsigned char reserved[16]; /* reserved for future use */
62 #define SNDRV_RAWMIDI_IOCTL_STATUS32 _IOWR('W', 0x20, struct snd_rawmidi_status32)
64 struct snd_rawmidi_status64 {
66 u8 rsvd[4]; /* alignment */
67 s64 tstamp_sec; /* Timestamp */
69 size_t avail; /* available bytes */
70 size_t xruns; /* count of overruns since last status (in bytes) */
71 unsigned char reserved[16]; /* reserved for future use */
74 #define SNDRV_RAWMIDI_IOCTL_STATUS64 _IOWR('W', 0x20, struct snd_rawmidi_status64)
76 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
78 struct snd_rawmidi *rawmidi;
80 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
81 if (rawmidi->card == card && rawmidi->device == device)
86 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
88 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
90 return SNDRV_RAWMIDI_LFLG_OUTPUT;
92 return SNDRV_RAWMIDI_LFLG_INPUT;
94 return SNDRV_RAWMIDI_LFLG_OPEN;
98 static inline bool __snd_rawmidi_ready(struct snd_rawmidi_runtime *runtime)
100 return runtime->avail >= runtime->avail_min;
103 static bool snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
105 struct snd_rawmidi_runtime *runtime = substream->runtime;
109 spin_lock_irqsave(&runtime->lock, flags);
110 ready = __snd_rawmidi_ready(runtime);
111 spin_unlock_irqrestore(&runtime->lock, flags);
115 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
118 struct snd_rawmidi_runtime *runtime = substream->runtime;
120 return runtime->avail >= runtime->avail_min &&
121 (!substream->append || runtime->avail >= count);
124 static void snd_rawmidi_input_event_work(struct work_struct *work)
126 struct snd_rawmidi_runtime *runtime =
127 container_of(work, struct snd_rawmidi_runtime, event_work);
130 runtime->event(runtime->substream);
133 /* buffer refcount management: call with runtime->lock held */
134 static inline void snd_rawmidi_buffer_ref(struct snd_rawmidi_runtime *runtime)
136 runtime->buffer_ref++;
139 static inline void snd_rawmidi_buffer_unref(struct snd_rawmidi_runtime *runtime)
141 runtime->buffer_ref--;
144 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
146 struct snd_rawmidi_runtime *runtime;
148 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
151 runtime->substream = substream;
152 spin_lock_init(&runtime->lock);
153 init_waitqueue_head(&runtime->sleep);
154 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
155 runtime->event = NULL;
156 runtime->buffer_size = PAGE_SIZE;
157 runtime->avail_min = 1;
158 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
161 runtime->avail = runtime->buffer_size;
162 runtime->buffer = kvzalloc(runtime->buffer_size, GFP_KERNEL);
163 if (!runtime->buffer) {
167 runtime->appl_ptr = runtime->hw_ptr = 0;
168 substream->runtime = runtime;
172 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
174 struct snd_rawmidi_runtime *runtime = substream->runtime;
176 kvfree(runtime->buffer);
178 substream->runtime = NULL;
182 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
184 if (!substream->opened)
186 substream->ops->trigger(substream, up);
189 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
191 if (!substream->opened)
193 substream->ops->trigger(substream, up);
195 cancel_work_sync(&substream->runtime->event_work);
198 static void __reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
202 runtime->appl_ptr = runtime->hw_ptr = 0;
203 runtime->avail = is_input ? 0 : runtime->buffer_size;
206 static void reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
211 spin_lock_irqsave(&runtime->lock, flags);
212 __reset_runtime_ptrs(runtime, is_input);
213 spin_unlock_irqrestore(&runtime->lock, flags);
216 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
218 snd_rawmidi_output_trigger(substream, 0);
219 reset_runtime_ptrs(substream->runtime, false);
222 EXPORT_SYMBOL(snd_rawmidi_drop_output);
224 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
228 struct snd_rawmidi_runtime *runtime = substream->runtime;
232 timeout = wait_event_interruptible_timeout(runtime->sleep,
233 (runtime->avail >= runtime->buffer_size),
235 if (signal_pending(current))
237 if (runtime->avail < runtime->buffer_size && !timeout) {
238 rmidi_warn(substream->rmidi,
239 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
240 (long)runtime->avail, (long)runtime->buffer_size);
244 if (err != -ERESTARTSYS) {
245 /* we need wait a while to make sure that Tx FIFOs are empty */
246 if (substream->ops->drain)
247 substream->ops->drain(substream);
250 snd_rawmidi_drop_output(substream);
254 EXPORT_SYMBOL(snd_rawmidi_drain_output);
256 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
258 snd_rawmidi_input_trigger(substream, 0);
259 reset_runtime_ptrs(substream->runtime, true);
262 EXPORT_SYMBOL(snd_rawmidi_drain_input);
264 /* look for an available substream for the given stream direction;
265 * if a specific subdevice is given, try to assign it
267 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
268 int stream, int mode,
269 struct snd_rawmidi_substream **sub_ret)
271 struct snd_rawmidi_substream *substream;
272 struct snd_rawmidi_str *s = &rmidi->streams[stream];
273 static const unsigned int info_flags[2] = {
274 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
275 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
278 if (!(rmidi->info_flags & info_flags[stream]))
280 if (subdevice >= 0 && subdevice >= s->substream_count)
283 list_for_each_entry(substream, &s->substreams, list) {
284 if (substream->opened) {
285 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
286 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
290 if (subdevice < 0 || subdevice == substream->number) {
291 *sub_ret = substream;
298 /* open and do ref-counting for the given substream */
299 static int open_substream(struct snd_rawmidi *rmidi,
300 struct snd_rawmidi_substream *substream,
305 if (substream->use_count == 0) {
306 err = snd_rawmidi_runtime_create(substream);
309 err = substream->ops->open(substream);
311 snd_rawmidi_runtime_free(substream);
314 substream->opened = 1;
315 substream->active_sensing = 0;
316 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
317 substream->append = 1;
318 substream->pid = get_pid(task_pid(current));
319 rmidi->streams[substream->stream].substream_opened++;
321 substream->use_count++;
325 static void close_substream(struct snd_rawmidi *rmidi,
326 struct snd_rawmidi_substream *substream,
329 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
330 struct snd_rawmidi_file *rfile)
332 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
335 rfile->input = rfile->output = NULL;
336 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
337 err = assign_substream(rmidi, subdevice,
338 SNDRV_RAWMIDI_STREAM_INPUT,
343 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
344 err = assign_substream(rmidi, subdevice,
345 SNDRV_RAWMIDI_STREAM_OUTPUT,
352 err = open_substream(rmidi, sinput, mode);
357 err = open_substream(rmidi, soutput, mode);
360 close_substream(rmidi, sinput, 0);
365 rfile->rmidi = rmidi;
366 rfile->input = sinput;
367 rfile->output = soutput;
371 /* called from sound/core/seq/seq_midi.c */
372 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
373 int mode, struct snd_rawmidi_file *rfile)
375 struct snd_rawmidi *rmidi;
378 if (snd_BUG_ON(!rfile))
381 mutex_lock(®ister_mutex);
382 rmidi = snd_rawmidi_search(card, device);
385 else if (!try_module_get(rmidi->card->module))
387 mutex_unlock(®ister_mutex);
391 mutex_lock(&rmidi->open_mutex);
392 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
393 mutex_unlock(&rmidi->open_mutex);
395 module_put(rmidi->card->module);
398 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
400 static int snd_rawmidi_open(struct inode *inode, struct file *file)
402 int maj = imajor(inode);
403 struct snd_card *card;
405 unsigned short fflags;
407 struct snd_rawmidi *rmidi;
408 struct snd_rawmidi_file *rawmidi_file = NULL;
409 wait_queue_entry_t wait;
411 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
412 return -EINVAL; /* invalid combination */
414 err = stream_open(inode, file);
418 if (maj == snd_major) {
419 rmidi = snd_lookup_minor_data(iminor(inode),
420 SNDRV_DEVICE_TYPE_RAWMIDI);
421 #ifdef CONFIG_SND_OSSEMUL
422 } else if (maj == SOUND_MAJOR) {
423 rmidi = snd_lookup_oss_minor_data(iminor(inode),
424 SNDRV_OSS_DEVICE_TYPE_MIDI);
432 if (!try_module_get(rmidi->card->module)) {
433 snd_card_unref(rmidi->card);
437 mutex_lock(&rmidi->open_mutex);
439 err = snd_card_file_add(card, file);
442 fflags = snd_rawmidi_file_flags(file);
443 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
444 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
445 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
446 if (rawmidi_file == NULL) {
450 init_waitqueue_entry(&wait, current);
451 add_wait_queue(&rmidi->open_wait, &wait);
453 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
454 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
457 if (err == -EAGAIN) {
458 if (file->f_flags & O_NONBLOCK) {
464 set_current_state(TASK_INTERRUPTIBLE);
465 mutex_unlock(&rmidi->open_mutex);
467 mutex_lock(&rmidi->open_mutex);
468 if (rmidi->card->shutdown) {
472 if (signal_pending(current)) {
477 remove_wait_queue(&rmidi->open_wait, &wait);
482 #ifdef CONFIG_SND_OSSEMUL
483 if (rawmidi_file->input && rawmidi_file->input->runtime)
484 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
485 if (rawmidi_file->output && rawmidi_file->output->runtime)
486 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
488 file->private_data = rawmidi_file;
489 mutex_unlock(&rmidi->open_mutex);
490 snd_card_unref(rmidi->card);
494 snd_card_file_remove(card, file);
496 mutex_unlock(&rmidi->open_mutex);
497 module_put(rmidi->card->module);
498 snd_card_unref(rmidi->card);
502 static void close_substream(struct snd_rawmidi *rmidi,
503 struct snd_rawmidi_substream *substream,
506 if (--substream->use_count)
510 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
511 snd_rawmidi_input_trigger(substream, 0);
513 if (substream->active_sensing) {
514 unsigned char buf = 0xfe;
515 /* sending single active sensing message
516 * to shut the device up
518 snd_rawmidi_kernel_write(substream, &buf, 1);
520 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
521 snd_rawmidi_output_trigger(substream, 0);
524 substream->ops->close(substream);
525 if (substream->runtime->private_free)
526 substream->runtime->private_free(substream);
527 snd_rawmidi_runtime_free(substream);
528 substream->opened = 0;
529 substream->append = 0;
530 put_pid(substream->pid);
531 substream->pid = NULL;
532 rmidi->streams[substream->stream].substream_opened--;
535 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
537 struct snd_rawmidi *rmidi;
539 rmidi = rfile->rmidi;
540 mutex_lock(&rmidi->open_mutex);
542 close_substream(rmidi, rfile->input, 1);
546 close_substream(rmidi, rfile->output, 1);
547 rfile->output = NULL;
550 mutex_unlock(&rmidi->open_mutex);
551 wake_up(&rmidi->open_wait);
554 /* called from sound/core/seq/seq_midi.c */
555 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
557 struct snd_rawmidi *rmidi;
559 if (snd_BUG_ON(!rfile))
562 rmidi = rfile->rmidi;
563 rawmidi_release_priv(rfile);
564 module_put(rmidi->card->module);
567 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
569 static int snd_rawmidi_release(struct inode *inode, struct file *file)
571 struct snd_rawmidi_file *rfile;
572 struct snd_rawmidi *rmidi;
573 struct module *module;
575 rfile = file->private_data;
576 rmidi = rfile->rmidi;
577 rawmidi_release_priv(rfile);
579 module = rmidi->card->module;
580 snd_card_file_remove(rmidi->card, file);
585 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
586 struct snd_rawmidi_info *info)
588 struct snd_rawmidi *rmidi;
590 if (substream == NULL)
592 rmidi = substream->rmidi;
593 memset(info, 0, sizeof(*info));
594 info->card = rmidi->card->number;
595 info->device = rmidi->device;
596 info->subdevice = substream->number;
597 info->stream = substream->stream;
598 info->flags = rmidi->info_flags;
599 strcpy(info->id, rmidi->id);
600 strcpy(info->name, rmidi->name);
601 strcpy(info->subname, substream->name);
602 info->subdevices_count = substream->pstr->substream_count;
603 info->subdevices_avail = (substream->pstr->substream_count -
604 substream->pstr->substream_opened);
608 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
609 struct snd_rawmidi_info __user *_info)
611 struct snd_rawmidi_info info;
614 err = snd_rawmidi_info(substream, &info);
617 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
622 static int __snd_rawmidi_info_select(struct snd_card *card,
623 struct snd_rawmidi_info *info)
625 struct snd_rawmidi *rmidi;
626 struct snd_rawmidi_str *pstr;
627 struct snd_rawmidi_substream *substream;
629 rmidi = snd_rawmidi_search(card, info->device);
632 if (info->stream < 0 || info->stream > 1)
634 info->stream = array_index_nospec(info->stream, 2);
635 pstr = &rmidi->streams[info->stream];
636 if (pstr->substream_count == 0)
638 if (info->subdevice >= pstr->substream_count)
640 list_for_each_entry(substream, &pstr->substreams, list) {
641 if ((unsigned int)substream->number == info->subdevice)
642 return snd_rawmidi_info(substream, info);
647 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
651 mutex_lock(®ister_mutex);
652 ret = __snd_rawmidi_info_select(card, info);
653 mutex_unlock(®ister_mutex);
656 EXPORT_SYMBOL(snd_rawmidi_info_select);
658 static int snd_rawmidi_info_select_user(struct snd_card *card,
659 struct snd_rawmidi_info __user *_info)
662 struct snd_rawmidi_info info;
664 if (get_user(info.device, &_info->device))
666 if (get_user(info.stream, &_info->stream))
668 if (get_user(info.subdevice, &_info->subdevice))
670 err = snd_rawmidi_info_select(card, &info);
673 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
678 static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime,
679 struct snd_rawmidi_params *params,
682 char *newbuf, *oldbuf;
683 unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK;
685 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L)
687 if (framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP && (params->buffer_size & 0x1f) != 0)
689 if (params->avail_min < 1 || params->avail_min > params->buffer_size)
691 if (params->buffer_size != runtime->buffer_size) {
692 newbuf = kvzalloc(params->buffer_size, GFP_KERNEL);
695 spin_lock_irq(&runtime->lock);
696 if (runtime->buffer_ref) {
697 spin_unlock_irq(&runtime->lock);
701 oldbuf = runtime->buffer;
702 runtime->buffer = newbuf;
703 runtime->buffer_size = params->buffer_size;
704 __reset_runtime_ptrs(runtime, is_input);
705 spin_unlock_irq(&runtime->lock);
708 runtime->avail_min = params->avail_min;
712 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
713 struct snd_rawmidi_params *params)
715 if (substream->append && substream->use_count > 1)
717 snd_rawmidi_drain_output(substream);
718 substream->active_sensing = !params->no_active_sensing;
719 return resize_runtime_buffer(substream->runtime, params, false);
721 EXPORT_SYMBOL(snd_rawmidi_output_params);
723 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
724 struct snd_rawmidi_params *params)
726 unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK;
727 unsigned int clock_type = params->mode & SNDRV_RAWMIDI_MODE_CLOCK_MASK;
730 if (framing == SNDRV_RAWMIDI_MODE_FRAMING_NONE && clock_type != SNDRV_RAWMIDI_MODE_CLOCK_NONE)
732 else if (clock_type > SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC_RAW)
734 if (framing > SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP)
736 snd_rawmidi_drain_input(substream);
737 err = resize_runtime_buffer(substream->runtime, params, true);
741 substream->framing = framing;
742 substream->clock_type = clock_type;
745 EXPORT_SYMBOL(snd_rawmidi_input_params);
747 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
748 struct snd_rawmidi_status64 *status)
750 struct snd_rawmidi_runtime *runtime = substream->runtime;
752 memset(status, 0, sizeof(*status));
753 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
754 spin_lock_irq(&runtime->lock);
755 status->avail = runtime->avail;
756 spin_unlock_irq(&runtime->lock);
760 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
761 struct snd_rawmidi_status64 *status)
763 struct snd_rawmidi_runtime *runtime = substream->runtime;
765 memset(status, 0, sizeof(*status));
766 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
767 spin_lock_irq(&runtime->lock);
768 status->avail = runtime->avail;
769 status->xruns = runtime->xruns;
771 spin_unlock_irq(&runtime->lock);
775 static int snd_rawmidi_ioctl_status32(struct snd_rawmidi_file *rfile,
776 struct snd_rawmidi_status32 __user *argp)
779 struct snd_rawmidi_status32 __user *status = argp;
780 struct snd_rawmidi_status32 status32;
781 struct snd_rawmidi_status64 status64;
783 if (copy_from_user(&status32, argp,
784 sizeof(struct snd_rawmidi_status32)))
787 switch (status32.stream) {
788 case SNDRV_RAWMIDI_STREAM_OUTPUT:
789 if (rfile->output == NULL)
791 err = snd_rawmidi_output_status(rfile->output, &status64);
793 case SNDRV_RAWMIDI_STREAM_INPUT:
794 if (rfile->input == NULL)
796 err = snd_rawmidi_input_status(rfile->input, &status64);
804 status32 = (struct snd_rawmidi_status32) {
805 .stream = status64.stream,
806 .tstamp_sec = status64.tstamp_sec,
807 .tstamp_nsec = status64.tstamp_nsec,
808 .avail = status64.avail,
809 .xruns = status64.xruns,
812 if (copy_to_user(status, &status32, sizeof(*status)))
818 static int snd_rawmidi_ioctl_status64(struct snd_rawmidi_file *rfile,
819 struct snd_rawmidi_status64 __user *argp)
822 struct snd_rawmidi_status64 status;
824 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status64)))
827 switch (status.stream) {
828 case SNDRV_RAWMIDI_STREAM_OUTPUT:
829 if (rfile->output == NULL)
831 err = snd_rawmidi_output_status(rfile->output, &status);
833 case SNDRV_RAWMIDI_STREAM_INPUT:
834 if (rfile->input == NULL)
836 err = snd_rawmidi_input_status(rfile->input, &status);
843 if (copy_to_user(argp, &status,
844 sizeof(struct snd_rawmidi_status64)))
849 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
851 struct snd_rawmidi_file *rfile;
852 void __user *argp = (void __user *)arg;
854 rfile = file->private_data;
855 if (((cmd >> 8) & 0xff) != 'W')
858 case SNDRV_RAWMIDI_IOCTL_PVERSION:
859 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
860 case SNDRV_RAWMIDI_IOCTL_INFO:
863 struct snd_rawmidi_info __user *info = argp;
865 if (get_user(stream, &info->stream))
868 case SNDRV_RAWMIDI_STREAM_INPUT:
869 return snd_rawmidi_info_user(rfile->input, info);
870 case SNDRV_RAWMIDI_STREAM_OUTPUT:
871 return snd_rawmidi_info_user(rfile->output, info);
876 case SNDRV_RAWMIDI_IOCTL_USER_PVERSION:
877 if (get_user(rfile->user_pversion, (unsigned int __user *)arg))
881 case SNDRV_RAWMIDI_IOCTL_PARAMS:
883 struct snd_rawmidi_params params;
885 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
887 if (rfile->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 2)) {
889 memset(params.reserved, 0, sizeof(params.reserved));
891 switch (params.stream) {
892 case SNDRV_RAWMIDI_STREAM_OUTPUT:
893 if (rfile->output == NULL)
895 return snd_rawmidi_output_params(rfile->output, ¶ms);
896 case SNDRV_RAWMIDI_STREAM_INPUT:
897 if (rfile->input == NULL)
899 return snd_rawmidi_input_params(rfile->input, ¶ms);
904 case SNDRV_RAWMIDI_IOCTL_STATUS32:
905 return snd_rawmidi_ioctl_status32(rfile, argp);
906 case SNDRV_RAWMIDI_IOCTL_STATUS64:
907 return snd_rawmidi_ioctl_status64(rfile, argp);
908 case SNDRV_RAWMIDI_IOCTL_DROP:
912 if (get_user(val, (int __user *) argp))
915 case SNDRV_RAWMIDI_STREAM_OUTPUT:
916 if (rfile->output == NULL)
918 return snd_rawmidi_drop_output(rfile->output);
923 case SNDRV_RAWMIDI_IOCTL_DRAIN:
927 if (get_user(val, (int __user *) argp))
930 case SNDRV_RAWMIDI_STREAM_OUTPUT:
931 if (rfile->output == NULL)
933 return snd_rawmidi_drain_output(rfile->output);
934 case SNDRV_RAWMIDI_STREAM_INPUT:
935 if (rfile->input == NULL)
937 return snd_rawmidi_drain_input(rfile->input);
943 rmidi_dbg(rfile->rmidi,
944 "rawmidi: unknown command = 0x%x\n", cmd);
949 static int snd_rawmidi_control_ioctl(struct snd_card *card,
950 struct snd_ctl_file *control,
954 void __user *argp = (void __user *)arg;
957 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
961 if (get_user(device, (int __user *)argp))
963 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
964 device = SNDRV_RAWMIDI_DEVICES - 1;
965 mutex_lock(®ister_mutex);
966 device = device < 0 ? 0 : device + 1;
967 while (device < SNDRV_RAWMIDI_DEVICES) {
968 if (snd_rawmidi_search(card, device))
972 if (device == SNDRV_RAWMIDI_DEVICES)
974 mutex_unlock(®ister_mutex);
975 if (put_user(device, (int __user *)argp))
979 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
983 if (get_user(val, (int __user *)argp))
985 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
988 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
989 return snd_rawmidi_info_select_user(card, argp);
994 static int receive_with_tstamp_framing(struct snd_rawmidi_substream *substream,
995 const unsigned char *buffer, int src_count, const struct timespec64 *tstamp)
997 struct snd_rawmidi_runtime *runtime = substream->runtime;
998 struct snd_rawmidi_framing_tstamp *dest_ptr;
999 struct snd_rawmidi_framing_tstamp frame = { .tv_sec = tstamp->tv_sec, .tv_nsec = tstamp->tv_nsec };
1000 int dest_frames = 0;
1001 int orig_count = src_count;
1002 int frame_size = sizeof(struct snd_rawmidi_framing_tstamp);
1004 BUILD_BUG_ON(frame_size != 0x20);
1005 if (snd_BUG_ON((runtime->hw_ptr & 0x1f) != 0))
1008 while (src_count > 0) {
1009 if ((int)(runtime->buffer_size - runtime->avail) < frame_size) {
1010 runtime->xruns += src_count;
1013 if (src_count >= SNDRV_RAWMIDI_FRAMING_DATA_LENGTH)
1014 frame.length = SNDRV_RAWMIDI_FRAMING_DATA_LENGTH;
1016 frame.length = src_count;
1017 memset(frame.data, 0, SNDRV_RAWMIDI_FRAMING_DATA_LENGTH);
1019 memcpy(frame.data, buffer, frame.length);
1020 buffer += frame.length;
1021 src_count -= frame.length;
1022 dest_ptr = (struct snd_rawmidi_framing_tstamp *) (runtime->buffer + runtime->hw_ptr);
1024 runtime->avail += frame_size;
1025 runtime->hw_ptr += frame_size;
1026 runtime->hw_ptr %= runtime->buffer_size;
1029 return orig_count - src_count;
1032 static struct timespec64 get_framing_tstamp(struct snd_rawmidi_substream *substream)
1034 struct timespec64 ts64 = {0, 0};
1036 switch (substream->clock_type) {
1037 case SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC_RAW:
1038 ktime_get_raw_ts64(&ts64);
1040 case SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC:
1041 ktime_get_ts64(&ts64);
1043 case SNDRV_RAWMIDI_MODE_CLOCK_REALTIME:
1044 ktime_get_real_ts64(&ts64);
1051 * snd_rawmidi_receive - receive the input data from the device
1052 * @substream: the rawmidi substream
1053 * @buffer: the buffer pointer
1054 * @count: the data size to read
1056 * Reads the data from the internal buffer.
1058 * Return: The size of read data, or a negative error code on failure.
1060 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
1061 const unsigned char *buffer, int count)
1063 unsigned long flags;
1064 struct timespec64 ts64 = get_framing_tstamp(substream);
1065 int result = 0, count1;
1066 struct snd_rawmidi_runtime *runtime = substream->runtime;
1068 if (!substream->opened)
1070 if (runtime->buffer == NULL) {
1071 rmidi_dbg(substream->rmidi,
1072 "snd_rawmidi_receive: input is not active!!!\n");
1076 spin_lock_irqsave(&runtime->lock, flags);
1077 if (substream->framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) {
1078 result = receive_with_tstamp_framing(substream, buffer, count, &ts64);
1079 } else if (count == 1) { /* special case, faster code */
1081 if (runtime->avail < runtime->buffer_size) {
1082 runtime->buffer[runtime->hw_ptr++] = buffer[0];
1083 runtime->hw_ptr %= runtime->buffer_size;
1090 substream->bytes += count;
1091 count1 = runtime->buffer_size - runtime->hw_ptr;
1094 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1095 count1 = runtime->buffer_size - runtime->avail;
1096 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
1097 runtime->hw_ptr += count1;
1098 runtime->hw_ptr %= runtime->buffer_size;
1099 runtime->avail += count1;
1105 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
1106 count1 = runtime->buffer_size - runtime->avail;
1107 runtime->xruns += count - count1;
1110 memcpy(runtime->buffer, buffer, count1);
1111 runtime->hw_ptr = count1;
1112 runtime->avail += count1;
1119 schedule_work(&runtime->event_work);
1120 else if (__snd_rawmidi_ready(runtime))
1121 wake_up(&runtime->sleep);
1123 spin_unlock_irqrestore(&runtime->lock, flags);
1126 EXPORT_SYMBOL(snd_rawmidi_receive);
1128 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
1129 unsigned char __user *userbuf,
1130 unsigned char *kernelbuf, long count)
1132 unsigned long flags;
1133 long result = 0, count1;
1134 struct snd_rawmidi_runtime *runtime = substream->runtime;
1135 unsigned long appl_ptr;
1138 spin_lock_irqsave(&runtime->lock, flags);
1139 snd_rawmidi_buffer_ref(runtime);
1140 while (count > 0 && runtime->avail) {
1141 count1 = runtime->buffer_size - runtime->appl_ptr;
1144 if (count1 > (int)runtime->avail)
1145 count1 = runtime->avail;
1147 /* update runtime->appl_ptr before unlocking for userbuf */
1148 appl_ptr = runtime->appl_ptr;
1149 runtime->appl_ptr += count1;
1150 runtime->appl_ptr %= runtime->buffer_size;
1151 runtime->avail -= count1;
1154 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
1156 spin_unlock_irqrestore(&runtime->lock, flags);
1157 if (copy_to_user(userbuf + result,
1158 runtime->buffer + appl_ptr, count1))
1160 spin_lock_irqsave(&runtime->lock, flags);
1168 snd_rawmidi_buffer_unref(runtime);
1169 spin_unlock_irqrestore(&runtime->lock, flags);
1170 return result > 0 ? result : err;
1173 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
1174 unsigned char *buf, long count)
1176 snd_rawmidi_input_trigger(substream, 1);
1177 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
1179 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1181 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1186 struct snd_rawmidi_file *rfile;
1187 struct snd_rawmidi_substream *substream;
1188 struct snd_rawmidi_runtime *runtime;
1190 rfile = file->private_data;
1191 substream = rfile->input;
1192 if (substream == NULL)
1194 runtime = substream->runtime;
1195 snd_rawmidi_input_trigger(substream, 1);
1198 spin_lock_irq(&runtime->lock);
1199 while (!__snd_rawmidi_ready(runtime)) {
1200 wait_queue_entry_t wait;
1202 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1203 spin_unlock_irq(&runtime->lock);
1204 return result > 0 ? result : -EAGAIN;
1206 init_waitqueue_entry(&wait, current);
1207 add_wait_queue(&runtime->sleep, &wait);
1208 set_current_state(TASK_INTERRUPTIBLE);
1209 spin_unlock_irq(&runtime->lock);
1211 remove_wait_queue(&runtime->sleep, &wait);
1212 if (rfile->rmidi->card->shutdown)
1214 if (signal_pending(current))
1215 return result > 0 ? result : -ERESTARTSYS;
1216 spin_lock_irq(&runtime->lock);
1217 if (!runtime->avail) {
1218 spin_unlock_irq(&runtime->lock);
1219 return result > 0 ? result : -EIO;
1222 spin_unlock_irq(&runtime->lock);
1223 count1 = snd_rawmidi_kernel_read1(substream,
1224 (unsigned char __user *)buf,
1228 return result > 0 ? result : count1;
1237 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1238 * @substream: the rawmidi substream
1240 * Return: 1 if the internal output buffer is empty, 0 if not.
1242 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1244 struct snd_rawmidi_runtime *runtime = substream->runtime;
1246 unsigned long flags;
1248 if (runtime->buffer == NULL) {
1249 rmidi_dbg(substream->rmidi,
1250 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1253 spin_lock_irqsave(&runtime->lock, flags);
1254 result = runtime->avail >= runtime->buffer_size;
1255 spin_unlock_irqrestore(&runtime->lock, flags);
1258 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1261 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1262 * @substream: the rawmidi substream
1263 * @buffer: the buffer pointer
1264 * @count: data size to transfer
1266 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1268 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1269 unsigned char *buffer, int count)
1272 struct snd_rawmidi_runtime *runtime = substream->runtime;
1274 if (runtime->buffer == NULL) {
1275 rmidi_dbg(substream->rmidi,
1276 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1280 if (runtime->avail >= runtime->buffer_size) {
1281 /* warning: lowlevel layer MUST trigger down the hardware */
1284 if (count == 1) { /* special case, faster code */
1285 *buffer = runtime->buffer[runtime->hw_ptr];
1288 count1 = runtime->buffer_size - runtime->hw_ptr;
1291 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1292 count1 = runtime->buffer_size - runtime->avail;
1293 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1297 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1298 count = runtime->buffer_size - runtime->avail - count1;
1299 memcpy(buffer + count1, runtime->buffer, count);
1306 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1309 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1310 * @substream: the rawmidi substream
1311 * @buffer: the buffer pointer
1312 * @count: data size to transfer
1314 * Copies data from the internal output buffer to the given buffer.
1316 * Call this in the interrupt handler when the midi output is ready,
1317 * and call snd_rawmidi_transmit_ack() after the transmission is
1320 * Return: The size of copied data, or a negative error code on failure.
1322 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1323 unsigned char *buffer, int count)
1325 struct snd_rawmidi_runtime *runtime = substream->runtime;
1327 unsigned long flags;
1329 spin_lock_irqsave(&runtime->lock, flags);
1330 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1331 spin_unlock_irqrestore(&runtime->lock, flags);
1334 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1337 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1338 * @substream: the rawmidi substream
1339 * @count: the transferred count
1341 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1343 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1345 struct snd_rawmidi_runtime *runtime = substream->runtime;
1347 if (runtime->buffer == NULL) {
1348 rmidi_dbg(substream->rmidi,
1349 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1352 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1353 runtime->hw_ptr += count;
1354 runtime->hw_ptr %= runtime->buffer_size;
1355 runtime->avail += count;
1356 substream->bytes += count;
1358 if (runtime->drain || __snd_rawmidi_ready(runtime))
1359 wake_up(&runtime->sleep);
1363 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1366 * snd_rawmidi_transmit_ack - acknowledge the transmission
1367 * @substream: the rawmidi substream
1368 * @count: the transferred count
1370 * Advances the hardware pointer for the internal output buffer with
1371 * the given size and updates the condition.
1372 * Call after the transmission is finished.
1374 * Return: The advanced size if successful, or a negative error code on failure.
1376 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1378 struct snd_rawmidi_runtime *runtime = substream->runtime;
1380 unsigned long flags;
1382 spin_lock_irqsave(&runtime->lock, flags);
1383 result = __snd_rawmidi_transmit_ack(substream, count);
1384 spin_unlock_irqrestore(&runtime->lock, flags);
1387 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1390 * snd_rawmidi_transmit - copy from the buffer to the device
1391 * @substream: the rawmidi substream
1392 * @buffer: the buffer pointer
1393 * @count: the data size to transfer
1395 * Copies data from the buffer to the device and advances the pointer.
1397 * Return: The copied size if successful, or a negative error code on failure.
1399 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1400 unsigned char *buffer, int count)
1402 struct snd_rawmidi_runtime *runtime = substream->runtime;
1404 unsigned long flags;
1406 spin_lock_irqsave(&runtime->lock, flags);
1407 if (!substream->opened)
1410 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1414 result = __snd_rawmidi_transmit_ack(substream, count);
1416 spin_unlock_irqrestore(&runtime->lock, flags);
1419 EXPORT_SYMBOL(snd_rawmidi_transmit);
1422 * snd_rawmidi_proceed - Discard the all pending bytes and proceed
1423 * @substream: rawmidi substream
1425 * Return: the number of discarded bytes
1427 int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream)
1429 struct snd_rawmidi_runtime *runtime = substream->runtime;
1430 unsigned long flags;
1433 spin_lock_irqsave(&runtime->lock, flags);
1434 if (runtime->avail < runtime->buffer_size) {
1435 count = runtime->buffer_size - runtime->avail;
1436 __snd_rawmidi_transmit_ack(substream, count);
1438 spin_unlock_irqrestore(&runtime->lock, flags);
1441 EXPORT_SYMBOL(snd_rawmidi_proceed);
1443 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1444 const unsigned char __user *userbuf,
1445 const unsigned char *kernelbuf,
1448 unsigned long flags;
1449 long count1, result;
1450 struct snd_rawmidi_runtime *runtime = substream->runtime;
1451 unsigned long appl_ptr;
1453 if (!kernelbuf && !userbuf)
1455 if (snd_BUG_ON(!runtime->buffer))
1459 spin_lock_irqsave(&runtime->lock, flags);
1460 if (substream->append) {
1461 if ((long)runtime->avail < count) {
1462 spin_unlock_irqrestore(&runtime->lock, flags);
1466 snd_rawmidi_buffer_ref(runtime);
1467 while (count > 0 && runtime->avail > 0) {
1468 count1 = runtime->buffer_size - runtime->appl_ptr;
1471 if (count1 > (long)runtime->avail)
1472 count1 = runtime->avail;
1474 /* update runtime->appl_ptr before unlocking for userbuf */
1475 appl_ptr = runtime->appl_ptr;
1476 runtime->appl_ptr += count1;
1477 runtime->appl_ptr %= runtime->buffer_size;
1478 runtime->avail -= count1;
1481 memcpy(runtime->buffer + appl_ptr,
1482 kernelbuf + result, count1);
1484 spin_unlock_irqrestore(&runtime->lock, flags);
1485 if (copy_from_user(runtime->buffer + appl_ptr,
1486 userbuf + result, count1)) {
1487 spin_lock_irqsave(&runtime->lock, flags);
1488 result = result > 0 ? result : -EFAULT;
1491 spin_lock_irqsave(&runtime->lock, flags);
1497 count1 = runtime->avail < runtime->buffer_size;
1498 snd_rawmidi_buffer_unref(runtime);
1499 spin_unlock_irqrestore(&runtime->lock, flags);
1501 snd_rawmidi_output_trigger(substream, 1);
1505 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1506 const unsigned char *buf, long count)
1508 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1510 EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1512 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1513 size_t count, loff_t *offset)
1515 long result, timeout;
1517 struct snd_rawmidi_file *rfile;
1518 struct snd_rawmidi_runtime *runtime;
1519 struct snd_rawmidi_substream *substream;
1521 rfile = file->private_data;
1522 substream = rfile->output;
1523 runtime = substream->runtime;
1524 /* we cannot put an atomic message to our buffer */
1525 if (substream->append && count > runtime->buffer_size)
1529 spin_lock_irq(&runtime->lock);
1530 while (!snd_rawmidi_ready_append(substream, count)) {
1531 wait_queue_entry_t wait;
1533 if (file->f_flags & O_NONBLOCK) {
1534 spin_unlock_irq(&runtime->lock);
1535 return result > 0 ? result : -EAGAIN;
1537 init_waitqueue_entry(&wait, current);
1538 add_wait_queue(&runtime->sleep, &wait);
1539 set_current_state(TASK_INTERRUPTIBLE);
1540 spin_unlock_irq(&runtime->lock);
1541 timeout = schedule_timeout(30 * HZ);
1542 remove_wait_queue(&runtime->sleep, &wait);
1543 if (rfile->rmidi->card->shutdown)
1545 if (signal_pending(current))
1546 return result > 0 ? result : -ERESTARTSYS;
1547 spin_lock_irq(&runtime->lock);
1548 if (!runtime->avail && !timeout) {
1549 spin_unlock_irq(&runtime->lock);
1550 return result > 0 ? result : -EIO;
1553 spin_unlock_irq(&runtime->lock);
1554 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1556 return result > 0 ? result : count1;
1559 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1563 if (file->f_flags & O_DSYNC) {
1564 spin_lock_irq(&runtime->lock);
1565 while (runtime->avail != runtime->buffer_size) {
1566 wait_queue_entry_t wait;
1567 unsigned int last_avail = runtime->avail;
1569 init_waitqueue_entry(&wait, current);
1570 add_wait_queue(&runtime->sleep, &wait);
1571 set_current_state(TASK_INTERRUPTIBLE);
1572 spin_unlock_irq(&runtime->lock);
1573 timeout = schedule_timeout(30 * HZ);
1574 remove_wait_queue(&runtime->sleep, &wait);
1575 if (signal_pending(current))
1576 return result > 0 ? result : -ERESTARTSYS;
1577 if (runtime->avail == last_avail && !timeout)
1578 return result > 0 ? result : -EIO;
1579 spin_lock_irq(&runtime->lock);
1581 spin_unlock_irq(&runtime->lock);
1586 static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait)
1588 struct snd_rawmidi_file *rfile;
1589 struct snd_rawmidi_runtime *runtime;
1592 rfile = file->private_data;
1593 if (rfile->input != NULL) {
1594 runtime = rfile->input->runtime;
1595 snd_rawmidi_input_trigger(rfile->input, 1);
1596 poll_wait(file, &runtime->sleep, wait);
1598 if (rfile->output != NULL) {
1599 runtime = rfile->output->runtime;
1600 poll_wait(file, &runtime->sleep, wait);
1603 if (rfile->input != NULL) {
1604 if (snd_rawmidi_ready(rfile->input))
1605 mask |= EPOLLIN | EPOLLRDNORM;
1607 if (rfile->output != NULL) {
1608 if (snd_rawmidi_ready(rfile->output))
1609 mask |= EPOLLOUT | EPOLLWRNORM;
1616 #ifdef CONFIG_COMPAT
1617 #include "rawmidi_compat.c"
1619 #define snd_rawmidi_ioctl_compat NULL
1625 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1626 struct snd_info_buffer *buffer)
1628 struct snd_rawmidi *rmidi;
1629 struct snd_rawmidi_substream *substream;
1630 struct snd_rawmidi_runtime *runtime;
1631 unsigned long buffer_size, avail, xruns;
1632 unsigned int clock_type;
1633 static const char *clock_names[4] = { "none", "realtime", "monotonic", "monotonic raw" };
1635 rmidi = entry->private_data;
1636 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1637 mutex_lock(&rmidi->open_mutex);
1638 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1639 list_for_each_entry(substream,
1640 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1644 " Tx bytes : %lu\n",
1646 (unsigned long) substream->bytes);
1647 if (substream->opened) {
1649 " Owner PID : %d\n",
1650 pid_vnr(substream->pid));
1651 runtime = substream->runtime;
1652 spin_lock_irq(&runtime->lock);
1653 buffer_size = runtime->buffer_size;
1654 avail = runtime->avail;
1655 spin_unlock_irq(&runtime->lock);
1658 " Buffer size : %lu\n"
1660 runtime->oss ? "OSS compatible" : "native",
1661 buffer_size, avail);
1665 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1666 list_for_each_entry(substream,
1667 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1671 " Rx bytes : %lu\n",
1673 (unsigned long) substream->bytes);
1674 if (substream->opened) {
1676 " Owner PID : %d\n",
1677 pid_vnr(substream->pid));
1678 runtime = substream->runtime;
1679 spin_lock_irq(&runtime->lock);
1680 buffer_size = runtime->buffer_size;
1681 avail = runtime->avail;
1682 xruns = runtime->xruns;
1683 spin_unlock_irq(&runtime->lock);
1685 " Buffer size : %lu\n"
1687 " Overruns : %lu\n",
1688 buffer_size, avail, xruns);
1689 if (substream->framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) {
1690 clock_type = substream->clock_type >> SNDRV_RAWMIDI_MODE_CLOCK_SHIFT;
1691 if (!snd_BUG_ON(clock_type >= ARRAY_SIZE(clock_names)))
1693 " Framing : tstamp\n"
1694 " Clock type : %s\n",
1695 clock_names[clock_type]);
1700 mutex_unlock(&rmidi->open_mutex);
1704 * Register functions
1707 static const struct file_operations snd_rawmidi_f_ops = {
1708 .owner = THIS_MODULE,
1709 .read = snd_rawmidi_read,
1710 .write = snd_rawmidi_write,
1711 .open = snd_rawmidi_open,
1712 .release = snd_rawmidi_release,
1713 .llseek = no_llseek,
1714 .poll = snd_rawmidi_poll,
1715 .unlocked_ioctl = snd_rawmidi_ioctl,
1716 .compat_ioctl = snd_rawmidi_ioctl_compat,
1719 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1720 struct snd_rawmidi_str *stream,
1724 struct snd_rawmidi_substream *substream;
1727 for (idx = 0; idx < count; idx++) {
1728 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1731 substream->stream = direction;
1732 substream->number = idx;
1733 substream->rmidi = rmidi;
1734 substream->pstr = stream;
1735 list_add_tail(&substream->list, &stream->substreams);
1736 stream->substream_count++;
1741 static void release_rawmidi_device(struct device *dev)
1743 kfree(container_of(dev, struct snd_rawmidi, dev));
1747 * snd_rawmidi_new - create a rawmidi instance
1748 * @card: the card instance
1749 * @id: the id string
1750 * @device: the device index
1751 * @output_count: the number of output streams
1752 * @input_count: the number of input streams
1753 * @rrawmidi: the pointer to store the new rawmidi instance
1755 * Creates a new rawmidi instance.
1756 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1758 * Return: Zero if successful, or a negative error code on failure.
1760 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1761 int output_count, int input_count,
1762 struct snd_rawmidi **rrawmidi)
1764 struct snd_rawmidi *rmidi;
1766 static const struct snd_device_ops ops = {
1767 .dev_free = snd_rawmidi_dev_free,
1768 .dev_register = snd_rawmidi_dev_register,
1769 .dev_disconnect = snd_rawmidi_dev_disconnect,
1772 if (snd_BUG_ON(!card))
1776 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1780 rmidi->device = device;
1781 mutex_init(&rmidi->open_mutex);
1782 init_waitqueue_head(&rmidi->open_wait);
1783 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1784 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1787 strscpy(rmidi->id, id, sizeof(rmidi->id));
1789 snd_device_initialize(&rmidi->dev, card);
1790 rmidi->dev.release = release_rawmidi_device;
1791 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1793 err = snd_rawmidi_alloc_substreams(rmidi,
1794 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1795 SNDRV_RAWMIDI_STREAM_INPUT,
1799 err = snd_rawmidi_alloc_substreams(rmidi,
1800 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1801 SNDRV_RAWMIDI_STREAM_OUTPUT,
1805 err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops);
1814 snd_rawmidi_free(rmidi);
1817 EXPORT_SYMBOL(snd_rawmidi_new);
1819 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1821 struct snd_rawmidi_substream *substream;
1823 while (!list_empty(&stream->substreams)) {
1824 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1825 list_del(&substream->list);
1830 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1835 snd_info_free_entry(rmidi->proc_entry);
1836 rmidi->proc_entry = NULL;
1837 mutex_lock(®ister_mutex);
1838 if (rmidi->ops && rmidi->ops->dev_unregister)
1839 rmidi->ops->dev_unregister(rmidi);
1840 mutex_unlock(®ister_mutex);
1842 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1843 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1844 if (rmidi->private_free)
1845 rmidi->private_free(rmidi);
1846 put_device(&rmidi->dev);
1850 static int snd_rawmidi_dev_free(struct snd_device *device)
1852 struct snd_rawmidi *rmidi = device->device_data;
1854 return snd_rawmidi_free(rmidi);
1857 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1858 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1860 struct snd_rawmidi *rmidi = device->private_data;
1862 rmidi->seq_dev = NULL;
1866 static int snd_rawmidi_dev_register(struct snd_device *device)
1869 struct snd_info_entry *entry;
1871 struct snd_rawmidi *rmidi = device->device_data;
1873 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1876 mutex_lock(®ister_mutex);
1877 if (snd_rawmidi_search(rmidi->card, rmidi->device))
1880 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1881 mutex_unlock(®ister_mutex);
1885 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1886 rmidi->card, rmidi->device,
1887 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
1889 rmidi_err(rmidi, "unable to register\n");
1892 if (rmidi->ops && rmidi->ops->dev_register) {
1893 err = rmidi->ops->dev_register(rmidi);
1895 goto error_unregister;
1897 #ifdef CONFIG_SND_OSSEMUL
1899 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1900 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1901 rmidi->card, 0, &snd_rawmidi_f_ops,
1904 "unable to register OSS rawmidi device %i:%i\n",
1905 rmidi->card->number, 0);
1908 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1909 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1913 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1914 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1915 rmidi->card, 1, &snd_rawmidi_f_ops,
1918 "unable to register OSS rawmidi device %i:%i\n",
1919 rmidi->card->number, 1);
1924 #endif /* CONFIG_SND_OSSEMUL */
1925 sprintf(name, "midi%d", rmidi->device);
1926 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1928 entry->private_data = rmidi;
1929 entry->c.text.read = snd_rawmidi_proc_info_read;
1930 if (snd_info_register(entry) < 0) {
1931 snd_info_free_entry(entry);
1935 rmidi->proc_entry = entry;
1936 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1937 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1938 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1939 rmidi->seq_dev->private_data = rmidi;
1940 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1941 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1942 snd_device_register(rmidi->card, rmidi->seq_dev);
1949 snd_unregister_device(&rmidi->dev);
1951 mutex_lock(®ister_mutex);
1952 list_del(&rmidi->list);
1953 mutex_unlock(®ister_mutex);
1957 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1959 struct snd_rawmidi *rmidi = device->device_data;
1962 mutex_lock(®ister_mutex);
1963 mutex_lock(&rmidi->open_mutex);
1964 wake_up(&rmidi->open_wait);
1965 list_del_init(&rmidi->list);
1966 for (dir = 0; dir < 2; dir++) {
1967 struct snd_rawmidi_substream *s;
1969 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1971 wake_up(&s->runtime->sleep);
1975 #ifdef CONFIG_SND_OSSEMUL
1976 if (rmidi->ossreg) {
1977 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1978 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1979 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1980 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1983 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1984 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1987 #endif /* CONFIG_SND_OSSEMUL */
1988 snd_unregister_device(&rmidi->dev);
1989 mutex_unlock(&rmidi->open_mutex);
1990 mutex_unlock(®ister_mutex);
1995 * snd_rawmidi_set_ops - set the rawmidi operators
1996 * @rmidi: the rawmidi instance
1997 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1998 * @ops: the operator table
2000 * Sets the rawmidi operators for the given stream direction.
2002 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
2003 const struct snd_rawmidi_ops *ops)
2005 struct snd_rawmidi_substream *substream;
2007 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
2008 substream->ops = ops;
2010 EXPORT_SYMBOL(snd_rawmidi_set_ops);
2016 static int __init alsa_rawmidi_init(void)
2019 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
2020 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
2021 #ifdef CONFIG_SND_OSSEMUL
2023 /* check device map table */
2024 for (i = 0; i < SNDRV_CARDS; i++) {
2025 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
2026 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
2030 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
2031 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
2037 #endif /* CONFIG_SND_OSSEMUL */
2041 static void __exit alsa_rawmidi_exit(void)
2043 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
2044 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
2047 module_init(alsa_rawmidi_init)
2048 module_exit(alsa_rawmidi_exit)