Linux 2.6.31.8
[linux/fpc-iii.git] / sound / core / rawmidi.c
blob70d6f25ba526147d50148cd04fff2f9bf612d0dd
1 /*
2 * Abstract layer for MIDI v1.0 stream
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/core.h>
23 #include <linux/major.h>
24 #include <linux/init.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/wait.h>
29 #include <linux/mutex.h>
30 #include <linux/moduleparam.h>
31 #include <linux/delay.h>
32 #include <sound/rawmidi.h>
33 #include <sound/info.h>
34 #include <sound/control.h>
35 #include <sound/minors.h>
36 #include <sound/initval.h>
38 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
39 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
40 MODULE_LICENSE("GPL");
42 #ifdef CONFIG_SND_OSSEMUL
43 static int midi_map[SNDRV_CARDS];
44 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
45 module_param_array(midi_map, int, NULL, 0444);
46 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
47 module_param_array(amidi_map, int, NULL, 0444);
48 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
49 #endif /* CONFIG_SND_OSSEMUL */
51 static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
52 static int snd_rawmidi_dev_free(struct snd_device *device);
53 static int snd_rawmidi_dev_register(struct snd_device *device);
54 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
56 static LIST_HEAD(snd_rawmidi_devices);
57 static DEFINE_MUTEX(register_mutex);
59 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
61 struct snd_rawmidi *rawmidi;
63 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
64 if (rawmidi->card == card && rawmidi->device == device)
65 return rawmidi;
66 return NULL;
69 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
71 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
72 case FMODE_WRITE:
73 return SNDRV_RAWMIDI_LFLG_OUTPUT;
74 case FMODE_READ:
75 return SNDRV_RAWMIDI_LFLG_INPUT;
76 default:
77 return SNDRV_RAWMIDI_LFLG_OPEN;
81 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
83 struct snd_rawmidi_runtime *runtime = substream->runtime;
84 return runtime->avail >= runtime->avail_min;
87 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
88 size_t count)
90 struct snd_rawmidi_runtime *runtime = substream->runtime;
91 return runtime->avail >= runtime->avail_min &&
92 (!substream->append || runtime->avail >= count);
95 static void snd_rawmidi_input_event_tasklet(unsigned long data)
97 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
98 substream->runtime->event(substream);
101 static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
103 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
104 substream->ops->trigger(substream, 1);
107 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
109 struct snd_rawmidi_runtime *runtime;
111 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
112 return -ENOMEM;
113 spin_lock_init(&runtime->lock);
114 init_waitqueue_head(&runtime->sleep);
115 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
116 tasklet_init(&runtime->tasklet,
117 snd_rawmidi_input_event_tasklet,
118 (unsigned long)substream);
119 else
120 tasklet_init(&runtime->tasklet,
121 snd_rawmidi_output_trigger_tasklet,
122 (unsigned long)substream);
123 runtime->event = NULL;
124 runtime->buffer_size = PAGE_SIZE;
125 runtime->avail_min = 1;
126 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
127 runtime->avail = 0;
128 else
129 runtime->avail = runtime->buffer_size;
130 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
131 kfree(runtime);
132 return -ENOMEM;
134 runtime->appl_ptr = runtime->hw_ptr = 0;
135 substream->runtime = runtime;
136 return 0;
139 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
141 struct snd_rawmidi_runtime *runtime = substream->runtime;
143 kfree(runtime->buffer);
144 kfree(runtime);
145 substream->runtime = NULL;
146 return 0;
149 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
151 if (!substream->opened)
152 return;
153 if (up) {
154 tasklet_schedule(&substream->runtime->tasklet);
155 } else {
156 tasklet_kill(&substream->runtime->tasklet);
157 substream->ops->trigger(substream, 0);
161 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
163 if (!substream->opened)
164 return;
165 substream->ops->trigger(substream, up);
166 if (!up && substream->runtime->event)
167 tasklet_kill(&substream->runtime->tasklet);
170 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
172 unsigned long flags;
173 struct snd_rawmidi_runtime *runtime = substream->runtime;
175 snd_rawmidi_output_trigger(substream, 0);
176 runtime->drain = 0;
177 spin_lock_irqsave(&runtime->lock, flags);
178 runtime->appl_ptr = runtime->hw_ptr = 0;
179 runtime->avail = runtime->buffer_size;
180 spin_unlock_irqrestore(&runtime->lock, flags);
181 return 0;
184 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
186 int err;
187 long timeout;
188 struct snd_rawmidi_runtime *runtime = substream->runtime;
190 err = 0;
191 runtime->drain = 1;
192 timeout = wait_event_interruptible_timeout(runtime->sleep,
193 (runtime->avail >= runtime->buffer_size),
194 10*HZ);
195 if (signal_pending(current))
196 err = -ERESTARTSYS;
197 if (runtime->avail < runtime->buffer_size && !timeout) {
198 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
199 err = -EIO;
201 runtime->drain = 0;
202 if (err != -ERESTARTSYS) {
203 /* we need wait a while to make sure that Tx FIFOs are empty */
204 if (substream->ops->drain)
205 substream->ops->drain(substream);
206 else
207 msleep(50);
208 snd_rawmidi_drop_output(substream);
210 return err;
213 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
215 unsigned long flags;
216 struct snd_rawmidi_runtime *runtime = substream->runtime;
218 snd_rawmidi_input_trigger(substream, 0);
219 runtime->drain = 0;
220 spin_lock_irqsave(&runtime->lock, flags);
221 runtime->appl_ptr = runtime->hw_ptr = 0;
222 runtime->avail = 0;
223 spin_unlock_irqrestore(&runtime->lock, flags);
224 return 0;
227 /* look for an available substream for the given stream direction;
228 * if a specific subdevice is given, try to assign it
230 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
231 int stream, int mode,
232 struct snd_rawmidi_substream **sub_ret)
234 struct snd_rawmidi_substream *substream;
235 struct snd_rawmidi_str *s = &rmidi->streams[stream];
236 static unsigned int info_flags[2] = {
237 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
238 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
241 if (!(rmidi->info_flags & info_flags[stream]))
242 return -ENXIO;
243 if (subdevice >= 0 && subdevice >= s->substream_count)
244 return -ENODEV;
245 if (s->substream_opened >= s->substream_count)
246 return -EAGAIN;
248 list_for_each_entry(substream, &s->substreams, list) {
249 if (substream->opened) {
250 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
251 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
252 !substream->append)
253 continue;
255 if (subdevice < 0 || subdevice == substream->number) {
256 *sub_ret = substream;
257 return 0;
260 return -EAGAIN;
263 /* open and do ref-counting for the given substream */
264 static int open_substream(struct snd_rawmidi *rmidi,
265 struct snd_rawmidi_substream *substream,
266 int mode)
268 int err;
270 if (substream->use_count == 0) {
271 err = snd_rawmidi_runtime_create(substream);
272 if (err < 0)
273 return err;
274 err = substream->ops->open(substream);
275 if (err < 0) {
276 snd_rawmidi_runtime_free(substream);
277 return err;
279 substream->opened = 1;
280 substream->active_sensing = 0;
281 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
282 substream->append = 1;
284 substream->use_count++;
285 rmidi->streams[substream->stream].substream_opened++;
286 return 0;
289 static void close_substream(struct snd_rawmidi *rmidi,
290 struct snd_rawmidi_substream *substream,
291 int cleanup);
293 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
294 struct snd_rawmidi_file *rfile)
296 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
297 int err;
299 rfile->input = rfile->output = NULL;
300 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
301 err = assign_substream(rmidi, subdevice,
302 SNDRV_RAWMIDI_STREAM_INPUT,
303 mode, &sinput);
304 if (err < 0)
305 return err;
307 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
308 err = assign_substream(rmidi, subdevice,
309 SNDRV_RAWMIDI_STREAM_OUTPUT,
310 mode, &soutput);
311 if (err < 0)
312 return err;
315 if (sinput) {
316 err = open_substream(rmidi, sinput, mode);
317 if (err < 0)
318 return err;
320 if (soutput) {
321 err = open_substream(rmidi, soutput, mode);
322 if (err < 0) {
323 if (sinput)
324 close_substream(rmidi, sinput, 0);
325 return err;
329 rfile->rmidi = rmidi;
330 rfile->input = sinput;
331 rfile->output = soutput;
332 return 0;
335 /* called from sound/core/seq/seq_midi.c */
336 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
337 int mode, struct snd_rawmidi_file * rfile)
339 struct snd_rawmidi *rmidi;
340 int err;
342 if (snd_BUG_ON(!rfile))
343 return -EINVAL;
345 mutex_lock(&register_mutex);
346 rmidi = snd_rawmidi_search(card, device);
347 if (rmidi == NULL) {
348 mutex_unlock(&register_mutex);
349 return -ENODEV;
351 if (!try_module_get(rmidi->card->module)) {
352 mutex_unlock(&register_mutex);
353 return -ENXIO;
355 mutex_unlock(&register_mutex);
357 mutex_lock(&rmidi->open_mutex);
358 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
359 mutex_unlock(&rmidi->open_mutex);
360 if (err < 0)
361 module_put(rmidi->card->module);
362 return err;
365 static int snd_rawmidi_open(struct inode *inode, struct file *file)
367 int maj = imajor(inode);
368 struct snd_card *card;
369 int subdevice;
370 unsigned short fflags;
371 int err;
372 struct snd_rawmidi *rmidi;
373 struct snd_rawmidi_file *rawmidi_file = NULL;
374 wait_queue_t wait;
375 struct snd_ctl_file *kctl;
377 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
378 return -EINVAL; /* invalid combination */
380 if (maj == snd_major) {
381 rmidi = snd_lookup_minor_data(iminor(inode),
382 SNDRV_DEVICE_TYPE_RAWMIDI);
383 #ifdef CONFIG_SND_OSSEMUL
384 } else if (maj == SOUND_MAJOR) {
385 rmidi = snd_lookup_oss_minor_data(iminor(inode),
386 SNDRV_OSS_DEVICE_TYPE_MIDI);
387 #endif
388 } else
389 return -ENXIO;
391 if (rmidi == NULL)
392 return -ENODEV;
394 if (!try_module_get(rmidi->card->module))
395 return -ENXIO;
397 mutex_lock(&rmidi->open_mutex);
398 card = rmidi->card;
399 err = snd_card_file_add(card, file);
400 if (err < 0)
401 goto __error_card;
402 fflags = snd_rawmidi_file_flags(file);
403 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
404 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
405 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
406 if (rawmidi_file == NULL) {
407 err = -ENOMEM;
408 goto __error;
410 init_waitqueue_entry(&wait, current);
411 add_wait_queue(&rmidi->open_wait, &wait);
412 while (1) {
413 subdevice = -1;
414 read_lock(&card->ctl_files_rwlock);
415 list_for_each_entry(kctl, &card->ctl_files, list) {
416 if (kctl->pid == current->pid) {
417 subdevice = kctl->prefer_rawmidi_subdevice;
418 if (subdevice != -1)
419 break;
422 read_unlock(&card->ctl_files_rwlock);
423 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
424 if (err >= 0)
425 break;
426 if (err == -EAGAIN) {
427 if (file->f_flags & O_NONBLOCK) {
428 err = -EBUSY;
429 break;
431 } else
432 break;
433 set_current_state(TASK_INTERRUPTIBLE);
434 mutex_unlock(&rmidi->open_mutex);
435 schedule();
436 mutex_lock(&rmidi->open_mutex);
437 if (signal_pending(current)) {
438 err = -ERESTARTSYS;
439 break;
442 remove_wait_queue(&rmidi->open_wait, &wait);
443 if (err < 0) {
444 kfree(rawmidi_file);
445 goto __error;
447 #ifdef CONFIG_SND_OSSEMUL
448 if (rawmidi_file->input && rawmidi_file->input->runtime)
449 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
450 if (rawmidi_file->output && rawmidi_file->output->runtime)
451 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
452 #endif
453 file->private_data = rawmidi_file;
454 mutex_unlock(&rmidi->open_mutex);
455 return 0;
457 __error:
458 snd_card_file_remove(card, file);
459 __error_card:
460 mutex_unlock(&rmidi->open_mutex);
461 module_put(rmidi->card->module);
462 return err;
465 static void close_substream(struct snd_rawmidi *rmidi,
466 struct snd_rawmidi_substream *substream,
467 int cleanup)
469 rmidi->streams[substream->stream].substream_opened--;
470 if (--substream->use_count)
471 return;
473 if (cleanup) {
474 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
475 snd_rawmidi_input_trigger(substream, 0);
476 else {
477 if (substream->active_sensing) {
478 unsigned char buf = 0xfe;
479 /* sending single active sensing message
480 * to shut the device up
482 snd_rawmidi_kernel_write(substream, &buf, 1);
484 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
485 snd_rawmidi_output_trigger(substream, 0);
488 substream->ops->close(substream);
489 if (substream->runtime->private_free)
490 substream->runtime->private_free(substream);
491 snd_rawmidi_runtime_free(substream);
492 substream->opened = 0;
493 substream->append = 0;
496 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
498 struct snd_rawmidi *rmidi;
500 rmidi = rfile->rmidi;
501 mutex_lock(&rmidi->open_mutex);
502 if (rfile->input) {
503 close_substream(rmidi, rfile->input, 1);
504 rfile->input = NULL;
506 if (rfile->output) {
507 close_substream(rmidi, rfile->output, 1);
508 rfile->output = NULL;
510 rfile->rmidi = NULL;
511 mutex_unlock(&rmidi->open_mutex);
512 wake_up(&rmidi->open_wait);
515 /* called from sound/core/seq/seq_midi.c */
516 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
518 struct snd_rawmidi *rmidi;
520 if (snd_BUG_ON(!rfile))
521 return -ENXIO;
523 rmidi = rfile->rmidi;
524 rawmidi_release_priv(rfile);
525 module_put(rmidi->card->module);
526 return 0;
529 static int snd_rawmidi_release(struct inode *inode, struct file *file)
531 struct snd_rawmidi_file *rfile;
532 struct snd_rawmidi *rmidi;
534 rfile = file->private_data;
535 rmidi = rfile->rmidi;
536 rawmidi_release_priv(rfile);
537 kfree(rfile);
538 snd_card_file_remove(rmidi->card, file);
539 module_put(rmidi->card->module);
540 return 0;
543 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
544 struct snd_rawmidi_info *info)
546 struct snd_rawmidi *rmidi;
548 if (substream == NULL)
549 return -ENODEV;
550 rmidi = substream->rmidi;
551 memset(info, 0, sizeof(*info));
552 info->card = rmidi->card->number;
553 info->device = rmidi->device;
554 info->subdevice = substream->number;
555 info->stream = substream->stream;
556 info->flags = rmidi->info_flags;
557 strcpy(info->id, rmidi->id);
558 strcpy(info->name, rmidi->name);
559 strcpy(info->subname, substream->name);
560 info->subdevices_count = substream->pstr->substream_count;
561 info->subdevices_avail = (substream->pstr->substream_count -
562 substream->pstr->substream_opened);
563 return 0;
566 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
567 struct snd_rawmidi_info __user * _info)
569 struct snd_rawmidi_info info;
570 int err;
571 if ((err = snd_rawmidi_info(substream, &info)) < 0)
572 return err;
573 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
574 return -EFAULT;
575 return 0;
578 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
580 struct snd_rawmidi *rmidi;
581 struct snd_rawmidi_str *pstr;
582 struct snd_rawmidi_substream *substream;
584 mutex_lock(&register_mutex);
585 rmidi = snd_rawmidi_search(card, info->device);
586 mutex_unlock(&register_mutex);
587 if (!rmidi)
588 return -ENXIO;
589 if (info->stream < 0 || info->stream > 1)
590 return -EINVAL;
591 pstr = &rmidi->streams[info->stream];
592 if (pstr->substream_count == 0)
593 return -ENOENT;
594 if (info->subdevice >= pstr->substream_count)
595 return -ENXIO;
596 list_for_each_entry(substream, &pstr->substreams, list) {
597 if ((unsigned int)substream->number == info->subdevice)
598 return snd_rawmidi_info(substream, info);
600 return -ENXIO;
603 static int snd_rawmidi_info_select_user(struct snd_card *card,
604 struct snd_rawmidi_info __user *_info)
606 int err;
607 struct snd_rawmidi_info info;
608 if (get_user(info.device, &_info->device))
609 return -EFAULT;
610 if (get_user(info.stream, &_info->stream))
611 return -EFAULT;
612 if (get_user(info.subdevice, &_info->subdevice))
613 return -EFAULT;
614 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
615 return err;
616 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
617 return -EFAULT;
618 return 0;
621 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
622 struct snd_rawmidi_params * params)
624 char *newbuf;
625 struct snd_rawmidi_runtime *runtime = substream->runtime;
627 if (substream->append && substream->use_count > 1)
628 return -EBUSY;
629 snd_rawmidi_drain_output(substream);
630 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
631 return -EINVAL;
633 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
634 return -EINVAL;
636 if (params->buffer_size != runtime->buffer_size) {
637 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
638 if (!newbuf)
639 return -ENOMEM;
640 kfree(runtime->buffer);
641 runtime->buffer = newbuf;
642 runtime->buffer_size = params->buffer_size;
643 runtime->avail = runtime->buffer_size;
645 runtime->avail_min = params->avail_min;
646 substream->active_sensing = !params->no_active_sensing;
647 return 0;
650 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
651 struct snd_rawmidi_params * params)
653 char *newbuf;
654 struct snd_rawmidi_runtime *runtime = substream->runtime;
656 snd_rawmidi_drain_input(substream);
657 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
658 return -EINVAL;
660 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
661 return -EINVAL;
663 if (params->buffer_size != runtime->buffer_size) {
664 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
665 if (!newbuf)
666 return -ENOMEM;
667 kfree(runtime->buffer);
668 runtime->buffer = newbuf;
669 runtime->buffer_size = params->buffer_size;
671 runtime->avail_min = params->avail_min;
672 return 0;
675 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
676 struct snd_rawmidi_status * status)
678 struct snd_rawmidi_runtime *runtime = substream->runtime;
680 memset(status, 0, sizeof(*status));
681 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
682 spin_lock_irq(&runtime->lock);
683 status->avail = runtime->avail;
684 spin_unlock_irq(&runtime->lock);
685 return 0;
688 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
689 struct snd_rawmidi_status * status)
691 struct snd_rawmidi_runtime *runtime = substream->runtime;
693 memset(status, 0, sizeof(*status));
694 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
695 spin_lock_irq(&runtime->lock);
696 status->avail = runtime->avail;
697 status->xruns = runtime->xruns;
698 runtime->xruns = 0;
699 spin_unlock_irq(&runtime->lock);
700 return 0;
703 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
705 struct snd_rawmidi_file *rfile;
706 void __user *argp = (void __user *)arg;
708 rfile = file->private_data;
709 if (((cmd >> 8) & 0xff) != 'W')
710 return -ENOTTY;
711 switch (cmd) {
712 case SNDRV_RAWMIDI_IOCTL_PVERSION:
713 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
714 case SNDRV_RAWMIDI_IOCTL_INFO:
716 int stream;
717 struct snd_rawmidi_info __user *info = argp;
718 if (get_user(stream, &info->stream))
719 return -EFAULT;
720 switch (stream) {
721 case SNDRV_RAWMIDI_STREAM_INPUT:
722 return snd_rawmidi_info_user(rfile->input, info);
723 case SNDRV_RAWMIDI_STREAM_OUTPUT:
724 return snd_rawmidi_info_user(rfile->output, info);
725 default:
726 return -EINVAL;
729 case SNDRV_RAWMIDI_IOCTL_PARAMS:
731 struct snd_rawmidi_params params;
732 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
733 return -EFAULT;
734 switch (params.stream) {
735 case SNDRV_RAWMIDI_STREAM_OUTPUT:
736 if (rfile->output == NULL)
737 return -EINVAL;
738 return snd_rawmidi_output_params(rfile->output, &params);
739 case SNDRV_RAWMIDI_STREAM_INPUT:
740 if (rfile->input == NULL)
741 return -EINVAL;
742 return snd_rawmidi_input_params(rfile->input, &params);
743 default:
744 return -EINVAL;
747 case SNDRV_RAWMIDI_IOCTL_STATUS:
749 int err = 0;
750 struct snd_rawmidi_status status;
751 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
752 return -EFAULT;
753 switch (status.stream) {
754 case SNDRV_RAWMIDI_STREAM_OUTPUT:
755 if (rfile->output == NULL)
756 return -EINVAL;
757 err = snd_rawmidi_output_status(rfile->output, &status);
758 break;
759 case SNDRV_RAWMIDI_STREAM_INPUT:
760 if (rfile->input == NULL)
761 return -EINVAL;
762 err = snd_rawmidi_input_status(rfile->input, &status);
763 break;
764 default:
765 return -EINVAL;
767 if (err < 0)
768 return err;
769 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
770 return -EFAULT;
771 return 0;
773 case SNDRV_RAWMIDI_IOCTL_DROP:
775 int val;
776 if (get_user(val, (int __user *) argp))
777 return -EFAULT;
778 switch (val) {
779 case SNDRV_RAWMIDI_STREAM_OUTPUT:
780 if (rfile->output == NULL)
781 return -EINVAL;
782 return snd_rawmidi_drop_output(rfile->output);
783 default:
784 return -EINVAL;
787 case SNDRV_RAWMIDI_IOCTL_DRAIN:
789 int val;
790 if (get_user(val, (int __user *) argp))
791 return -EFAULT;
792 switch (val) {
793 case SNDRV_RAWMIDI_STREAM_OUTPUT:
794 if (rfile->output == NULL)
795 return -EINVAL;
796 return snd_rawmidi_drain_output(rfile->output);
797 case SNDRV_RAWMIDI_STREAM_INPUT:
798 if (rfile->input == NULL)
799 return -EINVAL;
800 return snd_rawmidi_drain_input(rfile->input);
801 default:
802 return -EINVAL;
805 #ifdef CONFIG_SND_DEBUG
806 default:
807 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
808 #endif
810 return -ENOTTY;
813 static int snd_rawmidi_control_ioctl(struct snd_card *card,
814 struct snd_ctl_file *control,
815 unsigned int cmd,
816 unsigned long arg)
818 void __user *argp = (void __user *)arg;
820 switch (cmd) {
821 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
823 int device;
825 if (get_user(device, (int __user *)argp))
826 return -EFAULT;
827 mutex_lock(&register_mutex);
828 device = device < 0 ? 0 : device + 1;
829 while (device < SNDRV_RAWMIDI_DEVICES) {
830 if (snd_rawmidi_search(card, device))
831 break;
832 device++;
834 if (device == SNDRV_RAWMIDI_DEVICES)
835 device = -1;
836 mutex_unlock(&register_mutex);
837 if (put_user(device, (int __user *)argp))
838 return -EFAULT;
839 return 0;
841 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
843 int val;
845 if (get_user(val, (int __user *)argp))
846 return -EFAULT;
847 control->prefer_rawmidi_subdevice = val;
848 return 0;
850 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
851 return snd_rawmidi_info_select_user(card, argp);
853 return -ENOIOCTLCMD;
857 * snd_rawmidi_receive - receive the input data from the device
858 * @substream: the rawmidi substream
859 * @buffer: the buffer pointer
860 * @count: the data size to read
862 * Reads the data from the internal buffer.
864 * Returns the size of read data, or a negative error code on failure.
866 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
867 const unsigned char *buffer, int count)
869 unsigned long flags;
870 int result = 0, count1;
871 struct snd_rawmidi_runtime *runtime = substream->runtime;
873 if (!substream->opened)
874 return -EBADFD;
875 if (runtime->buffer == NULL) {
876 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
877 return -EINVAL;
879 spin_lock_irqsave(&runtime->lock, flags);
880 if (count == 1) { /* special case, faster code */
881 substream->bytes++;
882 if (runtime->avail < runtime->buffer_size) {
883 runtime->buffer[runtime->hw_ptr++] = buffer[0];
884 runtime->hw_ptr %= runtime->buffer_size;
885 runtime->avail++;
886 result++;
887 } else {
888 runtime->xruns++;
890 } else {
891 substream->bytes += count;
892 count1 = runtime->buffer_size - runtime->hw_ptr;
893 if (count1 > count)
894 count1 = count;
895 if (count1 > (int)(runtime->buffer_size - runtime->avail))
896 count1 = runtime->buffer_size - runtime->avail;
897 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
898 runtime->hw_ptr += count1;
899 runtime->hw_ptr %= runtime->buffer_size;
900 runtime->avail += count1;
901 count -= count1;
902 result += count1;
903 if (count > 0) {
904 buffer += count1;
905 count1 = count;
906 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
907 count1 = runtime->buffer_size - runtime->avail;
908 runtime->xruns += count - count1;
910 if (count1 > 0) {
911 memcpy(runtime->buffer, buffer, count1);
912 runtime->hw_ptr = count1;
913 runtime->avail += count1;
914 result += count1;
918 if (result > 0) {
919 if (runtime->event)
920 tasklet_schedule(&runtime->tasklet);
921 else if (snd_rawmidi_ready(substream))
922 wake_up(&runtime->sleep);
924 spin_unlock_irqrestore(&runtime->lock, flags);
925 return result;
928 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
929 unsigned char __user *userbuf,
930 unsigned char *kernelbuf, long count)
932 unsigned long flags;
933 long result = 0, count1;
934 struct snd_rawmidi_runtime *runtime = substream->runtime;
936 while (count > 0 && runtime->avail) {
937 count1 = runtime->buffer_size - runtime->appl_ptr;
938 if (count1 > count)
939 count1 = count;
940 spin_lock_irqsave(&runtime->lock, flags);
941 if (count1 > (int)runtime->avail)
942 count1 = runtime->avail;
943 if (kernelbuf)
944 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
945 if (userbuf) {
946 spin_unlock_irqrestore(&runtime->lock, flags);
947 if (copy_to_user(userbuf + result,
948 runtime->buffer + runtime->appl_ptr, count1)) {
949 return result > 0 ? result : -EFAULT;
951 spin_lock_irqsave(&runtime->lock, flags);
953 runtime->appl_ptr += count1;
954 runtime->appl_ptr %= runtime->buffer_size;
955 runtime->avail -= count1;
956 spin_unlock_irqrestore(&runtime->lock, flags);
957 result += count1;
958 count -= count1;
960 return result;
963 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
964 unsigned char *buf, long count)
966 snd_rawmidi_input_trigger(substream, 1);
967 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
970 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
971 loff_t *offset)
973 long result;
974 int count1;
975 struct snd_rawmidi_file *rfile;
976 struct snd_rawmidi_substream *substream;
977 struct snd_rawmidi_runtime *runtime;
979 rfile = file->private_data;
980 substream = rfile->input;
981 if (substream == NULL)
982 return -EIO;
983 runtime = substream->runtime;
984 snd_rawmidi_input_trigger(substream, 1);
985 result = 0;
986 while (count > 0) {
987 spin_lock_irq(&runtime->lock);
988 while (!snd_rawmidi_ready(substream)) {
989 wait_queue_t wait;
990 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
991 spin_unlock_irq(&runtime->lock);
992 return result > 0 ? result : -EAGAIN;
994 init_waitqueue_entry(&wait, current);
995 add_wait_queue(&runtime->sleep, &wait);
996 set_current_state(TASK_INTERRUPTIBLE);
997 spin_unlock_irq(&runtime->lock);
998 schedule();
999 remove_wait_queue(&runtime->sleep, &wait);
1000 if (signal_pending(current))
1001 return result > 0 ? result : -ERESTARTSYS;
1002 if (!runtime->avail)
1003 return result > 0 ? result : -EIO;
1004 spin_lock_irq(&runtime->lock);
1006 spin_unlock_irq(&runtime->lock);
1007 count1 = snd_rawmidi_kernel_read1(substream,
1008 (unsigned char __user *)buf,
1009 NULL/*kernelbuf*/,
1010 count);
1011 if (count1 < 0)
1012 return result > 0 ? result : count1;
1013 result += count1;
1014 buf += count1;
1015 count -= count1;
1017 return result;
1021 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1022 * @substream: the rawmidi substream
1024 * Returns 1 if the internal output buffer is empty, 0 if not.
1026 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1028 struct snd_rawmidi_runtime *runtime = substream->runtime;
1029 int result;
1030 unsigned long flags;
1032 if (runtime->buffer == NULL) {
1033 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1034 return 1;
1036 spin_lock_irqsave(&runtime->lock, flags);
1037 result = runtime->avail >= runtime->buffer_size;
1038 spin_unlock_irqrestore(&runtime->lock, flags);
1039 return result;
1043 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1044 * @substream: the rawmidi substream
1045 * @buffer: the buffer pointer
1046 * @count: data size to transfer
1048 * Copies data from the internal output buffer to the given buffer.
1050 * Call this in the interrupt handler when the midi output is ready,
1051 * and call snd_rawmidi_transmit_ack() after the transmission is
1052 * finished.
1054 * Returns the size of copied data, or a negative error code on failure.
1056 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1057 unsigned char *buffer, int count)
1059 unsigned long flags;
1060 int result, count1;
1061 struct snd_rawmidi_runtime *runtime = substream->runtime;
1063 if (runtime->buffer == NULL) {
1064 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1065 return -EINVAL;
1067 result = 0;
1068 spin_lock_irqsave(&runtime->lock, flags);
1069 if (runtime->avail >= runtime->buffer_size) {
1070 /* warning: lowlevel layer MUST trigger down the hardware */
1071 goto __skip;
1073 if (count == 1) { /* special case, faster code */
1074 *buffer = runtime->buffer[runtime->hw_ptr];
1075 result++;
1076 } else {
1077 count1 = runtime->buffer_size - runtime->hw_ptr;
1078 if (count1 > count)
1079 count1 = count;
1080 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1081 count1 = runtime->buffer_size - runtime->avail;
1082 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1083 count -= count1;
1084 result += count1;
1085 if (count > 0) {
1086 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1087 count = runtime->buffer_size - runtime->avail - count1;
1088 memcpy(buffer + count1, runtime->buffer, count);
1089 result += count;
1092 __skip:
1093 spin_unlock_irqrestore(&runtime->lock, flags);
1094 return result;
1098 * snd_rawmidi_transmit_ack - acknowledge the transmission
1099 * @substream: the rawmidi substream
1100 * @count: the tranferred count
1102 * Advances the hardware pointer for the internal output buffer with
1103 * the given size and updates the condition.
1104 * Call after the transmission is finished.
1106 * Returns the advanced size if successful, or a negative error code on failure.
1108 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1110 unsigned long flags;
1111 struct snd_rawmidi_runtime *runtime = substream->runtime;
1113 if (runtime->buffer == NULL) {
1114 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1115 return -EINVAL;
1117 spin_lock_irqsave(&runtime->lock, flags);
1118 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1119 runtime->hw_ptr += count;
1120 runtime->hw_ptr %= runtime->buffer_size;
1121 runtime->avail += count;
1122 substream->bytes += count;
1123 if (count > 0) {
1124 if (runtime->drain || snd_rawmidi_ready(substream))
1125 wake_up(&runtime->sleep);
1127 spin_unlock_irqrestore(&runtime->lock, flags);
1128 return count;
1132 * snd_rawmidi_transmit - copy from the buffer to the device
1133 * @substream: the rawmidi substream
1134 * @buffer: the buffer pointer
1135 * @count: the data size to transfer
1137 * Copies data from the buffer to the device and advances the pointer.
1139 * Returns the copied size if successful, or a negative error code on failure.
1141 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1142 unsigned char *buffer, int count)
1144 if (!substream->opened)
1145 return -EBADFD;
1146 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1147 if (count < 0)
1148 return count;
1149 return snd_rawmidi_transmit_ack(substream, count);
1152 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1153 const unsigned char __user *userbuf,
1154 const unsigned char *kernelbuf,
1155 long count)
1157 unsigned long flags;
1158 long count1, result;
1159 struct snd_rawmidi_runtime *runtime = substream->runtime;
1161 if (snd_BUG_ON(!kernelbuf && !userbuf))
1162 return -EINVAL;
1163 if (snd_BUG_ON(!runtime->buffer))
1164 return -EINVAL;
1166 result = 0;
1167 spin_lock_irqsave(&runtime->lock, flags);
1168 if (substream->append) {
1169 if ((long)runtime->avail < count) {
1170 spin_unlock_irqrestore(&runtime->lock, flags);
1171 return -EAGAIN;
1174 while (count > 0 && runtime->avail > 0) {
1175 count1 = runtime->buffer_size - runtime->appl_ptr;
1176 if (count1 > count)
1177 count1 = count;
1178 if (count1 > (long)runtime->avail)
1179 count1 = runtime->avail;
1180 if (kernelbuf)
1181 memcpy(runtime->buffer + runtime->appl_ptr,
1182 kernelbuf + result, count1);
1183 else if (userbuf) {
1184 spin_unlock_irqrestore(&runtime->lock, flags);
1185 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1186 userbuf + result, count1)) {
1187 spin_lock_irqsave(&runtime->lock, flags);
1188 result = result > 0 ? result : -EFAULT;
1189 goto __end;
1191 spin_lock_irqsave(&runtime->lock, flags);
1193 runtime->appl_ptr += count1;
1194 runtime->appl_ptr %= runtime->buffer_size;
1195 runtime->avail -= count1;
1196 result += count1;
1197 count -= count1;
1199 __end:
1200 count1 = runtime->avail < runtime->buffer_size;
1201 spin_unlock_irqrestore(&runtime->lock, flags);
1202 if (count1)
1203 snd_rawmidi_output_trigger(substream, 1);
1204 return result;
1207 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1208 const unsigned char *buf, long count)
1210 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1213 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1214 size_t count, loff_t *offset)
1216 long result, timeout;
1217 int count1;
1218 struct snd_rawmidi_file *rfile;
1219 struct snd_rawmidi_runtime *runtime;
1220 struct snd_rawmidi_substream *substream;
1222 rfile = file->private_data;
1223 substream = rfile->output;
1224 runtime = substream->runtime;
1225 /* we cannot put an atomic message to our buffer */
1226 if (substream->append && count > runtime->buffer_size)
1227 return -EIO;
1228 result = 0;
1229 while (count > 0) {
1230 spin_lock_irq(&runtime->lock);
1231 while (!snd_rawmidi_ready_append(substream, count)) {
1232 wait_queue_t wait;
1233 if (file->f_flags & O_NONBLOCK) {
1234 spin_unlock_irq(&runtime->lock);
1235 return result > 0 ? result : -EAGAIN;
1237 init_waitqueue_entry(&wait, current);
1238 add_wait_queue(&runtime->sleep, &wait);
1239 set_current_state(TASK_INTERRUPTIBLE);
1240 spin_unlock_irq(&runtime->lock);
1241 timeout = schedule_timeout(30 * HZ);
1242 remove_wait_queue(&runtime->sleep, &wait);
1243 if (signal_pending(current))
1244 return result > 0 ? result : -ERESTARTSYS;
1245 if (!runtime->avail && !timeout)
1246 return result > 0 ? result : -EIO;
1247 spin_lock_irq(&runtime->lock);
1249 spin_unlock_irq(&runtime->lock);
1250 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1251 if (count1 < 0)
1252 return result > 0 ? result : count1;
1253 result += count1;
1254 buf += count1;
1255 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1256 break;
1257 count -= count1;
1259 if (file->f_flags & O_SYNC) {
1260 spin_lock_irq(&runtime->lock);
1261 while (runtime->avail != runtime->buffer_size) {
1262 wait_queue_t wait;
1263 unsigned int last_avail = runtime->avail;
1264 init_waitqueue_entry(&wait, current);
1265 add_wait_queue(&runtime->sleep, &wait);
1266 set_current_state(TASK_INTERRUPTIBLE);
1267 spin_unlock_irq(&runtime->lock);
1268 timeout = schedule_timeout(30 * HZ);
1269 remove_wait_queue(&runtime->sleep, &wait);
1270 if (signal_pending(current))
1271 return result > 0 ? result : -ERESTARTSYS;
1272 if (runtime->avail == last_avail && !timeout)
1273 return result > 0 ? result : -EIO;
1274 spin_lock_irq(&runtime->lock);
1276 spin_unlock_irq(&runtime->lock);
1278 return result;
1281 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1283 struct snd_rawmidi_file *rfile;
1284 struct snd_rawmidi_runtime *runtime;
1285 unsigned int mask;
1287 rfile = file->private_data;
1288 if (rfile->input != NULL) {
1289 runtime = rfile->input->runtime;
1290 snd_rawmidi_input_trigger(rfile->input, 1);
1291 poll_wait(file, &runtime->sleep, wait);
1293 if (rfile->output != NULL) {
1294 runtime = rfile->output->runtime;
1295 poll_wait(file, &runtime->sleep, wait);
1297 mask = 0;
1298 if (rfile->input != NULL) {
1299 if (snd_rawmidi_ready(rfile->input))
1300 mask |= POLLIN | POLLRDNORM;
1302 if (rfile->output != NULL) {
1303 if (snd_rawmidi_ready(rfile->output))
1304 mask |= POLLOUT | POLLWRNORM;
1306 return mask;
1311 #ifdef CONFIG_COMPAT
1312 #include "rawmidi_compat.c"
1313 #else
1314 #define snd_rawmidi_ioctl_compat NULL
1315 #endif
1321 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1322 struct snd_info_buffer *buffer)
1324 struct snd_rawmidi *rmidi;
1325 struct snd_rawmidi_substream *substream;
1326 struct snd_rawmidi_runtime *runtime;
1328 rmidi = entry->private_data;
1329 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1330 mutex_lock(&rmidi->open_mutex);
1331 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1332 list_for_each_entry(substream,
1333 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1334 list) {
1335 snd_iprintf(buffer,
1336 "Output %d\n"
1337 " Tx bytes : %lu\n",
1338 substream->number,
1339 (unsigned long) substream->bytes);
1340 if (substream->opened) {
1341 runtime = substream->runtime;
1342 snd_iprintf(buffer,
1343 " Mode : %s\n"
1344 " Buffer size : %lu\n"
1345 " Avail : %lu\n",
1346 runtime->oss ? "OSS compatible" : "native",
1347 (unsigned long) runtime->buffer_size,
1348 (unsigned long) runtime->avail);
1352 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1353 list_for_each_entry(substream,
1354 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1355 list) {
1356 snd_iprintf(buffer,
1357 "Input %d\n"
1358 " Rx bytes : %lu\n",
1359 substream->number,
1360 (unsigned long) substream->bytes);
1361 if (substream->opened) {
1362 runtime = substream->runtime;
1363 snd_iprintf(buffer,
1364 " Buffer size : %lu\n"
1365 " Avail : %lu\n"
1366 " Overruns : %lu\n",
1367 (unsigned long) runtime->buffer_size,
1368 (unsigned long) runtime->avail,
1369 (unsigned long) runtime->xruns);
1373 mutex_unlock(&rmidi->open_mutex);
1377 * Register functions
1380 static const struct file_operations snd_rawmidi_f_ops =
1382 .owner = THIS_MODULE,
1383 .read = snd_rawmidi_read,
1384 .write = snd_rawmidi_write,
1385 .open = snd_rawmidi_open,
1386 .release = snd_rawmidi_release,
1387 .poll = snd_rawmidi_poll,
1388 .unlocked_ioctl = snd_rawmidi_ioctl,
1389 .compat_ioctl = snd_rawmidi_ioctl_compat,
1392 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1393 struct snd_rawmidi_str *stream,
1394 int direction,
1395 int count)
1397 struct snd_rawmidi_substream *substream;
1398 int idx;
1400 for (idx = 0; idx < count; idx++) {
1401 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1402 if (substream == NULL) {
1403 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1404 return -ENOMEM;
1406 substream->stream = direction;
1407 substream->number = idx;
1408 substream->rmidi = rmidi;
1409 substream->pstr = stream;
1410 list_add_tail(&substream->list, &stream->substreams);
1411 stream->substream_count++;
1413 return 0;
1417 * snd_rawmidi_new - create a rawmidi instance
1418 * @card: the card instance
1419 * @id: the id string
1420 * @device: the device index
1421 * @output_count: the number of output streams
1422 * @input_count: the number of input streams
1423 * @rrawmidi: the pointer to store the new rawmidi instance
1425 * Creates a new rawmidi instance.
1426 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1428 * Returns zero if successful, or a negative error code on failure.
1430 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1431 int output_count, int input_count,
1432 struct snd_rawmidi ** rrawmidi)
1434 struct snd_rawmidi *rmidi;
1435 int err;
1436 static struct snd_device_ops ops = {
1437 .dev_free = snd_rawmidi_dev_free,
1438 .dev_register = snd_rawmidi_dev_register,
1439 .dev_disconnect = snd_rawmidi_dev_disconnect,
1442 if (snd_BUG_ON(!card))
1443 return -ENXIO;
1444 if (rrawmidi)
1445 *rrawmidi = NULL;
1446 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1447 if (rmidi == NULL) {
1448 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1449 return -ENOMEM;
1451 rmidi->card = card;
1452 rmidi->device = device;
1453 mutex_init(&rmidi->open_mutex);
1454 init_waitqueue_head(&rmidi->open_wait);
1455 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1456 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1458 if (id != NULL)
1459 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1460 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1461 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1462 SNDRV_RAWMIDI_STREAM_INPUT,
1463 input_count)) < 0) {
1464 snd_rawmidi_free(rmidi);
1465 return err;
1467 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1468 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1469 SNDRV_RAWMIDI_STREAM_OUTPUT,
1470 output_count)) < 0) {
1471 snd_rawmidi_free(rmidi);
1472 return err;
1474 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1475 snd_rawmidi_free(rmidi);
1476 return err;
1478 if (rrawmidi)
1479 *rrawmidi = rmidi;
1480 return 0;
1483 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1485 struct snd_rawmidi_substream *substream;
1487 while (!list_empty(&stream->substreams)) {
1488 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1489 list_del(&substream->list);
1490 kfree(substream);
1494 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1496 if (!rmidi)
1497 return 0;
1499 snd_info_free_entry(rmidi->proc_entry);
1500 rmidi->proc_entry = NULL;
1501 mutex_lock(&register_mutex);
1502 if (rmidi->ops && rmidi->ops->dev_unregister)
1503 rmidi->ops->dev_unregister(rmidi);
1504 mutex_unlock(&register_mutex);
1506 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1507 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1508 if (rmidi->private_free)
1509 rmidi->private_free(rmidi);
1510 kfree(rmidi);
1511 return 0;
1514 static int snd_rawmidi_dev_free(struct snd_device *device)
1516 struct snd_rawmidi *rmidi = device->device_data;
1517 return snd_rawmidi_free(rmidi);
1520 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1521 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1523 struct snd_rawmidi *rmidi = device->private_data;
1524 rmidi->seq_dev = NULL;
1526 #endif
1528 static int snd_rawmidi_dev_register(struct snd_device *device)
1530 int err;
1531 struct snd_info_entry *entry;
1532 char name[16];
1533 struct snd_rawmidi *rmidi = device->device_data;
1535 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1536 return -ENOMEM;
1537 mutex_lock(&register_mutex);
1538 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1539 mutex_unlock(&register_mutex);
1540 return -EBUSY;
1542 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1543 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1544 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1545 rmidi->card, rmidi->device,
1546 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1547 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1548 list_del(&rmidi->list);
1549 mutex_unlock(&register_mutex);
1550 return err;
1552 if (rmidi->ops && rmidi->ops->dev_register &&
1553 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1554 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1555 list_del(&rmidi->list);
1556 mutex_unlock(&register_mutex);
1557 return err;
1559 #ifdef CONFIG_SND_OSSEMUL
1560 rmidi->ossreg = 0;
1561 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1562 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1563 rmidi->card, 0, &snd_rawmidi_f_ops,
1564 rmidi, name) < 0) {
1565 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1566 } else {
1567 rmidi->ossreg++;
1568 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1569 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1570 #endif
1573 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1574 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1575 rmidi->card, 1, &snd_rawmidi_f_ops,
1576 rmidi, name) < 0) {
1577 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1578 } else {
1579 rmidi->ossreg++;
1582 #endif /* CONFIG_SND_OSSEMUL */
1583 mutex_unlock(&register_mutex);
1584 sprintf(name, "midi%d", rmidi->device);
1585 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1586 if (entry) {
1587 entry->private_data = rmidi;
1588 entry->c.text.read = snd_rawmidi_proc_info_read;
1589 if (snd_info_register(entry) < 0) {
1590 snd_info_free_entry(entry);
1591 entry = NULL;
1594 rmidi->proc_entry = entry;
1595 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1596 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1597 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1598 rmidi->seq_dev->private_data = rmidi;
1599 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1600 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1601 snd_device_register(rmidi->card, rmidi->seq_dev);
1604 #endif
1605 return 0;
1608 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1610 struct snd_rawmidi *rmidi = device->device_data;
1612 mutex_lock(&register_mutex);
1613 list_del_init(&rmidi->list);
1614 #ifdef CONFIG_SND_OSSEMUL
1615 if (rmidi->ossreg) {
1616 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1617 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1618 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1619 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1620 #endif
1622 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1623 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1624 rmidi->ossreg = 0;
1626 #endif /* CONFIG_SND_OSSEMUL */
1627 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1628 mutex_unlock(&register_mutex);
1629 return 0;
1633 * snd_rawmidi_set_ops - set the rawmidi operators
1634 * @rmidi: the rawmidi instance
1635 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1636 * @ops: the operator table
1638 * Sets the rawmidi operators for the given stream direction.
1640 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1641 struct snd_rawmidi_ops *ops)
1643 struct snd_rawmidi_substream *substream;
1645 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1646 substream->ops = ops;
1650 * ENTRY functions
1653 static int __init alsa_rawmidi_init(void)
1656 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1657 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1658 #ifdef CONFIG_SND_OSSEMUL
1659 { int i;
1660 /* check device map table */
1661 for (i = 0; i < SNDRV_CARDS; i++) {
1662 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1663 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1664 midi_map[i] = 0;
1666 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1667 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1668 amidi_map[i] = 1;
1672 #endif /* CONFIG_SND_OSSEMUL */
1673 return 0;
1676 static void __exit alsa_rawmidi_exit(void)
1678 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1679 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1682 module_init(alsa_rawmidi_init)
1683 module_exit(alsa_rawmidi_exit)
1685 EXPORT_SYMBOL(snd_rawmidi_output_params);
1686 EXPORT_SYMBOL(snd_rawmidi_input_params);
1687 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1688 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1689 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1690 EXPORT_SYMBOL(snd_rawmidi_receive);
1691 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1692 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1693 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1694 EXPORT_SYMBOL(snd_rawmidi_transmit);
1695 EXPORT_SYMBOL(snd_rawmidi_new);
1696 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1697 EXPORT_SYMBOL(snd_rawmidi_info_select);
1698 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1699 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1700 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1701 EXPORT_SYMBOL(snd_rawmidi_kernel_write);