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/driver.h>
23 #include <sound/core.h>
24 #include <linux/major.h>
25 #include <linux/init.h>
26 #include <linux/sched.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_tasklet(unsigned long data)
98 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
99 substream->runtime->event(substream);
102 static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
104 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
105 substream->ops->trigger(substream, 1);
108 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
110 struct snd_rawmidi_runtime *runtime;
112 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
114 spin_lock_init(&runtime->lock);
115 init_waitqueue_head(&runtime->sleep);
116 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
117 tasklet_init(&runtime->tasklet,
118 snd_rawmidi_input_event_tasklet,
119 (unsigned long)substream);
121 tasklet_init(&runtime->tasklet,
122 snd_rawmidi_output_trigger_tasklet,
123 (unsigned long)substream);
124 runtime->event = NULL;
125 runtime->buffer_size = PAGE_SIZE;
126 runtime->avail_min = 1;
127 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
130 runtime->avail = runtime->buffer_size;
131 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
135 runtime->appl_ptr = runtime->hw_ptr = 0;
136 substream->runtime = runtime;
140 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
142 struct snd_rawmidi_runtime *runtime = substream->runtime;
144 kfree(runtime->buffer);
146 substream->runtime = NULL;
150 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
153 tasklet_hi_schedule(&substream->runtime->tasklet);
155 tasklet_kill(&substream->runtime->tasklet);
156 substream->ops->trigger(substream, 0);
160 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
162 substream->ops->trigger(substream, up);
163 if (!up && substream->runtime->event)
164 tasklet_kill(&substream->runtime->tasklet);
167 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
170 struct snd_rawmidi_runtime *runtime = substream->runtime;
172 snd_rawmidi_output_trigger(substream, 0);
174 spin_lock_irqsave(&runtime->lock, flags);
175 runtime->appl_ptr = runtime->hw_ptr = 0;
176 runtime->avail = runtime->buffer_size;
177 spin_unlock_irqrestore(&runtime->lock, flags);
181 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
185 struct snd_rawmidi_runtime *runtime = substream->runtime;
189 timeout = wait_event_interruptible_timeout(runtime->sleep,
190 (runtime->avail >= runtime->buffer_size),
192 if (signal_pending(current))
194 if (runtime->avail < runtime->buffer_size && !timeout) {
195 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
199 if (err != -ERESTARTSYS) {
200 /* we need wait a while to make sure that Tx FIFOs are empty */
201 if (substream->ops->drain)
202 substream->ops->drain(substream);
205 snd_rawmidi_drop_output(substream);
210 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
213 struct snd_rawmidi_runtime *runtime = substream->runtime;
215 snd_rawmidi_input_trigger(substream, 0);
217 spin_lock_irqsave(&runtime->lock, flags);
218 runtime->appl_ptr = runtime->hw_ptr = 0;
220 spin_unlock_irqrestore(&runtime->lock, flags);
224 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
225 int mode, struct snd_rawmidi_file * rfile)
227 struct snd_rawmidi *rmidi;
228 struct list_head *list1, *list2;
229 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
230 struct snd_rawmidi_runtime *input = NULL, *output = NULL;
234 rfile->input = rfile->output = NULL;
235 mutex_lock(®ister_mutex);
236 rmidi = snd_rawmidi_search(card, device);
237 mutex_unlock(®ister_mutex);
242 if (!try_module_get(rmidi->card->module)) {
246 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
247 mutex_lock(&rmidi->open_mutex);
248 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
249 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
253 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
257 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
258 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
263 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
264 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
268 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
272 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
273 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
278 list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
280 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
282 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
288 sinput = list_entry(list1, struct snd_rawmidi_substream, list);
289 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
291 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
296 list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
298 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
300 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
306 soutput = list_entry(list2, struct snd_rawmidi_substream, list);
307 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
308 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
309 if (soutput->opened && !soutput->append)
316 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
321 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
322 if ((err = snd_rawmidi_runtime_create(sinput)) < 0)
324 input = sinput->runtime;
325 if ((err = sinput->ops->open(sinput)) < 0)
328 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
332 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
335 if ((err = snd_rawmidi_runtime_create(soutput)) < 0) {
336 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
337 sinput->ops->close(sinput);
340 output = soutput->runtime;
341 if ((err = soutput->ops->open(soutput)) < 0) {
342 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
343 sinput->ops->close(sinput);
348 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
350 if (soutput->use_count++ == 0)
351 soutput->active_sensing = 1;
352 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
356 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
357 mutex_unlock(&rmidi->open_mutex);
359 rfile->rmidi = rmidi;
360 rfile->input = sinput;
361 rfile->output = soutput;
367 snd_rawmidi_runtime_free(sinput);
369 snd_rawmidi_runtime_free(soutput);
370 module_put(rmidi->card->module);
371 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
372 mutex_unlock(&rmidi->open_mutex);
377 static int snd_rawmidi_open(struct inode *inode, struct file *file)
379 int maj = imajor(inode);
380 struct snd_card *card;
382 unsigned short fflags;
384 struct snd_rawmidi *rmidi;
385 struct snd_rawmidi_file *rawmidi_file;
387 struct snd_ctl_file *kctl;
389 if (maj == snd_major) {
390 rmidi = snd_lookup_minor_data(iminor(inode),
391 SNDRV_DEVICE_TYPE_RAWMIDI);
392 #ifdef CONFIG_SND_OSSEMUL
393 } else if (maj == SOUND_MAJOR) {
394 rmidi = snd_lookup_oss_minor_data(iminor(inode),
395 SNDRV_OSS_DEVICE_TYPE_MIDI);
402 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
403 return -EINVAL; /* invalid combination */
405 err = snd_card_file_add(card, file);
408 fflags = snd_rawmidi_file_flags(file);
409 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
410 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
411 fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
412 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
413 if (rawmidi_file == NULL) {
414 snd_card_file_remove(card, file);
417 init_waitqueue_entry(&wait, current);
418 add_wait_queue(&rmidi->open_wait, &wait);
419 mutex_lock(&rmidi->open_mutex);
422 down_read(&card->controls_rwsem);
423 list_for_each_entry(kctl, &card->ctl_files, list) {
424 if (kctl->pid == current->pid) {
425 subdevice = kctl->prefer_rawmidi_subdevice;
430 up_read(&card->controls_rwsem);
431 err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
432 subdevice, fflags, rawmidi_file);
435 if (err == -EAGAIN) {
436 if (file->f_flags & O_NONBLOCK) {
442 set_current_state(TASK_INTERRUPTIBLE);
443 mutex_unlock(&rmidi->open_mutex);
445 mutex_lock(&rmidi->open_mutex);
446 if (signal_pending(current)) {
451 #ifdef CONFIG_SND_OSSEMUL
452 if (rawmidi_file->input && rawmidi_file->input->runtime)
453 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
454 if (rawmidi_file->output && rawmidi_file->output->runtime)
455 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
457 remove_wait_queue(&rmidi->open_wait, &wait);
459 file->private_data = rawmidi_file;
461 snd_card_file_remove(card, file);
464 mutex_unlock(&rmidi->open_mutex);
468 int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
470 struct snd_rawmidi *rmidi;
471 struct snd_rawmidi_substream *substream;
472 struct snd_rawmidi_runtime *runtime;
474 snd_assert(rfile != NULL, return -ENXIO);
475 snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
476 rmidi = rfile->rmidi;
477 mutex_lock(&rmidi->open_mutex);
478 if (rfile->input != NULL) {
479 substream = rfile->input;
481 runtime = substream->runtime;
482 snd_rawmidi_input_trigger(substream, 0);
483 substream->ops->close(substream);
484 if (runtime->private_free != NULL)
485 runtime->private_free(substream);
486 snd_rawmidi_runtime_free(substream);
487 substream->opened = 0;
488 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
490 if (rfile->output != NULL) {
491 substream = rfile->output;
492 rfile->output = NULL;
493 if (--substream->use_count == 0) {
494 runtime = substream->runtime;
495 if (substream->active_sensing) {
496 unsigned char buf = 0xfe;
497 /* sending single active sensing message to shut the device up */
498 snd_rawmidi_kernel_write(substream, &buf, 1);
500 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
501 snd_rawmidi_output_trigger(substream, 0);
502 substream->ops->close(substream);
503 if (runtime->private_free != NULL)
504 runtime->private_free(substream);
505 snd_rawmidi_runtime_free(substream);
506 substream->opened = 0;
507 substream->append = 0;
509 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
511 mutex_unlock(&rmidi->open_mutex);
512 module_put(rmidi->card->module);
516 static int snd_rawmidi_release(struct inode *inode, struct file *file)
518 struct snd_rawmidi_file *rfile;
519 struct snd_rawmidi *rmidi;
522 rfile = file->private_data;
523 err = snd_rawmidi_kernel_release(rfile);
524 rmidi = rfile->rmidi;
525 wake_up(&rmidi->open_wait);
527 snd_card_file_remove(rmidi->card, file);
531 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
532 struct snd_rawmidi_info *info)
534 struct snd_rawmidi *rmidi;
536 if (substream == NULL)
538 rmidi = substream->rmidi;
539 memset(info, 0, sizeof(*info));
540 info->card = rmidi->card->number;
541 info->device = rmidi->device;
542 info->subdevice = substream->number;
543 info->stream = substream->stream;
544 info->flags = rmidi->info_flags;
545 strcpy(info->id, rmidi->id);
546 strcpy(info->name, rmidi->name);
547 strcpy(info->subname, substream->name);
548 info->subdevices_count = substream->pstr->substream_count;
549 info->subdevices_avail = (substream->pstr->substream_count -
550 substream->pstr->substream_opened);
554 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
555 struct snd_rawmidi_info __user * _info)
557 struct snd_rawmidi_info info;
559 if ((err = snd_rawmidi_info(substream, &info)) < 0)
561 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
566 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
568 struct snd_rawmidi *rmidi;
569 struct snd_rawmidi_str *pstr;
570 struct snd_rawmidi_substream *substream;
572 mutex_lock(®ister_mutex);
573 rmidi = snd_rawmidi_search(card, info->device);
574 mutex_unlock(®ister_mutex);
577 if (info->stream < 0 || info->stream > 1)
579 pstr = &rmidi->streams[info->stream];
580 if (pstr->substream_count == 0)
582 if (info->subdevice >= pstr->substream_count)
584 list_for_each_entry(substream, &pstr->substreams, list) {
585 if ((unsigned int)substream->number == info->subdevice)
586 return snd_rawmidi_info(substream, info);
591 static int snd_rawmidi_info_select_user(struct snd_card *card,
592 struct snd_rawmidi_info __user *_info)
595 struct snd_rawmidi_info info;
596 if (get_user(info.device, &_info->device))
598 if (get_user(info.stream, &_info->stream))
600 if (get_user(info.subdevice, &_info->subdevice))
602 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
604 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
609 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
610 struct snd_rawmidi_params * params)
613 struct snd_rawmidi_runtime *runtime = substream->runtime;
615 if (substream->append && substream->use_count > 1)
617 snd_rawmidi_drain_output(substream);
618 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
621 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
624 if (params->buffer_size != runtime->buffer_size) {
625 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
628 kfree(runtime->buffer);
629 runtime->buffer = newbuf;
630 runtime->buffer_size = params->buffer_size;
631 runtime->avail = runtime->buffer_size;
633 runtime->avail_min = params->avail_min;
634 substream->active_sensing = !params->no_active_sensing;
638 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
639 struct snd_rawmidi_params * params)
642 struct snd_rawmidi_runtime *runtime = substream->runtime;
644 snd_rawmidi_drain_input(substream);
645 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
648 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
651 if (params->buffer_size != runtime->buffer_size) {
652 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
655 kfree(runtime->buffer);
656 runtime->buffer = newbuf;
657 runtime->buffer_size = params->buffer_size;
659 runtime->avail_min = params->avail_min;
663 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
664 struct snd_rawmidi_status * status)
666 struct snd_rawmidi_runtime *runtime = substream->runtime;
668 memset(status, 0, sizeof(*status));
669 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
670 spin_lock_irq(&runtime->lock);
671 status->avail = runtime->avail;
672 spin_unlock_irq(&runtime->lock);
676 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
677 struct snd_rawmidi_status * status)
679 struct snd_rawmidi_runtime *runtime = substream->runtime;
681 memset(status, 0, sizeof(*status));
682 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
683 spin_lock_irq(&runtime->lock);
684 status->avail = runtime->avail;
685 status->xruns = runtime->xruns;
687 spin_unlock_irq(&runtime->lock);
691 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
693 struct snd_rawmidi_file *rfile;
694 void __user *argp = (void __user *)arg;
696 rfile = file->private_data;
697 if (((cmd >> 8) & 0xff) != 'W')
700 case SNDRV_RAWMIDI_IOCTL_PVERSION:
701 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
702 case SNDRV_RAWMIDI_IOCTL_INFO:
705 struct snd_rawmidi_info __user *info = argp;
706 if (get_user(stream, &info->stream))
709 case SNDRV_RAWMIDI_STREAM_INPUT:
710 return snd_rawmidi_info_user(rfile->input, info);
711 case SNDRV_RAWMIDI_STREAM_OUTPUT:
712 return snd_rawmidi_info_user(rfile->output, info);
717 case SNDRV_RAWMIDI_IOCTL_PARAMS:
719 struct snd_rawmidi_params params;
720 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
722 switch (params.stream) {
723 case SNDRV_RAWMIDI_STREAM_OUTPUT:
724 if (rfile->output == NULL)
726 return snd_rawmidi_output_params(rfile->output, ¶ms);
727 case SNDRV_RAWMIDI_STREAM_INPUT:
728 if (rfile->input == NULL)
730 return snd_rawmidi_input_params(rfile->input, ¶ms);
735 case SNDRV_RAWMIDI_IOCTL_STATUS:
738 struct snd_rawmidi_status status;
739 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
741 switch (status.stream) {
742 case SNDRV_RAWMIDI_STREAM_OUTPUT:
743 if (rfile->output == NULL)
745 err = snd_rawmidi_output_status(rfile->output, &status);
747 case SNDRV_RAWMIDI_STREAM_INPUT:
748 if (rfile->input == NULL)
750 err = snd_rawmidi_input_status(rfile->input, &status);
757 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
761 case SNDRV_RAWMIDI_IOCTL_DROP:
764 if (get_user(val, (int __user *) argp))
767 case SNDRV_RAWMIDI_STREAM_OUTPUT:
768 if (rfile->output == NULL)
770 return snd_rawmidi_drop_output(rfile->output);
775 case SNDRV_RAWMIDI_IOCTL_DRAIN:
778 if (get_user(val, (int __user *) argp))
781 case SNDRV_RAWMIDI_STREAM_OUTPUT:
782 if (rfile->output == NULL)
784 return snd_rawmidi_drain_output(rfile->output);
785 case SNDRV_RAWMIDI_STREAM_INPUT:
786 if (rfile->input == NULL)
788 return snd_rawmidi_drain_input(rfile->input);
793 #ifdef CONFIG_SND_DEBUG
795 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
801 static int snd_rawmidi_control_ioctl(struct snd_card *card,
802 struct snd_ctl_file *control,
806 void __user *argp = (void __user *)arg;
809 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
813 if (get_user(device, (int __user *)argp))
815 mutex_lock(®ister_mutex);
816 device = device < 0 ? 0 : device + 1;
817 while (device < SNDRV_RAWMIDI_DEVICES) {
818 if (snd_rawmidi_search(card, device))
822 if (device == SNDRV_RAWMIDI_DEVICES)
824 mutex_unlock(®ister_mutex);
825 if (put_user(device, (int __user *)argp))
829 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
833 if (get_user(val, (int __user *)argp))
835 control->prefer_rawmidi_subdevice = val;
838 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
839 return snd_rawmidi_info_select_user(card, argp);
845 * snd_rawmidi_receive - receive the input data from the device
846 * @substream: the rawmidi substream
847 * @buffer: the buffer pointer
848 * @count: the data size to read
850 * Reads the data from the internal buffer.
852 * Returns the size of read data, or a negative error code on failure.
854 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
855 const unsigned char *buffer, int count)
858 int result = 0, count1;
859 struct snd_rawmidi_runtime *runtime = substream->runtime;
861 if (runtime->buffer == NULL) {
862 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
865 spin_lock_irqsave(&runtime->lock, flags);
866 if (count == 1) { /* special case, faster code */
868 if (runtime->avail < runtime->buffer_size) {
869 runtime->buffer[runtime->hw_ptr++] = buffer[0];
870 runtime->hw_ptr %= runtime->buffer_size;
877 substream->bytes += count;
878 count1 = runtime->buffer_size - runtime->hw_ptr;
881 if (count1 > (int)(runtime->buffer_size - runtime->avail))
882 count1 = runtime->buffer_size - runtime->avail;
883 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
884 runtime->hw_ptr += count1;
885 runtime->hw_ptr %= runtime->buffer_size;
886 runtime->avail += count1;
892 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
893 count1 = runtime->buffer_size - runtime->avail;
894 runtime->xruns += count - count1;
897 memcpy(runtime->buffer, buffer, count1);
898 runtime->hw_ptr = count1;
899 runtime->avail += count1;
906 tasklet_hi_schedule(&runtime->tasklet);
907 else if (snd_rawmidi_ready(substream))
908 wake_up(&runtime->sleep);
910 spin_unlock_irqrestore(&runtime->lock, flags);
914 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
915 unsigned char *buf, long count, int kernel)
918 long result = 0, count1;
919 struct snd_rawmidi_runtime *runtime = substream->runtime;
921 while (count > 0 && runtime->avail) {
922 count1 = runtime->buffer_size - runtime->appl_ptr;
925 spin_lock_irqsave(&runtime->lock, flags);
926 if (count1 > (int)runtime->avail)
927 count1 = runtime->avail;
929 memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
931 spin_unlock_irqrestore(&runtime->lock, flags);
932 if (copy_to_user((char __user *)buf + result,
933 runtime->buffer + runtime->appl_ptr, count1)) {
934 return result > 0 ? result : -EFAULT;
936 spin_lock_irqsave(&runtime->lock, flags);
938 runtime->appl_ptr += count1;
939 runtime->appl_ptr %= runtime->buffer_size;
940 runtime->avail -= count1;
941 spin_unlock_irqrestore(&runtime->lock, flags);
948 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
949 unsigned char *buf, long count)
951 snd_rawmidi_input_trigger(substream, 1);
952 return snd_rawmidi_kernel_read1(substream, buf, count, 1);
955 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
960 struct snd_rawmidi_file *rfile;
961 struct snd_rawmidi_substream *substream;
962 struct snd_rawmidi_runtime *runtime;
964 rfile = file->private_data;
965 substream = rfile->input;
966 if (substream == NULL)
968 runtime = substream->runtime;
969 snd_rawmidi_input_trigger(substream, 1);
972 spin_lock_irq(&runtime->lock);
973 while (!snd_rawmidi_ready(substream)) {
975 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
976 spin_unlock_irq(&runtime->lock);
977 return result > 0 ? result : -EAGAIN;
979 init_waitqueue_entry(&wait, current);
980 add_wait_queue(&runtime->sleep, &wait);
981 set_current_state(TASK_INTERRUPTIBLE);
982 spin_unlock_irq(&runtime->lock);
984 remove_wait_queue(&runtime->sleep, &wait);
985 if (signal_pending(current))
986 return result > 0 ? result : -ERESTARTSYS;
988 return result > 0 ? result : -EIO;
989 spin_lock_irq(&runtime->lock);
991 spin_unlock_irq(&runtime->lock);
992 count1 = snd_rawmidi_kernel_read1(substream,
993 (unsigned char __force *)buf,
996 return result > 0 ? result : count1;
1005 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1006 * @substream: the rawmidi substream
1008 * Returns 1 if the internal output buffer is empty, 0 if not.
1010 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1012 struct snd_rawmidi_runtime *runtime = substream->runtime;
1014 unsigned long flags;
1016 if (runtime->buffer == NULL) {
1017 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1020 spin_lock_irqsave(&runtime->lock, flags);
1021 result = runtime->avail >= runtime->buffer_size;
1022 spin_unlock_irqrestore(&runtime->lock, flags);
1027 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1028 * @substream: the rawmidi substream
1029 * @buffer: the buffer pointer
1030 * @count: data size to transfer
1032 * Copies data from the internal output buffer to the given buffer.
1034 * Call this in the interrupt handler when the midi output is ready,
1035 * and call snd_rawmidi_transmit_ack() after the transmission is
1038 * Returns the size of copied data, or a negative error code on failure.
1040 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1041 unsigned char *buffer, int count)
1043 unsigned long flags;
1045 struct snd_rawmidi_runtime *runtime = substream->runtime;
1047 if (runtime->buffer == NULL) {
1048 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1052 spin_lock_irqsave(&runtime->lock, flags);
1053 if (runtime->avail >= runtime->buffer_size) {
1054 /* warning: lowlevel layer MUST trigger down the hardware */
1057 if (count == 1) { /* special case, faster code */
1058 *buffer = runtime->buffer[runtime->hw_ptr];
1061 count1 = runtime->buffer_size - runtime->hw_ptr;
1064 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1065 count1 = runtime->buffer_size - runtime->avail;
1066 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1070 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1071 count = runtime->buffer_size - runtime->avail - count1;
1072 memcpy(buffer + count1, runtime->buffer, count);
1077 spin_unlock_irqrestore(&runtime->lock, flags);
1082 * snd_rawmidi_transmit_ack - acknowledge the transmission
1083 * @substream: the rawmidi substream
1084 * @count: the tranferred count
1086 * Advances the hardware pointer for the internal output buffer with
1087 * the given size and updates the condition.
1088 * Call after the transmission is finished.
1090 * Returns the advanced size if successful, or a negative error code on failure.
1092 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1094 unsigned long flags;
1095 struct snd_rawmidi_runtime *runtime = substream->runtime;
1097 if (runtime->buffer == NULL) {
1098 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1101 spin_lock_irqsave(&runtime->lock, flags);
1102 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1103 runtime->hw_ptr += count;
1104 runtime->hw_ptr %= runtime->buffer_size;
1105 runtime->avail += count;
1106 substream->bytes += count;
1108 if (runtime->drain || snd_rawmidi_ready(substream))
1109 wake_up(&runtime->sleep);
1111 spin_unlock_irqrestore(&runtime->lock, flags);
1116 * snd_rawmidi_transmit - copy from the buffer to the device
1117 * @substream: the rawmidi substream
1118 * @buffer: the buffer pointer
1119 * @count: the data size to transfer
1121 * Copies data from the buffer to the device and advances the pointer.
1123 * Returns the copied size if successful, or a negative error code on failure.
1125 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1126 unsigned char *buffer, int count)
1128 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1131 return snd_rawmidi_transmit_ack(substream, count);
1134 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1135 const unsigned char *buf, long count, int kernel)
1137 unsigned long flags;
1138 long count1, result;
1139 struct snd_rawmidi_runtime *runtime = substream->runtime;
1141 snd_assert(buf != NULL, return -EINVAL);
1142 snd_assert(runtime->buffer != NULL, return -EINVAL);
1145 spin_lock_irqsave(&runtime->lock, flags);
1146 if (substream->append) {
1147 if ((long)runtime->avail < count) {
1148 spin_unlock_irqrestore(&runtime->lock, flags);
1152 while (count > 0 && runtime->avail > 0) {
1153 count1 = runtime->buffer_size - runtime->appl_ptr;
1156 if (count1 > (long)runtime->avail)
1157 count1 = runtime->avail;
1159 memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1161 spin_unlock_irqrestore(&runtime->lock, flags);
1162 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1163 (char __user *)buf, count1)) {
1164 spin_lock_irqsave(&runtime->lock, flags);
1165 result = result > 0 ? result : -EFAULT;
1168 spin_lock_irqsave(&runtime->lock, flags);
1170 runtime->appl_ptr += count1;
1171 runtime->appl_ptr %= runtime->buffer_size;
1172 runtime->avail -= count1;
1178 count1 = runtime->avail < runtime->buffer_size;
1179 spin_unlock_irqrestore(&runtime->lock, flags);
1181 snd_rawmidi_output_trigger(substream, 1);
1185 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1186 const unsigned char *buf, long count)
1188 return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1191 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1192 size_t count, loff_t *offset)
1194 long result, timeout;
1196 struct snd_rawmidi_file *rfile;
1197 struct snd_rawmidi_runtime *runtime;
1198 struct snd_rawmidi_substream *substream;
1200 rfile = file->private_data;
1201 substream = rfile->output;
1202 runtime = substream->runtime;
1203 /* we cannot put an atomic message to our buffer */
1204 if (substream->append && count > runtime->buffer_size)
1208 spin_lock_irq(&runtime->lock);
1209 while (!snd_rawmidi_ready_append(substream, count)) {
1211 if (file->f_flags & O_NONBLOCK) {
1212 spin_unlock_irq(&runtime->lock);
1213 return result > 0 ? result : -EAGAIN;
1215 init_waitqueue_entry(&wait, current);
1216 add_wait_queue(&runtime->sleep, &wait);
1217 set_current_state(TASK_INTERRUPTIBLE);
1218 spin_unlock_irq(&runtime->lock);
1219 timeout = schedule_timeout(30 * HZ);
1220 remove_wait_queue(&runtime->sleep, &wait);
1221 if (signal_pending(current))
1222 return result > 0 ? result : -ERESTARTSYS;
1223 if (!runtime->avail && !timeout)
1224 return result > 0 ? result : -EIO;
1225 spin_lock_irq(&runtime->lock);
1227 spin_unlock_irq(&runtime->lock);
1228 count1 = snd_rawmidi_kernel_write1(substream,
1229 (unsigned char __force *)buf,
1232 return result > 0 ? result : count1;
1235 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1239 if (file->f_flags & O_SYNC) {
1240 spin_lock_irq(&runtime->lock);
1241 while (runtime->avail != runtime->buffer_size) {
1243 unsigned int last_avail = runtime->avail;
1244 init_waitqueue_entry(&wait, current);
1245 add_wait_queue(&runtime->sleep, &wait);
1246 set_current_state(TASK_INTERRUPTIBLE);
1247 spin_unlock_irq(&runtime->lock);
1248 timeout = schedule_timeout(30 * HZ);
1249 remove_wait_queue(&runtime->sleep, &wait);
1250 if (signal_pending(current))
1251 return result > 0 ? result : -ERESTARTSYS;
1252 if (runtime->avail == last_avail && !timeout)
1253 return result > 0 ? result : -EIO;
1254 spin_lock_irq(&runtime->lock);
1256 spin_unlock_irq(&runtime->lock);
1261 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1263 struct snd_rawmidi_file *rfile;
1264 struct snd_rawmidi_runtime *runtime;
1267 rfile = file->private_data;
1268 if (rfile->input != NULL) {
1269 runtime = rfile->input->runtime;
1270 snd_rawmidi_input_trigger(rfile->input, 1);
1271 poll_wait(file, &runtime->sleep, wait);
1273 if (rfile->output != NULL) {
1274 runtime = rfile->output->runtime;
1275 poll_wait(file, &runtime->sleep, wait);
1278 if (rfile->input != NULL) {
1279 if (snd_rawmidi_ready(rfile->input))
1280 mask |= POLLIN | POLLRDNORM;
1282 if (rfile->output != NULL) {
1283 if (snd_rawmidi_ready(rfile->output))
1284 mask |= POLLOUT | POLLWRNORM;
1291 #ifdef CONFIG_COMPAT
1292 #include "rawmidi_compat.c"
1294 #define snd_rawmidi_ioctl_compat NULL
1301 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1302 struct snd_info_buffer *buffer)
1304 struct snd_rawmidi *rmidi;
1305 struct snd_rawmidi_substream *substream;
1306 struct snd_rawmidi_runtime *runtime;
1308 rmidi = entry->private_data;
1309 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1310 mutex_lock(&rmidi->open_mutex);
1311 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1312 list_for_each_entry(substream,
1313 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1317 " Tx bytes : %lu\n",
1319 (unsigned long) substream->bytes);
1320 if (substream->opened) {
1321 runtime = substream->runtime;
1324 " Buffer size : %lu\n"
1326 runtime->oss ? "OSS compatible" : "native",
1327 (unsigned long) runtime->buffer_size,
1328 (unsigned long) runtime->avail);
1332 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1333 list_for_each_entry(substream,
1334 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1338 " Rx bytes : %lu\n",
1340 (unsigned long) substream->bytes);
1341 if (substream->opened) {
1342 runtime = substream->runtime;
1344 " Buffer size : %lu\n"
1346 " Overruns : %lu\n",
1347 (unsigned long) runtime->buffer_size,
1348 (unsigned long) runtime->avail,
1349 (unsigned long) runtime->xruns);
1353 mutex_unlock(&rmidi->open_mutex);
1357 * Register functions
1360 static const struct file_operations snd_rawmidi_f_ops =
1362 .owner = THIS_MODULE,
1363 .read = snd_rawmidi_read,
1364 .write = snd_rawmidi_write,
1365 .open = snd_rawmidi_open,
1366 .release = snd_rawmidi_release,
1367 .poll = snd_rawmidi_poll,
1368 .unlocked_ioctl = snd_rawmidi_ioctl,
1369 .compat_ioctl = snd_rawmidi_ioctl_compat,
1372 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1373 struct snd_rawmidi_str *stream,
1377 struct snd_rawmidi_substream *substream;
1380 for (idx = 0; idx < count; idx++) {
1381 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1382 if (substream == NULL) {
1383 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1386 substream->stream = direction;
1387 substream->number = idx;
1388 substream->rmidi = rmidi;
1389 substream->pstr = stream;
1390 list_add_tail(&substream->list, &stream->substreams);
1391 stream->substream_count++;
1397 * snd_rawmidi_new - create a rawmidi instance
1398 * @card: the card instance
1399 * @id: the id string
1400 * @device: the device index
1401 * @output_count: the number of output streams
1402 * @input_count: the number of input streams
1403 * @rrawmidi: the pointer to store the new rawmidi instance
1405 * Creates a new rawmidi instance.
1406 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1408 * Returns zero if successful, or a negative error code on failure.
1410 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1411 int output_count, int input_count,
1412 struct snd_rawmidi ** rrawmidi)
1414 struct snd_rawmidi *rmidi;
1416 static struct snd_device_ops ops = {
1417 .dev_free = snd_rawmidi_dev_free,
1418 .dev_register = snd_rawmidi_dev_register,
1419 .dev_disconnect = snd_rawmidi_dev_disconnect,
1422 snd_assert(rrawmidi != NULL, return -EINVAL);
1424 snd_assert(card != NULL, return -ENXIO);
1425 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1426 if (rmidi == NULL) {
1427 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1431 rmidi->device = device;
1432 mutex_init(&rmidi->open_mutex);
1433 init_waitqueue_head(&rmidi->open_wait);
1434 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1435 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1438 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1439 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1440 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1441 SNDRV_RAWMIDI_STREAM_INPUT,
1442 input_count)) < 0) {
1443 snd_rawmidi_free(rmidi);
1446 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1447 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1448 SNDRV_RAWMIDI_STREAM_OUTPUT,
1449 output_count)) < 0) {
1450 snd_rawmidi_free(rmidi);
1453 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1454 snd_rawmidi_free(rmidi);
1461 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1463 struct snd_rawmidi_substream *substream;
1465 while (!list_empty(&stream->substreams)) {
1466 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1467 list_del(&substream->list);
1472 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1474 snd_assert(rmidi != NULL, return -ENXIO);
1476 snd_info_free_entry(rmidi->proc_entry);
1477 rmidi->proc_entry = NULL;
1478 mutex_lock(®ister_mutex);
1479 if (rmidi->ops && rmidi->ops->dev_unregister)
1480 rmidi->ops->dev_unregister(rmidi);
1481 mutex_unlock(®ister_mutex);
1483 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1484 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1485 if (rmidi->private_free)
1486 rmidi->private_free(rmidi);
1491 static int snd_rawmidi_dev_free(struct snd_device *device)
1493 struct snd_rawmidi *rmidi = device->device_data;
1494 return snd_rawmidi_free(rmidi);
1497 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1498 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1500 struct snd_rawmidi *rmidi = device->private_data;
1501 rmidi->seq_dev = NULL;
1505 static int snd_rawmidi_dev_register(struct snd_device *device)
1508 struct snd_info_entry *entry;
1510 struct snd_rawmidi *rmidi = device->device_data;
1512 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1514 mutex_lock(®ister_mutex);
1515 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1516 mutex_unlock(®ister_mutex);
1519 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1520 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1521 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1522 rmidi->card, rmidi->device,
1523 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1524 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1525 list_del(&rmidi->list);
1526 mutex_unlock(®ister_mutex);
1529 if (rmidi->ops && rmidi->ops->dev_register &&
1530 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1531 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1532 list_del(&rmidi->list);
1533 mutex_unlock(®ister_mutex);
1536 #ifdef CONFIG_SND_OSSEMUL
1538 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1539 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1540 rmidi->card, 0, &snd_rawmidi_f_ops,
1542 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1545 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1546 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1550 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1551 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1552 rmidi->card, 1, &snd_rawmidi_f_ops,
1554 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1559 #endif /* CONFIG_SND_OSSEMUL */
1560 mutex_unlock(®ister_mutex);
1561 sprintf(name, "midi%d", rmidi->device);
1562 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1564 entry->private_data = rmidi;
1565 entry->c.text.read = snd_rawmidi_proc_info_read;
1566 if (snd_info_register(entry) < 0) {
1567 snd_info_free_entry(entry);
1571 rmidi->proc_entry = entry;
1572 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1573 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1574 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1575 rmidi->seq_dev->private_data = rmidi;
1576 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1577 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1578 snd_device_register(rmidi->card, rmidi->seq_dev);
1585 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1587 struct snd_rawmidi *rmidi = device->device_data;
1589 mutex_lock(®ister_mutex);
1590 list_del_init(&rmidi->list);
1591 #ifdef CONFIG_SND_OSSEMUL
1592 if (rmidi->ossreg) {
1593 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1594 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1595 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1596 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1599 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1600 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1603 #endif /* CONFIG_SND_OSSEMUL */
1604 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1605 mutex_unlock(®ister_mutex);
1610 * snd_rawmidi_set_ops - set the rawmidi operators
1611 * @rmidi: the rawmidi instance
1612 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1613 * @ops: the operator table
1615 * Sets the rawmidi operators for the given stream direction.
1617 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1618 struct snd_rawmidi_ops *ops)
1620 struct snd_rawmidi_substream *substream;
1622 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1623 substream->ops = ops;
1630 static int __init alsa_rawmidi_init(void)
1633 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1634 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1635 #ifdef CONFIG_SND_OSSEMUL
1637 /* check device map table */
1638 for (i = 0; i < SNDRV_CARDS; i++) {
1639 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1640 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1643 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1644 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1649 #endif /* CONFIG_SND_OSSEMUL */
1653 static void __exit alsa_rawmidi_exit(void)
1655 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1656 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1659 module_init(alsa_rawmidi_init)
1660 module_exit(alsa_rawmidi_exit)
1662 EXPORT_SYMBOL(snd_rawmidi_output_params);
1663 EXPORT_SYMBOL(snd_rawmidi_input_params);
1664 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1665 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1666 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1667 EXPORT_SYMBOL(snd_rawmidi_receive);
1668 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1669 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1670 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1671 EXPORT_SYMBOL(snd_rawmidi_transmit);
1672 EXPORT_SYMBOL(snd_rawmidi_new);
1673 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1674 EXPORT_SYMBOL(snd_rawmidi_info_select);
1675 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1676 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1677 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1678 EXPORT_SYMBOL(snd_rawmidi_kernel_write);