[ARM] Kirkwood: update defconfig
[linux-2.6/linux-2.6-openrd.git] / sound / core / rawmidi.c
blob473247c8e6d3e22c2e3466e80443e6e6e3054680
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 continue;
254 if (subdevice < 0 || subdevice == substream->number) {
255 *sub_ret = substream;
256 return 0;
259 return -EAGAIN;
262 /* open and do ref-counting for the given substream */
263 static int open_substream(struct snd_rawmidi *rmidi,
264 struct snd_rawmidi_substream *substream,
265 int mode)
267 int err;
269 err = snd_rawmidi_runtime_create(substream);
270 if (err < 0)
271 return err;
272 err = substream->ops->open(substream);
273 if (err < 0)
274 return err;
275 substream->opened = 1;
276 if (substream->use_count++ == 0)
277 substream->active_sensing = 1;
278 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
279 substream->append = 1;
280 rmidi->streams[substream->stream].substream_opened++;
281 return 0;
284 static void close_substream(struct snd_rawmidi *rmidi,
285 struct snd_rawmidi_substream *substream,
286 int cleanup);
288 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
289 struct snd_rawmidi_file *rfile)
291 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
292 int err;
294 rfile->input = rfile->output = NULL;
295 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
296 err = assign_substream(rmidi, subdevice,
297 SNDRV_RAWMIDI_STREAM_INPUT,
298 mode, &sinput);
299 if (err < 0)
300 goto __error;
302 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
303 err = assign_substream(rmidi, subdevice,
304 SNDRV_RAWMIDI_STREAM_OUTPUT,
305 mode, &soutput);
306 if (err < 0)
307 goto __error;
310 if (sinput) {
311 err = open_substream(rmidi, sinput, mode);
312 if (err < 0)
313 goto __error;
315 if (soutput) {
316 err = open_substream(rmidi, soutput, mode);
317 if (err < 0) {
318 if (sinput)
319 close_substream(rmidi, sinput, 0);
320 goto __error;
324 rfile->rmidi = rmidi;
325 rfile->input = sinput;
326 rfile->output = soutput;
327 return 0;
329 __error:
330 if (sinput && sinput->runtime)
331 snd_rawmidi_runtime_free(sinput);
332 if (soutput && soutput->runtime)
333 snd_rawmidi_runtime_free(soutput);
334 return err;
337 /* called from sound/core/seq/seq_midi.c */
338 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
339 int mode, struct snd_rawmidi_file * rfile)
341 struct snd_rawmidi *rmidi;
342 int err;
344 if (snd_BUG_ON(!rfile))
345 return -EINVAL;
347 mutex_lock(&register_mutex);
348 rmidi = snd_rawmidi_search(card, device);
349 if (rmidi == NULL) {
350 mutex_unlock(&register_mutex);
351 return -ENODEV;
353 if (!try_module_get(rmidi->card->module)) {
354 mutex_unlock(&register_mutex);
355 return -ENXIO;
357 mutex_unlock(&register_mutex);
359 mutex_lock(&rmidi->open_mutex);
360 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
361 mutex_unlock(&rmidi->open_mutex);
362 if (err < 0)
363 module_put(rmidi->card->module);
364 return err;
367 static int snd_rawmidi_open(struct inode *inode, struct file *file)
369 int maj = imajor(inode);
370 struct snd_card *card;
371 int subdevice;
372 unsigned short fflags;
373 int err;
374 struct snd_rawmidi *rmidi;
375 struct snd_rawmidi_file *rawmidi_file = NULL;
376 wait_queue_t wait;
377 struct snd_ctl_file *kctl;
379 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
380 return -EINVAL; /* invalid combination */
382 if (maj == snd_major) {
383 rmidi = snd_lookup_minor_data(iminor(inode),
384 SNDRV_DEVICE_TYPE_RAWMIDI);
385 #ifdef CONFIG_SND_OSSEMUL
386 } else if (maj == SOUND_MAJOR) {
387 rmidi = snd_lookup_oss_minor_data(iminor(inode),
388 SNDRV_OSS_DEVICE_TYPE_MIDI);
389 #endif
390 } else
391 return -ENXIO;
393 if (rmidi == NULL)
394 return -ENODEV;
396 if (!try_module_get(rmidi->card->module))
397 return -ENXIO;
399 mutex_lock(&rmidi->open_mutex);
400 card = rmidi->card;
401 err = snd_card_file_add(card, file);
402 if (err < 0)
403 goto __error_card;
404 fflags = snd_rawmidi_file_flags(file);
405 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
406 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
407 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
408 if (rawmidi_file == NULL) {
409 err = -ENOMEM;
410 goto __error;
412 init_waitqueue_entry(&wait, current);
413 add_wait_queue(&rmidi->open_wait, &wait);
414 while (1) {
415 subdevice = -1;
416 read_lock(&card->ctl_files_rwlock);
417 list_for_each_entry(kctl, &card->ctl_files, list) {
418 if (kctl->pid == current->pid) {
419 subdevice = kctl->prefer_rawmidi_subdevice;
420 if (subdevice != -1)
421 break;
424 read_unlock(&card->ctl_files_rwlock);
425 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
426 if (err >= 0)
427 break;
428 if (err == -EAGAIN) {
429 if (file->f_flags & O_NONBLOCK) {
430 err = -EBUSY;
431 break;
433 } else
434 break;
435 set_current_state(TASK_INTERRUPTIBLE);
436 mutex_unlock(&rmidi->open_mutex);
437 schedule();
438 mutex_lock(&rmidi->open_mutex);
439 if (signal_pending(current)) {
440 err = -ERESTARTSYS;
441 break;
444 remove_wait_queue(&rmidi->open_wait, &wait);
445 if (err < 0) {
446 kfree(rawmidi_file);
447 goto __error;
449 #ifdef CONFIG_SND_OSSEMUL
450 if (rawmidi_file->input && rawmidi_file->input->runtime)
451 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
452 if (rawmidi_file->output && rawmidi_file->output->runtime)
453 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
454 #endif
455 file->private_data = rawmidi_file;
456 mutex_unlock(&rmidi->open_mutex);
457 return 0;
459 __error:
460 snd_card_file_remove(card, file);
461 __error_card:
462 mutex_unlock(&rmidi->open_mutex);
463 module_put(rmidi->card->module);
464 return err;
467 static void close_substream(struct snd_rawmidi *rmidi,
468 struct snd_rawmidi_substream *substream,
469 int cleanup)
471 rmidi->streams[substream->stream].substream_opened--;
472 if (--substream->use_count)
473 return;
475 if (cleanup) {
476 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
477 snd_rawmidi_input_trigger(substream, 0);
478 else {
479 if (substream->active_sensing) {
480 unsigned char buf = 0xfe;
481 /* sending single active sensing message
482 * to shut the device up
484 snd_rawmidi_kernel_write(substream, &buf, 1);
486 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
487 snd_rawmidi_output_trigger(substream, 0);
490 substream->ops->close(substream);
491 if (substream->runtime->private_free)
492 substream->runtime->private_free(substream);
493 snd_rawmidi_runtime_free(substream);
494 substream->opened = 0;
495 substream->append = 0;
498 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
500 struct snd_rawmidi *rmidi;
502 rmidi = rfile->rmidi;
503 mutex_lock(&rmidi->open_mutex);
504 if (rfile->input) {
505 close_substream(rmidi, rfile->input, 1);
506 rfile->input = NULL;
508 if (rfile->output) {
509 close_substream(rmidi, rfile->output, 1);
510 rfile->output = NULL;
512 rfile->rmidi = NULL;
513 mutex_unlock(&rmidi->open_mutex);
514 wake_up(&rmidi->open_wait);
517 /* called from sound/core/seq/seq_midi.c */
518 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
520 struct snd_rawmidi *rmidi;
522 if (snd_BUG_ON(!rfile))
523 return -ENXIO;
525 rmidi = rfile->rmidi;
526 rawmidi_release_priv(rfile);
527 module_put(rmidi->card->module);
528 return 0;
531 static int snd_rawmidi_release(struct inode *inode, struct file *file)
533 struct snd_rawmidi_file *rfile;
534 struct snd_rawmidi *rmidi;
536 rfile = file->private_data;
537 rmidi = rfile->rmidi;
538 rawmidi_release_priv(rfile);
539 kfree(rfile);
540 snd_card_file_remove(rmidi->card, file);
541 module_put(rmidi->card->module);
542 return 0;
545 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
546 struct snd_rawmidi_info *info)
548 struct snd_rawmidi *rmidi;
550 if (substream == NULL)
551 return -ENODEV;
552 rmidi = substream->rmidi;
553 memset(info, 0, sizeof(*info));
554 info->card = rmidi->card->number;
555 info->device = rmidi->device;
556 info->subdevice = substream->number;
557 info->stream = substream->stream;
558 info->flags = rmidi->info_flags;
559 strcpy(info->id, rmidi->id);
560 strcpy(info->name, rmidi->name);
561 strcpy(info->subname, substream->name);
562 info->subdevices_count = substream->pstr->substream_count;
563 info->subdevices_avail = (substream->pstr->substream_count -
564 substream->pstr->substream_opened);
565 return 0;
568 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
569 struct snd_rawmidi_info __user * _info)
571 struct snd_rawmidi_info info;
572 int err;
573 if ((err = snd_rawmidi_info(substream, &info)) < 0)
574 return err;
575 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
576 return -EFAULT;
577 return 0;
580 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
582 struct snd_rawmidi *rmidi;
583 struct snd_rawmidi_str *pstr;
584 struct snd_rawmidi_substream *substream;
586 mutex_lock(&register_mutex);
587 rmidi = snd_rawmidi_search(card, info->device);
588 mutex_unlock(&register_mutex);
589 if (!rmidi)
590 return -ENXIO;
591 if (info->stream < 0 || info->stream > 1)
592 return -EINVAL;
593 pstr = &rmidi->streams[info->stream];
594 if (pstr->substream_count == 0)
595 return -ENOENT;
596 if (info->subdevice >= pstr->substream_count)
597 return -ENXIO;
598 list_for_each_entry(substream, &pstr->substreams, list) {
599 if ((unsigned int)substream->number == info->subdevice)
600 return snd_rawmidi_info(substream, info);
602 return -ENXIO;
605 static int snd_rawmidi_info_select_user(struct snd_card *card,
606 struct snd_rawmidi_info __user *_info)
608 int err;
609 struct snd_rawmidi_info info;
610 if (get_user(info.device, &_info->device))
611 return -EFAULT;
612 if (get_user(info.stream, &_info->stream))
613 return -EFAULT;
614 if (get_user(info.subdevice, &_info->subdevice))
615 return -EFAULT;
616 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
617 return err;
618 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
619 return -EFAULT;
620 return 0;
623 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
624 struct snd_rawmidi_params * params)
626 char *newbuf;
627 struct snd_rawmidi_runtime *runtime = substream->runtime;
629 if (substream->append && substream->use_count > 1)
630 return -EBUSY;
631 snd_rawmidi_drain_output(substream);
632 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
633 return -EINVAL;
635 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
636 return -EINVAL;
638 if (params->buffer_size != runtime->buffer_size) {
639 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
640 if (!newbuf)
641 return -ENOMEM;
642 kfree(runtime->buffer);
643 runtime->buffer = newbuf;
644 runtime->buffer_size = params->buffer_size;
645 runtime->avail = runtime->buffer_size;
647 runtime->avail_min = params->avail_min;
648 substream->active_sensing = !params->no_active_sensing;
649 return 0;
652 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
653 struct snd_rawmidi_params * params)
655 char *newbuf;
656 struct snd_rawmidi_runtime *runtime = substream->runtime;
658 snd_rawmidi_drain_input(substream);
659 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
660 return -EINVAL;
662 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
663 return -EINVAL;
665 if (params->buffer_size != runtime->buffer_size) {
666 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
667 if (!newbuf)
668 return -ENOMEM;
669 kfree(runtime->buffer);
670 runtime->buffer = newbuf;
671 runtime->buffer_size = params->buffer_size;
673 runtime->avail_min = params->avail_min;
674 return 0;
677 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
678 struct snd_rawmidi_status * status)
680 struct snd_rawmidi_runtime *runtime = substream->runtime;
682 memset(status, 0, sizeof(*status));
683 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
684 spin_lock_irq(&runtime->lock);
685 status->avail = runtime->avail;
686 spin_unlock_irq(&runtime->lock);
687 return 0;
690 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
691 struct snd_rawmidi_status * status)
693 struct snd_rawmidi_runtime *runtime = substream->runtime;
695 memset(status, 0, sizeof(*status));
696 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
697 spin_lock_irq(&runtime->lock);
698 status->avail = runtime->avail;
699 status->xruns = runtime->xruns;
700 runtime->xruns = 0;
701 spin_unlock_irq(&runtime->lock);
702 return 0;
705 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
707 struct snd_rawmidi_file *rfile;
708 void __user *argp = (void __user *)arg;
710 rfile = file->private_data;
711 if (((cmd >> 8) & 0xff) != 'W')
712 return -ENOTTY;
713 switch (cmd) {
714 case SNDRV_RAWMIDI_IOCTL_PVERSION:
715 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
716 case SNDRV_RAWMIDI_IOCTL_INFO:
718 int stream;
719 struct snd_rawmidi_info __user *info = argp;
720 if (get_user(stream, &info->stream))
721 return -EFAULT;
722 switch (stream) {
723 case SNDRV_RAWMIDI_STREAM_INPUT:
724 return snd_rawmidi_info_user(rfile->input, info);
725 case SNDRV_RAWMIDI_STREAM_OUTPUT:
726 return snd_rawmidi_info_user(rfile->output, info);
727 default:
728 return -EINVAL;
731 case SNDRV_RAWMIDI_IOCTL_PARAMS:
733 struct snd_rawmidi_params params;
734 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
735 return -EFAULT;
736 switch (params.stream) {
737 case SNDRV_RAWMIDI_STREAM_OUTPUT:
738 if (rfile->output == NULL)
739 return -EINVAL;
740 return snd_rawmidi_output_params(rfile->output, &params);
741 case SNDRV_RAWMIDI_STREAM_INPUT:
742 if (rfile->input == NULL)
743 return -EINVAL;
744 return snd_rawmidi_input_params(rfile->input, &params);
745 default:
746 return -EINVAL;
749 case SNDRV_RAWMIDI_IOCTL_STATUS:
751 int err = 0;
752 struct snd_rawmidi_status status;
753 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
754 return -EFAULT;
755 switch (status.stream) {
756 case SNDRV_RAWMIDI_STREAM_OUTPUT:
757 if (rfile->output == NULL)
758 return -EINVAL;
759 err = snd_rawmidi_output_status(rfile->output, &status);
760 break;
761 case SNDRV_RAWMIDI_STREAM_INPUT:
762 if (rfile->input == NULL)
763 return -EINVAL;
764 err = snd_rawmidi_input_status(rfile->input, &status);
765 break;
766 default:
767 return -EINVAL;
769 if (err < 0)
770 return err;
771 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
772 return -EFAULT;
773 return 0;
775 case SNDRV_RAWMIDI_IOCTL_DROP:
777 int val;
778 if (get_user(val, (int __user *) argp))
779 return -EFAULT;
780 switch (val) {
781 case SNDRV_RAWMIDI_STREAM_OUTPUT:
782 if (rfile->output == NULL)
783 return -EINVAL;
784 return snd_rawmidi_drop_output(rfile->output);
785 default:
786 return -EINVAL;
789 case SNDRV_RAWMIDI_IOCTL_DRAIN:
791 int val;
792 if (get_user(val, (int __user *) argp))
793 return -EFAULT;
794 switch (val) {
795 case SNDRV_RAWMIDI_STREAM_OUTPUT:
796 if (rfile->output == NULL)
797 return -EINVAL;
798 return snd_rawmidi_drain_output(rfile->output);
799 case SNDRV_RAWMIDI_STREAM_INPUT:
800 if (rfile->input == NULL)
801 return -EINVAL;
802 return snd_rawmidi_drain_input(rfile->input);
803 default:
804 return -EINVAL;
807 #ifdef CONFIG_SND_DEBUG
808 default:
809 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
810 #endif
812 return -ENOTTY;
815 static int snd_rawmidi_control_ioctl(struct snd_card *card,
816 struct snd_ctl_file *control,
817 unsigned int cmd,
818 unsigned long arg)
820 void __user *argp = (void __user *)arg;
822 switch (cmd) {
823 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
825 int device;
827 if (get_user(device, (int __user *)argp))
828 return -EFAULT;
829 mutex_lock(&register_mutex);
830 device = device < 0 ? 0 : device + 1;
831 while (device < SNDRV_RAWMIDI_DEVICES) {
832 if (snd_rawmidi_search(card, device))
833 break;
834 device++;
836 if (device == SNDRV_RAWMIDI_DEVICES)
837 device = -1;
838 mutex_unlock(&register_mutex);
839 if (put_user(device, (int __user *)argp))
840 return -EFAULT;
841 return 0;
843 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
845 int val;
847 if (get_user(val, (int __user *)argp))
848 return -EFAULT;
849 control->prefer_rawmidi_subdevice = val;
850 return 0;
852 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
853 return snd_rawmidi_info_select_user(card, argp);
855 return -ENOIOCTLCMD;
859 * snd_rawmidi_receive - receive the input data from the device
860 * @substream: the rawmidi substream
861 * @buffer: the buffer pointer
862 * @count: the data size to read
864 * Reads the data from the internal buffer.
866 * Returns the size of read data, or a negative error code on failure.
868 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
869 const unsigned char *buffer, int count)
871 unsigned long flags;
872 int result = 0, count1;
873 struct snd_rawmidi_runtime *runtime = substream->runtime;
875 if (!substream->opened)
876 return -EBADFD;
877 if (runtime->buffer == NULL) {
878 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
879 return -EINVAL;
881 spin_lock_irqsave(&runtime->lock, flags);
882 if (count == 1) { /* special case, faster code */
883 substream->bytes++;
884 if (runtime->avail < runtime->buffer_size) {
885 runtime->buffer[runtime->hw_ptr++] = buffer[0];
886 runtime->hw_ptr %= runtime->buffer_size;
887 runtime->avail++;
888 result++;
889 } else {
890 runtime->xruns++;
892 } else {
893 substream->bytes += count;
894 count1 = runtime->buffer_size - runtime->hw_ptr;
895 if (count1 > count)
896 count1 = count;
897 if (count1 > (int)(runtime->buffer_size - runtime->avail))
898 count1 = runtime->buffer_size - runtime->avail;
899 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
900 runtime->hw_ptr += count1;
901 runtime->hw_ptr %= runtime->buffer_size;
902 runtime->avail += count1;
903 count -= count1;
904 result += count1;
905 if (count > 0) {
906 buffer += count1;
907 count1 = count;
908 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
909 count1 = runtime->buffer_size - runtime->avail;
910 runtime->xruns += count - count1;
912 if (count1 > 0) {
913 memcpy(runtime->buffer, buffer, count1);
914 runtime->hw_ptr = count1;
915 runtime->avail += count1;
916 result += count1;
920 if (result > 0) {
921 if (runtime->event)
922 tasklet_schedule(&runtime->tasklet);
923 else if (snd_rawmidi_ready(substream))
924 wake_up(&runtime->sleep);
926 spin_unlock_irqrestore(&runtime->lock, flags);
927 return result;
930 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
931 unsigned char __user *userbuf,
932 unsigned char *kernelbuf, long count)
934 unsigned long flags;
935 long result = 0, count1;
936 struct snd_rawmidi_runtime *runtime = substream->runtime;
938 while (count > 0 && runtime->avail) {
939 count1 = runtime->buffer_size - runtime->appl_ptr;
940 if (count1 > count)
941 count1 = count;
942 spin_lock_irqsave(&runtime->lock, flags);
943 if (count1 > (int)runtime->avail)
944 count1 = runtime->avail;
945 if (kernelbuf)
946 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
947 if (userbuf) {
948 spin_unlock_irqrestore(&runtime->lock, flags);
949 if (copy_to_user(userbuf + result,
950 runtime->buffer + runtime->appl_ptr, count1)) {
951 return result > 0 ? result : -EFAULT;
953 spin_lock_irqsave(&runtime->lock, flags);
955 runtime->appl_ptr += count1;
956 runtime->appl_ptr %= runtime->buffer_size;
957 runtime->avail -= count1;
958 spin_unlock_irqrestore(&runtime->lock, flags);
959 result += count1;
960 count -= count1;
962 return result;
965 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
966 unsigned char *buf, long count)
968 snd_rawmidi_input_trigger(substream, 1);
969 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
972 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
973 loff_t *offset)
975 long result;
976 int count1;
977 struct snd_rawmidi_file *rfile;
978 struct snd_rawmidi_substream *substream;
979 struct snd_rawmidi_runtime *runtime;
981 rfile = file->private_data;
982 substream = rfile->input;
983 if (substream == NULL)
984 return -EIO;
985 runtime = substream->runtime;
986 snd_rawmidi_input_trigger(substream, 1);
987 result = 0;
988 while (count > 0) {
989 spin_lock_irq(&runtime->lock);
990 while (!snd_rawmidi_ready(substream)) {
991 wait_queue_t wait;
992 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
993 spin_unlock_irq(&runtime->lock);
994 return result > 0 ? result : -EAGAIN;
996 init_waitqueue_entry(&wait, current);
997 add_wait_queue(&runtime->sleep, &wait);
998 set_current_state(TASK_INTERRUPTIBLE);
999 spin_unlock_irq(&runtime->lock);
1000 schedule();
1001 remove_wait_queue(&runtime->sleep, &wait);
1002 if (signal_pending(current))
1003 return result > 0 ? result : -ERESTARTSYS;
1004 if (!runtime->avail)
1005 return result > 0 ? result : -EIO;
1006 spin_lock_irq(&runtime->lock);
1008 spin_unlock_irq(&runtime->lock);
1009 count1 = snd_rawmidi_kernel_read1(substream,
1010 (unsigned char __user *)buf,
1011 NULL/*kernelbuf*/,
1012 count);
1013 if (count1 < 0)
1014 return result > 0 ? result : count1;
1015 result += count1;
1016 buf += count1;
1017 count -= count1;
1019 return result;
1023 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1024 * @substream: the rawmidi substream
1026 * Returns 1 if the internal output buffer is empty, 0 if not.
1028 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1030 struct snd_rawmidi_runtime *runtime = substream->runtime;
1031 int result;
1032 unsigned long flags;
1034 if (runtime->buffer == NULL) {
1035 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1036 return 1;
1038 spin_lock_irqsave(&runtime->lock, flags);
1039 result = runtime->avail >= runtime->buffer_size;
1040 spin_unlock_irqrestore(&runtime->lock, flags);
1041 return result;
1045 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1046 * @substream: the rawmidi substream
1047 * @buffer: the buffer pointer
1048 * @count: data size to transfer
1050 * Copies data from the internal output buffer to the given buffer.
1052 * Call this in the interrupt handler when the midi output is ready,
1053 * and call snd_rawmidi_transmit_ack() after the transmission is
1054 * finished.
1056 * Returns the size of copied data, or a negative error code on failure.
1058 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1059 unsigned char *buffer, int count)
1061 unsigned long flags;
1062 int result, count1;
1063 struct snd_rawmidi_runtime *runtime = substream->runtime;
1065 if (runtime->buffer == NULL) {
1066 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1067 return -EINVAL;
1069 result = 0;
1070 spin_lock_irqsave(&runtime->lock, flags);
1071 if (runtime->avail >= runtime->buffer_size) {
1072 /* warning: lowlevel layer MUST trigger down the hardware */
1073 goto __skip;
1075 if (count == 1) { /* special case, faster code */
1076 *buffer = runtime->buffer[runtime->hw_ptr];
1077 result++;
1078 } else {
1079 count1 = runtime->buffer_size - runtime->hw_ptr;
1080 if (count1 > count)
1081 count1 = count;
1082 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1083 count1 = runtime->buffer_size - runtime->avail;
1084 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1085 count -= count1;
1086 result += count1;
1087 if (count > 0) {
1088 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1089 count = runtime->buffer_size - runtime->avail - count1;
1090 memcpy(buffer + count1, runtime->buffer, count);
1091 result += count;
1094 __skip:
1095 spin_unlock_irqrestore(&runtime->lock, flags);
1096 return result;
1100 * snd_rawmidi_transmit_ack - acknowledge the transmission
1101 * @substream: the rawmidi substream
1102 * @count: the tranferred count
1104 * Advances the hardware pointer for the internal output buffer with
1105 * the given size and updates the condition.
1106 * Call after the transmission is finished.
1108 * Returns the advanced size if successful, or a negative error code on failure.
1110 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1112 unsigned long flags;
1113 struct snd_rawmidi_runtime *runtime = substream->runtime;
1115 if (runtime->buffer == NULL) {
1116 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1117 return -EINVAL;
1119 spin_lock_irqsave(&runtime->lock, flags);
1120 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1121 runtime->hw_ptr += count;
1122 runtime->hw_ptr %= runtime->buffer_size;
1123 runtime->avail += count;
1124 substream->bytes += count;
1125 if (count > 0) {
1126 if (runtime->drain || snd_rawmidi_ready(substream))
1127 wake_up(&runtime->sleep);
1129 spin_unlock_irqrestore(&runtime->lock, flags);
1130 return count;
1134 * snd_rawmidi_transmit - copy from the buffer to the device
1135 * @substream: the rawmidi substream
1136 * @buffer: the buffer pointer
1137 * @count: the data size to transfer
1139 * Copies data from the buffer to the device and advances the pointer.
1141 * Returns the copied size if successful, or a negative error code on failure.
1143 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1144 unsigned char *buffer, int count)
1146 if (!substream->opened)
1147 return -EBADFD;
1148 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1149 if (count < 0)
1150 return count;
1151 return snd_rawmidi_transmit_ack(substream, count);
1154 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1155 const unsigned char __user *userbuf,
1156 const unsigned char *kernelbuf,
1157 long count)
1159 unsigned long flags;
1160 long count1, result;
1161 struct snd_rawmidi_runtime *runtime = substream->runtime;
1163 if (snd_BUG_ON(!kernelbuf && !userbuf))
1164 return -EINVAL;
1165 if (snd_BUG_ON(!runtime->buffer))
1166 return -EINVAL;
1168 result = 0;
1169 spin_lock_irqsave(&runtime->lock, flags);
1170 if (substream->append) {
1171 if ((long)runtime->avail < count) {
1172 spin_unlock_irqrestore(&runtime->lock, flags);
1173 return -EAGAIN;
1176 while (count > 0 && runtime->avail > 0) {
1177 count1 = runtime->buffer_size - runtime->appl_ptr;
1178 if (count1 > count)
1179 count1 = count;
1180 if (count1 > (long)runtime->avail)
1181 count1 = runtime->avail;
1182 if (kernelbuf)
1183 memcpy(runtime->buffer + runtime->appl_ptr,
1184 kernelbuf + result, count1);
1185 else if (userbuf) {
1186 spin_unlock_irqrestore(&runtime->lock, flags);
1187 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1188 userbuf + result, count1)) {
1189 spin_lock_irqsave(&runtime->lock, flags);
1190 result = result > 0 ? result : -EFAULT;
1191 goto __end;
1193 spin_lock_irqsave(&runtime->lock, flags);
1195 runtime->appl_ptr += count1;
1196 runtime->appl_ptr %= runtime->buffer_size;
1197 runtime->avail -= count1;
1198 result += count1;
1199 count -= count1;
1201 __end:
1202 count1 = runtime->avail < runtime->buffer_size;
1203 spin_unlock_irqrestore(&runtime->lock, flags);
1204 if (count1)
1205 snd_rawmidi_output_trigger(substream, 1);
1206 return result;
1209 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1210 const unsigned char *buf, long count)
1212 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1215 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1216 size_t count, loff_t *offset)
1218 long result, timeout;
1219 int count1;
1220 struct snd_rawmidi_file *rfile;
1221 struct snd_rawmidi_runtime *runtime;
1222 struct snd_rawmidi_substream *substream;
1224 rfile = file->private_data;
1225 substream = rfile->output;
1226 runtime = substream->runtime;
1227 /* we cannot put an atomic message to our buffer */
1228 if (substream->append && count > runtime->buffer_size)
1229 return -EIO;
1230 result = 0;
1231 while (count > 0) {
1232 spin_lock_irq(&runtime->lock);
1233 while (!snd_rawmidi_ready_append(substream, count)) {
1234 wait_queue_t wait;
1235 if (file->f_flags & O_NONBLOCK) {
1236 spin_unlock_irq(&runtime->lock);
1237 return result > 0 ? result : -EAGAIN;
1239 init_waitqueue_entry(&wait, current);
1240 add_wait_queue(&runtime->sleep, &wait);
1241 set_current_state(TASK_INTERRUPTIBLE);
1242 spin_unlock_irq(&runtime->lock);
1243 timeout = schedule_timeout(30 * HZ);
1244 remove_wait_queue(&runtime->sleep, &wait);
1245 if (signal_pending(current))
1246 return result > 0 ? result : -ERESTARTSYS;
1247 if (!runtime->avail && !timeout)
1248 return result > 0 ? result : -EIO;
1249 spin_lock_irq(&runtime->lock);
1251 spin_unlock_irq(&runtime->lock);
1252 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1253 if (count1 < 0)
1254 return result > 0 ? result : count1;
1255 result += count1;
1256 buf += count1;
1257 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1258 break;
1259 count -= count1;
1261 if (file->f_flags & O_SYNC) {
1262 spin_lock_irq(&runtime->lock);
1263 while (runtime->avail != runtime->buffer_size) {
1264 wait_queue_t wait;
1265 unsigned int last_avail = runtime->avail;
1266 init_waitqueue_entry(&wait, current);
1267 add_wait_queue(&runtime->sleep, &wait);
1268 set_current_state(TASK_INTERRUPTIBLE);
1269 spin_unlock_irq(&runtime->lock);
1270 timeout = schedule_timeout(30 * HZ);
1271 remove_wait_queue(&runtime->sleep, &wait);
1272 if (signal_pending(current))
1273 return result > 0 ? result : -ERESTARTSYS;
1274 if (runtime->avail == last_avail && !timeout)
1275 return result > 0 ? result : -EIO;
1276 spin_lock_irq(&runtime->lock);
1278 spin_unlock_irq(&runtime->lock);
1280 return result;
1283 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1285 struct snd_rawmidi_file *rfile;
1286 struct snd_rawmidi_runtime *runtime;
1287 unsigned int mask;
1289 rfile = file->private_data;
1290 if (rfile->input != NULL) {
1291 runtime = rfile->input->runtime;
1292 snd_rawmidi_input_trigger(rfile->input, 1);
1293 poll_wait(file, &runtime->sleep, wait);
1295 if (rfile->output != NULL) {
1296 runtime = rfile->output->runtime;
1297 poll_wait(file, &runtime->sleep, wait);
1299 mask = 0;
1300 if (rfile->input != NULL) {
1301 if (snd_rawmidi_ready(rfile->input))
1302 mask |= POLLIN | POLLRDNORM;
1304 if (rfile->output != NULL) {
1305 if (snd_rawmidi_ready(rfile->output))
1306 mask |= POLLOUT | POLLWRNORM;
1308 return mask;
1313 #ifdef CONFIG_COMPAT
1314 #include "rawmidi_compat.c"
1315 #else
1316 #define snd_rawmidi_ioctl_compat NULL
1317 #endif
1323 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1324 struct snd_info_buffer *buffer)
1326 struct snd_rawmidi *rmidi;
1327 struct snd_rawmidi_substream *substream;
1328 struct snd_rawmidi_runtime *runtime;
1330 rmidi = entry->private_data;
1331 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1332 mutex_lock(&rmidi->open_mutex);
1333 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1334 list_for_each_entry(substream,
1335 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1336 list) {
1337 snd_iprintf(buffer,
1338 "Output %d\n"
1339 " Tx bytes : %lu\n",
1340 substream->number,
1341 (unsigned long) substream->bytes);
1342 if (substream->opened) {
1343 runtime = substream->runtime;
1344 snd_iprintf(buffer,
1345 " Mode : %s\n"
1346 " Buffer size : %lu\n"
1347 " Avail : %lu\n",
1348 runtime->oss ? "OSS compatible" : "native",
1349 (unsigned long) runtime->buffer_size,
1350 (unsigned long) runtime->avail);
1354 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1355 list_for_each_entry(substream,
1356 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1357 list) {
1358 snd_iprintf(buffer,
1359 "Input %d\n"
1360 " Rx bytes : %lu\n",
1361 substream->number,
1362 (unsigned long) substream->bytes);
1363 if (substream->opened) {
1364 runtime = substream->runtime;
1365 snd_iprintf(buffer,
1366 " Buffer size : %lu\n"
1367 " Avail : %lu\n"
1368 " Overruns : %lu\n",
1369 (unsigned long) runtime->buffer_size,
1370 (unsigned long) runtime->avail,
1371 (unsigned long) runtime->xruns);
1375 mutex_unlock(&rmidi->open_mutex);
1379 * Register functions
1382 static const struct file_operations snd_rawmidi_f_ops =
1384 .owner = THIS_MODULE,
1385 .read = snd_rawmidi_read,
1386 .write = snd_rawmidi_write,
1387 .open = snd_rawmidi_open,
1388 .release = snd_rawmidi_release,
1389 .poll = snd_rawmidi_poll,
1390 .unlocked_ioctl = snd_rawmidi_ioctl,
1391 .compat_ioctl = snd_rawmidi_ioctl_compat,
1394 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1395 struct snd_rawmidi_str *stream,
1396 int direction,
1397 int count)
1399 struct snd_rawmidi_substream *substream;
1400 int idx;
1402 for (idx = 0; idx < count; idx++) {
1403 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1404 if (substream == NULL) {
1405 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1406 return -ENOMEM;
1408 substream->stream = direction;
1409 substream->number = idx;
1410 substream->rmidi = rmidi;
1411 substream->pstr = stream;
1412 list_add_tail(&substream->list, &stream->substreams);
1413 stream->substream_count++;
1415 return 0;
1419 * snd_rawmidi_new - create a rawmidi instance
1420 * @card: the card instance
1421 * @id: the id string
1422 * @device: the device index
1423 * @output_count: the number of output streams
1424 * @input_count: the number of input streams
1425 * @rrawmidi: the pointer to store the new rawmidi instance
1427 * Creates a new rawmidi instance.
1428 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1430 * Returns zero if successful, or a negative error code on failure.
1432 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1433 int output_count, int input_count,
1434 struct snd_rawmidi ** rrawmidi)
1436 struct snd_rawmidi *rmidi;
1437 int err;
1438 static struct snd_device_ops ops = {
1439 .dev_free = snd_rawmidi_dev_free,
1440 .dev_register = snd_rawmidi_dev_register,
1441 .dev_disconnect = snd_rawmidi_dev_disconnect,
1444 if (snd_BUG_ON(!card))
1445 return -ENXIO;
1446 if (rrawmidi)
1447 *rrawmidi = NULL;
1448 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1449 if (rmidi == NULL) {
1450 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1451 return -ENOMEM;
1453 rmidi->card = card;
1454 rmidi->device = device;
1455 mutex_init(&rmidi->open_mutex);
1456 init_waitqueue_head(&rmidi->open_wait);
1457 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1458 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1460 if (id != NULL)
1461 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1462 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1463 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1464 SNDRV_RAWMIDI_STREAM_INPUT,
1465 input_count)) < 0) {
1466 snd_rawmidi_free(rmidi);
1467 return err;
1469 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1470 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1471 SNDRV_RAWMIDI_STREAM_OUTPUT,
1472 output_count)) < 0) {
1473 snd_rawmidi_free(rmidi);
1474 return err;
1476 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1477 snd_rawmidi_free(rmidi);
1478 return err;
1480 if (rrawmidi)
1481 *rrawmidi = rmidi;
1482 return 0;
1485 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1487 struct snd_rawmidi_substream *substream;
1489 while (!list_empty(&stream->substreams)) {
1490 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1491 list_del(&substream->list);
1492 kfree(substream);
1496 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1498 if (!rmidi)
1499 return 0;
1501 snd_info_free_entry(rmidi->proc_entry);
1502 rmidi->proc_entry = NULL;
1503 mutex_lock(&register_mutex);
1504 if (rmidi->ops && rmidi->ops->dev_unregister)
1505 rmidi->ops->dev_unregister(rmidi);
1506 mutex_unlock(&register_mutex);
1508 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1509 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1510 if (rmidi->private_free)
1511 rmidi->private_free(rmidi);
1512 kfree(rmidi);
1513 return 0;
1516 static int snd_rawmidi_dev_free(struct snd_device *device)
1518 struct snd_rawmidi *rmidi = device->device_data;
1519 return snd_rawmidi_free(rmidi);
1522 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1523 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1525 struct snd_rawmidi *rmidi = device->private_data;
1526 rmidi->seq_dev = NULL;
1528 #endif
1530 static int snd_rawmidi_dev_register(struct snd_device *device)
1532 int err;
1533 struct snd_info_entry *entry;
1534 char name[16];
1535 struct snd_rawmidi *rmidi = device->device_data;
1537 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1538 return -ENOMEM;
1539 mutex_lock(&register_mutex);
1540 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1541 mutex_unlock(&register_mutex);
1542 return -EBUSY;
1544 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1545 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1546 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1547 rmidi->card, rmidi->device,
1548 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1549 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1550 list_del(&rmidi->list);
1551 mutex_unlock(&register_mutex);
1552 return err;
1554 if (rmidi->ops && rmidi->ops->dev_register &&
1555 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1556 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1557 list_del(&rmidi->list);
1558 mutex_unlock(&register_mutex);
1559 return err;
1561 #ifdef CONFIG_SND_OSSEMUL
1562 rmidi->ossreg = 0;
1563 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1564 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1565 rmidi->card, 0, &snd_rawmidi_f_ops,
1566 rmidi, name) < 0) {
1567 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1568 } else {
1569 rmidi->ossreg++;
1570 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1571 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1572 #endif
1575 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1576 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1577 rmidi->card, 1, &snd_rawmidi_f_ops,
1578 rmidi, name) < 0) {
1579 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1580 } else {
1581 rmidi->ossreg++;
1584 #endif /* CONFIG_SND_OSSEMUL */
1585 mutex_unlock(&register_mutex);
1586 sprintf(name, "midi%d", rmidi->device);
1587 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1588 if (entry) {
1589 entry->private_data = rmidi;
1590 entry->c.text.read = snd_rawmidi_proc_info_read;
1591 if (snd_info_register(entry) < 0) {
1592 snd_info_free_entry(entry);
1593 entry = NULL;
1596 rmidi->proc_entry = entry;
1597 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1598 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1599 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1600 rmidi->seq_dev->private_data = rmidi;
1601 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1602 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1603 snd_device_register(rmidi->card, rmidi->seq_dev);
1606 #endif
1607 return 0;
1610 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1612 struct snd_rawmidi *rmidi = device->device_data;
1614 mutex_lock(&register_mutex);
1615 list_del_init(&rmidi->list);
1616 #ifdef CONFIG_SND_OSSEMUL
1617 if (rmidi->ossreg) {
1618 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1619 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1620 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1621 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1622 #endif
1624 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1625 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1626 rmidi->ossreg = 0;
1628 #endif /* CONFIG_SND_OSSEMUL */
1629 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1630 mutex_unlock(&register_mutex);
1631 return 0;
1635 * snd_rawmidi_set_ops - set the rawmidi operators
1636 * @rmidi: the rawmidi instance
1637 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1638 * @ops: the operator table
1640 * Sets the rawmidi operators for the given stream direction.
1642 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1643 struct snd_rawmidi_ops *ops)
1645 struct snd_rawmidi_substream *substream;
1647 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1648 substream->ops = ops;
1652 * ENTRY functions
1655 static int __init alsa_rawmidi_init(void)
1658 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1659 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1660 #ifdef CONFIG_SND_OSSEMUL
1661 { int i;
1662 /* check device map table */
1663 for (i = 0; i < SNDRV_CARDS; i++) {
1664 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1665 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1666 midi_map[i] = 0;
1668 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1669 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1670 amidi_map[i] = 1;
1674 #endif /* CONFIG_SND_OSSEMUL */
1675 return 0;
1678 static void __exit alsa_rawmidi_exit(void)
1680 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1681 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1684 module_init(alsa_rawmidi_init)
1685 module_exit(alsa_rawmidi_exit)
1687 EXPORT_SYMBOL(snd_rawmidi_output_params);
1688 EXPORT_SYMBOL(snd_rawmidi_input_params);
1689 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1690 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1691 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1692 EXPORT_SYMBOL(snd_rawmidi_receive);
1693 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1694 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1695 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1696 EXPORT_SYMBOL(snd_rawmidi_transmit);
1697 EXPORT_SYMBOL(snd_rawmidi_new);
1698 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1699 EXPORT_SYMBOL(snd_rawmidi_info_select);
1700 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1701 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1702 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1703 EXPORT_SYMBOL(snd_rawmidi_kernel_write);