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/module.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 #define rmidi_err(rmidi, fmt, args...) \
60 dev_err((rmidi)->card->dev, fmt, ##args)
61 #define rmidi_warn(rmidi, fmt, args...) \
62 dev_warn((rmidi)->card->dev, fmt, ##args)
63 #define rmidi_dbg(rmidi, fmt, args...) \
64 dev_dbg((rmidi)->card->dev, fmt, ##args)
66 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
68 struct snd_rawmidi *rawmidi;
70 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
71 if (rawmidi->card == card && rawmidi->device == device)
76 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
78 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
80 return SNDRV_RAWMIDI_LFLG_OUTPUT;
82 return SNDRV_RAWMIDI_LFLG_INPUT;
84 return SNDRV_RAWMIDI_LFLG_OPEN;
88 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
90 struct snd_rawmidi_runtime *runtime = substream->runtime;
91 return runtime->avail >= runtime->avail_min;
94 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
97 struct snd_rawmidi_runtime *runtime = substream->runtime;
98 return runtime->avail >= runtime->avail_min &&
99 (!substream->append || runtime->avail >= count);
102 static void snd_rawmidi_input_event_work(struct work_struct *work)
104 struct snd_rawmidi_runtime *runtime =
105 container_of(work, struct snd_rawmidi_runtime, event_work);
107 runtime->event(runtime->substream);
110 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
112 struct snd_rawmidi_runtime *runtime;
114 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
116 runtime->substream = substream;
117 spin_lock_init(&runtime->lock);
118 init_waitqueue_head(&runtime->sleep);
119 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
120 runtime->event = NULL;
121 runtime->buffer_size = PAGE_SIZE;
122 runtime->avail_min = 1;
123 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
126 runtime->avail = runtime->buffer_size;
127 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
131 runtime->appl_ptr = runtime->hw_ptr = 0;
132 substream->runtime = runtime;
136 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
138 struct snd_rawmidi_runtime *runtime = substream->runtime;
140 kfree(runtime->buffer);
142 substream->runtime = NULL;
146 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
148 if (!substream->opened)
150 substream->ops->trigger(substream, up);
153 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
155 if (!substream->opened)
157 substream->ops->trigger(substream, up);
159 cancel_work_sync(&substream->runtime->event_work);
162 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
165 struct snd_rawmidi_runtime *runtime = substream->runtime;
167 snd_rawmidi_output_trigger(substream, 0);
169 spin_lock_irqsave(&runtime->lock, flags);
170 runtime->appl_ptr = runtime->hw_ptr = 0;
171 runtime->avail = runtime->buffer_size;
172 spin_unlock_irqrestore(&runtime->lock, flags);
176 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
180 struct snd_rawmidi_runtime *runtime = substream->runtime;
184 timeout = wait_event_interruptible_timeout(runtime->sleep,
185 (runtime->avail >= runtime->buffer_size),
187 if (signal_pending(current))
189 if (runtime->avail < runtime->buffer_size && !timeout) {
190 rmidi_warn(substream->rmidi,
191 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
192 (long)runtime->avail, (long)runtime->buffer_size);
196 if (err != -ERESTARTSYS) {
197 /* we need wait a while to make sure that Tx FIFOs are empty */
198 if (substream->ops->drain)
199 substream->ops->drain(substream);
202 snd_rawmidi_drop_output(substream);
207 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
210 struct snd_rawmidi_runtime *runtime = substream->runtime;
212 snd_rawmidi_input_trigger(substream, 0);
214 spin_lock_irqsave(&runtime->lock, flags);
215 runtime->appl_ptr = runtime->hw_ptr = 0;
217 spin_unlock_irqrestore(&runtime->lock, flags);
221 /* look for an available substream for the given stream direction;
222 * if a specific subdevice is given, try to assign it
224 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
225 int stream, int mode,
226 struct snd_rawmidi_substream **sub_ret)
228 struct snd_rawmidi_substream *substream;
229 struct snd_rawmidi_str *s = &rmidi->streams[stream];
230 static unsigned int info_flags[2] = {
231 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
232 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
235 if (!(rmidi->info_flags & info_flags[stream]))
237 if (subdevice >= 0 && subdevice >= s->substream_count)
240 list_for_each_entry(substream, &s->substreams, list) {
241 if (substream->opened) {
242 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
243 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
247 if (subdevice < 0 || subdevice == substream->number) {
248 *sub_ret = substream;
255 /* open and do ref-counting for the given substream */
256 static int open_substream(struct snd_rawmidi *rmidi,
257 struct snd_rawmidi_substream *substream,
262 if (substream->use_count == 0) {
263 err = snd_rawmidi_runtime_create(substream);
266 err = substream->ops->open(substream);
268 snd_rawmidi_runtime_free(substream);
271 substream->opened = 1;
272 substream->active_sensing = 0;
273 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
274 substream->append = 1;
275 substream->pid = get_pid(task_pid(current));
276 rmidi->streams[substream->stream].substream_opened++;
278 substream->use_count++;
282 static void close_substream(struct snd_rawmidi *rmidi,
283 struct snd_rawmidi_substream *substream,
286 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
287 struct snd_rawmidi_file *rfile)
289 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
292 rfile->input = rfile->output = NULL;
293 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
294 err = assign_substream(rmidi, subdevice,
295 SNDRV_RAWMIDI_STREAM_INPUT,
300 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
301 err = assign_substream(rmidi, subdevice,
302 SNDRV_RAWMIDI_STREAM_OUTPUT,
309 err = open_substream(rmidi, sinput, mode);
314 err = open_substream(rmidi, soutput, mode);
317 close_substream(rmidi, sinput, 0);
322 rfile->rmidi = rmidi;
323 rfile->input = sinput;
324 rfile->output = soutput;
328 /* called from sound/core/seq/seq_midi.c */
329 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
330 int mode, struct snd_rawmidi_file * rfile)
332 struct snd_rawmidi *rmidi;
335 if (snd_BUG_ON(!rfile))
338 mutex_lock(®ister_mutex);
339 rmidi = snd_rawmidi_search(card, device);
341 mutex_unlock(®ister_mutex);
344 if (!try_module_get(rmidi->card->module)) {
345 mutex_unlock(®ister_mutex);
348 mutex_unlock(®ister_mutex);
350 mutex_lock(&rmidi->open_mutex);
351 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
352 mutex_unlock(&rmidi->open_mutex);
354 module_put(rmidi->card->module);
358 static int snd_rawmidi_open(struct inode *inode, struct file *file)
360 int maj = imajor(inode);
361 struct snd_card *card;
363 unsigned short fflags;
365 struct snd_rawmidi *rmidi;
366 struct snd_rawmidi_file *rawmidi_file = NULL;
368 struct snd_ctl_file *kctl;
370 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
371 return -EINVAL; /* invalid combination */
373 err = nonseekable_open(inode, file);
377 if (maj == snd_major) {
378 rmidi = snd_lookup_minor_data(iminor(inode),
379 SNDRV_DEVICE_TYPE_RAWMIDI);
380 #ifdef CONFIG_SND_OSSEMUL
381 } else if (maj == SOUND_MAJOR) {
382 rmidi = snd_lookup_oss_minor_data(iminor(inode),
383 SNDRV_OSS_DEVICE_TYPE_MIDI);
391 if (!try_module_get(rmidi->card->module)) {
392 snd_card_unref(rmidi->card);
396 mutex_lock(&rmidi->open_mutex);
398 err = snd_card_file_add(card, file);
401 fflags = snd_rawmidi_file_flags(file);
402 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
403 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
404 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
405 if (rawmidi_file == NULL) {
409 init_waitqueue_entry(&wait, current);
410 add_wait_queue(&rmidi->open_wait, &wait);
413 read_lock(&card->ctl_files_rwlock);
414 list_for_each_entry(kctl, &card->ctl_files, list) {
415 if (kctl->pid == task_pid(current)) {
416 subdevice = kctl->prefer_rawmidi_subdevice;
421 read_unlock(&card->ctl_files_rwlock);
422 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
425 if (err == -EAGAIN) {
426 if (file->f_flags & O_NONBLOCK) {
432 set_current_state(TASK_INTERRUPTIBLE);
433 mutex_unlock(&rmidi->open_mutex);
435 mutex_lock(&rmidi->open_mutex);
436 if (rmidi->card->shutdown) {
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);
458 snd_card_unref(rmidi->card);
462 snd_card_file_remove(card, file);
464 mutex_unlock(&rmidi->open_mutex);
465 module_put(rmidi->card->module);
466 snd_card_unref(rmidi->card);
470 static void close_substream(struct snd_rawmidi *rmidi,
471 struct snd_rawmidi_substream *substream,
474 if (--substream->use_count)
478 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
479 snd_rawmidi_input_trigger(substream, 0);
481 if (substream->active_sensing) {
482 unsigned char buf = 0xfe;
483 /* sending single active sensing message
484 * to shut the device up
486 snd_rawmidi_kernel_write(substream, &buf, 1);
488 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
489 snd_rawmidi_output_trigger(substream, 0);
492 substream->ops->close(substream);
493 if (substream->runtime->private_free)
494 substream->runtime->private_free(substream);
495 snd_rawmidi_runtime_free(substream);
496 substream->opened = 0;
497 substream->append = 0;
498 put_pid(substream->pid);
499 substream->pid = NULL;
500 rmidi->streams[substream->stream].substream_opened--;
503 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
505 struct snd_rawmidi *rmidi;
507 rmidi = rfile->rmidi;
508 mutex_lock(&rmidi->open_mutex);
510 close_substream(rmidi, rfile->input, 1);
514 close_substream(rmidi, rfile->output, 1);
515 rfile->output = NULL;
518 mutex_unlock(&rmidi->open_mutex);
519 wake_up(&rmidi->open_wait);
522 /* called from sound/core/seq/seq_midi.c */
523 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
525 struct snd_rawmidi *rmidi;
527 if (snd_BUG_ON(!rfile))
530 rmidi = rfile->rmidi;
531 rawmidi_release_priv(rfile);
532 module_put(rmidi->card->module);
536 static int snd_rawmidi_release(struct inode *inode, struct file *file)
538 struct snd_rawmidi_file *rfile;
539 struct snd_rawmidi *rmidi;
540 struct module *module;
542 rfile = file->private_data;
543 rmidi = rfile->rmidi;
544 rawmidi_release_priv(rfile);
546 module = rmidi->card->module;
547 snd_card_file_remove(rmidi->card, file);
552 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
553 struct snd_rawmidi_info *info)
555 struct snd_rawmidi *rmidi;
557 if (substream == NULL)
559 rmidi = substream->rmidi;
560 memset(info, 0, sizeof(*info));
561 info->card = rmidi->card->number;
562 info->device = rmidi->device;
563 info->subdevice = substream->number;
564 info->stream = substream->stream;
565 info->flags = rmidi->info_flags;
566 strcpy(info->id, rmidi->id);
567 strcpy(info->name, rmidi->name);
568 strcpy(info->subname, substream->name);
569 info->subdevices_count = substream->pstr->substream_count;
570 info->subdevices_avail = (substream->pstr->substream_count -
571 substream->pstr->substream_opened);
575 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
576 struct snd_rawmidi_info __user * _info)
578 struct snd_rawmidi_info info;
580 if ((err = snd_rawmidi_info(substream, &info)) < 0)
582 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
587 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
589 struct snd_rawmidi *rmidi;
590 struct snd_rawmidi_str *pstr;
591 struct snd_rawmidi_substream *substream;
593 mutex_lock(®ister_mutex);
594 rmidi = snd_rawmidi_search(card, info->device);
595 mutex_unlock(®ister_mutex);
598 if (info->stream < 0 || info->stream > 1)
600 pstr = &rmidi->streams[info->stream];
601 if (pstr->substream_count == 0)
603 if (info->subdevice >= pstr->substream_count)
605 list_for_each_entry(substream, &pstr->substreams, list) {
606 if ((unsigned int)substream->number == info->subdevice)
607 return snd_rawmidi_info(substream, info);
612 static int snd_rawmidi_info_select_user(struct snd_card *card,
613 struct snd_rawmidi_info __user *_info)
616 struct snd_rawmidi_info info;
617 if (get_user(info.device, &_info->device))
619 if (get_user(info.stream, &_info->stream))
621 if (get_user(info.subdevice, &_info->subdevice))
623 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
625 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
630 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
631 struct snd_rawmidi_params * params)
634 struct snd_rawmidi_runtime *runtime = substream->runtime;
636 if (substream->append && substream->use_count > 1)
638 snd_rawmidi_drain_output(substream);
639 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
642 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
645 if (params->buffer_size != runtime->buffer_size) {
646 newbuf = krealloc(runtime->buffer, params->buffer_size,
650 runtime->buffer = newbuf;
651 runtime->buffer_size = params->buffer_size;
652 runtime->avail = runtime->buffer_size;
654 runtime->avail_min = params->avail_min;
655 substream->active_sensing = !params->no_active_sensing;
659 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
660 struct snd_rawmidi_params * params)
663 struct snd_rawmidi_runtime *runtime = substream->runtime;
665 snd_rawmidi_drain_input(substream);
666 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
669 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
672 if (params->buffer_size != runtime->buffer_size) {
673 newbuf = krealloc(runtime->buffer, params->buffer_size,
677 runtime->buffer = newbuf;
678 runtime->buffer_size = params->buffer_size;
680 runtime->avail_min = params->avail_min;
684 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
685 struct snd_rawmidi_status * status)
687 struct snd_rawmidi_runtime *runtime = substream->runtime;
689 memset(status, 0, sizeof(*status));
690 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
691 spin_lock_irq(&runtime->lock);
692 status->avail = runtime->avail;
693 spin_unlock_irq(&runtime->lock);
697 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
698 struct snd_rawmidi_status * status)
700 struct snd_rawmidi_runtime *runtime = substream->runtime;
702 memset(status, 0, sizeof(*status));
703 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
704 spin_lock_irq(&runtime->lock);
705 status->avail = runtime->avail;
706 status->xruns = runtime->xruns;
708 spin_unlock_irq(&runtime->lock);
712 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
714 struct snd_rawmidi_file *rfile;
715 void __user *argp = (void __user *)arg;
717 rfile = file->private_data;
718 if (((cmd >> 8) & 0xff) != 'W')
721 case SNDRV_RAWMIDI_IOCTL_PVERSION:
722 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
723 case SNDRV_RAWMIDI_IOCTL_INFO:
726 struct snd_rawmidi_info __user *info = argp;
727 if (get_user(stream, &info->stream))
730 case SNDRV_RAWMIDI_STREAM_INPUT:
731 return snd_rawmidi_info_user(rfile->input, info);
732 case SNDRV_RAWMIDI_STREAM_OUTPUT:
733 return snd_rawmidi_info_user(rfile->output, info);
738 case SNDRV_RAWMIDI_IOCTL_PARAMS:
740 struct snd_rawmidi_params params;
741 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
743 switch (params.stream) {
744 case SNDRV_RAWMIDI_STREAM_OUTPUT:
745 if (rfile->output == NULL)
747 return snd_rawmidi_output_params(rfile->output, ¶ms);
748 case SNDRV_RAWMIDI_STREAM_INPUT:
749 if (rfile->input == NULL)
751 return snd_rawmidi_input_params(rfile->input, ¶ms);
756 case SNDRV_RAWMIDI_IOCTL_STATUS:
759 struct snd_rawmidi_status status;
760 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
762 switch (status.stream) {
763 case SNDRV_RAWMIDI_STREAM_OUTPUT:
764 if (rfile->output == NULL)
766 err = snd_rawmidi_output_status(rfile->output, &status);
768 case SNDRV_RAWMIDI_STREAM_INPUT:
769 if (rfile->input == NULL)
771 err = snd_rawmidi_input_status(rfile->input, &status);
778 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
782 case SNDRV_RAWMIDI_IOCTL_DROP:
785 if (get_user(val, (int __user *) argp))
788 case SNDRV_RAWMIDI_STREAM_OUTPUT:
789 if (rfile->output == NULL)
791 return snd_rawmidi_drop_output(rfile->output);
796 case SNDRV_RAWMIDI_IOCTL_DRAIN:
799 if (get_user(val, (int __user *) argp))
802 case SNDRV_RAWMIDI_STREAM_OUTPUT:
803 if (rfile->output == NULL)
805 return snd_rawmidi_drain_output(rfile->output);
806 case SNDRV_RAWMIDI_STREAM_INPUT:
807 if (rfile->input == NULL)
809 return snd_rawmidi_drain_input(rfile->input);
815 rmidi_dbg(rfile->rmidi,
816 "rawmidi: unknown command = 0x%x\n", cmd);
821 static int snd_rawmidi_control_ioctl(struct snd_card *card,
822 struct snd_ctl_file *control,
826 void __user *argp = (void __user *)arg;
829 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
833 if (get_user(device, (int __user *)argp))
835 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
836 device = SNDRV_RAWMIDI_DEVICES - 1;
837 mutex_lock(®ister_mutex);
838 device = device < 0 ? 0 : device + 1;
839 while (device < SNDRV_RAWMIDI_DEVICES) {
840 if (snd_rawmidi_search(card, device))
844 if (device == SNDRV_RAWMIDI_DEVICES)
846 mutex_unlock(®ister_mutex);
847 if (put_user(device, (int __user *)argp))
851 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
855 if (get_user(val, (int __user *)argp))
857 control->prefer_rawmidi_subdevice = val;
860 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
861 return snd_rawmidi_info_select_user(card, argp);
867 * snd_rawmidi_receive - receive the input data from the device
868 * @substream: the rawmidi substream
869 * @buffer: the buffer pointer
870 * @count: the data size to read
872 * Reads the data from the internal buffer.
874 * Return: The size of read data, or a negative error code on failure.
876 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
877 const unsigned char *buffer, int count)
880 int result = 0, count1;
881 struct snd_rawmidi_runtime *runtime = substream->runtime;
883 if (!substream->opened)
885 if (runtime->buffer == NULL) {
886 rmidi_dbg(substream->rmidi,
887 "snd_rawmidi_receive: input is not active!!!\n");
890 spin_lock_irqsave(&runtime->lock, flags);
891 if (count == 1) { /* special case, faster code */
893 if (runtime->avail < runtime->buffer_size) {
894 runtime->buffer[runtime->hw_ptr++] = buffer[0];
895 runtime->hw_ptr %= runtime->buffer_size;
902 substream->bytes += count;
903 count1 = runtime->buffer_size - runtime->hw_ptr;
906 if (count1 > (int)(runtime->buffer_size - runtime->avail))
907 count1 = runtime->buffer_size - runtime->avail;
908 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
909 runtime->hw_ptr += count1;
910 runtime->hw_ptr %= runtime->buffer_size;
911 runtime->avail += count1;
917 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
918 count1 = runtime->buffer_size - runtime->avail;
919 runtime->xruns += count - count1;
922 memcpy(runtime->buffer, buffer, count1);
923 runtime->hw_ptr = count1;
924 runtime->avail += count1;
931 schedule_work(&runtime->event_work);
932 else if (snd_rawmidi_ready(substream))
933 wake_up(&runtime->sleep);
935 spin_unlock_irqrestore(&runtime->lock, flags);
939 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
940 unsigned char __user *userbuf,
941 unsigned char *kernelbuf, long count)
944 long result = 0, count1;
945 struct snd_rawmidi_runtime *runtime = substream->runtime;
947 while (count > 0 && runtime->avail) {
948 count1 = runtime->buffer_size - runtime->appl_ptr;
951 spin_lock_irqsave(&runtime->lock, flags);
952 if (count1 > (int)runtime->avail)
953 count1 = runtime->avail;
955 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
957 spin_unlock_irqrestore(&runtime->lock, flags);
958 if (copy_to_user(userbuf + result,
959 runtime->buffer + runtime->appl_ptr, count1)) {
960 return result > 0 ? result : -EFAULT;
962 spin_lock_irqsave(&runtime->lock, flags);
964 runtime->appl_ptr += count1;
965 runtime->appl_ptr %= runtime->buffer_size;
966 runtime->avail -= count1;
967 spin_unlock_irqrestore(&runtime->lock, flags);
974 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
975 unsigned char *buf, long count)
977 snd_rawmidi_input_trigger(substream, 1);
978 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
981 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
986 struct snd_rawmidi_file *rfile;
987 struct snd_rawmidi_substream *substream;
988 struct snd_rawmidi_runtime *runtime;
990 rfile = file->private_data;
991 substream = rfile->input;
992 if (substream == NULL)
994 runtime = substream->runtime;
995 snd_rawmidi_input_trigger(substream, 1);
998 spin_lock_irq(&runtime->lock);
999 while (!snd_rawmidi_ready(substream)) {
1001 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1002 spin_unlock_irq(&runtime->lock);
1003 return result > 0 ? result : -EAGAIN;
1005 init_waitqueue_entry(&wait, current);
1006 add_wait_queue(&runtime->sleep, &wait);
1007 set_current_state(TASK_INTERRUPTIBLE);
1008 spin_unlock_irq(&runtime->lock);
1010 remove_wait_queue(&runtime->sleep, &wait);
1011 if (rfile->rmidi->card->shutdown)
1013 if (signal_pending(current))
1014 return result > 0 ? result : -ERESTARTSYS;
1015 if (!runtime->avail)
1016 return result > 0 ? result : -EIO;
1017 spin_lock_irq(&runtime->lock);
1019 spin_unlock_irq(&runtime->lock);
1020 count1 = snd_rawmidi_kernel_read1(substream,
1021 (unsigned char __user *)buf,
1025 return result > 0 ? result : count1;
1034 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1035 * @substream: the rawmidi substream
1037 * Return: 1 if the internal output buffer is empty, 0 if not.
1039 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1041 struct snd_rawmidi_runtime *runtime = substream->runtime;
1043 unsigned long flags;
1045 if (runtime->buffer == NULL) {
1046 rmidi_dbg(substream->rmidi,
1047 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1050 spin_lock_irqsave(&runtime->lock, flags);
1051 result = runtime->avail >= runtime->buffer_size;
1052 spin_unlock_irqrestore(&runtime->lock, flags);
1057 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1058 * @substream: the rawmidi substream
1059 * @buffer: the buffer pointer
1060 * @count: data size to transfer
1062 * Copies data from the internal output buffer to the given buffer.
1064 * Call this in the interrupt handler when the midi output is ready,
1065 * and call snd_rawmidi_transmit_ack() after the transmission is
1068 * Return: The size of copied data, or a negative error code on failure.
1070 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1071 unsigned char *buffer, int count)
1073 unsigned long flags;
1075 struct snd_rawmidi_runtime *runtime = substream->runtime;
1077 if (runtime->buffer == NULL) {
1078 rmidi_dbg(substream->rmidi,
1079 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1083 spin_lock_irqsave(&runtime->lock, flags);
1084 if (runtime->avail >= runtime->buffer_size) {
1085 /* warning: lowlevel layer MUST trigger down the hardware */
1088 if (count == 1) { /* special case, faster code */
1089 *buffer = runtime->buffer[runtime->hw_ptr];
1092 count1 = runtime->buffer_size - runtime->hw_ptr;
1095 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1096 count1 = runtime->buffer_size - runtime->avail;
1097 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1101 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1102 count = runtime->buffer_size - runtime->avail - count1;
1103 memcpy(buffer + count1, runtime->buffer, count);
1108 spin_unlock_irqrestore(&runtime->lock, flags);
1113 * snd_rawmidi_transmit_ack - acknowledge the transmission
1114 * @substream: the rawmidi substream
1115 * @count: the transferred count
1117 * Advances the hardware pointer for the internal output buffer with
1118 * the given size and updates the condition.
1119 * Call after the transmission is finished.
1121 * Return: The advanced size if successful, or a negative error code on failure.
1123 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1125 unsigned long flags;
1126 struct snd_rawmidi_runtime *runtime = substream->runtime;
1128 if (runtime->buffer == NULL) {
1129 rmidi_dbg(substream->rmidi,
1130 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1133 spin_lock_irqsave(&runtime->lock, flags);
1134 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1135 runtime->hw_ptr += count;
1136 runtime->hw_ptr %= runtime->buffer_size;
1137 runtime->avail += count;
1138 substream->bytes += count;
1140 if (runtime->drain || snd_rawmidi_ready(substream))
1141 wake_up(&runtime->sleep);
1143 spin_unlock_irqrestore(&runtime->lock, flags);
1148 * snd_rawmidi_transmit - copy from the buffer to the device
1149 * @substream: the rawmidi substream
1150 * @buffer: the buffer pointer
1151 * @count: the data size to transfer
1153 * Copies data from the buffer to the device and advances the pointer.
1155 * Return: The copied size if successful, or a negative error code on failure.
1157 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1158 unsigned char *buffer, int count)
1160 if (!substream->opened)
1162 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1165 return snd_rawmidi_transmit_ack(substream, count);
1168 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1169 const unsigned char __user *userbuf,
1170 const unsigned char *kernelbuf,
1173 unsigned long flags;
1174 long count1, result;
1175 struct snd_rawmidi_runtime *runtime = substream->runtime;
1177 if (snd_BUG_ON(!kernelbuf && !userbuf))
1179 if (snd_BUG_ON(!runtime->buffer))
1183 spin_lock_irqsave(&runtime->lock, flags);
1184 if (substream->append) {
1185 if ((long)runtime->avail < count) {
1186 spin_unlock_irqrestore(&runtime->lock, flags);
1190 while (count > 0 && runtime->avail > 0) {
1191 count1 = runtime->buffer_size - runtime->appl_ptr;
1194 if (count1 > (long)runtime->avail)
1195 count1 = runtime->avail;
1197 memcpy(runtime->buffer + runtime->appl_ptr,
1198 kernelbuf + result, count1);
1200 spin_unlock_irqrestore(&runtime->lock, flags);
1201 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1202 userbuf + result, count1)) {
1203 spin_lock_irqsave(&runtime->lock, flags);
1204 result = result > 0 ? result : -EFAULT;
1207 spin_lock_irqsave(&runtime->lock, flags);
1209 runtime->appl_ptr += count1;
1210 runtime->appl_ptr %= runtime->buffer_size;
1211 runtime->avail -= count1;
1216 count1 = runtime->avail < runtime->buffer_size;
1217 spin_unlock_irqrestore(&runtime->lock, flags);
1219 snd_rawmidi_output_trigger(substream, 1);
1223 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1224 const unsigned char *buf, long count)
1226 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1229 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1230 size_t count, loff_t *offset)
1232 long result, timeout;
1234 struct snd_rawmidi_file *rfile;
1235 struct snd_rawmidi_runtime *runtime;
1236 struct snd_rawmidi_substream *substream;
1238 rfile = file->private_data;
1239 substream = rfile->output;
1240 runtime = substream->runtime;
1241 /* we cannot put an atomic message to our buffer */
1242 if (substream->append && count > runtime->buffer_size)
1246 spin_lock_irq(&runtime->lock);
1247 while (!snd_rawmidi_ready_append(substream, count)) {
1249 if (file->f_flags & O_NONBLOCK) {
1250 spin_unlock_irq(&runtime->lock);
1251 return result > 0 ? result : -EAGAIN;
1253 init_waitqueue_entry(&wait, current);
1254 add_wait_queue(&runtime->sleep, &wait);
1255 set_current_state(TASK_INTERRUPTIBLE);
1256 spin_unlock_irq(&runtime->lock);
1257 timeout = schedule_timeout(30 * HZ);
1258 remove_wait_queue(&runtime->sleep, &wait);
1259 if (rfile->rmidi->card->shutdown)
1261 if (signal_pending(current))
1262 return result > 0 ? result : -ERESTARTSYS;
1263 if (!runtime->avail && !timeout)
1264 return result > 0 ? result : -EIO;
1265 spin_lock_irq(&runtime->lock);
1267 spin_unlock_irq(&runtime->lock);
1268 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1270 return result > 0 ? result : count1;
1273 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1277 if (file->f_flags & O_DSYNC) {
1278 spin_lock_irq(&runtime->lock);
1279 while (runtime->avail != runtime->buffer_size) {
1281 unsigned int last_avail = runtime->avail;
1282 init_waitqueue_entry(&wait, current);
1283 add_wait_queue(&runtime->sleep, &wait);
1284 set_current_state(TASK_INTERRUPTIBLE);
1285 spin_unlock_irq(&runtime->lock);
1286 timeout = schedule_timeout(30 * HZ);
1287 remove_wait_queue(&runtime->sleep, &wait);
1288 if (signal_pending(current))
1289 return result > 0 ? result : -ERESTARTSYS;
1290 if (runtime->avail == last_avail && !timeout)
1291 return result > 0 ? result : -EIO;
1292 spin_lock_irq(&runtime->lock);
1294 spin_unlock_irq(&runtime->lock);
1299 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1301 struct snd_rawmidi_file *rfile;
1302 struct snd_rawmidi_runtime *runtime;
1305 rfile = file->private_data;
1306 if (rfile->input != NULL) {
1307 runtime = rfile->input->runtime;
1308 snd_rawmidi_input_trigger(rfile->input, 1);
1309 poll_wait(file, &runtime->sleep, wait);
1311 if (rfile->output != NULL) {
1312 runtime = rfile->output->runtime;
1313 poll_wait(file, &runtime->sleep, wait);
1316 if (rfile->input != NULL) {
1317 if (snd_rawmidi_ready(rfile->input))
1318 mask |= POLLIN | POLLRDNORM;
1320 if (rfile->output != NULL) {
1321 if (snd_rawmidi_ready(rfile->output))
1322 mask |= POLLOUT | POLLWRNORM;
1329 #ifdef CONFIG_COMPAT
1330 #include "rawmidi_compat.c"
1332 #define snd_rawmidi_ioctl_compat NULL
1339 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1340 struct snd_info_buffer *buffer)
1342 struct snd_rawmidi *rmidi;
1343 struct snd_rawmidi_substream *substream;
1344 struct snd_rawmidi_runtime *runtime;
1346 rmidi = entry->private_data;
1347 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1348 mutex_lock(&rmidi->open_mutex);
1349 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1350 list_for_each_entry(substream,
1351 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1355 " Tx bytes : %lu\n",
1357 (unsigned long) substream->bytes);
1358 if (substream->opened) {
1360 " Owner PID : %d\n",
1361 pid_vnr(substream->pid));
1362 runtime = substream->runtime;
1365 " Buffer size : %lu\n"
1367 runtime->oss ? "OSS compatible" : "native",
1368 (unsigned long) runtime->buffer_size,
1369 (unsigned long) runtime->avail);
1373 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1374 list_for_each_entry(substream,
1375 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1379 " Rx bytes : %lu\n",
1381 (unsigned long) substream->bytes);
1382 if (substream->opened) {
1384 " Owner PID : %d\n",
1385 pid_vnr(substream->pid));
1386 runtime = substream->runtime;
1388 " Buffer size : %lu\n"
1390 " Overruns : %lu\n",
1391 (unsigned long) runtime->buffer_size,
1392 (unsigned long) runtime->avail,
1393 (unsigned long) runtime->xruns);
1397 mutex_unlock(&rmidi->open_mutex);
1401 * Register functions
1404 static const struct file_operations snd_rawmidi_f_ops =
1406 .owner = THIS_MODULE,
1407 .read = snd_rawmidi_read,
1408 .write = snd_rawmidi_write,
1409 .open = snd_rawmidi_open,
1410 .release = snd_rawmidi_release,
1411 .llseek = no_llseek,
1412 .poll = snd_rawmidi_poll,
1413 .unlocked_ioctl = snd_rawmidi_ioctl,
1414 .compat_ioctl = snd_rawmidi_ioctl_compat,
1417 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1418 struct snd_rawmidi_str *stream,
1422 struct snd_rawmidi_substream *substream;
1425 for (idx = 0; idx < count; idx++) {
1426 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1427 if (substream == NULL) {
1428 rmidi_err(rmidi, "rawmidi: cannot allocate substream\n");
1431 substream->stream = direction;
1432 substream->number = idx;
1433 substream->rmidi = rmidi;
1434 substream->pstr = stream;
1435 list_add_tail(&substream->list, &stream->substreams);
1436 stream->substream_count++;
1442 * snd_rawmidi_new - create a rawmidi instance
1443 * @card: the card instance
1444 * @id: the id string
1445 * @device: the device index
1446 * @output_count: the number of output streams
1447 * @input_count: the number of input streams
1448 * @rrawmidi: the pointer to store the new rawmidi instance
1450 * Creates a new rawmidi instance.
1451 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1453 * Return: Zero if successful, or a negative error code on failure.
1455 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1456 int output_count, int input_count,
1457 struct snd_rawmidi ** rrawmidi)
1459 struct snd_rawmidi *rmidi;
1461 static struct snd_device_ops ops = {
1462 .dev_free = snd_rawmidi_dev_free,
1463 .dev_register = snd_rawmidi_dev_register,
1464 .dev_disconnect = snd_rawmidi_dev_disconnect,
1467 if (snd_BUG_ON(!card))
1471 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1472 if (rmidi == NULL) {
1473 dev_err(card->dev, "rawmidi: cannot allocate\n");
1477 rmidi->device = device;
1478 mutex_init(&rmidi->open_mutex);
1479 init_waitqueue_head(&rmidi->open_wait);
1480 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1481 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1484 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1485 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1486 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1487 SNDRV_RAWMIDI_STREAM_INPUT,
1488 input_count)) < 0) {
1489 snd_rawmidi_free(rmidi);
1492 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1493 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1494 SNDRV_RAWMIDI_STREAM_OUTPUT,
1495 output_count)) < 0) {
1496 snd_rawmidi_free(rmidi);
1499 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1500 snd_rawmidi_free(rmidi);
1508 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1510 struct snd_rawmidi_substream *substream;
1512 while (!list_empty(&stream->substreams)) {
1513 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1514 list_del(&substream->list);
1519 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1524 snd_info_free_entry(rmidi->proc_entry);
1525 rmidi->proc_entry = NULL;
1526 mutex_lock(®ister_mutex);
1527 if (rmidi->ops && rmidi->ops->dev_unregister)
1528 rmidi->ops->dev_unregister(rmidi);
1529 mutex_unlock(®ister_mutex);
1531 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1532 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1533 if (rmidi->private_free)
1534 rmidi->private_free(rmidi);
1539 static int snd_rawmidi_dev_free(struct snd_device *device)
1541 struct snd_rawmidi *rmidi = device->device_data;
1542 return snd_rawmidi_free(rmidi);
1545 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1546 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1548 struct snd_rawmidi *rmidi = device->private_data;
1549 rmidi->seq_dev = NULL;
1553 static int snd_rawmidi_dev_register(struct snd_device *device)
1556 struct snd_info_entry *entry;
1558 struct snd_rawmidi *rmidi = device->device_data;
1560 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1562 mutex_lock(®ister_mutex);
1563 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1564 mutex_unlock(®ister_mutex);
1567 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1568 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1569 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1570 rmidi->card, rmidi->device,
1571 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1572 rmidi_err(rmidi, "unable to register rawmidi device %i:%i\n",
1573 rmidi->card->number, rmidi->device);
1574 list_del(&rmidi->list);
1575 mutex_unlock(®ister_mutex);
1578 if (rmidi->ops && rmidi->ops->dev_register &&
1579 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1580 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1581 list_del(&rmidi->list);
1582 mutex_unlock(®ister_mutex);
1585 #ifdef CONFIG_SND_OSSEMUL
1587 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1588 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1589 rmidi->card, 0, &snd_rawmidi_f_ops,
1592 "unable to register OSS rawmidi device %i:%i\n",
1593 rmidi->card->number, 0);
1596 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1597 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1601 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1602 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1603 rmidi->card, 1, &snd_rawmidi_f_ops,
1606 "unable to register OSS rawmidi device %i:%i\n",
1607 rmidi->card->number, 1);
1612 #endif /* CONFIG_SND_OSSEMUL */
1613 mutex_unlock(®ister_mutex);
1614 sprintf(name, "midi%d", rmidi->device);
1615 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1617 entry->private_data = rmidi;
1618 entry->c.text.read = snd_rawmidi_proc_info_read;
1619 if (snd_info_register(entry) < 0) {
1620 snd_info_free_entry(entry);
1624 rmidi->proc_entry = entry;
1625 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1626 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1627 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1628 rmidi->seq_dev->private_data = rmidi;
1629 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1630 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1631 snd_device_register(rmidi->card, rmidi->seq_dev);
1638 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1640 struct snd_rawmidi *rmidi = device->device_data;
1643 mutex_lock(®ister_mutex);
1644 mutex_lock(&rmidi->open_mutex);
1645 wake_up(&rmidi->open_wait);
1646 list_del_init(&rmidi->list);
1647 for (dir = 0; dir < 2; dir++) {
1648 struct snd_rawmidi_substream *s;
1649 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1651 wake_up(&s->runtime->sleep);
1655 #ifdef CONFIG_SND_OSSEMUL
1656 if (rmidi->ossreg) {
1657 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1658 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1659 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1660 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1663 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1664 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1667 #endif /* CONFIG_SND_OSSEMUL */
1668 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1669 mutex_unlock(&rmidi->open_mutex);
1670 mutex_unlock(®ister_mutex);
1675 * snd_rawmidi_set_ops - set the rawmidi operators
1676 * @rmidi: the rawmidi instance
1677 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1678 * @ops: the operator table
1680 * Sets the rawmidi operators for the given stream direction.
1682 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1683 struct snd_rawmidi_ops *ops)
1685 struct snd_rawmidi_substream *substream;
1687 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1688 substream->ops = ops;
1695 static int __init alsa_rawmidi_init(void)
1698 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1699 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1700 #ifdef CONFIG_SND_OSSEMUL
1702 /* check device map table */
1703 for (i = 0; i < SNDRV_CARDS; i++) {
1704 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1705 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1709 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1710 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1716 #endif /* CONFIG_SND_OSSEMUL */
1720 static void __exit alsa_rawmidi_exit(void)
1722 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1723 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1726 module_init(alsa_rawmidi_init)
1727 module_exit(alsa_rawmidi_exit)
1729 EXPORT_SYMBOL(snd_rawmidi_output_params);
1730 EXPORT_SYMBOL(snd_rawmidi_input_params);
1731 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1732 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1733 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1734 EXPORT_SYMBOL(snd_rawmidi_receive);
1735 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1736 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1737 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1738 EXPORT_SYMBOL(snd_rawmidi_transmit);
1739 EXPORT_SYMBOL(snd_rawmidi_new);
1740 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1741 EXPORT_SYMBOL(snd_rawmidi_info_select);
1742 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1743 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1744 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1745 EXPORT_SYMBOL(snd_rawmidi_kernel_write);