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/moduleparam.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 static struct snd_rawmidi
*snd_rawmidi_search(struct snd_card
*card
, int device
)
61 struct snd_rawmidi
*rawmidi
;
63 list_for_each_entry(rawmidi
, &snd_rawmidi_devices
, list
)
64 if (rawmidi
->card
== card
&& rawmidi
->device
== device
)
69 static inline unsigned short snd_rawmidi_file_flags(struct file
*file
)
71 switch (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) {
73 return SNDRV_RAWMIDI_LFLG_OUTPUT
;
75 return SNDRV_RAWMIDI_LFLG_INPUT
;
77 return SNDRV_RAWMIDI_LFLG_OPEN
;
81 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream
*substream
)
83 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
84 return runtime
->avail
>= runtime
->avail_min
;
87 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream
*substream
,
90 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
91 return runtime
->avail
>= runtime
->avail_min
&&
92 (!substream
->append
|| runtime
->avail
>= count
);
95 static void snd_rawmidi_input_event_tasklet(unsigned long data
)
97 struct snd_rawmidi_substream
*substream
= (struct snd_rawmidi_substream
*)data
;
98 substream
->runtime
->event(substream
);
101 static void snd_rawmidi_output_trigger_tasklet(unsigned long data
)
103 struct snd_rawmidi_substream
*substream
= (struct snd_rawmidi_substream
*)data
;
104 substream
->ops
->trigger(substream
, 1);
107 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream
*substream
)
109 struct snd_rawmidi_runtime
*runtime
;
111 if ((runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
)) == NULL
)
113 spin_lock_init(&runtime
->lock
);
114 init_waitqueue_head(&runtime
->sleep
);
115 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
116 tasklet_init(&runtime
->tasklet
,
117 snd_rawmidi_input_event_tasklet
,
118 (unsigned long)substream
);
120 tasklet_init(&runtime
->tasklet
,
121 snd_rawmidi_output_trigger_tasklet
,
122 (unsigned long)substream
);
123 runtime
->event
= NULL
;
124 runtime
->buffer_size
= PAGE_SIZE
;
125 runtime
->avail_min
= 1;
126 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
129 runtime
->avail
= runtime
->buffer_size
;
130 if ((runtime
->buffer
= kmalloc(runtime
->buffer_size
, GFP_KERNEL
)) == NULL
) {
134 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
135 substream
->runtime
= runtime
;
139 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream
*substream
)
141 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
143 kfree(runtime
->buffer
);
145 substream
->runtime
= NULL
;
149 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream
*substream
,int up
)
151 if (!substream
->opened
)
154 tasklet_hi_schedule(&substream
->runtime
->tasklet
);
156 tasklet_kill(&substream
->runtime
->tasklet
);
157 substream
->ops
->trigger(substream
, 0);
161 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
163 if (!substream
->opened
)
165 substream
->ops
->trigger(substream
, up
);
166 if (!up
&& substream
->runtime
->event
)
167 tasklet_kill(&substream
->runtime
->tasklet
);
170 int snd_rawmidi_drop_output(struct snd_rawmidi_substream
*substream
)
173 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
175 snd_rawmidi_output_trigger(substream
, 0);
177 spin_lock_irqsave(&runtime
->lock
, flags
);
178 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
179 runtime
->avail
= runtime
->buffer_size
;
180 spin_unlock_irqrestore(&runtime
->lock
, flags
);
184 int snd_rawmidi_drain_output(struct snd_rawmidi_substream
*substream
)
188 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
192 timeout
= wait_event_interruptible_timeout(runtime
->sleep
,
193 (runtime
->avail
>= runtime
->buffer_size
),
195 if (signal_pending(current
))
197 if (runtime
->avail
< runtime
->buffer_size
&& !timeout
) {
198 snd_printk(KERN_WARNING
"rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime
->avail
, (long)runtime
->buffer_size
);
202 if (err
!= -ERESTARTSYS
) {
203 /* we need wait a while to make sure that Tx FIFOs are empty */
204 if (substream
->ops
->drain
)
205 substream
->ops
->drain(substream
);
208 snd_rawmidi_drop_output(substream
);
213 int snd_rawmidi_drain_input(struct snd_rawmidi_substream
*substream
)
216 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
218 snd_rawmidi_input_trigger(substream
, 0);
220 spin_lock_irqsave(&runtime
->lock
, flags
);
221 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
223 spin_unlock_irqrestore(&runtime
->lock
, flags
);
227 int snd_rawmidi_kernel_open(struct snd_card
*card
, int device
, int subdevice
,
228 int mode
, struct snd_rawmidi_file
* rfile
)
230 struct snd_rawmidi
*rmidi
;
231 struct list_head
*list1
, *list2
;
232 struct snd_rawmidi_substream
*sinput
= NULL
, *soutput
= NULL
;
233 struct snd_rawmidi_runtime
*input
= NULL
, *output
= NULL
;
237 rfile
->input
= rfile
->output
= NULL
;
238 mutex_lock(®ister_mutex
);
239 rmidi
= snd_rawmidi_search(card
, device
);
240 mutex_unlock(®ister_mutex
);
245 if (!try_module_get(rmidi
->card
->module
)) {
249 if (!(mode
& SNDRV_RAWMIDI_LFLG_NOOPENLOCK
))
250 mutex_lock(&rmidi
->open_mutex
);
251 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
252 if (!(rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
)) {
256 if (subdevice
>= 0 && (unsigned int)subdevice
>= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_count
) {
260 if (rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_opened
>=
261 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_count
) {
266 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
267 if (!(rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
)) {
271 if (subdevice
>= 0 && (unsigned int)subdevice
>= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_count
) {
275 if (rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_opened
>=
276 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_count
) {
281 list1
= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
.next
;
283 if (list1
== &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
) {
285 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
291 sinput
= list_entry(list1
, struct snd_rawmidi_substream
, list
);
292 if ((mode
& SNDRV_RAWMIDI_LFLG_INPUT
) && sinput
->opened
)
294 if (subdevice
< 0 || (subdevice
>= 0 && subdevice
== sinput
->number
))
299 list2
= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
.next
;
301 if (list2
== &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
) {
303 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
309 soutput
= list_entry(list2
, struct snd_rawmidi_substream
, list
);
310 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
311 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
) {
312 if (soutput
->opened
&& !soutput
->append
)
319 if (subdevice
< 0 || (subdevice
>= 0 && subdevice
== soutput
->number
))
324 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
325 if ((err
= snd_rawmidi_runtime_create(sinput
)) < 0)
327 input
= sinput
->runtime
;
328 if ((err
= sinput
->ops
->open(sinput
)) < 0)
331 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_opened
++;
335 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
338 if ((err
= snd_rawmidi_runtime_create(soutput
)) < 0) {
339 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
)
340 sinput
->ops
->close(sinput
);
343 output
= soutput
->runtime
;
344 if ((err
= soutput
->ops
->open(soutput
)) < 0) {
345 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
)
346 sinput
->ops
->close(sinput
);
351 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
)
353 if (soutput
->use_count
++ == 0)
354 soutput
->active_sensing
= 1;
355 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_opened
++;
359 if (!(mode
& SNDRV_RAWMIDI_LFLG_NOOPENLOCK
))
360 mutex_unlock(&rmidi
->open_mutex
);
362 rfile
->rmidi
= rmidi
;
363 rfile
->input
= sinput
;
364 rfile
->output
= soutput
;
370 snd_rawmidi_runtime_free(sinput
);
372 snd_rawmidi_runtime_free(soutput
);
373 module_put(rmidi
->card
->module
);
374 if (!(mode
& SNDRV_RAWMIDI_LFLG_NOOPENLOCK
))
375 mutex_unlock(&rmidi
->open_mutex
);
380 static int snd_rawmidi_open(struct inode
*inode
, struct file
*file
)
382 int maj
= imajor(inode
);
383 struct snd_card
*card
;
385 unsigned short fflags
;
387 struct snd_rawmidi
*rmidi
;
388 struct snd_rawmidi_file
*rawmidi_file
;
390 struct snd_ctl_file
*kctl
;
392 if (maj
== snd_major
) {
393 rmidi
= snd_lookup_minor_data(iminor(inode
),
394 SNDRV_DEVICE_TYPE_RAWMIDI
);
395 #ifdef CONFIG_SND_OSSEMUL
396 } else if (maj
== SOUND_MAJOR
) {
397 rmidi
= snd_lookup_oss_minor_data(iminor(inode
),
398 SNDRV_OSS_DEVICE_TYPE_MIDI
);
405 if ((file
->f_flags
& O_APPEND
) && !(file
->f_flags
& O_NONBLOCK
))
406 return -EINVAL
; /* invalid combination */
408 err
= snd_card_file_add(card
, file
);
411 fflags
= snd_rawmidi_file_flags(file
);
412 if ((file
->f_flags
& O_APPEND
) || maj
== SOUND_MAJOR
) /* OSS emul? */
413 fflags
|= SNDRV_RAWMIDI_LFLG_APPEND
;
414 fflags
|= SNDRV_RAWMIDI_LFLG_NOOPENLOCK
;
415 rawmidi_file
= kmalloc(sizeof(*rawmidi_file
), GFP_KERNEL
);
416 if (rawmidi_file
== NULL
) {
417 snd_card_file_remove(card
, file
);
420 init_waitqueue_entry(&wait
, current
);
421 add_wait_queue(&rmidi
->open_wait
, &wait
);
422 mutex_lock(&rmidi
->open_mutex
);
425 read_lock(&card
->ctl_files_rwlock
);
426 list_for_each_entry(kctl
, &card
->ctl_files
, list
) {
427 if (kctl
->pid
== current
->pid
) {
428 subdevice
= kctl
->prefer_rawmidi_subdevice
;
433 read_unlock(&card
->ctl_files_rwlock
);
434 err
= snd_rawmidi_kernel_open(rmidi
->card
, rmidi
->device
,
435 subdevice
, fflags
, rawmidi_file
);
438 if (err
== -EAGAIN
) {
439 if (file
->f_flags
& O_NONBLOCK
) {
445 set_current_state(TASK_INTERRUPTIBLE
);
446 mutex_unlock(&rmidi
->open_mutex
);
448 mutex_lock(&rmidi
->open_mutex
);
449 if (signal_pending(current
)) {
454 #ifdef CONFIG_SND_OSSEMUL
455 if (rawmidi_file
->input
&& rawmidi_file
->input
->runtime
)
456 rawmidi_file
->input
->runtime
->oss
= (maj
== SOUND_MAJOR
);
457 if (rawmidi_file
->output
&& rawmidi_file
->output
->runtime
)
458 rawmidi_file
->output
->runtime
->oss
= (maj
== SOUND_MAJOR
);
460 remove_wait_queue(&rmidi
->open_wait
, &wait
);
462 file
->private_data
= rawmidi_file
;
464 snd_card_file_remove(card
, file
);
467 mutex_unlock(&rmidi
->open_mutex
);
471 int snd_rawmidi_kernel_release(struct snd_rawmidi_file
* rfile
)
473 struct snd_rawmidi
*rmidi
;
474 struct snd_rawmidi_substream
*substream
;
475 struct snd_rawmidi_runtime
*runtime
;
477 if (snd_BUG_ON(!rfile
))
479 rmidi
= rfile
->rmidi
;
480 mutex_lock(&rmidi
->open_mutex
);
481 if (rfile
->input
!= NULL
) {
482 substream
= rfile
->input
;
484 runtime
= substream
->runtime
;
485 snd_rawmidi_input_trigger(substream
, 0);
486 substream
->ops
->close(substream
);
487 if (runtime
->private_free
!= NULL
)
488 runtime
->private_free(substream
);
489 snd_rawmidi_runtime_free(substream
);
490 substream
->opened
= 0;
491 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_opened
--;
493 if (rfile
->output
!= NULL
) {
494 substream
= rfile
->output
;
495 rfile
->output
= NULL
;
496 if (--substream
->use_count
== 0) {
497 runtime
= substream
->runtime
;
498 if (substream
->active_sensing
) {
499 unsigned char buf
= 0xfe;
500 /* sending single active sensing message to shut the device up */
501 snd_rawmidi_kernel_write(substream
, &buf
, 1);
503 if (snd_rawmidi_drain_output(substream
) == -ERESTARTSYS
)
504 snd_rawmidi_output_trigger(substream
, 0);
505 substream
->ops
->close(substream
);
506 if (runtime
->private_free
!= NULL
)
507 runtime
->private_free(substream
);
508 snd_rawmidi_runtime_free(substream
);
509 substream
->opened
= 0;
510 substream
->append
= 0;
512 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_opened
--;
514 mutex_unlock(&rmidi
->open_mutex
);
515 module_put(rmidi
->card
->module
);
519 static int snd_rawmidi_release(struct inode
*inode
, struct file
*file
)
521 struct snd_rawmidi_file
*rfile
;
522 struct snd_rawmidi
*rmidi
;
525 rfile
= file
->private_data
;
526 err
= snd_rawmidi_kernel_release(rfile
);
527 rmidi
= rfile
->rmidi
;
528 wake_up(&rmidi
->open_wait
);
530 snd_card_file_remove(rmidi
->card
, file
);
534 static int snd_rawmidi_info(struct snd_rawmidi_substream
*substream
,
535 struct snd_rawmidi_info
*info
)
537 struct snd_rawmidi
*rmidi
;
539 if (substream
== NULL
)
541 rmidi
= substream
->rmidi
;
542 memset(info
, 0, sizeof(*info
));
543 info
->card
= rmidi
->card
->number
;
544 info
->device
= rmidi
->device
;
545 info
->subdevice
= substream
->number
;
546 info
->stream
= substream
->stream
;
547 info
->flags
= rmidi
->info_flags
;
548 strcpy(info
->id
, rmidi
->id
);
549 strcpy(info
->name
, rmidi
->name
);
550 strcpy(info
->subname
, substream
->name
);
551 info
->subdevices_count
= substream
->pstr
->substream_count
;
552 info
->subdevices_avail
= (substream
->pstr
->substream_count
-
553 substream
->pstr
->substream_opened
);
557 static int snd_rawmidi_info_user(struct snd_rawmidi_substream
*substream
,
558 struct snd_rawmidi_info __user
* _info
)
560 struct snd_rawmidi_info info
;
562 if ((err
= snd_rawmidi_info(substream
, &info
)) < 0)
564 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
569 int snd_rawmidi_info_select(struct snd_card
*card
, struct snd_rawmidi_info
*info
)
571 struct snd_rawmidi
*rmidi
;
572 struct snd_rawmidi_str
*pstr
;
573 struct snd_rawmidi_substream
*substream
;
575 mutex_lock(®ister_mutex
);
576 rmidi
= snd_rawmidi_search(card
, info
->device
);
577 mutex_unlock(®ister_mutex
);
580 if (info
->stream
< 0 || info
->stream
> 1)
582 pstr
= &rmidi
->streams
[info
->stream
];
583 if (pstr
->substream_count
== 0)
585 if (info
->subdevice
>= pstr
->substream_count
)
587 list_for_each_entry(substream
, &pstr
->substreams
, list
) {
588 if ((unsigned int)substream
->number
== info
->subdevice
)
589 return snd_rawmidi_info(substream
, info
);
594 static int snd_rawmidi_info_select_user(struct snd_card
*card
,
595 struct snd_rawmidi_info __user
*_info
)
598 struct snd_rawmidi_info info
;
599 if (get_user(info
.device
, &_info
->device
))
601 if (get_user(info
.stream
, &_info
->stream
))
603 if (get_user(info
.subdevice
, &_info
->subdevice
))
605 if ((err
= snd_rawmidi_info_select(card
, &info
)) < 0)
607 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
612 int snd_rawmidi_output_params(struct snd_rawmidi_substream
*substream
,
613 struct snd_rawmidi_params
* params
)
616 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
618 if (substream
->append
&& substream
->use_count
> 1)
620 snd_rawmidi_drain_output(substream
);
621 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
624 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
627 if (params
->buffer_size
!= runtime
->buffer_size
) {
628 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
631 kfree(runtime
->buffer
);
632 runtime
->buffer
= newbuf
;
633 runtime
->buffer_size
= params
->buffer_size
;
634 runtime
->avail
= runtime
->buffer_size
;
636 runtime
->avail_min
= params
->avail_min
;
637 substream
->active_sensing
= !params
->no_active_sensing
;
641 int snd_rawmidi_input_params(struct snd_rawmidi_substream
*substream
,
642 struct snd_rawmidi_params
* params
)
645 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
647 snd_rawmidi_drain_input(substream
);
648 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
651 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
654 if (params
->buffer_size
!= runtime
->buffer_size
) {
655 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
658 kfree(runtime
->buffer
);
659 runtime
->buffer
= newbuf
;
660 runtime
->buffer_size
= params
->buffer_size
;
662 runtime
->avail_min
= params
->avail_min
;
666 static int snd_rawmidi_output_status(struct snd_rawmidi_substream
*substream
,
667 struct snd_rawmidi_status
* status
)
669 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
671 memset(status
, 0, sizeof(*status
));
672 status
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
673 spin_lock_irq(&runtime
->lock
);
674 status
->avail
= runtime
->avail
;
675 spin_unlock_irq(&runtime
->lock
);
679 static int snd_rawmidi_input_status(struct snd_rawmidi_substream
*substream
,
680 struct snd_rawmidi_status
* status
)
682 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
684 memset(status
, 0, sizeof(*status
));
685 status
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
686 spin_lock_irq(&runtime
->lock
);
687 status
->avail
= runtime
->avail
;
688 status
->xruns
= runtime
->xruns
;
690 spin_unlock_irq(&runtime
->lock
);
694 static long snd_rawmidi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
696 struct snd_rawmidi_file
*rfile
;
697 void __user
*argp
= (void __user
*)arg
;
699 rfile
= file
->private_data
;
700 if (((cmd
>> 8) & 0xff) != 'W')
703 case SNDRV_RAWMIDI_IOCTL_PVERSION
:
704 return put_user(SNDRV_RAWMIDI_VERSION
, (int __user
*)argp
) ? -EFAULT
: 0;
705 case SNDRV_RAWMIDI_IOCTL_INFO
:
708 struct snd_rawmidi_info __user
*info
= argp
;
709 if (get_user(stream
, &info
->stream
))
712 case SNDRV_RAWMIDI_STREAM_INPUT
:
713 return snd_rawmidi_info_user(rfile
->input
, info
);
714 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
715 return snd_rawmidi_info_user(rfile
->output
, info
);
720 case SNDRV_RAWMIDI_IOCTL_PARAMS
:
722 struct snd_rawmidi_params params
;
723 if (copy_from_user(¶ms
, argp
, sizeof(struct snd_rawmidi_params
)))
725 switch (params
.stream
) {
726 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
727 if (rfile
->output
== NULL
)
729 return snd_rawmidi_output_params(rfile
->output
, ¶ms
);
730 case SNDRV_RAWMIDI_STREAM_INPUT
:
731 if (rfile
->input
== NULL
)
733 return snd_rawmidi_input_params(rfile
->input
, ¶ms
);
738 case SNDRV_RAWMIDI_IOCTL_STATUS
:
741 struct snd_rawmidi_status status
;
742 if (copy_from_user(&status
, argp
, sizeof(struct snd_rawmidi_status
)))
744 switch (status
.stream
) {
745 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
746 if (rfile
->output
== NULL
)
748 err
= snd_rawmidi_output_status(rfile
->output
, &status
);
750 case SNDRV_RAWMIDI_STREAM_INPUT
:
751 if (rfile
->input
== NULL
)
753 err
= snd_rawmidi_input_status(rfile
->input
, &status
);
760 if (copy_to_user(argp
, &status
, sizeof(struct snd_rawmidi_status
)))
764 case SNDRV_RAWMIDI_IOCTL_DROP
:
767 if (get_user(val
, (int __user
*) argp
))
770 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
771 if (rfile
->output
== NULL
)
773 return snd_rawmidi_drop_output(rfile
->output
);
778 case SNDRV_RAWMIDI_IOCTL_DRAIN
:
781 if (get_user(val
, (int __user
*) argp
))
784 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
785 if (rfile
->output
== NULL
)
787 return snd_rawmidi_drain_output(rfile
->output
);
788 case SNDRV_RAWMIDI_STREAM_INPUT
:
789 if (rfile
->input
== NULL
)
791 return snd_rawmidi_drain_input(rfile
->input
);
796 #ifdef CONFIG_SND_DEBUG
798 snd_printk(KERN_WARNING
"rawmidi: unknown command = 0x%x\n", cmd
);
804 static int snd_rawmidi_control_ioctl(struct snd_card
*card
,
805 struct snd_ctl_file
*control
,
809 void __user
*argp
= (void __user
*)arg
;
812 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE
:
816 if (get_user(device
, (int __user
*)argp
))
818 mutex_lock(®ister_mutex
);
819 device
= device
< 0 ? 0 : device
+ 1;
820 while (device
< SNDRV_RAWMIDI_DEVICES
) {
821 if (snd_rawmidi_search(card
, device
))
825 if (device
== SNDRV_RAWMIDI_DEVICES
)
827 mutex_unlock(®ister_mutex
);
828 if (put_user(device
, (int __user
*)argp
))
832 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE
:
836 if (get_user(val
, (int __user
*)argp
))
838 control
->prefer_rawmidi_subdevice
= val
;
841 case SNDRV_CTL_IOCTL_RAWMIDI_INFO
:
842 return snd_rawmidi_info_select_user(card
, argp
);
848 * snd_rawmidi_receive - receive the input data from the device
849 * @substream: the rawmidi substream
850 * @buffer: the buffer pointer
851 * @count: the data size to read
853 * Reads the data from the internal buffer.
855 * Returns the size of read data, or a negative error code on failure.
857 int snd_rawmidi_receive(struct snd_rawmidi_substream
*substream
,
858 const unsigned char *buffer
, int count
)
861 int result
= 0, count1
;
862 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
864 if (!substream
->opened
)
866 if (runtime
->buffer
== NULL
) {
867 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
870 spin_lock_irqsave(&runtime
->lock
, flags
);
871 if (count
== 1) { /* special case, faster code */
873 if (runtime
->avail
< runtime
->buffer_size
) {
874 runtime
->buffer
[runtime
->hw_ptr
++] = buffer
[0];
875 runtime
->hw_ptr
%= runtime
->buffer_size
;
882 substream
->bytes
+= count
;
883 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
886 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
887 count1
= runtime
->buffer_size
- runtime
->avail
;
888 memcpy(runtime
->buffer
+ runtime
->hw_ptr
, buffer
, count1
);
889 runtime
->hw_ptr
+= count1
;
890 runtime
->hw_ptr
%= runtime
->buffer_size
;
891 runtime
->avail
+= count1
;
897 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
)) {
898 count1
= runtime
->buffer_size
- runtime
->avail
;
899 runtime
->xruns
+= count
- count1
;
902 memcpy(runtime
->buffer
, buffer
, count1
);
903 runtime
->hw_ptr
= count1
;
904 runtime
->avail
+= count1
;
911 tasklet_hi_schedule(&runtime
->tasklet
);
912 else if (snd_rawmidi_ready(substream
))
913 wake_up(&runtime
->sleep
);
915 spin_unlock_irqrestore(&runtime
->lock
, flags
);
919 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream
*substream
,
920 unsigned char __user
*userbuf
,
921 unsigned char *kernelbuf
, long count
)
924 long result
= 0, count1
;
925 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
927 while (count
> 0 && runtime
->avail
) {
928 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
931 spin_lock_irqsave(&runtime
->lock
, flags
);
932 if (count1
> (int)runtime
->avail
)
933 count1
= runtime
->avail
;
935 memcpy(kernelbuf
+ result
, runtime
->buffer
+ runtime
->appl_ptr
, count1
);
937 spin_unlock_irqrestore(&runtime
->lock
, flags
);
938 if (copy_to_user(userbuf
+ result
,
939 runtime
->buffer
+ runtime
->appl_ptr
, count1
)) {
940 return result
> 0 ? result
: -EFAULT
;
942 spin_lock_irqsave(&runtime
->lock
, flags
);
944 runtime
->appl_ptr
+= count1
;
945 runtime
->appl_ptr
%= runtime
->buffer_size
;
946 runtime
->avail
-= count1
;
947 spin_unlock_irqrestore(&runtime
->lock
, flags
);
954 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream
*substream
,
955 unsigned char *buf
, long count
)
957 snd_rawmidi_input_trigger(substream
, 1);
958 return snd_rawmidi_kernel_read1(substream
, NULL
/*userbuf*/, buf
, count
);
961 static ssize_t
snd_rawmidi_read(struct file
*file
, char __user
*buf
, size_t count
,
966 struct snd_rawmidi_file
*rfile
;
967 struct snd_rawmidi_substream
*substream
;
968 struct snd_rawmidi_runtime
*runtime
;
970 rfile
= file
->private_data
;
971 substream
= rfile
->input
;
972 if (substream
== NULL
)
974 runtime
= substream
->runtime
;
975 snd_rawmidi_input_trigger(substream
, 1);
978 spin_lock_irq(&runtime
->lock
);
979 while (!snd_rawmidi_ready(substream
)) {
981 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
982 spin_unlock_irq(&runtime
->lock
);
983 return result
> 0 ? result
: -EAGAIN
;
985 init_waitqueue_entry(&wait
, current
);
986 add_wait_queue(&runtime
->sleep
, &wait
);
987 set_current_state(TASK_INTERRUPTIBLE
);
988 spin_unlock_irq(&runtime
->lock
);
990 remove_wait_queue(&runtime
->sleep
, &wait
);
991 if (signal_pending(current
))
992 return result
> 0 ? result
: -ERESTARTSYS
;
994 return result
> 0 ? result
: -EIO
;
995 spin_lock_irq(&runtime
->lock
);
997 spin_unlock_irq(&runtime
->lock
);
998 count1
= snd_rawmidi_kernel_read1(substream
,
999 (unsigned char __user
*)buf
,
1003 return result
> 0 ? result
: count1
;
1012 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1013 * @substream: the rawmidi substream
1015 * Returns 1 if the internal output buffer is empty, 0 if not.
1017 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream
*substream
)
1019 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1021 unsigned long flags
;
1023 if (runtime
->buffer
== NULL
) {
1024 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1027 spin_lock_irqsave(&runtime
->lock
, flags
);
1028 result
= runtime
->avail
>= runtime
->buffer_size
;
1029 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1034 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1035 * @substream: the rawmidi substream
1036 * @buffer: the buffer pointer
1037 * @count: data size to transfer
1039 * Copies data from the internal output buffer to the given buffer.
1041 * Call this in the interrupt handler when the midi output is ready,
1042 * and call snd_rawmidi_transmit_ack() after the transmission is
1045 * Returns the size of copied data, or a negative error code on failure.
1047 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1048 unsigned char *buffer
, int count
)
1050 unsigned long flags
;
1052 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1054 if (runtime
->buffer
== NULL
) {
1055 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1059 spin_lock_irqsave(&runtime
->lock
, flags
);
1060 if (runtime
->avail
>= runtime
->buffer_size
) {
1061 /* warning: lowlevel layer MUST trigger down the hardware */
1064 if (count
== 1) { /* special case, faster code */
1065 *buffer
= runtime
->buffer
[runtime
->hw_ptr
];
1068 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
1071 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
1072 count1
= runtime
->buffer_size
- runtime
->avail
;
1073 memcpy(buffer
, runtime
->buffer
+ runtime
->hw_ptr
, count1
);
1077 if (count
> (int)(runtime
->buffer_size
- runtime
->avail
- count1
))
1078 count
= runtime
->buffer_size
- runtime
->avail
- count1
;
1079 memcpy(buffer
+ count1
, runtime
->buffer
, count
);
1084 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1089 * snd_rawmidi_transmit_ack - acknowledge the transmission
1090 * @substream: the rawmidi substream
1091 * @count: the tranferred count
1093 * Advances the hardware pointer for the internal output buffer with
1094 * the given size and updates the condition.
1095 * Call after the transmission is finished.
1097 * Returns the advanced size if successful, or a negative error code on failure.
1099 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1101 unsigned long flags
;
1102 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1104 if (runtime
->buffer
== NULL
) {
1105 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1108 spin_lock_irqsave(&runtime
->lock
, flags
);
1109 snd_BUG_ON(runtime
->avail
+ count
> runtime
->buffer_size
);
1110 runtime
->hw_ptr
+= count
;
1111 runtime
->hw_ptr
%= runtime
->buffer_size
;
1112 runtime
->avail
+= count
;
1113 substream
->bytes
+= count
;
1115 if (runtime
->drain
|| snd_rawmidi_ready(substream
))
1116 wake_up(&runtime
->sleep
);
1118 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1123 * snd_rawmidi_transmit - copy from the buffer to the device
1124 * @substream: the rawmidi substream
1125 * @buffer: the buffer pointer
1126 * @count: the data size to transfer
1128 * Copies data from the buffer to the device and advances the pointer.
1130 * Returns the copied size if successful, or a negative error code on failure.
1132 int snd_rawmidi_transmit(struct snd_rawmidi_substream
*substream
,
1133 unsigned char *buffer
, int count
)
1135 if (!substream
->opened
)
1137 count
= snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1140 return snd_rawmidi_transmit_ack(substream
, count
);
1143 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream
*substream
,
1144 const unsigned char __user
*userbuf
,
1145 const unsigned char *kernelbuf
,
1148 unsigned long flags
;
1149 long count1
, result
;
1150 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1152 if (snd_BUG_ON(!kernelbuf
&& !userbuf
))
1154 if (snd_BUG_ON(!runtime
->buffer
))
1158 spin_lock_irqsave(&runtime
->lock
, flags
);
1159 if (substream
->append
) {
1160 if ((long)runtime
->avail
< count
) {
1161 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1165 while (count
> 0 && runtime
->avail
> 0) {
1166 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1169 if (count1
> (long)runtime
->avail
)
1170 count1
= runtime
->avail
;
1172 memcpy(runtime
->buffer
+ runtime
->appl_ptr
,
1173 kernelbuf
+ result
, count1
);
1175 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1176 if (copy_from_user(runtime
->buffer
+ runtime
->appl_ptr
,
1177 userbuf
+ result
, count1
)) {
1178 spin_lock_irqsave(&runtime
->lock
, flags
);
1179 result
= result
> 0 ? result
: -EFAULT
;
1182 spin_lock_irqsave(&runtime
->lock
, flags
);
1184 runtime
->appl_ptr
+= count1
;
1185 runtime
->appl_ptr
%= runtime
->buffer_size
;
1186 runtime
->avail
-= count1
;
1191 count1
= runtime
->avail
< runtime
->buffer_size
;
1192 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1194 snd_rawmidi_output_trigger(substream
, 1);
1198 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream
*substream
,
1199 const unsigned char *buf
, long count
)
1201 return snd_rawmidi_kernel_write1(substream
, NULL
, buf
, count
);
1204 static ssize_t
snd_rawmidi_write(struct file
*file
, const char __user
*buf
,
1205 size_t count
, loff_t
*offset
)
1207 long result
, timeout
;
1209 struct snd_rawmidi_file
*rfile
;
1210 struct snd_rawmidi_runtime
*runtime
;
1211 struct snd_rawmidi_substream
*substream
;
1213 rfile
= file
->private_data
;
1214 substream
= rfile
->output
;
1215 runtime
= substream
->runtime
;
1216 /* we cannot put an atomic message to our buffer */
1217 if (substream
->append
&& count
> runtime
->buffer_size
)
1221 spin_lock_irq(&runtime
->lock
);
1222 while (!snd_rawmidi_ready_append(substream
, count
)) {
1224 if (file
->f_flags
& O_NONBLOCK
) {
1225 spin_unlock_irq(&runtime
->lock
);
1226 return result
> 0 ? result
: -EAGAIN
;
1228 init_waitqueue_entry(&wait
, current
);
1229 add_wait_queue(&runtime
->sleep
, &wait
);
1230 set_current_state(TASK_INTERRUPTIBLE
);
1231 spin_unlock_irq(&runtime
->lock
);
1232 timeout
= schedule_timeout(30 * HZ
);
1233 remove_wait_queue(&runtime
->sleep
, &wait
);
1234 if (signal_pending(current
))
1235 return result
> 0 ? result
: -ERESTARTSYS
;
1236 if (!runtime
->avail
&& !timeout
)
1237 return result
> 0 ? result
: -EIO
;
1238 spin_lock_irq(&runtime
->lock
);
1240 spin_unlock_irq(&runtime
->lock
);
1241 count1
= snd_rawmidi_kernel_write1(substream
, buf
, NULL
, count
);
1243 return result
> 0 ? result
: count1
;
1246 if ((size_t)count1
< count
&& (file
->f_flags
& O_NONBLOCK
))
1250 if (file
->f_flags
& O_SYNC
) {
1251 spin_lock_irq(&runtime
->lock
);
1252 while (runtime
->avail
!= runtime
->buffer_size
) {
1254 unsigned int last_avail
= runtime
->avail
;
1255 init_waitqueue_entry(&wait
, current
);
1256 add_wait_queue(&runtime
->sleep
, &wait
);
1257 set_current_state(TASK_INTERRUPTIBLE
);
1258 spin_unlock_irq(&runtime
->lock
);
1259 timeout
= schedule_timeout(30 * HZ
);
1260 remove_wait_queue(&runtime
->sleep
, &wait
);
1261 if (signal_pending(current
))
1262 return result
> 0 ? result
: -ERESTARTSYS
;
1263 if (runtime
->avail
== last_avail
&& !timeout
)
1264 return result
> 0 ? result
: -EIO
;
1265 spin_lock_irq(&runtime
->lock
);
1267 spin_unlock_irq(&runtime
->lock
);
1272 static unsigned int snd_rawmidi_poll(struct file
*file
, poll_table
* wait
)
1274 struct snd_rawmidi_file
*rfile
;
1275 struct snd_rawmidi_runtime
*runtime
;
1278 rfile
= file
->private_data
;
1279 if (rfile
->input
!= NULL
) {
1280 runtime
= rfile
->input
->runtime
;
1281 snd_rawmidi_input_trigger(rfile
->input
, 1);
1282 poll_wait(file
, &runtime
->sleep
, wait
);
1284 if (rfile
->output
!= NULL
) {
1285 runtime
= rfile
->output
->runtime
;
1286 poll_wait(file
, &runtime
->sleep
, wait
);
1289 if (rfile
->input
!= NULL
) {
1290 if (snd_rawmidi_ready(rfile
->input
))
1291 mask
|= POLLIN
| POLLRDNORM
;
1293 if (rfile
->output
!= NULL
) {
1294 if (snd_rawmidi_ready(rfile
->output
))
1295 mask
|= POLLOUT
| POLLWRNORM
;
1302 #ifdef CONFIG_COMPAT
1303 #include "rawmidi_compat.c"
1305 #define snd_rawmidi_ioctl_compat NULL
1312 static void snd_rawmidi_proc_info_read(struct snd_info_entry
*entry
,
1313 struct snd_info_buffer
*buffer
)
1315 struct snd_rawmidi
*rmidi
;
1316 struct snd_rawmidi_substream
*substream
;
1317 struct snd_rawmidi_runtime
*runtime
;
1319 rmidi
= entry
->private_data
;
1320 snd_iprintf(buffer
, "%s\n\n", rmidi
->name
);
1321 mutex_lock(&rmidi
->open_mutex
);
1322 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) {
1323 list_for_each_entry(substream
,
1324 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
,
1328 " Tx bytes : %lu\n",
1330 (unsigned long) substream
->bytes
);
1331 if (substream
->opened
) {
1332 runtime
= substream
->runtime
;
1335 " Buffer size : %lu\n"
1337 runtime
->oss
? "OSS compatible" : "native",
1338 (unsigned long) runtime
->buffer_size
,
1339 (unsigned long) runtime
->avail
);
1343 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
) {
1344 list_for_each_entry(substream
,
1345 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
,
1349 " Rx bytes : %lu\n",
1351 (unsigned long) substream
->bytes
);
1352 if (substream
->opened
) {
1353 runtime
= substream
->runtime
;
1355 " Buffer size : %lu\n"
1357 " Overruns : %lu\n",
1358 (unsigned long) runtime
->buffer_size
,
1359 (unsigned long) runtime
->avail
,
1360 (unsigned long) runtime
->xruns
);
1364 mutex_unlock(&rmidi
->open_mutex
);
1368 * Register functions
1371 static const struct file_operations snd_rawmidi_f_ops
=
1373 .owner
= THIS_MODULE
,
1374 .read
= snd_rawmidi_read
,
1375 .write
= snd_rawmidi_write
,
1376 .open
= snd_rawmidi_open
,
1377 .release
= snd_rawmidi_release
,
1378 .poll
= snd_rawmidi_poll
,
1379 .unlocked_ioctl
= snd_rawmidi_ioctl
,
1380 .compat_ioctl
= snd_rawmidi_ioctl_compat
,
1383 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi
*rmidi
,
1384 struct snd_rawmidi_str
*stream
,
1388 struct snd_rawmidi_substream
*substream
;
1391 for (idx
= 0; idx
< count
; idx
++) {
1392 substream
= kzalloc(sizeof(*substream
), GFP_KERNEL
);
1393 if (substream
== NULL
) {
1394 snd_printk(KERN_ERR
"rawmidi: cannot allocate substream\n");
1397 substream
->stream
= direction
;
1398 substream
->number
= idx
;
1399 substream
->rmidi
= rmidi
;
1400 substream
->pstr
= stream
;
1401 list_add_tail(&substream
->list
, &stream
->substreams
);
1402 stream
->substream_count
++;
1408 * snd_rawmidi_new - create a rawmidi instance
1409 * @card: the card instance
1410 * @id: the id string
1411 * @device: the device index
1412 * @output_count: the number of output streams
1413 * @input_count: the number of input streams
1414 * @rrawmidi: the pointer to store the new rawmidi instance
1416 * Creates a new rawmidi instance.
1417 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1419 * Returns zero if successful, or a negative error code on failure.
1421 int snd_rawmidi_new(struct snd_card
*card
, char *id
, int device
,
1422 int output_count
, int input_count
,
1423 struct snd_rawmidi
** rrawmidi
)
1425 struct snd_rawmidi
*rmidi
;
1427 static struct snd_device_ops ops
= {
1428 .dev_free
= snd_rawmidi_dev_free
,
1429 .dev_register
= snd_rawmidi_dev_register
,
1430 .dev_disconnect
= snd_rawmidi_dev_disconnect
,
1433 if (snd_BUG_ON(!card
))
1437 rmidi
= kzalloc(sizeof(*rmidi
), GFP_KERNEL
);
1438 if (rmidi
== NULL
) {
1439 snd_printk(KERN_ERR
"rawmidi: cannot allocate\n");
1443 rmidi
->device
= device
;
1444 mutex_init(&rmidi
->open_mutex
);
1445 init_waitqueue_head(&rmidi
->open_wait
);
1446 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
);
1447 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
);
1450 strlcpy(rmidi
->id
, id
, sizeof(rmidi
->id
));
1451 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1452 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
],
1453 SNDRV_RAWMIDI_STREAM_INPUT
,
1454 input_count
)) < 0) {
1455 snd_rawmidi_free(rmidi
);
1458 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1459 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
],
1460 SNDRV_RAWMIDI_STREAM_OUTPUT
,
1461 output_count
)) < 0) {
1462 snd_rawmidi_free(rmidi
);
1465 if ((err
= snd_device_new(card
, SNDRV_DEV_RAWMIDI
, rmidi
, &ops
)) < 0) {
1466 snd_rawmidi_free(rmidi
);
1474 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str
*stream
)
1476 struct snd_rawmidi_substream
*substream
;
1478 while (!list_empty(&stream
->substreams
)) {
1479 substream
= list_entry(stream
->substreams
.next
, struct snd_rawmidi_substream
, list
);
1480 list_del(&substream
->list
);
1485 static int snd_rawmidi_free(struct snd_rawmidi
*rmidi
)
1490 snd_info_free_entry(rmidi
->proc_entry
);
1491 rmidi
->proc_entry
= NULL
;
1492 mutex_lock(®ister_mutex
);
1493 if (rmidi
->ops
&& rmidi
->ops
->dev_unregister
)
1494 rmidi
->ops
->dev_unregister(rmidi
);
1495 mutex_unlock(®ister_mutex
);
1497 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
1498 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
1499 if (rmidi
->private_free
)
1500 rmidi
->private_free(rmidi
);
1505 static int snd_rawmidi_dev_free(struct snd_device
*device
)
1507 struct snd_rawmidi
*rmidi
= device
->device_data
;
1508 return snd_rawmidi_free(rmidi
);
1511 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1512 static void snd_rawmidi_dev_seq_free(struct snd_seq_device
*device
)
1514 struct snd_rawmidi
*rmidi
= device
->private_data
;
1515 rmidi
->seq_dev
= NULL
;
1519 static int snd_rawmidi_dev_register(struct snd_device
*device
)
1522 struct snd_info_entry
*entry
;
1524 struct snd_rawmidi
*rmidi
= device
->device_data
;
1526 if (rmidi
->device
>= SNDRV_RAWMIDI_DEVICES
)
1528 mutex_lock(®ister_mutex
);
1529 if (snd_rawmidi_search(rmidi
->card
, rmidi
->device
)) {
1530 mutex_unlock(®ister_mutex
);
1533 list_add_tail(&rmidi
->list
, &snd_rawmidi_devices
);
1534 sprintf(name
, "midiC%iD%i", rmidi
->card
->number
, rmidi
->device
);
1535 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI
,
1536 rmidi
->card
, rmidi
->device
,
1537 &snd_rawmidi_f_ops
, rmidi
, name
)) < 0) {
1538 snd_printk(KERN_ERR
"unable to register rawmidi device %i:%i\n", rmidi
->card
->number
, rmidi
->device
);
1539 list_del(&rmidi
->list
);
1540 mutex_unlock(®ister_mutex
);
1543 if (rmidi
->ops
&& rmidi
->ops
->dev_register
&&
1544 (err
= rmidi
->ops
->dev_register(rmidi
)) < 0) {
1545 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1546 list_del(&rmidi
->list
);
1547 mutex_unlock(®ister_mutex
);
1550 #ifdef CONFIG_SND_OSSEMUL
1552 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1553 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1554 rmidi
->card
, 0, &snd_rawmidi_f_ops
,
1556 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 0);
1559 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1560 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
, rmidi
->name
);
1564 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
]) {
1565 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1566 rmidi
->card
, 1, &snd_rawmidi_f_ops
,
1568 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 1);
1573 #endif /* CONFIG_SND_OSSEMUL */
1574 mutex_unlock(®ister_mutex
);
1575 sprintf(name
, "midi%d", rmidi
->device
);
1576 entry
= snd_info_create_card_entry(rmidi
->card
, name
, rmidi
->card
->proc_root
);
1578 entry
->private_data
= rmidi
;
1579 entry
->c
.text
.read
= snd_rawmidi_proc_info_read
;
1580 if (snd_info_register(entry
) < 0) {
1581 snd_info_free_entry(entry
);
1585 rmidi
->proc_entry
= entry
;
1586 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1587 if (!rmidi
->ops
|| !rmidi
->ops
->dev_register
) { /* own registration mechanism */
1588 if (snd_seq_device_new(rmidi
->card
, rmidi
->device
, SNDRV_SEQ_DEV_ID_MIDISYNTH
, 0, &rmidi
->seq_dev
) >= 0) {
1589 rmidi
->seq_dev
->private_data
= rmidi
;
1590 rmidi
->seq_dev
->private_free
= snd_rawmidi_dev_seq_free
;
1591 sprintf(rmidi
->seq_dev
->name
, "MIDI %d-%d", rmidi
->card
->number
, rmidi
->device
);
1592 snd_device_register(rmidi
->card
, rmidi
->seq_dev
);
1599 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
)
1601 struct snd_rawmidi
*rmidi
= device
->device_data
;
1603 mutex_lock(®ister_mutex
);
1604 list_del_init(&rmidi
->list
);
1605 #ifdef CONFIG_SND_OSSEMUL
1606 if (rmidi
->ossreg
) {
1607 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1608 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 0);
1609 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1610 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
);
1613 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
])
1614 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 1);
1617 #endif /* CONFIG_SND_OSSEMUL */
1618 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1619 mutex_unlock(®ister_mutex
);
1624 * snd_rawmidi_set_ops - set the rawmidi operators
1625 * @rmidi: the rawmidi instance
1626 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1627 * @ops: the operator table
1629 * Sets the rawmidi operators for the given stream direction.
1631 void snd_rawmidi_set_ops(struct snd_rawmidi
*rmidi
, int stream
,
1632 struct snd_rawmidi_ops
*ops
)
1634 struct snd_rawmidi_substream
*substream
;
1636 list_for_each_entry(substream
, &rmidi
->streams
[stream
].substreams
, list
)
1637 substream
->ops
= ops
;
1644 static int __init
alsa_rawmidi_init(void)
1647 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl
);
1648 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl
);
1649 #ifdef CONFIG_SND_OSSEMUL
1651 /* check device map table */
1652 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1653 if (midi_map
[i
] < 0 || midi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1654 snd_printk(KERN_ERR
"invalid midi_map[%d] = %d\n", i
, midi_map
[i
]);
1657 if (amidi_map
[i
] < 0 || amidi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1658 snd_printk(KERN_ERR
"invalid amidi_map[%d] = %d\n", i
, amidi_map
[i
]);
1663 #endif /* CONFIG_SND_OSSEMUL */
1667 static void __exit
alsa_rawmidi_exit(void)
1669 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl
);
1670 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl
);
1673 module_init(alsa_rawmidi_init
)
1674 module_exit(alsa_rawmidi_exit
)
1676 EXPORT_SYMBOL(snd_rawmidi_output_params
);
1677 EXPORT_SYMBOL(snd_rawmidi_input_params
);
1678 EXPORT_SYMBOL(snd_rawmidi_drop_output
);
1679 EXPORT_SYMBOL(snd_rawmidi_drain_output
);
1680 EXPORT_SYMBOL(snd_rawmidi_drain_input
);
1681 EXPORT_SYMBOL(snd_rawmidi_receive
);
1682 EXPORT_SYMBOL(snd_rawmidi_transmit_empty
);
1683 EXPORT_SYMBOL(snd_rawmidi_transmit_peek
);
1684 EXPORT_SYMBOL(snd_rawmidi_transmit_ack
);
1685 EXPORT_SYMBOL(snd_rawmidi_transmit
);
1686 EXPORT_SYMBOL(snd_rawmidi_new
);
1687 EXPORT_SYMBOL(snd_rawmidi_set_ops
);
1688 EXPORT_SYMBOL(snd_rawmidi_info_select
);
1689 EXPORT_SYMBOL(snd_rawmidi_kernel_open
);
1690 EXPORT_SYMBOL(snd_rawmidi_kernel_release
);
1691 EXPORT_SYMBOL(snd_rawmidi_kernel_read
);
1692 EXPORT_SYMBOL(snd_rawmidi_kernel_write
);