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 rawmidi_file->user_pversion = 0;
451 init_waitqueue_entry(&wait, current);
452 add_wait_queue(&rmidi->open_wait, &wait);
454 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
455 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
458 if (err == -EAGAIN) {
459 if (file->f_flags & O_NONBLOCK) {
465 set_current_state(TASK_INTERRUPTIBLE);
466 mutex_unlock(&rmidi->open_mutex);
468 mutex_lock(&rmidi->open_mutex);
469 if (rmidi->card->shutdown) {
473 if (signal_pending(current)) {
478 remove_wait_queue(&rmidi->open_wait, &wait);
483 #ifdef CONFIG_SND_OSSEMUL
484 if (rawmidi_file->input && rawmidi_file->input->runtime)
485 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
486 if (rawmidi_file->output && rawmidi_file->output->runtime)
487 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
489 file->private_data = rawmidi_file;
490 mutex_unlock(&rmidi->open_mutex);
491 snd_card_unref(rmidi->card);
495 snd_card_file_remove(card, file);
497 mutex_unlock(&rmidi->open_mutex);
498 module_put(rmidi->card->module);
499 snd_card_unref(rmidi->card);
503 static void close_substream(struct snd_rawmidi *rmidi,
504 struct snd_rawmidi_substream *substream,
507 if (--substream->use_count)
511 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
512 snd_rawmidi_input_trigger(substream, 0);
514 if (substream->active_sensing) {
515 unsigned char buf = 0xfe;
516 /* sending single active sensing message
517 * to shut the device up
519 snd_rawmidi_kernel_write(substream, &buf, 1);
521 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
522 snd_rawmidi_output_trigger(substream, 0);
525 substream->ops->close(substream);
526 if (substream->runtime->private_free)
527 substream->runtime->private_free(substream);
528 snd_rawmidi_runtime_free(substream);
529 substream->opened = 0;
530 substream->append = 0;
531 put_pid(substream->pid);
532 substream->pid = NULL;
533 rmidi->streams[substream->stream].substream_opened--;
536 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
538 struct snd_rawmidi *rmidi;
540 rmidi = rfile->rmidi;
541 mutex_lock(&rmidi->open_mutex);
543 close_substream(rmidi, rfile->input, 1);
547 close_substream(rmidi, rfile->output, 1);
548 rfile->output = NULL;
551 mutex_unlock(&rmidi->open_mutex);
552 wake_up(&rmidi->open_wait);
555 /* called from sound/core/seq/seq_midi.c */
556 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
558 struct snd_rawmidi *rmidi;
560 if (snd_BUG_ON(!rfile))
563 rmidi = rfile->rmidi;
564 rawmidi_release_priv(rfile);
565 module_put(rmidi->card->module);
568 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
570 static int snd_rawmidi_release(struct inode *inode, struct file *file)
572 struct snd_rawmidi_file *rfile;
573 struct snd_rawmidi *rmidi;
574 struct module *module;
576 rfile = file->private_data;
577 rmidi = rfile->rmidi;
578 rawmidi_release_priv(rfile);
580 module = rmidi->card->module;
581 snd_card_file_remove(rmidi->card, file);
586 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
587 struct snd_rawmidi_info *info)
589 struct snd_rawmidi *rmidi;
591 if (substream == NULL)
593 rmidi = substream->rmidi;
594 memset(info, 0, sizeof(*info));
595 info->card = rmidi->card->number;
596 info->device = rmidi->device;
597 info->subdevice = substream->number;
598 info->stream = substream->stream;
599 info->flags = rmidi->info_flags;
600 strcpy(info->id, rmidi->id);
601 strcpy(info->name, rmidi->name);
602 strcpy(info->subname, substream->name);
603 info->subdevices_count = substream->pstr->substream_count;
604 info->subdevices_avail = (substream->pstr->substream_count -
605 substream->pstr->substream_opened);
609 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
610 struct snd_rawmidi_info __user *_info)
612 struct snd_rawmidi_info info;
615 err = snd_rawmidi_info(substream, &info);
618 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
623 static int __snd_rawmidi_info_select(struct snd_card *card,
624 struct snd_rawmidi_info *info)
626 struct snd_rawmidi *rmidi;
627 struct snd_rawmidi_str *pstr;
628 struct snd_rawmidi_substream *substream;
630 rmidi = snd_rawmidi_search(card, info->device);
633 if (info->stream < 0 || info->stream > 1)
635 info->stream = array_index_nospec(info->stream, 2);
636 pstr = &rmidi->streams[info->stream];
637 if (pstr->substream_count == 0)
639 if (info->subdevice >= pstr->substream_count)
641 list_for_each_entry(substream, &pstr->substreams, list) {
642 if ((unsigned int)substream->number == info->subdevice)
643 return snd_rawmidi_info(substream, info);
648 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
652 mutex_lock(®ister_mutex);
653 ret = __snd_rawmidi_info_select(card, info);
654 mutex_unlock(®ister_mutex);
657 EXPORT_SYMBOL(snd_rawmidi_info_select);
659 static int snd_rawmidi_info_select_user(struct snd_card *card,
660 struct snd_rawmidi_info __user *_info)
663 struct snd_rawmidi_info info;
665 if (get_user(info.device, &_info->device))
667 if (get_user(info.stream, &_info->stream))
669 if (get_user(info.subdevice, &_info->subdevice))
671 err = snd_rawmidi_info_select(card, &info);
674 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
679 static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime,
680 struct snd_rawmidi_params *params,
683 char *newbuf, *oldbuf;
684 unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK;
686 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L)
688 if (framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP && (params->buffer_size & 0x1f) != 0)
690 if (params->avail_min < 1 || params->avail_min > params->buffer_size)
692 if (params->buffer_size != runtime->buffer_size) {
693 newbuf = kvzalloc(params->buffer_size, GFP_KERNEL);
696 spin_lock_irq(&runtime->lock);
697 if (runtime->buffer_ref) {
698 spin_unlock_irq(&runtime->lock);
702 oldbuf = runtime->buffer;
703 runtime->buffer = newbuf;
704 runtime->buffer_size = params->buffer_size;
705 __reset_runtime_ptrs(runtime, is_input);
706 spin_unlock_irq(&runtime->lock);
709 runtime->avail_min = params->avail_min;
713 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
714 struct snd_rawmidi_params *params)
716 if (substream->append && substream->use_count > 1)
718 snd_rawmidi_drain_output(substream);
719 substream->active_sensing = !params->no_active_sensing;
720 return resize_runtime_buffer(substream->runtime, params, false);
722 EXPORT_SYMBOL(snd_rawmidi_output_params);
724 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
725 struct snd_rawmidi_params *params)
727 unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK;
728 unsigned int clock_type = params->mode & SNDRV_RAWMIDI_MODE_CLOCK_MASK;
731 if (framing == SNDRV_RAWMIDI_MODE_FRAMING_NONE && clock_type != SNDRV_RAWMIDI_MODE_CLOCK_NONE)
733 else if (clock_type > SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC_RAW)
735 if (framing > SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP)
737 snd_rawmidi_drain_input(substream);
738 err = resize_runtime_buffer(substream->runtime, params, true);
742 substream->framing = framing;
743 substream->clock_type = clock_type;
746 EXPORT_SYMBOL(snd_rawmidi_input_params);
748 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
749 struct snd_rawmidi_status64 *status)
751 struct snd_rawmidi_runtime *runtime = substream->runtime;
753 memset(status, 0, sizeof(*status));
754 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
755 spin_lock_irq(&runtime->lock);
756 status->avail = runtime->avail;
757 spin_unlock_irq(&runtime->lock);
761 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
762 struct snd_rawmidi_status64 *status)
764 struct snd_rawmidi_runtime *runtime = substream->runtime;
766 memset(status, 0, sizeof(*status));
767 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
768 spin_lock_irq(&runtime->lock);
769 status->avail = runtime->avail;
770 status->xruns = runtime->xruns;
772 spin_unlock_irq(&runtime->lock);
776 static int snd_rawmidi_ioctl_status32(struct snd_rawmidi_file *rfile,
777 struct snd_rawmidi_status32 __user *argp)
780 struct snd_rawmidi_status32 __user *status = argp;
781 struct snd_rawmidi_status32 status32;
782 struct snd_rawmidi_status64 status64;
784 if (copy_from_user(&status32, argp,
785 sizeof(struct snd_rawmidi_status32)))
788 switch (status32.stream) {
789 case SNDRV_RAWMIDI_STREAM_OUTPUT:
790 if (rfile->output == NULL)
792 err = snd_rawmidi_output_status(rfile->output, &status64);
794 case SNDRV_RAWMIDI_STREAM_INPUT:
795 if (rfile->input == NULL)
797 err = snd_rawmidi_input_status(rfile->input, &status64);
805 status32 = (struct snd_rawmidi_status32) {
806 .stream = status64.stream,
807 .tstamp_sec = status64.tstamp_sec,
808 .tstamp_nsec = status64.tstamp_nsec,
809 .avail = status64.avail,
810 .xruns = status64.xruns,
813 if (copy_to_user(status, &status32, sizeof(*status)))
819 static int snd_rawmidi_ioctl_status64(struct snd_rawmidi_file *rfile,
820 struct snd_rawmidi_status64 __user *argp)
823 struct snd_rawmidi_status64 status;
825 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status64)))
828 switch (status.stream) {
829 case SNDRV_RAWMIDI_STREAM_OUTPUT:
830 if (rfile->output == NULL)
832 err = snd_rawmidi_output_status(rfile->output, &status);
834 case SNDRV_RAWMIDI_STREAM_INPUT:
835 if (rfile->input == NULL)
837 err = snd_rawmidi_input_status(rfile->input, &status);
844 if (copy_to_user(argp, &status,
845 sizeof(struct snd_rawmidi_status64)))
850 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
852 struct snd_rawmidi_file *rfile;
853 void __user *argp = (void __user *)arg;
855 rfile = file->private_data;
856 if (((cmd >> 8) & 0xff) != 'W')
859 case SNDRV_RAWMIDI_IOCTL_PVERSION:
860 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
861 case SNDRV_RAWMIDI_IOCTL_INFO:
864 struct snd_rawmidi_info __user *info = argp;
866 if (get_user(stream, &info->stream))
869 case SNDRV_RAWMIDI_STREAM_INPUT:
870 return snd_rawmidi_info_user(rfile->input, info);
871 case SNDRV_RAWMIDI_STREAM_OUTPUT:
872 return snd_rawmidi_info_user(rfile->output, info);
877 case SNDRV_RAWMIDI_IOCTL_USER_PVERSION:
878 if (get_user(rfile->user_pversion, (unsigned int __user *)arg))
882 case SNDRV_RAWMIDI_IOCTL_PARAMS:
884 struct snd_rawmidi_params params;
886 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
888 if (rfile->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 2)) {
890 memset(params.reserved, 0, sizeof(params.reserved));
892 switch (params.stream) {
893 case SNDRV_RAWMIDI_STREAM_OUTPUT:
894 if (rfile->output == NULL)
896 return snd_rawmidi_output_params(rfile->output, ¶ms);
897 case SNDRV_RAWMIDI_STREAM_INPUT:
898 if (rfile->input == NULL)
900 return snd_rawmidi_input_params(rfile->input, ¶ms);
905 case SNDRV_RAWMIDI_IOCTL_STATUS32:
906 return snd_rawmidi_ioctl_status32(rfile, argp);
907 case SNDRV_RAWMIDI_IOCTL_STATUS64:
908 return snd_rawmidi_ioctl_status64(rfile, argp);
909 case SNDRV_RAWMIDI_IOCTL_DROP:
913 if (get_user(val, (int __user *) argp))
916 case SNDRV_RAWMIDI_STREAM_OUTPUT:
917 if (rfile->output == NULL)
919 return snd_rawmidi_drop_output(rfile->output);
924 case SNDRV_RAWMIDI_IOCTL_DRAIN:
928 if (get_user(val, (int __user *) argp))
931 case SNDRV_RAWMIDI_STREAM_OUTPUT:
932 if (rfile->output == NULL)
934 return snd_rawmidi_drain_output(rfile->output);
935 case SNDRV_RAWMIDI_STREAM_INPUT:
936 if (rfile->input == NULL)
938 return snd_rawmidi_drain_input(rfile->input);
944 rmidi_dbg(rfile->rmidi,
945 "rawmidi: unknown command = 0x%x\n", cmd);
950 static int snd_rawmidi_control_ioctl(struct snd_card *card,
951 struct snd_ctl_file *control,
955 void __user *argp = (void __user *)arg;
958 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
962 if (get_user(device, (int __user *)argp))
964 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
965 device = SNDRV_RAWMIDI_DEVICES - 1;
966 mutex_lock(®ister_mutex);
967 device = device < 0 ? 0 : device + 1;
968 while (device < SNDRV_RAWMIDI_DEVICES) {
969 if (snd_rawmidi_search(card, device))
973 if (device == SNDRV_RAWMIDI_DEVICES)
975 mutex_unlock(®ister_mutex);
976 if (put_user(device, (int __user *)argp))
980 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
984 if (get_user(val, (int __user *)argp))
986 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
989 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
990 return snd_rawmidi_info_select_user(card, argp);
995 static int receive_with_tstamp_framing(struct snd_rawmidi_substream *substream,
996 const unsigned char *buffer, int src_count, const struct timespec64 *tstamp)
998 struct snd_rawmidi_runtime *runtime = substream->runtime;
999 struct snd_rawmidi_framing_tstamp *dest_ptr;
1000 struct snd_rawmidi_framing_tstamp frame = { .tv_sec = tstamp->tv_sec, .tv_nsec = tstamp->tv_nsec };
1001 int dest_frames = 0;
1002 int orig_count = src_count;
1003 int frame_size = sizeof(struct snd_rawmidi_framing_tstamp);
1005 BUILD_BUG_ON(frame_size != 0x20);
1006 if (snd_BUG_ON((runtime->hw_ptr & 0x1f) != 0))
1009 while (src_count > 0) {
1010 if ((int)(runtime->buffer_size - runtime->avail) < frame_size) {
1011 runtime->xruns += src_count;
1014 if (src_count >= SNDRV_RAWMIDI_FRAMING_DATA_LENGTH)
1015 frame.length = SNDRV_RAWMIDI_FRAMING_DATA_LENGTH;
1017 frame.length = src_count;
1018 memset(frame.data, 0, SNDRV_RAWMIDI_FRAMING_DATA_LENGTH);
1020 memcpy(frame.data, buffer, frame.length);
1021 buffer += frame.length;
1022 src_count -= frame.length;
1023 dest_ptr = (struct snd_rawmidi_framing_tstamp *) (runtime->buffer + runtime->hw_ptr);
1025 runtime->avail += frame_size;
1026 runtime->hw_ptr += frame_size;
1027 runtime->hw_ptr %= runtime->buffer_size;
1030 return orig_count - src_count;
1033 static struct timespec64 get_framing_tstamp(struct snd_rawmidi_substream *substream)
1035 struct timespec64 ts64 = {0, 0};
1037 switch (substream->clock_type) {
1038 case SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC_RAW:
1039 ktime_get_raw_ts64(&ts64);
1041 case SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC:
1042 ktime_get_ts64(&ts64);
1044 case SNDRV_RAWMIDI_MODE_CLOCK_REALTIME:
1045 ktime_get_real_ts64(&ts64);
1052 * snd_rawmidi_receive - receive the input data from the device
1053 * @substream: the rawmidi substream
1054 * @buffer: the buffer pointer
1055 * @count: the data size to read
1057 * Reads the data from the internal buffer.
1059 * Return: The size of read data, or a negative error code on failure.
1061 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
1062 const unsigned char *buffer, int count)
1064 unsigned long flags;
1065 struct timespec64 ts64 = get_framing_tstamp(substream);
1066 int result = 0, count1;
1067 struct snd_rawmidi_runtime *runtime = substream->runtime;
1069 if (!substream->opened)
1071 if (runtime->buffer == NULL) {
1072 rmidi_dbg(substream->rmidi,
1073 "snd_rawmidi_receive: input is not active!!!\n");
1077 spin_lock_irqsave(&runtime->lock, flags);
1078 if (substream->framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) {
1079 result = receive_with_tstamp_framing(substream, buffer, count, &ts64);
1080 } else if (count == 1) { /* special case, faster code */
1082 if (runtime->avail < runtime->buffer_size) {
1083 runtime->buffer[runtime->hw_ptr++] = buffer[0];
1084 runtime->hw_ptr %= runtime->buffer_size;
1091 substream->bytes += count;
1092 count1 = runtime->buffer_size - runtime->hw_ptr;
1095 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1096 count1 = runtime->buffer_size - runtime->avail;
1097 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
1098 runtime->hw_ptr += count1;
1099 runtime->hw_ptr %= runtime->buffer_size;
1100 runtime->avail += count1;
1106 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
1107 count1 = runtime->buffer_size - runtime->avail;
1108 runtime->xruns += count - count1;
1111 memcpy(runtime->buffer, buffer, count1);
1112 runtime->hw_ptr = count1;
1113 runtime->avail += count1;
1120 schedule_work(&runtime->event_work);
1121 else if (__snd_rawmidi_ready(runtime))
1122 wake_up(&runtime->sleep);
1124 spin_unlock_irqrestore(&runtime->lock, flags);
1127 EXPORT_SYMBOL(snd_rawmidi_receive);
1129 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
1130 unsigned char __user *userbuf,
1131 unsigned char *kernelbuf, long count)
1133 unsigned long flags;
1134 long result = 0, count1;
1135 struct snd_rawmidi_runtime *runtime = substream->runtime;
1136 unsigned long appl_ptr;
1139 spin_lock_irqsave(&runtime->lock, flags);
1140 snd_rawmidi_buffer_ref(runtime);
1141 while (count > 0 && runtime->avail) {
1142 count1 = runtime->buffer_size - runtime->appl_ptr;
1145 if (count1 > (int)runtime->avail)
1146 count1 = runtime->avail;
1148 /* update runtime->appl_ptr before unlocking for userbuf */
1149 appl_ptr = runtime->appl_ptr;
1150 runtime->appl_ptr += count1;
1151 runtime->appl_ptr %= runtime->buffer_size;
1152 runtime->avail -= count1;
1155 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
1157 spin_unlock_irqrestore(&runtime->lock, flags);
1158 if (copy_to_user(userbuf + result,
1159 runtime->buffer + appl_ptr, count1))
1161 spin_lock_irqsave(&runtime->lock, flags);
1169 snd_rawmidi_buffer_unref(runtime);
1170 spin_unlock_irqrestore(&runtime->lock, flags);
1171 return result > 0 ? result : err;
1174 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
1175 unsigned char *buf, long count)
1177 snd_rawmidi_input_trigger(substream, 1);
1178 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
1180 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1182 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1187 struct snd_rawmidi_file *rfile;
1188 struct snd_rawmidi_substream *substream;
1189 struct snd_rawmidi_runtime *runtime;
1191 rfile = file->private_data;
1192 substream = rfile->input;
1193 if (substream == NULL)
1195 runtime = substream->runtime;
1196 snd_rawmidi_input_trigger(substream, 1);
1199 spin_lock_irq(&runtime->lock);
1200 while (!__snd_rawmidi_ready(runtime)) {
1201 wait_queue_entry_t wait;
1203 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1204 spin_unlock_irq(&runtime->lock);
1205 return result > 0 ? result : -EAGAIN;
1207 init_waitqueue_entry(&wait, current);
1208 add_wait_queue(&runtime->sleep, &wait);
1209 set_current_state(TASK_INTERRUPTIBLE);
1210 spin_unlock_irq(&runtime->lock);
1212 remove_wait_queue(&runtime->sleep, &wait);
1213 if (rfile->rmidi->card->shutdown)
1215 if (signal_pending(current))
1216 return result > 0 ? result : -ERESTARTSYS;
1217 spin_lock_irq(&runtime->lock);
1218 if (!runtime->avail) {
1219 spin_unlock_irq(&runtime->lock);
1220 return result > 0 ? result : -EIO;
1223 spin_unlock_irq(&runtime->lock);
1224 count1 = snd_rawmidi_kernel_read1(substream,
1225 (unsigned char __user *)buf,
1229 return result > 0 ? result : count1;
1238 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1239 * @substream: the rawmidi substream
1241 * Return: 1 if the internal output buffer is empty, 0 if not.
1243 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1245 struct snd_rawmidi_runtime *runtime = substream->runtime;
1247 unsigned long flags;
1249 if (runtime->buffer == NULL) {
1250 rmidi_dbg(substream->rmidi,
1251 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1254 spin_lock_irqsave(&runtime->lock, flags);
1255 result = runtime->avail >= runtime->buffer_size;
1256 spin_unlock_irqrestore(&runtime->lock, flags);
1259 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1262 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1263 * @substream: the rawmidi substream
1264 * @buffer: the buffer pointer
1265 * @count: data size to transfer
1267 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1269 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1270 unsigned char *buffer, int count)
1273 struct snd_rawmidi_runtime *runtime = substream->runtime;
1275 if (runtime->buffer == NULL) {
1276 rmidi_dbg(substream->rmidi,
1277 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1281 if (runtime->avail >= runtime->buffer_size) {
1282 /* warning: lowlevel layer MUST trigger down the hardware */
1285 if (count == 1) { /* special case, faster code */
1286 *buffer = runtime->buffer[runtime->hw_ptr];
1289 count1 = runtime->buffer_size - runtime->hw_ptr;
1292 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1293 count1 = runtime->buffer_size - runtime->avail;
1294 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1298 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1299 count = runtime->buffer_size - runtime->avail - count1;
1300 memcpy(buffer + count1, runtime->buffer, count);
1307 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1310 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1311 * @substream: the rawmidi substream
1312 * @buffer: the buffer pointer
1313 * @count: data size to transfer
1315 * Copies data from the internal output buffer to the given buffer.
1317 * Call this in the interrupt handler when the midi output is ready,
1318 * and call snd_rawmidi_transmit_ack() after the transmission is
1321 * Return: The size of copied data, or a negative error code on failure.
1323 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1324 unsigned char *buffer, int count)
1326 struct snd_rawmidi_runtime *runtime = substream->runtime;
1328 unsigned long flags;
1330 spin_lock_irqsave(&runtime->lock, flags);
1331 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1332 spin_unlock_irqrestore(&runtime->lock, flags);
1335 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1338 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1339 * @substream: the rawmidi substream
1340 * @count: the transferred count
1342 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1344 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1346 struct snd_rawmidi_runtime *runtime = substream->runtime;
1348 if (runtime->buffer == NULL) {
1349 rmidi_dbg(substream->rmidi,
1350 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1353 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1354 runtime->hw_ptr += count;
1355 runtime->hw_ptr %= runtime->buffer_size;
1356 runtime->avail += count;
1357 substream->bytes += count;
1359 if (runtime->drain || __snd_rawmidi_ready(runtime))
1360 wake_up(&runtime->sleep);
1364 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1367 * snd_rawmidi_transmit_ack - acknowledge the transmission
1368 * @substream: the rawmidi substream
1369 * @count: the transferred count
1371 * Advances the hardware pointer for the internal output buffer with
1372 * the given size and updates the condition.
1373 * Call after the transmission is finished.
1375 * Return: The advanced size if successful, or a negative error code on failure.
1377 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1379 struct snd_rawmidi_runtime *runtime = substream->runtime;
1381 unsigned long flags;
1383 spin_lock_irqsave(&runtime->lock, flags);
1384 result = __snd_rawmidi_transmit_ack(substream, count);
1385 spin_unlock_irqrestore(&runtime->lock, flags);
1388 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1391 * snd_rawmidi_transmit - copy from the buffer to the device
1392 * @substream: the rawmidi substream
1393 * @buffer: the buffer pointer
1394 * @count: the data size to transfer
1396 * Copies data from the buffer to the device and advances the pointer.
1398 * Return: The copied size if successful, or a negative error code on failure.
1400 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1401 unsigned char *buffer, int count)
1403 struct snd_rawmidi_runtime *runtime = substream->runtime;
1405 unsigned long flags;
1407 spin_lock_irqsave(&runtime->lock, flags);
1408 if (!substream->opened)
1411 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1415 result = __snd_rawmidi_transmit_ack(substream, count);
1417 spin_unlock_irqrestore(&runtime->lock, flags);
1420 EXPORT_SYMBOL(snd_rawmidi_transmit);
1423 * snd_rawmidi_proceed - Discard the all pending bytes and proceed
1424 * @substream: rawmidi substream
1426 * Return: the number of discarded bytes
1428 int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream)
1430 struct snd_rawmidi_runtime *runtime = substream->runtime;
1431 unsigned long flags;
1434 spin_lock_irqsave(&runtime->lock, flags);
1435 if (runtime->avail < runtime->buffer_size) {
1436 count = runtime->buffer_size - runtime->avail;
1437 __snd_rawmidi_transmit_ack(substream, count);
1439 spin_unlock_irqrestore(&runtime->lock, flags);
1442 EXPORT_SYMBOL(snd_rawmidi_proceed);
1444 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1445 const unsigned char __user *userbuf,
1446 const unsigned char *kernelbuf,
1449 unsigned long flags;
1450 long count1, result;
1451 struct snd_rawmidi_runtime *runtime = substream->runtime;
1452 unsigned long appl_ptr;
1454 if (!kernelbuf && !userbuf)
1456 if (snd_BUG_ON(!runtime->buffer))
1460 spin_lock_irqsave(&runtime->lock, flags);
1461 if (substream->append) {
1462 if ((long)runtime->avail < count) {
1463 spin_unlock_irqrestore(&runtime->lock, flags);
1467 snd_rawmidi_buffer_ref(runtime);
1468 while (count > 0 && runtime->avail > 0) {
1469 count1 = runtime->buffer_size - runtime->appl_ptr;
1472 if (count1 > (long)runtime->avail)
1473 count1 = runtime->avail;
1475 /* update runtime->appl_ptr before unlocking for userbuf */
1476 appl_ptr = runtime->appl_ptr;
1477 runtime->appl_ptr += count1;
1478 runtime->appl_ptr %= runtime->buffer_size;
1479 runtime->avail -= count1;
1482 memcpy(runtime->buffer + appl_ptr,
1483 kernelbuf + result, count1);
1485 spin_unlock_irqrestore(&runtime->lock, flags);
1486 if (copy_from_user(runtime->buffer + appl_ptr,
1487 userbuf + result, count1)) {
1488 spin_lock_irqsave(&runtime->lock, flags);
1489 result = result > 0 ? result : -EFAULT;
1492 spin_lock_irqsave(&runtime->lock, flags);
1498 count1 = runtime->avail < runtime->buffer_size;
1499 snd_rawmidi_buffer_unref(runtime);
1500 spin_unlock_irqrestore(&runtime->lock, flags);
1502 snd_rawmidi_output_trigger(substream, 1);
1506 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1507 const unsigned char *buf, long count)
1509 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1511 EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1513 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1514 size_t count, loff_t *offset)
1516 long result, timeout;
1518 struct snd_rawmidi_file *rfile;
1519 struct snd_rawmidi_runtime *runtime;
1520 struct snd_rawmidi_substream *substream;
1522 rfile = file->private_data;
1523 substream = rfile->output;
1524 runtime = substream->runtime;
1525 /* we cannot put an atomic message to our buffer */
1526 if (substream->append && count > runtime->buffer_size)
1530 spin_lock_irq(&runtime->lock);
1531 while (!snd_rawmidi_ready_append(substream, count)) {
1532 wait_queue_entry_t wait;
1534 if (file->f_flags & O_NONBLOCK) {
1535 spin_unlock_irq(&runtime->lock);
1536 return result > 0 ? result : -EAGAIN;
1538 init_waitqueue_entry(&wait, current);
1539 add_wait_queue(&runtime->sleep, &wait);
1540 set_current_state(TASK_INTERRUPTIBLE);
1541 spin_unlock_irq(&runtime->lock);
1542 timeout = schedule_timeout(30 * HZ);
1543 remove_wait_queue(&runtime->sleep, &wait);
1544 if (rfile->rmidi->card->shutdown)
1546 if (signal_pending(current))
1547 return result > 0 ? result : -ERESTARTSYS;
1548 spin_lock_irq(&runtime->lock);
1549 if (!runtime->avail && !timeout) {
1550 spin_unlock_irq(&runtime->lock);
1551 return result > 0 ? result : -EIO;
1554 spin_unlock_irq(&runtime->lock);
1555 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1557 return result > 0 ? result : count1;
1560 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1564 if (file->f_flags & O_DSYNC) {
1565 spin_lock_irq(&runtime->lock);
1566 while (runtime->avail != runtime->buffer_size) {
1567 wait_queue_entry_t wait;
1568 unsigned int last_avail = runtime->avail;
1570 init_waitqueue_entry(&wait, current);
1571 add_wait_queue(&runtime->sleep, &wait);
1572 set_current_state(TASK_INTERRUPTIBLE);
1573 spin_unlock_irq(&runtime->lock);
1574 timeout = schedule_timeout(30 * HZ);
1575 remove_wait_queue(&runtime->sleep, &wait);
1576 if (signal_pending(current))
1577 return result > 0 ? result : -ERESTARTSYS;
1578 if (runtime->avail == last_avail && !timeout)
1579 return result > 0 ? result : -EIO;
1580 spin_lock_irq(&runtime->lock);
1582 spin_unlock_irq(&runtime->lock);
1587 static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait)
1589 struct snd_rawmidi_file *rfile;
1590 struct snd_rawmidi_runtime *runtime;
1593 rfile = file->private_data;
1594 if (rfile->input != NULL) {
1595 runtime = rfile->input->runtime;
1596 snd_rawmidi_input_trigger(rfile->input, 1);
1597 poll_wait(file, &runtime->sleep, wait);
1599 if (rfile->output != NULL) {
1600 runtime = rfile->output->runtime;
1601 poll_wait(file, &runtime->sleep, wait);
1604 if (rfile->input != NULL) {
1605 if (snd_rawmidi_ready(rfile->input))
1606 mask |= EPOLLIN | EPOLLRDNORM;
1608 if (rfile->output != NULL) {
1609 if (snd_rawmidi_ready(rfile->output))
1610 mask |= EPOLLOUT | EPOLLWRNORM;
1617 #ifdef CONFIG_COMPAT
1618 #include "rawmidi_compat.c"
1620 #define snd_rawmidi_ioctl_compat NULL
1626 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1627 struct snd_info_buffer *buffer)
1629 struct snd_rawmidi *rmidi;
1630 struct snd_rawmidi_substream *substream;
1631 struct snd_rawmidi_runtime *runtime;
1632 unsigned long buffer_size, avail, xruns;
1633 unsigned int clock_type;
1634 static const char *clock_names[4] = { "none", "realtime", "monotonic", "monotonic raw" };
1636 rmidi = entry->private_data;
1637 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1638 mutex_lock(&rmidi->open_mutex);
1639 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1640 list_for_each_entry(substream,
1641 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1645 " Tx bytes : %lu\n",
1647 (unsigned long) substream->bytes);
1648 if (substream->opened) {
1650 " Owner PID : %d\n",
1651 pid_vnr(substream->pid));
1652 runtime = substream->runtime;
1653 spin_lock_irq(&runtime->lock);
1654 buffer_size = runtime->buffer_size;
1655 avail = runtime->avail;
1656 spin_unlock_irq(&runtime->lock);
1659 " Buffer size : %lu\n"
1661 runtime->oss ? "OSS compatible" : "native",
1662 buffer_size, avail);
1666 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1667 list_for_each_entry(substream,
1668 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1672 " Rx bytes : %lu\n",
1674 (unsigned long) substream->bytes);
1675 if (substream->opened) {
1677 " Owner PID : %d\n",
1678 pid_vnr(substream->pid));
1679 runtime = substream->runtime;
1680 spin_lock_irq(&runtime->lock);
1681 buffer_size = runtime->buffer_size;
1682 avail = runtime->avail;
1683 xruns = runtime->xruns;
1684 spin_unlock_irq(&runtime->lock);
1686 " Buffer size : %lu\n"
1688 " Overruns : %lu\n",
1689 buffer_size, avail, xruns);
1690 if (substream->framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) {
1691 clock_type = substream->clock_type >> SNDRV_RAWMIDI_MODE_CLOCK_SHIFT;
1692 if (!snd_BUG_ON(clock_type >= ARRAY_SIZE(clock_names)))
1694 " Framing : tstamp\n"
1695 " Clock type : %s\n",
1696 clock_names[clock_type]);
1701 mutex_unlock(&rmidi->open_mutex);
1705 * Register functions
1708 static const struct file_operations snd_rawmidi_f_ops = {
1709 .owner = THIS_MODULE,
1710 .read = snd_rawmidi_read,
1711 .write = snd_rawmidi_write,
1712 .open = snd_rawmidi_open,
1713 .release = snd_rawmidi_release,
1714 .llseek = no_llseek,
1715 .poll = snd_rawmidi_poll,
1716 .unlocked_ioctl = snd_rawmidi_ioctl,
1717 .compat_ioctl = snd_rawmidi_ioctl_compat,
1720 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1721 struct snd_rawmidi_str *stream,
1725 struct snd_rawmidi_substream *substream;
1728 for (idx = 0; idx < count; idx++) {
1729 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1732 substream->stream = direction;
1733 substream->number = idx;
1734 substream->rmidi = rmidi;
1735 substream->pstr = stream;
1736 list_add_tail(&substream->list, &stream->substreams);
1737 stream->substream_count++;
1742 static void release_rawmidi_device(struct device *dev)
1744 kfree(container_of(dev, struct snd_rawmidi, dev));
1748 * snd_rawmidi_new - create a rawmidi instance
1749 * @card: the card instance
1750 * @id: the id string
1751 * @device: the device index
1752 * @output_count: the number of output streams
1753 * @input_count: the number of input streams
1754 * @rrawmidi: the pointer to store the new rawmidi instance
1756 * Creates a new rawmidi instance.
1757 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1759 * Return: Zero if successful, or a negative error code on failure.
1761 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1762 int output_count, int input_count,
1763 struct snd_rawmidi **rrawmidi)
1765 struct snd_rawmidi *rmidi;
1767 static const struct snd_device_ops ops = {
1768 .dev_free = snd_rawmidi_dev_free,
1769 .dev_register = snd_rawmidi_dev_register,
1770 .dev_disconnect = snd_rawmidi_dev_disconnect,
1773 if (snd_BUG_ON(!card))
1777 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1781 rmidi->device = device;
1782 mutex_init(&rmidi->open_mutex);
1783 init_waitqueue_head(&rmidi->open_wait);
1784 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1785 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1788 strscpy(rmidi->id, id, sizeof(rmidi->id));
1790 snd_device_initialize(&rmidi->dev, card);
1791 rmidi->dev.release = release_rawmidi_device;
1792 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1794 err = snd_rawmidi_alloc_substreams(rmidi,
1795 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1796 SNDRV_RAWMIDI_STREAM_INPUT,
1800 err = snd_rawmidi_alloc_substreams(rmidi,
1801 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1802 SNDRV_RAWMIDI_STREAM_OUTPUT,
1806 err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops);
1815 snd_rawmidi_free(rmidi);
1818 EXPORT_SYMBOL(snd_rawmidi_new);
1820 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1822 struct snd_rawmidi_substream *substream;
1824 while (!list_empty(&stream->substreams)) {
1825 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1826 list_del(&substream->list);
1831 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1836 snd_info_free_entry(rmidi->proc_entry);
1837 rmidi->proc_entry = NULL;
1838 mutex_lock(®ister_mutex);
1839 if (rmidi->ops && rmidi->ops->dev_unregister)
1840 rmidi->ops->dev_unregister(rmidi);
1841 mutex_unlock(®ister_mutex);
1843 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1844 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1845 if (rmidi->private_free)
1846 rmidi->private_free(rmidi);
1847 put_device(&rmidi->dev);
1851 static int snd_rawmidi_dev_free(struct snd_device *device)
1853 struct snd_rawmidi *rmidi = device->device_data;
1855 return snd_rawmidi_free(rmidi);
1858 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1859 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1861 struct snd_rawmidi *rmidi = device->private_data;
1863 rmidi->seq_dev = NULL;
1867 static int snd_rawmidi_dev_register(struct snd_device *device)
1870 struct snd_info_entry *entry;
1872 struct snd_rawmidi *rmidi = device->device_data;
1874 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1877 mutex_lock(®ister_mutex);
1878 if (snd_rawmidi_search(rmidi->card, rmidi->device))
1881 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1882 mutex_unlock(®ister_mutex);
1886 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1887 rmidi->card, rmidi->device,
1888 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
1890 rmidi_err(rmidi, "unable to register\n");
1893 if (rmidi->ops && rmidi->ops->dev_register) {
1894 err = rmidi->ops->dev_register(rmidi);
1896 goto error_unregister;
1898 #ifdef CONFIG_SND_OSSEMUL
1900 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1901 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1902 rmidi->card, 0, &snd_rawmidi_f_ops,
1905 "unable to register OSS rawmidi device %i:%i\n",
1906 rmidi->card->number, 0);
1909 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1910 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1914 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1915 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1916 rmidi->card, 1, &snd_rawmidi_f_ops,
1919 "unable to register OSS rawmidi device %i:%i\n",
1920 rmidi->card->number, 1);
1925 #endif /* CONFIG_SND_OSSEMUL */
1926 sprintf(name, "midi%d", rmidi->device);
1927 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1929 entry->private_data = rmidi;
1930 entry->c.text.read = snd_rawmidi_proc_info_read;
1931 if (snd_info_register(entry) < 0) {
1932 snd_info_free_entry(entry);
1936 rmidi->proc_entry = entry;
1937 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1938 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1939 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1940 rmidi->seq_dev->private_data = rmidi;
1941 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1942 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1943 snd_device_register(rmidi->card, rmidi->seq_dev);
1950 snd_unregister_device(&rmidi->dev);
1952 mutex_lock(®ister_mutex);
1953 list_del(&rmidi->list);
1954 mutex_unlock(®ister_mutex);
1958 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1960 struct snd_rawmidi *rmidi = device->device_data;
1963 mutex_lock(®ister_mutex);
1964 mutex_lock(&rmidi->open_mutex);
1965 wake_up(&rmidi->open_wait);
1966 list_del_init(&rmidi->list);
1967 for (dir = 0; dir < 2; dir++) {
1968 struct snd_rawmidi_substream *s;
1970 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1972 wake_up(&s->runtime->sleep);
1976 #ifdef CONFIG_SND_OSSEMUL
1977 if (rmidi->ossreg) {
1978 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1979 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1980 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1981 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1984 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1985 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1988 #endif /* CONFIG_SND_OSSEMUL */
1989 snd_unregister_device(&rmidi->dev);
1990 mutex_unlock(&rmidi->open_mutex);
1991 mutex_unlock(®ister_mutex);
1996 * snd_rawmidi_set_ops - set the rawmidi operators
1997 * @rmidi: the rawmidi instance
1998 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1999 * @ops: the operator table
2001 * Sets the rawmidi operators for the given stream direction.
2003 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
2004 const struct snd_rawmidi_ops *ops)
2006 struct snd_rawmidi_substream *substream;
2008 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
2009 substream->ops = ops;
2011 EXPORT_SYMBOL(snd_rawmidi_set_ops);
2017 static int __init alsa_rawmidi_init(void)
2020 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
2021 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
2022 #ifdef CONFIG_SND_OSSEMUL
2024 /* check device map table */
2025 for (i = 0; i < SNDRV_CARDS; i++) {
2026 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
2027 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
2031 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
2032 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
2038 #endif /* CONFIG_SND_OSSEMUL */
2042 static void __exit alsa_rawmidi_exit(void)
2044 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
2045 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
2048 module_init(alsa_rawmidi_init)
2049 module_exit(alsa_rawmidi_exit)