1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Abstract layer for MIDI v1.0 stream
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <sound/core.h>
8 #include <linux/major.h>
9 #include <linux/init.h>
10 #include <linux/sched/signal.h>
11 #include <linux/slab.h>
12 #include <linux/time.h>
13 #include <linux/wait.h>
14 #include <linux/mutex.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
18 #include <linux/nospec.h>
19 #include <sound/rawmidi.h>
20 #include <sound/info.h>
21 #include <sound/control.h>
22 #include <sound/minors.h>
23 #include <sound/initval.h>
25 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
26 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
27 MODULE_LICENSE("GPL");
29 #ifdef CONFIG_SND_OSSEMUL
30 static int midi_map
[SNDRV_CARDS
];
31 static int amidi_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
32 module_param_array(midi_map
, int, NULL
, 0444);
33 MODULE_PARM_DESC(midi_map
, "Raw MIDI device number assigned to 1st OSS device.");
34 module_param_array(amidi_map
, int, NULL
, 0444);
35 MODULE_PARM_DESC(amidi_map
, "Raw MIDI device number assigned to 2nd OSS device.");
36 #endif /* CONFIG_SND_OSSEMUL */
38 static int snd_rawmidi_free(struct snd_rawmidi
*rawmidi
);
39 static int snd_rawmidi_dev_free(struct snd_device
*device
);
40 static int snd_rawmidi_dev_register(struct snd_device
*device
);
41 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
);
43 static LIST_HEAD(snd_rawmidi_devices
);
44 static DEFINE_MUTEX(register_mutex
);
46 #define rmidi_err(rmidi, fmt, args...) \
47 dev_err(&(rmidi)->dev, fmt, ##args)
48 #define rmidi_warn(rmidi, fmt, args...) \
49 dev_warn(&(rmidi)->dev, fmt, ##args)
50 #define rmidi_dbg(rmidi, fmt, args...) \
51 dev_dbg(&(rmidi)->dev, fmt, ##args)
53 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 int snd_rawmidi_ready(struct snd_rawmidi_substream
*substream
)
100 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
102 return runtime
->avail
>= runtime
->avail_min
;
105 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream
*substream
,
108 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
110 return runtime
->avail
>= runtime
->avail_min
&&
111 (!substream
->append
|| runtime
->avail
>= count
);
114 static void snd_rawmidi_input_event_work(struct work_struct
*work
)
116 struct snd_rawmidi_runtime
*runtime
=
117 container_of(work
, struct snd_rawmidi_runtime
, event_work
);
120 runtime
->event(runtime
->substream
);
123 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream
*substream
)
125 struct snd_rawmidi_runtime
*runtime
;
127 runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
);
130 runtime
->substream
= substream
;
131 spin_lock_init(&runtime
->lock
);
132 init_waitqueue_head(&runtime
->sleep
);
133 INIT_WORK(&runtime
->event_work
, snd_rawmidi_input_event_work
);
134 runtime
->event
= NULL
;
135 runtime
->buffer_size
= PAGE_SIZE
;
136 runtime
->avail_min
= 1;
137 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
140 runtime
->avail
= runtime
->buffer_size
;
141 runtime
->buffer
= kvzalloc(runtime
->buffer_size
, GFP_KERNEL
);
142 if (!runtime
->buffer
) {
146 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
147 substream
->runtime
= runtime
;
151 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream
*substream
)
153 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
155 kvfree(runtime
->buffer
);
157 substream
->runtime
= NULL
;
161 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream
*substream
, int up
)
163 if (!substream
->opened
)
165 substream
->ops
->trigger(substream
, up
);
168 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
170 if (!substream
->opened
)
172 substream
->ops
->trigger(substream
, up
);
174 cancel_work_sync(&substream
->runtime
->event_work
);
177 static void __reset_runtime_ptrs(struct snd_rawmidi_runtime
*runtime
,
181 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
182 runtime
->avail
= is_input
? 0 : runtime
->buffer_size
;
185 static void reset_runtime_ptrs(struct snd_rawmidi_runtime
*runtime
,
190 spin_lock_irqsave(&runtime
->lock
, flags
);
191 __reset_runtime_ptrs(runtime
, is_input
);
192 spin_unlock_irqrestore(&runtime
->lock
, flags
);
195 int snd_rawmidi_drop_output(struct snd_rawmidi_substream
*substream
)
197 snd_rawmidi_output_trigger(substream
, 0);
198 reset_runtime_ptrs(substream
->runtime
, false);
201 EXPORT_SYMBOL(snd_rawmidi_drop_output
);
203 int snd_rawmidi_drain_output(struct snd_rawmidi_substream
*substream
)
207 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
211 timeout
= wait_event_interruptible_timeout(runtime
->sleep
,
212 (runtime
->avail
>= runtime
->buffer_size
),
214 if (signal_pending(current
))
216 if (runtime
->avail
< runtime
->buffer_size
&& !timeout
) {
217 rmidi_warn(substream
->rmidi
,
218 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
219 (long)runtime
->avail
, (long)runtime
->buffer_size
);
223 if (err
!= -ERESTARTSYS
) {
224 /* we need wait a while to make sure that Tx FIFOs are empty */
225 if (substream
->ops
->drain
)
226 substream
->ops
->drain(substream
);
229 snd_rawmidi_drop_output(substream
);
233 EXPORT_SYMBOL(snd_rawmidi_drain_output
);
235 int snd_rawmidi_drain_input(struct snd_rawmidi_substream
*substream
)
237 snd_rawmidi_input_trigger(substream
, 0);
238 reset_runtime_ptrs(substream
->runtime
, true);
241 EXPORT_SYMBOL(snd_rawmidi_drain_input
);
243 /* look for an available substream for the given stream direction;
244 * if a specific subdevice is given, try to assign it
246 static int assign_substream(struct snd_rawmidi
*rmidi
, int subdevice
,
247 int stream
, int mode
,
248 struct snd_rawmidi_substream
**sub_ret
)
250 struct snd_rawmidi_substream
*substream
;
251 struct snd_rawmidi_str
*s
= &rmidi
->streams
[stream
];
252 static const unsigned int info_flags
[2] = {
253 [SNDRV_RAWMIDI_STREAM_OUTPUT
] = SNDRV_RAWMIDI_INFO_OUTPUT
,
254 [SNDRV_RAWMIDI_STREAM_INPUT
] = SNDRV_RAWMIDI_INFO_INPUT
,
257 if (!(rmidi
->info_flags
& info_flags
[stream
]))
259 if (subdevice
>= 0 && subdevice
>= s
->substream_count
)
262 list_for_each_entry(substream
, &s
->substreams
, list
) {
263 if (substream
->opened
) {
264 if (stream
== SNDRV_RAWMIDI_STREAM_INPUT
||
265 !(mode
& SNDRV_RAWMIDI_LFLG_APPEND
) ||
269 if (subdevice
< 0 || subdevice
== substream
->number
) {
270 *sub_ret
= substream
;
277 /* open and do ref-counting for the given substream */
278 static int open_substream(struct snd_rawmidi
*rmidi
,
279 struct snd_rawmidi_substream
*substream
,
284 if (substream
->use_count
== 0) {
285 err
= snd_rawmidi_runtime_create(substream
);
288 err
= substream
->ops
->open(substream
);
290 snd_rawmidi_runtime_free(substream
);
293 substream
->opened
= 1;
294 substream
->active_sensing
= 0;
295 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
)
296 substream
->append
= 1;
297 substream
->pid
= get_pid(task_pid(current
));
298 rmidi
->streams
[substream
->stream
].substream_opened
++;
300 substream
->use_count
++;
304 static void close_substream(struct snd_rawmidi
*rmidi
,
305 struct snd_rawmidi_substream
*substream
,
308 static int rawmidi_open_priv(struct snd_rawmidi
*rmidi
, int subdevice
, int mode
,
309 struct snd_rawmidi_file
*rfile
)
311 struct snd_rawmidi_substream
*sinput
= NULL
, *soutput
= NULL
;
314 rfile
->input
= rfile
->output
= NULL
;
315 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
316 err
= assign_substream(rmidi
, subdevice
,
317 SNDRV_RAWMIDI_STREAM_INPUT
,
322 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
323 err
= assign_substream(rmidi
, subdevice
,
324 SNDRV_RAWMIDI_STREAM_OUTPUT
,
331 err
= open_substream(rmidi
, sinput
, mode
);
336 err
= open_substream(rmidi
, soutput
, mode
);
339 close_substream(rmidi
, sinput
, 0);
344 rfile
->rmidi
= rmidi
;
345 rfile
->input
= sinput
;
346 rfile
->output
= soutput
;
350 /* called from sound/core/seq/seq_midi.c */
351 int snd_rawmidi_kernel_open(struct snd_card
*card
, int device
, int subdevice
,
352 int mode
, struct snd_rawmidi_file
*rfile
)
354 struct snd_rawmidi
*rmidi
;
357 if (snd_BUG_ON(!rfile
))
360 mutex_lock(®ister_mutex
);
361 rmidi
= snd_rawmidi_search(card
, device
);
364 else if (!try_module_get(rmidi
->card
->module
))
366 mutex_unlock(®ister_mutex
);
370 mutex_lock(&rmidi
->open_mutex
);
371 err
= rawmidi_open_priv(rmidi
, subdevice
, mode
, rfile
);
372 mutex_unlock(&rmidi
->open_mutex
);
374 module_put(rmidi
->card
->module
);
377 EXPORT_SYMBOL(snd_rawmidi_kernel_open
);
379 static int snd_rawmidi_open(struct inode
*inode
, struct file
*file
)
381 int maj
= imajor(inode
);
382 struct snd_card
*card
;
384 unsigned short fflags
;
386 struct snd_rawmidi
*rmidi
;
387 struct snd_rawmidi_file
*rawmidi_file
= NULL
;
388 wait_queue_entry_t wait
;
390 if ((file
->f_flags
& O_APPEND
) && !(file
->f_flags
& O_NONBLOCK
))
391 return -EINVAL
; /* invalid combination */
393 err
= stream_open(inode
, file
);
397 if (maj
== snd_major
) {
398 rmidi
= snd_lookup_minor_data(iminor(inode
),
399 SNDRV_DEVICE_TYPE_RAWMIDI
);
400 #ifdef CONFIG_SND_OSSEMUL
401 } else if (maj
== SOUND_MAJOR
) {
402 rmidi
= snd_lookup_oss_minor_data(iminor(inode
),
403 SNDRV_OSS_DEVICE_TYPE_MIDI
);
411 if (!try_module_get(rmidi
->card
->module
)) {
412 snd_card_unref(rmidi
->card
);
416 mutex_lock(&rmidi
->open_mutex
);
418 err
= snd_card_file_add(card
, file
);
421 fflags
= snd_rawmidi_file_flags(file
);
422 if ((file
->f_flags
& O_APPEND
) || maj
== SOUND_MAJOR
) /* OSS emul? */
423 fflags
|= SNDRV_RAWMIDI_LFLG_APPEND
;
424 rawmidi_file
= kmalloc(sizeof(*rawmidi_file
), GFP_KERNEL
);
425 if (rawmidi_file
== NULL
) {
429 init_waitqueue_entry(&wait
, current
);
430 add_wait_queue(&rmidi
->open_wait
, &wait
);
432 subdevice
= snd_ctl_get_preferred_subdevice(card
, SND_CTL_SUBDEV_RAWMIDI
);
433 err
= rawmidi_open_priv(rmidi
, subdevice
, fflags
, rawmidi_file
);
436 if (err
== -EAGAIN
) {
437 if (file
->f_flags
& O_NONBLOCK
) {
443 set_current_state(TASK_INTERRUPTIBLE
);
444 mutex_unlock(&rmidi
->open_mutex
);
446 mutex_lock(&rmidi
->open_mutex
);
447 if (rmidi
->card
->shutdown
) {
451 if (signal_pending(current
)) {
456 remove_wait_queue(&rmidi
->open_wait
, &wait
);
461 #ifdef CONFIG_SND_OSSEMUL
462 if (rawmidi_file
->input
&& rawmidi_file
->input
->runtime
)
463 rawmidi_file
->input
->runtime
->oss
= (maj
== SOUND_MAJOR
);
464 if (rawmidi_file
->output
&& rawmidi_file
->output
->runtime
)
465 rawmidi_file
->output
->runtime
->oss
= (maj
== SOUND_MAJOR
);
467 file
->private_data
= rawmidi_file
;
468 mutex_unlock(&rmidi
->open_mutex
);
469 snd_card_unref(rmidi
->card
);
473 snd_card_file_remove(card
, file
);
475 mutex_unlock(&rmidi
->open_mutex
);
476 module_put(rmidi
->card
->module
);
477 snd_card_unref(rmidi
->card
);
481 static void close_substream(struct snd_rawmidi
*rmidi
,
482 struct snd_rawmidi_substream
*substream
,
485 if (--substream
->use_count
)
489 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
490 snd_rawmidi_input_trigger(substream
, 0);
492 if (substream
->active_sensing
) {
493 unsigned char buf
= 0xfe;
494 /* sending single active sensing message
495 * to shut the device up
497 snd_rawmidi_kernel_write(substream
, &buf
, 1);
499 if (snd_rawmidi_drain_output(substream
) == -ERESTARTSYS
)
500 snd_rawmidi_output_trigger(substream
, 0);
503 substream
->ops
->close(substream
);
504 if (substream
->runtime
->private_free
)
505 substream
->runtime
->private_free(substream
);
506 snd_rawmidi_runtime_free(substream
);
507 substream
->opened
= 0;
508 substream
->append
= 0;
509 put_pid(substream
->pid
);
510 substream
->pid
= NULL
;
511 rmidi
->streams
[substream
->stream
].substream_opened
--;
514 static void rawmidi_release_priv(struct snd_rawmidi_file
*rfile
)
516 struct snd_rawmidi
*rmidi
;
518 rmidi
= rfile
->rmidi
;
519 mutex_lock(&rmidi
->open_mutex
);
521 close_substream(rmidi
, rfile
->input
, 1);
525 close_substream(rmidi
, rfile
->output
, 1);
526 rfile
->output
= NULL
;
529 mutex_unlock(&rmidi
->open_mutex
);
530 wake_up(&rmidi
->open_wait
);
533 /* called from sound/core/seq/seq_midi.c */
534 int snd_rawmidi_kernel_release(struct snd_rawmidi_file
*rfile
)
536 struct snd_rawmidi
*rmidi
;
538 if (snd_BUG_ON(!rfile
))
541 rmidi
= rfile
->rmidi
;
542 rawmidi_release_priv(rfile
);
543 module_put(rmidi
->card
->module
);
546 EXPORT_SYMBOL(snd_rawmidi_kernel_release
);
548 static int snd_rawmidi_release(struct inode
*inode
, struct file
*file
)
550 struct snd_rawmidi_file
*rfile
;
551 struct snd_rawmidi
*rmidi
;
552 struct module
*module
;
554 rfile
= file
->private_data
;
555 rmidi
= rfile
->rmidi
;
556 rawmidi_release_priv(rfile
);
558 module
= rmidi
->card
->module
;
559 snd_card_file_remove(rmidi
->card
, file
);
564 static int snd_rawmidi_info(struct snd_rawmidi_substream
*substream
,
565 struct snd_rawmidi_info
*info
)
567 struct snd_rawmidi
*rmidi
;
569 if (substream
== NULL
)
571 rmidi
= substream
->rmidi
;
572 memset(info
, 0, sizeof(*info
));
573 info
->card
= rmidi
->card
->number
;
574 info
->device
= rmidi
->device
;
575 info
->subdevice
= substream
->number
;
576 info
->stream
= substream
->stream
;
577 info
->flags
= rmidi
->info_flags
;
578 strcpy(info
->id
, rmidi
->id
);
579 strcpy(info
->name
, rmidi
->name
);
580 strcpy(info
->subname
, substream
->name
);
581 info
->subdevices_count
= substream
->pstr
->substream_count
;
582 info
->subdevices_avail
= (substream
->pstr
->substream_count
-
583 substream
->pstr
->substream_opened
);
587 static int snd_rawmidi_info_user(struct snd_rawmidi_substream
*substream
,
588 struct snd_rawmidi_info __user
*_info
)
590 struct snd_rawmidi_info info
;
593 err
= snd_rawmidi_info(substream
, &info
);
596 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
601 static int __snd_rawmidi_info_select(struct snd_card
*card
,
602 struct snd_rawmidi_info
*info
)
604 struct snd_rawmidi
*rmidi
;
605 struct snd_rawmidi_str
*pstr
;
606 struct snd_rawmidi_substream
*substream
;
608 rmidi
= snd_rawmidi_search(card
, info
->device
);
611 if (info
->stream
< 0 || info
->stream
> 1)
613 info
->stream
= array_index_nospec(info
->stream
, 2);
614 pstr
= &rmidi
->streams
[info
->stream
];
615 if (pstr
->substream_count
== 0)
617 if (info
->subdevice
>= pstr
->substream_count
)
619 list_for_each_entry(substream
, &pstr
->substreams
, list
) {
620 if ((unsigned int)substream
->number
== info
->subdevice
)
621 return snd_rawmidi_info(substream
, info
);
626 int snd_rawmidi_info_select(struct snd_card
*card
, struct snd_rawmidi_info
*info
)
630 mutex_lock(®ister_mutex
);
631 ret
= __snd_rawmidi_info_select(card
, info
);
632 mutex_unlock(®ister_mutex
);
635 EXPORT_SYMBOL(snd_rawmidi_info_select
);
637 static int snd_rawmidi_info_select_user(struct snd_card
*card
,
638 struct snd_rawmidi_info __user
*_info
)
641 struct snd_rawmidi_info info
;
643 if (get_user(info
.device
, &_info
->device
))
645 if (get_user(info
.stream
, &_info
->stream
))
647 if (get_user(info
.subdevice
, &_info
->subdevice
))
649 err
= snd_rawmidi_info_select(card
, &info
);
652 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
657 static int resize_runtime_buffer(struct snd_rawmidi_runtime
*runtime
,
658 struct snd_rawmidi_params
*params
,
661 char *newbuf
, *oldbuf
;
663 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L)
665 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
)
667 if (params
->buffer_size
!= runtime
->buffer_size
) {
668 newbuf
= kvzalloc(params
->buffer_size
, GFP_KERNEL
);
671 spin_lock_irq(&runtime
->lock
);
672 oldbuf
= runtime
->buffer
;
673 runtime
->buffer
= newbuf
;
674 runtime
->buffer_size
= params
->buffer_size
;
675 __reset_runtime_ptrs(runtime
, is_input
);
676 spin_unlock_irq(&runtime
->lock
);
679 runtime
->avail_min
= params
->avail_min
;
683 int snd_rawmidi_output_params(struct snd_rawmidi_substream
*substream
,
684 struct snd_rawmidi_params
*params
)
686 if (substream
->append
&& substream
->use_count
> 1)
688 snd_rawmidi_drain_output(substream
);
689 substream
->active_sensing
= !params
->no_active_sensing
;
690 return resize_runtime_buffer(substream
->runtime
, params
, false);
692 EXPORT_SYMBOL(snd_rawmidi_output_params
);
694 int snd_rawmidi_input_params(struct snd_rawmidi_substream
*substream
,
695 struct snd_rawmidi_params
*params
)
697 snd_rawmidi_drain_input(substream
);
698 return resize_runtime_buffer(substream
->runtime
, params
, true);
700 EXPORT_SYMBOL(snd_rawmidi_input_params
);
702 static int snd_rawmidi_output_status(struct snd_rawmidi_substream
*substream
,
703 struct snd_rawmidi_status64
*status
)
705 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
707 memset(status
, 0, sizeof(*status
));
708 status
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
709 spin_lock_irq(&runtime
->lock
);
710 status
->avail
= runtime
->avail
;
711 spin_unlock_irq(&runtime
->lock
);
715 static int snd_rawmidi_input_status(struct snd_rawmidi_substream
*substream
,
716 struct snd_rawmidi_status64
*status
)
718 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
720 memset(status
, 0, sizeof(*status
));
721 status
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
722 spin_lock_irq(&runtime
->lock
);
723 status
->avail
= runtime
->avail
;
724 status
->xruns
= runtime
->xruns
;
726 spin_unlock_irq(&runtime
->lock
);
730 static int snd_rawmidi_ioctl_status32(struct snd_rawmidi_file
*rfile
,
731 struct snd_rawmidi_status32 __user
*argp
)
734 struct snd_rawmidi_status32 __user
*status
= argp
;
735 struct snd_rawmidi_status32 status32
;
736 struct snd_rawmidi_status64 status64
;
738 if (copy_from_user(&status32
, argp
,
739 sizeof(struct snd_rawmidi_status32
)))
742 switch (status32
.stream
) {
743 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
744 if (rfile
->output
== NULL
)
746 err
= snd_rawmidi_output_status(rfile
->output
, &status64
);
748 case SNDRV_RAWMIDI_STREAM_INPUT
:
749 if (rfile
->input
== NULL
)
751 err
= snd_rawmidi_input_status(rfile
->input
, &status64
);
759 status32
= (struct snd_rawmidi_status32
) {
760 .stream
= status64
.stream
,
761 .tstamp_sec
= status64
.tstamp_sec
,
762 .tstamp_nsec
= status64
.tstamp_nsec
,
763 .avail
= status64
.avail
,
764 .xruns
= status64
.xruns
,
767 if (copy_to_user(status
, &status32
, sizeof(*status
)))
773 static int snd_rawmidi_ioctl_status64(struct snd_rawmidi_file
*rfile
,
774 struct snd_rawmidi_status64 __user
*argp
)
777 struct snd_rawmidi_status64 status
;
779 if (copy_from_user(&status
, argp
, sizeof(struct snd_rawmidi_status64
)))
782 switch (status
.stream
) {
783 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
784 if (rfile
->output
== NULL
)
786 err
= snd_rawmidi_output_status(rfile
->output
, &status
);
788 case SNDRV_RAWMIDI_STREAM_INPUT
:
789 if (rfile
->input
== NULL
)
791 err
= snd_rawmidi_input_status(rfile
->input
, &status
);
798 if (copy_to_user(argp
, &status
,
799 sizeof(struct snd_rawmidi_status64
)))
804 static long snd_rawmidi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
806 struct snd_rawmidi_file
*rfile
;
807 void __user
*argp
= (void __user
*)arg
;
809 rfile
= file
->private_data
;
810 if (((cmd
>> 8) & 0xff) != 'W')
813 case SNDRV_RAWMIDI_IOCTL_PVERSION
:
814 return put_user(SNDRV_RAWMIDI_VERSION
, (int __user
*)argp
) ? -EFAULT
: 0;
815 case SNDRV_RAWMIDI_IOCTL_INFO
:
818 struct snd_rawmidi_info __user
*info
= argp
;
820 if (get_user(stream
, &info
->stream
))
823 case SNDRV_RAWMIDI_STREAM_INPUT
:
824 return snd_rawmidi_info_user(rfile
->input
, info
);
825 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
826 return snd_rawmidi_info_user(rfile
->output
, info
);
831 case SNDRV_RAWMIDI_IOCTL_PARAMS
:
833 struct snd_rawmidi_params params
;
835 if (copy_from_user(¶ms
, argp
, sizeof(struct snd_rawmidi_params
)))
837 switch (params
.stream
) {
838 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
839 if (rfile
->output
== NULL
)
841 return snd_rawmidi_output_params(rfile
->output
, ¶ms
);
842 case SNDRV_RAWMIDI_STREAM_INPUT
:
843 if (rfile
->input
== NULL
)
845 return snd_rawmidi_input_params(rfile
->input
, ¶ms
);
850 case SNDRV_RAWMIDI_IOCTL_STATUS32
:
851 return snd_rawmidi_ioctl_status32(rfile
, argp
);
852 case SNDRV_RAWMIDI_IOCTL_STATUS64
:
853 return snd_rawmidi_ioctl_status64(rfile
, argp
);
854 case SNDRV_RAWMIDI_IOCTL_DROP
:
858 if (get_user(val
, (int __user
*) argp
))
861 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
862 if (rfile
->output
== NULL
)
864 return snd_rawmidi_drop_output(rfile
->output
);
869 case SNDRV_RAWMIDI_IOCTL_DRAIN
:
873 if (get_user(val
, (int __user
*) argp
))
876 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
877 if (rfile
->output
== NULL
)
879 return snd_rawmidi_drain_output(rfile
->output
);
880 case SNDRV_RAWMIDI_STREAM_INPUT
:
881 if (rfile
->input
== NULL
)
883 return snd_rawmidi_drain_input(rfile
->input
);
889 rmidi_dbg(rfile
->rmidi
,
890 "rawmidi: unknown command = 0x%x\n", cmd
);
895 static int snd_rawmidi_control_ioctl(struct snd_card
*card
,
896 struct snd_ctl_file
*control
,
900 void __user
*argp
= (void __user
*)arg
;
903 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE
:
907 if (get_user(device
, (int __user
*)argp
))
909 if (device
>= SNDRV_RAWMIDI_DEVICES
) /* next device is -1 */
910 device
= SNDRV_RAWMIDI_DEVICES
- 1;
911 mutex_lock(®ister_mutex
);
912 device
= device
< 0 ? 0 : device
+ 1;
913 while (device
< SNDRV_RAWMIDI_DEVICES
) {
914 if (snd_rawmidi_search(card
, device
))
918 if (device
== SNDRV_RAWMIDI_DEVICES
)
920 mutex_unlock(®ister_mutex
);
921 if (put_user(device
, (int __user
*)argp
))
925 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE
:
929 if (get_user(val
, (int __user
*)argp
))
931 control
->preferred_subdevice
[SND_CTL_SUBDEV_RAWMIDI
] = val
;
934 case SNDRV_CTL_IOCTL_RAWMIDI_INFO
:
935 return snd_rawmidi_info_select_user(card
, argp
);
941 * snd_rawmidi_receive - receive the input data from the device
942 * @substream: the rawmidi substream
943 * @buffer: the buffer pointer
944 * @count: the data size to read
946 * Reads the data from the internal buffer.
948 * Return: The size of read data, or a negative error code on failure.
950 int snd_rawmidi_receive(struct snd_rawmidi_substream
*substream
,
951 const unsigned char *buffer
, int count
)
954 int result
= 0, count1
;
955 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
957 if (!substream
->opened
)
959 if (runtime
->buffer
== NULL
) {
960 rmidi_dbg(substream
->rmidi
,
961 "snd_rawmidi_receive: input is not active!!!\n");
964 spin_lock_irqsave(&runtime
->lock
, flags
);
965 if (count
== 1) { /* special case, faster code */
967 if (runtime
->avail
< runtime
->buffer_size
) {
968 runtime
->buffer
[runtime
->hw_ptr
++] = buffer
[0];
969 runtime
->hw_ptr
%= runtime
->buffer_size
;
976 substream
->bytes
+= count
;
977 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
980 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
981 count1
= runtime
->buffer_size
- runtime
->avail
;
982 memcpy(runtime
->buffer
+ runtime
->hw_ptr
, buffer
, count1
);
983 runtime
->hw_ptr
+= count1
;
984 runtime
->hw_ptr
%= runtime
->buffer_size
;
985 runtime
->avail
+= count1
;
991 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
)) {
992 count1
= runtime
->buffer_size
- runtime
->avail
;
993 runtime
->xruns
+= count
- count1
;
996 memcpy(runtime
->buffer
, buffer
, count1
);
997 runtime
->hw_ptr
= count1
;
998 runtime
->avail
+= count1
;
1005 schedule_work(&runtime
->event_work
);
1006 else if (snd_rawmidi_ready(substream
))
1007 wake_up(&runtime
->sleep
);
1009 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1012 EXPORT_SYMBOL(snd_rawmidi_receive
);
1014 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream
*substream
,
1015 unsigned char __user
*userbuf
,
1016 unsigned char *kernelbuf
, long count
)
1018 unsigned long flags
;
1019 long result
= 0, count1
;
1020 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1021 unsigned long appl_ptr
;
1023 spin_lock_irqsave(&runtime
->lock
, flags
);
1024 while (count
> 0 && runtime
->avail
) {
1025 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1028 if (count1
> (int)runtime
->avail
)
1029 count1
= runtime
->avail
;
1031 /* update runtime->appl_ptr before unlocking for userbuf */
1032 appl_ptr
= runtime
->appl_ptr
;
1033 runtime
->appl_ptr
+= count1
;
1034 runtime
->appl_ptr
%= runtime
->buffer_size
;
1035 runtime
->avail
-= count1
;
1038 memcpy(kernelbuf
+ result
, runtime
->buffer
+ appl_ptr
, count1
);
1040 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1041 if (copy_to_user(userbuf
+ result
,
1042 runtime
->buffer
+ appl_ptr
, count1
)) {
1043 return result
> 0 ? result
: -EFAULT
;
1045 spin_lock_irqsave(&runtime
->lock
, flags
);
1050 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1054 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream
*substream
,
1055 unsigned char *buf
, long count
)
1057 snd_rawmidi_input_trigger(substream
, 1);
1058 return snd_rawmidi_kernel_read1(substream
, NULL
/*userbuf*/, buf
, count
);
1060 EXPORT_SYMBOL(snd_rawmidi_kernel_read
);
1062 static ssize_t
snd_rawmidi_read(struct file
*file
, char __user
*buf
, size_t count
,
1067 struct snd_rawmidi_file
*rfile
;
1068 struct snd_rawmidi_substream
*substream
;
1069 struct snd_rawmidi_runtime
*runtime
;
1071 rfile
= file
->private_data
;
1072 substream
= rfile
->input
;
1073 if (substream
== NULL
)
1075 runtime
= substream
->runtime
;
1076 snd_rawmidi_input_trigger(substream
, 1);
1079 spin_lock_irq(&runtime
->lock
);
1080 while (!snd_rawmidi_ready(substream
)) {
1081 wait_queue_entry_t wait
;
1083 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1084 spin_unlock_irq(&runtime
->lock
);
1085 return result
> 0 ? result
: -EAGAIN
;
1087 init_waitqueue_entry(&wait
, current
);
1088 add_wait_queue(&runtime
->sleep
, &wait
);
1089 set_current_state(TASK_INTERRUPTIBLE
);
1090 spin_unlock_irq(&runtime
->lock
);
1092 remove_wait_queue(&runtime
->sleep
, &wait
);
1093 if (rfile
->rmidi
->card
->shutdown
)
1095 if (signal_pending(current
))
1096 return result
> 0 ? result
: -ERESTARTSYS
;
1097 if (!runtime
->avail
)
1098 return result
> 0 ? result
: -EIO
;
1099 spin_lock_irq(&runtime
->lock
);
1101 spin_unlock_irq(&runtime
->lock
);
1102 count1
= snd_rawmidi_kernel_read1(substream
,
1103 (unsigned char __user
*)buf
,
1107 return result
> 0 ? result
: count1
;
1116 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1117 * @substream: the rawmidi substream
1119 * Return: 1 if the internal output buffer is empty, 0 if not.
1121 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream
*substream
)
1123 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1125 unsigned long flags
;
1127 if (runtime
->buffer
== NULL
) {
1128 rmidi_dbg(substream
->rmidi
,
1129 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1132 spin_lock_irqsave(&runtime
->lock
, flags
);
1133 result
= runtime
->avail
>= runtime
->buffer_size
;
1134 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1137 EXPORT_SYMBOL(snd_rawmidi_transmit_empty
);
1140 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1141 * @substream: the rawmidi substream
1142 * @buffer: the buffer pointer
1143 * @count: data size to transfer
1145 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1147 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1148 unsigned char *buffer
, int count
)
1151 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1153 if (runtime
->buffer
== NULL
) {
1154 rmidi_dbg(substream
->rmidi
,
1155 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1159 if (runtime
->avail
>= runtime
->buffer_size
) {
1160 /* warning: lowlevel layer MUST trigger down the hardware */
1163 if (count
== 1) { /* special case, faster code */
1164 *buffer
= runtime
->buffer
[runtime
->hw_ptr
];
1167 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
1170 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
1171 count1
= runtime
->buffer_size
- runtime
->avail
;
1172 memcpy(buffer
, runtime
->buffer
+ runtime
->hw_ptr
, count1
);
1176 if (count
> (int)(runtime
->buffer_size
- runtime
->avail
- count1
))
1177 count
= runtime
->buffer_size
- runtime
->avail
- count1
;
1178 memcpy(buffer
+ count1
, runtime
->buffer
, count
);
1185 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek
);
1188 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1189 * @substream: the rawmidi substream
1190 * @buffer: the buffer pointer
1191 * @count: data size to transfer
1193 * Copies data from the internal output buffer to the given buffer.
1195 * Call this in the interrupt handler when the midi output is ready,
1196 * and call snd_rawmidi_transmit_ack() after the transmission is
1199 * Return: The size of copied data, or a negative error code on failure.
1201 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1202 unsigned char *buffer
, int count
)
1204 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1206 unsigned long flags
;
1208 spin_lock_irqsave(&runtime
->lock
, flags
);
1209 result
= __snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1210 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1213 EXPORT_SYMBOL(snd_rawmidi_transmit_peek
);
1216 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1217 * @substream: the rawmidi substream
1218 * @count: the transferred count
1220 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1222 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1224 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1226 if (runtime
->buffer
== NULL
) {
1227 rmidi_dbg(substream
->rmidi
,
1228 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1231 snd_BUG_ON(runtime
->avail
+ count
> runtime
->buffer_size
);
1232 runtime
->hw_ptr
+= count
;
1233 runtime
->hw_ptr
%= runtime
->buffer_size
;
1234 runtime
->avail
+= count
;
1235 substream
->bytes
+= count
;
1237 if (runtime
->drain
|| snd_rawmidi_ready(substream
))
1238 wake_up(&runtime
->sleep
);
1242 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack
);
1245 * snd_rawmidi_transmit_ack - acknowledge the transmission
1246 * @substream: the rawmidi substream
1247 * @count: the transferred count
1249 * Advances the hardware pointer for the internal output buffer with
1250 * the given size and updates the condition.
1251 * Call after the transmission is finished.
1253 * Return: The advanced size if successful, or a negative error code on failure.
1255 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1257 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1259 unsigned long flags
;
1261 spin_lock_irqsave(&runtime
->lock
, flags
);
1262 result
= __snd_rawmidi_transmit_ack(substream
, count
);
1263 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1266 EXPORT_SYMBOL(snd_rawmidi_transmit_ack
);
1269 * snd_rawmidi_transmit - copy from the buffer to the device
1270 * @substream: the rawmidi substream
1271 * @buffer: the buffer pointer
1272 * @count: the data size to transfer
1274 * Copies data from the buffer to the device and advances the pointer.
1276 * Return: The copied size if successful, or a negative error code on failure.
1278 int snd_rawmidi_transmit(struct snd_rawmidi_substream
*substream
,
1279 unsigned char *buffer
, int count
)
1281 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1283 unsigned long flags
;
1285 spin_lock_irqsave(&runtime
->lock
, flags
);
1286 if (!substream
->opened
)
1289 count
= __snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1293 result
= __snd_rawmidi_transmit_ack(substream
, count
);
1295 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1298 EXPORT_SYMBOL(snd_rawmidi_transmit
);
1301 * snd_rawmidi_proceed - Discard the all pending bytes and proceed
1302 * @substream: rawmidi substream
1304 * Return: the number of discarded bytes
1306 int snd_rawmidi_proceed(struct snd_rawmidi_substream
*substream
)
1308 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1309 unsigned long flags
;
1312 spin_lock_irqsave(&runtime
->lock
, flags
);
1313 if (runtime
->avail
< runtime
->buffer_size
) {
1314 count
= runtime
->buffer_size
- runtime
->avail
;
1315 __snd_rawmidi_transmit_ack(substream
, count
);
1317 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1320 EXPORT_SYMBOL(snd_rawmidi_proceed
);
1322 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream
*substream
,
1323 const unsigned char __user
*userbuf
,
1324 const unsigned char *kernelbuf
,
1327 unsigned long flags
;
1328 long count1
, result
;
1329 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1330 unsigned long appl_ptr
;
1332 if (!kernelbuf
&& !userbuf
)
1334 if (snd_BUG_ON(!runtime
->buffer
))
1338 spin_lock_irqsave(&runtime
->lock
, flags
);
1339 if (substream
->append
) {
1340 if ((long)runtime
->avail
< count
) {
1341 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1345 while (count
> 0 && runtime
->avail
> 0) {
1346 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1349 if (count1
> (long)runtime
->avail
)
1350 count1
= runtime
->avail
;
1352 /* update runtime->appl_ptr before unlocking for userbuf */
1353 appl_ptr
= runtime
->appl_ptr
;
1354 runtime
->appl_ptr
+= count1
;
1355 runtime
->appl_ptr
%= runtime
->buffer_size
;
1356 runtime
->avail
-= count1
;
1359 memcpy(runtime
->buffer
+ appl_ptr
,
1360 kernelbuf
+ result
, count1
);
1362 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1363 if (copy_from_user(runtime
->buffer
+ appl_ptr
,
1364 userbuf
+ result
, count1
)) {
1365 spin_lock_irqsave(&runtime
->lock
, flags
);
1366 result
= result
> 0 ? result
: -EFAULT
;
1369 spin_lock_irqsave(&runtime
->lock
, flags
);
1375 count1
= runtime
->avail
< runtime
->buffer_size
;
1376 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1378 snd_rawmidi_output_trigger(substream
, 1);
1382 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream
*substream
,
1383 const unsigned char *buf
, long count
)
1385 return snd_rawmidi_kernel_write1(substream
, NULL
, buf
, count
);
1387 EXPORT_SYMBOL(snd_rawmidi_kernel_write
);
1389 static ssize_t
snd_rawmidi_write(struct file
*file
, const char __user
*buf
,
1390 size_t count
, loff_t
*offset
)
1392 long result
, timeout
;
1394 struct snd_rawmidi_file
*rfile
;
1395 struct snd_rawmidi_runtime
*runtime
;
1396 struct snd_rawmidi_substream
*substream
;
1398 rfile
= file
->private_data
;
1399 substream
= rfile
->output
;
1400 runtime
= substream
->runtime
;
1401 /* we cannot put an atomic message to our buffer */
1402 if (substream
->append
&& count
> runtime
->buffer_size
)
1406 spin_lock_irq(&runtime
->lock
);
1407 while (!snd_rawmidi_ready_append(substream
, count
)) {
1408 wait_queue_entry_t wait
;
1410 if (file
->f_flags
& O_NONBLOCK
) {
1411 spin_unlock_irq(&runtime
->lock
);
1412 return result
> 0 ? result
: -EAGAIN
;
1414 init_waitqueue_entry(&wait
, current
);
1415 add_wait_queue(&runtime
->sleep
, &wait
);
1416 set_current_state(TASK_INTERRUPTIBLE
);
1417 spin_unlock_irq(&runtime
->lock
);
1418 timeout
= schedule_timeout(30 * HZ
);
1419 remove_wait_queue(&runtime
->sleep
, &wait
);
1420 if (rfile
->rmidi
->card
->shutdown
)
1422 if (signal_pending(current
))
1423 return result
> 0 ? result
: -ERESTARTSYS
;
1424 if (!runtime
->avail
&& !timeout
)
1425 return result
> 0 ? result
: -EIO
;
1426 spin_lock_irq(&runtime
->lock
);
1428 spin_unlock_irq(&runtime
->lock
);
1429 count1
= snd_rawmidi_kernel_write1(substream
, buf
, NULL
, count
);
1431 return result
> 0 ? result
: count1
;
1434 if ((size_t)count1
< count
&& (file
->f_flags
& O_NONBLOCK
))
1438 if (file
->f_flags
& O_DSYNC
) {
1439 spin_lock_irq(&runtime
->lock
);
1440 while (runtime
->avail
!= runtime
->buffer_size
) {
1441 wait_queue_entry_t wait
;
1442 unsigned int last_avail
= runtime
->avail
;
1444 init_waitqueue_entry(&wait
, current
);
1445 add_wait_queue(&runtime
->sleep
, &wait
);
1446 set_current_state(TASK_INTERRUPTIBLE
);
1447 spin_unlock_irq(&runtime
->lock
);
1448 timeout
= schedule_timeout(30 * HZ
);
1449 remove_wait_queue(&runtime
->sleep
, &wait
);
1450 if (signal_pending(current
))
1451 return result
> 0 ? result
: -ERESTARTSYS
;
1452 if (runtime
->avail
== last_avail
&& !timeout
)
1453 return result
> 0 ? result
: -EIO
;
1454 spin_lock_irq(&runtime
->lock
);
1456 spin_unlock_irq(&runtime
->lock
);
1461 static __poll_t
snd_rawmidi_poll(struct file
*file
, poll_table
*wait
)
1463 struct snd_rawmidi_file
*rfile
;
1464 struct snd_rawmidi_runtime
*runtime
;
1467 rfile
= file
->private_data
;
1468 if (rfile
->input
!= NULL
) {
1469 runtime
= rfile
->input
->runtime
;
1470 snd_rawmidi_input_trigger(rfile
->input
, 1);
1471 poll_wait(file
, &runtime
->sleep
, wait
);
1473 if (rfile
->output
!= NULL
) {
1474 runtime
= rfile
->output
->runtime
;
1475 poll_wait(file
, &runtime
->sleep
, wait
);
1478 if (rfile
->input
!= NULL
) {
1479 if (snd_rawmidi_ready(rfile
->input
))
1480 mask
|= EPOLLIN
| EPOLLRDNORM
;
1482 if (rfile
->output
!= NULL
) {
1483 if (snd_rawmidi_ready(rfile
->output
))
1484 mask
|= EPOLLOUT
| EPOLLWRNORM
;
1491 #ifdef CONFIG_COMPAT
1492 #include "rawmidi_compat.c"
1494 #define snd_rawmidi_ioctl_compat NULL
1500 static void snd_rawmidi_proc_info_read(struct snd_info_entry
*entry
,
1501 struct snd_info_buffer
*buffer
)
1503 struct snd_rawmidi
*rmidi
;
1504 struct snd_rawmidi_substream
*substream
;
1505 struct snd_rawmidi_runtime
*runtime
;
1507 rmidi
= entry
->private_data
;
1508 snd_iprintf(buffer
, "%s\n\n", rmidi
->name
);
1509 mutex_lock(&rmidi
->open_mutex
);
1510 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) {
1511 list_for_each_entry(substream
,
1512 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
,
1516 " Tx bytes : %lu\n",
1518 (unsigned long) substream
->bytes
);
1519 if (substream
->opened
) {
1521 " Owner PID : %d\n",
1522 pid_vnr(substream
->pid
));
1523 runtime
= substream
->runtime
;
1526 " Buffer size : %lu\n"
1528 runtime
->oss
? "OSS compatible" : "native",
1529 (unsigned long) runtime
->buffer_size
,
1530 (unsigned long) runtime
->avail
);
1534 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
) {
1535 list_for_each_entry(substream
,
1536 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
,
1540 " Rx bytes : %lu\n",
1542 (unsigned long) substream
->bytes
);
1543 if (substream
->opened
) {
1545 " Owner PID : %d\n",
1546 pid_vnr(substream
->pid
));
1547 runtime
= substream
->runtime
;
1549 " Buffer size : %lu\n"
1551 " Overruns : %lu\n",
1552 (unsigned long) runtime
->buffer_size
,
1553 (unsigned long) runtime
->avail
,
1554 (unsigned long) runtime
->xruns
);
1558 mutex_unlock(&rmidi
->open_mutex
);
1562 * Register functions
1565 static const struct file_operations snd_rawmidi_f_ops
= {
1566 .owner
= THIS_MODULE
,
1567 .read
= snd_rawmidi_read
,
1568 .write
= snd_rawmidi_write
,
1569 .open
= snd_rawmidi_open
,
1570 .release
= snd_rawmidi_release
,
1571 .llseek
= no_llseek
,
1572 .poll
= snd_rawmidi_poll
,
1573 .unlocked_ioctl
= snd_rawmidi_ioctl
,
1574 .compat_ioctl
= snd_rawmidi_ioctl_compat
,
1577 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi
*rmidi
,
1578 struct snd_rawmidi_str
*stream
,
1582 struct snd_rawmidi_substream
*substream
;
1585 for (idx
= 0; idx
< count
; idx
++) {
1586 substream
= kzalloc(sizeof(*substream
), GFP_KERNEL
);
1589 substream
->stream
= direction
;
1590 substream
->number
= idx
;
1591 substream
->rmidi
= rmidi
;
1592 substream
->pstr
= stream
;
1593 list_add_tail(&substream
->list
, &stream
->substreams
);
1594 stream
->substream_count
++;
1599 static void release_rawmidi_device(struct device
*dev
)
1601 kfree(container_of(dev
, struct snd_rawmidi
, dev
));
1605 * snd_rawmidi_new - create a rawmidi instance
1606 * @card: the card instance
1607 * @id: the id string
1608 * @device: the device index
1609 * @output_count: the number of output streams
1610 * @input_count: the number of input streams
1611 * @rrawmidi: the pointer to store the new rawmidi instance
1613 * Creates a new rawmidi instance.
1614 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1616 * Return: Zero if successful, or a negative error code on failure.
1618 int snd_rawmidi_new(struct snd_card
*card
, char *id
, int device
,
1619 int output_count
, int input_count
,
1620 struct snd_rawmidi
**rrawmidi
)
1622 struct snd_rawmidi
*rmidi
;
1624 static const struct snd_device_ops ops
= {
1625 .dev_free
= snd_rawmidi_dev_free
,
1626 .dev_register
= snd_rawmidi_dev_register
,
1627 .dev_disconnect
= snd_rawmidi_dev_disconnect
,
1630 if (snd_BUG_ON(!card
))
1634 rmidi
= kzalloc(sizeof(*rmidi
), GFP_KERNEL
);
1638 rmidi
->device
= device
;
1639 mutex_init(&rmidi
->open_mutex
);
1640 init_waitqueue_head(&rmidi
->open_wait
);
1641 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
);
1642 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
);
1645 strlcpy(rmidi
->id
, id
, sizeof(rmidi
->id
));
1647 snd_device_initialize(&rmidi
->dev
, card
);
1648 rmidi
->dev
.release
= release_rawmidi_device
;
1649 dev_set_name(&rmidi
->dev
, "midiC%iD%i", card
->number
, device
);
1651 err
= snd_rawmidi_alloc_substreams(rmidi
,
1652 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
],
1653 SNDRV_RAWMIDI_STREAM_INPUT
,
1657 err
= snd_rawmidi_alloc_substreams(rmidi
,
1658 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
],
1659 SNDRV_RAWMIDI_STREAM_OUTPUT
,
1663 err
= snd_device_new(card
, SNDRV_DEV_RAWMIDI
, rmidi
, &ops
);
1672 snd_rawmidi_free(rmidi
);
1675 EXPORT_SYMBOL(snd_rawmidi_new
);
1677 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str
*stream
)
1679 struct snd_rawmidi_substream
*substream
;
1681 while (!list_empty(&stream
->substreams
)) {
1682 substream
= list_entry(stream
->substreams
.next
, struct snd_rawmidi_substream
, list
);
1683 list_del(&substream
->list
);
1688 static int snd_rawmidi_free(struct snd_rawmidi
*rmidi
)
1693 snd_info_free_entry(rmidi
->proc_entry
);
1694 rmidi
->proc_entry
= NULL
;
1695 mutex_lock(®ister_mutex
);
1696 if (rmidi
->ops
&& rmidi
->ops
->dev_unregister
)
1697 rmidi
->ops
->dev_unregister(rmidi
);
1698 mutex_unlock(®ister_mutex
);
1700 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
1701 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
1702 if (rmidi
->private_free
)
1703 rmidi
->private_free(rmidi
);
1704 put_device(&rmidi
->dev
);
1708 static int snd_rawmidi_dev_free(struct snd_device
*device
)
1710 struct snd_rawmidi
*rmidi
= device
->device_data
;
1712 return snd_rawmidi_free(rmidi
);
1715 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1716 static void snd_rawmidi_dev_seq_free(struct snd_seq_device
*device
)
1718 struct snd_rawmidi
*rmidi
= device
->private_data
;
1720 rmidi
->seq_dev
= NULL
;
1724 static int snd_rawmidi_dev_register(struct snd_device
*device
)
1727 struct snd_info_entry
*entry
;
1729 struct snd_rawmidi
*rmidi
= device
->device_data
;
1731 if (rmidi
->device
>= SNDRV_RAWMIDI_DEVICES
)
1734 mutex_lock(®ister_mutex
);
1735 if (snd_rawmidi_search(rmidi
->card
, rmidi
->device
))
1738 list_add_tail(&rmidi
->list
, &snd_rawmidi_devices
);
1739 mutex_unlock(®ister_mutex
);
1743 err
= snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI
,
1744 rmidi
->card
, rmidi
->device
,
1745 &snd_rawmidi_f_ops
, rmidi
, &rmidi
->dev
);
1747 rmidi_err(rmidi
, "unable to register\n");
1750 if (rmidi
->ops
&& rmidi
->ops
->dev_register
) {
1751 err
= rmidi
->ops
->dev_register(rmidi
);
1753 goto error_unregister
;
1755 #ifdef CONFIG_SND_OSSEMUL
1757 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1758 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1759 rmidi
->card
, 0, &snd_rawmidi_f_ops
,
1762 "unable to register OSS rawmidi device %i:%i\n",
1763 rmidi
->card
->number
, 0);
1766 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1767 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
, rmidi
->name
);
1771 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
]) {
1772 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1773 rmidi
->card
, 1, &snd_rawmidi_f_ops
,
1776 "unable to register OSS rawmidi device %i:%i\n",
1777 rmidi
->card
->number
, 1);
1782 #endif /* CONFIG_SND_OSSEMUL */
1783 sprintf(name
, "midi%d", rmidi
->device
);
1784 entry
= snd_info_create_card_entry(rmidi
->card
, name
, rmidi
->card
->proc_root
);
1786 entry
->private_data
= rmidi
;
1787 entry
->c
.text
.read
= snd_rawmidi_proc_info_read
;
1788 if (snd_info_register(entry
) < 0) {
1789 snd_info_free_entry(entry
);
1793 rmidi
->proc_entry
= entry
;
1794 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1795 if (!rmidi
->ops
|| !rmidi
->ops
->dev_register
) { /* own registration mechanism */
1796 if (snd_seq_device_new(rmidi
->card
, rmidi
->device
, SNDRV_SEQ_DEV_ID_MIDISYNTH
, 0, &rmidi
->seq_dev
) >= 0) {
1797 rmidi
->seq_dev
->private_data
= rmidi
;
1798 rmidi
->seq_dev
->private_free
= snd_rawmidi_dev_seq_free
;
1799 sprintf(rmidi
->seq_dev
->name
, "MIDI %d-%d", rmidi
->card
->number
, rmidi
->device
);
1800 snd_device_register(rmidi
->card
, rmidi
->seq_dev
);
1807 snd_unregister_device(&rmidi
->dev
);
1809 mutex_lock(®ister_mutex
);
1810 list_del(&rmidi
->list
);
1811 mutex_unlock(®ister_mutex
);
1815 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
)
1817 struct snd_rawmidi
*rmidi
= device
->device_data
;
1820 mutex_lock(®ister_mutex
);
1821 mutex_lock(&rmidi
->open_mutex
);
1822 wake_up(&rmidi
->open_wait
);
1823 list_del_init(&rmidi
->list
);
1824 for (dir
= 0; dir
< 2; dir
++) {
1825 struct snd_rawmidi_substream
*s
;
1827 list_for_each_entry(s
, &rmidi
->streams
[dir
].substreams
, list
) {
1829 wake_up(&s
->runtime
->sleep
);
1833 #ifdef CONFIG_SND_OSSEMUL
1834 if (rmidi
->ossreg
) {
1835 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1836 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 0);
1837 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1838 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
);
1841 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
])
1842 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 1);
1845 #endif /* CONFIG_SND_OSSEMUL */
1846 snd_unregister_device(&rmidi
->dev
);
1847 mutex_unlock(&rmidi
->open_mutex
);
1848 mutex_unlock(®ister_mutex
);
1853 * snd_rawmidi_set_ops - set the rawmidi operators
1854 * @rmidi: the rawmidi instance
1855 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1856 * @ops: the operator table
1858 * Sets the rawmidi operators for the given stream direction.
1860 void snd_rawmidi_set_ops(struct snd_rawmidi
*rmidi
, int stream
,
1861 const struct snd_rawmidi_ops
*ops
)
1863 struct snd_rawmidi_substream
*substream
;
1865 list_for_each_entry(substream
, &rmidi
->streams
[stream
].substreams
, list
)
1866 substream
->ops
= ops
;
1868 EXPORT_SYMBOL(snd_rawmidi_set_ops
);
1874 static int __init
alsa_rawmidi_init(void)
1877 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl
);
1878 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl
);
1879 #ifdef CONFIG_SND_OSSEMUL
1881 /* check device map table */
1882 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1883 if (midi_map
[i
] < 0 || midi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1884 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1888 if (amidi_map
[i
] < 0 || amidi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1889 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1895 #endif /* CONFIG_SND_OSSEMUL */
1899 static void __exit
alsa_rawmidi_exit(void)
1901 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl
);
1902 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl
);
1905 module_init(alsa_rawmidi_init
)
1906 module_exit(alsa_rawmidi_exit
)