2 * Abstract layer for MIDI v1.0 stream
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/core.h>
23 #include <linux/major.h>
24 #include <linux/init.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/wait.h>
29 #include <linux/mutex.h>
30 #include <linux/moduleparam.h>
31 #include <linux/delay.h>
32 #include <sound/rawmidi.h>
33 #include <sound/info.h>
34 #include <sound/control.h>
35 #include <sound/minors.h>
36 #include <sound/initval.h>
38 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
39 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
40 MODULE_LICENSE("GPL");
42 #ifdef CONFIG_SND_OSSEMUL
43 static int midi_map
[SNDRV_CARDS
];
44 static int amidi_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
45 module_param_array(midi_map
, int, NULL
, 0444);
46 MODULE_PARM_DESC(midi_map
, "Raw MIDI device number assigned to 1st OSS device.");
47 module_param_array(amidi_map
, int, NULL
, 0444);
48 MODULE_PARM_DESC(amidi_map
, "Raw MIDI device number assigned to 2nd OSS device.");
49 #endif /* CONFIG_SND_OSSEMUL */
51 static int snd_rawmidi_free(struct snd_rawmidi
*rawmidi
);
52 static int snd_rawmidi_dev_free(struct snd_device
*device
);
53 static int snd_rawmidi_dev_register(struct snd_device
*device
);
54 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
);
56 static LIST_HEAD(snd_rawmidi_devices
);
57 static DEFINE_MUTEX(register_mutex
);
59 static struct snd_rawmidi
*snd_rawmidi_search(struct snd_card
*card
, int device
)
61 struct snd_rawmidi
*rawmidi
;
63 list_for_each_entry(rawmidi
, &snd_rawmidi_devices
, list
)
64 if (rawmidi
->card
== card
&& rawmidi
->device
== device
)
69 static inline unsigned short snd_rawmidi_file_flags(struct file
*file
)
71 switch (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) {
73 return SNDRV_RAWMIDI_LFLG_OUTPUT
;
75 return SNDRV_RAWMIDI_LFLG_INPUT
;
77 return SNDRV_RAWMIDI_LFLG_OPEN
;
81 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream
*substream
)
83 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
84 return runtime
->avail
>= runtime
->avail_min
;
87 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream
*substream
,
90 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
91 return runtime
->avail
>= runtime
->avail_min
&&
92 (!substream
->append
|| runtime
->avail
>= count
);
95 static void snd_rawmidi_input_event_tasklet(unsigned long data
)
97 struct snd_rawmidi_substream
*substream
= (struct snd_rawmidi_substream
*)data
;
98 substream
->runtime
->event(substream
);
101 static void snd_rawmidi_output_trigger_tasklet(unsigned long data
)
103 struct snd_rawmidi_substream
*substream
= (struct snd_rawmidi_substream
*)data
;
104 substream
->ops
->trigger(substream
, 1);
107 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream
*substream
)
109 struct snd_rawmidi_runtime
*runtime
;
111 if ((runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
)) == NULL
)
113 spin_lock_init(&runtime
->lock
);
114 init_waitqueue_head(&runtime
->sleep
);
115 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
116 tasklet_init(&runtime
->tasklet
,
117 snd_rawmidi_input_event_tasklet
,
118 (unsigned long)substream
);
120 tasklet_init(&runtime
->tasklet
,
121 snd_rawmidi_output_trigger_tasklet
,
122 (unsigned long)substream
);
123 runtime
->event
= NULL
;
124 runtime
->buffer_size
= PAGE_SIZE
;
125 runtime
->avail_min
= 1;
126 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
129 runtime
->avail
= runtime
->buffer_size
;
130 if ((runtime
->buffer
= kmalloc(runtime
->buffer_size
, GFP_KERNEL
)) == NULL
) {
134 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
135 substream
->runtime
= runtime
;
139 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream
*substream
)
141 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
143 kfree(runtime
->buffer
);
145 substream
->runtime
= NULL
;
149 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream
*substream
,int up
)
151 if (!substream
->opened
)
154 tasklet_schedule(&substream
->runtime
->tasklet
);
156 tasklet_kill(&substream
->runtime
->tasklet
);
157 substream
->ops
->trigger(substream
, 0);
161 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
163 if (!substream
->opened
)
165 substream
->ops
->trigger(substream
, up
);
166 if (!up
&& substream
->runtime
->event
)
167 tasklet_kill(&substream
->runtime
->tasklet
);
170 int snd_rawmidi_drop_output(struct snd_rawmidi_substream
*substream
)
173 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
175 snd_rawmidi_output_trigger(substream
, 0);
177 spin_lock_irqsave(&runtime
->lock
, flags
);
178 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
179 runtime
->avail
= runtime
->buffer_size
;
180 spin_unlock_irqrestore(&runtime
->lock
, flags
);
184 int snd_rawmidi_drain_output(struct snd_rawmidi_substream
*substream
)
188 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
192 timeout
= wait_event_interruptible_timeout(runtime
->sleep
,
193 (runtime
->avail
>= runtime
->buffer_size
),
195 if (signal_pending(current
))
197 if (runtime
->avail
< runtime
->buffer_size
&& !timeout
) {
198 snd_printk(KERN_WARNING
"rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime
->avail
, (long)runtime
->buffer_size
);
202 if (err
!= -ERESTARTSYS
) {
203 /* we need wait a while to make sure that Tx FIFOs are empty */
204 if (substream
->ops
->drain
)
205 substream
->ops
->drain(substream
);
208 snd_rawmidi_drop_output(substream
);
213 int snd_rawmidi_drain_input(struct snd_rawmidi_substream
*substream
)
216 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
218 snd_rawmidi_input_trigger(substream
, 0);
220 spin_lock_irqsave(&runtime
->lock
, flags
);
221 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
223 spin_unlock_irqrestore(&runtime
->lock
, flags
);
227 /* look for an available substream for the given stream direction;
228 * if a specific subdevice is given, try to assign it
230 static int assign_substream(struct snd_rawmidi
*rmidi
, int subdevice
,
231 int stream
, int mode
,
232 struct snd_rawmidi_substream
**sub_ret
)
234 struct snd_rawmidi_substream
*substream
;
235 struct snd_rawmidi_str
*s
= &rmidi
->streams
[stream
];
236 static unsigned int info_flags
[2] = {
237 [SNDRV_RAWMIDI_STREAM_OUTPUT
] = SNDRV_RAWMIDI_INFO_OUTPUT
,
238 [SNDRV_RAWMIDI_STREAM_INPUT
] = SNDRV_RAWMIDI_INFO_INPUT
,
241 if (!(rmidi
->info_flags
& info_flags
[stream
]))
243 if (subdevice
>= 0 && subdevice
>= s
->substream_count
)
245 if (s
->substream_opened
>= s
->substream_count
)
248 list_for_each_entry(substream
, &s
->substreams
, list
) {
249 if (substream
->opened
) {
250 if (stream
== SNDRV_RAWMIDI_STREAM_INPUT
||
251 !(mode
& SNDRV_RAWMIDI_LFLG_APPEND
) ||
255 if (subdevice
< 0 || subdevice
== substream
->number
) {
256 *sub_ret
= substream
;
263 /* open and do ref-counting for the given substream */
264 static int open_substream(struct snd_rawmidi
*rmidi
,
265 struct snd_rawmidi_substream
*substream
,
270 if (substream
->use_count
== 0) {
271 err
= snd_rawmidi_runtime_create(substream
);
274 err
= substream
->ops
->open(substream
);
276 snd_rawmidi_runtime_free(substream
);
279 substream
->opened
= 1;
280 substream
->active_sensing
= 0;
281 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
)
282 substream
->append
= 1;
284 substream
->use_count
++;
285 rmidi
->streams
[substream
->stream
].substream_opened
++;
289 static void close_substream(struct snd_rawmidi
*rmidi
,
290 struct snd_rawmidi_substream
*substream
,
293 static int rawmidi_open_priv(struct snd_rawmidi
*rmidi
, int subdevice
, int mode
,
294 struct snd_rawmidi_file
*rfile
)
296 struct snd_rawmidi_substream
*sinput
= NULL
, *soutput
= NULL
;
299 rfile
->input
= rfile
->output
= NULL
;
300 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
301 err
= assign_substream(rmidi
, subdevice
,
302 SNDRV_RAWMIDI_STREAM_INPUT
,
307 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
308 err
= assign_substream(rmidi
, subdevice
,
309 SNDRV_RAWMIDI_STREAM_OUTPUT
,
316 err
= open_substream(rmidi
, sinput
, mode
);
321 err
= open_substream(rmidi
, soutput
, mode
);
324 close_substream(rmidi
, sinput
, 0);
329 rfile
->rmidi
= rmidi
;
330 rfile
->input
= sinput
;
331 rfile
->output
= soutput
;
335 /* called from sound/core/seq/seq_midi.c */
336 int snd_rawmidi_kernel_open(struct snd_card
*card
, int device
, int subdevice
,
337 int mode
, struct snd_rawmidi_file
* rfile
)
339 struct snd_rawmidi
*rmidi
;
342 if (snd_BUG_ON(!rfile
))
345 mutex_lock(®ister_mutex
);
346 rmidi
= snd_rawmidi_search(card
, device
);
348 mutex_unlock(®ister_mutex
);
351 if (!try_module_get(rmidi
->card
->module
)) {
352 mutex_unlock(®ister_mutex
);
355 mutex_unlock(®ister_mutex
);
357 mutex_lock(&rmidi
->open_mutex
);
358 err
= rawmidi_open_priv(rmidi
, subdevice
, mode
, rfile
);
359 mutex_unlock(&rmidi
->open_mutex
);
361 module_put(rmidi
->card
->module
);
365 static int snd_rawmidi_open(struct inode
*inode
, struct file
*file
)
367 int maj
= imajor(inode
);
368 struct snd_card
*card
;
370 unsigned short fflags
;
372 struct snd_rawmidi
*rmidi
;
373 struct snd_rawmidi_file
*rawmidi_file
= NULL
;
375 struct snd_ctl_file
*kctl
;
377 if ((file
->f_flags
& O_APPEND
) && !(file
->f_flags
& O_NONBLOCK
))
378 return -EINVAL
; /* invalid combination */
380 if (maj
== snd_major
) {
381 rmidi
= snd_lookup_minor_data(iminor(inode
),
382 SNDRV_DEVICE_TYPE_RAWMIDI
);
383 #ifdef CONFIG_SND_OSSEMUL
384 } else if (maj
== SOUND_MAJOR
) {
385 rmidi
= snd_lookup_oss_minor_data(iminor(inode
),
386 SNDRV_OSS_DEVICE_TYPE_MIDI
);
394 if (!try_module_get(rmidi
->card
->module
))
397 mutex_lock(&rmidi
->open_mutex
);
399 err
= snd_card_file_add(card
, file
);
402 fflags
= snd_rawmidi_file_flags(file
);
403 if ((file
->f_flags
& O_APPEND
) || maj
== SOUND_MAJOR
) /* OSS emul? */
404 fflags
|= SNDRV_RAWMIDI_LFLG_APPEND
;
405 rawmidi_file
= kmalloc(sizeof(*rawmidi_file
), GFP_KERNEL
);
406 if (rawmidi_file
== NULL
) {
410 init_waitqueue_entry(&wait
, current
);
411 add_wait_queue(&rmidi
->open_wait
, &wait
);
414 read_lock(&card
->ctl_files_rwlock
);
415 list_for_each_entry(kctl
, &card
->ctl_files
, list
) {
416 if (kctl
->pid
== current
->pid
) {
417 subdevice
= kctl
->prefer_rawmidi_subdevice
;
422 read_unlock(&card
->ctl_files_rwlock
);
423 err
= rawmidi_open_priv(rmidi
, subdevice
, fflags
, rawmidi_file
);
426 if (err
== -EAGAIN
) {
427 if (file
->f_flags
& O_NONBLOCK
) {
433 set_current_state(TASK_INTERRUPTIBLE
);
434 mutex_unlock(&rmidi
->open_mutex
);
436 mutex_lock(&rmidi
->open_mutex
);
437 if (signal_pending(current
)) {
442 remove_wait_queue(&rmidi
->open_wait
, &wait
);
447 #ifdef CONFIG_SND_OSSEMUL
448 if (rawmidi_file
->input
&& rawmidi_file
->input
->runtime
)
449 rawmidi_file
->input
->runtime
->oss
= (maj
== SOUND_MAJOR
);
450 if (rawmidi_file
->output
&& rawmidi_file
->output
->runtime
)
451 rawmidi_file
->output
->runtime
->oss
= (maj
== SOUND_MAJOR
);
453 file
->private_data
= rawmidi_file
;
454 mutex_unlock(&rmidi
->open_mutex
);
458 snd_card_file_remove(card
, file
);
460 mutex_unlock(&rmidi
->open_mutex
);
461 module_put(rmidi
->card
->module
);
465 static void close_substream(struct snd_rawmidi
*rmidi
,
466 struct snd_rawmidi_substream
*substream
,
469 rmidi
->streams
[substream
->stream
].substream_opened
--;
470 if (--substream
->use_count
)
474 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
475 snd_rawmidi_input_trigger(substream
, 0);
477 if (substream
->active_sensing
) {
478 unsigned char buf
= 0xfe;
479 /* sending single active sensing message
480 * to shut the device up
482 snd_rawmidi_kernel_write(substream
, &buf
, 1);
484 if (snd_rawmidi_drain_output(substream
) == -ERESTARTSYS
)
485 snd_rawmidi_output_trigger(substream
, 0);
488 substream
->ops
->close(substream
);
489 if (substream
->runtime
->private_free
)
490 substream
->runtime
->private_free(substream
);
491 snd_rawmidi_runtime_free(substream
);
492 substream
->opened
= 0;
493 substream
->append
= 0;
496 static void rawmidi_release_priv(struct snd_rawmidi_file
*rfile
)
498 struct snd_rawmidi
*rmidi
;
500 rmidi
= rfile
->rmidi
;
501 mutex_lock(&rmidi
->open_mutex
);
503 close_substream(rmidi
, rfile
->input
, 1);
507 close_substream(rmidi
, rfile
->output
, 1);
508 rfile
->output
= NULL
;
511 mutex_unlock(&rmidi
->open_mutex
);
512 wake_up(&rmidi
->open_wait
);
515 /* called from sound/core/seq/seq_midi.c */
516 int snd_rawmidi_kernel_release(struct snd_rawmidi_file
*rfile
)
518 struct snd_rawmidi
*rmidi
;
520 if (snd_BUG_ON(!rfile
))
523 rmidi
= rfile
->rmidi
;
524 rawmidi_release_priv(rfile
);
525 module_put(rmidi
->card
->module
);
529 static int snd_rawmidi_release(struct inode
*inode
, struct file
*file
)
531 struct snd_rawmidi_file
*rfile
;
532 struct snd_rawmidi
*rmidi
;
533 struct module
*module
;
535 rfile
= file
->private_data
;
536 rmidi
= rfile
->rmidi
;
537 rawmidi_release_priv(rfile
);
539 module
= rmidi
->card
->module
;
540 snd_card_file_remove(rmidi
->card
, file
);
545 static int snd_rawmidi_info(struct snd_rawmidi_substream
*substream
,
546 struct snd_rawmidi_info
*info
)
548 struct snd_rawmidi
*rmidi
;
550 if (substream
== NULL
)
552 rmidi
= substream
->rmidi
;
553 memset(info
, 0, sizeof(*info
));
554 info
->card
= rmidi
->card
->number
;
555 info
->device
= rmidi
->device
;
556 info
->subdevice
= substream
->number
;
557 info
->stream
= substream
->stream
;
558 info
->flags
= rmidi
->info_flags
;
559 strcpy(info
->id
, rmidi
->id
);
560 strcpy(info
->name
, rmidi
->name
);
561 strcpy(info
->subname
, substream
->name
);
562 info
->subdevices_count
= substream
->pstr
->substream_count
;
563 info
->subdevices_avail
= (substream
->pstr
->substream_count
-
564 substream
->pstr
->substream_opened
);
568 static int snd_rawmidi_info_user(struct snd_rawmidi_substream
*substream
,
569 struct snd_rawmidi_info __user
* _info
)
571 struct snd_rawmidi_info info
;
573 if ((err
= snd_rawmidi_info(substream
, &info
)) < 0)
575 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
580 int snd_rawmidi_info_select(struct snd_card
*card
, struct snd_rawmidi_info
*info
)
582 struct snd_rawmidi
*rmidi
;
583 struct snd_rawmidi_str
*pstr
;
584 struct snd_rawmidi_substream
*substream
;
586 mutex_lock(®ister_mutex
);
587 rmidi
= snd_rawmidi_search(card
, info
->device
);
588 mutex_unlock(®ister_mutex
);
591 if (info
->stream
< 0 || info
->stream
> 1)
593 pstr
= &rmidi
->streams
[info
->stream
];
594 if (pstr
->substream_count
== 0)
596 if (info
->subdevice
>= pstr
->substream_count
)
598 list_for_each_entry(substream
, &pstr
->substreams
, list
) {
599 if ((unsigned int)substream
->number
== info
->subdevice
)
600 return snd_rawmidi_info(substream
, info
);
605 static int snd_rawmidi_info_select_user(struct snd_card
*card
,
606 struct snd_rawmidi_info __user
*_info
)
609 struct snd_rawmidi_info info
;
610 if (get_user(info
.device
, &_info
->device
))
612 if (get_user(info
.stream
, &_info
->stream
))
614 if (get_user(info
.subdevice
, &_info
->subdevice
))
616 if ((err
= snd_rawmidi_info_select(card
, &info
)) < 0)
618 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
623 int snd_rawmidi_output_params(struct snd_rawmidi_substream
*substream
,
624 struct snd_rawmidi_params
* params
)
627 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
629 if (substream
->append
&& substream
->use_count
> 1)
631 snd_rawmidi_drain_output(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 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
642 kfree(runtime
->buffer
);
643 runtime
->buffer
= newbuf
;
644 runtime
->buffer_size
= params
->buffer_size
;
645 runtime
->avail
= runtime
->buffer_size
;
647 runtime
->avail_min
= params
->avail_min
;
648 substream
->active_sensing
= !params
->no_active_sensing
;
652 int snd_rawmidi_input_params(struct snd_rawmidi_substream
*substream
,
653 struct snd_rawmidi_params
* params
)
656 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
658 snd_rawmidi_drain_input(substream
);
659 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
662 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
665 if (params
->buffer_size
!= runtime
->buffer_size
) {
666 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
669 kfree(runtime
->buffer
);
670 runtime
->buffer
= newbuf
;
671 runtime
->buffer_size
= params
->buffer_size
;
673 runtime
->avail_min
= params
->avail_min
;
677 static int snd_rawmidi_output_status(struct snd_rawmidi_substream
*substream
,
678 struct snd_rawmidi_status
* status
)
680 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
682 memset(status
, 0, sizeof(*status
));
683 status
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
684 spin_lock_irq(&runtime
->lock
);
685 status
->avail
= runtime
->avail
;
686 spin_unlock_irq(&runtime
->lock
);
690 static int snd_rawmidi_input_status(struct snd_rawmidi_substream
*substream
,
691 struct snd_rawmidi_status
* status
)
693 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
695 memset(status
, 0, sizeof(*status
));
696 status
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
697 spin_lock_irq(&runtime
->lock
);
698 status
->avail
= runtime
->avail
;
699 status
->xruns
= runtime
->xruns
;
701 spin_unlock_irq(&runtime
->lock
);
705 static long snd_rawmidi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
707 struct snd_rawmidi_file
*rfile
;
708 void __user
*argp
= (void __user
*)arg
;
710 rfile
= file
->private_data
;
711 if (((cmd
>> 8) & 0xff) != 'W')
714 case SNDRV_RAWMIDI_IOCTL_PVERSION
:
715 return put_user(SNDRV_RAWMIDI_VERSION
, (int __user
*)argp
) ? -EFAULT
: 0;
716 case SNDRV_RAWMIDI_IOCTL_INFO
:
719 struct snd_rawmidi_info __user
*info
= argp
;
720 if (get_user(stream
, &info
->stream
))
723 case SNDRV_RAWMIDI_STREAM_INPUT
:
724 return snd_rawmidi_info_user(rfile
->input
, info
);
725 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
726 return snd_rawmidi_info_user(rfile
->output
, info
);
731 case SNDRV_RAWMIDI_IOCTL_PARAMS
:
733 struct snd_rawmidi_params params
;
734 if (copy_from_user(¶ms
, argp
, sizeof(struct snd_rawmidi_params
)))
736 switch (params
.stream
) {
737 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
738 if (rfile
->output
== NULL
)
740 return snd_rawmidi_output_params(rfile
->output
, ¶ms
);
741 case SNDRV_RAWMIDI_STREAM_INPUT
:
742 if (rfile
->input
== NULL
)
744 return snd_rawmidi_input_params(rfile
->input
, ¶ms
);
749 case SNDRV_RAWMIDI_IOCTL_STATUS
:
752 struct snd_rawmidi_status status
;
753 if (copy_from_user(&status
, argp
, sizeof(struct snd_rawmidi_status
)))
755 switch (status
.stream
) {
756 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
757 if (rfile
->output
== NULL
)
759 err
= snd_rawmidi_output_status(rfile
->output
, &status
);
761 case SNDRV_RAWMIDI_STREAM_INPUT
:
762 if (rfile
->input
== NULL
)
764 err
= snd_rawmidi_input_status(rfile
->input
, &status
);
771 if (copy_to_user(argp
, &status
, sizeof(struct snd_rawmidi_status
)))
775 case SNDRV_RAWMIDI_IOCTL_DROP
:
778 if (get_user(val
, (int __user
*) argp
))
781 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
782 if (rfile
->output
== NULL
)
784 return snd_rawmidi_drop_output(rfile
->output
);
789 case SNDRV_RAWMIDI_IOCTL_DRAIN
:
792 if (get_user(val
, (int __user
*) argp
))
795 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
796 if (rfile
->output
== NULL
)
798 return snd_rawmidi_drain_output(rfile
->output
);
799 case SNDRV_RAWMIDI_STREAM_INPUT
:
800 if (rfile
->input
== NULL
)
802 return snd_rawmidi_drain_input(rfile
->input
);
807 #ifdef CONFIG_SND_DEBUG
809 snd_printk(KERN_WARNING
"rawmidi: unknown command = 0x%x\n", cmd
);
815 static int snd_rawmidi_control_ioctl(struct snd_card
*card
,
816 struct snd_ctl_file
*control
,
820 void __user
*argp
= (void __user
*)arg
;
823 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE
:
827 if (get_user(device
, (int __user
*)argp
))
829 mutex_lock(®ister_mutex
);
830 device
= device
< 0 ? 0 : device
+ 1;
831 while (device
< SNDRV_RAWMIDI_DEVICES
) {
832 if (snd_rawmidi_search(card
, device
))
836 if (device
== SNDRV_RAWMIDI_DEVICES
)
838 mutex_unlock(®ister_mutex
);
839 if (put_user(device
, (int __user
*)argp
))
843 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE
:
847 if (get_user(val
, (int __user
*)argp
))
849 control
->prefer_rawmidi_subdevice
= val
;
852 case SNDRV_CTL_IOCTL_RAWMIDI_INFO
:
853 return snd_rawmidi_info_select_user(card
, argp
);
859 * snd_rawmidi_receive - receive the input data from the device
860 * @substream: the rawmidi substream
861 * @buffer: the buffer pointer
862 * @count: the data size to read
864 * Reads the data from the internal buffer.
866 * Returns the size of read data, or a negative error code on failure.
868 int snd_rawmidi_receive(struct snd_rawmidi_substream
*substream
,
869 const unsigned char *buffer
, int count
)
872 int result
= 0, count1
;
873 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
875 if (!substream
->opened
)
877 if (runtime
->buffer
== NULL
) {
878 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
881 spin_lock_irqsave(&runtime
->lock
, flags
);
882 if (count
== 1) { /* special case, faster code */
884 if (runtime
->avail
< runtime
->buffer_size
) {
885 runtime
->buffer
[runtime
->hw_ptr
++] = buffer
[0];
886 runtime
->hw_ptr
%= runtime
->buffer_size
;
893 substream
->bytes
+= count
;
894 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
897 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
898 count1
= runtime
->buffer_size
- runtime
->avail
;
899 memcpy(runtime
->buffer
+ runtime
->hw_ptr
, buffer
, count1
);
900 runtime
->hw_ptr
+= count1
;
901 runtime
->hw_ptr
%= runtime
->buffer_size
;
902 runtime
->avail
+= count1
;
908 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
)) {
909 count1
= runtime
->buffer_size
- runtime
->avail
;
910 runtime
->xruns
+= count
- count1
;
913 memcpy(runtime
->buffer
, buffer
, count1
);
914 runtime
->hw_ptr
= count1
;
915 runtime
->avail
+= count1
;
922 tasklet_schedule(&runtime
->tasklet
);
923 else if (snd_rawmidi_ready(substream
))
924 wake_up(&runtime
->sleep
);
926 spin_unlock_irqrestore(&runtime
->lock
, flags
);
930 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream
*substream
,
931 unsigned char __user
*userbuf
,
932 unsigned char *kernelbuf
, long count
)
935 long result
= 0, count1
;
936 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
938 while (count
> 0 && runtime
->avail
) {
939 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
942 spin_lock_irqsave(&runtime
->lock
, flags
);
943 if (count1
> (int)runtime
->avail
)
944 count1
= runtime
->avail
;
946 memcpy(kernelbuf
+ result
, runtime
->buffer
+ runtime
->appl_ptr
, count1
);
948 spin_unlock_irqrestore(&runtime
->lock
, flags
);
949 if (copy_to_user(userbuf
+ result
,
950 runtime
->buffer
+ runtime
->appl_ptr
, count1
)) {
951 return result
> 0 ? result
: -EFAULT
;
953 spin_lock_irqsave(&runtime
->lock
, flags
);
955 runtime
->appl_ptr
+= count1
;
956 runtime
->appl_ptr
%= runtime
->buffer_size
;
957 runtime
->avail
-= count1
;
958 spin_unlock_irqrestore(&runtime
->lock
, flags
);
965 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream
*substream
,
966 unsigned char *buf
, long count
)
968 snd_rawmidi_input_trigger(substream
, 1);
969 return snd_rawmidi_kernel_read1(substream
, NULL
/*userbuf*/, buf
, count
);
972 static ssize_t
snd_rawmidi_read(struct file
*file
, char __user
*buf
, size_t count
,
977 struct snd_rawmidi_file
*rfile
;
978 struct snd_rawmidi_substream
*substream
;
979 struct snd_rawmidi_runtime
*runtime
;
981 rfile
= file
->private_data
;
982 substream
= rfile
->input
;
983 if (substream
== NULL
)
985 runtime
= substream
->runtime
;
986 snd_rawmidi_input_trigger(substream
, 1);
989 spin_lock_irq(&runtime
->lock
);
990 while (!snd_rawmidi_ready(substream
)) {
992 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
993 spin_unlock_irq(&runtime
->lock
);
994 return result
> 0 ? result
: -EAGAIN
;
996 init_waitqueue_entry(&wait
, current
);
997 add_wait_queue(&runtime
->sleep
, &wait
);
998 set_current_state(TASK_INTERRUPTIBLE
);
999 spin_unlock_irq(&runtime
->lock
);
1001 remove_wait_queue(&runtime
->sleep
, &wait
);
1002 if (signal_pending(current
))
1003 return result
> 0 ? result
: -ERESTARTSYS
;
1004 if (!runtime
->avail
)
1005 return result
> 0 ? result
: -EIO
;
1006 spin_lock_irq(&runtime
->lock
);
1008 spin_unlock_irq(&runtime
->lock
);
1009 count1
= snd_rawmidi_kernel_read1(substream
,
1010 (unsigned char __user
*)buf
,
1014 return result
> 0 ? result
: count1
;
1023 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1024 * @substream: the rawmidi substream
1026 * Returns 1 if the internal output buffer is empty, 0 if not.
1028 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream
*substream
)
1030 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1032 unsigned long flags
;
1034 if (runtime
->buffer
== NULL
) {
1035 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1038 spin_lock_irqsave(&runtime
->lock
, flags
);
1039 result
= runtime
->avail
>= runtime
->buffer_size
;
1040 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1045 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1046 * @substream: the rawmidi substream
1047 * @buffer: the buffer pointer
1048 * @count: data size to transfer
1050 * Copies data from the internal output buffer to the given buffer.
1052 * Call this in the interrupt handler when the midi output is ready,
1053 * and call snd_rawmidi_transmit_ack() after the transmission is
1056 * Returns the size of copied data, or a negative error code on failure.
1058 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1059 unsigned char *buffer
, int count
)
1061 unsigned long flags
;
1063 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1065 if (runtime
->buffer
== NULL
) {
1066 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1070 spin_lock_irqsave(&runtime
->lock
, flags
);
1071 if (runtime
->avail
>= runtime
->buffer_size
) {
1072 /* warning: lowlevel layer MUST trigger down the hardware */
1075 if (count
== 1) { /* special case, faster code */
1076 *buffer
= runtime
->buffer
[runtime
->hw_ptr
];
1079 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
1082 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
1083 count1
= runtime
->buffer_size
- runtime
->avail
;
1084 memcpy(buffer
, runtime
->buffer
+ runtime
->hw_ptr
, count1
);
1088 if (count
> (int)(runtime
->buffer_size
- runtime
->avail
- count1
))
1089 count
= runtime
->buffer_size
- runtime
->avail
- count1
;
1090 memcpy(buffer
+ count1
, runtime
->buffer
, count
);
1095 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1100 * snd_rawmidi_transmit_ack - acknowledge the transmission
1101 * @substream: the rawmidi substream
1102 * @count: the tranferred count
1104 * Advances the hardware pointer for the internal output buffer with
1105 * the given size and updates the condition.
1106 * Call after the transmission is finished.
1108 * Returns the advanced size if successful, or a negative error code on failure.
1110 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1112 unsigned long flags
;
1113 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1115 if (runtime
->buffer
== NULL
) {
1116 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1119 spin_lock_irqsave(&runtime
->lock
, flags
);
1120 snd_BUG_ON(runtime
->avail
+ count
> runtime
->buffer_size
);
1121 runtime
->hw_ptr
+= count
;
1122 runtime
->hw_ptr
%= runtime
->buffer_size
;
1123 runtime
->avail
+= count
;
1124 substream
->bytes
+= count
;
1126 if (runtime
->drain
|| snd_rawmidi_ready(substream
))
1127 wake_up(&runtime
->sleep
);
1129 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1134 * snd_rawmidi_transmit - copy from the buffer to the device
1135 * @substream: the rawmidi substream
1136 * @buffer: the buffer pointer
1137 * @count: the data size to transfer
1139 * Copies data from the buffer to the device and advances the pointer.
1141 * Returns the copied size if successful, or a negative error code on failure.
1143 int snd_rawmidi_transmit(struct snd_rawmidi_substream
*substream
,
1144 unsigned char *buffer
, int count
)
1146 if (!substream
->opened
)
1148 count
= snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1151 return snd_rawmidi_transmit_ack(substream
, count
);
1154 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream
*substream
,
1155 const unsigned char __user
*userbuf
,
1156 const unsigned char *kernelbuf
,
1159 unsigned long flags
;
1160 long count1
, result
;
1161 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1163 if (snd_BUG_ON(!kernelbuf
&& !userbuf
))
1165 if (snd_BUG_ON(!runtime
->buffer
))
1169 spin_lock_irqsave(&runtime
->lock
, flags
);
1170 if (substream
->append
) {
1171 if ((long)runtime
->avail
< count
) {
1172 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1176 while (count
> 0 && runtime
->avail
> 0) {
1177 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1180 if (count1
> (long)runtime
->avail
)
1181 count1
= runtime
->avail
;
1183 memcpy(runtime
->buffer
+ runtime
->appl_ptr
,
1184 kernelbuf
+ result
, count1
);
1186 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1187 if (copy_from_user(runtime
->buffer
+ runtime
->appl_ptr
,
1188 userbuf
+ result
, count1
)) {
1189 spin_lock_irqsave(&runtime
->lock
, flags
);
1190 result
= result
> 0 ? result
: -EFAULT
;
1193 spin_lock_irqsave(&runtime
->lock
, flags
);
1195 runtime
->appl_ptr
+= count1
;
1196 runtime
->appl_ptr
%= runtime
->buffer_size
;
1197 runtime
->avail
-= count1
;
1202 count1
= runtime
->avail
< runtime
->buffer_size
;
1203 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1205 snd_rawmidi_output_trigger(substream
, 1);
1209 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream
*substream
,
1210 const unsigned char *buf
, long count
)
1212 return snd_rawmidi_kernel_write1(substream
, NULL
, buf
, count
);
1215 static ssize_t
snd_rawmidi_write(struct file
*file
, const char __user
*buf
,
1216 size_t count
, loff_t
*offset
)
1218 long result
, timeout
;
1220 struct snd_rawmidi_file
*rfile
;
1221 struct snd_rawmidi_runtime
*runtime
;
1222 struct snd_rawmidi_substream
*substream
;
1224 rfile
= file
->private_data
;
1225 substream
= rfile
->output
;
1226 runtime
= substream
->runtime
;
1227 /* we cannot put an atomic message to our buffer */
1228 if (substream
->append
&& count
> runtime
->buffer_size
)
1232 spin_lock_irq(&runtime
->lock
);
1233 while (!snd_rawmidi_ready_append(substream
, count
)) {
1235 if (file
->f_flags
& O_NONBLOCK
) {
1236 spin_unlock_irq(&runtime
->lock
);
1237 return result
> 0 ? result
: -EAGAIN
;
1239 init_waitqueue_entry(&wait
, current
);
1240 add_wait_queue(&runtime
->sleep
, &wait
);
1241 set_current_state(TASK_INTERRUPTIBLE
);
1242 spin_unlock_irq(&runtime
->lock
);
1243 timeout
= schedule_timeout(30 * HZ
);
1244 remove_wait_queue(&runtime
->sleep
, &wait
);
1245 if (signal_pending(current
))
1246 return result
> 0 ? result
: -ERESTARTSYS
;
1247 if (!runtime
->avail
&& !timeout
)
1248 return result
> 0 ? result
: -EIO
;
1249 spin_lock_irq(&runtime
->lock
);
1251 spin_unlock_irq(&runtime
->lock
);
1252 count1
= snd_rawmidi_kernel_write1(substream
, buf
, NULL
, count
);
1254 return result
> 0 ? result
: count1
;
1257 if ((size_t)count1
< count
&& (file
->f_flags
& O_NONBLOCK
))
1261 if (file
->f_flags
& O_SYNC
) {
1262 spin_lock_irq(&runtime
->lock
);
1263 while (runtime
->avail
!= runtime
->buffer_size
) {
1265 unsigned int last_avail
= runtime
->avail
;
1266 init_waitqueue_entry(&wait
, current
);
1267 add_wait_queue(&runtime
->sleep
, &wait
);
1268 set_current_state(TASK_INTERRUPTIBLE
);
1269 spin_unlock_irq(&runtime
->lock
);
1270 timeout
= schedule_timeout(30 * HZ
);
1271 remove_wait_queue(&runtime
->sleep
, &wait
);
1272 if (signal_pending(current
))
1273 return result
> 0 ? result
: -ERESTARTSYS
;
1274 if (runtime
->avail
== last_avail
&& !timeout
)
1275 return result
> 0 ? result
: -EIO
;
1276 spin_lock_irq(&runtime
->lock
);
1278 spin_unlock_irq(&runtime
->lock
);
1283 static unsigned int snd_rawmidi_poll(struct file
*file
, poll_table
* wait
)
1285 struct snd_rawmidi_file
*rfile
;
1286 struct snd_rawmidi_runtime
*runtime
;
1289 rfile
= file
->private_data
;
1290 if (rfile
->input
!= NULL
) {
1291 runtime
= rfile
->input
->runtime
;
1292 snd_rawmidi_input_trigger(rfile
->input
, 1);
1293 poll_wait(file
, &runtime
->sleep
, wait
);
1295 if (rfile
->output
!= NULL
) {
1296 runtime
= rfile
->output
->runtime
;
1297 poll_wait(file
, &runtime
->sleep
, wait
);
1300 if (rfile
->input
!= NULL
) {
1301 if (snd_rawmidi_ready(rfile
->input
))
1302 mask
|= POLLIN
| POLLRDNORM
;
1304 if (rfile
->output
!= NULL
) {
1305 if (snd_rawmidi_ready(rfile
->output
))
1306 mask
|= POLLOUT
| POLLWRNORM
;
1313 #ifdef CONFIG_COMPAT
1314 #include "rawmidi_compat.c"
1316 #define snd_rawmidi_ioctl_compat NULL
1323 static void snd_rawmidi_proc_info_read(struct snd_info_entry
*entry
,
1324 struct snd_info_buffer
*buffer
)
1326 struct snd_rawmidi
*rmidi
;
1327 struct snd_rawmidi_substream
*substream
;
1328 struct snd_rawmidi_runtime
*runtime
;
1330 rmidi
= entry
->private_data
;
1331 snd_iprintf(buffer
, "%s\n\n", rmidi
->name
);
1332 mutex_lock(&rmidi
->open_mutex
);
1333 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) {
1334 list_for_each_entry(substream
,
1335 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
,
1339 " Tx bytes : %lu\n",
1341 (unsigned long) substream
->bytes
);
1342 if (substream
->opened
) {
1343 runtime
= substream
->runtime
;
1346 " Buffer size : %lu\n"
1348 runtime
->oss
? "OSS compatible" : "native",
1349 (unsigned long) runtime
->buffer_size
,
1350 (unsigned long) runtime
->avail
);
1354 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
) {
1355 list_for_each_entry(substream
,
1356 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
,
1360 " Rx bytes : %lu\n",
1362 (unsigned long) substream
->bytes
);
1363 if (substream
->opened
) {
1364 runtime
= substream
->runtime
;
1366 " Buffer size : %lu\n"
1368 " Overruns : %lu\n",
1369 (unsigned long) runtime
->buffer_size
,
1370 (unsigned long) runtime
->avail
,
1371 (unsigned long) runtime
->xruns
);
1375 mutex_unlock(&rmidi
->open_mutex
);
1379 * Register functions
1382 static const struct file_operations snd_rawmidi_f_ops
=
1384 .owner
= THIS_MODULE
,
1385 .read
= snd_rawmidi_read
,
1386 .write
= snd_rawmidi_write
,
1387 .open
= snd_rawmidi_open
,
1388 .release
= snd_rawmidi_release
,
1389 .poll
= snd_rawmidi_poll
,
1390 .unlocked_ioctl
= snd_rawmidi_ioctl
,
1391 .compat_ioctl
= snd_rawmidi_ioctl_compat
,
1394 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi
*rmidi
,
1395 struct snd_rawmidi_str
*stream
,
1399 struct snd_rawmidi_substream
*substream
;
1402 for (idx
= 0; idx
< count
; idx
++) {
1403 substream
= kzalloc(sizeof(*substream
), GFP_KERNEL
);
1404 if (substream
== NULL
) {
1405 snd_printk(KERN_ERR
"rawmidi: cannot allocate substream\n");
1408 substream
->stream
= direction
;
1409 substream
->number
= idx
;
1410 substream
->rmidi
= rmidi
;
1411 substream
->pstr
= stream
;
1412 list_add_tail(&substream
->list
, &stream
->substreams
);
1413 stream
->substream_count
++;
1419 * snd_rawmidi_new - create a rawmidi instance
1420 * @card: the card instance
1421 * @id: the id string
1422 * @device: the device index
1423 * @output_count: the number of output streams
1424 * @input_count: the number of input streams
1425 * @rrawmidi: the pointer to store the new rawmidi instance
1427 * Creates a new rawmidi instance.
1428 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1430 * Returns zero if successful, or a negative error code on failure.
1432 int snd_rawmidi_new(struct snd_card
*card
, char *id
, int device
,
1433 int output_count
, int input_count
,
1434 struct snd_rawmidi
** rrawmidi
)
1436 struct snd_rawmidi
*rmidi
;
1438 static struct snd_device_ops ops
= {
1439 .dev_free
= snd_rawmidi_dev_free
,
1440 .dev_register
= snd_rawmidi_dev_register
,
1441 .dev_disconnect
= snd_rawmidi_dev_disconnect
,
1444 if (snd_BUG_ON(!card
))
1448 rmidi
= kzalloc(sizeof(*rmidi
), GFP_KERNEL
);
1449 if (rmidi
== NULL
) {
1450 snd_printk(KERN_ERR
"rawmidi: cannot allocate\n");
1454 rmidi
->device
= device
;
1455 mutex_init(&rmidi
->open_mutex
);
1456 init_waitqueue_head(&rmidi
->open_wait
);
1457 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
);
1458 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
);
1461 strlcpy(rmidi
->id
, id
, sizeof(rmidi
->id
));
1462 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1463 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
],
1464 SNDRV_RAWMIDI_STREAM_INPUT
,
1465 input_count
)) < 0) {
1466 snd_rawmidi_free(rmidi
);
1469 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1470 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
],
1471 SNDRV_RAWMIDI_STREAM_OUTPUT
,
1472 output_count
)) < 0) {
1473 snd_rawmidi_free(rmidi
);
1476 if ((err
= snd_device_new(card
, SNDRV_DEV_RAWMIDI
, rmidi
, &ops
)) < 0) {
1477 snd_rawmidi_free(rmidi
);
1485 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str
*stream
)
1487 struct snd_rawmidi_substream
*substream
;
1489 while (!list_empty(&stream
->substreams
)) {
1490 substream
= list_entry(stream
->substreams
.next
, struct snd_rawmidi_substream
, list
);
1491 list_del(&substream
->list
);
1496 static int snd_rawmidi_free(struct snd_rawmidi
*rmidi
)
1501 snd_info_free_entry(rmidi
->proc_entry
);
1502 rmidi
->proc_entry
= NULL
;
1503 mutex_lock(®ister_mutex
);
1504 if (rmidi
->ops
&& rmidi
->ops
->dev_unregister
)
1505 rmidi
->ops
->dev_unregister(rmidi
);
1506 mutex_unlock(®ister_mutex
);
1508 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
1509 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
1510 if (rmidi
->private_free
)
1511 rmidi
->private_free(rmidi
);
1516 static int snd_rawmidi_dev_free(struct snd_device
*device
)
1518 struct snd_rawmidi
*rmidi
= device
->device_data
;
1519 return snd_rawmidi_free(rmidi
);
1522 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1523 static void snd_rawmidi_dev_seq_free(struct snd_seq_device
*device
)
1525 struct snd_rawmidi
*rmidi
= device
->private_data
;
1526 rmidi
->seq_dev
= NULL
;
1530 static int snd_rawmidi_dev_register(struct snd_device
*device
)
1533 struct snd_info_entry
*entry
;
1535 struct snd_rawmidi
*rmidi
= device
->device_data
;
1537 if (rmidi
->device
>= SNDRV_RAWMIDI_DEVICES
)
1539 mutex_lock(®ister_mutex
);
1540 if (snd_rawmidi_search(rmidi
->card
, rmidi
->device
)) {
1541 mutex_unlock(®ister_mutex
);
1544 list_add_tail(&rmidi
->list
, &snd_rawmidi_devices
);
1545 sprintf(name
, "midiC%iD%i", rmidi
->card
->number
, rmidi
->device
);
1546 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI
,
1547 rmidi
->card
, rmidi
->device
,
1548 &snd_rawmidi_f_ops
, rmidi
, name
)) < 0) {
1549 snd_printk(KERN_ERR
"unable to register rawmidi device %i:%i\n", rmidi
->card
->number
, rmidi
->device
);
1550 list_del(&rmidi
->list
);
1551 mutex_unlock(®ister_mutex
);
1554 if (rmidi
->ops
&& rmidi
->ops
->dev_register
&&
1555 (err
= rmidi
->ops
->dev_register(rmidi
)) < 0) {
1556 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1557 list_del(&rmidi
->list
);
1558 mutex_unlock(®ister_mutex
);
1561 #ifdef CONFIG_SND_OSSEMUL
1563 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1564 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1565 rmidi
->card
, 0, &snd_rawmidi_f_ops
,
1567 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 0);
1570 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1571 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
, rmidi
->name
);
1575 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
]) {
1576 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1577 rmidi
->card
, 1, &snd_rawmidi_f_ops
,
1579 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 1);
1584 #endif /* CONFIG_SND_OSSEMUL */
1585 mutex_unlock(®ister_mutex
);
1586 sprintf(name
, "midi%d", rmidi
->device
);
1587 entry
= snd_info_create_card_entry(rmidi
->card
, name
, rmidi
->card
->proc_root
);
1589 entry
->private_data
= rmidi
;
1590 entry
->c
.text
.read
= snd_rawmidi_proc_info_read
;
1591 if (snd_info_register(entry
) < 0) {
1592 snd_info_free_entry(entry
);
1596 rmidi
->proc_entry
= entry
;
1597 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1598 if (!rmidi
->ops
|| !rmidi
->ops
->dev_register
) { /* own registration mechanism */
1599 if (snd_seq_device_new(rmidi
->card
, rmidi
->device
, SNDRV_SEQ_DEV_ID_MIDISYNTH
, 0, &rmidi
->seq_dev
) >= 0) {
1600 rmidi
->seq_dev
->private_data
= rmidi
;
1601 rmidi
->seq_dev
->private_free
= snd_rawmidi_dev_seq_free
;
1602 sprintf(rmidi
->seq_dev
->name
, "MIDI %d-%d", rmidi
->card
->number
, rmidi
->device
);
1603 snd_device_register(rmidi
->card
, rmidi
->seq_dev
);
1610 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
)
1612 struct snd_rawmidi
*rmidi
= device
->device_data
;
1614 mutex_lock(®ister_mutex
);
1615 list_del_init(&rmidi
->list
);
1616 #ifdef CONFIG_SND_OSSEMUL
1617 if (rmidi
->ossreg
) {
1618 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1619 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 0);
1620 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1621 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
);
1624 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
])
1625 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 1);
1628 #endif /* CONFIG_SND_OSSEMUL */
1629 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1630 mutex_unlock(®ister_mutex
);
1635 * snd_rawmidi_set_ops - set the rawmidi operators
1636 * @rmidi: the rawmidi instance
1637 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1638 * @ops: the operator table
1640 * Sets the rawmidi operators for the given stream direction.
1642 void snd_rawmidi_set_ops(struct snd_rawmidi
*rmidi
, int stream
,
1643 struct snd_rawmidi_ops
*ops
)
1645 struct snd_rawmidi_substream
*substream
;
1647 list_for_each_entry(substream
, &rmidi
->streams
[stream
].substreams
, list
)
1648 substream
->ops
= ops
;
1655 static int __init
alsa_rawmidi_init(void)
1658 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl
);
1659 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl
);
1660 #ifdef CONFIG_SND_OSSEMUL
1662 /* check device map table */
1663 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1664 if (midi_map
[i
] < 0 || midi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1665 snd_printk(KERN_ERR
"invalid midi_map[%d] = %d\n", i
, midi_map
[i
]);
1668 if (amidi_map
[i
] < 0 || amidi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1669 snd_printk(KERN_ERR
"invalid amidi_map[%d] = %d\n", i
, amidi_map
[i
]);
1674 #endif /* CONFIG_SND_OSSEMUL */
1678 static void __exit
alsa_rawmidi_exit(void)
1680 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl
);
1681 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl
);
1684 module_init(alsa_rawmidi_init
)
1685 module_exit(alsa_rawmidi_exit
)
1687 EXPORT_SYMBOL(snd_rawmidi_output_params
);
1688 EXPORT_SYMBOL(snd_rawmidi_input_params
);
1689 EXPORT_SYMBOL(snd_rawmidi_drop_output
);
1690 EXPORT_SYMBOL(snd_rawmidi_drain_output
);
1691 EXPORT_SYMBOL(snd_rawmidi_drain_input
);
1692 EXPORT_SYMBOL(snd_rawmidi_receive
);
1693 EXPORT_SYMBOL(snd_rawmidi_transmit_empty
);
1694 EXPORT_SYMBOL(snd_rawmidi_transmit_peek
);
1695 EXPORT_SYMBOL(snd_rawmidi_transmit_ack
);
1696 EXPORT_SYMBOL(snd_rawmidi_transmit
);
1697 EXPORT_SYMBOL(snd_rawmidi_new
);
1698 EXPORT_SYMBOL(snd_rawmidi_set_ops
);
1699 EXPORT_SYMBOL(snd_rawmidi_info_select
);
1700 EXPORT_SYMBOL(snd_rawmidi_kernel_open
);
1701 EXPORT_SYMBOL(snd_rawmidi_kernel_release
);
1702 EXPORT_SYMBOL(snd_rawmidi_kernel_read
);
1703 EXPORT_SYMBOL(snd_rawmidi_kernel_write
);