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/module.h>
27 #include <linux/slab.h>
28 #include <linux/time.h>
29 #include <linux/wait.h>
30 #include <linux/mutex.h>
31 #include <linux/moduleparam.h>
32 #include <linux/delay.h>
33 #include <sound/rawmidi.h>
34 #include <sound/info.h>
35 #include <sound/control.h>
36 #include <sound/minors.h>
37 #include <sound/initval.h>
39 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
40 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
41 MODULE_LICENSE("GPL");
43 #ifdef CONFIG_SND_OSSEMUL
44 static int midi_map
[SNDRV_CARDS
];
45 static int amidi_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
46 module_param_array(midi_map
, int, NULL
, 0444);
47 MODULE_PARM_DESC(midi_map
, "Raw MIDI device number assigned to 1st OSS device.");
48 module_param_array(amidi_map
, int, NULL
, 0444);
49 MODULE_PARM_DESC(amidi_map
, "Raw MIDI device number assigned to 2nd OSS device.");
50 #endif /* CONFIG_SND_OSSEMUL */
52 static int snd_rawmidi_free(struct snd_rawmidi
*rawmidi
);
53 static int snd_rawmidi_dev_free(struct snd_device
*device
);
54 static int snd_rawmidi_dev_register(struct snd_device
*device
);
55 static int snd_rawmidi_dev_disconnect(struct snd_device
*device
);
57 static LIST_HEAD(snd_rawmidi_devices
);
58 static DEFINE_MUTEX(register_mutex
);
60 static struct snd_rawmidi
*snd_rawmidi_search(struct snd_card
*card
, int device
)
62 struct snd_rawmidi
*rawmidi
;
64 list_for_each_entry(rawmidi
, &snd_rawmidi_devices
, list
)
65 if (rawmidi
->card
== card
&& rawmidi
->device
== device
)
70 static inline unsigned short snd_rawmidi_file_flags(struct file
*file
)
72 switch (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) {
74 return SNDRV_RAWMIDI_LFLG_OUTPUT
;
76 return SNDRV_RAWMIDI_LFLG_INPUT
;
78 return SNDRV_RAWMIDI_LFLG_OPEN
;
82 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream
*substream
)
84 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
85 return runtime
->avail
>= runtime
->avail_min
;
88 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream
*substream
,
91 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
92 return runtime
->avail
>= runtime
->avail_min
&&
93 (!substream
->append
|| runtime
->avail
>= count
);
96 static void snd_rawmidi_input_event_work(struct work_struct
*work
)
98 struct snd_rawmidi_runtime
*runtime
=
99 container_of(work
, struct snd_rawmidi_runtime
, event_work
);
101 runtime
->event(runtime
->substream
);
104 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream
*substream
)
106 struct snd_rawmidi_runtime
*runtime
;
108 if ((runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
)) == NULL
)
110 runtime
->substream
= substream
;
111 spin_lock_init(&runtime
->lock
);
112 init_waitqueue_head(&runtime
->sleep
);
113 INIT_WORK(&runtime
->event_work
, snd_rawmidi_input_event_work
);
114 runtime
->event
= NULL
;
115 runtime
->buffer_size
= PAGE_SIZE
;
116 runtime
->avail_min
= 1;
117 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
120 runtime
->avail
= runtime
->buffer_size
;
121 if ((runtime
->buffer
= kmalloc(runtime
->buffer_size
, GFP_KERNEL
)) == NULL
) {
125 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
126 substream
->runtime
= runtime
;
130 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream
*substream
)
132 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
134 kfree(runtime
->buffer
);
136 substream
->runtime
= NULL
;
140 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream
*substream
,int up
)
142 if (!substream
->opened
)
144 substream
->ops
->trigger(substream
, up
);
147 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
149 if (!substream
->opened
)
151 substream
->ops
->trigger(substream
, up
);
153 cancel_work_sync(&substream
->runtime
->event_work
);
156 int snd_rawmidi_drop_output(struct snd_rawmidi_substream
*substream
)
159 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
161 snd_rawmidi_output_trigger(substream
, 0);
163 spin_lock_irqsave(&runtime
->lock
, flags
);
164 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
165 runtime
->avail
= runtime
->buffer_size
;
166 spin_unlock_irqrestore(&runtime
->lock
, flags
);
170 int snd_rawmidi_drain_output(struct snd_rawmidi_substream
*substream
)
174 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
178 timeout
= wait_event_interruptible_timeout(runtime
->sleep
,
179 (runtime
->avail
>= runtime
->buffer_size
),
181 if (signal_pending(current
))
183 if (runtime
->avail
< runtime
->buffer_size
&& !timeout
) {
184 snd_printk(KERN_WARNING
"rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime
->avail
, (long)runtime
->buffer_size
);
188 if (err
!= -ERESTARTSYS
) {
189 /* we need wait a while to make sure that Tx FIFOs are empty */
190 if (substream
->ops
->drain
)
191 substream
->ops
->drain(substream
);
194 snd_rawmidi_drop_output(substream
);
199 int snd_rawmidi_drain_input(struct snd_rawmidi_substream
*substream
)
202 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
204 snd_rawmidi_input_trigger(substream
, 0);
206 spin_lock_irqsave(&runtime
->lock
, flags
);
207 runtime
->appl_ptr
= runtime
->hw_ptr
= 0;
209 spin_unlock_irqrestore(&runtime
->lock
, flags
);
213 /* look for an available substream for the given stream direction;
214 * if a specific subdevice is given, try to assign it
216 static int assign_substream(struct snd_rawmidi
*rmidi
, int subdevice
,
217 int stream
, int mode
,
218 struct snd_rawmidi_substream
**sub_ret
)
220 struct snd_rawmidi_substream
*substream
;
221 struct snd_rawmidi_str
*s
= &rmidi
->streams
[stream
];
222 static unsigned int info_flags
[2] = {
223 [SNDRV_RAWMIDI_STREAM_OUTPUT
] = SNDRV_RAWMIDI_INFO_OUTPUT
,
224 [SNDRV_RAWMIDI_STREAM_INPUT
] = SNDRV_RAWMIDI_INFO_INPUT
,
227 if (!(rmidi
->info_flags
& info_flags
[stream
]))
229 if (subdevice
>= 0 && subdevice
>= s
->substream_count
)
232 list_for_each_entry(substream
, &s
->substreams
, list
) {
233 if (substream
->opened
) {
234 if (stream
== SNDRV_RAWMIDI_STREAM_INPUT
||
235 !(mode
& SNDRV_RAWMIDI_LFLG_APPEND
) ||
239 if (subdevice
< 0 || subdevice
== substream
->number
) {
240 *sub_ret
= substream
;
247 /* open and do ref-counting for the given substream */
248 static int open_substream(struct snd_rawmidi
*rmidi
,
249 struct snd_rawmidi_substream
*substream
,
254 if (substream
->use_count
== 0) {
255 err
= snd_rawmidi_runtime_create(substream
);
258 err
= substream
->ops
->open(substream
);
260 snd_rawmidi_runtime_free(substream
);
263 substream
->opened
= 1;
264 substream
->active_sensing
= 0;
265 if (mode
& SNDRV_RAWMIDI_LFLG_APPEND
)
266 substream
->append
= 1;
267 substream
->pid
= get_pid(task_pid(current
));
268 rmidi
->streams
[substream
->stream
].substream_opened
++;
270 substream
->use_count
++;
274 static void close_substream(struct snd_rawmidi
*rmidi
,
275 struct snd_rawmidi_substream
*substream
,
278 static int rawmidi_open_priv(struct snd_rawmidi
*rmidi
, int subdevice
, int mode
,
279 struct snd_rawmidi_file
*rfile
)
281 struct snd_rawmidi_substream
*sinput
= NULL
, *soutput
= NULL
;
284 rfile
->input
= rfile
->output
= NULL
;
285 if (mode
& SNDRV_RAWMIDI_LFLG_INPUT
) {
286 err
= assign_substream(rmidi
, subdevice
,
287 SNDRV_RAWMIDI_STREAM_INPUT
,
292 if (mode
& SNDRV_RAWMIDI_LFLG_OUTPUT
) {
293 err
= assign_substream(rmidi
, subdevice
,
294 SNDRV_RAWMIDI_STREAM_OUTPUT
,
301 err
= open_substream(rmidi
, sinput
, mode
);
306 err
= open_substream(rmidi
, soutput
, mode
);
309 close_substream(rmidi
, sinput
, 0);
314 rfile
->rmidi
= rmidi
;
315 rfile
->input
= sinput
;
316 rfile
->output
= soutput
;
320 /* called from sound/core/seq/seq_midi.c */
321 int snd_rawmidi_kernel_open(struct snd_card
*card
, int device
, int subdevice
,
322 int mode
, struct snd_rawmidi_file
* rfile
)
324 struct snd_rawmidi
*rmidi
;
327 if (snd_BUG_ON(!rfile
))
330 mutex_lock(®ister_mutex
);
331 rmidi
= snd_rawmidi_search(card
, device
);
333 mutex_unlock(®ister_mutex
);
336 if (!try_module_get(rmidi
->card
->module
)) {
337 mutex_unlock(®ister_mutex
);
340 mutex_unlock(®ister_mutex
);
342 mutex_lock(&rmidi
->open_mutex
);
343 err
= rawmidi_open_priv(rmidi
, subdevice
, mode
, rfile
);
344 mutex_unlock(&rmidi
->open_mutex
);
346 module_put(rmidi
->card
->module
);
350 static int snd_rawmidi_open(struct inode
*inode
, struct file
*file
)
352 int maj
= imajor(inode
);
353 struct snd_card
*card
;
355 unsigned short fflags
;
357 struct snd_rawmidi
*rmidi
;
358 struct snd_rawmidi_file
*rawmidi_file
= NULL
;
360 struct snd_ctl_file
*kctl
;
362 if ((file
->f_flags
& O_APPEND
) && !(file
->f_flags
& O_NONBLOCK
))
363 return -EINVAL
; /* invalid combination */
365 err
= nonseekable_open(inode
, file
);
369 if (maj
== snd_major
) {
370 rmidi
= snd_lookup_minor_data(iminor(inode
),
371 SNDRV_DEVICE_TYPE_RAWMIDI
);
372 #ifdef CONFIG_SND_OSSEMUL
373 } else if (maj
== SOUND_MAJOR
) {
374 rmidi
= snd_lookup_oss_minor_data(iminor(inode
),
375 SNDRV_OSS_DEVICE_TYPE_MIDI
);
383 if (!try_module_get(rmidi
->card
->module
))
386 mutex_lock(&rmidi
->open_mutex
);
388 err
= snd_card_file_add(card
, file
);
391 fflags
= snd_rawmidi_file_flags(file
);
392 if ((file
->f_flags
& O_APPEND
) || maj
== SOUND_MAJOR
) /* OSS emul? */
393 fflags
|= SNDRV_RAWMIDI_LFLG_APPEND
;
394 rawmidi_file
= kmalloc(sizeof(*rawmidi_file
), GFP_KERNEL
);
395 if (rawmidi_file
== NULL
) {
399 init_waitqueue_entry(&wait
, current
);
400 add_wait_queue(&rmidi
->open_wait
, &wait
);
403 read_lock(&card
->ctl_files_rwlock
);
404 list_for_each_entry(kctl
, &card
->ctl_files
, list
) {
405 if (kctl
->pid
== task_pid(current
)) {
406 subdevice
= kctl
->prefer_rawmidi_subdevice
;
411 read_unlock(&card
->ctl_files_rwlock
);
412 err
= rawmidi_open_priv(rmidi
, subdevice
, fflags
, rawmidi_file
);
415 if (err
== -EAGAIN
) {
416 if (file
->f_flags
& O_NONBLOCK
) {
422 set_current_state(TASK_INTERRUPTIBLE
);
423 mutex_unlock(&rmidi
->open_mutex
);
425 mutex_lock(&rmidi
->open_mutex
);
426 if (signal_pending(current
)) {
431 remove_wait_queue(&rmidi
->open_wait
, &wait
);
436 #ifdef CONFIG_SND_OSSEMUL
437 if (rawmidi_file
->input
&& rawmidi_file
->input
->runtime
)
438 rawmidi_file
->input
->runtime
->oss
= (maj
== SOUND_MAJOR
);
439 if (rawmidi_file
->output
&& rawmidi_file
->output
->runtime
)
440 rawmidi_file
->output
->runtime
->oss
= (maj
== SOUND_MAJOR
);
442 file
->private_data
= rawmidi_file
;
443 mutex_unlock(&rmidi
->open_mutex
);
447 snd_card_file_remove(card
, file
);
449 mutex_unlock(&rmidi
->open_mutex
);
450 module_put(rmidi
->card
->module
);
454 static void close_substream(struct snd_rawmidi
*rmidi
,
455 struct snd_rawmidi_substream
*substream
,
458 if (--substream
->use_count
)
462 if (substream
->stream
== SNDRV_RAWMIDI_STREAM_INPUT
)
463 snd_rawmidi_input_trigger(substream
, 0);
465 if (substream
->active_sensing
) {
466 unsigned char buf
= 0xfe;
467 /* sending single active sensing message
468 * to shut the device up
470 snd_rawmidi_kernel_write(substream
, &buf
, 1);
472 if (snd_rawmidi_drain_output(substream
) == -ERESTARTSYS
)
473 snd_rawmidi_output_trigger(substream
, 0);
476 substream
->ops
->close(substream
);
477 if (substream
->runtime
->private_free
)
478 substream
->runtime
->private_free(substream
);
479 snd_rawmidi_runtime_free(substream
);
480 substream
->opened
= 0;
481 substream
->append
= 0;
482 put_pid(substream
->pid
);
483 substream
->pid
= NULL
;
484 rmidi
->streams
[substream
->stream
].substream_opened
--;
487 static void rawmidi_release_priv(struct snd_rawmidi_file
*rfile
)
489 struct snd_rawmidi
*rmidi
;
491 rmidi
= rfile
->rmidi
;
492 mutex_lock(&rmidi
->open_mutex
);
494 close_substream(rmidi
, rfile
->input
, 1);
498 close_substream(rmidi
, rfile
->output
, 1);
499 rfile
->output
= NULL
;
502 mutex_unlock(&rmidi
->open_mutex
);
503 wake_up(&rmidi
->open_wait
);
506 /* called from sound/core/seq/seq_midi.c */
507 int snd_rawmidi_kernel_release(struct snd_rawmidi_file
*rfile
)
509 struct snd_rawmidi
*rmidi
;
511 if (snd_BUG_ON(!rfile
))
514 rmidi
= rfile
->rmidi
;
515 rawmidi_release_priv(rfile
);
516 module_put(rmidi
->card
->module
);
520 static int snd_rawmidi_release(struct inode
*inode
, struct file
*file
)
522 struct snd_rawmidi_file
*rfile
;
523 struct snd_rawmidi
*rmidi
;
524 struct module
*module
;
526 rfile
= file
->private_data
;
527 rmidi
= rfile
->rmidi
;
528 rawmidi_release_priv(rfile
);
530 module
= rmidi
->card
->module
;
531 snd_card_file_remove(rmidi
->card
, file
);
536 static int snd_rawmidi_info(struct snd_rawmidi_substream
*substream
,
537 struct snd_rawmidi_info
*info
)
539 struct snd_rawmidi
*rmidi
;
541 if (substream
== NULL
)
543 rmidi
= substream
->rmidi
;
544 memset(info
, 0, sizeof(*info
));
545 info
->card
= rmidi
->card
->number
;
546 info
->device
= rmidi
->device
;
547 info
->subdevice
= substream
->number
;
548 info
->stream
= substream
->stream
;
549 info
->flags
= rmidi
->info_flags
;
550 strcpy(info
->id
, rmidi
->id
);
551 strcpy(info
->name
, rmidi
->name
);
552 strcpy(info
->subname
, substream
->name
);
553 info
->subdevices_count
= substream
->pstr
->substream_count
;
554 info
->subdevices_avail
= (substream
->pstr
->substream_count
-
555 substream
->pstr
->substream_opened
);
559 static int snd_rawmidi_info_user(struct snd_rawmidi_substream
*substream
,
560 struct snd_rawmidi_info __user
* _info
)
562 struct snd_rawmidi_info info
;
564 if ((err
= snd_rawmidi_info(substream
, &info
)) < 0)
566 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
571 int snd_rawmidi_info_select(struct snd_card
*card
, struct snd_rawmidi_info
*info
)
573 struct snd_rawmidi
*rmidi
;
574 struct snd_rawmidi_str
*pstr
;
575 struct snd_rawmidi_substream
*substream
;
577 mutex_lock(®ister_mutex
);
578 rmidi
= snd_rawmidi_search(card
, info
->device
);
579 mutex_unlock(®ister_mutex
);
582 if (info
->stream
< 0 || info
->stream
> 1)
584 pstr
= &rmidi
->streams
[info
->stream
];
585 if (pstr
->substream_count
== 0)
587 if (info
->subdevice
>= pstr
->substream_count
)
589 list_for_each_entry(substream
, &pstr
->substreams
, list
) {
590 if ((unsigned int)substream
->number
== info
->subdevice
)
591 return snd_rawmidi_info(substream
, info
);
596 static int snd_rawmidi_info_select_user(struct snd_card
*card
,
597 struct snd_rawmidi_info __user
*_info
)
600 struct snd_rawmidi_info info
;
601 if (get_user(info
.device
, &_info
->device
))
603 if (get_user(info
.stream
, &_info
->stream
))
605 if (get_user(info
.subdevice
, &_info
->subdevice
))
607 if ((err
= snd_rawmidi_info_select(card
, &info
)) < 0)
609 if (copy_to_user(_info
, &info
, sizeof(struct snd_rawmidi_info
)))
614 int snd_rawmidi_output_params(struct snd_rawmidi_substream
*substream
,
615 struct snd_rawmidi_params
* params
)
618 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
620 if (substream
->append
&& substream
->use_count
> 1)
622 snd_rawmidi_drain_output(substream
);
623 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
626 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
629 if (params
->buffer_size
!= runtime
->buffer_size
) {
630 newbuf
= krealloc(runtime
->buffer
, params
->buffer_size
,
634 runtime
->buffer
= newbuf
;
635 runtime
->buffer_size
= params
->buffer_size
;
636 runtime
->avail
= runtime
->buffer_size
;
638 runtime
->avail_min
= params
->avail_min
;
639 substream
->active_sensing
= !params
->no_active_sensing
;
643 int snd_rawmidi_input_params(struct snd_rawmidi_substream
*substream
,
644 struct snd_rawmidi_params
* params
)
647 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
649 snd_rawmidi_drain_input(substream
);
650 if (params
->buffer_size
< 32 || params
->buffer_size
> 1024L * 1024L) {
653 if (params
->avail_min
< 1 || params
->avail_min
> params
->buffer_size
) {
656 if (params
->buffer_size
!= runtime
->buffer_size
) {
657 newbuf
= krealloc(runtime
->buffer
, params
->buffer_size
,
661 runtime
->buffer
= newbuf
;
662 runtime
->buffer_size
= params
->buffer_size
;
664 runtime
->avail_min
= params
->avail_min
;
668 static int snd_rawmidi_output_status(struct snd_rawmidi_substream
*substream
,
669 struct snd_rawmidi_status
* status
)
671 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
673 memset(status
, 0, sizeof(*status
));
674 status
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
675 spin_lock_irq(&runtime
->lock
);
676 status
->avail
= runtime
->avail
;
677 spin_unlock_irq(&runtime
->lock
);
681 static int snd_rawmidi_input_status(struct snd_rawmidi_substream
*substream
,
682 struct snd_rawmidi_status
* status
)
684 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
686 memset(status
, 0, sizeof(*status
));
687 status
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
688 spin_lock_irq(&runtime
->lock
);
689 status
->avail
= runtime
->avail
;
690 status
->xruns
= runtime
->xruns
;
692 spin_unlock_irq(&runtime
->lock
);
696 static long snd_rawmidi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
698 struct snd_rawmidi_file
*rfile
;
699 void __user
*argp
= (void __user
*)arg
;
701 rfile
= file
->private_data
;
702 if (((cmd
>> 8) & 0xff) != 'W')
705 case SNDRV_RAWMIDI_IOCTL_PVERSION
:
706 return put_user(SNDRV_RAWMIDI_VERSION
, (int __user
*)argp
) ? -EFAULT
: 0;
707 case SNDRV_RAWMIDI_IOCTL_INFO
:
710 struct snd_rawmidi_info __user
*info
= argp
;
711 if (get_user(stream
, &info
->stream
))
714 case SNDRV_RAWMIDI_STREAM_INPUT
:
715 return snd_rawmidi_info_user(rfile
->input
, info
);
716 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
717 return snd_rawmidi_info_user(rfile
->output
, info
);
722 case SNDRV_RAWMIDI_IOCTL_PARAMS
:
724 struct snd_rawmidi_params params
;
725 if (copy_from_user(¶ms
, argp
, sizeof(struct snd_rawmidi_params
)))
727 switch (params
.stream
) {
728 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
729 if (rfile
->output
== NULL
)
731 return snd_rawmidi_output_params(rfile
->output
, ¶ms
);
732 case SNDRV_RAWMIDI_STREAM_INPUT
:
733 if (rfile
->input
== NULL
)
735 return snd_rawmidi_input_params(rfile
->input
, ¶ms
);
740 case SNDRV_RAWMIDI_IOCTL_STATUS
:
743 struct snd_rawmidi_status status
;
744 if (copy_from_user(&status
, argp
, sizeof(struct snd_rawmidi_status
)))
746 switch (status
.stream
) {
747 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
748 if (rfile
->output
== NULL
)
750 err
= snd_rawmidi_output_status(rfile
->output
, &status
);
752 case SNDRV_RAWMIDI_STREAM_INPUT
:
753 if (rfile
->input
== NULL
)
755 err
= snd_rawmidi_input_status(rfile
->input
, &status
);
762 if (copy_to_user(argp
, &status
, sizeof(struct snd_rawmidi_status
)))
766 case SNDRV_RAWMIDI_IOCTL_DROP
:
769 if (get_user(val
, (int __user
*) argp
))
772 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
773 if (rfile
->output
== NULL
)
775 return snd_rawmidi_drop_output(rfile
->output
);
780 case SNDRV_RAWMIDI_IOCTL_DRAIN
:
783 if (get_user(val
, (int __user
*) argp
))
786 case SNDRV_RAWMIDI_STREAM_OUTPUT
:
787 if (rfile
->output
== NULL
)
789 return snd_rawmidi_drain_output(rfile
->output
);
790 case SNDRV_RAWMIDI_STREAM_INPUT
:
791 if (rfile
->input
== NULL
)
793 return snd_rawmidi_drain_input(rfile
->input
);
798 #ifdef CONFIG_SND_DEBUG
800 snd_printk(KERN_WARNING
"rawmidi: unknown command = 0x%x\n", cmd
);
806 static int snd_rawmidi_control_ioctl(struct snd_card
*card
,
807 struct snd_ctl_file
*control
,
811 void __user
*argp
= (void __user
*)arg
;
814 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE
:
818 if (get_user(device
, (int __user
*)argp
))
820 if (device
>= SNDRV_RAWMIDI_DEVICES
) /* next device is -1 */
821 device
= SNDRV_RAWMIDI_DEVICES
- 1;
822 mutex_lock(®ister_mutex
);
823 device
= device
< 0 ? 0 : device
+ 1;
824 while (device
< SNDRV_RAWMIDI_DEVICES
) {
825 if (snd_rawmidi_search(card
, device
))
829 if (device
== SNDRV_RAWMIDI_DEVICES
)
831 mutex_unlock(®ister_mutex
);
832 if (put_user(device
, (int __user
*)argp
))
836 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE
:
840 if (get_user(val
, (int __user
*)argp
))
842 control
->prefer_rawmidi_subdevice
= val
;
845 case SNDRV_CTL_IOCTL_RAWMIDI_INFO
:
846 return snd_rawmidi_info_select_user(card
, argp
);
852 * snd_rawmidi_receive - receive the input data from the device
853 * @substream: the rawmidi substream
854 * @buffer: the buffer pointer
855 * @count: the data size to read
857 * Reads the data from the internal buffer.
859 * Returns the size of read data, or a negative error code on failure.
861 int snd_rawmidi_receive(struct snd_rawmidi_substream
*substream
,
862 const unsigned char *buffer
, int count
)
865 int result
= 0, count1
;
866 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
868 if (!substream
->opened
)
870 if (runtime
->buffer
== NULL
) {
871 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
874 spin_lock_irqsave(&runtime
->lock
, flags
);
875 if (count
== 1) { /* special case, faster code */
877 if (runtime
->avail
< runtime
->buffer_size
) {
878 runtime
->buffer
[runtime
->hw_ptr
++] = buffer
[0];
879 runtime
->hw_ptr
%= runtime
->buffer_size
;
886 substream
->bytes
+= count
;
887 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
890 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
891 count1
= runtime
->buffer_size
- runtime
->avail
;
892 memcpy(runtime
->buffer
+ runtime
->hw_ptr
, buffer
, count1
);
893 runtime
->hw_ptr
+= count1
;
894 runtime
->hw_ptr
%= runtime
->buffer_size
;
895 runtime
->avail
+= count1
;
901 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
)) {
902 count1
= runtime
->buffer_size
- runtime
->avail
;
903 runtime
->xruns
+= count
- count1
;
906 memcpy(runtime
->buffer
, buffer
, count1
);
907 runtime
->hw_ptr
= count1
;
908 runtime
->avail
+= count1
;
915 schedule_work(&runtime
->event_work
);
916 else if (snd_rawmidi_ready(substream
))
917 wake_up(&runtime
->sleep
);
919 spin_unlock_irqrestore(&runtime
->lock
, flags
);
923 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream
*substream
,
924 unsigned char __user
*userbuf
,
925 unsigned char *kernelbuf
, long count
)
928 long result
= 0, count1
;
929 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
931 while (count
> 0 && runtime
->avail
) {
932 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
935 spin_lock_irqsave(&runtime
->lock
, flags
);
936 if (count1
> (int)runtime
->avail
)
937 count1
= runtime
->avail
;
939 memcpy(kernelbuf
+ result
, runtime
->buffer
+ runtime
->appl_ptr
, count1
);
941 spin_unlock_irqrestore(&runtime
->lock
, flags
);
942 if (copy_to_user(userbuf
+ result
,
943 runtime
->buffer
+ runtime
->appl_ptr
, count1
)) {
944 return result
> 0 ? result
: -EFAULT
;
946 spin_lock_irqsave(&runtime
->lock
, flags
);
948 runtime
->appl_ptr
+= count1
;
949 runtime
->appl_ptr
%= runtime
->buffer_size
;
950 runtime
->avail
-= count1
;
951 spin_unlock_irqrestore(&runtime
->lock
, flags
);
958 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream
*substream
,
959 unsigned char *buf
, long count
)
961 snd_rawmidi_input_trigger(substream
, 1);
962 return snd_rawmidi_kernel_read1(substream
, NULL
/*userbuf*/, buf
, count
);
965 static ssize_t
snd_rawmidi_read(struct file
*file
, char __user
*buf
, size_t count
,
970 struct snd_rawmidi_file
*rfile
;
971 struct snd_rawmidi_substream
*substream
;
972 struct snd_rawmidi_runtime
*runtime
;
974 rfile
= file
->private_data
;
975 substream
= rfile
->input
;
976 if (substream
== NULL
)
978 runtime
= substream
->runtime
;
979 snd_rawmidi_input_trigger(substream
, 1);
982 spin_lock_irq(&runtime
->lock
);
983 while (!snd_rawmidi_ready(substream
)) {
985 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
986 spin_unlock_irq(&runtime
->lock
);
987 return result
> 0 ? result
: -EAGAIN
;
989 init_waitqueue_entry(&wait
, current
);
990 add_wait_queue(&runtime
->sleep
, &wait
);
991 set_current_state(TASK_INTERRUPTIBLE
);
992 spin_unlock_irq(&runtime
->lock
);
994 remove_wait_queue(&runtime
->sleep
, &wait
);
995 if (signal_pending(current
))
996 return result
> 0 ? result
: -ERESTARTSYS
;
998 return result
> 0 ? result
: -EIO
;
999 spin_lock_irq(&runtime
->lock
);
1001 spin_unlock_irq(&runtime
->lock
);
1002 count1
= snd_rawmidi_kernel_read1(substream
,
1003 (unsigned char __user
*)buf
,
1007 return result
> 0 ? result
: count1
;
1016 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1017 * @substream: the rawmidi substream
1019 * Returns 1 if the internal output buffer is empty, 0 if not.
1021 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream
*substream
)
1023 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1025 unsigned long flags
;
1027 if (runtime
->buffer
== NULL
) {
1028 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1031 spin_lock_irqsave(&runtime
->lock
, flags
);
1032 result
= runtime
->avail
>= runtime
->buffer_size
;
1033 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1038 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1039 * @substream: the rawmidi substream
1040 * @buffer: the buffer pointer
1041 * @count: data size to transfer
1043 * Copies data from the internal output buffer to the given buffer.
1045 * Call this in the interrupt handler when the midi output is ready,
1046 * and call snd_rawmidi_transmit_ack() after the transmission is
1049 * Returns the size of copied data, or a negative error code on failure.
1051 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream
*substream
,
1052 unsigned char *buffer
, int count
)
1054 unsigned long flags
;
1056 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1058 if (runtime
->buffer
== NULL
) {
1059 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1063 spin_lock_irqsave(&runtime
->lock
, flags
);
1064 if (runtime
->avail
>= runtime
->buffer_size
) {
1065 /* warning: lowlevel layer MUST trigger down the hardware */
1068 if (count
== 1) { /* special case, faster code */
1069 *buffer
= runtime
->buffer
[runtime
->hw_ptr
];
1072 count1
= runtime
->buffer_size
- runtime
->hw_ptr
;
1075 if (count1
> (int)(runtime
->buffer_size
- runtime
->avail
))
1076 count1
= runtime
->buffer_size
- runtime
->avail
;
1077 memcpy(buffer
, runtime
->buffer
+ runtime
->hw_ptr
, count1
);
1081 if (count
> (int)(runtime
->buffer_size
- runtime
->avail
- count1
))
1082 count
= runtime
->buffer_size
- runtime
->avail
- count1
;
1083 memcpy(buffer
+ count1
, runtime
->buffer
, count
);
1088 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1093 * snd_rawmidi_transmit_ack - acknowledge the transmission
1094 * @substream: the rawmidi substream
1095 * @count: the tranferred count
1097 * Advances the hardware pointer for the internal output buffer with
1098 * the given size and updates the condition.
1099 * Call after the transmission is finished.
1101 * Returns the advanced size if successful, or a negative error code on failure.
1103 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream
*substream
, int count
)
1105 unsigned long flags
;
1106 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1108 if (runtime
->buffer
== NULL
) {
1109 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1112 spin_lock_irqsave(&runtime
->lock
, flags
);
1113 snd_BUG_ON(runtime
->avail
+ count
> runtime
->buffer_size
);
1114 runtime
->hw_ptr
+= count
;
1115 runtime
->hw_ptr
%= runtime
->buffer_size
;
1116 runtime
->avail
+= count
;
1117 substream
->bytes
+= count
;
1119 if (runtime
->drain
|| snd_rawmidi_ready(substream
))
1120 wake_up(&runtime
->sleep
);
1122 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1127 * snd_rawmidi_transmit - copy from the buffer to the device
1128 * @substream: the rawmidi substream
1129 * @buffer: the buffer pointer
1130 * @count: the data size to transfer
1132 * Copies data from the buffer to the device and advances the pointer.
1134 * Returns the copied size if successful, or a negative error code on failure.
1136 int snd_rawmidi_transmit(struct snd_rawmidi_substream
*substream
,
1137 unsigned char *buffer
, int count
)
1139 if (!substream
->opened
)
1141 count
= snd_rawmidi_transmit_peek(substream
, buffer
, count
);
1144 return snd_rawmidi_transmit_ack(substream
, count
);
1147 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream
*substream
,
1148 const unsigned char __user
*userbuf
,
1149 const unsigned char *kernelbuf
,
1152 unsigned long flags
;
1153 long count1
, result
;
1154 struct snd_rawmidi_runtime
*runtime
= substream
->runtime
;
1156 if (snd_BUG_ON(!kernelbuf
&& !userbuf
))
1158 if (snd_BUG_ON(!runtime
->buffer
))
1162 spin_lock_irqsave(&runtime
->lock
, flags
);
1163 if (substream
->append
) {
1164 if ((long)runtime
->avail
< count
) {
1165 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1169 while (count
> 0 && runtime
->avail
> 0) {
1170 count1
= runtime
->buffer_size
- runtime
->appl_ptr
;
1173 if (count1
> (long)runtime
->avail
)
1174 count1
= runtime
->avail
;
1176 memcpy(runtime
->buffer
+ runtime
->appl_ptr
,
1177 kernelbuf
+ result
, count1
);
1179 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1180 if (copy_from_user(runtime
->buffer
+ runtime
->appl_ptr
,
1181 userbuf
+ result
, count1
)) {
1182 spin_lock_irqsave(&runtime
->lock
, flags
);
1183 result
= result
> 0 ? result
: -EFAULT
;
1186 spin_lock_irqsave(&runtime
->lock
, flags
);
1188 runtime
->appl_ptr
+= count1
;
1189 runtime
->appl_ptr
%= runtime
->buffer_size
;
1190 runtime
->avail
-= count1
;
1195 count1
= runtime
->avail
< runtime
->buffer_size
;
1196 spin_unlock_irqrestore(&runtime
->lock
, flags
);
1198 snd_rawmidi_output_trigger(substream
, 1);
1202 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream
*substream
,
1203 const unsigned char *buf
, long count
)
1205 return snd_rawmidi_kernel_write1(substream
, NULL
, buf
, count
);
1208 static ssize_t
snd_rawmidi_write(struct file
*file
, const char __user
*buf
,
1209 size_t count
, loff_t
*offset
)
1211 long result
, timeout
;
1213 struct snd_rawmidi_file
*rfile
;
1214 struct snd_rawmidi_runtime
*runtime
;
1215 struct snd_rawmidi_substream
*substream
;
1217 rfile
= file
->private_data
;
1218 substream
= rfile
->output
;
1219 runtime
= substream
->runtime
;
1220 /* we cannot put an atomic message to our buffer */
1221 if (substream
->append
&& count
> runtime
->buffer_size
)
1225 spin_lock_irq(&runtime
->lock
);
1226 while (!snd_rawmidi_ready_append(substream
, count
)) {
1228 if (file
->f_flags
& O_NONBLOCK
) {
1229 spin_unlock_irq(&runtime
->lock
);
1230 return result
> 0 ? result
: -EAGAIN
;
1232 init_waitqueue_entry(&wait
, current
);
1233 add_wait_queue(&runtime
->sleep
, &wait
);
1234 set_current_state(TASK_INTERRUPTIBLE
);
1235 spin_unlock_irq(&runtime
->lock
);
1236 timeout
= schedule_timeout(30 * HZ
);
1237 remove_wait_queue(&runtime
->sleep
, &wait
);
1238 if (signal_pending(current
))
1239 return result
> 0 ? result
: -ERESTARTSYS
;
1240 if (!runtime
->avail
&& !timeout
)
1241 return result
> 0 ? result
: -EIO
;
1242 spin_lock_irq(&runtime
->lock
);
1244 spin_unlock_irq(&runtime
->lock
);
1245 count1
= snd_rawmidi_kernel_write1(substream
, buf
, NULL
, count
);
1247 return result
> 0 ? result
: count1
;
1250 if ((size_t)count1
< count
&& (file
->f_flags
& O_NONBLOCK
))
1254 if (file
->f_flags
& O_DSYNC
) {
1255 spin_lock_irq(&runtime
->lock
);
1256 while (runtime
->avail
!= runtime
->buffer_size
) {
1258 unsigned int last_avail
= runtime
->avail
;
1259 init_waitqueue_entry(&wait
, current
);
1260 add_wait_queue(&runtime
->sleep
, &wait
);
1261 set_current_state(TASK_INTERRUPTIBLE
);
1262 spin_unlock_irq(&runtime
->lock
);
1263 timeout
= schedule_timeout(30 * HZ
);
1264 remove_wait_queue(&runtime
->sleep
, &wait
);
1265 if (signal_pending(current
))
1266 return result
> 0 ? result
: -ERESTARTSYS
;
1267 if (runtime
->avail
== last_avail
&& !timeout
)
1268 return result
> 0 ? result
: -EIO
;
1269 spin_lock_irq(&runtime
->lock
);
1271 spin_unlock_irq(&runtime
->lock
);
1276 static unsigned int snd_rawmidi_poll(struct file
*file
, poll_table
* wait
)
1278 struct snd_rawmidi_file
*rfile
;
1279 struct snd_rawmidi_runtime
*runtime
;
1282 rfile
= file
->private_data
;
1283 if (rfile
->input
!= NULL
) {
1284 runtime
= rfile
->input
->runtime
;
1285 snd_rawmidi_input_trigger(rfile
->input
, 1);
1286 poll_wait(file
, &runtime
->sleep
, wait
);
1288 if (rfile
->output
!= NULL
) {
1289 runtime
= rfile
->output
->runtime
;
1290 poll_wait(file
, &runtime
->sleep
, wait
);
1293 if (rfile
->input
!= NULL
) {
1294 if (snd_rawmidi_ready(rfile
->input
))
1295 mask
|= POLLIN
| POLLRDNORM
;
1297 if (rfile
->output
!= NULL
) {
1298 if (snd_rawmidi_ready(rfile
->output
))
1299 mask
|= POLLOUT
| POLLWRNORM
;
1306 #ifdef CONFIG_COMPAT
1307 #include "rawmidi_compat.c"
1309 #define snd_rawmidi_ioctl_compat NULL
1316 static void snd_rawmidi_proc_info_read(struct snd_info_entry
*entry
,
1317 struct snd_info_buffer
*buffer
)
1319 struct snd_rawmidi
*rmidi
;
1320 struct snd_rawmidi_substream
*substream
;
1321 struct snd_rawmidi_runtime
*runtime
;
1323 rmidi
= entry
->private_data
;
1324 snd_iprintf(buffer
, "%s\n\n", rmidi
->name
);
1325 mutex_lock(&rmidi
->open_mutex
);
1326 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) {
1327 list_for_each_entry(substream
,
1328 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
].substreams
,
1332 " Tx bytes : %lu\n",
1334 (unsigned long) substream
->bytes
);
1335 if (substream
->opened
) {
1337 " Owner PID : %d\n",
1338 pid_vnr(substream
->pid
));
1339 runtime
= substream
->runtime
;
1342 " Buffer size : %lu\n"
1344 runtime
->oss
? "OSS compatible" : "native",
1345 (unsigned long) runtime
->buffer_size
,
1346 (unsigned long) runtime
->avail
);
1350 if (rmidi
->info_flags
& SNDRV_RAWMIDI_INFO_INPUT
) {
1351 list_for_each_entry(substream
,
1352 &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
].substreams
,
1356 " Rx bytes : %lu\n",
1358 (unsigned long) substream
->bytes
);
1359 if (substream
->opened
) {
1361 " Owner PID : %d\n",
1362 pid_vnr(substream
->pid
));
1363 runtime
= substream
->runtime
;
1365 " Buffer size : %lu\n"
1367 " Overruns : %lu\n",
1368 (unsigned long) runtime
->buffer_size
,
1369 (unsigned long) runtime
->avail
,
1370 (unsigned long) runtime
->xruns
);
1374 mutex_unlock(&rmidi
->open_mutex
);
1378 * Register functions
1381 static const struct file_operations snd_rawmidi_f_ops
=
1383 .owner
= THIS_MODULE
,
1384 .read
= snd_rawmidi_read
,
1385 .write
= snd_rawmidi_write
,
1386 .open
= snd_rawmidi_open
,
1387 .release
= snd_rawmidi_release
,
1388 .llseek
= no_llseek
,
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
);