2 * Abstract layer for MIDI v1.0 stream
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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/driver.h>
23 #include <sound/core.h>
24 #include <linux/major.h>
25 #include <linux/init.h>
26 #include <linux/smp_lock.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/time.h>
30 #include <linux/wait.h>
31 #include <linux/mutex.h>
32 #include <linux/moduleparam.h>
33 #include <linux/delay.h>
34 #include <linux/wait.h>
35 #include <sound/rawmidi.h>
36 #include <sound/info.h>
37 #include <sound/control.h>
38 #include <sound/minors.h>
39 #include <sound/initval.h>
41 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
42 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
43 MODULE_LICENSE("GPL");
45 #ifdef CONFIG_SND_OSSEMUL
46 static int midi_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 0};
47 static int amidi_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
48 module_param_array(midi_map
, int, NULL
, 0444);
49 MODULE_PARM_DESC(midi_map
, "Raw MIDI device number assigned to 1st OSS device.");
50 module_param_array(amidi_map
, int, NULL
, 0444);
51 MODULE_PARM_DESC(amidi_map
, "Raw MIDI device number assigned to 2nd OSS device.");
52 #endif /* CONFIG_SND_OSSEMUL */
54 static int snd_rawmidi_free(struct snd_rawmidi
*rawmidi
);
55 static int snd_rawmidi_dev_free(struct snd_device
*device
);
56 static int snd_rawmidi_dev_register(struct snd_device
*device
);
57 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
);
58 static int snd_rawmidi_dev_unregister(struct snd_device
*device
);
60 static LIST_HEAD(snd_rawmidi_devices
);
61 static DEFINE_MUTEX(register_mutex
);
63 static struct snd_rawmidi
*snd_rawmidi_search(struct snd_card
*card
, int device
)
66 struct snd_rawmidi
*rawmidi
;
68 list_for_each(p
, &snd_rawmidi_devices
) {
69 rawmidi
= list_entry(p
, struct snd_rawmidi
, list
);
70 if (rawmidi
->card
== card
&& rawmidi
->device
== device
)
76 static inline unsigned short snd_rawmidi_file_flags(struct file
*file
)
78 switch (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) {
80 return SNDRV_RAWMIDI_LFLG_OUTPUT
;
82 return SNDRV_RAWMIDI_LFLG_INPUT
;
84 return SNDRV_RAWMIDI_LFLG_OPEN
;
88 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream
*substream
)
90 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
91 return runtime
->avail
>= runtime
->avail_min
;
94 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream
*substream
,
97 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
98 return runtime
->avail
>= runtime
->avail_min
&&
99 (!substream
->append
|| runtime
->avail
>= count
);
102 static void snd_rawmidi_input_event_tasklet(unsigned long data
)
104 struct snd_rawmidi_substream
*substream
= (struct snd_rawmidi_substream
*)data
;
105 substream
->runtime
->event(substream
);
108 static void snd_rawmidi_output_trigger_tasklet(unsigned long data
)
110 struct snd_rawmidi_substream
*substream
= (struct snd_rawmidi_substream
*)data
;
111 substream
->ops
->trigger(substream
, 1);
114 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream
*substream
)
116 struct snd_rawmidi_runtime
*runtime
;
118 if ((runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
)) == NULL
)
120 spin_lock_init(&runtime
->lock
);
121 init_waitqueue_head(&runtime
->sleep
);
122 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
123 tasklet_init(&runtime
->tasklet
,
124 snd_rawmidi_input_event_tasklet
,
125 (unsigned long)substream
);
127 tasklet_init(&runtime
->tasklet
,
128 snd_rawmidi_output_trigger_tasklet
,
129 (unsigned long)substream
);
130 runtime
->event
= NULL
;
131 runtime
->buffer_size
= PAGE_SIZE
;
132 runtime
->avail_min
= 1;
133 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
136 runtime
->avail
= runtime
->buffer_size
;
137 if ((runtime
->buffer
= kmalloc(runtime
->buffer_size
, GFP_KERNEL
)) == NULL
) {
141 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
142 substream
->runtime
= runtime
;
146 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream
*substream
)
148 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
150 kfree(runtime
->buffer
);
152 substream
->runtime
= NULL
;
156 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream
*substream
,int up
)
159 tasklet_hi_schedule(&substream
->runtime
->tasklet
);
161 tasklet_kill(&substream
->runtime
->tasklet
);
162 substream
->ops
->trigger(substream
, 0);
166 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
168 substream
->ops
->trigger(substream
, up
);
169 if (!up
&& substream
->runtime
->event
)
170 tasklet_kill(&substream
->runtime
->tasklet
);
173 int snd_rawmidi_drop_output(struct snd_rawmidi_substream
*substream
)
176 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
178 snd_rawmidi_output_trigger(substream
, 0);
180 spin_lock_irqsave(&runtime
->lock
, flags
);
181 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
182 runtime
->avail
= runtime
->buffer_size
;
183 spin_unlock_irqrestore(&runtime
->lock
, flags
);
187 int snd_rawmidi_drain_output(struct snd_rawmidi_substream
*substream
)
191 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
195 timeout
= wait_event_interruptible_timeout(runtime
->sleep
,
196 (runtime
->avail
>= runtime
->buffer_size
),
198 if (signal_pending(current
))
200 if (runtime
->avail
< runtime
->buffer_size
&& !timeout
) {
201 snd_printk(KERN_WARNING
"rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime
->avail
, (long)runtime
->buffer_size
);
205 if (err
!= -ERESTARTSYS
) {
206 /* we need wait a while to make sure that Tx FIFOs are empty */
207 if (substream
->ops
->drain
)
208 substream
->ops
->drain(substream
);
211 snd_rawmidi_drop_output(substream
);
216 int snd_rawmidi_drain_input(struct snd_rawmidi_substream
*substream
)
219 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
221 snd_rawmidi_input_trigger(substream
, 0);
223 spin_lock_irqsave(&runtime
->lock
, flags
);
224 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
226 spin_unlock_irqrestore(&runtime
->lock
, flags
);
230 int snd_rawmidi_kernel_open(struct snd_card
*card
, int device
, int subdevice
,
231 int mode
, struct snd_rawmidi_file
* rfile
)
233 struct snd_rawmidi
*rmidi
;
234 struct list_head
*list1
, *list2
;
235 struct snd_rawmidi_substream
*sinput
= NULL
, *soutput
= NULL
;
236 struct snd_rawmidi_runtime
*input
= NULL
, *output
= NULL
;
240 rfile
->input
= rfile
->output
= NULL
;
241 mutex_lock(®ister_mutex
);
242 rmidi
= snd_rawmidi_search(card
, device
);
243 mutex_unlock(®ister_mutex
);
248 if (!try_module_get(rmidi
->card
->module
)) {
252 if (!(mode
& SNDRV_RAWMIDI_LFLG_NOOPENLOCK
))
253 mutex_lock(&rmidi
->open_mutex
);
254 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
255 if (!(rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
)) {
259 if (subdevice
>= 0 && (unsigned int)subdevice
>= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_count
) {
263 if (rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_opened
>=
264 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_count
) {
269 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
270 if (!(rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
)) {
274 if (subdevice
>= 0 && (unsigned int)subdevice
>= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_count
) {
278 if (rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_opened
>=
279 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_count
) {
284 list1
= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
.next
;
286 if (list1
== &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
) {
288 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
294 sinput
= list_entry(list1
, struct snd_rawmidi_substream
, list
);
295 if ((mode
& SNDRV_RAWMIDI_LFLG_INPUT
) && sinput
->opened
)
297 if (subdevice
< 0 || (subdevice
>= 0 && subdevice
== sinput
->number
))
302 list2
= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
.next
;
304 if (list2
== &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
) {
306 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
312 soutput
= list_entry(list2
, struct snd_rawmidi_substream
, list
);
313 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
314 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
) {
315 if (soutput
->opened
&& !soutput
->append
)
322 if (subdevice
< 0 || (subdevice
>= 0 && subdevice
== soutput
->number
))
327 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
328 if ((err
= snd_rawmidi_runtime_create(sinput
)) < 0)
330 input
= sinput
->runtime
;
331 if ((err
= sinput
->ops
->open(sinput
)) < 0)
334 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_opened
++;
338 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
341 if ((err
= snd_rawmidi_runtime_create(soutput
)) < 0) {
342 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
)
343 sinput
->ops
->close(sinput
);
346 output
= soutput
->runtime
;
347 if ((err
= soutput
->ops
->open(soutput
)) < 0) {
348 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
)
349 sinput
->ops
->close(sinput
);
354 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
)
356 if (soutput
->use_count
++ == 0)
357 soutput
->active_sensing
= 1;
358 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_opened
++;
362 if (!(mode
& SNDRV_RAWMIDI_LFLG_NOOPENLOCK
))
363 mutex_unlock(&rmidi
->open_mutex
);
365 rfile
->rmidi
= rmidi
;
366 rfile
->input
= sinput
;
367 rfile
->output
= soutput
;
373 snd_rawmidi_runtime_free(sinput
);
375 snd_rawmidi_runtime_free(soutput
);
376 module_put(rmidi
->card
->module
);
377 if (!(mode
& SNDRV_RAWMIDI_LFLG_NOOPENLOCK
))
378 mutex_unlock(&rmidi
->open_mutex
);
383 static int snd_rawmidi_open(struct inode
*inode
, struct file
*file
)
385 int maj
= imajor(inode
);
386 struct snd_card
*card
;
388 unsigned short fflags
;
390 struct snd_rawmidi
*rmidi
;
391 struct snd_rawmidi_file
*rawmidi_file
;
393 struct list_head
*list
;
394 struct snd_ctl_file
*kctl
;
396 if (maj
== snd_major
) {
397 rmidi
= snd_lookup_minor_data(iminor(inode
),
398 SNDRV_DEVICE_TYPE_RAWMIDI
);
399 #ifdef CONFIG_SND_OSSEMUL
400 } else if (maj
== SOUND_MAJOR
) {
401 rmidi
= snd_lookup_oss_minor_data(iminor(inode
),
402 SNDRV_OSS_DEVICE_TYPE_MIDI
);
409 if ((file
->f_flags
& O_APPEND
) && !(file
->f_flags
& O_NONBLOCK
))
410 return -EINVAL
; /* invalid combination */
412 err
= snd_card_file_add(card
, file
);
415 fflags
= snd_rawmidi_file_flags(file
);
416 if ((file
->f_flags
& O_APPEND
) || maj
== SOUND_MAJOR
) /* OSS emul? */
417 fflags
|= SNDRV_RAWMIDI_LFLG_APPEND
;
418 fflags
|= SNDRV_RAWMIDI_LFLG_NOOPENLOCK
;
419 rawmidi_file
= kmalloc(sizeof(*rawmidi_file
), GFP_KERNEL
);
420 if (rawmidi_file
== NULL
) {
421 snd_card_file_remove(card
, file
);
424 init_waitqueue_entry(&wait
, current
);
425 add_wait_queue(&rmidi
->open_wait
, &wait
);
426 mutex_lock(&rmidi
->open_mutex
);
429 down_read(&card
->controls_rwsem
);
430 list_for_each(list
, &card
->ctl_files
) {
431 kctl
= snd_ctl_file(list
);
432 if (kctl
->pid
== current
->pid
) {
433 subdevice
= kctl
->prefer_rawmidi_subdevice
;
437 up_read(&card
->controls_rwsem
);
438 err
= snd_rawmidi_kernel_open(rmidi
->card
, rmidi
->device
,
439 subdevice
, fflags
, rawmidi_file
);
442 if (err
== -EAGAIN
) {
443 if (file
->f_flags
& O_NONBLOCK
) {
449 set_current_state(TASK_INTERRUPTIBLE
);
450 mutex_unlock(&rmidi
->open_mutex
);
452 mutex_lock(&rmidi
->open_mutex
);
453 if (signal_pending(current
)) {
458 #ifdef CONFIG_SND_OSSEMUL
459 if (rawmidi_file
->input
&& rawmidi_file
->input
->runtime
)
460 rawmidi_file
->input
->runtime
->oss
= (maj
== SOUND_MAJOR
);
461 if (rawmidi_file
->output
&& rawmidi_file
->output
->runtime
)
462 rawmidi_file
->output
->runtime
->oss
= (maj
== SOUND_MAJOR
);
464 remove_wait_queue(&rmidi
->open_wait
, &wait
);
466 file
->private_data
= rawmidi_file
;
468 snd_card_file_remove(card
, file
);
471 mutex_unlock(&rmidi
->open_mutex
);
475 int snd_rawmidi_kernel_release(struct snd_rawmidi_file
* rfile
)
477 struct snd_rawmidi
*rmidi
;
478 struct snd_rawmidi_substream
*substream
;
479 struct snd_rawmidi_runtime
*runtime
;
481 snd_assert(rfile
!= NULL
, return -ENXIO
);
482 snd_assert(rfile
->input
!= NULL
|| rfile
->output
!= NULL
, return -ENXIO
);
483 rmidi
= rfile
->rmidi
;
484 mutex_lock(&rmidi
->open_mutex
);
485 if (rfile
->input
!= NULL
) {
486 substream
= rfile
->input
;
488 runtime
= substream
->runtime
;
489 snd_rawmidi_input_trigger(substream
, 0);
490 substream
->ops
->close(substream
);
491 if (runtime
->private_free
!= NULL
)
492 runtime
->private_free(substream
);
493 snd_rawmidi_runtime_free(substream
);
494 substream
->opened
= 0;
495 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_opened
--;
497 if (rfile
->output
!= NULL
) {
498 substream
= rfile
->output
;
499 rfile
->output
= NULL
;
500 if (--substream
->use_count
== 0) {
501 runtime
= substream
->runtime
;
502 if (substream
->active_sensing
) {
503 unsigned char buf
= 0xfe;
504 /* sending single active sensing message to shut the device up */
505 snd_rawmidi_kernel_write(substream
, &buf
, 1);
507 if (snd_rawmidi_drain_output(substream
) == -ERESTARTSYS
)
508 snd_rawmidi_output_trigger(substream
, 0);
509 substream
->ops
->close(substream
);
510 if (runtime
->private_free
!= NULL
)
511 runtime
->private_free(substream
);
512 snd_rawmidi_runtime_free(substream
);
513 substream
->opened
= 0;
514 substream
->append
= 0;
516 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_opened
--;
518 mutex_unlock(&rmidi
->open_mutex
);
519 module_put(rmidi
->card
->module
);
523 static int snd_rawmidi_release(struct inode
*inode
, struct file
*file
)
525 struct snd_rawmidi_file
*rfile
;
526 struct snd_rawmidi
*rmidi
;
529 rfile
= file
->private_data
;
530 err
= snd_rawmidi_kernel_release(rfile
);
531 rmidi
= rfile
->rmidi
;
532 wake_up(&rmidi
->open_wait
);
534 snd_card_file_remove(rmidi
->card
, file
);
538 static int snd_rawmidi_info(struct snd_rawmidi_substream
*substream
,
539 struct snd_rawmidi_info
*info
)
541 struct snd_rawmidi
*rmidi
;
543 if (substream
== NULL
)
545 rmidi
= substream
->rmidi
;
546 memset(info
, 0, sizeof(*info
));
547 info
->card
= rmidi
->card
->number
;
548 info
->device
= rmidi
->device
;
549 info
->subdevice
= substream
->number
;
550 info
->stream
= substream
->stream
;
551 info
->flags
= rmidi
->info_flags
;
552 strcpy(info
->id
, rmidi
->id
);
553 strcpy(info
->name
, rmidi
->name
);
554 strcpy(info
->subname
, substream
->name
);
555 info
->subdevices_count
= substream
->pstr
->substream_count
;
556 info
->subdevices_avail
= (substream
->pstr
->substream_count
-
557 substream
->pstr
->substream_opened
);
561 static int snd_rawmidi_info_user(struct snd_rawmidi_substream
*substream
,
562 struct snd_rawmidi_info __user
* _info
)
564 struct snd_rawmidi_info info
;
566 if ((err
= snd_rawmidi_info(substream
, &info
)) < 0)
568 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
573 int snd_rawmidi_info_select(struct snd_card
*card
, struct snd_rawmidi_info
*info
)
575 struct snd_rawmidi
*rmidi
;
576 struct snd_rawmidi_str
*pstr
;
577 struct snd_rawmidi_substream
*substream
;
578 struct list_head
*list
;
580 mutex_lock(®ister_mutex
);
581 rmidi
= snd_rawmidi_search(card
, info
->device
);
582 mutex_unlock(®ister_mutex
);
585 if (info
->stream
< 0 || info
->stream
> 1)
587 pstr
= &rmidi
->streams
[info
->stream
];
588 if (pstr
->substream_count
== 0)
590 if (info
->subdevice
>= pstr
->substream_count
)
592 list_for_each(list
, &pstr
->substreams
) {
593 substream
= list_entry(list
, struct snd_rawmidi_substream
, list
);
594 if ((unsigned int)substream
->number
== info
->subdevice
)
595 return snd_rawmidi_info(substream
, info
);
600 static int snd_rawmidi_info_select_user(struct snd_card
*card
,
601 struct snd_rawmidi_info __user
*_info
)
604 struct snd_rawmidi_info info
;
605 if (get_user(info
.device
, &_info
->device
))
607 if (get_user(info
.stream
, &_info
->stream
))
609 if (get_user(info
.subdevice
, &_info
->subdevice
))
611 if ((err
= snd_rawmidi_info_select(card
, &info
)) < 0)
613 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
618 int snd_rawmidi_output_params(struct snd_rawmidi_substream
*substream
,
619 struct snd_rawmidi_params
* params
)
622 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
624 if (substream
->append
&& substream
->use_count
> 1)
626 snd_rawmidi_drain_output(substream
);
627 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
630 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
633 if (params
->buffer_size
!= runtime
->buffer_size
) {
634 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
637 kfree(runtime
->buffer
);
638 runtime
->buffer
= newbuf
;
639 runtime
->buffer_size
= params
->buffer_size
;
640 runtime
->avail
= runtime
->buffer_size
;
642 runtime
->avail_min
= params
->avail_min
;
643 substream
->active_sensing
= !params
->no_active_sensing
;
647 int snd_rawmidi_input_params(struct snd_rawmidi_substream
*substream
,
648 struct snd_rawmidi_params
* params
)
651 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
653 snd_rawmidi_drain_input(substream
);
654 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
657 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
660 if (params
->buffer_size
!= runtime
->buffer_size
) {
661 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
664 kfree(runtime
->buffer
);
665 runtime
->buffer
= newbuf
;
666 runtime
->buffer_size
= params
->buffer_size
;
668 runtime
->avail_min
= params
->avail_min
;
672 static int snd_rawmidi_output_status(struct snd_rawmidi_substream
*substream
,
673 struct snd_rawmidi_status
* status
)
675 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
677 memset(status
, 0, sizeof(*status
));
678 status
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
679 spin_lock_irq(&runtime
->lock
);
680 status
->avail
= runtime
->avail
;
681 spin_unlock_irq(&runtime
->lock
);
685 static int snd_rawmidi_input_status(struct snd_rawmidi_substream
*substream
,
686 struct snd_rawmidi_status
* status
)
688 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
690 memset(status
, 0, sizeof(*status
));
691 status
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
692 spin_lock_irq(&runtime
->lock
);
693 status
->avail
= runtime
->avail
;
694 status
->xruns
= runtime
->xruns
;
696 spin_unlock_irq(&runtime
->lock
);
700 static long snd_rawmidi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
702 struct snd_rawmidi_file
*rfile
;
703 void __user
*argp
= (void __user
*)arg
;
705 rfile
= file
->private_data
;
706 if (((cmd
>> 8) & 0xff) != 'W')
709 case SNDRV_RAWMIDI_IOCTL_PVERSION
:
710 return put_user(SNDRV_RAWMIDI_VERSION
, (int __user
*)argp
) ? -EFAULT
: 0;
711 case SNDRV_RAWMIDI_IOCTL_INFO
:
714 struct snd_rawmidi_info __user
*info
= argp
;
715 if (get_user(stream
, &info
->stream
))
718 case SNDRV_RAWMIDI_STREAM_INPUT
:
719 return snd_rawmidi_info_user(rfile
->input
, info
);
720 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
721 return snd_rawmidi_info_user(rfile
->output
, info
);
726 case SNDRV_RAWMIDI_IOCTL_PARAMS
:
728 struct snd_rawmidi_params params
;
729 if (copy_from_user(¶ms
, argp
, sizeof(struct snd_rawmidi_params
)))
731 switch (params
.stream
) {
732 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
733 if (rfile
->output
== NULL
)
735 return snd_rawmidi_output_params(rfile
->output
, ¶ms
);
736 case SNDRV_RAWMIDI_STREAM_INPUT
:
737 if (rfile
->input
== NULL
)
739 return snd_rawmidi_input_params(rfile
->input
, ¶ms
);
744 case SNDRV_RAWMIDI_IOCTL_STATUS
:
747 struct snd_rawmidi_status status
;
748 if (copy_from_user(&status
, argp
, sizeof(struct snd_rawmidi_status
)))
750 switch (status
.stream
) {
751 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
752 if (rfile
->output
== NULL
)
754 err
= snd_rawmidi_output_status(rfile
->output
, &status
);
756 case SNDRV_RAWMIDI_STREAM_INPUT
:
757 if (rfile
->input
== NULL
)
759 err
= snd_rawmidi_input_status(rfile
->input
, &status
);
766 if (copy_to_user(argp
, &status
, sizeof(struct snd_rawmidi_status
)))
770 case SNDRV_RAWMIDI_IOCTL_DROP
:
773 if (get_user(val
, (int __user
*) argp
))
776 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
777 if (rfile
->output
== NULL
)
779 return snd_rawmidi_drop_output(rfile
->output
);
784 case SNDRV_RAWMIDI_IOCTL_DRAIN
:
787 if (get_user(val
, (int __user
*) argp
))
790 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
791 if (rfile
->output
== NULL
)
793 return snd_rawmidi_drain_output(rfile
->output
);
794 case SNDRV_RAWMIDI_STREAM_INPUT
:
795 if (rfile
->input
== NULL
)
797 return snd_rawmidi_drain_input(rfile
->input
);
802 #ifdef CONFIG_SND_DEBUG
804 snd_printk(KERN_WARNING
"rawmidi: unknown command = 0x%x\n", cmd
);
810 static int snd_rawmidi_control_ioctl(struct snd_card
*card
,
811 struct snd_ctl_file
*control
,
815 void __user
*argp
= (void __user
*)arg
;
818 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE
:
822 if (get_user(device
, (int __user
*)argp
))
824 mutex_lock(®ister_mutex
);
825 device
= device
< 0 ? 0 : device
+ 1;
826 while (device
< SNDRV_RAWMIDI_DEVICES
) {
827 if (snd_rawmidi_search(card
, device
))
831 if (device
== SNDRV_RAWMIDI_DEVICES
)
833 mutex_unlock(®ister_mutex
);
834 if (put_user(device
, (int __user
*)argp
))
838 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE
:
842 if (get_user(val
, (int __user
*)argp
))
844 control
->prefer_rawmidi_subdevice
= val
;
847 case SNDRV_CTL_IOCTL_RAWMIDI_INFO
:
848 return snd_rawmidi_info_select_user(card
, argp
);
854 * snd_rawmidi_receive - receive the input data from the device
855 * @substream: the rawmidi substream
856 * @buffer: the buffer pointer
857 * @count: the data size to read
859 * Reads the data from the internal buffer.
861 * Returns the size of read data, or a negative error code on failure.
863 int snd_rawmidi_receive(struct snd_rawmidi_substream
*substream
,
864 const unsigned char *buffer
, int count
)
867 int result
= 0, count1
;
868 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
870 if (runtime
->buffer
== NULL
) {
871 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
874 spin_lock_irqsave(&runtime
->lock
, flags
);
875 if (count
== 1) { /* special case, faster code */
877 if (runtime
->avail
< runtime
->buffer_size
) {
878 runtime
->buffer
[runtime
->hw_ptr
++] = buffer
[0];
879 runtime
->hw_ptr
%= runtime
->buffer_size
;
886 substream
->bytes
+= count
;
887 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
890 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
891 count1
= runtime
->buffer_size
- runtime
->avail
;
892 memcpy(runtime
->buffer
+ runtime
->hw_ptr
, buffer
, count1
);
893 runtime
->hw_ptr
+= count1
;
894 runtime
->hw_ptr
%= runtime
->buffer_size
;
895 runtime
->avail
+= count1
;
901 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
)) {
902 count1
= runtime
->buffer_size
- runtime
->avail
;
903 runtime
->xruns
+= count
- count1
;
906 memcpy(runtime
->buffer
, buffer
, count1
);
907 runtime
->hw_ptr
= count1
;
908 runtime
->avail
+= count1
;
915 tasklet_hi_schedule(&runtime
->tasklet
);
916 else if (snd_rawmidi_ready(substream
))
917 wake_up(&runtime
->sleep
);
919 spin_unlock_irqrestore(&runtime
->lock
, flags
);
923 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream
*substream
,
924 unsigned char *buf
, long count
, int kernel
)
927 long result
= 0, count1
;
928 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
930 while (count
> 0 && runtime
->avail
) {
931 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
934 spin_lock_irqsave(&runtime
->lock
, flags
);
935 if (count1
> (int)runtime
->avail
)
936 count1
= runtime
->avail
;
938 memcpy(buf
+ result
, runtime
->buffer
+ runtime
->appl_ptr
, count1
);
940 spin_unlock_irqrestore(&runtime
->lock
, flags
);
941 if (copy_to_user((char __user
*)buf
+ result
,
942 runtime
->buffer
+ runtime
->appl_ptr
, count1
)) {
943 return result
> 0 ? result
: -EFAULT
;
945 spin_lock_irqsave(&runtime
->lock
, flags
);
947 runtime
->appl_ptr
+= count1
;
948 runtime
->appl_ptr
%= runtime
->buffer_size
;
949 runtime
->avail
-= count1
;
950 spin_unlock_irqrestore(&runtime
->lock
, flags
);
957 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream
*substream
,
958 unsigned char *buf
, long count
)
960 snd_rawmidi_input_trigger(substream
, 1);
961 return snd_rawmidi_kernel_read1(substream
, buf
, count
, 1);
964 static ssize_t
snd_rawmidi_read(struct file
*file
, char __user
*buf
, size_t count
,
969 struct snd_rawmidi_file
*rfile
;
970 struct snd_rawmidi_substream
*substream
;
971 struct snd_rawmidi_runtime
*runtime
;
973 rfile
= file
->private_data
;
974 substream
= rfile
->input
;
975 if (substream
== NULL
)
977 runtime
= substream
->runtime
;
978 snd_rawmidi_input_trigger(substream
, 1);
981 spin_lock_irq(&runtime
->lock
);
982 while (!snd_rawmidi_ready(substream
)) {
984 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
985 spin_unlock_irq(&runtime
->lock
);
986 return result
> 0 ? result
: -EAGAIN
;
988 init_waitqueue_entry(&wait
, current
);
989 add_wait_queue(&runtime
->sleep
, &wait
);
990 set_current_state(TASK_INTERRUPTIBLE
);
991 spin_unlock_irq(&runtime
->lock
);
993 remove_wait_queue(&runtime
->sleep
, &wait
);
994 if (signal_pending(current
))
995 return result
> 0 ? result
: -ERESTARTSYS
;
997 return result
> 0 ? result
: -EIO
;
998 spin_lock_irq(&runtime
->lock
);
1000 spin_unlock_irq(&runtime
->lock
);
1001 count1
= snd_rawmidi_kernel_read1(substream
,
1002 (unsigned char __force
*)buf
,
1005 return result
> 0 ? result
: count1
;
1014 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1015 * @substream: the rawmidi substream
1017 * Returns 1 if the internal output buffer is empty, 0 if not.
1019 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream
*substream
)
1021 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1023 unsigned long flags
;
1025 if (runtime
->buffer
== NULL
) {
1026 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1029 spin_lock_irqsave(&runtime
->lock
, flags
);
1030 result
= runtime
->avail
>= runtime
->buffer_size
;
1031 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1036 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1037 * @substream: the rawmidi substream
1038 * @buffer: the buffer pointer
1039 * @count: data size to transfer
1041 * Copies data from the internal output buffer to the given buffer.
1043 * Call this in the interrupt handler when the midi output is ready,
1044 * and call snd_rawmidi_transmit_ack() after the transmission is
1047 * Returns the size of copied data, or a negative error code on failure.
1049 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1050 unsigned char *buffer
, int count
)
1052 unsigned long flags
;
1054 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1056 if (runtime
->buffer
== NULL
) {
1057 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1061 spin_lock_irqsave(&runtime
->lock
, flags
);
1062 if (runtime
->avail
>= runtime
->buffer_size
) {
1063 /* warning: lowlevel layer MUST trigger down the hardware */
1066 if (count
== 1) { /* special case, faster code */
1067 *buffer
= runtime
->buffer
[runtime
->hw_ptr
];
1070 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
1073 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
1074 count1
= runtime
->buffer_size
- runtime
->avail
;
1075 memcpy(buffer
, runtime
->buffer
+ runtime
->hw_ptr
, count1
);
1079 if (count
> (int)(runtime
->buffer_size
- runtime
->avail
- count1
))
1080 count
= runtime
->buffer_size
- runtime
->avail
- count1
;
1081 memcpy(buffer
+ count1
, runtime
->buffer
, count
);
1086 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1091 * snd_rawmidi_transmit_ack - acknowledge the transmission
1092 * @substream: the rawmidi substream
1093 * @count: the tranferred count
1095 * Advances the hardware pointer for the internal output buffer with
1096 * the given size and updates the condition.
1097 * Call after the transmission is finished.
1099 * Returns the advanced size if successful, or a negative error code on failure.
1101 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1103 unsigned long flags
;
1104 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1106 if (runtime
->buffer
== NULL
) {
1107 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1110 spin_lock_irqsave(&runtime
->lock
, flags
);
1111 snd_assert(runtime
->avail
+ count
<= runtime
->buffer_size
, );
1112 runtime
->hw_ptr
+= count
;
1113 runtime
->hw_ptr
%= runtime
->buffer_size
;
1114 runtime
->avail
+= count
;
1115 substream
->bytes
+= count
;
1117 if (runtime
->drain
|| snd_rawmidi_ready(substream
))
1118 wake_up(&runtime
->sleep
);
1120 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1125 * snd_rawmidi_transmit - copy from the buffer to the device
1126 * @substream: the rawmidi substream
1127 * @buffer: the buffer pointer
1128 * @count: the data size to transfer
1130 * Copies data from the buffer to the device and advances the pointer.
1132 * Returns the copied size if successful, or a negative error code on failure.
1134 int snd_rawmidi_transmit(struct snd_rawmidi_substream
*substream
,
1135 unsigned char *buffer
, int count
)
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 *buf
, long count
, int kernel
)
1146 unsigned long flags
;
1147 long count1
, result
;
1148 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1150 snd_assert(buf
!= NULL
, return -EINVAL
);
1151 snd_assert(runtime
->buffer
!= NULL
, return -EINVAL
);
1154 spin_lock_irqsave(&runtime
->lock
, flags
);
1155 if (substream
->append
) {
1156 if ((long)runtime
->avail
< count
) {
1157 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1161 while (count
> 0 && runtime
->avail
> 0) {
1162 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1165 if (count1
> (long)runtime
->avail
)
1166 count1
= runtime
->avail
;
1168 memcpy(runtime
->buffer
+ runtime
->appl_ptr
, buf
, count1
);
1170 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1171 if (copy_from_user(runtime
->buffer
+ runtime
->appl_ptr
,
1172 (char __user
*)buf
, count1
)) {
1173 spin_lock_irqsave(&runtime
->lock
, flags
);
1174 result
= result
> 0 ? result
: -EFAULT
;
1177 spin_lock_irqsave(&runtime
->lock
, flags
);
1179 runtime
->appl_ptr
+= count1
;
1180 runtime
->appl_ptr
%= runtime
->buffer_size
;
1181 runtime
->avail
-= count1
;
1187 count1
= runtime
->avail
< runtime
->buffer_size
;
1188 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1190 snd_rawmidi_output_trigger(substream
, 1);
1194 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream
*substream
,
1195 const unsigned char *buf
, long count
)
1197 return snd_rawmidi_kernel_write1(substream
, buf
, count
, 1);
1200 static ssize_t
snd_rawmidi_write(struct file
*file
, const char __user
*buf
,
1201 size_t count
, loff_t
*offset
)
1203 long result
, timeout
;
1205 struct snd_rawmidi_file
*rfile
;
1206 struct snd_rawmidi_runtime
*runtime
;
1207 struct snd_rawmidi_substream
*substream
;
1209 rfile
= file
->private_data
;
1210 substream
= rfile
->output
;
1211 runtime
= substream
->runtime
;
1212 /* we cannot put an atomic message to our buffer */
1213 if (substream
->append
&& count
> runtime
->buffer_size
)
1217 spin_lock_irq(&runtime
->lock
);
1218 while (!snd_rawmidi_ready_append(substream
, count
)) {
1220 if (file
->f_flags
& O_NONBLOCK
) {
1221 spin_unlock_irq(&runtime
->lock
);
1222 return result
> 0 ? result
: -EAGAIN
;
1224 init_waitqueue_entry(&wait
, current
);
1225 add_wait_queue(&runtime
->sleep
, &wait
);
1226 set_current_state(TASK_INTERRUPTIBLE
);
1227 spin_unlock_irq(&runtime
->lock
);
1228 timeout
= schedule_timeout(30 * HZ
);
1229 remove_wait_queue(&runtime
->sleep
, &wait
);
1230 if (signal_pending(current
))
1231 return result
> 0 ? result
: -ERESTARTSYS
;
1232 if (!runtime
->avail
&& !timeout
)
1233 return result
> 0 ? result
: -EIO
;
1234 spin_lock_irq(&runtime
->lock
);
1236 spin_unlock_irq(&runtime
->lock
);
1237 count1
= snd_rawmidi_kernel_write1(substream
,
1238 (unsigned char __force
*)buf
,
1241 return result
> 0 ? result
: count1
;
1244 if ((size_t)count1
< count
&& (file
->f_flags
& O_NONBLOCK
))
1248 if (file
->f_flags
& O_SYNC
) {
1249 spin_lock_irq(&runtime
->lock
);
1250 while (runtime
->avail
!= runtime
->buffer_size
) {
1252 unsigned int last_avail
= runtime
->avail
;
1253 init_waitqueue_entry(&wait
, current
);
1254 add_wait_queue(&runtime
->sleep
, &wait
);
1255 set_current_state(TASK_INTERRUPTIBLE
);
1256 spin_unlock_irq(&runtime
->lock
);
1257 timeout
= schedule_timeout(30 * HZ
);
1258 remove_wait_queue(&runtime
->sleep
, &wait
);
1259 if (signal_pending(current
))
1260 return result
> 0 ? result
: -ERESTARTSYS
;
1261 if (runtime
->avail
== last_avail
&& !timeout
)
1262 return result
> 0 ? result
: -EIO
;
1263 spin_lock_irq(&runtime
->lock
);
1265 spin_unlock_irq(&runtime
->lock
);
1270 static unsigned int snd_rawmidi_poll(struct file
*file
, poll_table
* wait
)
1272 struct snd_rawmidi_file
*rfile
;
1273 struct snd_rawmidi_runtime
*runtime
;
1276 rfile
= file
->private_data
;
1277 if (rfile
->input
!= NULL
) {
1278 runtime
= rfile
->input
->runtime
;
1279 snd_rawmidi_input_trigger(rfile
->input
, 1);
1280 poll_wait(file
, &runtime
->sleep
, wait
);
1282 if (rfile
->output
!= NULL
) {
1283 runtime
= rfile
->output
->runtime
;
1284 poll_wait(file
, &runtime
->sleep
, wait
);
1287 if (rfile
->input
!= NULL
) {
1288 if (snd_rawmidi_ready(rfile
->input
))
1289 mask
|= POLLIN
| POLLRDNORM
;
1291 if (rfile
->output
!= NULL
) {
1292 if (snd_rawmidi_ready(rfile
->output
))
1293 mask
|= POLLOUT
| POLLWRNORM
;
1300 #ifdef CONFIG_COMPAT
1301 #include "rawmidi_compat.c"
1303 #define snd_rawmidi_ioctl_compat NULL
1310 static void snd_rawmidi_proc_info_read(struct snd_info_entry
*entry
,
1311 struct snd_info_buffer
*buffer
)
1313 struct snd_rawmidi
*rmidi
;
1314 struct snd_rawmidi_substream
*substream
;
1315 struct snd_rawmidi_runtime
*runtime
;
1316 struct list_head
*list
;
1318 rmidi
= entry
->private_data
;
1319 snd_iprintf(buffer
, "%s\n\n", rmidi
->name
);
1320 mutex_lock(&rmidi
->open_mutex
);
1321 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) {
1322 list_for_each(list
, &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
) {
1323 substream
= list_entry(list
, struct snd_rawmidi_substream
, list
);
1326 " Tx bytes : %lu\n",
1328 (unsigned long) substream
->bytes
);
1329 if (substream
->opened
) {
1330 runtime
= substream
->runtime
;
1333 " Buffer size : %lu\n"
1335 runtime
->oss
? "OSS compatible" : "native",
1336 (unsigned long) runtime
->buffer_size
,
1337 (unsigned long) runtime
->avail
);
1341 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
) {
1342 list_for_each(list
, &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
) {
1343 substream
= list_entry(list
, struct snd_rawmidi_substream
, list
);
1346 " Rx bytes : %lu\n",
1348 (unsigned long) substream
->bytes
);
1349 if (substream
->opened
) {
1350 runtime
= substream
->runtime
;
1352 " Buffer size : %lu\n"
1354 " Overruns : %lu\n",
1355 (unsigned long) runtime
->buffer_size
,
1356 (unsigned long) runtime
->avail
,
1357 (unsigned long) runtime
->xruns
);
1361 mutex_unlock(&rmidi
->open_mutex
);
1365 * Register functions
1368 static struct file_operations snd_rawmidi_f_ops
=
1370 .owner
= THIS_MODULE
,
1371 .read
= snd_rawmidi_read
,
1372 .write
= snd_rawmidi_write
,
1373 .open
= snd_rawmidi_open
,
1374 .release
= snd_rawmidi_release
,
1375 .poll
= snd_rawmidi_poll
,
1376 .unlocked_ioctl
= snd_rawmidi_ioctl
,
1377 .compat_ioctl
= snd_rawmidi_ioctl_compat
,
1380 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi
*rmidi
,
1381 struct snd_rawmidi_str
*stream
,
1385 struct snd_rawmidi_substream
*substream
;
1388 INIT_LIST_HEAD(&stream
->substreams
);
1389 for (idx
= 0; idx
< count
; idx
++) {
1390 substream
= kzalloc(sizeof(*substream
), GFP_KERNEL
);
1391 if (substream
== NULL
) {
1392 snd_printk(KERN_ERR
"rawmidi: cannot allocate substream\n");
1395 substream
->stream
= direction
;
1396 substream
->number
= idx
;
1397 substream
->rmidi
= rmidi
;
1398 substream
->pstr
= stream
;
1399 list_add_tail(&substream
->list
, &stream
->substreams
);
1400 stream
->substream_count
++;
1406 * snd_rawmidi_new - create a rawmidi instance
1407 * @card: the card instance
1408 * @id: the id string
1409 * @device: the device index
1410 * @output_count: the number of output streams
1411 * @input_count: the number of input streams
1412 * @rrawmidi: the pointer to store the new rawmidi instance
1414 * Creates a new rawmidi instance.
1415 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1417 * Returns zero if successful, or a negative error code on failure.
1419 int snd_rawmidi_new(struct snd_card
*card
, char *id
, int device
,
1420 int output_count
, int input_count
,
1421 struct snd_rawmidi
** rrawmidi
)
1423 struct snd_rawmidi
*rmidi
;
1425 static struct snd_device_ops ops
= {
1426 .dev_free
= snd_rawmidi_dev_free
,
1427 .dev_register
= snd_rawmidi_dev_register
,
1428 .dev_disconnect
= snd_rawmidi_dev_disconnect
,
1429 .dev_unregister
= snd_rawmidi_dev_unregister
1432 snd_assert(rrawmidi
!= NULL
, return -EINVAL
);
1434 snd_assert(card
!= NULL
, return -ENXIO
);
1435 rmidi
= kzalloc(sizeof(*rmidi
), GFP_KERNEL
);
1436 if (rmidi
== NULL
) {
1437 snd_printk(KERN_ERR
"rawmidi: cannot allocate\n");
1441 rmidi
->device
= device
;
1442 mutex_init(&rmidi
->open_mutex
);
1443 init_waitqueue_head(&rmidi
->open_wait
);
1445 strlcpy(rmidi
->id
, id
, sizeof(rmidi
->id
));
1446 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1447 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
],
1448 SNDRV_RAWMIDI_STREAM_INPUT
,
1449 input_count
)) < 0) {
1450 snd_rawmidi_free(rmidi
);
1453 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1454 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
],
1455 SNDRV_RAWMIDI_STREAM_OUTPUT
,
1456 output_count
)) < 0) {
1457 snd_rawmidi_free(rmidi
);
1460 if ((err
= snd_device_new(card
, SNDRV_DEV_RAWMIDI
, rmidi
, &ops
)) < 0) {
1461 snd_rawmidi_free(rmidi
);
1468 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str
*stream
)
1470 struct snd_rawmidi_substream
*substream
;
1472 while (!list_empty(&stream
->substreams
)) {
1473 substream
= list_entry(stream
->substreams
.next
, struct snd_rawmidi_substream
, list
);
1474 list_del(&substream
->list
);
1479 static int snd_rawmidi_free(struct snd_rawmidi
*rmidi
)
1481 snd_assert(rmidi
!= NULL
, return -ENXIO
);
1482 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
1483 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
1484 if (rmidi
->private_free
)
1485 rmidi
->private_free(rmidi
);
1490 static int snd_rawmidi_dev_free(struct snd_device
*device
)
1492 struct snd_rawmidi
*rmidi
= device
->device_data
;
1493 return snd_rawmidi_free(rmidi
);
1496 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1497 static void snd_rawmidi_dev_seq_free(struct snd_seq_device
*device
)
1499 struct snd_rawmidi
*rmidi
= device
->private_data
;
1500 rmidi
->seq_dev
= NULL
;
1504 static int snd_rawmidi_dev_register(struct snd_device
*device
)
1507 struct snd_info_entry
*entry
;
1509 struct snd_rawmidi
*rmidi
= device
->device_data
;
1511 if (rmidi
->device
>= SNDRV_RAWMIDI_DEVICES
)
1513 mutex_lock(®ister_mutex
);
1514 if (snd_rawmidi_search(rmidi
->card
, rmidi
->device
)) {
1515 mutex_unlock(®ister_mutex
);
1518 list_add_tail(&rmidi
->list
, &snd_rawmidi_devices
);
1519 sprintf(name
, "midiC%iD%i", rmidi
->card
->number
, rmidi
->device
);
1520 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI
,
1521 rmidi
->card
, rmidi
->device
,
1522 &snd_rawmidi_f_ops
, rmidi
, name
)) < 0) {
1523 snd_printk(KERN_ERR
"unable to register rawmidi device %i:%i\n", rmidi
->card
->number
, rmidi
->device
);
1524 list_del(&rmidi
->list
);
1525 mutex_unlock(®ister_mutex
);
1528 if (rmidi
->ops
&& rmidi
->ops
->dev_register
&&
1529 (err
= rmidi
->ops
->dev_register(rmidi
)) < 0) {
1530 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1531 list_del(&rmidi
->list
);
1532 mutex_unlock(®ister_mutex
);
1535 #ifdef CONFIG_SND_OSSEMUL
1537 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1538 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1539 rmidi
->card
, 0, &snd_rawmidi_f_ops
,
1541 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 0);
1544 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1545 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
, rmidi
->name
);
1549 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
]) {
1550 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1551 rmidi
->card
, 1, &snd_rawmidi_f_ops
,
1553 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 1);
1558 #endif /* CONFIG_SND_OSSEMUL */
1559 mutex_unlock(®ister_mutex
);
1560 sprintf(name
, "midi%d", rmidi
->device
);
1561 entry
= snd_info_create_card_entry(rmidi
->card
, name
, rmidi
->card
->proc_root
);
1563 entry
->private_data
= rmidi
;
1564 entry
->c
.text
.read_size
= 1024;
1565 entry
->c
.text
.read
= snd_rawmidi_proc_info_read
;
1566 if (snd_info_register(entry
) < 0) {
1567 snd_info_free_entry(entry
);
1571 rmidi
->proc_entry
= entry
;
1572 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1573 if (!rmidi
->ops
|| !rmidi
->ops
->dev_register
) { /* own registration mechanism */
1574 if (snd_seq_device_new(rmidi
->card
, rmidi
->device
, SNDRV_SEQ_DEV_ID_MIDISYNTH
, 0, &rmidi
->seq_dev
) >= 0) {
1575 rmidi
->seq_dev
->private_data
= rmidi
;
1576 rmidi
->seq_dev
->private_free
= snd_rawmidi_dev_seq_free
;
1577 sprintf(rmidi
->seq_dev
->name
, "MIDI %d-%d", rmidi
->card
->number
, rmidi
->device
);
1578 snd_device_register(rmidi
->card
, rmidi
->seq_dev
);
1585 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
)
1587 struct snd_rawmidi
*rmidi
= device
->device_data
;
1589 mutex_lock(®ister_mutex
);
1590 list_del_init(&rmidi
->list
);
1591 mutex_unlock(®ister_mutex
);
1595 static int snd_rawmidi_dev_unregister(struct snd_device
*device
)
1597 struct snd_rawmidi
*rmidi
= device
->device_data
;
1599 snd_assert(rmidi
!= NULL
, return -ENXIO
);
1600 mutex_lock(®ister_mutex
);
1601 list_del(&rmidi
->list
);
1602 if (rmidi
->proc_entry
) {
1603 snd_info_unregister(rmidi
->proc_entry
);
1604 rmidi
->proc_entry
= NULL
;
1606 #ifdef CONFIG_SND_OSSEMUL
1607 if (rmidi
->ossreg
) {
1608 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1609 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 0);
1610 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1611 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
);
1614 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
])
1615 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 1);
1618 #endif /* CONFIG_SND_OSSEMUL */
1619 if (rmidi
->ops
&& rmidi
->ops
->dev_unregister
)
1620 rmidi
->ops
->dev_unregister(rmidi
);
1621 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1622 mutex_unlock(®ister_mutex
);
1623 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1624 if (rmidi
->seq_dev
) {
1625 snd_device_free(rmidi
->card
, rmidi
->seq_dev
);
1626 rmidi
->seq_dev
= NULL
;
1629 return snd_rawmidi_free(rmidi
);
1633 * snd_rawmidi_set_ops - set the rawmidi operators
1634 * @rmidi: the rawmidi instance
1635 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1636 * @ops: the operator table
1638 * Sets the rawmidi operators for the given stream direction.
1640 void snd_rawmidi_set_ops(struct snd_rawmidi
*rmidi
, int stream
,
1641 struct snd_rawmidi_ops
*ops
)
1643 struct list_head
*list
;
1644 struct snd_rawmidi_substream
*substream
;
1646 list_for_each(list
, &rmidi
->streams
[stream
].substreams
) {
1647 substream
= list_entry(list
, struct snd_rawmidi_substream
, list
);
1648 substream
->ops
= ops
;
1656 static int __init
alsa_rawmidi_init(void)
1659 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl
);
1660 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl
);
1661 #ifdef CONFIG_SND_OSSEMUL
1663 /* check device map table */
1664 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1665 if (midi_map
[i
] < 0 || midi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1666 snd_printk(KERN_ERR
"invalid midi_map[%d] = %d\n", i
, midi_map
[i
]);
1669 if (amidi_map
[i
] < 0 || amidi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1670 snd_printk(KERN_ERR
"invalid amidi_map[%d] = %d\n", i
, amidi_map
[i
]);
1675 #endif /* CONFIG_SND_OSSEMUL */
1679 static void __exit
alsa_rawmidi_exit(void)
1681 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl
);
1682 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl
);
1685 module_init(alsa_rawmidi_init
)
1686 module_exit(alsa_rawmidi_exit
)
1688 EXPORT_SYMBOL(snd_rawmidi_output_params
);
1689 EXPORT_SYMBOL(snd_rawmidi_input_params
);
1690 EXPORT_SYMBOL(snd_rawmidi_drop_output
);
1691 EXPORT_SYMBOL(snd_rawmidi_drain_output
);
1692 EXPORT_SYMBOL(snd_rawmidi_drain_input
);
1693 EXPORT_SYMBOL(snd_rawmidi_receive
);
1694 EXPORT_SYMBOL(snd_rawmidi_transmit_empty
);
1695 EXPORT_SYMBOL(snd_rawmidi_transmit_peek
);
1696 EXPORT_SYMBOL(snd_rawmidi_transmit_ack
);
1697 EXPORT_SYMBOL(snd_rawmidi_transmit
);
1698 EXPORT_SYMBOL(snd_rawmidi_new
);
1699 EXPORT_SYMBOL(snd_rawmidi_set_ops
);
1700 EXPORT_SYMBOL(snd_rawmidi_info_select
);
1701 EXPORT_SYMBOL(snd_rawmidi_kernel_open
);
1702 EXPORT_SYMBOL(snd_rawmidi_kernel_release
);
1703 EXPORT_SYMBOL(snd_rawmidi_kernel_read
);
1704 EXPORT_SYMBOL(snd_rawmidi_kernel_write
);