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.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 <linux/nospec.h>
33 #include <sound/rawmidi.h>
34 #include <sound/info.h>
35 #include <sound/control.h>
36 #include <sound/minors.h>
37 #include <sound/initval.h>
39 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
40 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
41 MODULE_LICENSE("GPL");
43 #ifdef CONFIG_SND_OSSEMUL
44 static int midi_map
[SNDRV_CARDS
];
45 static int amidi_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
46 module_param_array(midi_map
, int, NULL
, 0444);
47 MODULE_PARM_DESC(midi_map
, "Raw MIDI device number assigned to 1st OSS device.");
48 module_param_array(amidi_map
, int, NULL
, 0444);
49 MODULE_PARM_DESC(amidi_map
, "Raw MIDI device number assigned to 2nd OSS device.");
50 #endif /* CONFIG_SND_OSSEMUL */
52 static int snd_rawmidi_free(struct snd_rawmidi
*rawmidi
);
53 static int snd_rawmidi_dev_free(struct snd_device
*device
);
54 static int snd_rawmidi_dev_register(struct snd_device
*device
);
55 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
);
57 static LIST_HEAD(snd_rawmidi_devices
);
58 static DEFINE_MUTEX(register_mutex
);
60 #define rmidi_err(rmidi, fmt, args...) \
61 dev_err(&(rmidi)->dev, fmt, ##args)
62 #define rmidi_warn(rmidi, fmt, args...) \
63 dev_warn(&(rmidi)->dev, fmt, ##args)
64 #define rmidi_dbg(rmidi, fmt, args...) \
65 dev_dbg(&(rmidi)->dev, fmt, ##args)
67 static struct snd_rawmidi
*snd_rawmidi_search(struct snd_card
*card
, int device
)
69 struct snd_rawmidi
*rawmidi
;
71 list_for_each_entry(rawmidi
, &snd_rawmidi_devices
, list
)
72 if (rawmidi
->card
== card
&& rawmidi
->device
== device
)
77 static inline unsigned short snd_rawmidi_file_flags(struct file
*file
)
79 switch (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) {
81 return SNDRV_RAWMIDI_LFLG_OUTPUT
;
83 return SNDRV_RAWMIDI_LFLG_INPUT
;
85 return SNDRV_RAWMIDI_LFLG_OPEN
;
89 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream
*substream
)
91 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
92 return runtime
->avail
>= runtime
->avail_min
;
95 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream
*substream
,
98 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
99 return runtime
->avail
>= runtime
->avail_min
&&
100 (!substream
->append
|| runtime
->avail
>= count
);
103 static void snd_rawmidi_input_event_work(struct work_struct
*work
)
105 struct snd_rawmidi_runtime
*runtime
=
106 container_of(work
, struct snd_rawmidi_runtime
, event_work
);
108 runtime
->event(runtime
->substream
);
111 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream
*substream
)
113 struct snd_rawmidi_runtime
*runtime
;
115 if ((runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
)) == NULL
)
117 runtime
->substream
= substream
;
118 spin_lock_init(&runtime
->lock
);
119 init_waitqueue_head(&runtime
->sleep
);
120 INIT_WORK(&runtime
->event_work
, snd_rawmidi_input_event_work
);
121 runtime
->event
= NULL
;
122 runtime
->buffer_size
= PAGE_SIZE
;
123 runtime
->avail_min
= 1;
124 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
127 runtime
->avail
= runtime
->buffer_size
;
128 if ((runtime
->buffer
= kmalloc(runtime
->buffer_size
, GFP_KERNEL
)) == NULL
) {
132 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
133 substream
->runtime
= runtime
;
137 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream
*substream
)
139 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
141 kfree(runtime
->buffer
);
143 substream
->runtime
= NULL
;
147 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream
*substream
,int up
)
149 if (!substream
->opened
)
151 substream
->ops
->trigger(substream
, up
);
154 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
156 if (!substream
->opened
)
158 substream
->ops
->trigger(substream
, up
);
160 cancel_work_sync(&substream
->runtime
->event_work
);
163 int snd_rawmidi_drop_output(struct snd_rawmidi_substream
*substream
)
166 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
168 snd_rawmidi_output_trigger(substream
, 0);
170 spin_lock_irqsave(&runtime
->lock
, flags
);
171 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
172 runtime
->avail
= runtime
->buffer_size
;
173 spin_unlock_irqrestore(&runtime
->lock
, flags
);
176 EXPORT_SYMBOL(snd_rawmidi_drop_output
);
178 int snd_rawmidi_drain_output(struct snd_rawmidi_substream
*substream
)
182 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
186 timeout
= wait_event_interruptible_timeout(runtime
->sleep
,
187 (runtime
->avail
>= runtime
->buffer_size
),
189 if (signal_pending(current
))
191 if (runtime
->avail
< runtime
->buffer_size
&& !timeout
) {
192 rmidi_warn(substream
->rmidi
,
193 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
194 (long)runtime
->avail
, (long)runtime
->buffer_size
);
198 if (err
!= -ERESTARTSYS
) {
199 /* we need wait a while to make sure that Tx FIFOs are empty */
200 if (substream
->ops
->drain
)
201 substream
->ops
->drain(substream
);
204 snd_rawmidi_drop_output(substream
);
208 EXPORT_SYMBOL(snd_rawmidi_drain_output
);
210 int snd_rawmidi_drain_input(struct snd_rawmidi_substream
*substream
)
213 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
215 snd_rawmidi_input_trigger(substream
, 0);
217 spin_lock_irqsave(&runtime
->lock
, flags
);
218 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
220 spin_unlock_irqrestore(&runtime
->lock
, flags
);
223 EXPORT_SYMBOL(snd_rawmidi_drain_input
);
225 /* look for an available substream for the given stream direction;
226 * if a specific subdevice is given, try to assign it
228 static int assign_substream(struct snd_rawmidi
*rmidi
, int subdevice
,
229 int stream
, int mode
,
230 struct snd_rawmidi_substream
**sub_ret
)
232 struct snd_rawmidi_substream
*substream
;
233 struct snd_rawmidi_str
*s
= &rmidi
->streams
[stream
];
234 static unsigned int info_flags
[2] = {
235 [SNDRV_RAWMIDI_STREAM_OUTPUT
] = SNDRV_RAWMIDI_INFO_OUTPUT
,
236 [SNDRV_RAWMIDI_STREAM_INPUT
] = SNDRV_RAWMIDI_INFO_INPUT
,
239 if (!(rmidi
->info_flags
& info_flags
[stream
]))
241 if (subdevice
>= 0 && subdevice
>= s
->substream_count
)
244 list_for_each_entry(substream
, &s
->substreams
, list
) {
245 if (substream
->opened
) {
246 if (stream
== SNDRV_RAWMIDI_STREAM_INPUT
||
247 !(mode
& SNDRV_RAWMIDI_LFLG_APPEND
) ||
251 if (subdevice
< 0 || subdevice
== substream
->number
) {
252 *sub_ret
= substream
;
259 /* open and do ref-counting for the given substream */
260 static int open_substream(struct snd_rawmidi
*rmidi
,
261 struct snd_rawmidi_substream
*substream
,
266 if (substream
->use_count
== 0) {
267 err
= snd_rawmidi_runtime_create(substream
);
270 err
= substream
->ops
->open(substream
);
272 snd_rawmidi_runtime_free(substream
);
275 substream
->opened
= 1;
276 substream
->active_sensing
= 0;
277 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
)
278 substream
->append
= 1;
279 substream
->pid
= get_pid(task_pid(current
));
280 rmidi
->streams
[substream
->stream
].substream_opened
++;
282 substream
->use_count
++;
286 static void close_substream(struct snd_rawmidi
*rmidi
,
287 struct snd_rawmidi_substream
*substream
,
290 static int rawmidi_open_priv(struct snd_rawmidi
*rmidi
, int subdevice
, int mode
,
291 struct snd_rawmidi_file
*rfile
)
293 struct snd_rawmidi_substream
*sinput
= NULL
, *soutput
= NULL
;
296 rfile
->input
= rfile
->output
= NULL
;
297 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
298 err
= assign_substream(rmidi
, subdevice
,
299 SNDRV_RAWMIDI_STREAM_INPUT
,
304 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
305 err
= assign_substream(rmidi
, subdevice
,
306 SNDRV_RAWMIDI_STREAM_OUTPUT
,
313 err
= open_substream(rmidi
, sinput
, mode
);
318 err
= open_substream(rmidi
, soutput
, mode
);
321 close_substream(rmidi
, sinput
, 0);
326 rfile
->rmidi
= rmidi
;
327 rfile
->input
= sinput
;
328 rfile
->output
= soutput
;
332 /* called from sound/core/seq/seq_midi.c */
333 int snd_rawmidi_kernel_open(struct snd_card
*card
, int device
, int subdevice
,
334 int mode
, struct snd_rawmidi_file
* rfile
)
336 struct snd_rawmidi
*rmidi
;
339 if (snd_BUG_ON(!rfile
))
342 mutex_lock(®ister_mutex
);
343 rmidi
= snd_rawmidi_search(card
, device
);
345 mutex_unlock(®ister_mutex
);
348 if (!try_module_get(rmidi
->card
->module
)) {
349 mutex_unlock(®ister_mutex
);
352 mutex_unlock(®ister_mutex
);
354 mutex_lock(&rmidi
->open_mutex
);
355 err
= rawmidi_open_priv(rmidi
, subdevice
, mode
, rfile
);
356 mutex_unlock(&rmidi
->open_mutex
);
358 module_put(rmidi
->card
->module
);
361 EXPORT_SYMBOL(snd_rawmidi_kernel_open
);
363 static int snd_rawmidi_open(struct inode
*inode
, struct file
*file
)
365 int maj
= imajor(inode
);
366 struct snd_card
*card
;
368 unsigned short fflags
;
370 struct snd_rawmidi
*rmidi
;
371 struct snd_rawmidi_file
*rawmidi_file
= NULL
;
374 if ((file
->f_flags
& O_APPEND
) && !(file
->f_flags
& O_NONBLOCK
))
375 return -EINVAL
; /* invalid combination */
377 err
= nonseekable_open(inode
, file
);
381 if (maj
== snd_major
) {
382 rmidi
= snd_lookup_minor_data(iminor(inode
),
383 SNDRV_DEVICE_TYPE_RAWMIDI
);
384 #ifdef CONFIG_SND_OSSEMUL
385 } else if (maj
== SOUND_MAJOR
) {
386 rmidi
= snd_lookup_oss_minor_data(iminor(inode
),
387 SNDRV_OSS_DEVICE_TYPE_MIDI
);
395 if (!try_module_get(rmidi
->card
->module
)) {
396 snd_card_unref(rmidi
->card
);
400 mutex_lock(&rmidi
->open_mutex
);
402 err
= snd_card_file_add(card
, file
);
405 fflags
= snd_rawmidi_file_flags(file
);
406 if ((file
->f_flags
& O_APPEND
) || maj
== SOUND_MAJOR
) /* OSS emul? */
407 fflags
|= SNDRV_RAWMIDI_LFLG_APPEND
;
408 rawmidi_file
= kmalloc(sizeof(*rawmidi_file
), GFP_KERNEL
);
409 if (rawmidi_file
== NULL
) {
413 init_waitqueue_entry(&wait
, current
);
414 add_wait_queue(&rmidi
->open_wait
, &wait
);
416 subdevice
= snd_ctl_get_preferred_subdevice(card
, SND_CTL_SUBDEV_RAWMIDI
);
417 err
= rawmidi_open_priv(rmidi
, subdevice
, fflags
, rawmidi_file
);
420 if (err
== -EAGAIN
) {
421 if (file
->f_flags
& O_NONBLOCK
) {
427 set_current_state(TASK_INTERRUPTIBLE
);
428 mutex_unlock(&rmidi
->open_mutex
);
430 mutex_lock(&rmidi
->open_mutex
);
431 if (rmidi
->card
->shutdown
) {
435 if (signal_pending(current
)) {
440 remove_wait_queue(&rmidi
->open_wait
, &wait
);
445 #ifdef CONFIG_SND_OSSEMUL
446 if (rawmidi_file
->input
&& rawmidi_file
->input
->runtime
)
447 rawmidi_file
->input
->runtime
->oss
= (maj
== SOUND_MAJOR
);
448 if (rawmidi_file
->output
&& rawmidi_file
->output
->runtime
)
449 rawmidi_file
->output
->runtime
->oss
= (maj
== SOUND_MAJOR
);
451 file
->private_data
= rawmidi_file
;
452 mutex_unlock(&rmidi
->open_mutex
);
453 snd_card_unref(rmidi
->card
);
457 snd_card_file_remove(card
, file
);
459 mutex_unlock(&rmidi
->open_mutex
);
460 module_put(rmidi
->card
->module
);
461 snd_card_unref(rmidi
->card
);
465 static void close_substream(struct snd_rawmidi
*rmidi
,
466 struct snd_rawmidi_substream
*substream
,
469 if (--substream
->use_count
)
473 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
474 snd_rawmidi_input_trigger(substream
, 0);
476 if (substream
->active_sensing
) {
477 unsigned char buf
= 0xfe;
478 /* sending single active sensing message
479 * to shut the device up
481 snd_rawmidi_kernel_write(substream
, &buf
, 1);
483 if (snd_rawmidi_drain_output(substream
) == -ERESTARTSYS
)
484 snd_rawmidi_output_trigger(substream
, 0);
487 substream
->ops
->close(substream
);
488 if (substream
->runtime
->private_free
)
489 substream
->runtime
->private_free(substream
);
490 snd_rawmidi_runtime_free(substream
);
491 substream
->opened
= 0;
492 substream
->append
= 0;
493 put_pid(substream
->pid
);
494 substream
->pid
= NULL
;
495 rmidi
->streams
[substream
->stream
].substream_opened
--;
498 static void rawmidi_release_priv(struct snd_rawmidi_file
*rfile
)
500 struct snd_rawmidi
*rmidi
;
502 rmidi
= rfile
->rmidi
;
503 mutex_lock(&rmidi
->open_mutex
);
505 close_substream(rmidi
, rfile
->input
, 1);
509 close_substream(rmidi
, rfile
->output
, 1);
510 rfile
->output
= NULL
;
513 mutex_unlock(&rmidi
->open_mutex
);
514 wake_up(&rmidi
->open_wait
);
517 /* called from sound/core/seq/seq_midi.c */
518 int snd_rawmidi_kernel_release(struct snd_rawmidi_file
*rfile
)
520 struct snd_rawmidi
*rmidi
;
522 if (snd_BUG_ON(!rfile
))
525 rmidi
= rfile
->rmidi
;
526 rawmidi_release_priv(rfile
);
527 module_put(rmidi
->card
->module
);
530 EXPORT_SYMBOL(snd_rawmidi_kernel_release
);
532 static int snd_rawmidi_release(struct inode
*inode
, struct file
*file
)
534 struct snd_rawmidi_file
*rfile
;
535 struct snd_rawmidi
*rmidi
;
536 struct module
*module
;
538 rfile
= file
->private_data
;
539 rmidi
= rfile
->rmidi
;
540 rawmidi_release_priv(rfile
);
542 module
= rmidi
->card
->module
;
543 snd_card_file_remove(rmidi
->card
, file
);
548 static int snd_rawmidi_info(struct snd_rawmidi_substream
*substream
,
549 struct snd_rawmidi_info
*info
)
551 struct snd_rawmidi
*rmidi
;
553 if (substream
== NULL
)
555 rmidi
= substream
->rmidi
;
556 memset(info
, 0, sizeof(*info
));
557 info
->card
= rmidi
->card
->number
;
558 info
->device
= rmidi
->device
;
559 info
->subdevice
= substream
->number
;
560 info
->stream
= substream
->stream
;
561 info
->flags
= rmidi
->info_flags
;
562 strcpy(info
->id
, rmidi
->id
);
563 strcpy(info
->name
, rmidi
->name
);
564 strcpy(info
->subname
, substream
->name
);
565 info
->subdevices_count
= substream
->pstr
->substream_count
;
566 info
->subdevices_avail
= (substream
->pstr
->substream_count
-
567 substream
->pstr
->substream_opened
);
571 static int snd_rawmidi_info_user(struct snd_rawmidi_substream
*substream
,
572 struct snd_rawmidi_info __user
* _info
)
574 struct snd_rawmidi_info info
;
576 if ((err
= snd_rawmidi_info(substream
, &info
)) < 0)
578 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
583 static int __snd_rawmidi_info_select(struct snd_card
*card
,
584 struct snd_rawmidi_info
*info
)
586 struct snd_rawmidi
*rmidi
;
587 struct snd_rawmidi_str
*pstr
;
588 struct snd_rawmidi_substream
*substream
;
590 rmidi
= snd_rawmidi_search(card
, info
->device
);
593 if (info
->stream
< 0 || info
->stream
> 1)
595 info
->stream
= array_index_nospec(info
->stream
, 2);
596 pstr
= &rmidi
->streams
[info
->stream
];
597 if (pstr
->substream_count
== 0)
599 if (info
->subdevice
>= pstr
->substream_count
)
601 list_for_each_entry(substream
, &pstr
->substreams
, list
) {
602 if ((unsigned int)substream
->number
== info
->subdevice
)
603 return snd_rawmidi_info(substream
, info
);
608 int snd_rawmidi_info_select(struct snd_card
*card
, struct snd_rawmidi_info
*info
)
612 mutex_lock(®ister_mutex
);
613 ret
= __snd_rawmidi_info_select(card
, info
);
614 mutex_unlock(®ister_mutex
);
617 EXPORT_SYMBOL(snd_rawmidi_info_select
);
619 static int snd_rawmidi_info_select_user(struct snd_card
*card
,
620 struct snd_rawmidi_info __user
*_info
)
623 struct snd_rawmidi_info info
;
624 if (get_user(info
.device
, &_info
->device
))
626 if (get_user(info
.stream
, &_info
->stream
))
628 if (get_user(info
.subdevice
, &_info
->subdevice
))
630 if ((err
= snd_rawmidi_info_select(card
, &info
)) < 0)
632 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
637 int snd_rawmidi_output_params(struct snd_rawmidi_substream
*substream
,
638 struct snd_rawmidi_params
* params
)
640 char *newbuf
, *oldbuf
;
641 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
643 if (substream
->append
&& substream
->use_count
> 1)
645 snd_rawmidi_drain_output(substream
);
646 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
649 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
652 if (params
->buffer_size
!= runtime
->buffer_size
) {
653 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
656 spin_lock_irq(&runtime
->lock
);
657 oldbuf
= runtime
->buffer
;
658 runtime
->buffer
= newbuf
;
659 runtime
->buffer_size
= params
->buffer_size
;
660 runtime
->avail
= runtime
->buffer_size
;
661 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
662 spin_unlock_irq(&runtime
->lock
);
665 runtime
->avail_min
= params
->avail_min
;
666 substream
->active_sensing
= !params
->no_active_sensing
;
669 EXPORT_SYMBOL(snd_rawmidi_output_params
);
671 int snd_rawmidi_input_params(struct snd_rawmidi_substream
*substream
,
672 struct snd_rawmidi_params
* params
)
674 char *newbuf
, *oldbuf
;
675 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
677 snd_rawmidi_drain_input(substream
);
678 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
681 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
684 if (params
->buffer_size
!= runtime
->buffer_size
) {
685 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
688 spin_lock_irq(&runtime
->lock
);
689 oldbuf
= runtime
->buffer
;
690 runtime
->buffer
= newbuf
;
691 runtime
->buffer_size
= params
->buffer_size
;
692 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
693 spin_unlock_irq(&runtime
->lock
);
696 runtime
->avail_min
= params
->avail_min
;
699 EXPORT_SYMBOL(snd_rawmidi_input_params
);
701 static int snd_rawmidi_output_status(struct snd_rawmidi_substream
*substream
,
702 struct snd_rawmidi_status
* status
)
704 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
706 memset(status
, 0, sizeof(*status
));
707 status
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
708 spin_lock_irq(&runtime
->lock
);
709 status
->avail
= runtime
->avail
;
710 spin_unlock_irq(&runtime
->lock
);
714 static int snd_rawmidi_input_status(struct snd_rawmidi_substream
*substream
,
715 struct snd_rawmidi_status
* status
)
717 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
719 memset(status
, 0, sizeof(*status
));
720 status
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
721 spin_lock_irq(&runtime
->lock
);
722 status
->avail
= runtime
->avail
;
723 status
->xruns
= runtime
->xruns
;
725 spin_unlock_irq(&runtime
->lock
);
729 static long snd_rawmidi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
731 struct snd_rawmidi_file
*rfile
;
732 void __user
*argp
= (void __user
*)arg
;
734 rfile
= file
->private_data
;
735 if (((cmd
>> 8) & 0xff) != 'W')
738 case SNDRV_RAWMIDI_IOCTL_PVERSION
:
739 return put_user(SNDRV_RAWMIDI_VERSION
, (int __user
*)argp
) ? -EFAULT
: 0;
740 case SNDRV_RAWMIDI_IOCTL_INFO
:
743 struct snd_rawmidi_info __user
*info
= argp
;
744 if (get_user(stream
, &info
->stream
))
747 case SNDRV_RAWMIDI_STREAM_INPUT
:
748 return snd_rawmidi_info_user(rfile
->input
, info
);
749 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
750 return snd_rawmidi_info_user(rfile
->output
, info
);
755 case SNDRV_RAWMIDI_IOCTL_PARAMS
:
757 struct snd_rawmidi_params params
;
758 if (copy_from_user(¶ms
, argp
, sizeof(struct snd_rawmidi_params
)))
760 switch (params
.stream
) {
761 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
762 if (rfile
->output
== NULL
)
764 return snd_rawmidi_output_params(rfile
->output
, ¶ms
);
765 case SNDRV_RAWMIDI_STREAM_INPUT
:
766 if (rfile
->input
== NULL
)
768 return snd_rawmidi_input_params(rfile
->input
, ¶ms
);
773 case SNDRV_RAWMIDI_IOCTL_STATUS
:
776 struct snd_rawmidi_status status
;
777 if (copy_from_user(&status
, argp
, sizeof(struct snd_rawmidi_status
)))
779 switch (status
.stream
) {
780 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
781 if (rfile
->output
== NULL
)
783 err
= snd_rawmidi_output_status(rfile
->output
, &status
);
785 case SNDRV_RAWMIDI_STREAM_INPUT
:
786 if (rfile
->input
== NULL
)
788 err
= snd_rawmidi_input_status(rfile
->input
, &status
);
795 if (copy_to_user(argp
, &status
, sizeof(struct snd_rawmidi_status
)))
799 case SNDRV_RAWMIDI_IOCTL_DROP
:
802 if (get_user(val
, (int __user
*) argp
))
805 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
806 if (rfile
->output
== NULL
)
808 return snd_rawmidi_drop_output(rfile
->output
);
813 case SNDRV_RAWMIDI_IOCTL_DRAIN
:
816 if (get_user(val
, (int __user
*) argp
))
819 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
820 if (rfile
->output
== NULL
)
822 return snd_rawmidi_drain_output(rfile
->output
);
823 case SNDRV_RAWMIDI_STREAM_INPUT
:
824 if (rfile
->input
== NULL
)
826 return snd_rawmidi_drain_input(rfile
->input
);
832 rmidi_dbg(rfile
->rmidi
,
833 "rawmidi: unknown command = 0x%x\n", cmd
);
838 static int snd_rawmidi_control_ioctl(struct snd_card
*card
,
839 struct snd_ctl_file
*control
,
843 void __user
*argp
= (void __user
*)arg
;
846 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE
:
850 if (get_user(device
, (int __user
*)argp
))
852 if (device
>= SNDRV_RAWMIDI_DEVICES
) /* next device is -1 */
853 device
= SNDRV_RAWMIDI_DEVICES
- 1;
854 mutex_lock(®ister_mutex
);
855 device
= device
< 0 ? 0 : device
+ 1;
856 while (device
< SNDRV_RAWMIDI_DEVICES
) {
857 if (snd_rawmidi_search(card
, device
))
861 if (device
== SNDRV_RAWMIDI_DEVICES
)
863 mutex_unlock(®ister_mutex
);
864 if (put_user(device
, (int __user
*)argp
))
868 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE
:
872 if (get_user(val
, (int __user
*)argp
))
874 control
->preferred_subdevice
[SND_CTL_SUBDEV_RAWMIDI
] = val
;
877 case SNDRV_CTL_IOCTL_RAWMIDI_INFO
:
878 return snd_rawmidi_info_select_user(card
, argp
);
884 * snd_rawmidi_receive - receive the input data from the device
885 * @substream: the rawmidi substream
886 * @buffer: the buffer pointer
887 * @count: the data size to read
889 * Reads the data from the internal buffer.
891 * Return: The size of read data, or a negative error code on failure.
893 int snd_rawmidi_receive(struct snd_rawmidi_substream
*substream
,
894 const unsigned char *buffer
, int count
)
897 int result
= 0, count1
;
898 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
900 if (!substream
->opened
)
902 if (runtime
->buffer
== NULL
) {
903 rmidi_dbg(substream
->rmidi
,
904 "snd_rawmidi_receive: input is not active!!!\n");
907 spin_lock_irqsave(&runtime
->lock
, flags
);
908 if (count
== 1) { /* special case, faster code */
910 if (runtime
->avail
< runtime
->buffer_size
) {
911 runtime
->buffer
[runtime
->hw_ptr
++] = buffer
[0];
912 runtime
->hw_ptr
%= runtime
->buffer_size
;
919 substream
->bytes
+= count
;
920 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
923 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
924 count1
= runtime
->buffer_size
- runtime
->avail
;
925 memcpy(runtime
->buffer
+ runtime
->hw_ptr
, buffer
, count1
);
926 runtime
->hw_ptr
+= count1
;
927 runtime
->hw_ptr
%= runtime
->buffer_size
;
928 runtime
->avail
+= count1
;
934 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
)) {
935 count1
= runtime
->buffer_size
- runtime
->avail
;
936 runtime
->xruns
+= count
- count1
;
939 memcpy(runtime
->buffer
, buffer
, count1
);
940 runtime
->hw_ptr
= count1
;
941 runtime
->avail
+= count1
;
948 schedule_work(&runtime
->event_work
);
949 else if (snd_rawmidi_ready(substream
))
950 wake_up(&runtime
->sleep
);
952 spin_unlock_irqrestore(&runtime
->lock
, flags
);
955 EXPORT_SYMBOL(snd_rawmidi_receive
);
957 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream
*substream
,
958 unsigned char __user
*userbuf
,
959 unsigned char *kernelbuf
, long count
)
962 long result
= 0, count1
;
963 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
964 unsigned long appl_ptr
;
966 spin_lock_irqsave(&runtime
->lock
, flags
);
967 while (count
> 0 && runtime
->avail
) {
968 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
971 if (count1
> (int)runtime
->avail
)
972 count1
= runtime
->avail
;
974 /* update runtime->appl_ptr before unlocking for userbuf */
975 appl_ptr
= runtime
->appl_ptr
;
976 runtime
->appl_ptr
+= count1
;
977 runtime
->appl_ptr
%= runtime
->buffer_size
;
978 runtime
->avail
-= count1
;
981 memcpy(kernelbuf
+ result
, runtime
->buffer
+ appl_ptr
, count1
);
983 spin_unlock_irqrestore(&runtime
->lock
, flags
);
984 if (copy_to_user(userbuf
+ result
,
985 runtime
->buffer
+ appl_ptr
, count1
)) {
986 return result
> 0 ? result
: -EFAULT
;
988 spin_lock_irqsave(&runtime
->lock
, flags
);
993 spin_unlock_irqrestore(&runtime
->lock
, flags
);
997 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream
*substream
,
998 unsigned char *buf
, long count
)
1000 snd_rawmidi_input_trigger(substream
, 1);
1001 return snd_rawmidi_kernel_read1(substream
, NULL
/*userbuf*/, buf
, count
);
1003 EXPORT_SYMBOL(snd_rawmidi_kernel_read
);
1005 static ssize_t
snd_rawmidi_read(struct file
*file
, char __user
*buf
, size_t count
,
1010 struct snd_rawmidi_file
*rfile
;
1011 struct snd_rawmidi_substream
*substream
;
1012 struct snd_rawmidi_runtime
*runtime
;
1014 rfile
= file
->private_data
;
1015 substream
= rfile
->input
;
1016 if (substream
== NULL
)
1018 runtime
= substream
->runtime
;
1019 snd_rawmidi_input_trigger(substream
, 1);
1022 spin_lock_irq(&runtime
->lock
);
1023 while (!snd_rawmidi_ready(substream
)) {
1025 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1026 spin_unlock_irq(&runtime
->lock
);
1027 return result
> 0 ? result
: -EAGAIN
;
1029 init_waitqueue_entry(&wait
, current
);
1030 add_wait_queue(&runtime
->sleep
, &wait
);
1031 set_current_state(TASK_INTERRUPTIBLE
);
1032 spin_unlock_irq(&runtime
->lock
);
1034 remove_wait_queue(&runtime
->sleep
, &wait
);
1035 if (rfile
->rmidi
->card
->shutdown
)
1037 if (signal_pending(current
))
1038 return result
> 0 ? result
: -ERESTARTSYS
;
1039 if (!runtime
->avail
)
1040 return result
> 0 ? result
: -EIO
;
1041 spin_lock_irq(&runtime
->lock
);
1043 spin_unlock_irq(&runtime
->lock
);
1044 count1
= snd_rawmidi_kernel_read1(substream
,
1045 (unsigned char __user
*)buf
,
1049 return result
> 0 ? result
: count1
;
1058 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1059 * @substream: the rawmidi substream
1061 * Return: 1 if the internal output buffer is empty, 0 if not.
1063 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream
*substream
)
1065 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1067 unsigned long flags
;
1069 if (runtime
->buffer
== NULL
) {
1070 rmidi_dbg(substream
->rmidi
,
1071 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1074 spin_lock_irqsave(&runtime
->lock
, flags
);
1075 result
= runtime
->avail
>= runtime
->buffer_size
;
1076 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1079 EXPORT_SYMBOL(snd_rawmidi_transmit_empty
);
1082 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1083 * @substream: the rawmidi substream
1084 * @buffer: the buffer pointer
1085 * @count: data size to transfer
1087 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1089 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1090 unsigned char *buffer
, int count
)
1093 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1095 if (runtime
->buffer
== NULL
) {
1096 rmidi_dbg(substream
->rmidi
,
1097 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1101 if (runtime
->avail
>= runtime
->buffer_size
) {
1102 /* warning: lowlevel layer MUST trigger down the hardware */
1105 if (count
== 1) { /* special case, faster code */
1106 *buffer
= runtime
->buffer
[runtime
->hw_ptr
];
1109 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
1112 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
1113 count1
= runtime
->buffer_size
- runtime
->avail
;
1114 memcpy(buffer
, runtime
->buffer
+ runtime
->hw_ptr
, count1
);
1118 if (count
> (int)(runtime
->buffer_size
- runtime
->avail
- count1
))
1119 count
= runtime
->buffer_size
- runtime
->avail
- count1
;
1120 memcpy(buffer
+ count1
, runtime
->buffer
, count
);
1127 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek
);
1130 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1131 * @substream: the rawmidi substream
1132 * @buffer: the buffer pointer
1133 * @count: data size to transfer
1135 * Copies data from the internal output buffer to the given buffer.
1137 * Call this in the interrupt handler when the midi output is ready,
1138 * and call snd_rawmidi_transmit_ack() after the transmission is
1141 * Return: The size of copied data, or a negative error code on failure.
1143 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1144 unsigned char *buffer
, int count
)
1146 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1148 unsigned long flags
;
1150 spin_lock_irqsave(&runtime
->lock
, flags
);
1151 result
= __snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1152 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1155 EXPORT_SYMBOL(snd_rawmidi_transmit_peek
);
1158 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1159 * @substream: the rawmidi substream
1160 * @count: the transferred count
1162 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1164 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1166 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1168 if (runtime
->buffer
== NULL
) {
1169 rmidi_dbg(substream
->rmidi
,
1170 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1173 snd_BUG_ON(runtime
->avail
+ count
> runtime
->buffer_size
);
1174 runtime
->hw_ptr
+= count
;
1175 runtime
->hw_ptr
%= runtime
->buffer_size
;
1176 runtime
->avail
+= count
;
1177 substream
->bytes
+= count
;
1179 if (runtime
->drain
|| snd_rawmidi_ready(substream
))
1180 wake_up(&runtime
->sleep
);
1184 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack
);
1187 * snd_rawmidi_transmit_ack - acknowledge the transmission
1188 * @substream: the rawmidi substream
1189 * @count: the transferred count
1191 * Advances the hardware pointer for the internal output buffer with
1192 * the given size and updates the condition.
1193 * Call after the transmission is finished.
1195 * Return: The advanced size if successful, or a negative error code on failure.
1197 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1199 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1201 unsigned long flags
;
1203 spin_lock_irqsave(&runtime
->lock
, flags
);
1204 result
= __snd_rawmidi_transmit_ack(substream
, count
);
1205 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1208 EXPORT_SYMBOL(snd_rawmidi_transmit_ack
);
1211 * snd_rawmidi_transmit - copy from the buffer to the device
1212 * @substream: the rawmidi substream
1213 * @buffer: the buffer pointer
1214 * @count: the data size to transfer
1216 * Copies data from the buffer to the device and advances the pointer.
1218 * Return: The copied size if successful, or a negative error code on failure.
1220 int snd_rawmidi_transmit(struct snd_rawmidi_substream
*substream
,
1221 unsigned char *buffer
, int count
)
1223 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1225 unsigned long flags
;
1227 spin_lock_irqsave(&runtime
->lock
, flags
);
1228 if (!substream
->opened
)
1231 count
= __snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1235 result
= __snd_rawmidi_transmit_ack(substream
, count
);
1237 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1240 EXPORT_SYMBOL(snd_rawmidi_transmit
);
1242 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream
*substream
,
1243 const unsigned char __user
*userbuf
,
1244 const unsigned char *kernelbuf
,
1247 unsigned long flags
;
1248 long count1
, result
;
1249 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1250 unsigned long appl_ptr
;
1252 if (!kernelbuf
&& !userbuf
)
1254 if (snd_BUG_ON(!runtime
->buffer
))
1258 spin_lock_irqsave(&runtime
->lock
, flags
);
1259 if (substream
->append
) {
1260 if ((long)runtime
->avail
< count
) {
1261 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1265 while (count
> 0 && runtime
->avail
> 0) {
1266 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1269 if (count1
> (long)runtime
->avail
)
1270 count1
= runtime
->avail
;
1272 /* update runtime->appl_ptr before unlocking for userbuf */
1273 appl_ptr
= runtime
->appl_ptr
;
1274 runtime
->appl_ptr
+= count1
;
1275 runtime
->appl_ptr
%= runtime
->buffer_size
;
1276 runtime
->avail
-= count1
;
1279 memcpy(runtime
->buffer
+ appl_ptr
,
1280 kernelbuf
+ result
, count1
);
1282 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1283 if (copy_from_user(runtime
->buffer
+ appl_ptr
,
1284 userbuf
+ result
, count1
)) {
1285 spin_lock_irqsave(&runtime
->lock
, flags
);
1286 result
= result
> 0 ? result
: -EFAULT
;
1289 spin_lock_irqsave(&runtime
->lock
, flags
);
1295 count1
= runtime
->avail
< runtime
->buffer_size
;
1296 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1298 snd_rawmidi_output_trigger(substream
, 1);
1302 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream
*substream
,
1303 const unsigned char *buf
, long count
)
1305 return snd_rawmidi_kernel_write1(substream
, NULL
, buf
, count
);
1307 EXPORT_SYMBOL(snd_rawmidi_kernel_write
);
1309 static ssize_t
snd_rawmidi_write(struct file
*file
, const char __user
*buf
,
1310 size_t count
, loff_t
*offset
)
1312 long result
, timeout
;
1314 struct snd_rawmidi_file
*rfile
;
1315 struct snd_rawmidi_runtime
*runtime
;
1316 struct snd_rawmidi_substream
*substream
;
1318 rfile
= file
->private_data
;
1319 substream
= rfile
->output
;
1320 runtime
= substream
->runtime
;
1321 /* we cannot put an atomic message to our buffer */
1322 if (substream
->append
&& count
> runtime
->buffer_size
)
1326 spin_lock_irq(&runtime
->lock
);
1327 while (!snd_rawmidi_ready_append(substream
, count
)) {
1329 if (file
->f_flags
& O_NONBLOCK
) {
1330 spin_unlock_irq(&runtime
->lock
);
1331 return result
> 0 ? result
: -EAGAIN
;
1333 init_waitqueue_entry(&wait
, current
);
1334 add_wait_queue(&runtime
->sleep
, &wait
);
1335 set_current_state(TASK_INTERRUPTIBLE
);
1336 spin_unlock_irq(&runtime
->lock
);
1337 timeout
= schedule_timeout(30 * HZ
);
1338 remove_wait_queue(&runtime
->sleep
, &wait
);
1339 if (rfile
->rmidi
->card
->shutdown
)
1341 if (signal_pending(current
))
1342 return result
> 0 ? result
: -ERESTARTSYS
;
1343 if (!runtime
->avail
&& !timeout
)
1344 return result
> 0 ? result
: -EIO
;
1345 spin_lock_irq(&runtime
->lock
);
1347 spin_unlock_irq(&runtime
->lock
);
1348 count1
= snd_rawmidi_kernel_write1(substream
, buf
, NULL
, count
);
1350 return result
> 0 ? result
: count1
;
1353 if ((size_t)count1
< count
&& (file
->f_flags
& O_NONBLOCK
))
1357 if (file
->f_flags
& O_DSYNC
) {
1358 spin_lock_irq(&runtime
->lock
);
1359 while (runtime
->avail
!= runtime
->buffer_size
) {
1361 unsigned int last_avail
= runtime
->avail
;
1362 init_waitqueue_entry(&wait
, current
);
1363 add_wait_queue(&runtime
->sleep
, &wait
);
1364 set_current_state(TASK_INTERRUPTIBLE
);
1365 spin_unlock_irq(&runtime
->lock
);
1366 timeout
= schedule_timeout(30 * HZ
);
1367 remove_wait_queue(&runtime
->sleep
, &wait
);
1368 if (signal_pending(current
))
1369 return result
> 0 ? result
: -ERESTARTSYS
;
1370 if (runtime
->avail
== last_avail
&& !timeout
)
1371 return result
> 0 ? result
: -EIO
;
1372 spin_lock_irq(&runtime
->lock
);
1374 spin_unlock_irq(&runtime
->lock
);
1379 static unsigned int snd_rawmidi_poll(struct file
*file
, poll_table
* wait
)
1381 struct snd_rawmidi_file
*rfile
;
1382 struct snd_rawmidi_runtime
*runtime
;
1385 rfile
= file
->private_data
;
1386 if (rfile
->input
!= NULL
) {
1387 runtime
= rfile
->input
->runtime
;
1388 snd_rawmidi_input_trigger(rfile
->input
, 1);
1389 poll_wait(file
, &runtime
->sleep
, wait
);
1391 if (rfile
->output
!= NULL
) {
1392 runtime
= rfile
->output
->runtime
;
1393 poll_wait(file
, &runtime
->sleep
, wait
);
1396 if (rfile
->input
!= NULL
) {
1397 if (snd_rawmidi_ready(rfile
->input
))
1398 mask
|= POLLIN
| POLLRDNORM
;
1400 if (rfile
->output
!= NULL
) {
1401 if (snd_rawmidi_ready(rfile
->output
))
1402 mask
|= POLLOUT
| POLLWRNORM
;
1409 #ifdef CONFIG_COMPAT
1410 #include "rawmidi_compat.c"
1412 #define snd_rawmidi_ioctl_compat NULL
1419 static void snd_rawmidi_proc_info_read(struct snd_info_entry
*entry
,
1420 struct snd_info_buffer
*buffer
)
1422 struct snd_rawmidi
*rmidi
;
1423 struct snd_rawmidi_substream
*substream
;
1424 struct snd_rawmidi_runtime
*runtime
;
1426 rmidi
= entry
->private_data
;
1427 snd_iprintf(buffer
, "%s\n\n", rmidi
->name
);
1428 mutex_lock(&rmidi
->open_mutex
);
1429 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) {
1430 list_for_each_entry(substream
,
1431 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
,
1435 " Tx bytes : %lu\n",
1437 (unsigned long) substream
->bytes
);
1438 if (substream
->opened
) {
1440 " Owner PID : %d\n",
1441 pid_vnr(substream
->pid
));
1442 runtime
= substream
->runtime
;
1445 " Buffer size : %lu\n"
1447 runtime
->oss
? "OSS compatible" : "native",
1448 (unsigned long) runtime
->buffer_size
,
1449 (unsigned long) runtime
->avail
);
1453 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
) {
1454 list_for_each_entry(substream
,
1455 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
,
1459 " Rx bytes : %lu\n",
1461 (unsigned long) substream
->bytes
);
1462 if (substream
->opened
) {
1464 " Owner PID : %d\n",
1465 pid_vnr(substream
->pid
));
1466 runtime
= substream
->runtime
;
1468 " Buffer size : %lu\n"
1470 " Overruns : %lu\n",
1471 (unsigned long) runtime
->buffer_size
,
1472 (unsigned long) runtime
->avail
,
1473 (unsigned long) runtime
->xruns
);
1477 mutex_unlock(&rmidi
->open_mutex
);
1481 * Register functions
1484 static const struct file_operations snd_rawmidi_f_ops
=
1486 .owner
= THIS_MODULE
,
1487 .read
= snd_rawmidi_read
,
1488 .write
= snd_rawmidi_write
,
1489 .open
= snd_rawmidi_open
,
1490 .release
= snd_rawmidi_release
,
1491 .llseek
= no_llseek
,
1492 .poll
= snd_rawmidi_poll
,
1493 .unlocked_ioctl
= snd_rawmidi_ioctl
,
1494 .compat_ioctl
= snd_rawmidi_ioctl_compat
,
1497 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi
*rmidi
,
1498 struct snd_rawmidi_str
*stream
,
1502 struct snd_rawmidi_substream
*substream
;
1505 for (idx
= 0; idx
< count
; idx
++) {
1506 substream
= kzalloc(sizeof(*substream
), GFP_KERNEL
);
1509 substream
->stream
= direction
;
1510 substream
->number
= idx
;
1511 substream
->rmidi
= rmidi
;
1512 substream
->pstr
= stream
;
1513 list_add_tail(&substream
->list
, &stream
->substreams
);
1514 stream
->substream_count
++;
1519 static void release_rawmidi_device(struct device
*dev
)
1521 kfree(container_of(dev
, struct snd_rawmidi
, dev
));
1525 * snd_rawmidi_new - create a rawmidi instance
1526 * @card: the card instance
1527 * @id: the id string
1528 * @device: the device index
1529 * @output_count: the number of output streams
1530 * @input_count: the number of input streams
1531 * @rrawmidi: the pointer to store the new rawmidi instance
1533 * Creates a new rawmidi instance.
1534 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1536 * Return: Zero if successful, or a negative error code on failure.
1538 int snd_rawmidi_new(struct snd_card
*card
, char *id
, int device
,
1539 int output_count
, int input_count
,
1540 struct snd_rawmidi
** rrawmidi
)
1542 struct snd_rawmidi
*rmidi
;
1544 static struct snd_device_ops ops
= {
1545 .dev_free
= snd_rawmidi_dev_free
,
1546 .dev_register
= snd_rawmidi_dev_register
,
1547 .dev_disconnect
= snd_rawmidi_dev_disconnect
,
1550 if (snd_BUG_ON(!card
))
1554 rmidi
= kzalloc(sizeof(*rmidi
), GFP_KERNEL
);
1558 rmidi
->device
= device
;
1559 mutex_init(&rmidi
->open_mutex
);
1560 init_waitqueue_head(&rmidi
->open_wait
);
1561 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
);
1562 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
);
1565 strlcpy(rmidi
->id
, id
, sizeof(rmidi
->id
));
1567 snd_device_initialize(&rmidi
->dev
, card
);
1568 rmidi
->dev
.release
= release_rawmidi_device
;
1569 dev_set_name(&rmidi
->dev
, "midiC%iD%i", card
->number
, device
);
1571 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1572 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
],
1573 SNDRV_RAWMIDI_STREAM_INPUT
,
1574 input_count
)) < 0) {
1575 snd_rawmidi_free(rmidi
);
1578 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1579 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
],
1580 SNDRV_RAWMIDI_STREAM_OUTPUT
,
1581 output_count
)) < 0) {
1582 snd_rawmidi_free(rmidi
);
1585 if ((err
= snd_device_new(card
, SNDRV_DEV_RAWMIDI
, rmidi
, &ops
)) < 0) {
1586 snd_rawmidi_free(rmidi
);
1593 EXPORT_SYMBOL(snd_rawmidi_new
);
1595 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str
*stream
)
1597 struct snd_rawmidi_substream
*substream
;
1599 while (!list_empty(&stream
->substreams
)) {
1600 substream
= list_entry(stream
->substreams
.next
, struct snd_rawmidi_substream
, list
);
1601 list_del(&substream
->list
);
1606 static int snd_rawmidi_free(struct snd_rawmidi
*rmidi
)
1611 snd_info_free_entry(rmidi
->proc_entry
);
1612 rmidi
->proc_entry
= NULL
;
1613 mutex_lock(®ister_mutex
);
1614 if (rmidi
->ops
&& rmidi
->ops
->dev_unregister
)
1615 rmidi
->ops
->dev_unregister(rmidi
);
1616 mutex_unlock(®ister_mutex
);
1618 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
1619 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
1620 if (rmidi
->private_free
)
1621 rmidi
->private_free(rmidi
);
1622 put_device(&rmidi
->dev
);
1626 static int snd_rawmidi_dev_free(struct snd_device
*device
)
1628 struct snd_rawmidi
*rmidi
= device
->device_data
;
1629 return snd_rawmidi_free(rmidi
);
1632 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1633 static void snd_rawmidi_dev_seq_free(struct snd_seq_device
*device
)
1635 struct snd_rawmidi
*rmidi
= device
->private_data
;
1636 rmidi
->seq_dev
= NULL
;
1640 static int snd_rawmidi_dev_register(struct snd_device
*device
)
1643 struct snd_info_entry
*entry
;
1645 struct snd_rawmidi
*rmidi
= device
->device_data
;
1647 if (rmidi
->device
>= SNDRV_RAWMIDI_DEVICES
)
1649 mutex_lock(®ister_mutex
);
1650 if (snd_rawmidi_search(rmidi
->card
, rmidi
->device
)) {
1651 mutex_unlock(®ister_mutex
);
1654 list_add_tail(&rmidi
->list
, &snd_rawmidi_devices
);
1655 mutex_unlock(®ister_mutex
);
1656 err
= snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI
,
1657 rmidi
->card
, rmidi
->device
,
1658 &snd_rawmidi_f_ops
, rmidi
, &rmidi
->dev
);
1660 rmidi_err(rmidi
, "unable to register\n");
1661 mutex_lock(®ister_mutex
);
1662 list_del(&rmidi
->list
);
1663 mutex_unlock(®ister_mutex
);
1666 if (rmidi
->ops
&& rmidi
->ops
->dev_register
&&
1667 (err
= rmidi
->ops
->dev_register(rmidi
)) < 0) {
1668 snd_unregister_device(&rmidi
->dev
);
1669 mutex_lock(®ister_mutex
);
1670 list_del(&rmidi
->list
);
1671 mutex_unlock(®ister_mutex
);
1674 #ifdef CONFIG_SND_OSSEMUL
1676 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1677 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1678 rmidi
->card
, 0, &snd_rawmidi_f_ops
,
1681 "unable to register OSS rawmidi device %i:%i\n",
1682 rmidi
->card
->number
, 0);
1685 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1686 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
, rmidi
->name
);
1690 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
]) {
1691 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1692 rmidi
->card
, 1, &snd_rawmidi_f_ops
,
1695 "unable to register OSS rawmidi device %i:%i\n",
1696 rmidi
->card
->number
, 1);
1701 #endif /* CONFIG_SND_OSSEMUL */
1702 sprintf(name
, "midi%d", rmidi
->device
);
1703 entry
= snd_info_create_card_entry(rmidi
->card
, name
, rmidi
->card
->proc_root
);
1705 entry
->private_data
= rmidi
;
1706 entry
->c
.text
.read
= snd_rawmidi_proc_info_read
;
1707 if (snd_info_register(entry
) < 0) {
1708 snd_info_free_entry(entry
);
1712 rmidi
->proc_entry
= entry
;
1713 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1714 if (!rmidi
->ops
|| !rmidi
->ops
->dev_register
) { /* own registration mechanism */
1715 if (snd_seq_device_new(rmidi
->card
, rmidi
->device
, SNDRV_SEQ_DEV_ID_MIDISYNTH
, 0, &rmidi
->seq_dev
) >= 0) {
1716 rmidi
->seq_dev
->private_data
= rmidi
;
1717 rmidi
->seq_dev
->private_free
= snd_rawmidi_dev_seq_free
;
1718 sprintf(rmidi
->seq_dev
->name
, "MIDI %d-%d", rmidi
->card
->number
, rmidi
->device
);
1719 snd_device_register(rmidi
->card
, rmidi
->seq_dev
);
1726 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
)
1728 struct snd_rawmidi
*rmidi
= device
->device_data
;
1731 mutex_lock(®ister_mutex
);
1732 mutex_lock(&rmidi
->open_mutex
);
1733 wake_up(&rmidi
->open_wait
);
1734 list_del_init(&rmidi
->list
);
1735 for (dir
= 0; dir
< 2; dir
++) {
1736 struct snd_rawmidi_substream
*s
;
1737 list_for_each_entry(s
, &rmidi
->streams
[dir
].substreams
, list
) {
1739 wake_up(&s
->runtime
->sleep
);
1743 #ifdef CONFIG_SND_OSSEMUL
1744 if (rmidi
->ossreg
) {
1745 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1746 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 0);
1747 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1748 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
);
1751 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
])
1752 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 1);
1755 #endif /* CONFIG_SND_OSSEMUL */
1756 snd_unregister_device(&rmidi
->dev
);
1757 mutex_unlock(&rmidi
->open_mutex
);
1758 mutex_unlock(®ister_mutex
);
1763 * snd_rawmidi_set_ops - set the rawmidi operators
1764 * @rmidi: the rawmidi instance
1765 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1766 * @ops: the operator table
1768 * Sets the rawmidi operators for the given stream direction.
1770 void snd_rawmidi_set_ops(struct snd_rawmidi
*rmidi
, int stream
,
1771 struct snd_rawmidi_ops
*ops
)
1773 struct snd_rawmidi_substream
*substream
;
1775 list_for_each_entry(substream
, &rmidi
->streams
[stream
].substreams
, list
)
1776 substream
->ops
= ops
;
1778 EXPORT_SYMBOL(snd_rawmidi_set_ops
);
1784 static int __init
alsa_rawmidi_init(void)
1787 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl
);
1788 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl
);
1789 #ifdef CONFIG_SND_OSSEMUL
1791 /* check device map table */
1792 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1793 if (midi_map
[i
] < 0 || midi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1794 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1798 if (amidi_map
[i
] < 0 || amidi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1799 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1805 #endif /* CONFIG_SND_OSSEMUL */
1809 static void __exit
alsa_rawmidi_exit(void)
1811 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl
);
1812 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl
);
1815 module_init(alsa_rawmidi_init
)
1816 module_exit(alsa_rawmidi_exit
)