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 <sound/rawmidi.h>
33 #include <sound/info.h>
34 #include <sound/control.h>
35 #include <sound/minors.h>
36 #include <sound/initval.h>
38 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
39 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
40 MODULE_LICENSE("GPL");
42 #ifdef CONFIG_SND_OSSEMUL
43 static int midi_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 0};
44 static int amidi_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
46 module_param_array(midi_map
, int, boot_devs
, 0444);
47 MODULE_PARM_DESC(midi_map
, "Raw MIDI device number assigned to 1st OSS device.");
48 module_param_array(amidi_map
, int, boot_devs
, 0444);
49 MODULE_PARM_DESC(amidi_map
, "Raw MIDI device number assigned to 2nd OSS device.");
50 #endif /* CONFIG_SND_OSSEMUL */
52 static int snd_rawmidi_free(snd_rawmidi_t
*rawmidi
);
53 static int snd_rawmidi_dev_free(snd_device_t
*device
);
54 static int snd_rawmidi_dev_register(snd_device_t
*device
);
55 static int snd_rawmidi_dev_disconnect(snd_device_t
*device
);
56 static int snd_rawmidi_dev_unregister(snd_device_t
*device
);
58 snd_rawmidi_t
*snd_rawmidi_devices
[SNDRV_CARDS
* SNDRV_RAWMIDI_DEVICES
];
60 static DECLARE_MUTEX(register_mutex
);
62 static inline unsigned short snd_rawmidi_file_flags(struct file
*file
)
64 switch (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) {
66 return SNDRV_RAWMIDI_LFLG_OUTPUT
;
68 return SNDRV_RAWMIDI_LFLG_INPUT
;
70 return SNDRV_RAWMIDI_LFLG_OPEN
;
74 static inline int snd_rawmidi_ready(snd_rawmidi_substream_t
* substream
)
76 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
77 return runtime
->avail
>= runtime
->avail_min
;
80 static inline int snd_rawmidi_ready_append(snd_rawmidi_substream_t
* substream
, size_t count
)
82 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
83 return runtime
->avail
>= runtime
->avail_min
&&
84 (!substream
->append
|| runtime
->avail
>= count
);
87 static int snd_rawmidi_init(snd_rawmidi_substream_t
*substream
)
89 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
90 spin_lock_init(&runtime
->lock
);
91 init_waitqueue_head(&runtime
->sleep
);
92 runtime
->event
= NULL
;
93 runtime
->buffer_size
= PAGE_SIZE
;
94 runtime
->avail_min
= 1;
95 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
98 runtime
->avail
= runtime
->buffer_size
;
99 if ((runtime
->buffer
= kmalloc(runtime
->buffer_size
, GFP_KERNEL
)) == NULL
)
101 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
105 static int snd_rawmidi_done_buffer(snd_rawmidi_runtime_t
*runtime
)
107 if (runtime
->buffer
) {
108 kfree(runtime
->buffer
);
109 runtime
->buffer
= NULL
;
114 int snd_rawmidi_drop_output(snd_rawmidi_substream_t
* substream
)
116 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
118 substream
->ops
->trigger(substream
, 0);
119 runtime
->trigger
= 0;
121 /* interrupts are not enabled at this moment,
122 so spinlock is not required */
123 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
124 runtime
->avail
= runtime
->buffer_size
;
128 int snd_rawmidi_drain_output(snd_rawmidi_substream_t
* substream
)
132 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
136 while (runtime
->avail
< runtime
->buffer_size
) {
137 timeout
= interruptible_sleep_on_timeout(&runtime
->sleep
, 10 * HZ
);
138 if (signal_pending(current
)) {
142 if (runtime
->avail
< runtime
->buffer_size
&& !timeout
) {
143 snd_printk(KERN_WARNING
"rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime
->avail
, (long)runtime
->buffer_size
);
149 if (err
!= -ERESTARTSYS
) {
150 /* we need wait a while to make sure that Tx FIFOs are empty */
151 if (substream
->ops
->drain
)
152 substream
->ops
->drain(substream
);
154 set_current_state(TASK_UNINTERRUPTIBLE
);
155 schedule_timeout(HZ
/ 20);
157 snd_rawmidi_drop_output(substream
);
162 int snd_rawmidi_drain_input(snd_rawmidi_substream_t
* substream
)
164 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
166 substream
->ops
->trigger(substream
, 0);
167 runtime
->trigger
= 0;
169 /* interrupts aren't enabled at this moment, so spinlock isn't needed */
170 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
175 int snd_rawmidi_kernel_open(int cardnum
, int device
, int subdevice
,
176 int mode
, snd_rawmidi_file_t
* rfile
)
178 snd_rawmidi_t
*rmidi
;
179 struct list_head
*list1
, *list2
;
180 snd_rawmidi_substream_t
*sinput
, *soutput
;
181 snd_rawmidi_runtime_t
*input
= NULL
, *output
= NULL
;
185 rfile
->input
= rfile
->output
= NULL
;
186 rmidi
= snd_rawmidi_devices
[(cardnum
* SNDRV_RAWMIDI_DEVICES
) + device
];
191 if (!try_module_get(rmidi
->card
->module
)) {
195 if (!(mode
& SNDRV_RAWMIDI_LFLG_NOOPENLOCK
))
196 down(&rmidi
->open_mutex
);
197 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
198 if (!(rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
)) {
202 if (subdevice
>= 0 && (unsigned int)subdevice
>= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_count
) {
206 if (rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_opened
>=
207 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_count
) {
212 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
213 if (!(rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
)) {
217 if (subdevice
>= 0 && (unsigned int)subdevice
>= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_count
) {
221 if (rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_opened
>=
222 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_count
) {
227 list1
= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
.next
;
229 if (list1
== &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
) {
231 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
237 sinput
= list_entry(list1
, snd_rawmidi_substream_t
, list
);
238 if ((mode
& SNDRV_RAWMIDI_LFLG_INPUT
) && sinput
->opened
)
240 if (subdevice
< 0 || (subdevice
>= 0 && subdevice
== sinput
->number
))
245 list2
= rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
.next
;
247 if (list2
== &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
) {
249 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
255 soutput
= list_entry(list2
, snd_rawmidi_substream_t
, list
);
256 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
257 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
) {
258 if (soutput
->opened
&& !soutput
->append
)
265 if (subdevice
< 0 || (subdevice
>= 0 && subdevice
== soutput
->number
))
270 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
271 input
= kcalloc(1, sizeof(*input
), GFP_KERNEL
);
276 sinput
->runtime
= input
;
277 if (snd_rawmidi_init(sinput
) < 0) {
281 if ((err
= sinput
->ops
->open(sinput
)) < 0) {
282 sinput
->runtime
= NULL
;
286 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_opened
++;
290 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
293 output
= kcalloc(1, sizeof(*output
), GFP_KERNEL
);
294 if (output
== NULL
) {
298 soutput
->runtime
= output
;
299 if (snd_rawmidi_init(soutput
) < 0) {
300 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
301 sinput
->ops
->close(sinput
);
302 sinput
->runtime
= NULL
;
307 if ((err
= soutput
->ops
->open(soutput
)) < 0) {
308 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
309 sinput
->ops
->close(sinput
);
310 sinput
->runtime
= NULL
;
312 soutput
->runtime
= NULL
;
317 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
)
319 if (soutput
->use_count
++ == 0)
320 soutput
->active_sensing
= 1;
321 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_opened
++;
325 if (!(mode
& SNDRV_RAWMIDI_LFLG_NOOPENLOCK
))
326 up(&rmidi
->open_mutex
);
328 rfile
->rmidi
= rmidi
;
329 rfile
->input
= sinput
;
330 rfile
->output
= soutput
;
336 snd_rawmidi_done_buffer(input
);
339 if (output
!= NULL
) {
340 snd_rawmidi_done_buffer(output
);
343 module_put(rmidi
->card
->module
);
344 if (!(mode
& SNDRV_RAWMIDI_LFLG_NOOPENLOCK
))
345 up(&rmidi
->open_mutex
);
350 static int snd_rawmidi_open(struct inode
*inode
, struct file
*file
)
352 int maj
= imajor(inode
);
355 int device
, subdevice
;
356 unsigned short fflags
;
358 snd_rawmidi_t
*rmidi
;
359 snd_rawmidi_file_t
*rawmidi_file
;
361 struct list_head
*list
;
362 snd_ctl_file_t
*kctl
;
365 case CONFIG_SND_MAJOR
:
366 cardnum
= SNDRV_MINOR_CARD(iminor(inode
));
367 cardnum
%= SNDRV_CARDS
;
368 device
= SNDRV_MINOR_DEVICE(iminor(inode
)) - SNDRV_MINOR_RAWMIDI
;
369 device
%= SNDRV_MINOR_RAWMIDIS
;
371 #ifdef CONFIG_SND_OSSEMUL
373 cardnum
= SNDRV_MINOR_OSS_CARD(iminor(inode
));
374 cardnum
%= SNDRV_CARDS
;
375 device
= SNDRV_MINOR_OSS_DEVICE(iminor(inode
)) == SNDRV_MINOR_OSS_MIDI
?
376 midi_map
[cardnum
] : amidi_map
[cardnum
];
383 rmidi
= snd_rawmidi_devices
[(cardnum
* SNDRV_RAWMIDI_DEVICES
) + device
];
386 #ifdef CONFIG_SND_OSSEMUL
387 if (maj
== SOUND_MAJOR
&& !rmidi
->ossreg
)
390 if ((file
->f_flags
& O_APPEND
) && !(file
->f_flags
& O_NONBLOCK
))
391 return -EINVAL
; /* invalid combination */
393 err
= snd_card_file_add(card
, file
);
396 fflags
= snd_rawmidi_file_flags(file
);
397 if ((file
->f_flags
& O_APPEND
) || maj
!= CONFIG_SND_MAJOR
) /* OSS emul? */
398 fflags
|= SNDRV_RAWMIDI_LFLG_APPEND
;
399 fflags
|= SNDRV_RAWMIDI_LFLG_NOOPENLOCK
;
400 rawmidi_file
= kmalloc(sizeof(*rawmidi_file
), GFP_KERNEL
);
401 if (rawmidi_file
== NULL
) {
402 snd_card_file_remove(card
, file
);
405 init_waitqueue_entry(&wait
, current
);
406 add_wait_queue(&rmidi
->open_wait
, &wait
);
407 down(&rmidi
->open_mutex
);
410 down_read(&card
->controls_rwsem
);
411 list_for_each(list
, &card
->ctl_files
) {
412 kctl
= snd_ctl_file(list
);
413 if (kctl
->pid
== current
->pid
) {
414 subdevice
= kctl
->prefer_rawmidi_subdevice
;
418 up_read(&card
->controls_rwsem
);
419 err
= snd_rawmidi_kernel_open(cardnum
, device
, subdevice
, fflags
, rawmidi_file
);
422 if (err
== -EAGAIN
) {
423 if (file
->f_flags
& O_NONBLOCK
) {
429 set_current_state(TASK_INTERRUPTIBLE
);
430 up(&rmidi
->open_mutex
);
432 down(&rmidi
->open_mutex
);
433 if (signal_pending(current
)) {
438 #ifdef CONFIG_SND_OSSEMUL
439 if (rawmidi_file
->input
&& rawmidi_file
->input
->runtime
)
440 rawmidi_file
->input
->runtime
->oss
= (maj
== SOUND_MAJOR
);
441 if (rawmidi_file
->output
&& rawmidi_file
->output
->runtime
)
442 rawmidi_file
->output
->runtime
->oss
= (maj
== SOUND_MAJOR
);
444 remove_wait_queue(&rmidi
->open_wait
, &wait
);
446 file
->private_data
= rawmidi_file
;
448 snd_card_file_remove(card
, file
);
451 up(&rmidi
->open_mutex
);
455 int snd_rawmidi_kernel_release(snd_rawmidi_file_t
* rfile
)
457 snd_rawmidi_t
*rmidi
;
458 snd_rawmidi_substream_t
*substream
;
459 snd_rawmidi_runtime_t
*runtime
;
461 snd_assert(rfile
!= NULL
, return -ENXIO
);
462 snd_assert(rfile
->input
!= NULL
|| rfile
->output
!= NULL
, return -ENXIO
);
463 rmidi
= rfile
->rmidi
;
464 down(&rmidi
->open_mutex
);
465 if (rfile
->input
!= NULL
) {
466 substream
= rfile
->input
;
468 runtime
= substream
->runtime
;
469 runtime
->trigger
= 0;
470 substream
->ops
->trigger(substream
, 0);
471 substream
->ops
->close(substream
);
472 snd_rawmidi_done_buffer(runtime
);
473 if (runtime
->private_free
!= NULL
)
474 runtime
->private_free(substream
);
476 substream
->runtime
= NULL
;
477 substream
->opened
= 0;
478 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substream_opened
--;
480 if (rfile
->output
!= NULL
) {
481 substream
= rfile
->output
;
482 rfile
->output
= NULL
;
483 if (--substream
->use_count
== 0) {
484 runtime
= substream
->runtime
;
485 if (substream
->active_sensing
) {
486 unsigned char buf
= 0xfe;
487 /* sending single active sensing message to shut the device up */
488 snd_rawmidi_kernel_write(substream
, &buf
, 1);
490 if (snd_rawmidi_drain_output(substream
) == -ERESTARTSYS
)
491 substream
->ops
->trigger(substream
, 0);
492 substream
->ops
->close(substream
);
493 snd_rawmidi_done_buffer(runtime
);
494 if (runtime
->private_free
!= NULL
)
495 runtime
->private_free(substream
);
497 substream
->runtime
= NULL
;
498 substream
->opened
= 0;
499 substream
->append
= 0;
501 rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substream_opened
--;
503 up(&rmidi
->open_mutex
);
504 module_put(rmidi
->card
->module
);
508 static int snd_rawmidi_release(struct inode
*inode
, struct file
*file
)
510 snd_rawmidi_file_t
*rfile
;
511 snd_rawmidi_t
*rmidi
;
514 rfile
= file
->private_data
;
515 err
= snd_rawmidi_kernel_release(rfile
);
516 rmidi
= rfile
->rmidi
;
517 wake_up(&rmidi
->open_wait
);
519 snd_card_file_remove(rmidi
->card
, file
);
523 int snd_rawmidi_info(snd_rawmidi_substream_t
*substream
, snd_rawmidi_info_t
*info
)
525 snd_rawmidi_t
*rmidi
;
527 if (substream
== NULL
)
529 rmidi
= substream
->rmidi
;
530 memset(info
, 0, sizeof(*info
));
531 info
->card
= rmidi
->card
->number
;
532 info
->device
= rmidi
->device
;
533 info
->subdevice
= substream
->number
;
534 info
->stream
= substream
->stream
;
535 info
->flags
= rmidi
->info_flags
;
536 strcpy(info
->id
, rmidi
->id
);
537 strcpy(info
->name
, rmidi
->name
);
538 strcpy(info
->subname
, substream
->name
);
539 info
->subdevices_count
= substream
->pstr
->substream_count
;
540 info
->subdevices_avail
= (substream
->pstr
->substream_count
-
541 substream
->pstr
->substream_opened
);
545 static int snd_rawmidi_info_user(snd_rawmidi_substream_t
*substream
, snd_rawmidi_info_t __user
* _info
)
547 snd_rawmidi_info_t info
;
549 if ((err
= snd_rawmidi_info(substream
, &info
)) < 0)
551 if (copy_to_user(_info
, &info
, sizeof(snd_rawmidi_info_t
)))
556 int snd_rawmidi_info_select(snd_card_t
*card
, snd_rawmidi_info_t
*info
)
558 snd_rawmidi_t
*rmidi
;
559 snd_rawmidi_str_t
*pstr
;
560 snd_rawmidi_substream_t
*substream
;
561 struct list_head
*list
;
562 if (info
->device
>= SNDRV_RAWMIDI_DEVICES
)
564 rmidi
= snd_rawmidi_devices
[card
->number
* SNDRV_RAWMIDI_DEVICES
+ info
->device
];
565 if (info
->stream
< 0 || info
->stream
> 1)
567 pstr
= &rmidi
->streams
[info
->stream
];
568 if (pstr
->substream_count
== 0)
570 if (info
->subdevice
>= pstr
->substream_count
)
572 list_for_each(list
, &pstr
->substreams
) {
573 substream
= list_entry(list
, snd_rawmidi_substream_t
, list
);
574 if ((unsigned int)substream
->number
== info
->subdevice
)
575 return snd_rawmidi_info(substream
, info
);
580 static int snd_rawmidi_info_select_user(snd_card_t
*card
,
581 snd_rawmidi_info_t __user
*_info
)
584 snd_rawmidi_info_t info
;
585 if (get_user(info
.device
, &_info
->device
))
587 if (get_user(info
.stream
, &_info
->stream
))
589 if (get_user(info
.subdevice
, &_info
->subdevice
))
591 if ((err
= snd_rawmidi_info_select(card
, &info
)) < 0)
593 if (copy_to_user(_info
, &info
, sizeof(snd_rawmidi_info_t
)))
598 int snd_rawmidi_output_params(snd_rawmidi_substream_t
* substream
,
599 snd_rawmidi_params_t
* params
)
602 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
604 if (substream
->append
&& substream
->use_count
> 1)
606 snd_rawmidi_drain_output(substream
);
607 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
610 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
613 if (params
->buffer_size
!= runtime
->buffer_size
) {
614 if ((newbuf
= (char *) kmalloc(params
->buffer_size
, GFP_KERNEL
)) == NULL
)
616 kfree(runtime
->buffer
);
617 runtime
->buffer
= newbuf
;
618 runtime
->buffer_size
= params
->buffer_size
;
620 runtime
->avail_min
= params
->avail_min
;
621 substream
->active_sensing
= !params
->no_active_sensing
;
625 int snd_rawmidi_input_params(snd_rawmidi_substream_t
* substream
,
626 snd_rawmidi_params_t
* params
)
629 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
631 snd_rawmidi_drain_input(substream
);
632 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
635 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
638 if (params
->buffer_size
!= runtime
->buffer_size
) {
639 if ((newbuf
= (char *) kmalloc(params
->buffer_size
, GFP_KERNEL
)) == NULL
)
641 kfree(runtime
->buffer
);
642 runtime
->buffer
= newbuf
;
643 runtime
->buffer_size
= params
->buffer_size
;
645 runtime
->avail_min
= params
->avail_min
;
649 static int snd_rawmidi_output_status(snd_rawmidi_substream_t
* substream
,
650 snd_rawmidi_status_t
* status
)
652 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
654 memset(status
, 0, sizeof(*status
));
655 status
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
656 spin_lock_irq(&runtime
->lock
);
657 status
->avail
= runtime
->avail
;
658 spin_unlock_irq(&runtime
->lock
);
662 static int snd_rawmidi_input_status(snd_rawmidi_substream_t
* substream
,
663 snd_rawmidi_status_t
* status
)
665 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
667 memset(status
, 0, sizeof(*status
));
668 status
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
669 spin_lock_irq(&runtime
->lock
);
670 status
->avail
= runtime
->avail
;
671 status
->xruns
= runtime
->xruns
;
673 spin_unlock_irq(&runtime
->lock
);
677 static inline int _snd_rawmidi_ioctl(struct inode
*inode
, struct file
*file
,
678 unsigned int cmd
, unsigned long arg
)
680 snd_rawmidi_file_t
*rfile
;
681 void __user
*argp
= (void __user
*)arg
;
683 rfile
= file
->private_data
;
684 if (((cmd
>> 8) & 0xff) != 'W')
687 case SNDRV_RAWMIDI_IOCTL_PVERSION
:
688 return put_user(SNDRV_RAWMIDI_VERSION
, (int __user
*)argp
) ? -EFAULT
: 0;
689 case SNDRV_RAWMIDI_IOCTL_INFO
:
691 snd_rawmidi_stream_t stream
;
692 snd_rawmidi_info_t __user
*info
= argp
;
693 if (get_user(stream
, &info
->stream
))
696 case SNDRV_RAWMIDI_STREAM_INPUT
:
697 return snd_rawmidi_info_user(rfile
->input
, info
);
698 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
699 return snd_rawmidi_info_user(rfile
->output
, info
);
704 case SNDRV_RAWMIDI_IOCTL_PARAMS
:
706 snd_rawmidi_params_t params
;
707 if (copy_from_user(¶ms
, argp
, sizeof(snd_rawmidi_params_t
)))
709 switch (params
.stream
) {
710 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
711 if (rfile
->output
== NULL
)
713 return snd_rawmidi_output_params(rfile
->output
, ¶ms
);
714 case SNDRV_RAWMIDI_STREAM_INPUT
:
715 if (rfile
->input
== NULL
)
717 return snd_rawmidi_input_params(rfile
->input
, ¶ms
);
722 case SNDRV_RAWMIDI_IOCTL_STATUS
:
725 snd_rawmidi_status_t status
;
726 if (copy_from_user(&status
, argp
, sizeof(snd_rawmidi_status_t
)))
728 switch (status
.stream
) {
729 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
730 if (rfile
->output
== NULL
)
732 err
= snd_rawmidi_output_status(rfile
->output
, &status
);
734 case SNDRV_RAWMIDI_STREAM_INPUT
:
735 if (rfile
->input
== NULL
)
737 err
= snd_rawmidi_input_status(rfile
->input
, &status
);
744 if (copy_to_user(argp
, &status
, sizeof(snd_rawmidi_status_t
)))
748 case SNDRV_RAWMIDI_IOCTL_DROP
:
751 if (get_user(val
, (long __user
*) argp
))
754 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
755 if (rfile
->output
== NULL
)
757 return snd_rawmidi_drop_output(rfile
->output
);
762 case SNDRV_RAWMIDI_IOCTL_DRAIN
:
765 if (get_user(val
, (long __user
*) argp
))
768 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
769 if (rfile
->output
== NULL
)
771 return snd_rawmidi_drain_output(rfile
->output
);
772 case SNDRV_RAWMIDI_STREAM_INPUT
:
773 if (rfile
->input
== NULL
)
775 return snd_rawmidi_drain_input(rfile
->input
);
780 #ifdef CONFIG_SND_DEBUG
782 snd_printk(KERN_WARNING
"rawmidi: unknown command = 0x%x\n", cmd
);
788 /* FIXME: need to unlock BKL to allow preemption */
789 static int snd_rawmidi_ioctl(struct inode
*inode
, struct file
*file
,
790 unsigned int cmd
, unsigned long arg
)
794 err
= _snd_rawmidi_ioctl(inode
, file
, cmd
, arg
);
799 int snd_rawmidi_control_ioctl(snd_card_t
* card
, snd_ctl_file_t
* control
,
800 unsigned int cmd
, unsigned long arg
)
802 void __user
*argp
= (void __user
*)arg
;
805 tmp
= card
->number
* SNDRV_RAWMIDI_DEVICES
;
807 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE
:
811 if (get_user(device
, (int __user
*)argp
))
813 device
= device
< 0 ? 0 : device
+ 1;
814 while (device
< SNDRV_RAWMIDI_DEVICES
) {
815 if (snd_rawmidi_devices
[tmp
+ device
])
819 if (device
== SNDRV_RAWMIDI_DEVICES
)
821 if (put_user(device
, (int __user
*)argp
))
825 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE
:
829 if (get_user(val
, (int __user
*)argp
))
831 control
->prefer_rawmidi_subdevice
= val
;
834 case SNDRV_CTL_IOCTL_RAWMIDI_INFO
:
835 return snd_rawmidi_info_select_user(card
, argp
);
841 * snd_rawmidi_receive - receive the input data from the device
842 * @substream: the rawmidi substream
843 * @buffer: the buffer pointer
844 * @count: the data size to read
846 * Reads the data from the internal buffer.
848 * Returns the size of read data, or a negative error code on failure.
850 int snd_rawmidi_receive(snd_rawmidi_substream_t
* substream
, const unsigned char *buffer
, int count
)
853 int result
= 0, count1
;
854 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
856 if (runtime
->buffer
== NULL
) {
857 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
860 spin_lock_irqsave(&runtime
->lock
, flags
);
861 if (count
== 1) { /* special case, faster code */
863 if (runtime
->avail
< runtime
->buffer_size
) {
864 runtime
->buffer
[runtime
->hw_ptr
++] = buffer
[0];
865 runtime
->hw_ptr
%= runtime
->buffer_size
;
872 substream
->bytes
+= count
;
873 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
876 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
877 count1
= runtime
->buffer_size
- runtime
->avail
;
878 memcpy(runtime
->buffer
+ runtime
->hw_ptr
, buffer
, count1
);
879 runtime
->hw_ptr
+= count1
;
880 runtime
->hw_ptr
%= runtime
->buffer_size
;
881 runtime
->avail
+= count1
;
887 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
)) {
888 count1
= runtime
->buffer_size
- runtime
->avail
;
889 runtime
->xruns
= count
- count1
;
892 memcpy(runtime
->buffer
, buffer
, count1
);
893 runtime
->hw_ptr
= count1
;
894 runtime
->avail
+= count1
;
899 if (result
> 0 && runtime
->event
== NULL
) {
900 if (snd_rawmidi_ready(substream
))
901 wake_up(&runtime
->sleep
);
903 spin_unlock_irqrestore(&runtime
->lock
, flags
);
904 if (result
> 0 && runtime
->event
)
905 runtime
->event(substream
);
909 static long snd_rawmidi_kernel_read1(snd_rawmidi_substream_t
*substream
,
910 unsigned char *buf
, long count
, int kernel
)
913 long result
= 0, count1
;
914 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
916 while (count
> 0 && runtime
->avail
) {
917 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
920 spin_lock_irqsave(&runtime
->lock
, flags
);
921 if (count1
> (int)runtime
->avail
)
922 count1
= runtime
->avail
;
924 memcpy(buf
+ result
, runtime
->buffer
+ runtime
->appl_ptr
, count1
);
926 spin_unlock_irqrestore(&runtime
->lock
, flags
);
927 if (copy_to_user(buf
+ result
, runtime
->buffer
+ runtime
->appl_ptr
, count1
)) {
928 return result
> 0 ? result
: -EFAULT
;
930 spin_lock_irqsave(&runtime
->lock
, flags
);
932 runtime
->appl_ptr
+= count1
;
933 runtime
->appl_ptr
%= runtime
->buffer_size
;
934 runtime
->avail
-= count1
;
935 spin_unlock_irqrestore(&runtime
->lock
, flags
);
942 long snd_rawmidi_kernel_read(snd_rawmidi_substream_t
*substream
, unsigned char *buf
, long count
)
944 substream
->runtime
->trigger
= 1;
945 substream
->ops
->trigger(substream
, 1);
946 return snd_rawmidi_kernel_read1(substream
, buf
, count
, 1);
949 static ssize_t
snd_rawmidi_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*offset
)
953 snd_rawmidi_file_t
*rfile
;
954 snd_rawmidi_substream_t
*substream
;
955 snd_rawmidi_runtime_t
*runtime
;
957 rfile
= file
->private_data
;
958 substream
= rfile
->input
;
959 if (substream
== NULL
)
961 runtime
= substream
->runtime
;
962 runtime
->trigger
= 1;
963 substream
->ops
->trigger(substream
, 1);
966 spin_lock_irq(&runtime
->lock
);
967 while (!snd_rawmidi_ready(substream
)) {
969 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
970 spin_unlock_irq(&runtime
->lock
);
971 return result
> 0 ? result
: -EAGAIN
;
973 init_waitqueue_entry(&wait
, current
);
974 add_wait_queue(&runtime
->sleep
, &wait
);
975 set_current_state(TASK_INTERRUPTIBLE
);
976 spin_unlock_irq(&runtime
->lock
);
978 remove_wait_queue(&runtime
->sleep
, &wait
);
979 if (signal_pending(current
))
980 return result
> 0 ? result
: -ERESTARTSYS
;
982 return result
> 0 ? result
: -EIO
;
983 spin_lock_irq(&runtime
->lock
);
985 spin_unlock_irq(&runtime
->lock
);
986 count1
= snd_rawmidi_kernel_read1(substream
, buf
, count
, 0);
988 return result
> 0 ? result
: count1
;
997 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
998 * @substream: the rawmidi substream
1000 * Returns 1 if the internal output buffer is empty, 0 if not.
1002 int snd_rawmidi_transmit_empty(snd_rawmidi_substream_t
* substream
)
1004 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
1006 unsigned long flags
;
1008 if (runtime
->buffer
== NULL
) {
1009 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1012 spin_lock_irqsave(&runtime
->lock
, flags
);
1013 result
= runtime
->avail
>= runtime
->buffer_size
;
1015 runtime
->trigger
= 1;
1016 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1021 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1022 * @substream: the rawmidi substream
1023 * @buffer: the buffer pointer
1024 * @count: data size to transfer
1026 * Copies data from the internal output buffer to the given buffer.
1028 * Call this in the interrupt handler when the midi output is ready,
1029 * and call snd_rawmidi_transmit_ack() after the transmission is
1032 * Returns the size of copied data, or a negative error code on failure.
1034 int snd_rawmidi_transmit_peek(snd_rawmidi_substream_t
* substream
, unsigned char *buffer
, int count
)
1036 unsigned long flags
;
1038 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
1040 if (runtime
->buffer
== NULL
) {
1041 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1045 spin_lock_irqsave(&runtime
->lock
, flags
);
1046 if (runtime
->avail
>= runtime
->buffer_size
) {
1047 /* warning: lowlevel layer MUST trigger down the hardware */
1048 runtime
->trigger
= 0;
1051 if (count
== 1) { /* special case, faster code */
1052 *buffer
= runtime
->buffer
[runtime
->hw_ptr
];
1055 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
1058 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
1059 count1
= runtime
->buffer_size
- runtime
->avail
;
1060 memcpy(buffer
, runtime
->buffer
+ runtime
->hw_ptr
, count1
);
1064 memcpy(buffer
+ count1
, runtime
->buffer
, count
);
1067 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1072 * snd_rawmidi_transmit_ack - acknowledge the transmission
1073 * @substream: the rawmidi substream
1074 * @count: the tranferred count
1076 * Advances the hardware pointer for the internal output buffer with
1077 * the given size and updates the condition.
1078 * Call after the transmission is finished.
1080 * Returns the advanced size if successful, or a negative error code on failure.
1082 int snd_rawmidi_transmit_ack(snd_rawmidi_substream_t
* substream
, int count
)
1084 unsigned long flags
;
1085 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
1087 if (runtime
->buffer
== NULL
) {
1088 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1091 spin_lock_irqsave(&runtime
->lock
, flags
);
1092 snd_assert(runtime
->avail
+ count
<= runtime
->buffer_size
, );
1093 runtime
->hw_ptr
+= count
;
1094 runtime
->hw_ptr
%= runtime
->buffer_size
;
1095 runtime
->avail
+= count
;
1096 substream
->bytes
+= count
;
1098 wake_up(&runtime
->sleep
);
1100 if (count
> 0 && runtime
->event
== NULL
)
1101 if (snd_rawmidi_ready(substream
))
1102 wake_up(&runtime
->sleep
);
1103 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1104 if (count
> 0 && runtime
->event
)
1105 runtime
->event(substream
);
1110 * snd_rawmidi_transmit - copy from the buffer to the device
1111 * @substream: the rawmidi substream
1112 * @buf: the buffer pointer
1113 * @count: the data size to transfer
1115 * Copies data from the buffer to the device and advances the pointer.
1117 * Returns the copied size if successful, or a negative error code on failure.
1119 int snd_rawmidi_transmit(snd_rawmidi_substream_t
* substream
, unsigned char *buffer
, int count
)
1121 count
= snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1124 return snd_rawmidi_transmit_ack(substream
, count
);
1127 static long snd_rawmidi_kernel_write1(snd_rawmidi_substream_t
* substream
, const unsigned char *buf
, long count
, int kernel
)
1129 unsigned long flags
;
1130 long count1
, result
;
1131 snd_rawmidi_runtime_t
*runtime
= substream
->runtime
;
1133 snd_assert(buf
!= NULL
, return -EINVAL
);
1134 snd_assert(runtime
->buffer
!= NULL
, return -EINVAL
);
1137 spin_lock_irqsave(&runtime
->lock
, flags
);
1138 if (substream
->append
) {
1139 if ((long)runtime
->avail
< count
) {
1140 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1144 while (count
> 0 && runtime
->avail
> 0) {
1145 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1148 if (count1
> (long)runtime
->avail
)
1149 count1
= runtime
->avail
;
1151 memcpy(runtime
->buffer
+ runtime
->appl_ptr
, buf
, count1
);
1153 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1154 if (copy_from_user(runtime
->buffer
+ runtime
->appl_ptr
, buf
, count1
)) {
1155 spin_lock_irqsave(&runtime
->lock
, flags
);
1156 result
= result
> 0 ? result
: -EFAULT
;
1159 spin_lock_irqsave(&runtime
->lock
, flags
);
1161 runtime
->appl_ptr
+= count1
;
1162 runtime
->appl_ptr
%= runtime
->buffer_size
;
1163 runtime
->avail
-= count1
;
1170 runtime
->trigger
= 1;
1171 count1
= runtime
->avail
< runtime
->buffer_size
;
1172 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1174 substream
->ops
->trigger(substream
, 1);
1178 long snd_rawmidi_kernel_write(snd_rawmidi_substream_t
* substream
, const unsigned char *buf
, long count
)
1180 return snd_rawmidi_kernel_write1(substream
, buf
, count
, 1);
1183 static ssize_t
snd_rawmidi_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*offset
)
1185 long result
, timeout
;
1187 snd_rawmidi_file_t
*rfile
;
1188 snd_rawmidi_runtime_t
*runtime
;
1189 snd_rawmidi_substream_t
*substream
;
1191 rfile
= file
->private_data
;
1192 substream
= rfile
->output
;
1193 runtime
= substream
->runtime
;
1194 /* we cannot put an atomic message to our buffer */
1195 if (substream
->append
&& count
> runtime
->buffer_size
)
1199 spin_lock_irq(&runtime
->lock
);
1200 while (!snd_rawmidi_ready_append(substream
, count
)) {
1202 if (file
->f_flags
& O_NONBLOCK
) {
1203 spin_unlock_irq(&runtime
->lock
);
1204 return result
> 0 ? result
: -EAGAIN
;
1206 init_waitqueue_entry(&wait
, current
);
1207 add_wait_queue(&runtime
->sleep
, &wait
);
1208 set_current_state(TASK_INTERRUPTIBLE
);
1209 spin_unlock_irq(&runtime
->lock
);
1210 timeout
= schedule_timeout(30 * HZ
);
1211 remove_wait_queue(&runtime
->sleep
, &wait
);
1212 if (signal_pending(current
))
1213 return result
> 0 ? result
: -ERESTARTSYS
;
1214 if (!runtime
->avail
&& !timeout
)
1215 return result
> 0 ? result
: -EIO
;
1216 spin_lock_irq(&runtime
->lock
);
1218 spin_unlock_irq(&runtime
->lock
);
1219 count1
= snd_rawmidi_kernel_write1(substream
, buf
, count
, 0);
1221 return result
> 0 ? result
: count1
;
1224 if ((size_t)count1
< count
&& (file
->f_flags
& O_NONBLOCK
))
1228 if (file
->f_flags
& O_SYNC
) {
1229 spin_lock_irq(&runtime
->lock
);
1230 while (runtime
->avail
!= runtime
->buffer_size
) {
1232 unsigned int last_avail
= runtime
->avail
;
1233 init_waitqueue_entry(&wait
, current
);
1234 add_wait_queue(&runtime
->sleep
, &wait
);
1235 set_current_state(TASK_INTERRUPTIBLE
);
1236 spin_unlock_irq(&runtime
->lock
);
1237 timeout
= schedule_timeout(30 * HZ
);
1238 remove_wait_queue(&runtime
->sleep
, &wait
);
1239 if (signal_pending(current
))
1240 return result
> 0 ? result
: -ERESTARTSYS
;
1241 if (runtime
->avail
== last_avail
&& !timeout
)
1242 return result
> 0 ? result
: -EIO
;
1243 spin_lock_irq(&runtime
->lock
);
1245 spin_unlock_irq(&runtime
->lock
);
1250 static unsigned int snd_rawmidi_poll(struct file
*file
, poll_table
* wait
)
1252 snd_rawmidi_file_t
*rfile
;
1253 snd_rawmidi_runtime_t
*runtime
;
1256 rfile
= file
->private_data
;
1257 if (rfile
->input
!= NULL
) {
1258 runtime
= rfile
->input
->runtime
;
1259 runtime
->trigger
= 1;
1260 rfile
->input
->ops
->trigger(rfile
->input
, 1);
1261 poll_wait(file
, &runtime
->sleep
, wait
);
1263 if (rfile
->output
!= NULL
) {
1264 runtime
= rfile
->output
->runtime
;
1265 poll_wait(file
, &runtime
->sleep
, wait
);
1268 if (rfile
->input
!= NULL
) {
1269 if (snd_rawmidi_ready(rfile
->input
))
1270 mask
|= POLLIN
| POLLRDNORM
;
1272 if (rfile
->output
!= NULL
) {
1273 if (snd_rawmidi_ready(rfile
->output
))
1274 mask
|= POLLOUT
| POLLWRNORM
;
1283 static void snd_rawmidi_proc_info_read(snd_info_entry_t
*entry
,
1284 snd_info_buffer_t
* buffer
)
1286 snd_rawmidi_t
*rmidi
;
1287 snd_rawmidi_substream_t
*substream
;
1288 snd_rawmidi_runtime_t
*runtime
;
1289 struct list_head
*list
;
1291 rmidi
= entry
->private_data
;
1292 snd_iprintf(buffer
, "%s\n\n", rmidi
->name
);
1293 down(&rmidi
->open_mutex
);
1294 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) {
1295 list_for_each(list
, &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
) {
1296 substream
= list_entry(list
, snd_rawmidi_substream_t
, list
);
1299 " Tx bytes : %lu\n",
1301 (unsigned long) substream
->bytes
);
1302 if (substream
->opened
) {
1303 runtime
= substream
->runtime
;
1306 " Buffer size : %lu\n"
1308 runtime
->oss
? "OSS compatible" : "native",
1309 (unsigned long) runtime
->buffer_size
,
1310 (unsigned long) runtime
->avail
);
1314 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
) {
1315 list_for_each(list
, &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
) {
1316 substream
= list_entry(list
, snd_rawmidi_substream_t
, list
);
1319 " Rx bytes : %lu\n",
1321 (unsigned long) substream
->bytes
);
1322 if (substream
->opened
) {
1323 runtime
= substream
->runtime
;
1325 " Buffer size : %lu\n"
1327 " Overruns : %lu\n",
1328 (unsigned long) runtime
->buffer_size
,
1329 (unsigned long) runtime
->avail
,
1330 (unsigned long) runtime
->xruns
);
1334 up(&rmidi
->open_mutex
);
1338 * Register functions
1341 static struct file_operations snd_rawmidi_f_ops
=
1343 .owner
= THIS_MODULE
,
1344 .read
= snd_rawmidi_read
,
1345 .write
= snd_rawmidi_write
,
1346 .open
= snd_rawmidi_open
,
1347 .release
= snd_rawmidi_release
,
1348 .poll
= snd_rawmidi_poll
,
1349 .ioctl
= snd_rawmidi_ioctl
,
1352 static snd_minor_t snd_rawmidi_reg
=
1354 .comment
= "raw midi",
1355 .f_ops
= &snd_rawmidi_f_ops
,
1358 static int snd_rawmidi_alloc_substreams(snd_rawmidi_t
*rmidi
,
1359 snd_rawmidi_str_t
*stream
,
1363 snd_rawmidi_substream_t
*substream
;
1366 INIT_LIST_HEAD(&stream
->substreams
);
1367 for (idx
= 0; idx
< count
; idx
++) {
1368 substream
= kcalloc(1, sizeof(*substream
), GFP_KERNEL
);
1369 if (substream
== NULL
)
1371 substream
->stream
= direction
;
1372 substream
->number
= idx
;
1373 substream
->rmidi
= rmidi
;
1374 substream
->pstr
= stream
;
1375 list_add_tail(&substream
->list
, &stream
->substreams
);
1376 stream
->substream_count
++;
1382 * snd_rawmidi_new - create a rawmidi instance
1383 * @card: the card instance
1384 * @id: the id string
1385 * @device: the device index
1386 * @output_count: the number of output streams
1387 * @input_count: the number of input streams
1388 * @rrawmidi: the pointer to store the new rawmidi instance
1390 * Creates a new rawmidi instance.
1391 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1393 * Returns zero if successful, or a negative error code on failure.
1395 int snd_rawmidi_new(snd_card_t
* card
, char *id
, int device
,
1396 int output_count
, int input_count
,
1397 snd_rawmidi_t
** rrawmidi
)
1399 snd_rawmidi_t
*rmidi
;
1401 static snd_device_ops_t ops
= {
1402 .dev_free
= snd_rawmidi_dev_free
,
1403 .dev_register
= snd_rawmidi_dev_register
,
1404 .dev_disconnect
= snd_rawmidi_dev_disconnect
,
1405 .dev_unregister
= snd_rawmidi_dev_unregister
1408 snd_assert(rrawmidi
!= NULL
, return -EINVAL
);
1410 snd_assert(card
!= NULL
, return -ENXIO
);
1411 rmidi
= kcalloc(1, sizeof(*rmidi
), GFP_KERNEL
);
1415 rmidi
->device
= device
;
1416 init_MUTEX(&rmidi
->open_mutex
);
1417 init_waitqueue_head(&rmidi
->open_wait
);
1419 strlcpy(rmidi
->id
, id
, sizeof(rmidi
->id
));
1420 if ((err
= snd_rawmidi_alloc_substreams(rmidi
, &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
], SNDRV_RAWMIDI_STREAM_INPUT
, input_count
)) < 0) {
1421 snd_rawmidi_free(rmidi
);
1424 if ((err
= snd_rawmidi_alloc_substreams(rmidi
, &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
], SNDRV_RAWMIDI_STREAM_OUTPUT
, output_count
)) < 0) {
1425 snd_rawmidi_free(rmidi
);
1428 if ((err
= snd_device_new(card
, SNDRV_DEV_RAWMIDI
, rmidi
, &ops
)) < 0) {
1429 snd_rawmidi_free(rmidi
);
1436 static void snd_rawmidi_free_substreams(snd_rawmidi_str_t
*stream
)
1438 snd_rawmidi_substream_t
*substream
;
1440 while (!list_empty(&stream
->substreams
)) {
1441 substream
= list_entry(stream
->substreams
.next
, snd_rawmidi_substream_t
, list
);
1442 list_del(&substream
->list
);
1447 static int snd_rawmidi_free(snd_rawmidi_t
*rmidi
)
1449 snd_assert(rmidi
!= NULL
, return -ENXIO
);
1450 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
1451 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
1452 if (rmidi
->private_free
)
1453 rmidi
->private_free(rmidi
);
1458 static int snd_rawmidi_dev_free(snd_device_t
*device
)
1460 snd_rawmidi_t
*rmidi
= device
->device_data
;
1461 return snd_rawmidi_free(rmidi
);
1464 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1465 static void snd_rawmidi_dev_seq_free(snd_seq_device_t
*device
)
1467 snd_rawmidi_t
*rmidi
= device
->private_data
;
1468 rmidi
->seq_dev
= NULL
;
1472 static int snd_rawmidi_dev_register(snd_device_t
*device
)
1475 snd_info_entry_t
*entry
;
1477 snd_rawmidi_t
*rmidi
= device
->device_data
;
1479 if (rmidi
->device
>= SNDRV_RAWMIDI_DEVICES
)
1481 down(®ister_mutex
);
1482 idx
= (rmidi
->card
->number
* SNDRV_RAWMIDI_DEVICES
) + rmidi
->device
;
1483 if (snd_rawmidi_devices
[idx
] != NULL
) {
1484 up(®ister_mutex
);
1487 snd_rawmidi_devices
[idx
] = rmidi
;
1488 sprintf(name
, "midiC%iD%i", rmidi
->card
->number
, rmidi
->device
);
1489 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI
,
1490 rmidi
->card
, rmidi
->device
,
1491 &snd_rawmidi_reg
, name
)) < 0) {
1492 snd_printk(KERN_ERR
"unable to register rawmidi device %i:%i\n", rmidi
->card
->number
, rmidi
->device
);
1493 snd_rawmidi_devices
[idx
] = NULL
;
1494 up(®ister_mutex
);
1497 if (rmidi
->ops
&& rmidi
->ops
->dev_register
&&
1498 (err
= rmidi
->ops
->dev_register(rmidi
)) < 0) {
1499 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1500 snd_rawmidi_devices
[idx
] = NULL
;
1501 up(®ister_mutex
);
1504 #ifdef CONFIG_SND_OSSEMUL
1506 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1507 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1508 rmidi
->card
, 0, &snd_rawmidi_reg
, name
) < 0) {
1509 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 0);
1512 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1513 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
, rmidi
->name
);
1517 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
]) {
1518 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1519 rmidi
->card
, 1, &snd_rawmidi_reg
, name
) < 0) {
1520 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 1);
1525 #endif /* CONFIG_SND_OSSEMUL */
1526 up(®ister_mutex
);
1527 sprintf(name
, "midi%d", rmidi
->device
);
1528 entry
= snd_info_create_card_entry(rmidi
->card
, name
, rmidi
->card
->proc_root
);
1530 entry
->private_data
= rmidi
;
1531 entry
->c
.text
.read_size
= 1024;
1532 entry
->c
.text
.read
= snd_rawmidi_proc_info_read
;
1533 if (snd_info_register(entry
) < 0) {
1534 snd_info_free_entry(entry
);
1538 rmidi
->proc_entry
= entry
;
1539 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1540 if (!rmidi
->ops
|| !rmidi
->ops
->dev_register
) { /* own registration mechanism */
1541 if (snd_seq_device_new(rmidi
->card
, rmidi
->device
, SNDRV_SEQ_DEV_ID_MIDISYNTH
, 0, &rmidi
->seq_dev
) >= 0) {
1542 rmidi
->seq_dev
->private_data
= rmidi
;
1543 rmidi
->seq_dev
->private_free
= snd_rawmidi_dev_seq_free
;
1544 sprintf(rmidi
->seq_dev
->name
, "MIDI %d-%d", rmidi
->card
->number
, rmidi
->device
);
1545 snd_device_register(rmidi
->card
, rmidi
->seq_dev
);
1552 static int snd_rawmidi_dev_disconnect(snd_device_t
*device
)
1554 snd_rawmidi_t
*rmidi
= device
->device_data
;
1557 down(®ister_mutex
);
1558 idx
= (rmidi
->card
->number
* SNDRV_RAWMIDI_DEVICES
) + rmidi
->device
;
1559 snd_rawmidi_devices
[idx
] = NULL
;
1560 up(®ister_mutex
);
1564 static int snd_rawmidi_dev_unregister(snd_device_t
*device
)
1567 snd_rawmidi_t
*rmidi
= device
->device_data
;
1569 snd_assert(rmidi
!= NULL
, return -ENXIO
);
1570 down(®ister_mutex
);
1571 idx
= (rmidi
->card
->number
* SNDRV_RAWMIDI_DEVICES
) + rmidi
->device
;
1572 snd_rawmidi_devices
[idx
] = NULL
;
1573 if (rmidi
->proc_entry
) {
1574 snd_info_unregister(rmidi
->proc_entry
);
1575 rmidi
->proc_entry
= NULL
;
1577 #ifdef CONFIG_SND_OSSEMUL
1578 if (rmidi
->ossreg
) {
1579 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1580 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 0);
1581 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1582 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
);
1585 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
])
1586 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 1);
1589 #endif /* CONFIG_SND_OSSEMUL */
1590 if (rmidi
->ops
&& rmidi
->ops
->dev_unregister
)
1591 rmidi
->ops
->dev_unregister(rmidi
);
1592 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1593 up(®ister_mutex
);
1594 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1595 if (rmidi
->seq_dev
) {
1596 snd_device_free(rmidi
->card
, rmidi
->seq_dev
);
1597 rmidi
->seq_dev
= NULL
;
1600 return snd_rawmidi_free(rmidi
);
1604 * snd_rawmidi_set_ops - set the rawmidi operators
1605 * @rmidi: the rawmidi instance
1606 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1607 * @ops: the operator table
1609 * Sets the rawmidi operators for the given stream direction.
1611 void snd_rawmidi_set_ops(snd_rawmidi_t
*rmidi
, int stream
, snd_rawmidi_ops_t
*ops
)
1613 struct list_head
*list
;
1614 snd_rawmidi_substream_t
*substream
;
1616 list_for_each(list
, &rmidi
->streams
[stream
].substreams
) {
1617 substream
= list_entry(list
, snd_rawmidi_substream_t
, list
);
1618 substream
->ops
= ops
;
1626 static int __init
alsa_rawmidi_init(void)
1629 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl
);
1630 #ifdef CONFIG_SND_OSSEMUL
1632 /* check device map table */
1633 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1634 if (midi_map
[i
] < 0 || midi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1635 snd_printk(KERN_ERR
"invalid midi_map[%d] = %d\n", i
, midi_map
[i
]);
1638 if (amidi_map
[i
] < 0 || amidi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1639 snd_printk(KERN_ERR
"invalid amidi_map[%d] = %d\n", i
, amidi_map
[i
]);
1644 #endif /* CONFIG_SND_OSSEMUL */
1648 static void __exit
alsa_rawmidi_exit(void)
1650 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl
);
1653 module_init(alsa_rawmidi_init
)
1654 module_exit(alsa_rawmidi_exit
)
1656 EXPORT_SYMBOL(snd_rawmidi_output_params
);
1657 EXPORT_SYMBOL(snd_rawmidi_input_params
);
1658 EXPORT_SYMBOL(snd_rawmidi_drop_output
);
1659 EXPORT_SYMBOL(snd_rawmidi_drain_output
);
1660 EXPORT_SYMBOL(snd_rawmidi_drain_input
);
1661 EXPORT_SYMBOL(snd_rawmidi_receive
);
1662 EXPORT_SYMBOL(snd_rawmidi_transmit_empty
);
1663 EXPORT_SYMBOL(snd_rawmidi_transmit_peek
);
1664 EXPORT_SYMBOL(snd_rawmidi_transmit_ack
);
1665 EXPORT_SYMBOL(snd_rawmidi_transmit
);
1666 EXPORT_SYMBOL(snd_rawmidi_new
);
1667 EXPORT_SYMBOL(snd_rawmidi_set_ops
);
1668 EXPORT_SYMBOL(snd_rawmidi_info
);
1669 EXPORT_SYMBOL(snd_rawmidi_info_select
);
1670 EXPORT_SYMBOL(snd_rawmidi_kernel_open
);
1671 EXPORT_SYMBOL(snd_rawmidi_kernel_release
);
1672 EXPORT_SYMBOL(snd_rawmidi_kernel_read
);
1673 EXPORT_SYMBOL(snd_rawmidi_kernel_write
);