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/moduleparam.h>
32 #include <linux/delay.h>
33 #include <linux/wait.h>
34 #include <sound/rawmidi.h>
35 #include <sound/info.h>
36 #include <sound/control.h>
37 #include <sound/minors.h>
38 #include <sound/initval.h>
40 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
41 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
42 MODULE_LICENSE("GPL");
44 #ifdef CONFIG_SND_OSSEMUL
45 static int midi_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 0};
46 static int amidi_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
47 module_param_array(midi_map
, int, NULL
, 0444);
48 MODULE_PARM_DESC(midi_map
, "Raw MIDI device number assigned to 1st OSS device.");
49 module_param_array(amidi_map
, int, NULL
, 0444);
50 MODULE_PARM_DESC(amidi_map
, "Raw MIDI device number assigned to 2nd OSS device.");
51 #endif /* CONFIG_SND_OSSEMUL */
53 static int snd_rawmidi_free(struct snd_rawmidi
*rawmidi
);
54 static int snd_rawmidi_dev_free(struct snd_device
*device
);
55 static int snd_rawmidi_dev_register(struct snd_device
*device
);
56 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
);
57 static int snd_rawmidi_dev_unregister(struct snd_device
*device
);
59 static struct snd_rawmidi
*snd_rawmidi_devices
[SNDRV_CARDS
* SNDRV_RAWMIDI_DEVICES
];
61 static DECLARE_MUTEX(register_mutex
);
63 static inline unsigned short snd_rawmidi_file_flags(struct file
*file
)
65 switch (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) {
67 return SNDRV_RAWMIDI_LFLG_OUTPUT
;
69 return SNDRV_RAWMIDI_LFLG_INPUT
;
71 return SNDRV_RAWMIDI_LFLG_OPEN
;
75 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream
*substream
)
77 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
78 return runtime
->avail
>= runtime
->avail_min
;
81 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream
*substream
,
84 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
85 return runtime
->avail
>= runtime
->avail_min
&&
86 (!substream
->append
|| runtime
->avail
>= count
);
89 static void snd_rawmidi_input_event_tasklet(unsigned long data
)
91 struct snd_rawmidi_substream
*substream
= (struct snd_rawmidi_substream
*)data
;
92 substream
->runtime
->event(substream
);
95 static void snd_rawmidi_output_trigger_tasklet(unsigned long data
)
97 struct snd_rawmidi_substream
*substream
= (struct snd_rawmidi_substream
*)data
;
98 substream
->ops
->trigger(substream
, 1);
101 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream
*substream
)
103 struct snd_rawmidi_runtime
*runtime
;
105 if ((runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
)) == NULL
)
107 spin_lock_init(&runtime
->lock
);
108 init_waitqueue_head(&runtime
->sleep
);
109 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
110 tasklet_init(&runtime
->tasklet
,
111 snd_rawmidi_input_event_tasklet
,
112 (unsigned long)substream
);
114 tasklet_init(&runtime
->tasklet
,
115 snd_rawmidi_output_trigger_tasklet
,
116 (unsigned long)substream
);
117 runtime
->event
= NULL
;
118 runtime
->buffer_size
= PAGE_SIZE
;
119 runtime
->avail_min
= 1;
120 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
123 runtime
->avail
= runtime
->buffer_size
;
124 if ((runtime
->buffer
= kmalloc(runtime
->buffer_size
, GFP_KERNEL
)) == NULL
) {
128 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
129 substream
->runtime
= runtime
;
133 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream
*substream
)
135 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
137 kfree(runtime
->buffer
);
139 substream
->runtime
= NULL
;
143 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream
*substream
,int up
)
146 tasklet_hi_schedule(&substream
->runtime
->tasklet
);
148 tasklet_kill(&substream
->runtime
->tasklet
);
149 substream
->ops
->trigger(substream
, 0);
153 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
155 substream
->ops
->trigger(substream
, up
);
156 if (!up
&& substream
->runtime
->event
)
157 tasklet_kill(&substream
->runtime
->tasklet
);
160 int snd_rawmidi_drop_output(struct snd_rawmidi_substream
*substream
)
163 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
165 snd_rawmidi_output_trigger(substream
, 0);
167 spin_lock_irqsave(&runtime
->lock
, flags
);
168 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
169 runtime
->avail
= runtime
->buffer_size
;
170 spin_unlock_irqrestore(&runtime
->lock
, flags
);
174 int snd_rawmidi_drain_output(struct snd_rawmidi_substream
*substream
)
178 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
182 timeout
= wait_event_interruptible_timeout(runtime
->sleep
,
183 (runtime
->avail
>= runtime
->buffer_size
),
185 if (signal_pending(current
))
187 if (runtime
->avail
< runtime
->buffer_size
&& !timeout
) {
188 snd_printk(KERN_WARNING
"rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime
->avail
, (long)runtime
->buffer_size
);
192 if (err
!= -ERESTARTSYS
) {
193 /* we need wait a while to make sure that Tx FIFOs are empty */
194 if (substream
->ops
->drain
)
195 substream
->ops
->drain(substream
);
198 snd_rawmidi_drop_output(substream
);
203 int snd_rawmidi_drain_input(struct snd_rawmidi_substream
*substream
)
206 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
208 snd_rawmidi_input_trigger(substream
, 0);
210 spin_lock_irqsave(&runtime
->lock
, flags
);
211 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
213 spin_unlock_irqrestore(&runtime
->lock
, flags
);
217 int snd_rawmidi_kernel_open(int cardnum
, int device
, int subdevice
,
218 int mode
, struct snd_rawmidi_file
* rfile
)
220 struct snd_rawmidi
*rmidi
;
221 struct list_head
*list1
, *list2
;
222 struct snd_rawmidi_substream
*sinput
= NULL
, *soutput
= NULL
;
223 struct snd_rawmidi_runtime
*input
= NULL
, *output
= NULL
;
227 rfile
->input
= rfile
->output
= NULL
;
228 rmidi
= snd_rawmidi_devices
[(cardnum
* SNDRV_RAWMIDI_DEVICES
) + device
];
233 if (!try_module_get(rmidi
->card
->module
)) {
237 if (!(mode
& SNDRV_RAWMIDI_LFLG_NOOPENLOCK
))
238 down(&rmidi
->open_mutex
);
239 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
240 if (!(rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
)) {
244 if (subdevice
>= 0 && (unsigned int)subdevice
>= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_count
) {
248 if (rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_opened
>=
249 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_count
) {
254 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
255 if (!(rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
)) {
259 if (subdevice
>= 0 && (unsigned int)subdevice
>= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_count
) {
263 if (rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_opened
>=
264 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_count
) {
269 list1
= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
.next
;
271 if (list1
== &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
) {
273 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
279 sinput
= list_entry(list1
, struct snd_rawmidi_substream
, list
);
280 if ((mode
& SNDRV_RAWMIDI_LFLG_INPUT
) && sinput
->opened
)
282 if (subdevice
< 0 || (subdevice
>= 0 && subdevice
== sinput
->number
))
287 list2
= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
.next
;
289 if (list2
== &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
) {
291 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
297 soutput
= list_entry(list2
, struct snd_rawmidi_substream
, list
);
298 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
299 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
) {
300 if (soutput
->opened
&& !soutput
->append
)
307 if (subdevice
< 0 || (subdevice
>= 0 && subdevice
== soutput
->number
))
312 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
313 if ((err
= snd_rawmidi_runtime_create(sinput
)) < 0)
315 input
= sinput
->runtime
;
316 if ((err
= sinput
->ops
->open(sinput
)) < 0)
319 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_opened
++;
323 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
326 if ((err
= snd_rawmidi_runtime_create(soutput
)) < 0) {
327 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
)
328 sinput
->ops
->close(sinput
);
331 output
= soutput
->runtime
;
332 if ((err
= soutput
->ops
->open(soutput
)) < 0) {
333 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
)
334 sinput
->ops
->close(sinput
);
339 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
)
341 if (soutput
->use_count
++ == 0)
342 soutput
->active_sensing
= 1;
343 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_opened
++;
347 if (!(mode
& SNDRV_RAWMIDI_LFLG_NOOPENLOCK
))
348 up(&rmidi
->open_mutex
);
350 rfile
->rmidi
= rmidi
;
351 rfile
->input
= sinput
;
352 rfile
->output
= soutput
;
358 snd_rawmidi_runtime_free(sinput
);
360 snd_rawmidi_runtime_free(soutput
);
361 module_put(rmidi
->card
->module
);
362 if (!(mode
& SNDRV_RAWMIDI_LFLG_NOOPENLOCK
))
363 up(&rmidi
->open_mutex
);
368 static int snd_rawmidi_open(struct inode
*inode
, struct file
*file
)
370 int maj
= imajor(inode
);
372 struct snd_card
*card
;
373 int device
, subdevice
;
374 unsigned short fflags
;
376 struct snd_rawmidi
*rmidi
;
377 struct snd_rawmidi_file
*rawmidi_file
;
379 struct list_head
*list
;
380 struct snd_ctl_file
*kctl
;
382 if (maj
== snd_major
) {
383 cardnum
= SNDRV_MINOR_CARD(iminor(inode
));
384 cardnum
%= SNDRV_CARDS
;
385 device
= SNDRV_MINOR_DEVICE(iminor(inode
)) - SNDRV_MINOR_RAWMIDI
;
386 device
%= SNDRV_MINOR_RAWMIDIS
;
387 #ifdef CONFIG_SND_OSSEMUL
388 } else if (maj
== SOUND_MAJOR
) {
389 cardnum
= SNDRV_MINOR_OSS_CARD(iminor(inode
));
390 cardnum
%= SNDRV_CARDS
;
391 device
= SNDRV_MINOR_OSS_DEVICE(iminor(inode
)) == SNDRV_MINOR_OSS_MIDI
?
392 midi_map
[cardnum
] : amidi_map
[cardnum
];
397 rmidi
= snd_rawmidi_devices
[(cardnum
* SNDRV_RAWMIDI_DEVICES
) + device
];
400 #ifdef CONFIG_SND_OSSEMUL
401 if (maj
== SOUND_MAJOR
&& !rmidi
->ossreg
)
404 if ((file
->f_flags
& O_APPEND
) && !(file
->f_flags
& O_NONBLOCK
))
405 return -EINVAL
; /* invalid combination */
407 err
= snd_card_file_add(card
, file
);
410 fflags
= snd_rawmidi_file_flags(file
);
411 if ((file
->f_flags
& O_APPEND
) || maj
== SOUND_MAJOR
) /* OSS emul? */
412 fflags
|= SNDRV_RAWMIDI_LFLG_APPEND
;
413 fflags
|= SNDRV_RAWMIDI_LFLG_NOOPENLOCK
;
414 rawmidi_file
= kmalloc(sizeof(*rawmidi_file
), GFP_KERNEL
);
415 if (rawmidi_file
== NULL
) {
416 snd_card_file_remove(card
, file
);
419 init_waitqueue_entry(&wait
, current
);
420 add_wait_queue(&rmidi
->open_wait
, &wait
);
421 down(&rmidi
->open_mutex
);
424 down_read(&card
->controls_rwsem
);
425 list_for_each(list
, &card
->ctl_files
) {
426 kctl
= snd_ctl_file(list
);
427 if (kctl
->pid
== current
->pid
) {
428 subdevice
= kctl
->prefer_rawmidi_subdevice
;
432 up_read(&card
->controls_rwsem
);
433 err
= snd_rawmidi_kernel_open(cardnum
, device
, subdevice
, fflags
, rawmidi_file
);
436 if (err
== -EAGAIN
) {
437 if (file
->f_flags
& O_NONBLOCK
) {
443 set_current_state(TASK_INTERRUPTIBLE
);
444 up(&rmidi
->open_mutex
);
446 down(&rmidi
->open_mutex
);
447 if (signal_pending(current
)) {
452 #ifdef CONFIG_SND_OSSEMUL
453 if (rawmidi_file
->input
&& rawmidi_file
->input
->runtime
)
454 rawmidi_file
->input
->runtime
->oss
= (maj
== SOUND_MAJOR
);
455 if (rawmidi_file
->output
&& rawmidi_file
->output
->runtime
)
456 rawmidi_file
->output
->runtime
->oss
= (maj
== SOUND_MAJOR
);
458 remove_wait_queue(&rmidi
->open_wait
, &wait
);
460 file
->private_data
= rawmidi_file
;
462 snd_card_file_remove(card
, file
);
465 up(&rmidi
->open_mutex
);
469 int snd_rawmidi_kernel_release(struct snd_rawmidi_file
* rfile
)
471 struct snd_rawmidi
*rmidi
;
472 struct snd_rawmidi_substream
*substream
;
473 struct snd_rawmidi_runtime
*runtime
;
475 snd_assert(rfile
!= NULL
, return -ENXIO
);
476 snd_assert(rfile
->input
!= NULL
|| rfile
->output
!= NULL
, return -ENXIO
);
477 rmidi
= rfile
->rmidi
;
478 down(&rmidi
->open_mutex
);
479 if (rfile
->input
!= NULL
) {
480 substream
= rfile
->input
;
482 runtime
= substream
->runtime
;
483 snd_rawmidi_input_trigger(substream
, 0);
484 substream
->ops
->close(substream
);
485 if (runtime
->private_free
!= NULL
)
486 runtime
->private_free(substream
);
487 snd_rawmidi_runtime_free(substream
);
488 substream
->opened
= 0;
489 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_opened
--;
491 if (rfile
->output
!= NULL
) {
492 substream
= rfile
->output
;
493 rfile
->output
= NULL
;
494 if (--substream
->use_count
== 0) {
495 runtime
= substream
->runtime
;
496 if (substream
->active_sensing
) {
497 unsigned char buf
= 0xfe;
498 /* sending single active sensing message to shut the device up */
499 snd_rawmidi_kernel_write(substream
, &buf
, 1);
501 if (snd_rawmidi_drain_output(substream
) == -ERESTARTSYS
)
502 snd_rawmidi_output_trigger(substream
, 0);
503 substream
->ops
->close(substream
);
504 if (runtime
->private_free
!= NULL
)
505 runtime
->private_free(substream
);
506 snd_rawmidi_runtime_free(substream
);
507 substream
->opened
= 0;
508 substream
->append
= 0;
510 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_opened
--;
512 up(&rmidi
->open_mutex
);
513 module_put(rmidi
->card
->module
);
517 static int snd_rawmidi_release(struct inode
*inode
, struct file
*file
)
519 struct snd_rawmidi_file
*rfile
;
520 struct snd_rawmidi
*rmidi
;
523 rfile
= file
->private_data
;
524 err
= snd_rawmidi_kernel_release(rfile
);
525 rmidi
= rfile
->rmidi
;
526 wake_up(&rmidi
->open_wait
);
528 snd_card_file_remove(rmidi
->card
, file
);
532 int snd_rawmidi_info(struct snd_rawmidi_substream
*substream
,
533 struct snd_rawmidi_info
*info
)
535 struct snd_rawmidi
*rmidi
;
537 if (substream
== NULL
)
539 rmidi
= substream
->rmidi
;
540 memset(info
, 0, sizeof(*info
));
541 info
->card
= rmidi
->card
->number
;
542 info
->device
= rmidi
->device
;
543 info
->subdevice
= substream
->number
;
544 info
->stream
= substream
->stream
;
545 info
->flags
= rmidi
->info_flags
;
546 strcpy(info
->id
, rmidi
->id
);
547 strcpy(info
->name
, rmidi
->name
);
548 strcpy(info
->subname
, substream
->name
);
549 info
->subdevices_count
= substream
->pstr
->substream_count
;
550 info
->subdevices_avail
= (substream
->pstr
->substream_count
-
551 substream
->pstr
->substream_opened
);
555 static int snd_rawmidi_info_user(struct snd_rawmidi_substream
*substream
,
556 struct snd_rawmidi_info __user
* _info
)
558 struct snd_rawmidi_info info
;
560 if ((err
= snd_rawmidi_info(substream
, &info
)) < 0)
562 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
567 int snd_rawmidi_info_select(struct snd_card
*card
, struct snd_rawmidi_info
*info
)
569 struct snd_rawmidi
*rmidi
;
570 struct snd_rawmidi_str
*pstr
;
571 struct snd_rawmidi_substream
*substream
;
572 struct list_head
*list
;
573 if (info
->device
>= SNDRV_RAWMIDI_DEVICES
)
575 rmidi
= snd_rawmidi_devices
[card
->number
* SNDRV_RAWMIDI_DEVICES
+ info
->device
];
576 if (info
->stream
< 0 || info
->stream
> 1)
578 pstr
= &rmidi
->streams
[info
->stream
];
579 if (pstr
->substream_count
== 0)
581 if (info
->subdevice
>= pstr
->substream_count
)
583 list_for_each(list
, &pstr
->substreams
) {
584 substream
= list_entry(list
, struct snd_rawmidi_substream
, list
);
585 if ((unsigned int)substream
->number
== info
->subdevice
)
586 return snd_rawmidi_info(substream
, info
);
591 static int snd_rawmidi_info_select_user(struct snd_card
*card
,
592 struct snd_rawmidi_info __user
*_info
)
595 struct snd_rawmidi_info info
;
596 if (get_user(info
.device
, &_info
->device
))
598 if (get_user(info
.stream
, &_info
->stream
))
600 if (get_user(info
.subdevice
, &_info
->subdevice
))
602 if ((err
= snd_rawmidi_info_select(card
, &info
)) < 0)
604 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
609 int snd_rawmidi_output_params(struct snd_rawmidi_substream
*substream
,
610 struct snd_rawmidi_params
* params
)
613 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
615 if (substream
->append
&& substream
->use_count
> 1)
617 snd_rawmidi_drain_output(substream
);
618 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
621 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
624 if (params
->buffer_size
!= runtime
->buffer_size
) {
625 if ((newbuf
= (char *) kmalloc(params
->buffer_size
, GFP_KERNEL
)) == NULL
)
627 kfree(runtime
->buffer
);
628 runtime
->buffer
= newbuf
;
629 runtime
->buffer_size
= params
->buffer_size
;
631 runtime
->avail_min
= params
->avail_min
;
632 substream
->active_sensing
= !params
->no_active_sensing
;
636 int snd_rawmidi_input_params(struct snd_rawmidi_substream
*substream
,
637 struct snd_rawmidi_params
* params
)
640 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
642 snd_rawmidi_drain_input(substream
);
643 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
646 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
649 if (params
->buffer_size
!= runtime
->buffer_size
) {
650 if ((newbuf
= (char *) kmalloc(params
->buffer_size
, GFP_KERNEL
)) == NULL
)
652 kfree(runtime
->buffer
);
653 runtime
->buffer
= newbuf
;
654 runtime
->buffer_size
= params
->buffer_size
;
656 runtime
->avail_min
= params
->avail_min
;
660 static int snd_rawmidi_output_status(struct snd_rawmidi_substream
*substream
,
661 struct snd_rawmidi_status
* status
)
663 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
665 memset(status
, 0, sizeof(*status
));
666 status
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
667 spin_lock_irq(&runtime
->lock
);
668 status
->avail
= runtime
->avail
;
669 spin_unlock_irq(&runtime
->lock
);
673 static int snd_rawmidi_input_status(struct snd_rawmidi_substream
*substream
,
674 struct snd_rawmidi_status
* status
)
676 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
678 memset(status
, 0, sizeof(*status
));
679 status
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
680 spin_lock_irq(&runtime
->lock
);
681 status
->avail
= runtime
->avail
;
682 status
->xruns
= runtime
->xruns
;
684 spin_unlock_irq(&runtime
->lock
);
688 static long snd_rawmidi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
690 struct snd_rawmidi_file
*rfile
;
691 void __user
*argp
= (void __user
*)arg
;
693 rfile
= file
->private_data
;
694 if (((cmd
>> 8) & 0xff) != 'W')
697 case SNDRV_RAWMIDI_IOCTL_PVERSION
:
698 return put_user(SNDRV_RAWMIDI_VERSION
, (int __user
*)argp
) ? -EFAULT
: 0;
699 case SNDRV_RAWMIDI_IOCTL_INFO
:
702 struct snd_rawmidi_info __user
*info
= argp
;
703 if (get_user(stream
, &info
->stream
))
706 case SNDRV_RAWMIDI_STREAM_INPUT
:
707 return snd_rawmidi_info_user(rfile
->input
, info
);
708 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
709 return snd_rawmidi_info_user(rfile
->output
, info
);
714 case SNDRV_RAWMIDI_IOCTL_PARAMS
:
716 struct snd_rawmidi_params params
;
717 if (copy_from_user(¶ms
, argp
, sizeof(struct snd_rawmidi_params
)))
719 switch (params
.stream
) {
720 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
721 if (rfile
->output
== NULL
)
723 return snd_rawmidi_output_params(rfile
->output
, ¶ms
);
724 case SNDRV_RAWMIDI_STREAM_INPUT
:
725 if (rfile
->input
== NULL
)
727 return snd_rawmidi_input_params(rfile
->input
, ¶ms
);
732 case SNDRV_RAWMIDI_IOCTL_STATUS
:
735 struct snd_rawmidi_status status
;
736 if (copy_from_user(&status
, argp
, sizeof(struct snd_rawmidi_status
)))
738 switch (status
.stream
) {
739 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
740 if (rfile
->output
== NULL
)
742 err
= snd_rawmidi_output_status(rfile
->output
, &status
);
744 case SNDRV_RAWMIDI_STREAM_INPUT
:
745 if (rfile
->input
== NULL
)
747 err
= snd_rawmidi_input_status(rfile
->input
, &status
);
754 if (copy_to_user(argp
, &status
, sizeof(struct snd_rawmidi_status
)))
758 case SNDRV_RAWMIDI_IOCTL_DROP
:
761 if (get_user(val
, (int __user
*) argp
))
764 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
765 if (rfile
->output
== NULL
)
767 return snd_rawmidi_drop_output(rfile
->output
);
772 case SNDRV_RAWMIDI_IOCTL_DRAIN
:
775 if (get_user(val
, (int __user
*) argp
))
778 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
779 if (rfile
->output
== NULL
)
781 return snd_rawmidi_drain_output(rfile
->output
);
782 case SNDRV_RAWMIDI_STREAM_INPUT
:
783 if (rfile
->input
== NULL
)
785 return snd_rawmidi_drain_input(rfile
->input
);
790 #ifdef CONFIG_SND_DEBUG
792 snd_printk(KERN_WARNING
"rawmidi: unknown command = 0x%x\n", cmd
);
798 static int snd_rawmidi_control_ioctl(struct snd_card
*card
,
799 struct snd_ctl_file
*control
,
803 void __user
*argp
= (void __user
*)arg
;
806 tmp
= card
->number
* SNDRV_RAWMIDI_DEVICES
;
808 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE
:
812 if (get_user(device
, (int __user
*)argp
))
814 device
= device
< 0 ? 0 : device
+ 1;
815 while (device
< SNDRV_RAWMIDI_DEVICES
) {
816 if (snd_rawmidi_devices
[tmp
+ device
])
820 if (device
== SNDRV_RAWMIDI_DEVICES
)
822 if (put_user(device
, (int __user
*)argp
))
826 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE
:
830 if (get_user(val
, (int __user
*)argp
))
832 control
->prefer_rawmidi_subdevice
= val
;
835 case SNDRV_CTL_IOCTL_RAWMIDI_INFO
:
836 return snd_rawmidi_info_select_user(card
, argp
);
842 * snd_rawmidi_receive - receive the input data from the device
843 * @substream: the rawmidi substream
844 * @buffer: the buffer pointer
845 * @count: the data size to read
847 * Reads the data from the internal buffer.
849 * Returns the size of read data, or a negative error code on failure.
851 int snd_rawmidi_receive(struct snd_rawmidi_substream
*substream
,
852 const unsigned char *buffer
, int count
)
855 int result
= 0, count1
;
856 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
858 if (runtime
->buffer
== NULL
) {
859 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
862 spin_lock_irqsave(&runtime
->lock
, flags
);
863 if (count
== 1) { /* special case, faster code */
865 if (runtime
->avail
< runtime
->buffer_size
) {
866 runtime
->buffer
[runtime
->hw_ptr
++] = buffer
[0];
867 runtime
->hw_ptr
%= runtime
->buffer_size
;
874 substream
->bytes
+= count
;
875 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
878 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
879 count1
= runtime
->buffer_size
- runtime
->avail
;
880 memcpy(runtime
->buffer
+ runtime
->hw_ptr
, buffer
, count1
);
881 runtime
->hw_ptr
+= count1
;
882 runtime
->hw_ptr
%= runtime
->buffer_size
;
883 runtime
->avail
+= count1
;
889 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
)) {
890 count1
= runtime
->buffer_size
- runtime
->avail
;
891 runtime
->xruns
+= count
- count1
;
894 memcpy(runtime
->buffer
, buffer
, count1
);
895 runtime
->hw_ptr
= count1
;
896 runtime
->avail
+= count1
;
903 tasklet_hi_schedule(&runtime
->tasklet
);
904 else if (snd_rawmidi_ready(substream
))
905 wake_up(&runtime
->sleep
);
907 spin_unlock_irqrestore(&runtime
->lock
, flags
);
911 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream
*substream
,
912 unsigned char *buf
, long count
, int kernel
)
915 long result
= 0, count1
;
916 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
918 while (count
> 0 && runtime
->avail
) {
919 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
922 spin_lock_irqsave(&runtime
->lock
, flags
);
923 if (count1
> (int)runtime
->avail
)
924 count1
= runtime
->avail
;
926 memcpy(buf
+ result
, runtime
->buffer
+ runtime
->appl_ptr
, count1
);
928 spin_unlock_irqrestore(&runtime
->lock
, flags
);
929 if (copy_to_user((char __user
*)buf
+ result
,
930 runtime
->buffer
+ runtime
->appl_ptr
, count1
)) {
931 return result
> 0 ? result
: -EFAULT
;
933 spin_lock_irqsave(&runtime
->lock
, flags
);
935 runtime
->appl_ptr
+= count1
;
936 runtime
->appl_ptr
%= runtime
->buffer_size
;
937 runtime
->avail
-= count1
;
938 spin_unlock_irqrestore(&runtime
->lock
, flags
);
945 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream
*substream
,
946 unsigned char *buf
, long count
)
948 snd_rawmidi_input_trigger(substream
, 1);
949 return snd_rawmidi_kernel_read1(substream
, buf
, count
, 1);
952 static ssize_t
snd_rawmidi_read(struct file
*file
, char __user
*buf
, size_t count
,
957 struct snd_rawmidi_file
*rfile
;
958 struct snd_rawmidi_substream
*substream
;
959 struct snd_rawmidi_runtime
*runtime
;
961 rfile
= file
->private_data
;
962 substream
= rfile
->input
;
963 if (substream
== NULL
)
965 runtime
= substream
->runtime
;
966 snd_rawmidi_input_trigger(substream
, 1);
969 spin_lock_irq(&runtime
->lock
);
970 while (!snd_rawmidi_ready(substream
)) {
972 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
973 spin_unlock_irq(&runtime
->lock
);
974 return result
> 0 ? result
: -EAGAIN
;
976 init_waitqueue_entry(&wait
, current
);
977 add_wait_queue(&runtime
->sleep
, &wait
);
978 set_current_state(TASK_INTERRUPTIBLE
);
979 spin_unlock_irq(&runtime
->lock
);
981 remove_wait_queue(&runtime
->sleep
, &wait
);
982 if (signal_pending(current
))
983 return result
> 0 ? result
: -ERESTARTSYS
;
985 return result
> 0 ? result
: -EIO
;
986 spin_lock_irq(&runtime
->lock
);
988 spin_unlock_irq(&runtime
->lock
);
989 count1
= snd_rawmidi_kernel_read1(substream
,
990 (unsigned char __force
*)buf
,
993 return result
> 0 ? result
: count1
;
1002 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1003 * @substream: the rawmidi substream
1005 * Returns 1 if the internal output buffer is empty, 0 if not.
1007 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream
*substream
)
1009 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1011 unsigned long flags
;
1013 if (runtime
->buffer
== NULL
) {
1014 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1017 spin_lock_irqsave(&runtime
->lock
, flags
);
1018 result
= runtime
->avail
>= runtime
->buffer_size
;
1019 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1024 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1025 * @substream: the rawmidi substream
1026 * @buffer: the buffer pointer
1027 * @count: data size to transfer
1029 * Copies data from the internal output buffer to the given buffer.
1031 * Call this in the interrupt handler when the midi output is ready,
1032 * and call snd_rawmidi_transmit_ack() after the transmission is
1035 * Returns the size of copied data, or a negative error code on failure.
1037 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1038 unsigned char *buffer
, int count
)
1040 unsigned long flags
;
1042 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1044 if (runtime
->buffer
== NULL
) {
1045 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1049 spin_lock_irqsave(&runtime
->lock
, flags
);
1050 if (runtime
->avail
>= runtime
->buffer_size
) {
1051 /* warning: lowlevel layer MUST trigger down the hardware */
1054 if (count
== 1) { /* special case, faster code */
1055 *buffer
= runtime
->buffer
[runtime
->hw_ptr
];
1058 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
1061 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
1062 count1
= runtime
->buffer_size
- runtime
->avail
;
1063 memcpy(buffer
, runtime
->buffer
+ runtime
->hw_ptr
, count1
);
1067 if (count
> (int)(runtime
->buffer_size
- runtime
->avail
- count1
))
1068 count
= runtime
->buffer_size
- runtime
->avail
- count1
;
1069 memcpy(buffer
+ count1
, runtime
->buffer
, count
);
1074 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1079 * snd_rawmidi_transmit_ack - acknowledge the transmission
1080 * @substream: the rawmidi substream
1081 * @count: the tranferred count
1083 * Advances the hardware pointer for the internal output buffer with
1084 * the given size and updates the condition.
1085 * Call after the transmission is finished.
1087 * Returns the advanced size if successful, or a negative error code on failure.
1089 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1091 unsigned long flags
;
1092 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1094 if (runtime
->buffer
== NULL
) {
1095 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1098 spin_lock_irqsave(&runtime
->lock
, flags
);
1099 snd_assert(runtime
->avail
+ count
<= runtime
->buffer_size
, );
1100 runtime
->hw_ptr
+= count
;
1101 runtime
->hw_ptr
%= runtime
->buffer_size
;
1102 runtime
->avail
+= count
;
1103 substream
->bytes
+= count
;
1105 if (runtime
->drain
|| snd_rawmidi_ready(substream
))
1106 wake_up(&runtime
->sleep
);
1108 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1113 * snd_rawmidi_transmit - copy from the buffer to the device
1114 * @substream: the rawmidi substream
1115 * @buffer: the buffer pointer
1116 * @count: the data size to transfer
1118 * Copies data from the buffer to the device and advances the pointer.
1120 * Returns the copied size if successful, or a negative error code on failure.
1122 int snd_rawmidi_transmit(struct snd_rawmidi_substream
*substream
,
1123 unsigned char *buffer
, int count
)
1125 count
= snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1128 return snd_rawmidi_transmit_ack(substream
, count
);
1131 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream
*substream
,
1132 const unsigned char *buf
, long count
, int kernel
)
1134 unsigned long flags
;
1135 long count1
, result
;
1136 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1138 snd_assert(buf
!= NULL
, return -EINVAL
);
1139 snd_assert(runtime
->buffer
!= NULL
, return -EINVAL
);
1142 spin_lock_irqsave(&runtime
->lock
, flags
);
1143 if (substream
->append
) {
1144 if ((long)runtime
->avail
< count
) {
1145 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1149 while (count
> 0 && runtime
->avail
> 0) {
1150 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1153 if (count1
> (long)runtime
->avail
)
1154 count1
= runtime
->avail
;
1156 memcpy(runtime
->buffer
+ runtime
->appl_ptr
, buf
, count1
);
1158 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1159 if (copy_from_user(runtime
->buffer
+ runtime
->appl_ptr
,
1160 (char __user
*)buf
, count1
)) {
1161 spin_lock_irqsave(&runtime
->lock
, flags
);
1162 result
= result
> 0 ? result
: -EFAULT
;
1165 spin_lock_irqsave(&runtime
->lock
, flags
);
1167 runtime
->appl_ptr
+= count1
;
1168 runtime
->appl_ptr
%= runtime
->buffer_size
;
1169 runtime
->avail
-= count1
;
1175 count1
= runtime
->avail
< runtime
->buffer_size
;
1176 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1178 snd_rawmidi_output_trigger(substream
, 1);
1182 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream
*substream
,
1183 const unsigned char *buf
, long count
)
1185 return snd_rawmidi_kernel_write1(substream
, buf
, count
, 1);
1188 static ssize_t
snd_rawmidi_write(struct file
*file
, const char __user
*buf
,
1189 size_t count
, loff_t
*offset
)
1191 long result
, timeout
;
1193 struct snd_rawmidi_file
*rfile
;
1194 struct snd_rawmidi_runtime
*runtime
;
1195 struct snd_rawmidi_substream
*substream
;
1197 rfile
= file
->private_data
;
1198 substream
= rfile
->output
;
1199 runtime
= substream
->runtime
;
1200 /* we cannot put an atomic message to our buffer */
1201 if (substream
->append
&& count
> runtime
->buffer_size
)
1205 spin_lock_irq(&runtime
->lock
);
1206 while (!snd_rawmidi_ready_append(substream
, count
)) {
1208 if (file
->f_flags
& O_NONBLOCK
) {
1209 spin_unlock_irq(&runtime
->lock
);
1210 return result
> 0 ? result
: -EAGAIN
;
1212 init_waitqueue_entry(&wait
, current
);
1213 add_wait_queue(&runtime
->sleep
, &wait
);
1214 set_current_state(TASK_INTERRUPTIBLE
);
1215 spin_unlock_irq(&runtime
->lock
);
1216 timeout
= schedule_timeout(30 * HZ
);
1217 remove_wait_queue(&runtime
->sleep
, &wait
);
1218 if (signal_pending(current
))
1219 return result
> 0 ? result
: -ERESTARTSYS
;
1220 if (!runtime
->avail
&& !timeout
)
1221 return result
> 0 ? result
: -EIO
;
1222 spin_lock_irq(&runtime
->lock
);
1224 spin_unlock_irq(&runtime
->lock
);
1225 count1
= snd_rawmidi_kernel_write1(substream
,
1226 (unsigned char __force
*)buf
,
1229 return result
> 0 ? result
: count1
;
1232 if ((size_t)count1
< count
&& (file
->f_flags
& O_NONBLOCK
))
1236 if (file
->f_flags
& O_SYNC
) {
1237 spin_lock_irq(&runtime
->lock
);
1238 while (runtime
->avail
!= runtime
->buffer_size
) {
1240 unsigned int last_avail
= runtime
->avail
;
1241 init_waitqueue_entry(&wait
, current
);
1242 add_wait_queue(&runtime
->sleep
, &wait
);
1243 set_current_state(TASK_INTERRUPTIBLE
);
1244 spin_unlock_irq(&runtime
->lock
);
1245 timeout
= schedule_timeout(30 * HZ
);
1246 remove_wait_queue(&runtime
->sleep
, &wait
);
1247 if (signal_pending(current
))
1248 return result
> 0 ? result
: -ERESTARTSYS
;
1249 if (runtime
->avail
== last_avail
&& !timeout
)
1250 return result
> 0 ? result
: -EIO
;
1251 spin_lock_irq(&runtime
->lock
);
1253 spin_unlock_irq(&runtime
->lock
);
1258 static unsigned int snd_rawmidi_poll(struct file
*file
, poll_table
* wait
)
1260 struct snd_rawmidi_file
*rfile
;
1261 struct snd_rawmidi_runtime
*runtime
;
1264 rfile
= file
->private_data
;
1265 if (rfile
->input
!= NULL
) {
1266 runtime
= rfile
->input
->runtime
;
1267 snd_rawmidi_input_trigger(rfile
->input
, 1);
1268 poll_wait(file
, &runtime
->sleep
, wait
);
1270 if (rfile
->output
!= NULL
) {
1271 runtime
= rfile
->output
->runtime
;
1272 poll_wait(file
, &runtime
->sleep
, wait
);
1275 if (rfile
->input
!= NULL
) {
1276 if (snd_rawmidi_ready(rfile
->input
))
1277 mask
|= POLLIN
| POLLRDNORM
;
1279 if (rfile
->output
!= NULL
) {
1280 if (snd_rawmidi_ready(rfile
->output
))
1281 mask
|= POLLOUT
| POLLWRNORM
;
1288 #ifdef CONFIG_COMPAT
1289 #include "rawmidi_compat.c"
1291 #define snd_rawmidi_ioctl_compat NULL
1298 static void snd_rawmidi_proc_info_read(struct snd_info_entry
*entry
,
1299 struct snd_info_buffer
*buffer
)
1301 struct snd_rawmidi
*rmidi
;
1302 struct snd_rawmidi_substream
*substream
;
1303 struct snd_rawmidi_runtime
*runtime
;
1304 struct list_head
*list
;
1306 rmidi
= entry
->private_data
;
1307 snd_iprintf(buffer
, "%s\n\n", rmidi
->name
);
1308 down(&rmidi
->open_mutex
);
1309 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) {
1310 list_for_each(list
, &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
) {
1311 substream
= list_entry(list
, struct snd_rawmidi_substream
, list
);
1314 " Tx bytes : %lu\n",
1316 (unsigned long) substream
->bytes
);
1317 if (substream
->opened
) {
1318 runtime
= substream
->runtime
;
1321 " Buffer size : %lu\n"
1323 runtime
->oss
? "OSS compatible" : "native",
1324 (unsigned long) runtime
->buffer_size
,
1325 (unsigned long) runtime
->avail
);
1329 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
) {
1330 list_for_each(list
, &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
) {
1331 substream
= list_entry(list
, struct snd_rawmidi_substream
, list
);
1334 " Rx bytes : %lu\n",
1336 (unsigned long) substream
->bytes
);
1337 if (substream
->opened
) {
1338 runtime
= substream
->runtime
;
1340 " Buffer size : %lu\n"
1342 " Overruns : %lu\n",
1343 (unsigned long) runtime
->buffer_size
,
1344 (unsigned long) runtime
->avail
,
1345 (unsigned long) runtime
->xruns
);
1349 up(&rmidi
->open_mutex
);
1353 * Register functions
1356 static struct file_operations snd_rawmidi_f_ops
=
1358 .owner
= THIS_MODULE
,
1359 .read
= snd_rawmidi_read
,
1360 .write
= snd_rawmidi_write
,
1361 .open
= snd_rawmidi_open
,
1362 .release
= snd_rawmidi_release
,
1363 .poll
= snd_rawmidi_poll
,
1364 .unlocked_ioctl
= snd_rawmidi_ioctl
,
1365 .compat_ioctl
= snd_rawmidi_ioctl_compat
,
1368 static struct snd_minor snd_rawmidi_reg
=
1370 .comment
= "raw midi",
1371 .f_ops
= &snd_rawmidi_f_ops
,
1374 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi
*rmidi
,
1375 struct snd_rawmidi_str
*stream
,
1379 struct snd_rawmidi_substream
*substream
;
1382 INIT_LIST_HEAD(&stream
->substreams
);
1383 for (idx
= 0; idx
< count
; idx
++) {
1384 substream
= kzalloc(sizeof(*substream
), GFP_KERNEL
);
1385 if (substream
== NULL
) {
1386 snd_printk(KERN_ERR
"rawmidi: cannot allocate substream\n");
1389 substream
->stream
= direction
;
1390 substream
->number
= idx
;
1391 substream
->rmidi
= rmidi
;
1392 substream
->pstr
= stream
;
1393 list_add_tail(&substream
->list
, &stream
->substreams
);
1394 stream
->substream_count
++;
1400 * snd_rawmidi_new - create a rawmidi instance
1401 * @card: the card instance
1402 * @id: the id string
1403 * @device: the device index
1404 * @output_count: the number of output streams
1405 * @input_count: the number of input streams
1406 * @rrawmidi: the pointer to store the new rawmidi instance
1408 * Creates a new rawmidi instance.
1409 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1411 * Returns zero if successful, or a negative error code on failure.
1413 int snd_rawmidi_new(struct snd_card
*card
, char *id
, int device
,
1414 int output_count
, int input_count
,
1415 struct snd_rawmidi
** rrawmidi
)
1417 struct snd_rawmidi
*rmidi
;
1419 static struct snd_device_ops ops
= {
1420 .dev_free
= snd_rawmidi_dev_free
,
1421 .dev_register
= snd_rawmidi_dev_register
,
1422 .dev_disconnect
= snd_rawmidi_dev_disconnect
,
1423 .dev_unregister
= snd_rawmidi_dev_unregister
1426 snd_assert(rrawmidi
!= NULL
, return -EINVAL
);
1428 snd_assert(card
!= NULL
, return -ENXIO
);
1429 rmidi
= kzalloc(sizeof(*rmidi
), GFP_KERNEL
);
1430 if (rmidi
== NULL
) {
1431 snd_printk(KERN_ERR
"rawmidi: cannot allocate\n");
1435 rmidi
->device
= device
;
1436 init_MUTEX(&rmidi
->open_mutex
);
1437 init_waitqueue_head(&rmidi
->open_wait
);
1439 strlcpy(rmidi
->id
, id
, sizeof(rmidi
->id
));
1440 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1441 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
],
1442 SNDRV_RAWMIDI_STREAM_INPUT
,
1443 input_count
)) < 0) {
1444 snd_rawmidi_free(rmidi
);
1447 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1448 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
],
1449 SNDRV_RAWMIDI_STREAM_OUTPUT
,
1450 output_count
)) < 0) {
1451 snd_rawmidi_free(rmidi
);
1454 if ((err
= snd_device_new(card
, SNDRV_DEV_RAWMIDI
, rmidi
, &ops
)) < 0) {
1455 snd_rawmidi_free(rmidi
);
1462 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str
*stream
)
1464 struct snd_rawmidi_substream
*substream
;
1466 while (!list_empty(&stream
->substreams
)) {
1467 substream
= list_entry(stream
->substreams
.next
, struct snd_rawmidi_substream
, list
);
1468 list_del(&substream
->list
);
1473 static int snd_rawmidi_free(struct snd_rawmidi
*rmidi
)
1475 snd_assert(rmidi
!= NULL
, return -ENXIO
);
1476 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
1477 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
1478 if (rmidi
->private_free
)
1479 rmidi
->private_free(rmidi
);
1484 static int snd_rawmidi_dev_free(struct snd_device
*device
)
1486 struct snd_rawmidi
*rmidi
= device
->device_data
;
1487 return snd_rawmidi_free(rmidi
);
1490 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1491 static void snd_rawmidi_dev_seq_free(struct snd_seq_device
*device
)
1493 struct snd_rawmidi
*rmidi
= device
->private_data
;
1494 rmidi
->seq_dev
= NULL
;
1498 static int snd_rawmidi_dev_register(struct snd_device
*device
)
1501 struct snd_info_entry
*entry
;
1503 struct snd_rawmidi
*rmidi
= device
->device_data
;
1505 if (rmidi
->device
>= SNDRV_RAWMIDI_DEVICES
)
1507 down(®ister_mutex
);
1508 idx
= (rmidi
->card
->number
* SNDRV_RAWMIDI_DEVICES
) + rmidi
->device
;
1509 if (snd_rawmidi_devices
[idx
] != NULL
) {
1510 up(®ister_mutex
);
1513 snd_rawmidi_devices
[idx
] = rmidi
;
1514 sprintf(name
, "midiC%iD%i", rmidi
->card
->number
, rmidi
->device
);
1515 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI
,
1516 rmidi
->card
, rmidi
->device
,
1517 &snd_rawmidi_reg
, name
)) < 0) {
1518 snd_printk(KERN_ERR
"unable to register rawmidi device %i:%i\n", rmidi
->card
->number
, rmidi
->device
);
1519 snd_rawmidi_devices
[idx
] = NULL
;
1520 up(®ister_mutex
);
1523 if (rmidi
->ops
&& rmidi
->ops
->dev_register
&&
1524 (err
= rmidi
->ops
->dev_register(rmidi
)) < 0) {
1525 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1526 snd_rawmidi_devices
[idx
] = NULL
;
1527 up(®ister_mutex
);
1530 #ifdef CONFIG_SND_OSSEMUL
1532 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1533 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1534 rmidi
->card
, 0, &snd_rawmidi_reg
, name
) < 0) {
1535 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 0);
1538 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1539 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
, rmidi
->name
);
1543 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
]) {
1544 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1545 rmidi
->card
, 1, &snd_rawmidi_reg
, name
) < 0) {
1546 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 1);
1551 #endif /* CONFIG_SND_OSSEMUL */
1552 up(®ister_mutex
);
1553 sprintf(name
, "midi%d", rmidi
->device
);
1554 entry
= snd_info_create_card_entry(rmidi
->card
, name
, rmidi
->card
->proc_root
);
1556 entry
->private_data
= rmidi
;
1557 entry
->c
.text
.read_size
= 1024;
1558 entry
->c
.text
.read
= snd_rawmidi_proc_info_read
;
1559 if (snd_info_register(entry
) < 0) {
1560 snd_info_free_entry(entry
);
1564 rmidi
->proc_entry
= entry
;
1565 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1566 if (!rmidi
->ops
|| !rmidi
->ops
->dev_register
) { /* own registration mechanism */
1567 if (snd_seq_device_new(rmidi
->card
, rmidi
->device
, SNDRV_SEQ_DEV_ID_MIDISYNTH
, 0, &rmidi
->seq_dev
) >= 0) {
1568 rmidi
->seq_dev
->private_data
= rmidi
;
1569 rmidi
->seq_dev
->private_free
= snd_rawmidi_dev_seq_free
;
1570 sprintf(rmidi
->seq_dev
->name
, "MIDI %d-%d", rmidi
->card
->number
, rmidi
->device
);
1571 snd_device_register(rmidi
->card
, rmidi
->seq_dev
);
1578 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
)
1580 struct snd_rawmidi
*rmidi
= device
->device_data
;
1583 down(®ister_mutex
);
1584 idx
= (rmidi
->card
->number
* SNDRV_RAWMIDI_DEVICES
) + rmidi
->device
;
1585 snd_rawmidi_devices
[idx
] = NULL
;
1586 up(®ister_mutex
);
1590 static int snd_rawmidi_dev_unregister(struct snd_device
*device
)
1593 struct snd_rawmidi
*rmidi
= device
->device_data
;
1595 snd_assert(rmidi
!= NULL
, return -ENXIO
);
1596 down(®ister_mutex
);
1597 idx
= (rmidi
->card
->number
* SNDRV_RAWMIDI_DEVICES
) + rmidi
->device
;
1598 snd_rawmidi_devices
[idx
] = NULL
;
1599 if (rmidi
->proc_entry
) {
1600 snd_info_unregister(rmidi
->proc_entry
);
1601 rmidi
->proc_entry
= NULL
;
1603 #ifdef CONFIG_SND_OSSEMUL
1604 if (rmidi
->ossreg
) {
1605 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1606 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 0);
1607 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1608 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
);
1611 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
])
1612 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 1);
1615 #endif /* CONFIG_SND_OSSEMUL */
1616 if (rmidi
->ops
&& rmidi
->ops
->dev_unregister
)
1617 rmidi
->ops
->dev_unregister(rmidi
);
1618 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1619 up(®ister_mutex
);
1620 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1621 if (rmidi
->seq_dev
) {
1622 snd_device_free(rmidi
->card
, rmidi
->seq_dev
);
1623 rmidi
->seq_dev
= NULL
;
1626 return snd_rawmidi_free(rmidi
);
1630 * snd_rawmidi_set_ops - set the rawmidi operators
1631 * @rmidi: the rawmidi instance
1632 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1633 * @ops: the operator table
1635 * Sets the rawmidi operators for the given stream direction.
1637 void snd_rawmidi_set_ops(struct snd_rawmidi
*rmidi
, int stream
,
1638 struct snd_rawmidi_ops
*ops
)
1640 struct list_head
*list
;
1641 struct snd_rawmidi_substream
*substream
;
1643 list_for_each(list
, &rmidi
->streams
[stream
].substreams
) {
1644 substream
= list_entry(list
, struct snd_rawmidi_substream
, list
);
1645 substream
->ops
= ops
;
1653 static int __init
alsa_rawmidi_init(void)
1656 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl
);
1657 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl
);
1658 #ifdef CONFIG_SND_OSSEMUL
1660 /* check device map table */
1661 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1662 if (midi_map
[i
] < 0 || midi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1663 snd_printk(KERN_ERR
"invalid midi_map[%d] = %d\n", i
, midi_map
[i
]);
1666 if (amidi_map
[i
] < 0 || amidi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1667 snd_printk(KERN_ERR
"invalid amidi_map[%d] = %d\n", i
, amidi_map
[i
]);
1672 #endif /* CONFIG_SND_OSSEMUL */
1676 static void __exit
alsa_rawmidi_exit(void)
1678 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl
);
1679 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl
);
1682 module_init(alsa_rawmidi_init
)
1683 module_exit(alsa_rawmidi_exit
)
1685 EXPORT_SYMBOL(snd_rawmidi_output_params
);
1686 EXPORT_SYMBOL(snd_rawmidi_input_params
);
1687 EXPORT_SYMBOL(snd_rawmidi_drop_output
);
1688 EXPORT_SYMBOL(snd_rawmidi_drain_output
);
1689 EXPORT_SYMBOL(snd_rawmidi_drain_input
);
1690 EXPORT_SYMBOL(snd_rawmidi_receive
);
1691 EXPORT_SYMBOL(snd_rawmidi_transmit_empty
);
1692 EXPORT_SYMBOL(snd_rawmidi_transmit_peek
);
1693 EXPORT_SYMBOL(snd_rawmidi_transmit_ack
);
1694 EXPORT_SYMBOL(snd_rawmidi_transmit
);
1695 EXPORT_SYMBOL(snd_rawmidi_new
);
1696 EXPORT_SYMBOL(snd_rawmidi_set_ops
);
1697 EXPORT_SYMBOL(snd_rawmidi_info
);
1698 EXPORT_SYMBOL(snd_rawmidi_info_select
);
1699 EXPORT_SYMBOL(snd_rawmidi_kernel_open
);
1700 EXPORT_SYMBOL(snd_rawmidi_kernel_release
);
1701 EXPORT_SYMBOL(snd_rawmidi_kernel_read
);
1702 EXPORT_SYMBOL(snd_rawmidi_kernel_write
);