2 * Abstract layer for MIDI v1.0 stream
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/core.h>
23 #include <linux/major.h>
24 #include <linux/init.h>
25 #include <linux/sched/signal.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/wait.h>
29 #include <linux/mutex.h>
30 #include <linux/module.h>
31 #include <linux/delay.h>
32 #include <sound/rawmidi.h>
33 #include <sound/info.h>
34 #include <sound/control.h>
35 #include <sound/minors.h>
36 #include <sound/initval.h>
38 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
39 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
40 MODULE_LICENSE("GPL");
42 #ifdef CONFIG_SND_OSSEMUL
43 static int midi_map
[SNDRV_CARDS
];
44 static int amidi_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
45 module_param_array(midi_map
, int, NULL
, 0444);
46 MODULE_PARM_DESC(midi_map
, "Raw MIDI device number assigned to 1st OSS device.");
47 module_param_array(amidi_map
, int, NULL
, 0444);
48 MODULE_PARM_DESC(amidi_map
, "Raw MIDI device number assigned to 2nd OSS device.");
49 #endif /* CONFIG_SND_OSSEMUL */
51 static int snd_rawmidi_free(struct snd_rawmidi
*rawmidi
);
52 static int snd_rawmidi_dev_free(struct snd_device
*device
);
53 static int snd_rawmidi_dev_register(struct snd_device
*device
);
54 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
);
56 static LIST_HEAD(snd_rawmidi_devices
);
57 static DEFINE_MUTEX(register_mutex
);
59 #define rmidi_err(rmidi, fmt, args...) \
60 dev_err(&(rmidi)->dev, fmt, ##args)
61 #define rmidi_warn(rmidi, fmt, args...) \
62 dev_warn(&(rmidi)->dev, fmt, ##args)
63 #define rmidi_dbg(rmidi, fmt, args...) \
64 dev_dbg(&(rmidi)->dev, fmt, ##args)
66 static struct snd_rawmidi
*snd_rawmidi_search(struct snd_card
*card
, int device
)
68 struct snd_rawmidi
*rawmidi
;
70 list_for_each_entry(rawmidi
, &snd_rawmidi_devices
, list
)
71 if (rawmidi
->card
== card
&& rawmidi
->device
== device
)
76 static inline unsigned short snd_rawmidi_file_flags(struct file
*file
)
78 switch (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) {
80 return SNDRV_RAWMIDI_LFLG_OUTPUT
;
82 return SNDRV_RAWMIDI_LFLG_INPUT
;
84 return SNDRV_RAWMIDI_LFLG_OPEN
;
88 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream
*substream
)
90 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
91 return runtime
->avail
>= runtime
->avail_min
;
94 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream
*substream
,
97 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
98 return runtime
->avail
>= runtime
->avail_min
&&
99 (!substream
->append
|| runtime
->avail
>= count
);
102 static void snd_rawmidi_input_event_work(struct work_struct
*work
)
104 struct snd_rawmidi_runtime
*runtime
=
105 container_of(work
, struct snd_rawmidi_runtime
, event_work
);
107 runtime
->event(runtime
->substream
);
110 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream
*substream
)
112 struct snd_rawmidi_runtime
*runtime
;
114 if ((runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
)) == NULL
)
116 runtime
->substream
= substream
;
117 spin_lock_init(&runtime
->lock
);
118 init_waitqueue_head(&runtime
->sleep
);
119 INIT_WORK(&runtime
->event_work
, snd_rawmidi_input_event_work
);
120 runtime
->event
= NULL
;
121 runtime
->buffer_size
= PAGE_SIZE
;
122 runtime
->avail_min
= 1;
123 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
126 runtime
->avail
= runtime
->buffer_size
;
127 if ((runtime
->buffer
= kmalloc(runtime
->buffer_size
, GFP_KERNEL
)) == NULL
) {
131 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
132 substream
->runtime
= runtime
;
136 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream
*substream
)
138 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
140 kfree(runtime
->buffer
);
142 substream
->runtime
= NULL
;
146 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream
*substream
,int up
)
148 if (!substream
->opened
)
150 substream
->ops
->trigger(substream
, up
);
153 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
155 if (!substream
->opened
)
157 substream
->ops
->trigger(substream
, up
);
159 cancel_work_sync(&substream
->runtime
->event_work
);
162 int snd_rawmidi_drop_output(struct snd_rawmidi_substream
*substream
)
165 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
167 snd_rawmidi_output_trigger(substream
, 0);
169 spin_lock_irqsave(&runtime
->lock
, flags
);
170 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
171 runtime
->avail
= runtime
->buffer_size
;
172 spin_unlock_irqrestore(&runtime
->lock
, flags
);
175 EXPORT_SYMBOL(snd_rawmidi_drop_output
);
177 int snd_rawmidi_drain_output(struct snd_rawmidi_substream
*substream
)
181 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
185 timeout
= wait_event_interruptible_timeout(runtime
->sleep
,
186 (runtime
->avail
>= runtime
->buffer_size
),
188 if (signal_pending(current
))
190 if (runtime
->avail
< runtime
->buffer_size
&& !timeout
) {
191 rmidi_warn(substream
->rmidi
,
192 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
193 (long)runtime
->avail
, (long)runtime
->buffer_size
);
197 if (err
!= -ERESTARTSYS
) {
198 /* we need wait a while to make sure that Tx FIFOs are empty */
199 if (substream
->ops
->drain
)
200 substream
->ops
->drain(substream
);
203 snd_rawmidi_drop_output(substream
);
207 EXPORT_SYMBOL(snd_rawmidi_drain_output
);
209 int snd_rawmidi_drain_input(struct snd_rawmidi_substream
*substream
)
212 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
214 snd_rawmidi_input_trigger(substream
, 0);
216 spin_lock_irqsave(&runtime
->lock
, flags
);
217 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
219 spin_unlock_irqrestore(&runtime
->lock
, flags
);
222 EXPORT_SYMBOL(snd_rawmidi_drain_input
);
224 /* look for an available substream for the given stream direction;
225 * if a specific subdevice is given, try to assign it
227 static int assign_substream(struct snd_rawmidi
*rmidi
, int subdevice
,
228 int stream
, int mode
,
229 struct snd_rawmidi_substream
**sub_ret
)
231 struct snd_rawmidi_substream
*substream
;
232 struct snd_rawmidi_str
*s
= &rmidi
->streams
[stream
];
233 static unsigned int info_flags
[2] = {
234 [SNDRV_RAWMIDI_STREAM_OUTPUT
] = SNDRV_RAWMIDI_INFO_OUTPUT
,
235 [SNDRV_RAWMIDI_STREAM_INPUT
] = SNDRV_RAWMIDI_INFO_INPUT
,
238 if (!(rmidi
->info_flags
& info_flags
[stream
]))
240 if (subdevice
>= 0 && subdevice
>= s
->substream_count
)
243 list_for_each_entry(substream
, &s
->substreams
, list
) {
244 if (substream
->opened
) {
245 if (stream
== SNDRV_RAWMIDI_STREAM_INPUT
||
246 !(mode
& SNDRV_RAWMIDI_LFLG_APPEND
) ||
250 if (subdevice
< 0 || subdevice
== substream
->number
) {
251 *sub_ret
= substream
;
258 /* open and do ref-counting for the given substream */
259 static int open_substream(struct snd_rawmidi
*rmidi
,
260 struct snd_rawmidi_substream
*substream
,
265 if (substream
->use_count
== 0) {
266 err
= snd_rawmidi_runtime_create(substream
);
269 err
= substream
->ops
->open(substream
);
271 snd_rawmidi_runtime_free(substream
);
274 substream
->opened
= 1;
275 substream
->active_sensing
= 0;
276 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
)
277 substream
->append
= 1;
278 substream
->pid
= get_pid(task_pid(current
));
279 rmidi
->streams
[substream
->stream
].substream_opened
++;
281 substream
->use_count
++;
285 static void close_substream(struct snd_rawmidi
*rmidi
,
286 struct snd_rawmidi_substream
*substream
,
289 static int rawmidi_open_priv(struct snd_rawmidi
*rmidi
, int subdevice
, int mode
,
290 struct snd_rawmidi_file
*rfile
)
292 struct snd_rawmidi_substream
*sinput
= NULL
, *soutput
= NULL
;
295 rfile
->input
= rfile
->output
= NULL
;
296 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
297 err
= assign_substream(rmidi
, subdevice
,
298 SNDRV_RAWMIDI_STREAM_INPUT
,
303 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
304 err
= assign_substream(rmidi
, subdevice
,
305 SNDRV_RAWMIDI_STREAM_OUTPUT
,
312 err
= open_substream(rmidi
, sinput
, mode
);
317 err
= open_substream(rmidi
, soutput
, mode
);
320 close_substream(rmidi
, sinput
, 0);
325 rfile
->rmidi
= rmidi
;
326 rfile
->input
= sinput
;
327 rfile
->output
= soutput
;
331 /* called from sound/core/seq/seq_midi.c */
332 int snd_rawmidi_kernel_open(struct snd_card
*card
, int device
, int subdevice
,
333 int mode
, struct snd_rawmidi_file
* rfile
)
335 struct snd_rawmidi
*rmidi
;
338 if (snd_BUG_ON(!rfile
))
341 mutex_lock(®ister_mutex
);
342 rmidi
= snd_rawmidi_search(card
, device
);
344 mutex_unlock(®ister_mutex
);
347 if (!try_module_get(rmidi
->card
->module
)) {
348 mutex_unlock(®ister_mutex
);
351 mutex_unlock(®ister_mutex
);
353 mutex_lock(&rmidi
->open_mutex
);
354 err
= rawmidi_open_priv(rmidi
, subdevice
, mode
, rfile
);
355 mutex_unlock(&rmidi
->open_mutex
);
357 module_put(rmidi
->card
->module
);
360 EXPORT_SYMBOL(snd_rawmidi_kernel_open
);
362 static int snd_rawmidi_open(struct inode
*inode
, struct file
*file
)
364 int maj
= imajor(inode
);
365 struct snd_card
*card
;
367 unsigned short fflags
;
369 struct snd_rawmidi
*rmidi
;
370 struct snd_rawmidi_file
*rawmidi_file
= NULL
;
371 wait_queue_entry_t wait
;
373 if ((file
->f_flags
& O_APPEND
) && !(file
->f_flags
& O_NONBLOCK
))
374 return -EINVAL
; /* invalid combination */
376 err
= nonseekable_open(inode
, file
);
380 if (maj
== snd_major
) {
381 rmidi
= snd_lookup_minor_data(iminor(inode
),
382 SNDRV_DEVICE_TYPE_RAWMIDI
);
383 #ifdef CONFIG_SND_OSSEMUL
384 } else if (maj
== SOUND_MAJOR
) {
385 rmidi
= snd_lookup_oss_minor_data(iminor(inode
),
386 SNDRV_OSS_DEVICE_TYPE_MIDI
);
394 if (!try_module_get(rmidi
->card
->module
)) {
395 snd_card_unref(rmidi
->card
);
399 mutex_lock(&rmidi
->open_mutex
);
401 err
= snd_card_file_add(card
, file
);
404 fflags
= snd_rawmidi_file_flags(file
);
405 if ((file
->f_flags
& O_APPEND
) || maj
== SOUND_MAJOR
) /* OSS emul? */
406 fflags
|= SNDRV_RAWMIDI_LFLG_APPEND
;
407 rawmidi_file
= kmalloc(sizeof(*rawmidi_file
), GFP_KERNEL
);
408 if (rawmidi_file
== NULL
) {
412 init_waitqueue_entry(&wait
, current
);
413 add_wait_queue(&rmidi
->open_wait
, &wait
);
415 subdevice
= snd_ctl_get_preferred_subdevice(card
, SND_CTL_SUBDEV_RAWMIDI
);
416 err
= rawmidi_open_priv(rmidi
, subdevice
, fflags
, rawmidi_file
);
419 if (err
== -EAGAIN
) {
420 if (file
->f_flags
& O_NONBLOCK
) {
426 set_current_state(TASK_INTERRUPTIBLE
);
427 mutex_unlock(&rmidi
->open_mutex
);
429 mutex_lock(&rmidi
->open_mutex
);
430 if (rmidi
->card
->shutdown
) {
434 if (signal_pending(current
)) {
439 remove_wait_queue(&rmidi
->open_wait
, &wait
);
444 #ifdef CONFIG_SND_OSSEMUL
445 if (rawmidi_file
->input
&& rawmidi_file
->input
->runtime
)
446 rawmidi_file
->input
->runtime
->oss
= (maj
== SOUND_MAJOR
);
447 if (rawmidi_file
->output
&& rawmidi_file
->output
->runtime
)
448 rawmidi_file
->output
->runtime
->oss
= (maj
== SOUND_MAJOR
);
450 file
->private_data
= rawmidi_file
;
451 mutex_unlock(&rmidi
->open_mutex
);
452 snd_card_unref(rmidi
->card
);
456 snd_card_file_remove(card
, file
);
458 mutex_unlock(&rmidi
->open_mutex
);
459 module_put(rmidi
->card
->module
);
460 snd_card_unref(rmidi
->card
);
464 static void close_substream(struct snd_rawmidi
*rmidi
,
465 struct snd_rawmidi_substream
*substream
,
468 if (--substream
->use_count
)
472 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
473 snd_rawmidi_input_trigger(substream
, 0);
475 if (substream
->active_sensing
) {
476 unsigned char buf
= 0xfe;
477 /* sending single active sensing message
478 * to shut the device up
480 snd_rawmidi_kernel_write(substream
, &buf
, 1);
482 if (snd_rawmidi_drain_output(substream
) == -ERESTARTSYS
)
483 snd_rawmidi_output_trigger(substream
, 0);
486 substream
->ops
->close(substream
);
487 if (substream
->runtime
->private_free
)
488 substream
->runtime
->private_free(substream
);
489 snd_rawmidi_runtime_free(substream
);
490 substream
->opened
= 0;
491 substream
->append
= 0;
492 put_pid(substream
->pid
);
493 substream
->pid
= NULL
;
494 rmidi
->streams
[substream
->stream
].substream_opened
--;
497 static void rawmidi_release_priv(struct snd_rawmidi_file
*rfile
)
499 struct snd_rawmidi
*rmidi
;
501 rmidi
= rfile
->rmidi
;
502 mutex_lock(&rmidi
->open_mutex
);
504 close_substream(rmidi
, rfile
->input
, 1);
508 close_substream(rmidi
, rfile
->output
, 1);
509 rfile
->output
= NULL
;
512 mutex_unlock(&rmidi
->open_mutex
);
513 wake_up(&rmidi
->open_wait
);
516 /* called from sound/core/seq/seq_midi.c */
517 int snd_rawmidi_kernel_release(struct snd_rawmidi_file
*rfile
)
519 struct snd_rawmidi
*rmidi
;
521 if (snd_BUG_ON(!rfile
))
524 rmidi
= rfile
->rmidi
;
525 rawmidi_release_priv(rfile
);
526 module_put(rmidi
->card
->module
);
529 EXPORT_SYMBOL(snd_rawmidi_kernel_release
);
531 static int snd_rawmidi_release(struct inode
*inode
, struct file
*file
)
533 struct snd_rawmidi_file
*rfile
;
534 struct snd_rawmidi
*rmidi
;
535 struct module
*module
;
537 rfile
= file
->private_data
;
538 rmidi
= rfile
->rmidi
;
539 rawmidi_release_priv(rfile
);
541 module
= rmidi
->card
->module
;
542 snd_card_file_remove(rmidi
->card
, file
);
547 static int snd_rawmidi_info(struct snd_rawmidi_substream
*substream
,
548 struct snd_rawmidi_info
*info
)
550 struct snd_rawmidi
*rmidi
;
552 if (substream
== NULL
)
554 rmidi
= substream
->rmidi
;
555 memset(info
, 0, sizeof(*info
));
556 info
->card
= rmidi
->card
->number
;
557 info
->device
= rmidi
->device
;
558 info
->subdevice
= substream
->number
;
559 info
->stream
= substream
->stream
;
560 info
->flags
= rmidi
->info_flags
;
561 strcpy(info
->id
, rmidi
->id
);
562 strcpy(info
->name
, rmidi
->name
);
563 strcpy(info
->subname
, substream
->name
);
564 info
->subdevices_count
= substream
->pstr
->substream_count
;
565 info
->subdevices_avail
= (substream
->pstr
->substream_count
-
566 substream
->pstr
->substream_opened
);
570 static int snd_rawmidi_info_user(struct snd_rawmidi_substream
*substream
,
571 struct snd_rawmidi_info __user
* _info
)
573 struct snd_rawmidi_info info
;
575 if ((err
= snd_rawmidi_info(substream
, &info
)) < 0)
577 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
582 static int __snd_rawmidi_info_select(struct snd_card
*card
,
583 struct snd_rawmidi_info
*info
)
585 struct snd_rawmidi
*rmidi
;
586 struct snd_rawmidi_str
*pstr
;
587 struct snd_rawmidi_substream
*substream
;
589 rmidi
= snd_rawmidi_search(card
, info
->device
);
592 if (info
->stream
< 0 || info
->stream
> 1)
594 pstr
= &rmidi
->streams
[info
->stream
];
595 if (pstr
->substream_count
== 0)
597 if (info
->subdevice
>= pstr
->substream_count
)
599 list_for_each_entry(substream
, &pstr
->substreams
, list
) {
600 if ((unsigned int)substream
->number
== info
->subdevice
)
601 return snd_rawmidi_info(substream
, info
);
606 int snd_rawmidi_info_select(struct snd_card
*card
, struct snd_rawmidi_info
*info
)
610 mutex_lock(®ister_mutex
);
611 ret
= __snd_rawmidi_info_select(card
, info
);
612 mutex_unlock(®ister_mutex
);
615 EXPORT_SYMBOL(snd_rawmidi_info_select
);
617 static int snd_rawmidi_info_select_user(struct snd_card
*card
,
618 struct snd_rawmidi_info __user
*_info
)
621 struct snd_rawmidi_info info
;
622 if (get_user(info
.device
, &_info
->device
))
624 if (get_user(info
.stream
, &_info
->stream
))
626 if (get_user(info
.subdevice
, &_info
->subdevice
))
628 if ((err
= snd_rawmidi_info_select(card
, &info
)) < 0)
630 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
635 int snd_rawmidi_output_params(struct snd_rawmidi_substream
*substream
,
636 struct snd_rawmidi_params
* params
)
638 char *newbuf
, *oldbuf
;
639 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
641 if (substream
->append
&& substream
->use_count
> 1)
643 snd_rawmidi_drain_output(substream
);
644 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
647 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
650 if (params
->buffer_size
!= runtime
->buffer_size
) {
651 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
654 spin_lock_irq(&runtime
->lock
);
655 oldbuf
= runtime
->buffer
;
656 runtime
->buffer
= newbuf
;
657 runtime
->buffer_size
= params
->buffer_size
;
658 runtime
->avail
= runtime
->buffer_size
;
659 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
660 spin_unlock_irq(&runtime
->lock
);
663 runtime
->avail_min
= params
->avail_min
;
664 substream
->active_sensing
= !params
->no_active_sensing
;
667 EXPORT_SYMBOL(snd_rawmidi_output_params
);
669 int snd_rawmidi_input_params(struct snd_rawmidi_substream
*substream
,
670 struct snd_rawmidi_params
* params
)
672 char *newbuf
, *oldbuf
;
673 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
675 snd_rawmidi_drain_input(substream
);
676 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
679 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
682 if (params
->buffer_size
!= runtime
->buffer_size
) {
683 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
686 spin_lock_irq(&runtime
->lock
);
687 oldbuf
= runtime
->buffer
;
688 runtime
->buffer
= newbuf
;
689 runtime
->buffer_size
= params
->buffer_size
;
690 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
691 spin_unlock_irq(&runtime
->lock
);
694 runtime
->avail_min
= params
->avail_min
;
697 EXPORT_SYMBOL(snd_rawmidi_input_params
);
699 static int snd_rawmidi_output_status(struct snd_rawmidi_substream
*substream
,
700 struct snd_rawmidi_status
* status
)
702 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
704 memset(status
, 0, sizeof(*status
));
705 status
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
706 spin_lock_irq(&runtime
->lock
);
707 status
->avail
= runtime
->avail
;
708 spin_unlock_irq(&runtime
->lock
);
712 static int snd_rawmidi_input_status(struct snd_rawmidi_substream
*substream
,
713 struct snd_rawmidi_status
* status
)
715 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
717 memset(status
, 0, sizeof(*status
));
718 status
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
719 spin_lock_irq(&runtime
->lock
);
720 status
->avail
= runtime
->avail
;
721 status
->xruns
= runtime
->xruns
;
723 spin_unlock_irq(&runtime
->lock
);
727 static long snd_rawmidi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
729 struct snd_rawmidi_file
*rfile
;
730 void __user
*argp
= (void __user
*)arg
;
732 rfile
= file
->private_data
;
733 if (((cmd
>> 8) & 0xff) != 'W')
736 case SNDRV_RAWMIDI_IOCTL_PVERSION
:
737 return put_user(SNDRV_RAWMIDI_VERSION
, (int __user
*)argp
) ? -EFAULT
: 0;
738 case SNDRV_RAWMIDI_IOCTL_INFO
:
741 struct snd_rawmidi_info __user
*info
= argp
;
742 if (get_user(stream
, &info
->stream
))
745 case SNDRV_RAWMIDI_STREAM_INPUT
:
746 return snd_rawmidi_info_user(rfile
->input
, info
);
747 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
748 return snd_rawmidi_info_user(rfile
->output
, info
);
753 case SNDRV_RAWMIDI_IOCTL_PARAMS
:
755 struct snd_rawmidi_params params
;
756 if (copy_from_user(¶ms
, argp
, sizeof(struct snd_rawmidi_params
)))
758 switch (params
.stream
) {
759 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
760 if (rfile
->output
== NULL
)
762 return snd_rawmidi_output_params(rfile
->output
, ¶ms
);
763 case SNDRV_RAWMIDI_STREAM_INPUT
:
764 if (rfile
->input
== NULL
)
766 return snd_rawmidi_input_params(rfile
->input
, ¶ms
);
771 case SNDRV_RAWMIDI_IOCTL_STATUS
:
774 struct snd_rawmidi_status status
;
775 if (copy_from_user(&status
, argp
, sizeof(struct snd_rawmidi_status
)))
777 switch (status
.stream
) {
778 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
779 if (rfile
->output
== NULL
)
781 err
= snd_rawmidi_output_status(rfile
->output
, &status
);
783 case SNDRV_RAWMIDI_STREAM_INPUT
:
784 if (rfile
->input
== NULL
)
786 err
= snd_rawmidi_input_status(rfile
->input
, &status
);
793 if (copy_to_user(argp
, &status
, sizeof(struct snd_rawmidi_status
)))
797 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
:
814 if (get_user(val
, (int __user
*) argp
))
817 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
818 if (rfile
->output
== NULL
)
820 return snd_rawmidi_drain_output(rfile
->output
);
821 case SNDRV_RAWMIDI_STREAM_INPUT
:
822 if (rfile
->input
== NULL
)
824 return snd_rawmidi_drain_input(rfile
->input
);
830 rmidi_dbg(rfile
->rmidi
,
831 "rawmidi: unknown command = 0x%x\n", cmd
);
836 static int snd_rawmidi_control_ioctl(struct snd_card
*card
,
837 struct snd_ctl_file
*control
,
841 void __user
*argp
= (void __user
*)arg
;
844 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE
:
848 if (get_user(device
, (int __user
*)argp
))
850 if (device
>= SNDRV_RAWMIDI_DEVICES
) /* next device is -1 */
851 device
= SNDRV_RAWMIDI_DEVICES
- 1;
852 mutex_lock(®ister_mutex
);
853 device
= device
< 0 ? 0 : device
+ 1;
854 while (device
< SNDRV_RAWMIDI_DEVICES
) {
855 if (snd_rawmidi_search(card
, device
))
859 if (device
== SNDRV_RAWMIDI_DEVICES
)
861 mutex_unlock(®ister_mutex
);
862 if (put_user(device
, (int __user
*)argp
))
866 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE
:
870 if (get_user(val
, (int __user
*)argp
))
872 control
->preferred_subdevice
[SND_CTL_SUBDEV_RAWMIDI
] = val
;
875 case SNDRV_CTL_IOCTL_RAWMIDI_INFO
:
876 return snd_rawmidi_info_select_user(card
, argp
);
882 * snd_rawmidi_receive - receive the input data from the device
883 * @substream: the rawmidi substream
884 * @buffer: the buffer pointer
885 * @count: the data size to read
887 * Reads the data from the internal buffer.
889 * Return: The size of read data, or a negative error code on failure.
891 int snd_rawmidi_receive(struct snd_rawmidi_substream
*substream
,
892 const unsigned char *buffer
, int count
)
895 int result
= 0, count1
;
896 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
898 if (!substream
->opened
)
900 if (runtime
->buffer
== NULL
) {
901 rmidi_dbg(substream
->rmidi
,
902 "snd_rawmidi_receive: input is not active!!!\n");
905 spin_lock_irqsave(&runtime
->lock
, flags
);
906 if (count
== 1) { /* special case, faster code */
908 if (runtime
->avail
< runtime
->buffer_size
) {
909 runtime
->buffer
[runtime
->hw_ptr
++] = buffer
[0];
910 runtime
->hw_ptr
%= runtime
->buffer_size
;
917 substream
->bytes
+= count
;
918 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
921 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
922 count1
= runtime
->buffer_size
- runtime
->avail
;
923 memcpy(runtime
->buffer
+ runtime
->hw_ptr
, buffer
, count1
);
924 runtime
->hw_ptr
+= count1
;
925 runtime
->hw_ptr
%= runtime
->buffer_size
;
926 runtime
->avail
+= count1
;
932 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
)) {
933 count1
= runtime
->buffer_size
- runtime
->avail
;
934 runtime
->xruns
+= count
- count1
;
937 memcpy(runtime
->buffer
, buffer
, count1
);
938 runtime
->hw_ptr
= count1
;
939 runtime
->avail
+= count1
;
946 schedule_work(&runtime
->event_work
);
947 else if (snd_rawmidi_ready(substream
))
948 wake_up(&runtime
->sleep
);
950 spin_unlock_irqrestore(&runtime
->lock
, flags
);
953 EXPORT_SYMBOL(snd_rawmidi_receive
);
955 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream
*substream
,
956 unsigned char __user
*userbuf
,
957 unsigned char *kernelbuf
, long count
)
960 long result
= 0, count1
;
961 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
962 unsigned long appl_ptr
;
964 spin_lock_irqsave(&runtime
->lock
, flags
);
965 while (count
> 0 && runtime
->avail
) {
966 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
969 if (count1
> (int)runtime
->avail
)
970 count1
= runtime
->avail
;
972 /* update runtime->appl_ptr before unlocking for userbuf */
973 appl_ptr
= runtime
->appl_ptr
;
974 runtime
->appl_ptr
+= count1
;
975 runtime
->appl_ptr
%= runtime
->buffer_size
;
976 runtime
->avail
-= count1
;
979 memcpy(kernelbuf
+ result
, runtime
->buffer
+ appl_ptr
, count1
);
981 spin_unlock_irqrestore(&runtime
->lock
, flags
);
982 if (copy_to_user(userbuf
+ result
,
983 runtime
->buffer
+ appl_ptr
, count1
)) {
984 return result
> 0 ? result
: -EFAULT
;
986 spin_lock_irqsave(&runtime
->lock
, flags
);
991 spin_unlock_irqrestore(&runtime
->lock
, flags
);
995 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream
*substream
,
996 unsigned char *buf
, long count
)
998 snd_rawmidi_input_trigger(substream
, 1);
999 return snd_rawmidi_kernel_read1(substream
, NULL
/*userbuf*/, buf
, count
);
1001 EXPORT_SYMBOL(snd_rawmidi_kernel_read
);
1003 static ssize_t
snd_rawmidi_read(struct file
*file
, char __user
*buf
, size_t count
,
1008 struct snd_rawmidi_file
*rfile
;
1009 struct snd_rawmidi_substream
*substream
;
1010 struct snd_rawmidi_runtime
*runtime
;
1012 rfile
= file
->private_data
;
1013 substream
= rfile
->input
;
1014 if (substream
== NULL
)
1016 runtime
= substream
->runtime
;
1017 snd_rawmidi_input_trigger(substream
, 1);
1020 spin_lock_irq(&runtime
->lock
);
1021 while (!snd_rawmidi_ready(substream
)) {
1022 wait_queue_entry_t wait
;
1023 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1024 spin_unlock_irq(&runtime
->lock
);
1025 return result
> 0 ? result
: -EAGAIN
;
1027 init_waitqueue_entry(&wait
, current
);
1028 add_wait_queue(&runtime
->sleep
, &wait
);
1029 set_current_state(TASK_INTERRUPTIBLE
);
1030 spin_unlock_irq(&runtime
->lock
);
1032 remove_wait_queue(&runtime
->sleep
, &wait
);
1033 if (rfile
->rmidi
->card
->shutdown
)
1035 if (signal_pending(current
))
1036 return result
> 0 ? result
: -ERESTARTSYS
;
1037 if (!runtime
->avail
)
1038 return result
> 0 ? result
: -EIO
;
1039 spin_lock_irq(&runtime
->lock
);
1041 spin_unlock_irq(&runtime
->lock
);
1042 count1
= snd_rawmidi_kernel_read1(substream
,
1043 (unsigned char __user
*)buf
,
1047 return result
> 0 ? result
: count1
;
1056 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1057 * @substream: the rawmidi substream
1059 * Return: 1 if the internal output buffer is empty, 0 if not.
1061 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream
*substream
)
1063 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1065 unsigned long flags
;
1067 if (runtime
->buffer
== NULL
) {
1068 rmidi_dbg(substream
->rmidi
,
1069 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1072 spin_lock_irqsave(&runtime
->lock
, flags
);
1073 result
= runtime
->avail
>= runtime
->buffer_size
;
1074 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1077 EXPORT_SYMBOL(snd_rawmidi_transmit_empty
);
1080 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1081 * @substream: the rawmidi substream
1082 * @buffer: the buffer pointer
1083 * @count: data size to transfer
1085 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1087 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1088 unsigned char *buffer
, int count
)
1091 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1093 if (runtime
->buffer
== NULL
) {
1094 rmidi_dbg(substream
->rmidi
,
1095 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1099 if (runtime
->avail
>= runtime
->buffer_size
) {
1100 /* warning: lowlevel layer MUST trigger down the hardware */
1103 if (count
== 1) { /* special case, faster code */
1104 *buffer
= runtime
->buffer
[runtime
->hw_ptr
];
1107 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
1110 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
1111 count1
= runtime
->buffer_size
- runtime
->avail
;
1112 memcpy(buffer
, runtime
->buffer
+ runtime
->hw_ptr
, count1
);
1116 if (count
> (int)(runtime
->buffer_size
- runtime
->avail
- count1
))
1117 count
= runtime
->buffer_size
- runtime
->avail
- count1
;
1118 memcpy(buffer
+ count1
, runtime
->buffer
, count
);
1125 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek
);
1128 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1129 * @substream: the rawmidi substream
1130 * @buffer: the buffer pointer
1131 * @count: data size to transfer
1133 * Copies data from the internal output buffer to the given buffer.
1135 * Call this in the interrupt handler when the midi output is ready,
1136 * and call snd_rawmidi_transmit_ack() after the transmission is
1139 * Return: The size of copied data, or a negative error code on failure.
1141 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1142 unsigned char *buffer
, int count
)
1144 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1146 unsigned long flags
;
1148 spin_lock_irqsave(&runtime
->lock
, flags
);
1149 result
= __snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1150 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1153 EXPORT_SYMBOL(snd_rawmidi_transmit_peek
);
1156 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1157 * @substream: the rawmidi substream
1158 * @count: the transferred count
1160 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1162 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1164 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1166 if (runtime
->buffer
== NULL
) {
1167 rmidi_dbg(substream
->rmidi
,
1168 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1171 snd_BUG_ON(runtime
->avail
+ count
> runtime
->buffer_size
);
1172 runtime
->hw_ptr
+= count
;
1173 runtime
->hw_ptr
%= runtime
->buffer_size
;
1174 runtime
->avail
+= count
;
1175 substream
->bytes
+= count
;
1177 if (runtime
->drain
|| snd_rawmidi_ready(substream
))
1178 wake_up(&runtime
->sleep
);
1182 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack
);
1185 * snd_rawmidi_transmit_ack - acknowledge the transmission
1186 * @substream: the rawmidi substream
1187 * @count: the transferred count
1189 * Advances the hardware pointer for the internal output buffer with
1190 * the given size and updates the condition.
1191 * Call after the transmission is finished.
1193 * Return: The advanced size if successful, or a negative error code on failure.
1195 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1197 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1199 unsigned long flags
;
1201 spin_lock_irqsave(&runtime
->lock
, flags
);
1202 result
= __snd_rawmidi_transmit_ack(substream
, count
);
1203 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1206 EXPORT_SYMBOL(snd_rawmidi_transmit_ack
);
1209 * snd_rawmidi_transmit - copy from the buffer to the device
1210 * @substream: the rawmidi substream
1211 * @buffer: the buffer pointer
1212 * @count: the data size to transfer
1214 * Copies data from the buffer to the device and advances the pointer.
1216 * Return: The copied size if successful, or a negative error code on failure.
1218 int snd_rawmidi_transmit(struct snd_rawmidi_substream
*substream
,
1219 unsigned char *buffer
, int count
)
1221 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1223 unsigned long flags
;
1225 spin_lock_irqsave(&runtime
->lock
, flags
);
1226 if (!substream
->opened
)
1229 count
= __snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1233 result
= __snd_rawmidi_transmit_ack(substream
, count
);
1235 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1238 EXPORT_SYMBOL(snd_rawmidi_transmit
);
1240 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream
*substream
,
1241 const unsigned char __user
*userbuf
,
1242 const unsigned char *kernelbuf
,
1245 unsigned long flags
;
1246 long count1
, result
;
1247 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1248 unsigned long appl_ptr
;
1250 if (!kernelbuf
&& !userbuf
)
1252 if (snd_BUG_ON(!runtime
->buffer
))
1256 spin_lock_irqsave(&runtime
->lock
, flags
);
1257 if (substream
->append
) {
1258 if ((long)runtime
->avail
< count
) {
1259 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1263 while (count
> 0 && runtime
->avail
> 0) {
1264 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1267 if (count1
> (long)runtime
->avail
)
1268 count1
= runtime
->avail
;
1270 /* update runtime->appl_ptr before unlocking for userbuf */
1271 appl_ptr
= runtime
->appl_ptr
;
1272 runtime
->appl_ptr
+= count1
;
1273 runtime
->appl_ptr
%= runtime
->buffer_size
;
1274 runtime
->avail
-= count1
;
1277 memcpy(runtime
->buffer
+ appl_ptr
,
1278 kernelbuf
+ result
, count1
);
1280 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1281 if (copy_from_user(runtime
->buffer
+ appl_ptr
,
1282 userbuf
+ result
, count1
)) {
1283 spin_lock_irqsave(&runtime
->lock
, flags
);
1284 result
= result
> 0 ? result
: -EFAULT
;
1287 spin_lock_irqsave(&runtime
->lock
, flags
);
1293 count1
= runtime
->avail
< runtime
->buffer_size
;
1294 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1296 snd_rawmidi_output_trigger(substream
, 1);
1300 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream
*substream
,
1301 const unsigned char *buf
, long count
)
1303 return snd_rawmidi_kernel_write1(substream
, NULL
, buf
, count
);
1305 EXPORT_SYMBOL(snd_rawmidi_kernel_write
);
1307 static ssize_t
snd_rawmidi_write(struct file
*file
, const char __user
*buf
,
1308 size_t count
, loff_t
*offset
)
1310 long result
, timeout
;
1312 struct snd_rawmidi_file
*rfile
;
1313 struct snd_rawmidi_runtime
*runtime
;
1314 struct snd_rawmidi_substream
*substream
;
1316 rfile
= file
->private_data
;
1317 substream
= rfile
->output
;
1318 runtime
= substream
->runtime
;
1319 /* we cannot put an atomic message to our buffer */
1320 if (substream
->append
&& count
> runtime
->buffer_size
)
1324 spin_lock_irq(&runtime
->lock
);
1325 while (!snd_rawmidi_ready_append(substream
, count
)) {
1326 wait_queue_entry_t wait
;
1327 if (file
->f_flags
& O_NONBLOCK
) {
1328 spin_unlock_irq(&runtime
->lock
);
1329 return result
> 0 ? result
: -EAGAIN
;
1331 init_waitqueue_entry(&wait
, current
);
1332 add_wait_queue(&runtime
->sleep
, &wait
);
1333 set_current_state(TASK_INTERRUPTIBLE
);
1334 spin_unlock_irq(&runtime
->lock
);
1335 timeout
= schedule_timeout(30 * HZ
);
1336 remove_wait_queue(&runtime
->sleep
, &wait
);
1337 if (rfile
->rmidi
->card
->shutdown
)
1339 if (signal_pending(current
))
1340 return result
> 0 ? result
: -ERESTARTSYS
;
1341 if (!runtime
->avail
&& !timeout
)
1342 return result
> 0 ? result
: -EIO
;
1343 spin_lock_irq(&runtime
->lock
);
1345 spin_unlock_irq(&runtime
->lock
);
1346 count1
= snd_rawmidi_kernel_write1(substream
, buf
, NULL
, count
);
1348 return result
> 0 ? result
: count1
;
1351 if ((size_t)count1
< count
&& (file
->f_flags
& O_NONBLOCK
))
1355 if (file
->f_flags
& O_DSYNC
) {
1356 spin_lock_irq(&runtime
->lock
);
1357 while (runtime
->avail
!= runtime
->buffer_size
) {
1358 wait_queue_entry_t wait
;
1359 unsigned int last_avail
= runtime
->avail
;
1360 init_waitqueue_entry(&wait
, current
);
1361 add_wait_queue(&runtime
->sleep
, &wait
);
1362 set_current_state(TASK_INTERRUPTIBLE
);
1363 spin_unlock_irq(&runtime
->lock
);
1364 timeout
= schedule_timeout(30 * HZ
);
1365 remove_wait_queue(&runtime
->sleep
, &wait
);
1366 if (signal_pending(current
))
1367 return result
> 0 ? result
: -ERESTARTSYS
;
1368 if (runtime
->avail
== last_avail
&& !timeout
)
1369 return result
> 0 ? result
: -EIO
;
1370 spin_lock_irq(&runtime
->lock
);
1372 spin_unlock_irq(&runtime
->lock
);
1377 static __poll_t
snd_rawmidi_poll(struct file
*file
, poll_table
* wait
)
1379 struct snd_rawmidi_file
*rfile
;
1380 struct snd_rawmidi_runtime
*runtime
;
1383 rfile
= file
->private_data
;
1384 if (rfile
->input
!= NULL
) {
1385 runtime
= rfile
->input
->runtime
;
1386 snd_rawmidi_input_trigger(rfile
->input
, 1);
1387 poll_wait(file
, &runtime
->sleep
, wait
);
1389 if (rfile
->output
!= NULL
) {
1390 runtime
= rfile
->output
->runtime
;
1391 poll_wait(file
, &runtime
->sleep
, wait
);
1394 if (rfile
->input
!= NULL
) {
1395 if (snd_rawmidi_ready(rfile
->input
))
1396 mask
|= EPOLLIN
| EPOLLRDNORM
;
1398 if (rfile
->output
!= NULL
) {
1399 if (snd_rawmidi_ready(rfile
->output
))
1400 mask
|= EPOLLOUT
| EPOLLWRNORM
;
1407 #ifdef CONFIG_COMPAT
1408 #include "rawmidi_compat.c"
1410 #define snd_rawmidi_ioctl_compat NULL
1417 static void snd_rawmidi_proc_info_read(struct snd_info_entry
*entry
,
1418 struct snd_info_buffer
*buffer
)
1420 struct snd_rawmidi
*rmidi
;
1421 struct snd_rawmidi_substream
*substream
;
1422 struct snd_rawmidi_runtime
*runtime
;
1424 rmidi
= entry
->private_data
;
1425 snd_iprintf(buffer
, "%s\n\n", rmidi
->name
);
1426 mutex_lock(&rmidi
->open_mutex
);
1427 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) {
1428 list_for_each_entry(substream
,
1429 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
,
1433 " Tx bytes : %lu\n",
1435 (unsigned long) substream
->bytes
);
1436 if (substream
->opened
) {
1438 " Owner PID : %d\n",
1439 pid_vnr(substream
->pid
));
1440 runtime
= substream
->runtime
;
1443 " Buffer size : %lu\n"
1445 runtime
->oss
? "OSS compatible" : "native",
1446 (unsigned long) runtime
->buffer_size
,
1447 (unsigned long) runtime
->avail
);
1451 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
) {
1452 list_for_each_entry(substream
,
1453 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
,
1457 " Rx bytes : %lu\n",
1459 (unsigned long) substream
->bytes
);
1460 if (substream
->opened
) {
1462 " Owner PID : %d\n",
1463 pid_vnr(substream
->pid
));
1464 runtime
= substream
->runtime
;
1466 " Buffer size : %lu\n"
1468 " Overruns : %lu\n",
1469 (unsigned long) runtime
->buffer_size
,
1470 (unsigned long) runtime
->avail
,
1471 (unsigned long) runtime
->xruns
);
1475 mutex_unlock(&rmidi
->open_mutex
);
1479 * Register functions
1482 static const struct file_operations snd_rawmidi_f_ops
=
1484 .owner
= THIS_MODULE
,
1485 .read
= snd_rawmidi_read
,
1486 .write
= snd_rawmidi_write
,
1487 .open
= snd_rawmidi_open
,
1488 .release
= snd_rawmidi_release
,
1489 .llseek
= no_llseek
,
1490 .poll
= snd_rawmidi_poll
,
1491 .unlocked_ioctl
= snd_rawmidi_ioctl
,
1492 .compat_ioctl
= snd_rawmidi_ioctl_compat
,
1495 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi
*rmidi
,
1496 struct snd_rawmidi_str
*stream
,
1500 struct snd_rawmidi_substream
*substream
;
1503 for (idx
= 0; idx
< count
; idx
++) {
1504 substream
= kzalloc(sizeof(*substream
), GFP_KERNEL
);
1507 substream
->stream
= direction
;
1508 substream
->number
= idx
;
1509 substream
->rmidi
= rmidi
;
1510 substream
->pstr
= stream
;
1511 list_add_tail(&substream
->list
, &stream
->substreams
);
1512 stream
->substream_count
++;
1517 static void release_rawmidi_device(struct device
*dev
)
1519 kfree(container_of(dev
, struct snd_rawmidi
, dev
));
1523 * snd_rawmidi_new - create a rawmidi instance
1524 * @card: the card instance
1525 * @id: the id string
1526 * @device: the device index
1527 * @output_count: the number of output streams
1528 * @input_count: the number of input streams
1529 * @rrawmidi: the pointer to store the new rawmidi instance
1531 * Creates a new rawmidi instance.
1532 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1534 * Return: Zero if successful, or a negative error code on failure.
1536 int snd_rawmidi_new(struct snd_card
*card
, char *id
, int device
,
1537 int output_count
, int input_count
,
1538 struct snd_rawmidi
** rrawmidi
)
1540 struct snd_rawmidi
*rmidi
;
1542 static struct snd_device_ops ops
= {
1543 .dev_free
= snd_rawmidi_dev_free
,
1544 .dev_register
= snd_rawmidi_dev_register
,
1545 .dev_disconnect
= snd_rawmidi_dev_disconnect
,
1548 if (snd_BUG_ON(!card
))
1552 rmidi
= kzalloc(sizeof(*rmidi
), GFP_KERNEL
);
1556 rmidi
->device
= device
;
1557 mutex_init(&rmidi
->open_mutex
);
1558 init_waitqueue_head(&rmidi
->open_wait
);
1559 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
);
1560 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
);
1563 strlcpy(rmidi
->id
, id
, sizeof(rmidi
->id
));
1565 snd_device_initialize(&rmidi
->dev
, card
);
1566 rmidi
->dev
.release
= release_rawmidi_device
;
1567 dev_set_name(&rmidi
->dev
, "midiC%iD%i", card
->number
, device
);
1569 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1570 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
],
1571 SNDRV_RAWMIDI_STREAM_INPUT
,
1572 input_count
)) < 0) {
1573 snd_rawmidi_free(rmidi
);
1576 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1577 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
],
1578 SNDRV_RAWMIDI_STREAM_OUTPUT
,
1579 output_count
)) < 0) {
1580 snd_rawmidi_free(rmidi
);
1583 if ((err
= snd_device_new(card
, SNDRV_DEV_RAWMIDI
, rmidi
, &ops
)) < 0) {
1584 snd_rawmidi_free(rmidi
);
1591 EXPORT_SYMBOL(snd_rawmidi_new
);
1593 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str
*stream
)
1595 struct snd_rawmidi_substream
*substream
;
1597 while (!list_empty(&stream
->substreams
)) {
1598 substream
= list_entry(stream
->substreams
.next
, struct snd_rawmidi_substream
, list
);
1599 list_del(&substream
->list
);
1604 static int snd_rawmidi_free(struct snd_rawmidi
*rmidi
)
1609 snd_info_free_entry(rmidi
->proc_entry
);
1610 rmidi
->proc_entry
= NULL
;
1611 mutex_lock(®ister_mutex
);
1612 if (rmidi
->ops
&& rmidi
->ops
->dev_unregister
)
1613 rmidi
->ops
->dev_unregister(rmidi
);
1614 mutex_unlock(®ister_mutex
);
1616 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
1617 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
1618 if (rmidi
->private_free
)
1619 rmidi
->private_free(rmidi
);
1620 put_device(&rmidi
->dev
);
1624 static int snd_rawmidi_dev_free(struct snd_device
*device
)
1626 struct snd_rawmidi
*rmidi
= device
->device_data
;
1627 return snd_rawmidi_free(rmidi
);
1630 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1631 static void snd_rawmidi_dev_seq_free(struct snd_seq_device
*device
)
1633 struct snd_rawmidi
*rmidi
= device
->private_data
;
1634 rmidi
->seq_dev
= NULL
;
1638 static int snd_rawmidi_dev_register(struct snd_device
*device
)
1641 struct snd_info_entry
*entry
;
1643 struct snd_rawmidi
*rmidi
= device
->device_data
;
1645 if (rmidi
->device
>= SNDRV_RAWMIDI_DEVICES
)
1647 mutex_lock(®ister_mutex
);
1648 if (snd_rawmidi_search(rmidi
->card
, rmidi
->device
)) {
1649 mutex_unlock(®ister_mutex
);
1652 list_add_tail(&rmidi
->list
, &snd_rawmidi_devices
);
1653 mutex_unlock(®ister_mutex
);
1654 err
= snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI
,
1655 rmidi
->card
, rmidi
->device
,
1656 &snd_rawmidi_f_ops
, rmidi
, &rmidi
->dev
);
1658 rmidi_err(rmidi
, "unable to register\n");
1659 mutex_lock(®ister_mutex
);
1660 list_del(&rmidi
->list
);
1661 mutex_unlock(®ister_mutex
);
1664 if (rmidi
->ops
&& rmidi
->ops
->dev_register
&&
1665 (err
= rmidi
->ops
->dev_register(rmidi
)) < 0) {
1666 snd_unregister_device(&rmidi
->dev
);
1667 mutex_lock(®ister_mutex
);
1668 list_del(&rmidi
->list
);
1669 mutex_unlock(®ister_mutex
);
1672 #ifdef CONFIG_SND_OSSEMUL
1674 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1675 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1676 rmidi
->card
, 0, &snd_rawmidi_f_ops
,
1679 "unable to register OSS rawmidi device %i:%i\n",
1680 rmidi
->card
->number
, 0);
1683 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1684 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
, rmidi
->name
);
1688 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
]) {
1689 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1690 rmidi
->card
, 1, &snd_rawmidi_f_ops
,
1693 "unable to register OSS rawmidi device %i:%i\n",
1694 rmidi
->card
->number
, 1);
1699 #endif /* CONFIG_SND_OSSEMUL */
1700 sprintf(name
, "midi%d", rmidi
->device
);
1701 entry
= snd_info_create_card_entry(rmidi
->card
, name
, rmidi
->card
->proc_root
);
1703 entry
->private_data
= rmidi
;
1704 entry
->c
.text
.read
= snd_rawmidi_proc_info_read
;
1705 if (snd_info_register(entry
) < 0) {
1706 snd_info_free_entry(entry
);
1710 rmidi
->proc_entry
= entry
;
1711 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1712 if (!rmidi
->ops
|| !rmidi
->ops
->dev_register
) { /* own registration mechanism */
1713 if (snd_seq_device_new(rmidi
->card
, rmidi
->device
, SNDRV_SEQ_DEV_ID_MIDISYNTH
, 0, &rmidi
->seq_dev
) >= 0) {
1714 rmidi
->seq_dev
->private_data
= rmidi
;
1715 rmidi
->seq_dev
->private_free
= snd_rawmidi_dev_seq_free
;
1716 sprintf(rmidi
->seq_dev
->name
, "MIDI %d-%d", rmidi
->card
->number
, rmidi
->device
);
1717 snd_device_register(rmidi
->card
, rmidi
->seq_dev
);
1724 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
)
1726 struct snd_rawmidi
*rmidi
= device
->device_data
;
1729 mutex_lock(®ister_mutex
);
1730 mutex_lock(&rmidi
->open_mutex
);
1731 wake_up(&rmidi
->open_wait
);
1732 list_del_init(&rmidi
->list
);
1733 for (dir
= 0; dir
< 2; dir
++) {
1734 struct snd_rawmidi_substream
*s
;
1735 list_for_each_entry(s
, &rmidi
->streams
[dir
].substreams
, list
) {
1737 wake_up(&s
->runtime
->sleep
);
1741 #ifdef CONFIG_SND_OSSEMUL
1742 if (rmidi
->ossreg
) {
1743 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1744 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 0);
1745 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1746 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
);
1749 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
])
1750 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 1);
1753 #endif /* CONFIG_SND_OSSEMUL */
1754 snd_unregister_device(&rmidi
->dev
);
1755 mutex_unlock(&rmidi
->open_mutex
);
1756 mutex_unlock(®ister_mutex
);
1761 * snd_rawmidi_set_ops - set the rawmidi operators
1762 * @rmidi: the rawmidi instance
1763 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1764 * @ops: the operator table
1766 * Sets the rawmidi operators for the given stream direction.
1768 void snd_rawmidi_set_ops(struct snd_rawmidi
*rmidi
, int stream
,
1769 const struct snd_rawmidi_ops
*ops
)
1771 struct snd_rawmidi_substream
*substream
;
1773 list_for_each_entry(substream
, &rmidi
->streams
[stream
].substreams
, list
)
1774 substream
->ops
= ops
;
1776 EXPORT_SYMBOL(snd_rawmidi_set_ops
);
1782 static int __init
alsa_rawmidi_init(void)
1785 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl
);
1786 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl
);
1787 #ifdef CONFIG_SND_OSSEMUL
1789 /* check device map table */
1790 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1791 if (midi_map
[i
] < 0 || midi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1792 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1796 if (amidi_map
[i
] < 0 || amidi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1797 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1803 #endif /* CONFIG_SND_OSSEMUL */
1807 static void __exit
alsa_rawmidi_exit(void)
1809 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl
);
1810 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl
);
1813 module_init(alsa_rawmidi_init
)
1814 module_exit(alsa_rawmidi_exit
)