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
;
534 rfile
= file
->private_data
;
535 rmidi
= rfile
->rmidi
;
536 rawmidi_release_priv(rfile
);
538 snd_card_file_remove(rmidi
->card
, file
);
539 module_put(rmidi
->card
->module
);
543 static int snd_rawmidi_info(struct snd_rawmidi_substream
*substream
,
544 struct snd_rawmidi_info
*info
)
546 struct snd_rawmidi
*rmidi
;
548 if (substream
== NULL
)
550 rmidi
= substream
->rmidi
;
551 memset(info
, 0, sizeof(*info
));
552 info
->card
= rmidi
->card
->number
;
553 info
->device
= rmidi
->device
;
554 info
->subdevice
= substream
->number
;
555 info
->stream
= substream
->stream
;
556 info
->flags
= rmidi
->info_flags
;
557 strcpy(info
->id
, rmidi
->id
);
558 strcpy(info
->name
, rmidi
->name
);
559 strcpy(info
->subname
, substream
->name
);
560 info
->subdevices_count
= substream
->pstr
->substream_count
;
561 info
->subdevices_avail
= (substream
->pstr
->substream_count
-
562 substream
->pstr
->substream_opened
);
566 static int snd_rawmidi_info_user(struct snd_rawmidi_substream
*substream
,
567 struct snd_rawmidi_info __user
* _info
)
569 struct snd_rawmidi_info info
;
571 if ((err
= snd_rawmidi_info(substream
, &info
)) < 0)
573 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
578 int snd_rawmidi_info_select(struct snd_card
*card
, struct snd_rawmidi_info
*info
)
580 struct snd_rawmidi
*rmidi
;
581 struct snd_rawmidi_str
*pstr
;
582 struct snd_rawmidi_substream
*substream
;
584 mutex_lock(®ister_mutex
);
585 rmidi
= snd_rawmidi_search(card
, info
->device
);
586 mutex_unlock(®ister_mutex
);
589 if (info
->stream
< 0 || info
->stream
> 1)
591 pstr
= &rmidi
->streams
[info
->stream
];
592 if (pstr
->substream_count
== 0)
594 if (info
->subdevice
>= pstr
->substream_count
)
596 list_for_each_entry(substream
, &pstr
->substreams
, list
) {
597 if ((unsigned int)substream
->number
== info
->subdevice
)
598 return snd_rawmidi_info(substream
, info
);
603 static int snd_rawmidi_info_select_user(struct snd_card
*card
,
604 struct snd_rawmidi_info __user
*_info
)
607 struct snd_rawmidi_info info
;
608 if (get_user(info
.device
, &_info
->device
))
610 if (get_user(info
.stream
, &_info
->stream
))
612 if (get_user(info
.subdevice
, &_info
->subdevice
))
614 if ((err
= snd_rawmidi_info_select(card
, &info
)) < 0)
616 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
621 int snd_rawmidi_output_params(struct snd_rawmidi_substream
*substream
,
622 struct snd_rawmidi_params
* params
)
625 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
627 if (substream
->append
&& substream
->use_count
> 1)
629 snd_rawmidi_drain_output(substream
);
630 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
633 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
636 if (params
->buffer_size
!= runtime
->buffer_size
) {
637 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
640 kfree(runtime
->buffer
);
641 runtime
->buffer
= newbuf
;
642 runtime
->buffer_size
= params
->buffer_size
;
643 runtime
->avail
= runtime
->buffer_size
;
645 runtime
->avail_min
= params
->avail_min
;
646 substream
->active_sensing
= !params
->no_active_sensing
;
650 int snd_rawmidi_input_params(struct snd_rawmidi_substream
*substream
,
651 struct snd_rawmidi_params
* params
)
654 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
656 snd_rawmidi_drain_input(substream
);
657 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
660 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
663 if (params
->buffer_size
!= runtime
->buffer_size
) {
664 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
667 kfree(runtime
->buffer
);
668 runtime
->buffer
= newbuf
;
669 runtime
->buffer_size
= params
->buffer_size
;
671 runtime
->avail_min
= params
->avail_min
;
675 static int snd_rawmidi_output_status(struct snd_rawmidi_substream
*substream
,
676 struct snd_rawmidi_status
* status
)
678 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
680 memset(status
, 0, sizeof(*status
));
681 status
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
682 spin_lock_irq(&runtime
->lock
);
683 status
->avail
= runtime
->avail
;
684 spin_unlock_irq(&runtime
->lock
);
688 static int snd_rawmidi_input_status(struct snd_rawmidi_substream
*substream
,
689 struct snd_rawmidi_status
* status
)
691 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
693 memset(status
, 0, sizeof(*status
));
694 status
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
695 spin_lock_irq(&runtime
->lock
);
696 status
->avail
= runtime
->avail
;
697 status
->xruns
= runtime
->xruns
;
699 spin_unlock_irq(&runtime
->lock
);
703 static long snd_rawmidi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
705 struct snd_rawmidi_file
*rfile
;
706 void __user
*argp
= (void __user
*)arg
;
708 rfile
= file
->private_data
;
709 if (((cmd
>> 8) & 0xff) != 'W')
712 case SNDRV_RAWMIDI_IOCTL_PVERSION
:
713 return put_user(SNDRV_RAWMIDI_VERSION
, (int __user
*)argp
) ? -EFAULT
: 0;
714 case SNDRV_RAWMIDI_IOCTL_INFO
:
717 struct snd_rawmidi_info __user
*info
= argp
;
718 if (get_user(stream
, &info
->stream
))
721 case SNDRV_RAWMIDI_STREAM_INPUT
:
722 return snd_rawmidi_info_user(rfile
->input
, info
);
723 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
724 return snd_rawmidi_info_user(rfile
->output
, info
);
729 case SNDRV_RAWMIDI_IOCTL_PARAMS
:
731 struct snd_rawmidi_params params
;
732 if (copy_from_user(¶ms
, argp
, sizeof(struct snd_rawmidi_params
)))
734 switch (params
.stream
) {
735 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
736 if (rfile
->output
== NULL
)
738 return snd_rawmidi_output_params(rfile
->output
, ¶ms
);
739 case SNDRV_RAWMIDI_STREAM_INPUT
:
740 if (rfile
->input
== NULL
)
742 return snd_rawmidi_input_params(rfile
->input
, ¶ms
);
747 case SNDRV_RAWMIDI_IOCTL_STATUS
:
750 struct snd_rawmidi_status status
;
751 if (copy_from_user(&status
, argp
, sizeof(struct snd_rawmidi_status
)))
753 switch (status
.stream
) {
754 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
755 if (rfile
->output
== NULL
)
757 err
= snd_rawmidi_output_status(rfile
->output
, &status
);
759 case SNDRV_RAWMIDI_STREAM_INPUT
:
760 if (rfile
->input
== NULL
)
762 err
= snd_rawmidi_input_status(rfile
->input
, &status
);
769 if (copy_to_user(argp
, &status
, sizeof(struct snd_rawmidi_status
)))
773 case SNDRV_RAWMIDI_IOCTL_DROP
:
776 if (get_user(val
, (int __user
*) argp
))
779 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
780 if (rfile
->output
== NULL
)
782 return snd_rawmidi_drop_output(rfile
->output
);
787 case SNDRV_RAWMIDI_IOCTL_DRAIN
:
790 if (get_user(val
, (int __user
*) argp
))
793 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
794 if (rfile
->output
== NULL
)
796 return snd_rawmidi_drain_output(rfile
->output
);
797 case SNDRV_RAWMIDI_STREAM_INPUT
:
798 if (rfile
->input
== NULL
)
800 return snd_rawmidi_drain_input(rfile
->input
);
805 #ifdef CONFIG_SND_DEBUG
807 snd_printk(KERN_WARNING
"rawmidi: unknown command = 0x%x\n", cmd
);
813 static int snd_rawmidi_control_ioctl(struct snd_card
*card
,
814 struct snd_ctl_file
*control
,
818 void __user
*argp
= (void __user
*)arg
;
821 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE
:
825 if (get_user(device
, (int __user
*)argp
))
827 mutex_lock(®ister_mutex
);
828 device
= device
< 0 ? 0 : device
+ 1;
829 while (device
< SNDRV_RAWMIDI_DEVICES
) {
830 if (snd_rawmidi_search(card
, device
))
834 if (device
== SNDRV_RAWMIDI_DEVICES
)
836 mutex_unlock(®ister_mutex
);
837 if (put_user(device
, (int __user
*)argp
))
841 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE
:
845 if (get_user(val
, (int __user
*)argp
))
847 control
->prefer_rawmidi_subdevice
= val
;
850 case SNDRV_CTL_IOCTL_RAWMIDI_INFO
:
851 return snd_rawmidi_info_select_user(card
, argp
);
857 * snd_rawmidi_receive - receive the input data from the device
858 * @substream: the rawmidi substream
859 * @buffer: the buffer pointer
860 * @count: the data size to read
862 * Reads the data from the internal buffer.
864 * Returns the size of read data, or a negative error code on failure.
866 int snd_rawmidi_receive(struct snd_rawmidi_substream
*substream
,
867 const unsigned char *buffer
, int count
)
870 int result
= 0, count1
;
871 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
873 if (!substream
->opened
)
875 if (runtime
->buffer
== NULL
) {
876 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
879 spin_lock_irqsave(&runtime
->lock
, flags
);
880 if (count
== 1) { /* special case, faster code */
882 if (runtime
->avail
< runtime
->buffer_size
) {
883 runtime
->buffer
[runtime
->hw_ptr
++] = buffer
[0];
884 runtime
->hw_ptr
%= runtime
->buffer_size
;
891 substream
->bytes
+= count
;
892 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
895 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
896 count1
= runtime
->buffer_size
- runtime
->avail
;
897 memcpy(runtime
->buffer
+ runtime
->hw_ptr
, buffer
, count1
);
898 runtime
->hw_ptr
+= count1
;
899 runtime
->hw_ptr
%= runtime
->buffer_size
;
900 runtime
->avail
+= count1
;
906 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
)) {
907 count1
= runtime
->buffer_size
- runtime
->avail
;
908 runtime
->xruns
+= count
- count1
;
911 memcpy(runtime
->buffer
, buffer
, count1
);
912 runtime
->hw_ptr
= count1
;
913 runtime
->avail
+= count1
;
920 tasklet_schedule(&runtime
->tasklet
);
921 else if (snd_rawmidi_ready(substream
))
922 wake_up(&runtime
->sleep
);
924 spin_unlock_irqrestore(&runtime
->lock
, flags
);
928 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream
*substream
,
929 unsigned char __user
*userbuf
,
930 unsigned char *kernelbuf
, long count
)
933 long result
= 0, count1
;
934 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
936 while (count
> 0 && runtime
->avail
) {
937 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
940 spin_lock_irqsave(&runtime
->lock
, flags
);
941 if (count1
> (int)runtime
->avail
)
942 count1
= runtime
->avail
;
944 memcpy(kernelbuf
+ result
, runtime
->buffer
+ runtime
->appl_ptr
, count1
);
946 spin_unlock_irqrestore(&runtime
->lock
, flags
);
947 if (copy_to_user(userbuf
+ result
,
948 runtime
->buffer
+ runtime
->appl_ptr
, count1
)) {
949 return result
> 0 ? result
: -EFAULT
;
951 spin_lock_irqsave(&runtime
->lock
, flags
);
953 runtime
->appl_ptr
+= count1
;
954 runtime
->appl_ptr
%= runtime
->buffer_size
;
955 runtime
->avail
-= count1
;
956 spin_unlock_irqrestore(&runtime
->lock
, flags
);
963 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream
*substream
,
964 unsigned char *buf
, long count
)
966 snd_rawmidi_input_trigger(substream
, 1);
967 return snd_rawmidi_kernel_read1(substream
, NULL
/*userbuf*/, buf
, count
);
970 static ssize_t
snd_rawmidi_read(struct file
*file
, char __user
*buf
, size_t count
,
975 struct snd_rawmidi_file
*rfile
;
976 struct snd_rawmidi_substream
*substream
;
977 struct snd_rawmidi_runtime
*runtime
;
979 rfile
= file
->private_data
;
980 substream
= rfile
->input
;
981 if (substream
== NULL
)
983 runtime
= substream
->runtime
;
984 snd_rawmidi_input_trigger(substream
, 1);
987 spin_lock_irq(&runtime
->lock
);
988 while (!snd_rawmidi_ready(substream
)) {
990 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
991 spin_unlock_irq(&runtime
->lock
);
992 return result
> 0 ? result
: -EAGAIN
;
994 init_waitqueue_entry(&wait
, current
);
995 add_wait_queue(&runtime
->sleep
, &wait
);
996 set_current_state(TASK_INTERRUPTIBLE
);
997 spin_unlock_irq(&runtime
->lock
);
999 remove_wait_queue(&runtime
->sleep
, &wait
);
1000 if (signal_pending(current
))
1001 return result
> 0 ? result
: -ERESTARTSYS
;
1002 if (!runtime
->avail
)
1003 return result
> 0 ? result
: -EIO
;
1004 spin_lock_irq(&runtime
->lock
);
1006 spin_unlock_irq(&runtime
->lock
);
1007 count1
= snd_rawmidi_kernel_read1(substream
,
1008 (unsigned char __user
*)buf
,
1012 return result
> 0 ? result
: count1
;
1021 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1022 * @substream: the rawmidi substream
1024 * Returns 1 if the internal output buffer is empty, 0 if not.
1026 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream
*substream
)
1028 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1030 unsigned long flags
;
1032 if (runtime
->buffer
== NULL
) {
1033 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1036 spin_lock_irqsave(&runtime
->lock
, flags
);
1037 result
= runtime
->avail
>= runtime
->buffer_size
;
1038 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1043 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1044 * @substream: the rawmidi substream
1045 * @buffer: the buffer pointer
1046 * @count: data size to transfer
1048 * Copies data from the internal output buffer to the given buffer.
1050 * Call this in the interrupt handler when the midi output is ready,
1051 * and call snd_rawmidi_transmit_ack() after the transmission is
1054 * Returns the size of copied data, or a negative error code on failure.
1056 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1057 unsigned char *buffer
, int count
)
1059 unsigned long flags
;
1061 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1063 if (runtime
->buffer
== NULL
) {
1064 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1068 spin_lock_irqsave(&runtime
->lock
, flags
);
1069 if (runtime
->avail
>= runtime
->buffer_size
) {
1070 /* warning: lowlevel layer MUST trigger down the hardware */
1073 if (count
== 1) { /* special case, faster code */
1074 *buffer
= runtime
->buffer
[runtime
->hw_ptr
];
1077 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
1080 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
1081 count1
= runtime
->buffer_size
- runtime
->avail
;
1082 memcpy(buffer
, runtime
->buffer
+ runtime
->hw_ptr
, count1
);
1086 if (count
> (int)(runtime
->buffer_size
- runtime
->avail
- count1
))
1087 count
= runtime
->buffer_size
- runtime
->avail
- count1
;
1088 memcpy(buffer
+ count1
, runtime
->buffer
, count
);
1093 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1098 * snd_rawmidi_transmit_ack - acknowledge the transmission
1099 * @substream: the rawmidi substream
1100 * @count: the tranferred count
1102 * Advances the hardware pointer for the internal output buffer with
1103 * the given size and updates the condition.
1104 * Call after the transmission is finished.
1106 * Returns the advanced size if successful, or a negative error code on failure.
1108 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1110 unsigned long flags
;
1111 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1113 if (runtime
->buffer
== NULL
) {
1114 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1117 spin_lock_irqsave(&runtime
->lock
, flags
);
1118 snd_BUG_ON(runtime
->avail
+ count
> runtime
->buffer_size
);
1119 runtime
->hw_ptr
+= count
;
1120 runtime
->hw_ptr
%= runtime
->buffer_size
;
1121 runtime
->avail
+= count
;
1122 substream
->bytes
+= count
;
1124 if (runtime
->drain
|| snd_rawmidi_ready(substream
))
1125 wake_up(&runtime
->sleep
);
1127 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1132 * snd_rawmidi_transmit - copy from the buffer to the device
1133 * @substream: the rawmidi substream
1134 * @buffer: the buffer pointer
1135 * @count: the data size to transfer
1137 * Copies data from the buffer to the device and advances the pointer.
1139 * Returns the copied size if successful, or a negative error code on failure.
1141 int snd_rawmidi_transmit(struct snd_rawmidi_substream
*substream
,
1142 unsigned char *buffer
, int count
)
1144 if (!substream
->opened
)
1146 count
= snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1149 return snd_rawmidi_transmit_ack(substream
, count
);
1152 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream
*substream
,
1153 const unsigned char __user
*userbuf
,
1154 const unsigned char *kernelbuf
,
1157 unsigned long flags
;
1158 long count1
, result
;
1159 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1161 if (snd_BUG_ON(!kernelbuf
&& !userbuf
))
1163 if (snd_BUG_ON(!runtime
->buffer
))
1167 spin_lock_irqsave(&runtime
->lock
, flags
);
1168 if (substream
->append
) {
1169 if ((long)runtime
->avail
< count
) {
1170 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1174 while (count
> 0 && runtime
->avail
> 0) {
1175 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1178 if (count1
> (long)runtime
->avail
)
1179 count1
= runtime
->avail
;
1181 memcpy(runtime
->buffer
+ runtime
->appl_ptr
,
1182 kernelbuf
+ result
, count1
);
1184 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1185 if (copy_from_user(runtime
->buffer
+ runtime
->appl_ptr
,
1186 userbuf
+ result
, count1
)) {
1187 spin_lock_irqsave(&runtime
->lock
, flags
);
1188 result
= result
> 0 ? result
: -EFAULT
;
1191 spin_lock_irqsave(&runtime
->lock
, flags
);
1193 runtime
->appl_ptr
+= count1
;
1194 runtime
->appl_ptr
%= runtime
->buffer_size
;
1195 runtime
->avail
-= count1
;
1200 count1
= runtime
->avail
< runtime
->buffer_size
;
1201 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1203 snd_rawmidi_output_trigger(substream
, 1);
1207 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream
*substream
,
1208 const unsigned char *buf
, long count
)
1210 return snd_rawmidi_kernel_write1(substream
, NULL
, buf
, count
);
1213 static ssize_t
snd_rawmidi_write(struct file
*file
, const char __user
*buf
,
1214 size_t count
, loff_t
*offset
)
1216 long result
, timeout
;
1218 struct snd_rawmidi_file
*rfile
;
1219 struct snd_rawmidi_runtime
*runtime
;
1220 struct snd_rawmidi_substream
*substream
;
1222 rfile
= file
->private_data
;
1223 substream
= rfile
->output
;
1224 runtime
= substream
->runtime
;
1225 /* we cannot put an atomic message to our buffer */
1226 if (substream
->append
&& count
> runtime
->buffer_size
)
1230 spin_lock_irq(&runtime
->lock
);
1231 while (!snd_rawmidi_ready_append(substream
, count
)) {
1233 if (file
->f_flags
& O_NONBLOCK
) {
1234 spin_unlock_irq(&runtime
->lock
);
1235 return result
> 0 ? result
: -EAGAIN
;
1237 init_waitqueue_entry(&wait
, current
);
1238 add_wait_queue(&runtime
->sleep
, &wait
);
1239 set_current_state(TASK_INTERRUPTIBLE
);
1240 spin_unlock_irq(&runtime
->lock
);
1241 timeout
= schedule_timeout(30 * HZ
);
1242 remove_wait_queue(&runtime
->sleep
, &wait
);
1243 if (signal_pending(current
))
1244 return result
> 0 ? result
: -ERESTARTSYS
;
1245 if (!runtime
->avail
&& !timeout
)
1246 return result
> 0 ? result
: -EIO
;
1247 spin_lock_irq(&runtime
->lock
);
1249 spin_unlock_irq(&runtime
->lock
);
1250 count1
= snd_rawmidi_kernel_write1(substream
, buf
, NULL
, count
);
1252 return result
> 0 ? result
: count1
;
1255 if ((size_t)count1
< count
&& (file
->f_flags
& O_NONBLOCK
))
1259 if (file
->f_flags
& O_SYNC
) {
1260 spin_lock_irq(&runtime
->lock
);
1261 while (runtime
->avail
!= runtime
->buffer_size
) {
1263 unsigned int last_avail
= runtime
->avail
;
1264 init_waitqueue_entry(&wait
, current
);
1265 add_wait_queue(&runtime
->sleep
, &wait
);
1266 set_current_state(TASK_INTERRUPTIBLE
);
1267 spin_unlock_irq(&runtime
->lock
);
1268 timeout
= schedule_timeout(30 * HZ
);
1269 remove_wait_queue(&runtime
->sleep
, &wait
);
1270 if (signal_pending(current
))
1271 return result
> 0 ? result
: -ERESTARTSYS
;
1272 if (runtime
->avail
== last_avail
&& !timeout
)
1273 return result
> 0 ? result
: -EIO
;
1274 spin_lock_irq(&runtime
->lock
);
1276 spin_unlock_irq(&runtime
->lock
);
1281 static unsigned int snd_rawmidi_poll(struct file
*file
, poll_table
* wait
)
1283 struct snd_rawmidi_file
*rfile
;
1284 struct snd_rawmidi_runtime
*runtime
;
1287 rfile
= file
->private_data
;
1288 if (rfile
->input
!= NULL
) {
1289 runtime
= rfile
->input
->runtime
;
1290 snd_rawmidi_input_trigger(rfile
->input
, 1);
1291 poll_wait(file
, &runtime
->sleep
, wait
);
1293 if (rfile
->output
!= NULL
) {
1294 runtime
= rfile
->output
->runtime
;
1295 poll_wait(file
, &runtime
->sleep
, wait
);
1298 if (rfile
->input
!= NULL
) {
1299 if (snd_rawmidi_ready(rfile
->input
))
1300 mask
|= POLLIN
| POLLRDNORM
;
1302 if (rfile
->output
!= NULL
) {
1303 if (snd_rawmidi_ready(rfile
->output
))
1304 mask
|= POLLOUT
| POLLWRNORM
;
1311 #ifdef CONFIG_COMPAT
1312 #include "rawmidi_compat.c"
1314 #define snd_rawmidi_ioctl_compat NULL
1321 static void snd_rawmidi_proc_info_read(struct snd_info_entry
*entry
,
1322 struct snd_info_buffer
*buffer
)
1324 struct snd_rawmidi
*rmidi
;
1325 struct snd_rawmidi_substream
*substream
;
1326 struct snd_rawmidi_runtime
*runtime
;
1328 rmidi
= entry
->private_data
;
1329 snd_iprintf(buffer
, "%s\n\n", rmidi
->name
);
1330 mutex_lock(&rmidi
->open_mutex
);
1331 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) {
1332 list_for_each_entry(substream
,
1333 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
,
1337 " Tx bytes : %lu\n",
1339 (unsigned long) substream
->bytes
);
1340 if (substream
->opened
) {
1341 runtime
= substream
->runtime
;
1344 " Buffer size : %lu\n"
1346 runtime
->oss
? "OSS compatible" : "native",
1347 (unsigned long) runtime
->buffer_size
,
1348 (unsigned long) runtime
->avail
);
1352 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
) {
1353 list_for_each_entry(substream
,
1354 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
,
1358 " Rx bytes : %lu\n",
1360 (unsigned long) substream
->bytes
);
1361 if (substream
->opened
) {
1362 runtime
= substream
->runtime
;
1364 " Buffer size : %lu\n"
1366 " Overruns : %lu\n",
1367 (unsigned long) runtime
->buffer_size
,
1368 (unsigned long) runtime
->avail
,
1369 (unsigned long) runtime
->xruns
);
1373 mutex_unlock(&rmidi
->open_mutex
);
1377 * Register functions
1380 static const struct file_operations snd_rawmidi_f_ops
=
1382 .owner
= THIS_MODULE
,
1383 .read
= snd_rawmidi_read
,
1384 .write
= snd_rawmidi_write
,
1385 .open
= snd_rawmidi_open
,
1386 .release
= snd_rawmidi_release
,
1387 .poll
= snd_rawmidi_poll
,
1388 .unlocked_ioctl
= snd_rawmidi_ioctl
,
1389 .compat_ioctl
= snd_rawmidi_ioctl_compat
,
1392 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi
*rmidi
,
1393 struct snd_rawmidi_str
*stream
,
1397 struct snd_rawmidi_substream
*substream
;
1400 for (idx
= 0; idx
< count
; idx
++) {
1401 substream
= kzalloc(sizeof(*substream
), GFP_KERNEL
);
1402 if (substream
== NULL
) {
1403 snd_printk(KERN_ERR
"rawmidi: cannot allocate substream\n");
1406 substream
->stream
= direction
;
1407 substream
->number
= idx
;
1408 substream
->rmidi
= rmidi
;
1409 substream
->pstr
= stream
;
1410 list_add_tail(&substream
->list
, &stream
->substreams
);
1411 stream
->substream_count
++;
1417 * snd_rawmidi_new - create a rawmidi instance
1418 * @card: the card instance
1419 * @id: the id string
1420 * @device: the device index
1421 * @output_count: the number of output streams
1422 * @input_count: the number of input streams
1423 * @rrawmidi: the pointer to store the new rawmidi instance
1425 * Creates a new rawmidi instance.
1426 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1428 * Returns zero if successful, or a negative error code on failure.
1430 int snd_rawmidi_new(struct snd_card
*card
, char *id
, int device
,
1431 int output_count
, int input_count
,
1432 struct snd_rawmidi
** rrawmidi
)
1434 struct snd_rawmidi
*rmidi
;
1436 static struct snd_device_ops ops
= {
1437 .dev_free
= snd_rawmidi_dev_free
,
1438 .dev_register
= snd_rawmidi_dev_register
,
1439 .dev_disconnect
= snd_rawmidi_dev_disconnect
,
1442 if (snd_BUG_ON(!card
))
1446 rmidi
= kzalloc(sizeof(*rmidi
), GFP_KERNEL
);
1447 if (rmidi
== NULL
) {
1448 snd_printk(KERN_ERR
"rawmidi: cannot allocate\n");
1452 rmidi
->device
= device
;
1453 mutex_init(&rmidi
->open_mutex
);
1454 init_waitqueue_head(&rmidi
->open_wait
);
1455 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
);
1456 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
);
1459 strlcpy(rmidi
->id
, id
, sizeof(rmidi
->id
));
1460 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1461 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
],
1462 SNDRV_RAWMIDI_STREAM_INPUT
,
1463 input_count
)) < 0) {
1464 snd_rawmidi_free(rmidi
);
1467 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1468 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
],
1469 SNDRV_RAWMIDI_STREAM_OUTPUT
,
1470 output_count
)) < 0) {
1471 snd_rawmidi_free(rmidi
);
1474 if ((err
= snd_device_new(card
, SNDRV_DEV_RAWMIDI
, rmidi
, &ops
)) < 0) {
1475 snd_rawmidi_free(rmidi
);
1483 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str
*stream
)
1485 struct snd_rawmidi_substream
*substream
;
1487 while (!list_empty(&stream
->substreams
)) {
1488 substream
= list_entry(stream
->substreams
.next
, struct snd_rawmidi_substream
, list
);
1489 list_del(&substream
->list
);
1494 static int snd_rawmidi_free(struct snd_rawmidi
*rmidi
)
1499 snd_info_free_entry(rmidi
->proc_entry
);
1500 rmidi
->proc_entry
= NULL
;
1501 mutex_lock(®ister_mutex
);
1502 if (rmidi
->ops
&& rmidi
->ops
->dev_unregister
)
1503 rmidi
->ops
->dev_unregister(rmidi
);
1504 mutex_unlock(®ister_mutex
);
1506 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
1507 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
1508 if (rmidi
->private_free
)
1509 rmidi
->private_free(rmidi
);
1514 static int snd_rawmidi_dev_free(struct snd_device
*device
)
1516 struct snd_rawmidi
*rmidi
= device
->device_data
;
1517 return snd_rawmidi_free(rmidi
);
1520 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1521 static void snd_rawmidi_dev_seq_free(struct snd_seq_device
*device
)
1523 struct snd_rawmidi
*rmidi
= device
->private_data
;
1524 rmidi
->seq_dev
= NULL
;
1528 static int snd_rawmidi_dev_register(struct snd_device
*device
)
1531 struct snd_info_entry
*entry
;
1533 struct snd_rawmidi
*rmidi
= device
->device_data
;
1535 if (rmidi
->device
>= SNDRV_RAWMIDI_DEVICES
)
1537 mutex_lock(®ister_mutex
);
1538 if (snd_rawmidi_search(rmidi
->card
, rmidi
->device
)) {
1539 mutex_unlock(®ister_mutex
);
1542 list_add_tail(&rmidi
->list
, &snd_rawmidi_devices
);
1543 sprintf(name
, "midiC%iD%i", rmidi
->card
->number
, rmidi
->device
);
1544 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI
,
1545 rmidi
->card
, rmidi
->device
,
1546 &snd_rawmidi_f_ops
, rmidi
, name
)) < 0) {
1547 snd_printk(KERN_ERR
"unable to register rawmidi device %i:%i\n", rmidi
->card
->number
, rmidi
->device
);
1548 list_del(&rmidi
->list
);
1549 mutex_unlock(®ister_mutex
);
1552 if (rmidi
->ops
&& rmidi
->ops
->dev_register
&&
1553 (err
= rmidi
->ops
->dev_register(rmidi
)) < 0) {
1554 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1555 list_del(&rmidi
->list
);
1556 mutex_unlock(®ister_mutex
);
1559 #ifdef CONFIG_SND_OSSEMUL
1561 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1562 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1563 rmidi
->card
, 0, &snd_rawmidi_f_ops
,
1565 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 0);
1568 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1569 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
, rmidi
->name
);
1573 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
]) {
1574 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1575 rmidi
->card
, 1, &snd_rawmidi_f_ops
,
1577 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 1);
1582 #endif /* CONFIG_SND_OSSEMUL */
1583 mutex_unlock(®ister_mutex
);
1584 sprintf(name
, "midi%d", rmidi
->device
);
1585 entry
= snd_info_create_card_entry(rmidi
->card
, name
, rmidi
->card
->proc_root
);
1587 entry
->private_data
= rmidi
;
1588 entry
->c
.text
.read
= snd_rawmidi_proc_info_read
;
1589 if (snd_info_register(entry
) < 0) {
1590 snd_info_free_entry(entry
);
1594 rmidi
->proc_entry
= entry
;
1595 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1596 if (!rmidi
->ops
|| !rmidi
->ops
->dev_register
) { /* own registration mechanism */
1597 if (snd_seq_device_new(rmidi
->card
, rmidi
->device
, SNDRV_SEQ_DEV_ID_MIDISYNTH
, 0, &rmidi
->seq_dev
) >= 0) {
1598 rmidi
->seq_dev
->private_data
= rmidi
;
1599 rmidi
->seq_dev
->private_free
= snd_rawmidi_dev_seq_free
;
1600 sprintf(rmidi
->seq_dev
->name
, "MIDI %d-%d", rmidi
->card
->number
, rmidi
->device
);
1601 snd_device_register(rmidi
->card
, rmidi
->seq_dev
);
1608 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
)
1610 struct snd_rawmidi
*rmidi
= device
->device_data
;
1612 mutex_lock(®ister_mutex
);
1613 list_del_init(&rmidi
->list
);
1614 #ifdef CONFIG_SND_OSSEMUL
1615 if (rmidi
->ossreg
) {
1616 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1617 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 0);
1618 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1619 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
);
1622 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
])
1623 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 1);
1626 #endif /* CONFIG_SND_OSSEMUL */
1627 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1628 mutex_unlock(®ister_mutex
);
1633 * snd_rawmidi_set_ops - set the rawmidi operators
1634 * @rmidi: the rawmidi instance
1635 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1636 * @ops: the operator table
1638 * Sets the rawmidi operators for the given stream direction.
1640 void snd_rawmidi_set_ops(struct snd_rawmidi
*rmidi
, int stream
,
1641 struct snd_rawmidi_ops
*ops
)
1643 struct snd_rawmidi_substream
*substream
;
1645 list_for_each_entry(substream
, &rmidi
->streams
[stream
].substreams
, list
)
1646 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_select
);
1698 EXPORT_SYMBOL(snd_rawmidi_kernel_open
);
1699 EXPORT_SYMBOL(snd_rawmidi_kernel_release
);
1700 EXPORT_SYMBOL(snd_rawmidi_kernel_read
);
1701 EXPORT_SYMBOL(snd_rawmidi_kernel_write
);