1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Abstract layer for MIDI v1.0 stream
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <sound/core.h>
8 #include <linux/major.h>
9 #include <linux/init.h>
10 #include <linux/sched/signal.h>
11 #include <linux/slab.h>
12 #include <linux/time.h>
13 #include <linux/wait.h>
14 #include <linux/mutex.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
18 #include <linux/nospec.h>
19 #include <sound/rawmidi.h>
20 #include <sound/info.h>
21 #include <sound/control.h>
22 #include <sound/minors.h>
23 #include <sound/initval.h>
25 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
26 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
27 MODULE_LICENSE("GPL");
29 #ifdef CONFIG_SND_OSSEMUL
30 static int midi_map
[SNDRV_CARDS
];
31 static int amidi_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
32 module_param_array(midi_map
, int, NULL
, 0444);
33 MODULE_PARM_DESC(midi_map
, "Raw MIDI device number assigned to 1st OSS device.");
34 module_param_array(amidi_map
, int, NULL
, 0444);
35 MODULE_PARM_DESC(amidi_map
, "Raw MIDI device number assigned to 2nd OSS device.");
36 #endif /* CONFIG_SND_OSSEMUL */
38 static int snd_rawmidi_free(struct snd_rawmidi
*rawmidi
);
39 static int snd_rawmidi_dev_free(struct snd_device
*device
);
40 static int snd_rawmidi_dev_register(struct snd_device
*device
);
41 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
);
43 static LIST_HEAD(snd_rawmidi_devices
);
44 static DEFINE_MUTEX(register_mutex
);
46 #define rmidi_err(rmidi, fmt, args...) \
47 dev_err(&(rmidi)->dev, fmt, ##args)
48 #define rmidi_warn(rmidi, fmt, args...) \
49 dev_warn(&(rmidi)->dev, fmt, ##args)
50 #define rmidi_dbg(rmidi, fmt, args...) \
51 dev_dbg(&(rmidi)->dev, fmt, ##args)
53 static struct snd_rawmidi
*snd_rawmidi_search(struct snd_card
*card
, int device
)
55 struct snd_rawmidi
*rawmidi
;
57 list_for_each_entry(rawmidi
, &snd_rawmidi_devices
, list
)
58 if (rawmidi
->card
== card
&& rawmidi
->device
== device
)
63 static inline unsigned short snd_rawmidi_file_flags(struct file
*file
)
65 switch (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) {
67 return SNDRV_RAWMIDI_LFLG_OUTPUT
;
69 return SNDRV_RAWMIDI_LFLG_INPUT
;
71 return SNDRV_RAWMIDI_LFLG_OPEN
;
75 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream
*substream
)
77 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
79 return runtime
->avail
>= runtime
->avail_min
;
82 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream
*substream
,
85 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
87 return runtime
->avail
>= runtime
->avail_min
&&
88 (!substream
->append
|| runtime
->avail
>= count
);
91 static void snd_rawmidi_input_event_work(struct work_struct
*work
)
93 struct snd_rawmidi_runtime
*runtime
=
94 container_of(work
, struct snd_rawmidi_runtime
, event_work
);
97 runtime
->event(runtime
->substream
);
100 /* buffer refcount management: call with runtime->lock held */
101 static inline void snd_rawmidi_buffer_ref(struct snd_rawmidi_runtime
*runtime
)
103 runtime
->buffer_ref
++;
106 static inline void snd_rawmidi_buffer_unref(struct snd_rawmidi_runtime
*runtime
)
108 runtime
->buffer_ref
--;
111 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream
*substream
)
113 struct snd_rawmidi_runtime
*runtime
;
115 runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
);
118 runtime
->substream
= substream
;
119 spin_lock_init(&runtime
->lock
);
120 init_waitqueue_head(&runtime
->sleep
);
121 INIT_WORK(&runtime
->event_work
, snd_rawmidi_input_event_work
);
122 runtime
->event
= NULL
;
123 runtime
->buffer_size
= PAGE_SIZE
;
124 runtime
->avail_min
= 1;
125 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
128 runtime
->avail
= runtime
->buffer_size
;
129 runtime
->buffer
= kvzalloc(runtime
->buffer_size
, GFP_KERNEL
);
130 if (!runtime
->buffer
) {
134 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
135 substream
->runtime
= runtime
;
139 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream
*substream
)
141 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
143 kvfree(runtime
->buffer
);
145 substream
->runtime
= NULL
;
149 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream
*substream
, int up
)
151 if (!substream
->opened
)
153 substream
->ops
->trigger(substream
, up
);
156 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
158 if (!substream
->opened
)
160 substream
->ops
->trigger(substream
, up
);
162 cancel_work_sync(&substream
->runtime
->event_work
);
165 static void __reset_runtime_ptrs(struct snd_rawmidi_runtime
*runtime
,
169 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
170 runtime
->avail
= is_input
? 0 : runtime
->buffer_size
;
173 static void reset_runtime_ptrs(struct snd_rawmidi_runtime
*runtime
,
178 spin_lock_irqsave(&runtime
->lock
, flags
);
179 __reset_runtime_ptrs(runtime
, is_input
);
180 spin_unlock_irqrestore(&runtime
->lock
, flags
);
183 int snd_rawmidi_drop_output(struct snd_rawmidi_substream
*substream
)
185 snd_rawmidi_output_trigger(substream
, 0);
186 reset_runtime_ptrs(substream
->runtime
, false);
189 EXPORT_SYMBOL(snd_rawmidi_drop_output
);
191 int snd_rawmidi_drain_output(struct snd_rawmidi_substream
*substream
)
195 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
199 timeout
= wait_event_interruptible_timeout(runtime
->sleep
,
200 (runtime
->avail
>= runtime
->buffer_size
),
202 if (signal_pending(current
))
204 if (runtime
->avail
< runtime
->buffer_size
&& !timeout
) {
205 rmidi_warn(substream
->rmidi
,
206 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
207 (long)runtime
->avail
, (long)runtime
->buffer_size
);
211 if (err
!= -ERESTARTSYS
) {
212 /* we need wait a while to make sure that Tx FIFOs are empty */
213 if (substream
->ops
->drain
)
214 substream
->ops
->drain(substream
);
217 snd_rawmidi_drop_output(substream
);
221 EXPORT_SYMBOL(snd_rawmidi_drain_output
);
223 int snd_rawmidi_drain_input(struct snd_rawmidi_substream
*substream
)
225 snd_rawmidi_input_trigger(substream
, 0);
226 reset_runtime_ptrs(substream
->runtime
, true);
229 EXPORT_SYMBOL(snd_rawmidi_drain_input
);
231 /* look for an available substream for the given stream direction;
232 * if a specific subdevice is given, try to assign it
234 static int assign_substream(struct snd_rawmidi
*rmidi
, int subdevice
,
235 int stream
, int mode
,
236 struct snd_rawmidi_substream
**sub_ret
)
238 struct snd_rawmidi_substream
*substream
;
239 struct snd_rawmidi_str
*s
= &rmidi
->streams
[stream
];
240 static unsigned int info_flags
[2] = {
241 [SNDRV_RAWMIDI_STREAM_OUTPUT
] = SNDRV_RAWMIDI_INFO_OUTPUT
,
242 [SNDRV_RAWMIDI_STREAM_INPUT
] = SNDRV_RAWMIDI_INFO_INPUT
,
245 if (!(rmidi
->info_flags
& info_flags
[stream
]))
247 if (subdevice
>= 0 && subdevice
>= s
->substream_count
)
250 list_for_each_entry(substream
, &s
->substreams
, list
) {
251 if (substream
->opened
) {
252 if (stream
== SNDRV_RAWMIDI_STREAM_INPUT
||
253 !(mode
& SNDRV_RAWMIDI_LFLG_APPEND
) ||
257 if (subdevice
< 0 || subdevice
== substream
->number
) {
258 *sub_ret
= substream
;
265 /* open and do ref-counting for the given substream */
266 static int open_substream(struct snd_rawmidi
*rmidi
,
267 struct snd_rawmidi_substream
*substream
,
272 if (substream
->use_count
== 0) {
273 err
= snd_rawmidi_runtime_create(substream
);
276 err
= substream
->ops
->open(substream
);
278 snd_rawmidi_runtime_free(substream
);
281 substream
->opened
= 1;
282 substream
->active_sensing
= 0;
283 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
)
284 substream
->append
= 1;
285 substream
->pid
= get_pid(task_pid(current
));
286 rmidi
->streams
[substream
->stream
].substream_opened
++;
288 substream
->use_count
++;
292 static void close_substream(struct snd_rawmidi
*rmidi
,
293 struct snd_rawmidi_substream
*substream
,
296 static int rawmidi_open_priv(struct snd_rawmidi
*rmidi
, int subdevice
, int mode
,
297 struct snd_rawmidi_file
*rfile
)
299 struct snd_rawmidi_substream
*sinput
= NULL
, *soutput
= NULL
;
302 rfile
->input
= rfile
->output
= NULL
;
303 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
304 err
= assign_substream(rmidi
, subdevice
,
305 SNDRV_RAWMIDI_STREAM_INPUT
,
310 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
311 err
= assign_substream(rmidi
, subdevice
,
312 SNDRV_RAWMIDI_STREAM_OUTPUT
,
319 err
= open_substream(rmidi
, sinput
, mode
);
324 err
= open_substream(rmidi
, soutput
, mode
);
327 close_substream(rmidi
, sinput
, 0);
332 rfile
->rmidi
= rmidi
;
333 rfile
->input
= sinput
;
334 rfile
->output
= soutput
;
338 /* called from sound/core/seq/seq_midi.c */
339 int snd_rawmidi_kernel_open(struct snd_card
*card
, int device
, int subdevice
,
340 int mode
, struct snd_rawmidi_file
*rfile
)
342 struct snd_rawmidi
*rmidi
;
345 if (snd_BUG_ON(!rfile
))
348 mutex_lock(®ister_mutex
);
349 rmidi
= snd_rawmidi_search(card
, device
);
352 else if (!try_module_get(rmidi
->card
->module
))
354 mutex_unlock(®ister_mutex
);
358 mutex_lock(&rmidi
->open_mutex
);
359 err
= rawmidi_open_priv(rmidi
, subdevice
, mode
, rfile
);
360 mutex_unlock(&rmidi
->open_mutex
);
362 module_put(rmidi
->card
->module
);
365 EXPORT_SYMBOL(snd_rawmidi_kernel_open
);
367 static int snd_rawmidi_open(struct inode
*inode
, struct file
*file
)
369 int maj
= imajor(inode
);
370 struct snd_card
*card
;
372 unsigned short fflags
;
374 struct snd_rawmidi
*rmidi
;
375 struct snd_rawmidi_file
*rawmidi_file
= NULL
;
376 wait_queue_entry_t wait
;
378 if ((file
->f_flags
& O_APPEND
) && !(file
->f_flags
& O_NONBLOCK
))
379 return -EINVAL
; /* invalid combination */
381 err
= stream_open(inode
, file
);
385 if (maj
== snd_major
) {
386 rmidi
= snd_lookup_minor_data(iminor(inode
),
387 SNDRV_DEVICE_TYPE_RAWMIDI
);
388 #ifdef CONFIG_SND_OSSEMUL
389 } else if (maj
== SOUND_MAJOR
) {
390 rmidi
= snd_lookup_oss_minor_data(iminor(inode
),
391 SNDRV_OSS_DEVICE_TYPE_MIDI
);
399 if (!try_module_get(rmidi
->card
->module
)) {
400 snd_card_unref(rmidi
->card
);
404 mutex_lock(&rmidi
->open_mutex
);
406 err
= snd_card_file_add(card
, file
);
409 fflags
= snd_rawmidi_file_flags(file
);
410 if ((file
->f_flags
& O_APPEND
) || maj
== SOUND_MAJOR
) /* OSS emul? */
411 fflags
|= SNDRV_RAWMIDI_LFLG_APPEND
;
412 rawmidi_file
= kmalloc(sizeof(*rawmidi_file
), GFP_KERNEL
);
413 if (rawmidi_file
== NULL
) {
417 init_waitqueue_entry(&wait
, current
);
418 add_wait_queue(&rmidi
->open_wait
, &wait
);
420 subdevice
= snd_ctl_get_preferred_subdevice(card
, SND_CTL_SUBDEV_RAWMIDI
);
421 err
= rawmidi_open_priv(rmidi
, subdevice
, fflags
, rawmidi_file
);
424 if (err
== -EAGAIN
) {
425 if (file
->f_flags
& O_NONBLOCK
) {
431 set_current_state(TASK_INTERRUPTIBLE
);
432 mutex_unlock(&rmidi
->open_mutex
);
434 mutex_lock(&rmidi
->open_mutex
);
435 if (rmidi
->card
->shutdown
) {
439 if (signal_pending(current
)) {
444 remove_wait_queue(&rmidi
->open_wait
, &wait
);
449 #ifdef CONFIG_SND_OSSEMUL
450 if (rawmidi_file
->input
&& rawmidi_file
->input
->runtime
)
451 rawmidi_file
->input
->runtime
->oss
= (maj
== SOUND_MAJOR
);
452 if (rawmidi_file
->output
&& rawmidi_file
->output
->runtime
)
453 rawmidi_file
->output
->runtime
->oss
= (maj
== SOUND_MAJOR
);
455 file
->private_data
= rawmidi_file
;
456 mutex_unlock(&rmidi
->open_mutex
);
457 snd_card_unref(rmidi
->card
);
461 snd_card_file_remove(card
, file
);
463 mutex_unlock(&rmidi
->open_mutex
);
464 module_put(rmidi
->card
->module
);
465 snd_card_unref(rmidi
->card
);
469 static void close_substream(struct snd_rawmidi
*rmidi
,
470 struct snd_rawmidi_substream
*substream
,
473 if (--substream
->use_count
)
477 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
478 snd_rawmidi_input_trigger(substream
, 0);
480 if (substream
->active_sensing
) {
481 unsigned char buf
= 0xfe;
482 /* sending single active sensing message
483 * to shut the device up
485 snd_rawmidi_kernel_write(substream
, &buf
, 1);
487 if (snd_rawmidi_drain_output(substream
) == -ERESTARTSYS
)
488 snd_rawmidi_output_trigger(substream
, 0);
491 substream
->ops
->close(substream
);
492 if (substream
->runtime
->private_free
)
493 substream
->runtime
->private_free(substream
);
494 snd_rawmidi_runtime_free(substream
);
495 substream
->opened
= 0;
496 substream
->append
= 0;
497 put_pid(substream
->pid
);
498 substream
->pid
= NULL
;
499 rmidi
->streams
[substream
->stream
].substream_opened
--;
502 static void rawmidi_release_priv(struct snd_rawmidi_file
*rfile
)
504 struct snd_rawmidi
*rmidi
;
506 rmidi
= rfile
->rmidi
;
507 mutex_lock(&rmidi
->open_mutex
);
509 close_substream(rmidi
, rfile
->input
, 1);
513 close_substream(rmidi
, rfile
->output
, 1);
514 rfile
->output
= NULL
;
517 mutex_unlock(&rmidi
->open_mutex
);
518 wake_up(&rmidi
->open_wait
);
521 /* called from sound/core/seq/seq_midi.c */
522 int snd_rawmidi_kernel_release(struct snd_rawmidi_file
*rfile
)
524 struct snd_rawmidi
*rmidi
;
526 if (snd_BUG_ON(!rfile
))
529 rmidi
= rfile
->rmidi
;
530 rawmidi_release_priv(rfile
);
531 module_put(rmidi
->card
->module
);
534 EXPORT_SYMBOL(snd_rawmidi_kernel_release
);
536 static int snd_rawmidi_release(struct inode
*inode
, struct file
*file
)
538 struct snd_rawmidi_file
*rfile
;
539 struct snd_rawmidi
*rmidi
;
540 struct module
*module
;
542 rfile
= file
->private_data
;
543 rmidi
= rfile
->rmidi
;
544 rawmidi_release_priv(rfile
);
546 module
= rmidi
->card
->module
;
547 snd_card_file_remove(rmidi
->card
, file
);
552 static int snd_rawmidi_info(struct snd_rawmidi_substream
*substream
,
553 struct snd_rawmidi_info
*info
)
555 struct snd_rawmidi
*rmidi
;
557 if (substream
== NULL
)
559 rmidi
= substream
->rmidi
;
560 memset(info
, 0, sizeof(*info
));
561 info
->card
= rmidi
->card
->number
;
562 info
->device
= rmidi
->device
;
563 info
->subdevice
= substream
->number
;
564 info
->stream
= substream
->stream
;
565 info
->flags
= rmidi
->info_flags
;
566 strcpy(info
->id
, rmidi
->id
);
567 strcpy(info
->name
, rmidi
->name
);
568 strcpy(info
->subname
, substream
->name
);
569 info
->subdevices_count
= substream
->pstr
->substream_count
;
570 info
->subdevices_avail
= (substream
->pstr
->substream_count
-
571 substream
->pstr
->substream_opened
);
575 static int snd_rawmidi_info_user(struct snd_rawmidi_substream
*substream
,
576 struct snd_rawmidi_info __user
*_info
)
578 struct snd_rawmidi_info info
;
581 err
= snd_rawmidi_info(substream
, &info
);
584 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
589 static int __snd_rawmidi_info_select(struct snd_card
*card
,
590 struct snd_rawmidi_info
*info
)
592 struct snd_rawmidi
*rmidi
;
593 struct snd_rawmidi_str
*pstr
;
594 struct snd_rawmidi_substream
*substream
;
596 rmidi
= snd_rawmidi_search(card
, info
->device
);
599 if (info
->stream
< 0 || info
->stream
> 1)
601 info
->stream
= array_index_nospec(info
->stream
, 2);
602 pstr
= &rmidi
->streams
[info
->stream
];
603 if (pstr
->substream_count
== 0)
605 if (info
->subdevice
>= pstr
->substream_count
)
607 list_for_each_entry(substream
, &pstr
->substreams
, list
) {
608 if ((unsigned int)substream
->number
== info
->subdevice
)
609 return snd_rawmidi_info(substream
, info
);
614 int snd_rawmidi_info_select(struct snd_card
*card
, struct snd_rawmidi_info
*info
)
618 mutex_lock(®ister_mutex
);
619 ret
= __snd_rawmidi_info_select(card
, info
);
620 mutex_unlock(®ister_mutex
);
623 EXPORT_SYMBOL(snd_rawmidi_info_select
);
625 static int snd_rawmidi_info_select_user(struct snd_card
*card
,
626 struct snd_rawmidi_info __user
*_info
)
629 struct snd_rawmidi_info info
;
631 if (get_user(info
.device
, &_info
->device
))
633 if (get_user(info
.stream
, &_info
->stream
))
635 if (get_user(info
.subdevice
, &_info
->subdevice
))
637 err
= snd_rawmidi_info_select(card
, &info
);
640 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
645 static int resize_runtime_buffer(struct snd_rawmidi_runtime
*runtime
,
646 struct snd_rawmidi_params
*params
,
649 char *newbuf
, *oldbuf
;
651 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L)
653 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
)
655 if (params
->buffer_size
!= runtime
->buffer_size
) {
656 newbuf
= kvzalloc(params
->buffer_size
, GFP_KERNEL
);
659 spin_lock_irq(&runtime
->lock
);
660 if (runtime
->buffer_ref
) {
661 spin_unlock_irq(&runtime
->lock
);
665 oldbuf
= runtime
->buffer
;
666 runtime
->buffer
= newbuf
;
667 runtime
->buffer_size
= params
->buffer_size
;
668 __reset_runtime_ptrs(runtime
, is_input
);
669 spin_unlock_irq(&runtime
->lock
);
672 runtime
->avail_min
= params
->avail_min
;
676 int snd_rawmidi_output_params(struct snd_rawmidi_substream
*substream
,
677 struct snd_rawmidi_params
*params
)
679 if (substream
->append
&& substream
->use_count
> 1)
681 snd_rawmidi_drain_output(substream
);
682 substream
->active_sensing
= !params
->no_active_sensing
;
683 return resize_runtime_buffer(substream
->runtime
, params
, false);
685 EXPORT_SYMBOL(snd_rawmidi_output_params
);
687 int snd_rawmidi_input_params(struct snd_rawmidi_substream
*substream
,
688 struct snd_rawmidi_params
*params
)
690 snd_rawmidi_drain_input(substream
);
691 return resize_runtime_buffer(substream
->runtime
, params
, true);
693 EXPORT_SYMBOL(snd_rawmidi_input_params
);
695 static int snd_rawmidi_output_status(struct snd_rawmidi_substream
*substream
,
696 struct snd_rawmidi_status
*status
)
698 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
700 memset(status
, 0, sizeof(*status
));
701 status
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
702 spin_lock_irq(&runtime
->lock
);
703 status
->avail
= runtime
->avail
;
704 spin_unlock_irq(&runtime
->lock
);
708 static int snd_rawmidi_input_status(struct snd_rawmidi_substream
*substream
,
709 struct snd_rawmidi_status
*status
)
711 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
713 memset(status
, 0, sizeof(*status
));
714 status
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
715 spin_lock_irq(&runtime
->lock
);
716 status
->avail
= runtime
->avail
;
717 status
->xruns
= runtime
->xruns
;
719 spin_unlock_irq(&runtime
->lock
);
723 static long snd_rawmidi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
725 struct snd_rawmidi_file
*rfile
;
726 void __user
*argp
= (void __user
*)arg
;
728 rfile
= file
->private_data
;
729 if (((cmd
>> 8) & 0xff) != 'W')
732 case SNDRV_RAWMIDI_IOCTL_PVERSION
:
733 return put_user(SNDRV_RAWMIDI_VERSION
, (int __user
*)argp
) ? -EFAULT
: 0;
734 case SNDRV_RAWMIDI_IOCTL_INFO
:
737 struct snd_rawmidi_info __user
*info
= argp
;
739 if (get_user(stream
, &info
->stream
))
742 case SNDRV_RAWMIDI_STREAM_INPUT
:
743 return snd_rawmidi_info_user(rfile
->input
, info
);
744 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
745 return snd_rawmidi_info_user(rfile
->output
, info
);
750 case SNDRV_RAWMIDI_IOCTL_PARAMS
:
752 struct snd_rawmidi_params params
;
754 if (copy_from_user(¶ms
, argp
, sizeof(struct snd_rawmidi_params
)))
756 switch (params
.stream
) {
757 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
758 if (rfile
->output
== NULL
)
760 return snd_rawmidi_output_params(rfile
->output
, ¶ms
);
761 case SNDRV_RAWMIDI_STREAM_INPUT
:
762 if (rfile
->input
== NULL
)
764 return snd_rawmidi_input_params(rfile
->input
, ¶ms
);
769 case SNDRV_RAWMIDI_IOCTL_STATUS
:
772 struct snd_rawmidi_status status
;
774 if (copy_from_user(&status
, argp
, sizeof(struct snd_rawmidi_status
)))
776 switch (status
.stream
) {
777 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
778 if (rfile
->output
== NULL
)
780 err
= snd_rawmidi_output_status(rfile
->output
, &status
);
782 case SNDRV_RAWMIDI_STREAM_INPUT
:
783 if (rfile
->input
== NULL
)
785 err
= snd_rawmidi_input_status(rfile
->input
, &status
);
792 if (copy_to_user(argp
, &status
, sizeof(struct snd_rawmidi_status
)))
796 case SNDRV_RAWMIDI_IOCTL_DROP
:
800 if (get_user(val
, (int __user
*) argp
))
803 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
804 if (rfile
->output
== NULL
)
806 return snd_rawmidi_drop_output(rfile
->output
);
811 case SNDRV_RAWMIDI_IOCTL_DRAIN
:
815 if (get_user(val
, (int __user
*) argp
))
818 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
819 if (rfile
->output
== NULL
)
821 return snd_rawmidi_drain_output(rfile
->output
);
822 case SNDRV_RAWMIDI_STREAM_INPUT
:
823 if (rfile
->input
== NULL
)
825 return snd_rawmidi_drain_input(rfile
->input
);
831 rmidi_dbg(rfile
->rmidi
,
832 "rawmidi: unknown command = 0x%x\n", cmd
);
837 static int snd_rawmidi_control_ioctl(struct snd_card
*card
,
838 struct snd_ctl_file
*control
,
842 void __user
*argp
= (void __user
*)arg
;
845 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE
:
849 if (get_user(device
, (int __user
*)argp
))
851 if (device
>= SNDRV_RAWMIDI_DEVICES
) /* next device is -1 */
852 device
= SNDRV_RAWMIDI_DEVICES
- 1;
853 mutex_lock(®ister_mutex
);
854 device
= device
< 0 ? 0 : device
+ 1;
855 while (device
< SNDRV_RAWMIDI_DEVICES
) {
856 if (snd_rawmidi_search(card
, device
))
860 if (device
== SNDRV_RAWMIDI_DEVICES
)
862 mutex_unlock(®ister_mutex
);
863 if (put_user(device
, (int __user
*)argp
))
867 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE
:
871 if (get_user(val
, (int __user
*)argp
))
873 control
->preferred_subdevice
[SND_CTL_SUBDEV_RAWMIDI
] = val
;
876 case SNDRV_CTL_IOCTL_RAWMIDI_INFO
:
877 return snd_rawmidi_info_select_user(card
, argp
);
883 * snd_rawmidi_receive - receive the input data from the device
884 * @substream: the rawmidi substream
885 * @buffer: the buffer pointer
886 * @count: the data size to read
888 * Reads the data from the internal buffer.
890 * Return: The size of read data, or a negative error code on failure.
892 int snd_rawmidi_receive(struct snd_rawmidi_substream
*substream
,
893 const unsigned char *buffer
, int count
)
896 int result
= 0, count1
;
897 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
899 if (!substream
->opened
)
901 if (runtime
->buffer
== NULL
) {
902 rmidi_dbg(substream
->rmidi
,
903 "snd_rawmidi_receive: input is not active!!!\n");
906 spin_lock_irqsave(&runtime
->lock
, flags
);
907 if (count
== 1) { /* special case, faster code */
909 if (runtime
->avail
< runtime
->buffer_size
) {
910 runtime
->buffer
[runtime
->hw_ptr
++] = buffer
[0];
911 runtime
->hw_ptr
%= runtime
->buffer_size
;
918 substream
->bytes
+= count
;
919 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
922 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
923 count1
= runtime
->buffer_size
- runtime
->avail
;
924 memcpy(runtime
->buffer
+ runtime
->hw_ptr
, buffer
, count1
);
925 runtime
->hw_ptr
+= count1
;
926 runtime
->hw_ptr
%= runtime
->buffer_size
;
927 runtime
->avail
+= count1
;
933 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
)) {
934 count1
= runtime
->buffer_size
- runtime
->avail
;
935 runtime
->xruns
+= count
- count1
;
938 memcpy(runtime
->buffer
, buffer
, count1
);
939 runtime
->hw_ptr
= count1
;
940 runtime
->avail
+= count1
;
947 schedule_work(&runtime
->event_work
);
948 else if (snd_rawmidi_ready(substream
))
949 wake_up(&runtime
->sleep
);
951 spin_unlock_irqrestore(&runtime
->lock
, flags
);
954 EXPORT_SYMBOL(snd_rawmidi_receive
);
956 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream
*substream
,
957 unsigned char __user
*userbuf
,
958 unsigned char *kernelbuf
, long count
)
961 long result
= 0, count1
;
962 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
963 unsigned long appl_ptr
;
966 spin_lock_irqsave(&runtime
->lock
, flags
);
967 snd_rawmidi_buffer_ref(runtime
);
968 while (count
> 0 && runtime
->avail
) {
969 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
972 if (count1
> (int)runtime
->avail
)
973 count1
= runtime
->avail
;
975 /* update runtime->appl_ptr before unlocking for userbuf */
976 appl_ptr
= runtime
->appl_ptr
;
977 runtime
->appl_ptr
+= count1
;
978 runtime
->appl_ptr
%= runtime
->buffer_size
;
979 runtime
->avail
-= count1
;
982 memcpy(kernelbuf
+ result
, runtime
->buffer
+ appl_ptr
, count1
);
984 spin_unlock_irqrestore(&runtime
->lock
, flags
);
985 if (copy_to_user(userbuf
+ result
,
986 runtime
->buffer
+ appl_ptr
, count1
))
988 spin_lock_irqsave(&runtime
->lock
, flags
);
996 snd_rawmidi_buffer_unref(runtime
);
997 spin_unlock_irqrestore(&runtime
->lock
, flags
);
998 return result
> 0 ? result
: err
;
1001 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream
*substream
,
1002 unsigned char *buf
, long count
)
1004 snd_rawmidi_input_trigger(substream
, 1);
1005 return snd_rawmidi_kernel_read1(substream
, NULL
/*userbuf*/, buf
, count
);
1007 EXPORT_SYMBOL(snd_rawmidi_kernel_read
);
1009 static ssize_t
snd_rawmidi_read(struct file
*file
, char __user
*buf
, size_t count
,
1014 struct snd_rawmidi_file
*rfile
;
1015 struct snd_rawmidi_substream
*substream
;
1016 struct snd_rawmidi_runtime
*runtime
;
1018 rfile
= file
->private_data
;
1019 substream
= rfile
->input
;
1020 if (substream
== NULL
)
1022 runtime
= substream
->runtime
;
1023 snd_rawmidi_input_trigger(substream
, 1);
1026 spin_lock_irq(&runtime
->lock
);
1027 while (!snd_rawmidi_ready(substream
)) {
1028 wait_queue_entry_t wait
;
1030 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1031 spin_unlock_irq(&runtime
->lock
);
1032 return result
> 0 ? result
: -EAGAIN
;
1034 init_waitqueue_entry(&wait
, current
);
1035 add_wait_queue(&runtime
->sleep
, &wait
);
1036 set_current_state(TASK_INTERRUPTIBLE
);
1037 spin_unlock_irq(&runtime
->lock
);
1039 remove_wait_queue(&runtime
->sleep
, &wait
);
1040 if (rfile
->rmidi
->card
->shutdown
)
1042 if (signal_pending(current
))
1043 return result
> 0 ? result
: -ERESTARTSYS
;
1044 if (!runtime
->avail
)
1045 return result
> 0 ? result
: -EIO
;
1046 spin_lock_irq(&runtime
->lock
);
1048 spin_unlock_irq(&runtime
->lock
);
1049 count1
= snd_rawmidi_kernel_read1(substream
,
1050 (unsigned char __user
*)buf
,
1054 return result
> 0 ? result
: count1
;
1063 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1064 * @substream: the rawmidi substream
1066 * Return: 1 if the internal output buffer is empty, 0 if not.
1068 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream
*substream
)
1070 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1072 unsigned long flags
;
1074 if (runtime
->buffer
== NULL
) {
1075 rmidi_dbg(substream
->rmidi
,
1076 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1079 spin_lock_irqsave(&runtime
->lock
, flags
);
1080 result
= runtime
->avail
>= runtime
->buffer_size
;
1081 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1084 EXPORT_SYMBOL(snd_rawmidi_transmit_empty
);
1087 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1088 * @substream: the rawmidi substream
1089 * @buffer: the buffer pointer
1090 * @count: data size to transfer
1092 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1094 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1095 unsigned char *buffer
, int count
)
1098 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1100 if (runtime
->buffer
== NULL
) {
1101 rmidi_dbg(substream
->rmidi
,
1102 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1106 if (runtime
->avail
>= runtime
->buffer_size
) {
1107 /* warning: lowlevel layer MUST trigger down the hardware */
1110 if (count
== 1) { /* special case, faster code */
1111 *buffer
= runtime
->buffer
[runtime
->hw_ptr
];
1114 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
1117 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
1118 count1
= runtime
->buffer_size
- runtime
->avail
;
1119 memcpy(buffer
, runtime
->buffer
+ runtime
->hw_ptr
, count1
);
1123 if (count
> (int)(runtime
->buffer_size
- runtime
->avail
- count1
))
1124 count
= runtime
->buffer_size
- runtime
->avail
- count1
;
1125 memcpy(buffer
+ count1
, runtime
->buffer
, count
);
1132 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek
);
1135 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1136 * @substream: the rawmidi substream
1137 * @buffer: the buffer pointer
1138 * @count: data size to transfer
1140 * Copies data from the internal output buffer to the given buffer.
1142 * Call this in the interrupt handler when the midi output is ready,
1143 * and call snd_rawmidi_transmit_ack() after the transmission is
1146 * Return: The size of copied data, or a negative error code on failure.
1148 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1149 unsigned char *buffer
, int count
)
1151 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1153 unsigned long flags
;
1155 spin_lock_irqsave(&runtime
->lock
, flags
);
1156 result
= __snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1157 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1160 EXPORT_SYMBOL(snd_rawmidi_transmit_peek
);
1163 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1164 * @substream: the rawmidi substream
1165 * @count: the transferred count
1167 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1169 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1171 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1173 if (runtime
->buffer
== NULL
) {
1174 rmidi_dbg(substream
->rmidi
,
1175 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1178 snd_BUG_ON(runtime
->avail
+ count
> runtime
->buffer_size
);
1179 runtime
->hw_ptr
+= count
;
1180 runtime
->hw_ptr
%= runtime
->buffer_size
;
1181 runtime
->avail
+= count
;
1182 substream
->bytes
+= count
;
1184 if (runtime
->drain
|| snd_rawmidi_ready(substream
))
1185 wake_up(&runtime
->sleep
);
1189 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack
);
1192 * snd_rawmidi_transmit_ack - acknowledge the transmission
1193 * @substream: the rawmidi substream
1194 * @count: the transferred count
1196 * Advances the hardware pointer for the internal output buffer with
1197 * the given size and updates the condition.
1198 * Call after the transmission is finished.
1200 * Return: The advanced size if successful, or a negative error code on failure.
1202 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, 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_ack(substream
, count
);
1210 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1213 EXPORT_SYMBOL(snd_rawmidi_transmit_ack
);
1216 * snd_rawmidi_transmit - copy from the buffer to the device
1217 * @substream: the rawmidi substream
1218 * @buffer: the buffer pointer
1219 * @count: the data size to transfer
1221 * Copies data from the buffer to the device and advances the pointer.
1223 * Return: The copied size if successful, or a negative error code on failure.
1225 int snd_rawmidi_transmit(struct snd_rawmidi_substream
*substream
,
1226 unsigned char *buffer
, int count
)
1228 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1230 unsigned long flags
;
1232 spin_lock_irqsave(&runtime
->lock
, flags
);
1233 if (!substream
->opened
)
1236 count
= __snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1240 result
= __snd_rawmidi_transmit_ack(substream
, count
);
1242 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1245 EXPORT_SYMBOL(snd_rawmidi_transmit
);
1248 * snd_rawmidi_proceed - Discard the all pending bytes and proceed
1249 * @substream: rawmidi substream
1251 * Return: the number of discarded bytes
1253 int snd_rawmidi_proceed(struct snd_rawmidi_substream
*substream
)
1255 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1256 unsigned long flags
;
1259 spin_lock_irqsave(&runtime
->lock
, flags
);
1260 if (runtime
->avail
< runtime
->buffer_size
) {
1261 count
= runtime
->buffer_size
- runtime
->avail
;
1262 __snd_rawmidi_transmit_ack(substream
, count
);
1264 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1267 EXPORT_SYMBOL(snd_rawmidi_proceed
);
1269 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream
*substream
,
1270 const unsigned char __user
*userbuf
,
1271 const unsigned char *kernelbuf
,
1274 unsigned long flags
;
1275 long count1
, result
;
1276 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1277 unsigned long appl_ptr
;
1279 if (!kernelbuf
&& !userbuf
)
1281 if (snd_BUG_ON(!runtime
->buffer
))
1285 spin_lock_irqsave(&runtime
->lock
, flags
);
1286 if (substream
->append
) {
1287 if ((long)runtime
->avail
< count
) {
1288 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1292 snd_rawmidi_buffer_ref(runtime
);
1293 while (count
> 0 && runtime
->avail
> 0) {
1294 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1297 if (count1
> (long)runtime
->avail
)
1298 count1
= runtime
->avail
;
1300 /* update runtime->appl_ptr before unlocking for userbuf */
1301 appl_ptr
= runtime
->appl_ptr
;
1302 runtime
->appl_ptr
+= count1
;
1303 runtime
->appl_ptr
%= runtime
->buffer_size
;
1304 runtime
->avail
-= count1
;
1307 memcpy(runtime
->buffer
+ appl_ptr
,
1308 kernelbuf
+ result
, count1
);
1310 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1311 if (copy_from_user(runtime
->buffer
+ appl_ptr
,
1312 userbuf
+ result
, count1
)) {
1313 spin_lock_irqsave(&runtime
->lock
, flags
);
1314 result
= result
> 0 ? result
: -EFAULT
;
1317 spin_lock_irqsave(&runtime
->lock
, flags
);
1323 count1
= runtime
->avail
< runtime
->buffer_size
;
1324 snd_rawmidi_buffer_unref(runtime
);
1325 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1327 snd_rawmidi_output_trigger(substream
, 1);
1331 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream
*substream
,
1332 const unsigned char *buf
, long count
)
1334 return snd_rawmidi_kernel_write1(substream
, NULL
, buf
, count
);
1336 EXPORT_SYMBOL(snd_rawmidi_kernel_write
);
1338 static ssize_t
snd_rawmidi_write(struct file
*file
, const char __user
*buf
,
1339 size_t count
, loff_t
*offset
)
1341 long result
, timeout
;
1343 struct snd_rawmidi_file
*rfile
;
1344 struct snd_rawmidi_runtime
*runtime
;
1345 struct snd_rawmidi_substream
*substream
;
1347 rfile
= file
->private_data
;
1348 substream
= rfile
->output
;
1349 runtime
= substream
->runtime
;
1350 /* we cannot put an atomic message to our buffer */
1351 if (substream
->append
&& count
> runtime
->buffer_size
)
1355 spin_lock_irq(&runtime
->lock
);
1356 while (!snd_rawmidi_ready_append(substream
, count
)) {
1357 wait_queue_entry_t wait
;
1359 if (file
->f_flags
& O_NONBLOCK
) {
1360 spin_unlock_irq(&runtime
->lock
);
1361 return result
> 0 ? result
: -EAGAIN
;
1363 init_waitqueue_entry(&wait
, current
);
1364 add_wait_queue(&runtime
->sleep
, &wait
);
1365 set_current_state(TASK_INTERRUPTIBLE
);
1366 spin_unlock_irq(&runtime
->lock
);
1367 timeout
= schedule_timeout(30 * HZ
);
1368 remove_wait_queue(&runtime
->sleep
, &wait
);
1369 if (rfile
->rmidi
->card
->shutdown
)
1371 if (signal_pending(current
))
1372 return result
> 0 ? result
: -ERESTARTSYS
;
1373 if (!runtime
->avail
&& !timeout
)
1374 return result
> 0 ? result
: -EIO
;
1375 spin_lock_irq(&runtime
->lock
);
1377 spin_unlock_irq(&runtime
->lock
);
1378 count1
= snd_rawmidi_kernel_write1(substream
, buf
, NULL
, count
);
1380 return result
> 0 ? result
: count1
;
1383 if ((size_t)count1
< count
&& (file
->f_flags
& O_NONBLOCK
))
1387 if (file
->f_flags
& O_DSYNC
) {
1388 spin_lock_irq(&runtime
->lock
);
1389 while (runtime
->avail
!= runtime
->buffer_size
) {
1390 wait_queue_entry_t wait
;
1391 unsigned int last_avail
= runtime
->avail
;
1393 init_waitqueue_entry(&wait
, current
);
1394 add_wait_queue(&runtime
->sleep
, &wait
);
1395 set_current_state(TASK_INTERRUPTIBLE
);
1396 spin_unlock_irq(&runtime
->lock
);
1397 timeout
= schedule_timeout(30 * HZ
);
1398 remove_wait_queue(&runtime
->sleep
, &wait
);
1399 if (signal_pending(current
))
1400 return result
> 0 ? result
: -ERESTARTSYS
;
1401 if (runtime
->avail
== last_avail
&& !timeout
)
1402 return result
> 0 ? result
: -EIO
;
1403 spin_lock_irq(&runtime
->lock
);
1405 spin_unlock_irq(&runtime
->lock
);
1410 static __poll_t
snd_rawmidi_poll(struct file
*file
, poll_table
*wait
)
1412 struct snd_rawmidi_file
*rfile
;
1413 struct snd_rawmidi_runtime
*runtime
;
1416 rfile
= file
->private_data
;
1417 if (rfile
->input
!= NULL
) {
1418 runtime
= rfile
->input
->runtime
;
1419 snd_rawmidi_input_trigger(rfile
->input
, 1);
1420 poll_wait(file
, &runtime
->sleep
, wait
);
1422 if (rfile
->output
!= NULL
) {
1423 runtime
= rfile
->output
->runtime
;
1424 poll_wait(file
, &runtime
->sleep
, wait
);
1427 if (rfile
->input
!= NULL
) {
1428 if (snd_rawmidi_ready(rfile
->input
))
1429 mask
|= EPOLLIN
| EPOLLRDNORM
;
1431 if (rfile
->output
!= NULL
) {
1432 if (snd_rawmidi_ready(rfile
->output
))
1433 mask
|= EPOLLOUT
| EPOLLWRNORM
;
1440 #ifdef CONFIG_COMPAT
1441 #include "rawmidi_compat.c"
1443 #define snd_rawmidi_ioctl_compat NULL
1449 static void snd_rawmidi_proc_info_read(struct snd_info_entry
*entry
,
1450 struct snd_info_buffer
*buffer
)
1452 struct snd_rawmidi
*rmidi
;
1453 struct snd_rawmidi_substream
*substream
;
1454 struct snd_rawmidi_runtime
*runtime
;
1456 rmidi
= entry
->private_data
;
1457 snd_iprintf(buffer
, "%s\n\n", rmidi
->name
);
1458 mutex_lock(&rmidi
->open_mutex
);
1459 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) {
1460 list_for_each_entry(substream
,
1461 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
,
1465 " Tx bytes : %lu\n",
1467 (unsigned long) substream
->bytes
);
1468 if (substream
->opened
) {
1470 " Owner PID : %d\n",
1471 pid_vnr(substream
->pid
));
1472 runtime
= substream
->runtime
;
1475 " Buffer size : %lu\n"
1477 runtime
->oss
? "OSS compatible" : "native",
1478 (unsigned long) runtime
->buffer_size
,
1479 (unsigned long) runtime
->avail
);
1483 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
) {
1484 list_for_each_entry(substream
,
1485 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
,
1489 " Rx bytes : %lu\n",
1491 (unsigned long) substream
->bytes
);
1492 if (substream
->opened
) {
1494 " Owner PID : %d\n",
1495 pid_vnr(substream
->pid
));
1496 runtime
= substream
->runtime
;
1498 " Buffer size : %lu\n"
1500 " Overruns : %lu\n",
1501 (unsigned long) runtime
->buffer_size
,
1502 (unsigned long) runtime
->avail
,
1503 (unsigned long) runtime
->xruns
);
1507 mutex_unlock(&rmidi
->open_mutex
);
1511 * Register functions
1514 static const struct file_operations snd_rawmidi_f_ops
= {
1515 .owner
= THIS_MODULE
,
1516 .read
= snd_rawmidi_read
,
1517 .write
= snd_rawmidi_write
,
1518 .open
= snd_rawmidi_open
,
1519 .release
= snd_rawmidi_release
,
1520 .llseek
= no_llseek
,
1521 .poll
= snd_rawmidi_poll
,
1522 .unlocked_ioctl
= snd_rawmidi_ioctl
,
1523 .compat_ioctl
= snd_rawmidi_ioctl_compat
,
1526 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi
*rmidi
,
1527 struct snd_rawmidi_str
*stream
,
1531 struct snd_rawmidi_substream
*substream
;
1534 for (idx
= 0; idx
< count
; idx
++) {
1535 substream
= kzalloc(sizeof(*substream
), GFP_KERNEL
);
1538 substream
->stream
= direction
;
1539 substream
->number
= idx
;
1540 substream
->rmidi
= rmidi
;
1541 substream
->pstr
= stream
;
1542 list_add_tail(&substream
->list
, &stream
->substreams
);
1543 stream
->substream_count
++;
1548 static void release_rawmidi_device(struct device
*dev
)
1550 kfree(container_of(dev
, struct snd_rawmidi
, dev
));
1554 * snd_rawmidi_new - create a rawmidi instance
1555 * @card: the card instance
1556 * @id: the id string
1557 * @device: the device index
1558 * @output_count: the number of output streams
1559 * @input_count: the number of input streams
1560 * @rrawmidi: the pointer to store the new rawmidi instance
1562 * Creates a new rawmidi instance.
1563 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1565 * Return: Zero if successful, or a negative error code on failure.
1567 int snd_rawmidi_new(struct snd_card
*card
, char *id
, int device
,
1568 int output_count
, int input_count
,
1569 struct snd_rawmidi
**rrawmidi
)
1571 struct snd_rawmidi
*rmidi
;
1573 static struct snd_device_ops ops
= {
1574 .dev_free
= snd_rawmidi_dev_free
,
1575 .dev_register
= snd_rawmidi_dev_register
,
1576 .dev_disconnect
= snd_rawmidi_dev_disconnect
,
1579 if (snd_BUG_ON(!card
))
1583 rmidi
= kzalloc(sizeof(*rmidi
), GFP_KERNEL
);
1587 rmidi
->device
= device
;
1588 mutex_init(&rmidi
->open_mutex
);
1589 init_waitqueue_head(&rmidi
->open_wait
);
1590 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
);
1591 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
);
1594 strlcpy(rmidi
->id
, id
, sizeof(rmidi
->id
));
1596 snd_device_initialize(&rmidi
->dev
, card
);
1597 rmidi
->dev
.release
= release_rawmidi_device
;
1598 dev_set_name(&rmidi
->dev
, "midiC%iD%i", card
->number
, device
);
1600 err
= snd_rawmidi_alloc_substreams(rmidi
,
1601 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
],
1602 SNDRV_RAWMIDI_STREAM_INPUT
,
1606 err
= snd_rawmidi_alloc_substreams(rmidi
,
1607 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
],
1608 SNDRV_RAWMIDI_STREAM_OUTPUT
,
1612 err
= snd_device_new(card
, SNDRV_DEV_RAWMIDI
, rmidi
, &ops
);
1621 snd_rawmidi_free(rmidi
);
1624 EXPORT_SYMBOL(snd_rawmidi_new
);
1626 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str
*stream
)
1628 struct snd_rawmidi_substream
*substream
;
1630 while (!list_empty(&stream
->substreams
)) {
1631 substream
= list_entry(stream
->substreams
.next
, struct snd_rawmidi_substream
, list
);
1632 list_del(&substream
->list
);
1637 static int snd_rawmidi_free(struct snd_rawmidi
*rmidi
)
1642 snd_info_free_entry(rmidi
->proc_entry
);
1643 rmidi
->proc_entry
= NULL
;
1644 mutex_lock(®ister_mutex
);
1645 if (rmidi
->ops
&& rmidi
->ops
->dev_unregister
)
1646 rmidi
->ops
->dev_unregister(rmidi
);
1647 mutex_unlock(®ister_mutex
);
1649 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
1650 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
1651 if (rmidi
->private_free
)
1652 rmidi
->private_free(rmidi
);
1653 put_device(&rmidi
->dev
);
1657 static int snd_rawmidi_dev_free(struct snd_device
*device
)
1659 struct snd_rawmidi
*rmidi
= device
->device_data
;
1661 return snd_rawmidi_free(rmidi
);
1664 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1665 static void snd_rawmidi_dev_seq_free(struct snd_seq_device
*device
)
1667 struct snd_rawmidi
*rmidi
= device
->private_data
;
1669 rmidi
->seq_dev
= NULL
;
1673 static int snd_rawmidi_dev_register(struct snd_device
*device
)
1676 struct snd_info_entry
*entry
;
1678 struct snd_rawmidi
*rmidi
= device
->device_data
;
1680 if (rmidi
->device
>= SNDRV_RAWMIDI_DEVICES
)
1683 mutex_lock(®ister_mutex
);
1684 if (snd_rawmidi_search(rmidi
->card
, rmidi
->device
))
1687 list_add_tail(&rmidi
->list
, &snd_rawmidi_devices
);
1688 mutex_unlock(®ister_mutex
);
1692 err
= snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI
,
1693 rmidi
->card
, rmidi
->device
,
1694 &snd_rawmidi_f_ops
, rmidi
, &rmidi
->dev
);
1696 rmidi_err(rmidi
, "unable to register\n");
1699 if (rmidi
->ops
&& rmidi
->ops
->dev_register
) {
1700 err
= rmidi
->ops
->dev_register(rmidi
);
1702 goto error_unregister
;
1704 #ifdef CONFIG_SND_OSSEMUL
1706 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1707 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1708 rmidi
->card
, 0, &snd_rawmidi_f_ops
,
1711 "unable to register OSS rawmidi device %i:%i\n",
1712 rmidi
->card
->number
, 0);
1715 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1716 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
, rmidi
->name
);
1720 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
]) {
1721 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1722 rmidi
->card
, 1, &snd_rawmidi_f_ops
,
1725 "unable to register OSS rawmidi device %i:%i\n",
1726 rmidi
->card
->number
, 1);
1731 #endif /* CONFIG_SND_OSSEMUL */
1732 sprintf(name
, "midi%d", rmidi
->device
);
1733 entry
= snd_info_create_card_entry(rmidi
->card
, name
, rmidi
->card
->proc_root
);
1735 entry
->private_data
= rmidi
;
1736 entry
->c
.text
.read
= snd_rawmidi_proc_info_read
;
1737 if (snd_info_register(entry
) < 0) {
1738 snd_info_free_entry(entry
);
1742 rmidi
->proc_entry
= entry
;
1743 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1744 if (!rmidi
->ops
|| !rmidi
->ops
->dev_register
) { /* own registration mechanism */
1745 if (snd_seq_device_new(rmidi
->card
, rmidi
->device
, SNDRV_SEQ_DEV_ID_MIDISYNTH
, 0, &rmidi
->seq_dev
) >= 0) {
1746 rmidi
->seq_dev
->private_data
= rmidi
;
1747 rmidi
->seq_dev
->private_free
= snd_rawmidi_dev_seq_free
;
1748 sprintf(rmidi
->seq_dev
->name
, "MIDI %d-%d", rmidi
->card
->number
, rmidi
->device
);
1749 snd_device_register(rmidi
->card
, rmidi
->seq_dev
);
1756 snd_unregister_device(&rmidi
->dev
);
1758 mutex_lock(®ister_mutex
);
1759 list_del(&rmidi
->list
);
1760 mutex_unlock(®ister_mutex
);
1764 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
)
1766 struct snd_rawmidi
*rmidi
= device
->device_data
;
1769 mutex_lock(®ister_mutex
);
1770 mutex_lock(&rmidi
->open_mutex
);
1771 wake_up(&rmidi
->open_wait
);
1772 list_del_init(&rmidi
->list
);
1773 for (dir
= 0; dir
< 2; dir
++) {
1774 struct snd_rawmidi_substream
*s
;
1776 list_for_each_entry(s
, &rmidi
->streams
[dir
].substreams
, list
) {
1778 wake_up(&s
->runtime
->sleep
);
1782 #ifdef CONFIG_SND_OSSEMUL
1783 if (rmidi
->ossreg
) {
1784 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1785 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 0);
1786 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1787 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
);
1790 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
])
1791 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 1);
1794 #endif /* CONFIG_SND_OSSEMUL */
1795 snd_unregister_device(&rmidi
->dev
);
1796 mutex_unlock(&rmidi
->open_mutex
);
1797 mutex_unlock(®ister_mutex
);
1802 * snd_rawmidi_set_ops - set the rawmidi operators
1803 * @rmidi: the rawmidi instance
1804 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1805 * @ops: the operator table
1807 * Sets the rawmidi operators for the given stream direction.
1809 void snd_rawmidi_set_ops(struct snd_rawmidi
*rmidi
, int stream
,
1810 const struct snd_rawmidi_ops
*ops
)
1812 struct snd_rawmidi_substream
*substream
;
1814 list_for_each_entry(substream
, &rmidi
->streams
[stream
].substreams
, list
)
1815 substream
->ops
= ops
;
1817 EXPORT_SYMBOL(snd_rawmidi_set_ops
);
1823 static int __init
alsa_rawmidi_init(void)
1826 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl
);
1827 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl
);
1828 #ifdef CONFIG_SND_OSSEMUL
1830 /* check device map table */
1831 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1832 if (midi_map
[i
] < 0 || midi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1833 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1837 if (amidi_map
[i
] < 0 || amidi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1838 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1844 #endif /* CONFIG_SND_OSSEMUL */
1848 static void __exit
alsa_rawmidi_exit(void)
1850 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl
);
1851 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl
);
1854 module_init(alsa_rawmidi_init
)
1855 module_exit(alsa_rawmidi_exit
)