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
)
246 list_for_each_entry(substream
, &s
->substreams
, list
) {
247 if (substream
->opened
) {
248 if (stream
== SNDRV_RAWMIDI_STREAM_INPUT
||
249 !(mode
& SNDRV_RAWMIDI_LFLG_APPEND
) ||
253 if (subdevice
< 0 || subdevice
== substream
->number
) {
254 *sub_ret
= substream
;
261 /* open and do ref-counting for the given substream */
262 static int open_substream(struct snd_rawmidi
*rmidi
,
263 struct snd_rawmidi_substream
*substream
,
268 if (substream
->use_count
== 0) {
269 err
= snd_rawmidi_runtime_create(substream
);
272 err
= substream
->ops
->open(substream
);
274 snd_rawmidi_runtime_free(substream
);
277 substream
->opened
= 1;
278 substream
->active_sensing
= 0;
279 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
)
280 substream
->append
= 1;
281 substream
->pid
= get_pid(task_pid(current
));
282 rmidi
->streams
[substream
->stream
].substream_opened
++;
284 substream
->use_count
++;
288 static void close_substream(struct snd_rawmidi
*rmidi
,
289 struct snd_rawmidi_substream
*substream
,
292 static int rawmidi_open_priv(struct snd_rawmidi
*rmidi
, int subdevice
, int mode
,
293 struct snd_rawmidi_file
*rfile
)
295 struct snd_rawmidi_substream
*sinput
= NULL
, *soutput
= NULL
;
298 rfile
->input
= rfile
->output
= NULL
;
299 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
300 err
= assign_substream(rmidi
, subdevice
,
301 SNDRV_RAWMIDI_STREAM_INPUT
,
306 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
307 err
= assign_substream(rmidi
, subdevice
,
308 SNDRV_RAWMIDI_STREAM_OUTPUT
,
315 err
= open_substream(rmidi
, sinput
, mode
);
320 err
= open_substream(rmidi
, soutput
, mode
);
323 close_substream(rmidi
, sinput
, 0);
328 rfile
->rmidi
= rmidi
;
329 rfile
->input
= sinput
;
330 rfile
->output
= soutput
;
334 /* called from sound/core/seq/seq_midi.c */
335 int snd_rawmidi_kernel_open(struct snd_card
*card
, int device
, int subdevice
,
336 int mode
, struct snd_rawmidi_file
* rfile
)
338 struct snd_rawmidi
*rmidi
;
341 if (snd_BUG_ON(!rfile
))
344 mutex_lock(®ister_mutex
);
345 rmidi
= snd_rawmidi_search(card
, device
);
347 mutex_unlock(®ister_mutex
);
350 if (!try_module_get(rmidi
->card
->module
)) {
351 mutex_unlock(®ister_mutex
);
354 mutex_unlock(®ister_mutex
);
356 mutex_lock(&rmidi
->open_mutex
);
357 err
= rawmidi_open_priv(rmidi
, subdevice
, mode
, rfile
);
358 mutex_unlock(&rmidi
->open_mutex
);
360 module_put(rmidi
->card
->module
);
364 static int snd_rawmidi_open(struct inode
*inode
, struct file
*file
)
366 int maj
= imajor(inode
);
367 struct snd_card
*card
;
369 unsigned short fflags
;
371 struct snd_rawmidi
*rmidi
;
372 struct snd_rawmidi_file
*rawmidi_file
= NULL
;
374 struct snd_ctl_file
*kctl
;
376 if ((file
->f_flags
& O_APPEND
) && !(file
->f_flags
& O_NONBLOCK
))
377 return -EINVAL
; /* invalid combination */
379 err
= nonseekable_open(inode
, file
);
383 if (maj
== snd_major
) {
384 rmidi
= snd_lookup_minor_data(iminor(inode
),
385 SNDRV_DEVICE_TYPE_RAWMIDI
);
386 #ifdef CONFIG_SND_OSSEMUL
387 } else if (maj
== SOUND_MAJOR
) {
388 rmidi
= snd_lookup_oss_minor_data(iminor(inode
),
389 SNDRV_OSS_DEVICE_TYPE_MIDI
);
397 if (!try_module_get(rmidi
->card
->module
))
400 mutex_lock(&rmidi
->open_mutex
);
402 err
= snd_card_file_add(card
, file
);
405 fflags
= snd_rawmidi_file_flags(file
);
406 if ((file
->f_flags
& O_APPEND
) || maj
== SOUND_MAJOR
) /* OSS emul? */
407 fflags
|= SNDRV_RAWMIDI_LFLG_APPEND
;
408 rawmidi_file
= kmalloc(sizeof(*rawmidi_file
), GFP_KERNEL
);
409 if (rawmidi_file
== NULL
) {
413 init_waitqueue_entry(&wait
, current
);
414 add_wait_queue(&rmidi
->open_wait
, &wait
);
417 read_lock(&card
->ctl_files_rwlock
);
418 list_for_each_entry(kctl
, &card
->ctl_files
, list
) {
419 if (kctl
->pid
== task_pid(current
)) {
420 subdevice
= kctl
->prefer_rawmidi_subdevice
;
425 read_unlock(&card
->ctl_files_rwlock
);
426 err
= rawmidi_open_priv(rmidi
, subdevice
, fflags
, rawmidi_file
);
429 if (err
== -EAGAIN
) {
430 if (file
->f_flags
& O_NONBLOCK
) {
436 set_current_state(TASK_INTERRUPTIBLE
);
437 mutex_unlock(&rmidi
->open_mutex
);
439 mutex_lock(&rmidi
->open_mutex
);
440 if (signal_pending(current
)) {
445 remove_wait_queue(&rmidi
->open_wait
, &wait
);
450 #ifdef CONFIG_SND_OSSEMUL
451 if (rawmidi_file
->input
&& rawmidi_file
->input
->runtime
)
452 rawmidi_file
->input
->runtime
->oss
= (maj
== SOUND_MAJOR
);
453 if (rawmidi_file
->output
&& rawmidi_file
->output
->runtime
)
454 rawmidi_file
->output
->runtime
->oss
= (maj
== SOUND_MAJOR
);
456 file
->private_data
= rawmidi_file
;
457 mutex_unlock(&rmidi
->open_mutex
);
461 snd_card_file_remove(card
, file
);
463 mutex_unlock(&rmidi
->open_mutex
);
464 module_put(rmidi
->card
->module
);
468 static void close_substream(struct snd_rawmidi
*rmidi
,
469 struct snd_rawmidi_substream
*substream
,
472 if (--substream
->use_count
)
476 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
477 snd_rawmidi_input_trigger(substream
, 0);
479 if (substream
->active_sensing
) {
480 unsigned char buf
= 0xfe;
481 /* sending single active sensing message
482 * to shut the device up
484 snd_rawmidi_kernel_write(substream
, &buf
, 1);
486 if (snd_rawmidi_drain_output(substream
) == -ERESTARTSYS
)
487 snd_rawmidi_output_trigger(substream
, 0);
490 substream
->ops
->close(substream
);
491 if (substream
->runtime
->private_free
)
492 substream
->runtime
->private_free(substream
);
493 snd_rawmidi_runtime_free(substream
);
494 substream
->opened
= 0;
495 substream
->append
= 0;
496 put_pid(substream
->pid
);
497 substream
->pid
= NULL
;
498 rmidi
->streams
[substream
->stream
].substream_opened
--;
501 static void rawmidi_release_priv(struct snd_rawmidi_file
*rfile
)
503 struct snd_rawmidi
*rmidi
;
505 rmidi
= rfile
->rmidi
;
506 mutex_lock(&rmidi
->open_mutex
);
508 close_substream(rmidi
, rfile
->input
, 1);
512 close_substream(rmidi
, rfile
->output
, 1);
513 rfile
->output
= NULL
;
516 mutex_unlock(&rmidi
->open_mutex
);
517 wake_up(&rmidi
->open_wait
);
520 /* called from sound/core/seq/seq_midi.c */
521 int snd_rawmidi_kernel_release(struct snd_rawmidi_file
*rfile
)
523 struct snd_rawmidi
*rmidi
;
525 if (snd_BUG_ON(!rfile
))
528 rmidi
= rfile
->rmidi
;
529 rawmidi_release_priv(rfile
);
530 module_put(rmidi
->card
->module
);
534 static int snd_rawmidi_release(struct inode
*inode
, struct file
*file
)
536 struct snd_rawmidi_file
*rfile
;
537 struct snd_rawmidi
*rmidi
;
539 rfile
= file
->private_data
;
540 rmidi
= rfile
->rmidi
;
541 rawmidi_release_priv(rfile
);
543 snd_card_file_remove(rmidi
->card
, file
);
544 module_put(rmidi
->card
->module
);
548 static int snd_rawmidi_info(struct snd_rawmidi_substream
*substream
,
549 struct snd_rawmidi_info
*info
)
551 struct snd_rawmidi
*rmidi
;
553 if (substream
== NULL
)
555 rmidi
= substream
->rmidi
;
556 memset(info
, 0, sizeof(*info
));
557 info
->card
= rmidi
->card
->number
;
558 info
->device
= rmidi
->device
;
559 info
->subdevice
= substream
->number
;
560 info
->stream
= substream
->stream
;
561 info
->flags
= rmidi
->info_flags
;
562 strcpy(info
->id
, rmidi
->id
);
563 strcpy(info
->name
, rmidi
->name
);
564 strcpy(info
->subname
, substream
->name
);
565 info
->subdevices_count
= substream
->pstr
->substream_count
;
566 info
->subdevices_avail
= (substream
->pstr
->substream_count
-
567 substream
->pstr
->substream_opened
);
571 static int snd_rawmidi_info_user(struct snd_rawmidi_substream
*substream
,
572 struct snd_rawmidi_info __user
* _info
)
574 struct snd_rawmidi_info info
;
576 if ((err
= snd_rawmidi_info(substream
, &info
)) < 0)
578 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
583 int snd_rawmidi_info_select(struct snd_card
*card
, struct snd_rawmidi_info
*info
)
585 struct snd_rawmidi
*rmidi
;
586 struct snd_rawmidi_str
*pstr
;
587 struct snd_rawmidi_substream
*substream
;
589 mutex_lock(®ister_mutex
);
590 rmidi
= snd_rawmidi_search(card
, info
->device
);
591 mutex_unlock(®ister_mutex
);
594 if (info
->stream
< 0 || info
->stream
> 1)
596 pstr
= &rmidi
->streams
[info
->stream
];
597 if (pstr
->substream_count
== 0)
599 if (info
->subdevice
>= pstr
->substream_count
)
601 list_for_each_entry(substream
, &pstr
->substreams
, list
) {
602 if ((unsigned int)substream
->number
== info
->subdevice
)
603 return snd_rawmidi_info(substream
, info
);
608 static int snd_rawmidi_info_select_user(struct snd_card
*card
,
609 struct snd_rawmidi_info __user
*_info
)
612 struct snd_rawmidi_info info
;
613 if (get_user(info
.device
, &_info
->device
))
615 if (get_user(info
.stream
, &_info
->stream
))
617 if (get_user(info
.subdevice
, &_info
->subdevice
))
619 if ((err
= snd_rawmidi_info_select(card
, &info
)) < 0)
621 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
626 int snd_rawmidi_output_params(struct snd_rawmidi_substream
*substream
,
627 struct snd_rawmidi_params
* params
)
630 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
632 if (substream
->append
&& substream
->use_count
> 1)
634 snd_rawmidi_drain_output(substream
);
635 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
638 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
641 if (params
->buffer_size
!= runtime
->buffer_size
) {
642 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
645 kfree(runtime
->buffer
);
646 runtime
->buffer
= newbuf
;
647 runtime
->buffer_size
= params
->buffer_size
;
648 runtime
->avail
= runtime
->buffer_size
;
650 runtime
->avail_min
= params
->avail_min
;
651 substream
->active_sensing
= !params
->no_active_sensing
;
655 int snd_rawmidi_input_params(struct snd_rawmidi_substream
*substream
,
656 struct snd_rawmidi_params
* params
)
659 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
661 snd_rawmidi_drain_input(substream
);
662 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
665 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
668 if (params
->buffer_size
!= runtime
->buffer_size
) {
669 newbuf
= kmalloc(params
->buffer_size
, GFP_KERNEL
);
672 kfree(runtime
->buffer
);
673 runtime
->buffer
= newbuf
;
674 runtime
->buffer_size
= params
->buffer_size
;
676 runtime
->avail_min
= params
->avail_min
;
680 static int snd_rawmidi_output_status(struct snd_rawmidi_substream
*substream
,
681 struct snd_rawmidi_status
* status
)
683 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
685 memset(status
, 0, sizeof(*status
));
686 status
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
687 spin_lock_irq(&runtime
->lock
);
688 status
->avail
= runtime
->avail
;
689 spin_unlock_irq(&runtime
->lock
);
693 static int snd_rawmidi_input_status(struct snd_rawmidi_substream
*substream
,
694 struct snd_rawmidi_status
* status
)
696 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
698 memset(status
, 0, sizeof(*status
));
699 status
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
700 spin_lock_irq(&runtime
->lock
);
701 status
->avail
= runtime
->avail
;
702 status
->xruns
= runtime
->xruns
;
704 spin_unlock_irq(&runtime
->lock
);
708 static long snd_rawmidi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
710 struct snd_rawmidi_file
*rfile
;
711 void __user
*argp
= (void __user
*)arg
;
713 rfile
= file
->private_data
;
714 if (((cmd
>> 8) & 0xff) != 'W')
717 case SNDRV_RAWMIDI_IOCTL_PVERSION
:
718 return put_user(SNDRV_RAWMIDI_VERSION
, (int __user
*)argp
) ? -EFAULT
: 0;
719 case SNDRV_RAWMIDI_IOCTL_INFO
:
722 struct snd_rawmidi_info __user
*info
= argp
;
723 if (get_user(stream
, &info
->stream
))
726 case SNDRV_RAWMIDI_STREAM_INPUT
:
727 return snd_rawmidi_info_user(rfile
->input
, info
);
728 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
729 return snd_rawmidi_info_user(rfile
->output
, info
);
734 case SNDRV_RAWMIDI_IOCTL_PARAMS
:
736 struct snd_rawmidi_params params
;
737 if (copy_from_user(¶ms
, argp
, sizeof(struct snd_rawmidi_params
)))
739 switch (params
.stream
) {
740 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
741 if (rfile
->output
== NULL
)
743 return snd_rawmidi_output_params(rfile
->output
, ¶ms
);
744 case SNDRV_RAWMIDI_STREAM_INPUT
:
745 if (rfile
->input
== NULL
)
747 return snd_rawmidi_input_params(rfile
->input
, ¶ms
);
752 case SNDRV_RAWMIDI_IOCTL_STATUS
:
755 struct snd_rawmidi_status status
;
756 if (copy_from_user(&status
, argp
, sizeof(struct snd_rawmidi_status
)))
758 switch (status
.stream
) {
759 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
760 if (rfile
->output
== NULL
)
762 err
= snd_rawmidi_output_status(rfile
->output
, &status
);
764 case SNDRV_RAWMIDI_STREAM_INPUT
:
765 if (rfile
->input
== NULL
)
767 err
= snd_rawmidi_input_status(rfile
->input
, &status
);
774 if (copy_to_user(argp
, &status
, sizeof(struct snd_rawmidi_status
)))
778 case SNDRV_RAWMIDI_IOCTL_DROP
:
781 if (get_user(val
, (int __user
*) argp
))
784 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
785 if (rfile
->output
== NULL
)
787 return snd_rawmidi_drop_output(rfile
->output
);
792 case SNDRV_RAWMIDI_IOCTL_DRAIN
:
795 if (get_user(val
, (int __user
*) argp
))
798 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
799 if (rfile
->output
== NULL
)
801 return snd_rawmidi_drain_output(rfile
->output
);
802 case SNDRV_RAWMIDI_STREAM_INPUT
:
803 if (rfile
->input
== NULL
)
805 return snd_rawmidi_drain_input(rfile
->input
);
810 #ifdef CONFIG_SND_DEBUG
812 snd_printk(KERN_WARNING
"rawmidi: unknown command = 0x%x\n", cmd
);
818 static int snd_rawmidi_control_ioctl(struct snd_card
*card
,
819 struct snd_ctl_file
*control
,
823 void __user
*argp
= (void __user
*)arg
;
826 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE
:
830 if (get_user(device
, (int __user
*)argp
))
832 mutex_lock(®ister_mutex
);
833 device
= device
< 0 ? 0 : device
+ 1;
834 while (device
< SNDRV_RAWMIDI_DEVICES
) {
835 if (snd_rawmidi_search(card
, device
))
839 if (device
== SNDRV_RAWMIDI_DEVICES
)
841 mutex_unlock(®ister_mutex
);
842 if (put_user(device
, (int __user
*)argp
))
846 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE
:
850 if (get_user(val
, (int __user
*)argp
))
852 control
->prefer_rawmidi_subdevice
= val
;
855 case SNDRV_CTL_IOCTL_RAWMIDI_INFO
:
856 return snd_rawmidi_info_select_user(card
, argp
);
862 * snd_rawmidi_receive - receive the input data from the device
863 * @substream: the rawmidi substream
864 * @buffer: the buffer pointer
865 * @count: the data size to read
867 * Reads the data from the internal buffer.
869 * Returns the size of read data, or a negative error code on failure.
871 int snd_rawmidi_receive(struct snd_rawmidi_substream
*substream
,
872 const unsigned char *buffer
, int count
)
875 int result
= 0, count1
;
876 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
878 if (!substream
->opened
)
880 if (runtime
->buffer
== NULL
) {
881 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
884 spin_lock_irqsave(&runtime
->lock
, flags
);
885 if (count
== 1) { /* special case, faster code */
887 if (runtime
->avail
< runtime
->buffer_size
) {
888 runtime
->buffer
[runtime
->hw_ptr
++] = buffer
[0];
889 runtime
->hw_ptr
%= runtime
->buffer_size
;
896 substream
->bytes
+= count
;
897 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
900 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
901 count1
= runtime
->buffer_size
- runtime
->avail
;
902 memcpy(runtime
->buffer
+ runtime
->hw_ptr
, buffer
, count1
);
903 runtime
->hw_ptr
+= count1
;
904 runtime
->hw_ptr
%= runtime
->buffer_size
;
905 runtime
->avail
+= count1
;
911 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
)) {
912 count1
= runtime
->buffer_size
- runtime
->avail
;
913 runtime
->xruns
+= count
- count1
;
916 memcpy(runtime
->buffer
, buffer
, count1
);
917 runtime
->hw_ptr
= count1
;
918 runtime
->avail
+= count1
;
925 tasklet_schedule(&runtime
->tasklet
);
926 else if (snd_rawmidi_ready(substream
))
927 wake_up(&runtime
->sleep
);
929 spin_unlock_irqrestore(&runtime
->lock
, flags
);
933 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream
*substream
,
934 unsigned char __user
*userbuf
,
935 unsigned char *kernelbuf
, long count
)
938 long result
= 0, count1
;
939 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
941 while (count
> 0 && runtime
->avail
) {
942 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
945 spin_lock_irqsave(&runtime
->lock
, flags
);
946 if (count1
> (int)runtime
->avail
)
947 count1
= runtime
->avail
;
949 memcpy(kernelbuf
+ result
, runtime
->buffer
+ runtime
->appl_ptr
, count1
);
951 spin_unlock_irqrestore(&runtime
->lock
, flags
);
952 if (copy_to_user(userbuf
+ result
,
953 runtime
->buffer
+ runtime
->appl_ptr
, count1
)) {
954 return result
> 0 ? result
: -EFAULT
;
956 spin_lock_irqsave(&runtime
->lock
, flags
);
958 runtime
->appl_ptr
+= count1
;
959 runtime
->appl_ptr
%= runtime
->buffer_size
;
960 runtime
->avail
-= count1
;
961 spin_unlock_irqrestore(&runtime
->lock
, flags
);
968 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream
*substream
,
969 unsigned char *buf
, long count
)
971 snd_rawmidi_input_trigger(substream
, 1);
972 return snd_rawmidi_kernel_read1(substream
, NULL
/*userbuf*/, buf
, count
);
975 static ssize_t
snd_rawmidi_read(struct file
*file
, char __user
*buf
, size_t count
,
980 struct snd_rawmidi_file
*rfile
;
981 struct snd_rawmidi_substream
*substream
;
982 struct snd_rawmidi_runtime
*runtime
;
984 rfile
= file
->private_data
;
985 substream
= rfile
->input
;
986 if (substream
== NULL
)
988 runtime
= substream
->runtime
;
989 snd_rawmidi_input_trigger(substream
, 1);
992 spin_lock_irq(&runtime
->lock
);
993 while (!snd_rawmidi_ready(substream
)) {
995 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
996 spin_unlock_irq(&runtime
->lock
);
997 return result
> 0 ? result
: -EAGAIN
;
999 init_waitqueue_entry(&wait
, current
);
1000 add_wait_queue(&runtime
->sleep
, &wait
);
1001 set_current_state(TASK_INTERRUPTIBLE
);
1002 spin_unlock_irq(&runtime
->lock
);
1004 remove_wait_queue(&runtime
->sleep
, &wait
);
1005 if (signal_pending(current
))
1006 return result
> 0 ? result
: -ERESTARTSYS
;
1007 if (!runtime
->avail
)
1008 return result
> 0 ? result
: -EIO
;
1009 spin_lock_irq(&runtime
->lock
);
1011 spin_unlock_irq(&runtime
->lock
);
1012 count1
= snd_rawmidi_kernel_read1(substream
,
1013 (unsigned char __user
*)buf
,
1017 return result
> 0 ? result
: count1
;
1026 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1027 * @substream: the rawmidi substream
1029 * Returns 1 if the internal output buffer is empty, 0 if not.
1031 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream
*substream
)
1033 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1035 unsigned long flags
;
1037 if (runtime
->buffer
== NULL
) {
1038 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1041 spin_lock_irqsave(&runtime
->lock
, flags
);
1042 result
= runtime
->avail
>= runtime
->buffer_size
;
1043 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1048 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1049 * @substream: the rawmidi substream
1050 * @buffer: the buffer pointer
1051 * @count: data size to transfer
1053 * Copies data from the internal output buffer to the given buffer.
1055 * Call this in the interrupt handler when the midi output is ready,
1056 * and call snd_rawmidi_transmit_ack() after the transmission is
1059 * Returns the size of copied data, or a negative error code on failure.
1061 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1062 unsigned char *buffer
, int count
)
1064 unsigned long flags
;
1066 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1068 if (runtime
->buffer
== NULL
) {
1069 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1073 spin_lock_irqsave(&runtime
->lock
, flags
);
1074 if (runtime
->avail
>= runtime
->buffer_size
) {
1075 /* warning: lowlevel layer MUST trigger down the hardware */
1078 if (count
== 1) { /* special case, faster code */
1079 *buffer
= runtime
->buffer
[runtime
->hw_ptr
];
1082 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
1085 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
1086 count1
= runtime
->buffer_size
- runtime
->avail
;
1087 memcpy(buffer
, runtime
->buffer
+ runtime
->hw_ptr
, count1
);
1091 if (count
> (int)(runtime
->buffer_size
- runtime
->avail
- count1
))
1092 count
= runtime
->buffer_size
- runtime
->avail
- count1
;
1093 memcpy(buffer
+ count1
, runtime
->buffer
, count
);
1098 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1103 * snd_rawmidi_transmit_ack - acknowledge the transmission
1104 * @substream: the rawmidi substream
1105 * @count: the tranferred count
1107 * Advances the hardware pointer for the internal output buffer with
1108 * the given size and updates the condition.
1109 * Call after the transmission is finished.
1111 * Returns the advanced size if successful, or a negative error code on failure.
1113 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1115 unsigned long flags
;
1116 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1118 if (runtime
->buffer
== NULL
) {
1119 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1122 spin_lock_irqsave(&runtime
->lock
, flags
);
1123 snd_BUG_ON(runtime
->avail
+ count
> runtime
->buffer_size
);
1124 runtime
->hw_ptr
+= count
;
1125 runtime
->hw_ptr
%= runtime
->buffer_size
;
1126 runtime
->avail
+= count
;
1127 substream
->bytes
+= count
;
1129 if (runtime
->drain
|| snd_rawmidi_ready(substream
))
1130 wake_up(&runtime
->sleep
);
1132 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1137 * snd_rawmidi_transmit - copy from the buffer to the device
1138 * @substream: the rawmidi substream
1139 * @buffer: the buffer pointer
1140 * @count: the data size to transfer
1142 * Copies data from the buffer to the device and advances the pointer.
1144 * Returns the copied size if successful, or a negative error code on failure.
1146 int snd_rawmidi_transmit(struct snd_rawmidi_substream
*substream
,
1147 unsigned char *buffer
, int count
)
1149 if (!substream
->opened
)
1151 count
= snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1154 return snd_rawmidi_transmit_ack(substream
, count
);
1157 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream
*substream
,
1158 const unsigned char __user
*userbuf
,
1159 const unsigned char *kernelbuf
,
1162 unsigned long flags
;
1163 long count1
, result
;
1164 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1166 if (snd_BUG_ON(!kernelbuf
&& !userbuf
))
1168 if (snd_BUG_ON(!runtime
->buffer
))
1172 spin_lock_irqsave(&runtime
->lock
, flags
);
1173 if (substream
->append
) {
1174 if ((long)runtime
->avail
< count
) {
1175 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1179 while (count
> 0 && runtime
->avail
> 0) {
1180 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1183 if (count1
> (long)runtime
->avail
)
1184 count1
= runtime
->avail
;
1186 memcpy(runtime
->buffer
+ runtime
->appl_ptr
,
1187 kernelbuf
+ result
, count1
);
1189 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1190 if (copy_from_user(runtime
->buffer
+ runtime
->appl_ptr
,
1191 userbuf
+ result
, count1
)) {
1192 spin_lock_irqsave(&runtime
->lock
, flags
);
1193 result
= result
> 0 ? result
: -EFAULT
;
1196 spin_lock_irqsave(&runtime
->lock
, flags
);
1198 runtime
->appl_ptr
+= count1
;
1199 runtime
->appl_ptr
%= runtime
->buffer_size
;
1200 runtime
->avail
-= count1
;
1205 count1
= runtime
->avail
< runtime
->buffer_size
;
1206 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1208 snd_rawmidi_output_trigger(substream
, 1);
1212 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream
*substream
,
1213 const unsigned char *buf
, long count
)
1215 return snd_rawmidi_kernel_write1(substream
, NULL
, buf
, count
);
1218 static ssize_t
snd_rawmidi_write(struct file
*file
, const char __user
*buf
,
1219 size_t count
, loff_t
*offset
)
1221 long result
, timeout
;
1223 struct snd_rawmidi_file
*rfile
;
1224 struct snd_rawmidi_runtime
*runtime
;
1225 struct snd_rawmidi_substream
*substream
;
1227 rfile
= file
->private_data
;
1228 substream
= rfile
->output
;
1229 runtime
= substream
->runtime
;
1230 /* we cannot put an atomic message to our buffer */
1231 if (substream
->append
&& count
> runtime
->buffer_size
)
1235 spin_lock_irq(&runtime
->lock
);
1236 while (!snd_rawmidi_ready_append(substream
, count
)) {
1238 if (file
->f_flags
& O_NONBLOCK
) {
1239 spin_unlock_irq(&runtime
->lock
);
1240 return result
> 0 ? result
: -EAGAIN
;
1242 init_waitqueue_entry(&wait
, current
);
1243 add_wait_queue(&runtime
->sleep
, &wait
);
1244 set_current_state(TASK_INTERRUPTIBLE
);
1245 spin_unlock_irq(&runtime
->lock
);
1246 timeout
= schedule_timeout(30 * HZ
);
1247 remove_wait_queue(&runtime
->sleep
, &wait
);
1248 if (signal_pending(current
))
1249 return result
> 0 ? result
: -ERESTARTSYS
;
1250 if (!runtime
->avail
&& !timeout
)
1251 return result
> 0 ? result
: -EIO
;
1252 spin_lock_irq(&runtime
->lock
);
1254 spin_unlock_irq(&runtime
->lock
);
1255 count1
= snd_rawmidi_kernel_write1(substream
, buf
, NULL
, count
);
1257 return result
> 0 ? result
: count1
;
1260 if ((size_t)count1
< count
&& (file
->f_flags
& O_NONBLOCK
))
1264 if (file
->f_flags
& O_DSYNC
) {
1265 spin_lock_irq(&runtime
->lock
);
1266 while (runtime
->avail
!= runtime
->buffer_size
) {
1268 unsigned int last_avail
= runtime
->avail
;
1269 init_waitqueue_entry(&wait
, current
);
1270 add_wait_queue(&runtime
->sleep
, &wait
);
1271 set_current_state(TASK_INTERRUPTIBLE
);
1272 spin_unlock_irq(&runtime
->lock
);
1273 timeout
= schedule_timeout(30 * HZ
);
1274 remove_wait_queue(&runtime
->sleep
, &wait
);
1275 if (signal_pending(current
))
1276 return result
> 0 ? result
: -ERESTARTSYS
;
1277 if (runtime
->avail
== last_avail
&& !timeout
)
1278 return result
> 0 ? result
: -EIO
;
1279 spin_lock_irq(&runtime
->lock
);
1281 spin_unlock_irq(&runtime
->lock
);
1286 static unsigned int snd_rawmidi_poll(struct file
*file
, poll_table
* wait
)
1288 struct snd_rawmidi_file
*rfile
;
1289 struct snd_rawmidi_runtime
*runtime
;
1292 rfile
= file
->private_data
;
1293 if (rfile
->input
!= NULL
) {
1294 runtime
= rfile
->input
->runtime
;
1295 snd_rawmidi_input_trigger(rfile
->input
, 1);
1296 poll_wait(file
, &runtime
->sleep
, wait
);
1298 if (rfile
->output
!= NULL
) {
1299 runtime
= rfile
->output
->runtime
;
1300 poll_wait(file
, &runtime
->sleep
, wait
);
1303 if (rfile
->input
!= NULL
) {
1304 if (snd_rawmidi_ready(rfile
->input
))
1305 mask
|= POLLIN
| POLLRDNORM
;
1307 if (rfile
->output
!= NULL
) {
1308 if (snd_rawmidi_ready(rfile
->output
))
1309 mask
|= POLLOUT
| POLLWRNORM
;
1316 #ifdef CONFIG_COMPAT
1317 #include "rawmidi_compat.c"
1319 #define snd_rawmidi_ioctl_compat NULL
1326 static void snd_rawmidi_proc_info_read(struct snd_info_entry
*entry
,
1327 struct snd_info_buffer
*buffer
)
1329 struct snd_rawmidi
*rmidi
;
1330 struct snd_rawmidi_substream
*substream
;
1331 struct snd_rawmidi_runtime
*runtime
;
1333 rmidi
= entry
->private_data
;
1334 snd_iprintf(buffer
, "%s\n\n", rmidi
->name
);
1335 mutex_lock(&rmidi
->open_mutex
);
1336 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) {
1337 list_for_each_entry(substream
,
1338 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
,
1342 " Tx bytes : %lu\n",
1344 (unsigned long) substream
->bytes
);
1345 if (substream
->opened
) {
1347 " Owner PID : %d\n",
1348 pid_vnr(substream
->pid
));
1349 runtime
= substream
->runtime
;
1352 " Buffer size : %lu\n"
1354 runtime
->oss
? "OSS compatible" : "native",
1355 (unsigned long) runtime
->buffer_size
,
1356 (unsigned long) runtime
->avail
);
1360 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
) {
1361 list_for_each_entry(substream
,
1362 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
,
1366 " Rx bytes : %lu\n",
1368 (unsigned long) substream
->bytes
);
1369 if (substream
->opened
) {
1371 " Owner PID : %d\n",
1372 pid_vnr(substream
->pid
));
1373 runtime
= substream
->runtime
;
1375 " Buffer size : %lu\n"
1377 " Overruns : %lu\n",
1378 (unsigned long) runtime
->buffer_size
,
1379 (unsigned long) runtime
->avail
,
1380 (unsigned long) runtime
->xruns
);
1384 mutex_unlock(&rmidi
->open_mutex
);
1388 * Register functions
1391 static const struct file_operations snd_rawmidi_f_ops
=
1393 .owner
= THIS_MODULE
,
1394 .read
= snd_rawmidi_read
,
1395 .write
= snd_rawmidi_write
,
1396 .open
= snd_rawmidi_open
,
1397 .release
= snd_rawmidi_release
,
1398 .llseek
= no_llseek
,
1399 .poll
= snd_rawmidi_poll
,
1400 .unlocked_ioctl
= snd_rawmidi_ioctl
,
1401 .compat_ioctl
= snd_rawmidi_ioctl_compat
,
1404 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi
*rmidi
,
1405 struct snd_rawmidi_str
*stream
,
1409 struct snd_rawmidi_substream
*substream
;
1412 for (idx
= 0; idx
< count
; idx
++) {
1413 substream
= kzalloc(sizeof(*substream
), GFP_KERNEL
);
1414 if (substream
== NULL
) {
1415 snd_printk(KERN_ERR
"rawmidi: cannot allocate substream\n");
1418 substream
->stream
= direction
;
1419 substream
->number
= idx
;
1420 substream
->rmidi
= rmidi
;
1421 substream
->pstr
= stream
;
1422 list_add_tail(&substream
->list
, &stream
->substreams
);
1423 stream
->substream_count
++;
1429 * snd_rawmidi_new - create a rawmidi instance
1430 * @card: the card instance
1431 * @id: the id string
1432 * @device: the device index
1433 * @output_count: the number of output streams
1434 * @input_count: the number of input streams
1435 * @rrawmidi: the pointer to store the new rawmidi instance
1437 * Creates a new rawmidi instance.
1438 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1440 * Returns zero if successful, or a negative error code on failure.
1442 int snd_rawmidi_new(struct snd_card
*card
, char *id
, int device
,
1443 int output_count
, int input_count
,
1444 struct snd_rawmidi
** rrawmidi
)
1446 struct snd_rawmidi
*rmidi
;
1448 static struct snd_device_ops ops
= {
1449 .dev_free
= snd_rawmidi_dev_free
,
1450 .dev_register
= snd_rawmidi_dev_register
,
1451 .dev_disconnect
= snd_rawmidi_dev_disconnect
,
1454 if (snd_BUG_ON(!card
))
1458 rmidi
= kzalloc(sizeof(*rmidi
), GFP_KERNEL
);
1459 if (rmidi
== NULL
) {
1460 snd_printk(KERN_ERR
"rawmidi: cannot allocate\n");
1464 rmidi
->device
= device
;
1465 mutex_init(&rmidi
->open_mutex
);
1466 init_waitqueue_head(&rmidi
->open_wait
);
1467 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
);
1468 INIT_LIST_HEAD(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
);
1471 strlcpy(rmidi
->id
, id
, sizeof(rmidi
->id
));
1472 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1473 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
],
1474 SNDRV_RAWMIDI_STREAM_INPUT
,
1475 input_count
)) < 0) {
1476 snd_rawmidi_free(rmidi
);
1479 if ((err
= snd_rawmidi_alloc_substreams(rmidi
,
1480 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
],
1481 SNDRV_RAWMIDI_STREAM_OUTPUT
,
1482 output_count
)) < 0) {
1483 snd_rawmidi_free(rmidi
);
1486 if ((err
= snd_device_new(card
, SNDRV_DEV_RAWMIDI
, rmidi
, &ops
)) < 0) {
1487 snd_rawmidi_free(rmidi
);
1495 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str
*stream
)
1497 struct snd_rawmidi_substream
*substream
;
1499 while (!list_empty(&stream
->substreams
)) {
1500 substream
= list_entry(stream
->substreams
.next
, struct snd_rawmidi_substream
, list
);
1501 list_del(&substream
->list
);
1506 static int snd_rawmidi_free(struct snd_rawmidi
*rmidi
)
1511 snd_info_free_entry(rmidi
->proc_entry
);
1512 rmidi
->proc_entry
= NULL
;
1513 mutex_lock(®ister_mutex
);
1514 if (rmidi
->ops
&& rmidi
->ops
->dev_unregister
)
1515 rmidi
->ops
->dev_unregister(rmidi
);
1516 mutex_unlock(®ister_mutex
);
1518 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
1519 snd_rawmidi_free_substreams(&rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
1520 if (rmidi
->private_free
)
1521 rmidi
->private_free(rmidi
);
1526 static int snd_rawmidi_dev_free(struct snd_device
*device
)
1528 struct snd_rawmidi
*rmidi
= device
->device_data
;
1529 return snd_rawmidi_free(rmidi
);
1532 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1533 static void snd_rawmidi_dev_seq_free(struct snd_seq_device
*device
)
1535 struct snd_rawmidi
*rmidi
= device
->private_data
;
1536 rmidi
->seq_dev
= NULL
;
1540 static int snd_rawmidi_dev_register(struct snd_device
*device
)
1543 struct snd_info_entry
*entry
;
1545 struct snd_rawmidi
*rmidi
= device
->device_data
;
1547 if (rmidi
->device
>= SNDRV_RAWMIDI_DEVICES
)
1549 mutex_lock(®ister_mutex
);
1550 if (snd_rawmidi_search(rmidi
->card
, rmidi
->device
)) {
1551 mutex_unlock(®ister_mutex
);
1554 list_add_tail(&rmidi
->list
, &snd_rawmidi_devices
);
1555 sprintf(name
, "midiC%iD%i", rmidi
->card
->number
, rmidi
->device
);
1556 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI
,
1557 rmidi
->card
, rmidi
->device
,
1558 &snd_rawmidi_f_ops
, rmidi
, name
)) < 0) {
1559 snd_printk(KERN_ERR
"unable to register rawmidi device %i:%i\n", rmidi
->card
->number
, rmidi
->device
);
1560 list_del(&rmidi
->list
);
1561 mutex_unlock(®ister_mutex
);
1564 if (rmidi
->ops
&& rmidi
->ops
->dev_register
&&
1565 (err
= rmidi
->ops
->dev_register(rmidi
)) < 0) {
1566 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1567 list_del(&rmidi
->list
);
1568 mutex_unlock(®ister_mutex
);
1571 #ifdef CONFIG_SND_OSSEMUL
1573 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1574 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1575 rmidi
->card
, 0, &snd_rawmidi_f_ops
,
1577 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 0);
1580 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1581 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
, rmidi
->name
);
1585 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
]) {
1586 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
,
1587 rmidi
->card
, 1, &snd_rawmidi_f_ops
,
1589 snd_printk(KERN_ERR
"unable to register OSS rawmidi device %i:%i\n", rmidi
->card
->number
, 1);
1594 #endif /* CONFIG_SND_OSSEMUL */
1595 mutex_unlock(®ister_mutex
);
1596 sprintf(name
, "midi%d", rmidi
->device
);
1597 entry
= snd_info_create_card_entry(rmidi
->card
, name
, rmidi
->card
->proc_root
);
1599 entry
->private_data
= rmidi
;
1600 entry
->c
.text
.read
= snd_rawmidi_proc_info_read
;
1601 if (snd_info_register(entry
) < 0) {
1602 snd_info_free_entry(entry
);
1606 rmidi
->proc_entry
= entry
;
1607 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1608 if (!rmidi
->ops
|| !rmidi
->ops
->dev_register
) { /* own registration mechanism */
1609 if (snd_seq_device_new(rmidi
->card
, rmidi
->device
, SNDRV_SEQ_DEV_ID_MIDISYNTH
, 0, &rmidi
->seq_dev
) >= 0) {
1610 rmidi
->seq_dev
->private_data
= rmidi
;
1611 rmidi
->seq_dev
->private_free
= snd_rawmidi_dev_seq_free
;
1612 sprintf(rmidi
->seq_dev
->name
, "MIDI %d-%d", rmidi
->card
->number
, rmidi
->device
);
1613 snd_device_register(rmidi
->card
, rmidi
->seq_dev
);
1620 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
)
1622 struct snd_rawmidi
*rmidi
= device
->device_data
;
1624 mutex_lock(®ister_mutex
);
1625 list_del_init(&rmidi
->list
);
1626 #ifdef CONFIG_SND_OSSEMUL
1627 if (rmidi
->ossreg
) {
1628 if ((int)rmidi
->device
== midi_map
[rmidi
->card
->number
]) {
1629 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 0);
1630 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1631 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI
, rmidi
->card
->number
);
1634 if ((int)rmidi
->device
== amidi_map
[rmidi
->card
->number
])
1635 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI
, rmidi
->card
, 1);
1638 #endif /* CONFIG_SND_OSSEMUL */
1639 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI
, rmidi
->card
, rmidi
->device
);
1640 mutex_unlock(®ister_mutex
);
1645 * snd_rawmidi_set_ops - set the rawmidi operators
1646 * @rmidi: the rawmidi instance
1647 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1648 * @ops: the operator table
1650 * Sets the rawmidi operators for the given stream direction.
1652 void snd_rawmidi_set_ops(struct snd_rawmidi
*rmidi
, int stream
,
1653 struct snd_rawmidi_ops
*ops
)
1655 struct snd_rawmidi_substream
*substream
;
1657 list_for_each_entry(substream
, &rmidi
->streams
[stream
].substreams
, list
)
1658 substream
->ops
= ops
;
1665 static int __init
alsa_rawmidi_init(void)
1668 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl
);
1669 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl
);
1670 #ifdef CONFIG_SND_OSSEMUL
1672 /* check device map table */
1673 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1674 if (midi_map
[i
] < 0 || midi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1675 snd_printk(KERN_ERR
"invalid midi_map[%d] = %d\n", i
, midi_map
[i
]);
1678 if (amidi_map
[i
] < 0 || amidi_map
[i
] >= SNDRV_RAWMIDI_DEVICES
) {
1679 snd_printk(KERN_ERR
"invalid amidi_map[%d] = %d\n", i
, amidi_map
[i
]);
1684 #endif /* CONFIG_SND_OSSEMUL */
1688 static void __exit
alsa_rawmidi_exit(void)
1690 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl
);
1691 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl
);
1694 module_init(alsa_rawmidi_init
)
1695 module_exit(alsa_rawmidi_exit
)
1697 EXPORT_SYMBOL(snd_rawmidi_output_params
);
1698 EXPORT_SYMBOL(snd_rawmidi_input_params
);
1699 EXPORT_SYMBOL(snd_rawmidi_drop_output
);
1700 EXPORT_SYMBOL(snd_rawmidi_drain_output
);
1701 EXPORT_SYMBOL(snd_rawmidi_drain_input
);
1702 EXPORT_SYMBOL(snd_rawmidi_receive
);
1703 EXPORT_SYMBOL(snd_rawmidi_transmit_empty
);
1704 EXPORT_SYMBOL(snd_rawmidi_transmit_peek
);
1705 EXPORT_SYMBOL(snd_rawmidi_transmit_ack
);
1706 EXPORT_SYMBOL(snd_rawmidi_transmit
);
1707 EXPORT_SYMBOL(snd_rawmidi_new
);
1708 EXPORT_SYMBOL(snd_rawmidi_set_ops
);
1709 EXPORT_SYMBOL(snd_rawmidi_info_select
);
1710 EXPORT_SYMBOL(snd_rawmidi_kernel_open
);
1711 EXPORT_SYMBOL(snd_rawmidi_kernel_release
);
1712 EXPORT_SYMBOL(snd_rawmidi_kernel_read
);
1713 EXPORT_SYMBOL(snd_rawmidi_kernel_write
);