include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / sound / core / rawmidi.c
blobcbcdb048d993d7b1fe07b357b97017fe97e2ef31
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/module.h>
27 #include <linux/slab.h>
28 #include <linux/time.h>
29 #include <linux/wait.h>
30 #include <linux/mutex.h>
31 #include <linux/moduleparam.h>
32 #include <linux/delay.h>
33 #include <sound/rawmidi.h>
34 #include <sound/info.h>
35 #include <sound/control.h>
36 #include <sound/minors.h>
37 #include <sound/initval.h>
39 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
40 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
41 MODULE_LICENSE("GPL");
43 #ifdef CONFIG_SND_OSSEMUL
44 static int midi_map[SNDRV_CARDS];
45 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
46 module_param_array(midi_map, int, NULL, 0444);
47 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
48 module_param_array(amidi_map, int, NULL, 0444);
49 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
50 #endif /* CONFIG_SND_OSSEMUL */
52 static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
53 static int snd_rawmidi_dev_free(struct snd_device *device);
54 static int snd_rawmidi_dev_register(struct snd_device *device);
55 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
57 static LIST_HEAD(snd_rawmidi_devices);
58 static DEFINE_MUTEX(register_mutex);
60 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
62 struct snd_rawmidi *rawmidi;
64 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
65 if (rawmidi->card == card && rawmidi->device == device)
66 return rawmidi;
67 return NULL;
70 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
72 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
73 case FMODE_WRITE:
74 return SNDRV_RAWMIDI_LFLG_OUTPUT;
75 case FMODE_READ:
76 return SNDRV_RAWMIDI_LFLG_INPUT;
77 default:
78 return SNDRV_RAWMIDI_LFLG_OPEN;
82 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
84 struct snd_rawmidi_runtime *runtime = substream->runtime;
85 return runtime->avail >= runtime->avail_min;
88 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
89 size_t count)
91 struct snd_rawmidi_runtime *runtime = substream->runtime;
92 return runtime->avail >= runtime->avail_min &&
93 (!substream->append || runtime->avail >= count);
96 static void snd_rawmidi_input_event_work(struct work_struct *work)
98 struct snd_rawmidi_runtime *runtime =
99 container_of(work, struct snd_rawmidi_runtime, event_work);
100 if (runtime->event)
101 runtime->event(runtime->substream);
104 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
106 struct snd_rawmidi_runtime *runtime;
108 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
109 return -ENOMEM;
110 runtime->substream = substream;
111 spin_lock_init(&runtime->lock);
112 init_waitqueue_head(&runtime->sleep);
113 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
114 runtime->event = NULL;
115 runtime->buffer_size = PAGE_SIZE;
116 runtime->avail_min = 1;
117 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
118 runtime->avail = 0;
119 else
120 runtime->avail = runtime->buffer_size;
121 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
122 kfree(runtime);
123 return -ENOMEM;
125 runtime->appl_ptr = runtime->hw_ptr = 0;
126 substream->runtime = runtime;
127 return 0;
130 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
132 struct snd_rawmidi_runtime *runtime = substream->runtime;
134 kfree(runtime->buffer);
135 kfree(runtime);
136 substream->runtime = NULL;
137 return 0;
140 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
142 if (!substream->opened)
143 return;
144 substream->ops->trigger(substream, up);
147 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
149 if (!substream->opened)
150 return;
151 substream->ops->trigger(substream, up);
152 if (!up)
153 cancel_work_sync(&substream->runtime->event_work);
156 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
158 unsigned long flags;
159 struct snd_rawmidi_runtime *runtime = substream->runtime;
161 snd_rawmidi_output_trigger(substream, 0);
162 runtime->drain = 0;
163 spin_lock_irqsave(&runtime->lock, flags);
164 runtime->appl_ptr = runtime->hw_ptr = 0;
165 runtime->avail = runtime->buffer_size;
166 spin_unlock_irqrestore(&runtime->lock, flags);
167 return 0;
170 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
172 int err;
173 long timeout;
174 struct snd_rawmidi_runtime *runtime = substream->runtime;
176 err = 0;
177 runtime->drain = 1;
178 timeout = wait_event_interruptible_timeout(runtime->sleep,
179 (runtime->avail >= runtime->buffer_size),
180 10*HZ);
181 if (signal_pending(current))
182 err = -ERESTARTSYS;
183 if (runtime->avail < runtime->buffer_size && !timeout) {
184 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
185 err = -EIO;
187 runtime->drain = 0;
188 if (err != -ERESTARTSYS) {
189 /* we need wait a while to make sure that Tx FIFOs are empty */
190 if (substream->ops->drain)
191 substream->ops->drain(substream);
192 else
193 msleep(50);
194 snd_rawmidi_drop_output(substream);
196 return err;
199 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
201 unsigned long flags;
202 struct snd_rawmidi_runtime *runtime = substream->runtime;
204 snd_rawmidi_input_trigger(substream, 0);
205 runtime->drain = 0;
206 spin_lock_irqsave(&runtime->lock, flags);
207 runtime->appl_ptr = runtime->hw_ptr = 0;
208 runtime->avail = 0;
209 spin_unlock_irqrestore(&runtime->lock, flags);
210 return 0;
213 /* look for an available substream for the given stream direction;
214 * if a specific subdevice is given, try to assign it
216 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
217 int stream, int mode,
218 struct snd_rawmidi_substream **sub_ret)
220 struct snd_rawmidi_substream *substream;
221 struct snd_rawmidi_str *s = &rmidi->streams[stream];
222 static unsigned int info_flags[2] = {
223 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
224 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
227 if (!(rmidi->info_flags & info_flags[stream]))
228 return -ENXIO;
229 if (subdevice >= 0 && subdevice >= s->substream_count)
230 return -ENODEV;
232 list_for_each_entry(substream, &s->substreams, list) {
233 if (substream->opened) {
234 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
235 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
236 !substream->append)
237 continue;
239 if (subdevice < 0 || subdevice == substream->number) {
240 *sub_ret = substream;
241 return 0;
244 return -EAGAIN;
247 /* open and do ref-counting for the given substream */
248 static int open_substream(struct snd_rawmidi *rmidi,
249 struct snd_rawmidi_substream *substream,
250 int mode)
252 int err;
254 if (substream->use_count == 0) {
255 err = snd_rawmidi_runtime_create(substream);
256 if (err < 0)
257 return err;
258 err = substream->ops->open(substream);
259 if (err < 0) {
260 snd_rawmidi_runtime_free(substream);
261 return err;
263 substream->opened = 1;
264 substream->active_sensing = 0;
265 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
266 substream->append = 1;
267 substream->pid = get_pid(task_pid(current));
268 rmidi->streams[substream->stream].substream_opened++;
270 substream->use_count++;
271 return 0;
274 static void close_substream(struct snd_rawmidi *rmidi,
275 struct snd_rawmidi_substream *substream,
276 int cleanup);
278 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
279 struct snd_rawmidi_file *rfile)
281 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
282 int err;
284 rfile->input = rfile->output = NULL;
285 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
286 err = assign_substream(rmidi, subdevice,
287 SNDRV_RAWMIDI_STREAM_INPUT,
288 mode, &sinput);
289 if (err < 0)
290 return err;
292 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
293 err = assign_substream(rmidi, subdevice,
294 SNDRV_RAWMIDI_STREAM_OUTPUT,
295 mode, &soutput);
296 if (err < 0)
297 return err;
300 if (sinput) {
301 err = open_substream(rmidi, sinput, mode);
302 if (err < 0)
303 return err;
305 if (soutput) {
306 err = open_substream(rmidi, soutput, mode);
307 if (err < 0) {
308 if (sinput)
309 close_substream(rmidi, sinput, 0);
310 return err;
314 rfile->rmidi = rmidi;
315 rfile->input = sinput;
316 rfile->output = soutput;
317 return 0;
320 /* called from sound/core/seq/seq_midi.c */
321 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
322 int mode, struct snd_rawmidi_file * rfile)
324 struct snd_rawmidi *rmidi;
325 int err;
327 if (snd_BUG_ON(!rfile))
328 return -EINVAL;
330 mutex_lock(&register_mutex);
331 rmidi = snd_rawmidi_search(card, device);
332 if (rmidi == NULL) {
333 mutex_unlock(&register_mutex);
334 return -ENODEV;
336 if (!try_module_get(rmidi->card->module)) {
337 mutex_unlock(&register_mutex);
338 return -ENXIO;
340 mutex_unlock(&register_mutex);
342 mutex_lock(&rmidi->open_mutex);
343 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
344 mutex_unlock(&rmidi->open_mutex);
345 if (err < 0)
346 module_put(rmidi->card->module);
347 return err;
350 static int snd_rawmidi_open(struct inode *inode, struct file *file)
352 int maj = imajor(inode);
353 struct snd_card *card;
354 int subdevice;
355 unsigned short fflags;
356 int err;
357 struct snd_rawmidi *rmidi;
358 struct snd_rawmidi_file *rawmidi_file = NULL;
359 wait_queue_t wait;
360 struct snd_ctl_file *kctl;
362 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
363 return -EINVAL; /* invalid combination */
365 err = nonseekable_open(inode, file);
366 if (err < 0)
367 return err;
369 if (maj == snd_major) {
370 rmidi = snd_lookup_minor_data(iminor(inode),
371 SNDRV_DEVICE_TYPE_RAWMIDI);
372 #ifdef CONFIG_SND_OSSEMUL
373 } else if (maj == SOUND_MAJOR) {
374 rmidi = snd_lookup_oss_minor_data(iminor(inode),
375 SNDRV_OSS_DEVICE_TYPE_MIDI);
376 #endif
377 } else
378 return -ENXIO;
380 if (rmidi == NULL)
381 return -ENODEV;
383 if (!try_module_get(rmidi->card->module))
384 return -ENXIO;
386 mutex_lock(&rmidi->open_mutex);
387 card = rmidi->card;
388 err = snd_card_file_add(card, file);
389 if (err < 0)
390 goto __error_card;
391 fflags = snd_rawmidi_file_flags(file);
392 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
393 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
394 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
395 if (rawmidi_file == NULL) {
396 err = -ENOMEM;
397 goto __error;
399 init_waitqueue_entry(&wait, current);
400 add_wait_queue(&rmidi->open_wait, &wait);
401 while (1) {
402 subdevice = -1;
403 read_lock(&card->ctl_files_rwlock);
404 list_for_each_entry(kctl, &card->ctl_files, list) {
405 if (kctl->pid == task_pid(current)) {
406 subdevice = kctl->prefer_rawmidi_subdevice;
407 if (subdevice != -1)
408 break;
411 read_unlock(&card->ctl_files_rwlock);
412 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
413 if (err >= 0)
414 break;
415 if (err == -EAGAIN) {
416 if (file->f_flags & O_NONBLOCK) {
417 err = -EBUSY;
418 break;
420 } else
421 break;
422 set_current_state(TASK_INTERRUPTIBLE);
423 mutex_unlock(&rmidi->open_mutex);
424 schedule();
425 mutex_lock(&rmidi->open_mutex);
426 if (signal_pending(current)) {
427 err = -ERESTARTSYS;
428 break;
431 remove_wait_queue(&rmidi->open_wait, &wait);
432 if (err < 0) {
433 kfree(rawmidi_file);
434 goto __error;
436 #ifdef CONFIG_SND_OSSEMUL
437 if (rawmidi_file->input && rawmidi_file->input->runtime)
438 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
439 if (rawmidi_file->output && rawmidi_file->output->runtime)
440 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
441 #endif
442 file->private_data = rawmidi_file;
443 mutex_unlock(&rmidi->open_mutex);
444 return 0;
446 __error:
447 snd_card_file_remove(card, file);
448 __error_card:
449 mutex_unlock(&rmidi->open_mutex);
450 module_put(rmidi->card->module);
451 return err;
454 static void close_substream(struct snd_rawmidi *rmidi,
455 struct snd_rawmidi_substream *substream,
456 int cleanup)
458 if (--substream->use_count)
459 return;
461 if (cleanup) {
462 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
463 snd_rawmidi_input_trigger(substream, 0);
464 else {
465 if (substream->active_sensing) {
466 unsigned char buf = 0xfe;
467 /* sending single active sensing message
468 * to shut the device up
470 snd_rawmidi_kernel_write(substream, &buf, 1);
472 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
473 snd_rawmidi_output_trigger(substream, 0);
476 substream->ops->close(substream);
477 if (substream->runtime->private_free)
478 substream->runtime->private_free(substream);
479 snd_rawmidi_runtime_free(substream);
480 substream->opened = 0;
481 substream->append = 0;
482 put_pid(substream->pid);
483 substream->pid = NULL;
484 rmidi->streams[substream->stream].substream_opened--;
487 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
489 struct snd_rawmidi *rmidi;
491 rmidi = rfile->rmidi;
492 mutex_lock(&rmidi->open_mutex);
493 if (rfile->input) {
494 close_substream(rmidi, rfile->input, 1);
495 rfile->input = NULL;
497 if (rfile->output) {
498 close_substream(rmidi, rfile->output, 1);
499 rfile->output = NULL;
501 rfile->rmidi = NULL;
502 mutex_unlock(&rmidi->open_mutex);
503 wake_up(&rmidi->open_wait);
506 /* called from sound/core/seq/seq_midi.c */
507 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
509 struct snd_rawmidi *rmidi;
511 if (snd_BUG_ON(!rfile))
512 return -ENXIO;
514 rmidi = rfile->rmidi;
515 rawmidi_release_priv(rfile);
516 module_put(rmidi->card->module);
517 return 0;
520 static int snd_rawmidi_release(struct inode *inode, struct file *file)
522 struct snd_rawmidi_file *rfile;
523 struct snd_rawmidi *rmidi;
524 struct module *module;
526 rfile = file->private_data;
527 rmidi = rfile->rmidi;
528 rawmidi_release_priv(rfile);
529 kfree(rfile);
530 module = rmidi->card->module;
531 snd_card_file_remove(rmidi->card, file);
532 module_put(module);
533 return 0;
536 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
537 struct snd_rawmidi_info *info)
539 struct snd_rawmidi *rmidi;
541 if (substream == NULL)
542 return -ENODEV;
543 rmidi = substream->rmidi;
544 memset(info, 0, sizeof(*info));
545 info->card = rmidi->card->number;
546 info->device = rmidi->device;
547 info->subdevice = substream->number;
548 info->stream = substream->stream;
549 info->flags = rmidi->info_flags;
550 strcpy(info->id, rmidi->id);
551 strcpy(info->name, rmidi->name);
552 strcpy(info->subname, substream->name);
553 info->subdevices_count = substream->pstr->substream_count;
554 info->subdevices_avail = (substream->pstr->substream_count -
555 substream->pstr->substream_opened);
556 return 0;
559 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
560 struct snd_rawmidi_info __user * _info)
562 struct snd_rawmidi_info info;
563 int err;
564 if ((err = snd_rawmidi_info(substream, &info)) < 0)
565 return err;
566 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
567 return -EFAULT;
568 return 0;
571 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
573 struct snd_rawmidi *rmidi;
574 struct snd_rawmidi_str *pstr;
575 struct snd_rawmidi_substream *substream;
577 mutex_lock(&register_mutex);
578 rmidi = snd_rawmidi_search(card, info->device);
579 mutex_unlock(&register_mutex);
580 if (!rmidi)
581 return -ENXIO;
582 if (info->stream < 0 || info->stream > 1)
583 return -EINVAL;
584 pstr = &rmidi->streams[info->stream];
585 if (pstr->substream_count == 0)
586 return -ENOENT;
587 if (info->subdevice >= pstr->substream_count)
588 return -ENXIO;
589 list_for_each_entry(substream, &pstr->substreams, list) {
590 if ((unsigned int)substream->number == info->subdevice)
591 return snd_rawmidi_info(substream, info);
593 return -ENXIO;
596 static int snd_rawmidi_info_select_user(struct snd_card *card,
597 struct snd_rawmidi_info __user *_info)
599 int err;
600 struct snd_rawmidi_info info;
601 if (get_user(info.device, &_info->device))
602 return -EFAULT;
603 if (get_user(info.stream, &_info->stream))
604 return -EFAULT;
605 if (get_user(info.subdevice, &_info->subdevice))
606 return -EFAULT;
607 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
608 return err;
609 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
610 return -EFAULT;
611 return 0;
614 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
615 struct snd_rawmidi_params * params)
617 char *newbuf;
618 struct snd_rawmidi_runtime *runtime = substream->runtime;
620 if (substream->append && substream->use_count > 1)
621 return -EBUSY;
622 snd_rawmidi_drain_output(substream);
623 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
624 return -EINVAL;
626 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
627 return -EINVAL;
629 if (params->buffer_size != runtime->buffer_size) {
630 newbuf = krealloc(runtime->buffer, params->buffer_size,
631 GFP_KERNEL);
632 if (!newbuf)
633 return -ENOMEM;
634 runtime->buffer = newbuf;
635 runtime->buffer_size = params->buffer_size;
636 runtime->avail = runtime->buffer_size;
638 runtime->avail_min = params->avail_min;
639 substream->active_sensing = !params->no_active_sensing;
640 return 0;
643 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
644 struct snd_rawmidi_params * params)
646 char *newbuf;
647 struct snd_rawmidi_runtime *runtime = substream->runtime;
649 snd_rawmidi_drain_input(substream);
650 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
651 return -EINVAL;
653 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
654 return -EINVAL;
656 if (params->buffer_size != runtime->buffer_size) {
657 newbuf = krealloc(runtime->buffer, params->buffer_size,
658 GFP_KERNEL);
659 if (!newbuf)
660 return -ENOMEM;
661 runtime->buffer = newbuf;
662 runtime->buffer_size = params->buffer_size;
664 runtime->avail_min = params->avail_min;
665 return 0;
668 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
669 struct snd_rawmidi_status * status)
671 struct snd_rawmidi_runtime *runtime = substream->runtime;
673 memset(status, 0, sizeof(*status));
674 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
675 spin_lock_irq(&runtime->lock);
676 status->avail = runtime->avail;
677 spin_unlock_irq(&runtime->lock);
678 return 0;
681 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
682 struct snd_rawmidi_status * status)
684 struct snd_rawmidi_runtime *runtime = substream->runtime;
686 memset(status, 0, sizeof(*status));
687 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
688 spin_lock_irq(&runtime->lock);
689 status->avail = runtime->avail;
690 status->xruns = runtime->xruns;
691 runtime->xruns = 0;
692 spin_unlock_irq(&runtime->lock);
693 return 0;
696 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
698 struct snd_rawmidi_file *rfile;
699 void __user *argp = (void __user *)arg;
701 rfile = file->private_data;
702 if (((cmd >> 8) & 0xff) != 'W')
703 return -ENOTTY;
704 switch (cmd) {
705 case SNDRV_RAWMIDI_IOCTL_PVERSION:
706 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
707 case SNDRV_RAWMIDI_IOCTL_INFO:
709 int stream;
710 struct snd_rawmidi_info __user *info = argp;
711 if (get_user(stream, &info->stream))
712 return -EFAULT;
713 switch (stream) {
714 case SNDRV_RAWMIDI_STREAM_INPUT:
715 return snd_rawmidi_info_user(rfile->input, info);
716 case SNDRV_RAWMIDI_STREAM_OUTPUT:
717 return snd_rawmidi_info_user(rfile->output, info);
718 default:
719 return -EINVAL;
722 case SNDRV_RAWMIDI_IOCTL_PARAMS:
724 struct snd_rawmidi_params params;
725 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
726 return -EFAULT;
727 switch (params.stream) {
728 case SNDRV_RAWMIDI_STREAM_OUTPUT:
729 if (rfile->output == NULL)
730 return -EINVAL;
731 return snd_rawmidi_output_params(rfile->output, &params);
732 case SNDRV_RAWMIDI_STREAM_INPUT:
733 if (rfile->input == NULL)
734 return -EINVAL;
735 return snd_rawmidi_input_params(rfile->input, &params);
736 default:
737 return -EINVAL;
740 case SNDRV_RAWMIDI_IOCTL_STATUS:
742 int err = 0;
743 struct snd_rawmidi_status status;
744 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
745 return -EFAULT;
746 switch (status.stream) {
747 case SNDRV_RAWMIDI_STREAM_OUTPUT:
748 if (rfile->output == NULL)
749 return -EINVAL;
750 err = snd_rawmidi_output_status(rfile->output, &status);
751 break;
752 case SNDRV_RAWMIDI_STREAM_INPUT:
753 if (rfile->input == NULL)
754 return -EINVAL;
755 err = snd_rawmidi_input_status(rfile->input, &status);
756 break;
757 default:
758 return -EINVAL;
760 if (err < 0)
761 return err;
762 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
763 return -EFAULT;
764 return 0;
766 case SNDRV_RAWMIDI_IOCTL_DROP:
768 int val;
769 if (get_user(val, (int __user *) argp))
770 return -EFAULT;
771 switch (val) {
772 case SNDRV_RAWMIDI_STREAM_OUTPUT:
773 if (rfile->output == NULL)
774 return -EINVAL;
775 return snd_rawmidi_drop_output(rfile->output);
776 default:
777 return -EINVAL;
780 case SNDRV_RAWMIDI_IOCTL_DRAIN:
782 int val;
783 if (get_user(val, (int __user *) argp))
784 return -EFAULT;
785 switch (val) {
786 case SNDRV_RAWMIDI_STREAM_OUTPUT:
787 if (rfile->output == NULL)
788 return -EINVAL;
789 return snd_rawmidi_drain_output(rfile->output);
790 case SNDRV_RAWMIDI_STREAM_INPUT:
791 if (rfile->input == NULL)
792 return -EINVAL;
793 return snd_rawmidi_drain_input(rfile->input);
794 default:
795 return -EINVAL;
798 #ifdef CONFIG_SND_DEBUG
799 default:
800 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
801 #endif
803 return -ENOTTY;
806 static int snd_rawmidi_control_ioctl(struct snd_card *card,
807 struct snd_ctl_file *control,
808 unsigned int cmd,
809 unsigned long arg)
811 void __user *argp = (void __user *)arg;
813 switch (cmd) {
814 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
816 int device;
818 if (get_user(device, (int __user *)argp))
819 return -EFAULT;
820 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
821 device = SNDRV_RAWMIDI_DEVICES - 1;
822 mutex_lock(&register_mutex);
823 device = device < 0 ? 0 : device + 1;
824 while (device < SNDRV_RAWMIDI_DEVICES) {
825 if (snd_rawmidi_search(card, device))
826 break;
827 device++;
829 if (device == SNDRV_RAWMIDI_DEVICES)
830 device = -1;
831 mutex_unlock(&register_mutex);
832 if (put_user(device, (int __user *)argp))
833 return -EFAULT;
834 return 0;
836 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
838 int val;
840 if (get_user(val, (int __user *)argp))
841 return -EFAULT;
842 control->prefer_rawmidi_subdevice = val;
843 return 0;
845 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
846 return snd_rawmidi_info_select_user(card, argp);
848 return -ENOIOCTLCMD;
852 * snd_rawmidi_receive - receive the input data from the device
853 * @substream: the rawmidi substream
854 * @buffer: the buffer pointer
855 * @count: the data size to read
857 * Reads the data from the internal buffer.
859 * Returns the size of read data, or a negative error code on failure.
861 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
862 const unsigned char *buffer, int count)
864 unsigned long flags;
865 int result = 0, count1;
866 struct snd_rawmidi_runtime *runtime = substream->runtime;
868 if (!substream->opened)
869 return -EBADFD;
870 if (runtime->buffer == NULL) {
871 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
872 return -EINVAL;
874 spin_lock_irqsave(&runtime->lock, flags);
875 if (count == 1) { /* special case, faster code */
876 substream->bytes++;
877 if (runtime->avail < runtime->buffer_size) {
878 runtime->buffer[runtime->hw_ptr++] = buffer[0];
879 runtime->hw_ptr %= runtime->buffer_size;
880 runtime->avail++;
881 result++;
882 } else {
883 runtime->xruns++;
885 } else {
886 substream->bytes += count;
887 count1 = runtime->buffer_size - runtime->hw_ptr;
888 if (count1 > count)
889 count1 = count;
890 if (count1 > (int)(runtime->buffer_size - runtime->avail))
891 count1 = runtime->buffer_size - runtime->avail;
892 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
893 runtime->hw_ptr += count1;
894 runtime->hw_ptr %= runtime->buffer_size;
895 runtime->avail += count1;
896 count -= count1;
897 result += count1;
898 if (count > 0) {
899 buffer += count1;
900 count1 = count;
901 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
902 count1 = runtime->buffer_size - runtime->avail;
903 runtime->xruns += count - count1;
905 if (count1 > 0) {
906 memcpy(runtime->buffer, buffer, count1);
907 runtime->hw_ptr = count1;
908 runtime->avail += count1;
909 result += count1;
913 if (result > 0) {
914 if (runtime->event)
915 schedule_work(&runtime->event_work);
916 else if (snd_rawmidi_ready(substream))
917 wake_up(&runtime->sleep);
919 spin_unlock_irqrestore(&runtime->lock, flags);
920 return result;
923 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
924 unsigned char __user *userbuf,
925 unsigned char *kernelbuf, long count)
927 unsigned long flags;
928 long result = 0, count1;
929 struct snd_rawmidi_runtime *runtime = substream->runtime;
931 while (count > 0 && runtime->avail) {
932 count1 = runtime->buffer_size - runtime->appl_ptr;
933 if (count1 > count)
934 count1 = count;
935 spin_lock_irqsave(&runtime->lock, flags);
936 if (count1 > (int)runtime->avail)
937 count1 = runtime->avail;
938 if (kernelbuf)
939 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
940 if (userbuf) {
941 spin_unlock_irqrestore(&runtime->lock, flags);
942 if (copy_to_user(userbuf + result,
943 runtime->buffer + runtime->appl_ptr, count1)) {
944 return result > 0 ? result : -EFAULT;
946 spin_lock_irqsave(&runtime->lock, flags);
948 runtime->appl_ptr += count1;
949 runtime->appl_ptr %= runtime->buffer_size;
950 runtime->avail -= count1;
951 spin_unlock_irqrestore(&runtime->lock, flags);
952 result += count1;
953 count -= count1;
955 return result;
958 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
959 unsigned char *buf, long count)
961 snd_rawmidi_input_trigger(substream, 1);
962 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
965 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
966 loff_t *offset)
968 long result;
969 int count1;
970 struct snd_rawmidi_file *rfile;
971 struct snd_rawmidi_substream *substream;
972 struct snd_rawmidi_runtime *runtime;
974 rfile = file->private_data;
975 substream = rfile->input;
976 if (substream == NULL)
977 return -EIO;
978 runtime = substream->runtime;
979 snd_rawmidi_input_trigger(substream, 1);
980 result = 0;
981 while (count > 0) {
982 spin_lock_irq(&runtime->lock);
983 while (!snd_rawmidi_ready(substream)) {
984 wait_queue_t wait;
985 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
986 spin_unlock_irq(&runtime->lock);
987 return result > 0 ? result : -EAGAIN;
989 init_waitqueue_entry(&wait, current);
990 add_wait_queue(&runtime->sleep, &wait);
991 set_current_state(TASK_INTERRUPTIBLE);
992 spin_unlock_irq(&runtime->lock);
993 schedule();
994 remove_wait_queue(&runtime->sleep, &wait);
995 if (signal_pending(current))
996 return result > 0 ? result : -ERESTARTSYS;
997 if (!runtime->avail)
998 return result > 0 ? result : -EIO;
999 spin_lock_irq(&runtime->lock);
1001 spin_unlock_irq(&runtime->lock);
1002 count1 = snd_rawmidi_kernel_read1(substream,
1003 (unsigned char __user *)buf,
1004 NULL/*kernelbuf*/,
1005 count);
1006 if (count1 < 0)
1007 return result > 0 ? result : count1;
1008 result += count1;
1009 buf += count1;
1010 count -= count1;
1012 return result;
1016 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1017 * @substream: the rawmidi substream
1019 * Returns 1 if the internal output buffer is empty, 0 if not.
1021 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1023 struct snd_rawmidi_runtime *runtime = substream->runtime;
1024 int result;
1025 unsigned long flags;
1027 if (runtime->buffer == NULL) {
1028 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1029 return 1;
1031 spin_lock_irqsave(&runtime->lock, flags);
1032 result = runtime->avail >= runtime->buffer_size;
1033 spin_unlock_irqrestore(&runtime->lock, flags);
1034 return result;
1038 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1039 * @substream: the rawmidi substream
1040 * @buffer: the buffer pointer
1041 * @count: data size to transfer
1043 * Copies data from the internal output buffer to the given buffer.
1045 * Call this in the interrupt handler when the midi output is ready,
1046 * and call snd_rawmidi_transmit_ack() after the transmission is
1047 * finished.
1049 * Returns the size of copied data, or a negative error code on failure.
1051 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1052 unsigned char *buffer, int count)
1054 unsigned long flags;
1055 int result, count1;
1056 struct snd_rawmidi_runtime *runtime = substream->runtime;
1058 if (runtime->buffer == NULL) {
1059 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1060 return -EINVAL;
1062 result = 0;
1063 spin_lock_irqsave(&runtime->lock, flags);
1064 if (runtime->avail >= runtime->buffer_size) {
1065 /* warning: lowlevel layer MUST trigger down the hardware */
1066 goto __skip;
1068 if (count == 1) { /* special case, faster code */
1069 *buffer = runtime->buffer[runtime->hw_ptr];
1070 result++;
1071 } else {
1072 count1 = runtime->buffer_size - runtime->hw_ptr;
1073 if (count1 > count)
1074 count1 = count;
1075 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1076 count1 = runtime->buffer_size - runtime->avail;
1077 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1078 count -= count1;
1079 result += count1;
1080 if (count > 0) {
1081 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1082 count = runtime->buffer_size - runtime->avail - count1;
1083 memcpy(buffer + count1, runtime->buffer, count);
1084 result += count;
1087 __skip:
1088 spin_unlock_irqrestore(&runtime->lock, flags);
1089 return result;
1093 * snd_rawmidi_transmit_ack - acknowledge the transmission
1094 * @substream: the rawmidi substream
1095 * @count: the tranferred count
1097 * Advances the hardware pointer for the internal output buffer with
1098 * the given size and updates the condition.
1099 * Call after the transmission is finished.
1101 * Returns the advanced size if successful, or a negative error code on failure.
1103 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1105 unsigned long flags;
1106 struct snd_rawmidi_runtime *runtime = substream->runtime;
1108 if (runtime->buffer == NULL) {
1109 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1110 return -EINVAL;
1112 spin_lock_irqsave(&runtime->lock, flags);
1113 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1114 runtime->hw_ptr += count;
1115 runtime->hw_ptr %= runtime->buffer_size;
1116 runtime->avail += count;
1117 substream->bytes += count;
1118 if (count > 0) {
1119 if (runtime->drain || snd_rawmidi_ready(substream))
1120 wake_up(&runtime->sleep);
1122 spin_unlock_irqrestore(&runtime->lock, flags);
1123 return count;
1127 * snd_rawmidi_transmit - copy from the buffer to the device
1128 * @substream: the rawmidi substream
1129 * @buffer: the buffer pointer
1130 * @count: the data size to transfer
1132 * Copies data from the buffer to the device and advances the pointer.
1134 * Returns the copied size if successful, or a negative error code on failure.
1136 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1137 unsigned char *buffer, int count)
1139 if (!substream->opened)
1140 return -EBADFD;
1141 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1142 if (count < 0)
1143 return count;
1144 return snd_rawmidi_transmit_ack(substream, count);
1147 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1148 const unsigned char __user *userbuf,
1149 const unsigned char *kernelbuf,
1150 long count)
1152 unsigned long flags;
1153 long count1, result;
1154 struct snd_rawmidi_runtime *runtime = substream->runtime;
1156 if (snd_BUG_ON(!kernelbuf && !userbuf))
1157 return -EINVAL;
1158 if (snd_BUG_ON(!runtime->buffer))
1159 return -EINVAL;
1161 result = 0;
1162 spin_lock_irqsave(&runtime->lock, flags);
1163 if (substream->append) {
1164 if ((long)runtime->avail < count) {
1165 spin_unlock_irqrestore(&runtime->lock, flags);
1166 return -EAGAIN;
1169 while (count > 0 && runtime->avail > 0) {
1170 count1 = runtime->buffer_size - runtime->appl_ptr;
1171 if (count1 > count)
1172 count1 = count;
1173 if (count1 > (long)runtime->avail)
1174 count1 = runtime->avail;
1175 if (kernelbuf)
1176 memcpy(runtime->buffer + runtime->appl_ptr,
1177 kernelbuf + result, count1);
1178 else if (userbuf) {
1179 spin_unlock_irqrestore(&runtime->lock, flags);
1180 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1181 userbuf + result, count1)) {
1182 spin_lock_irqsave(&runtime->lock, flags);
1183 result = result > 0 ? result : -EFAULT;
1184 goto __end;
1186 spin_lock_irqsave(&runtime->lock, flags);
1188 runtime->appl_ptr += count1;
1189 runtime->appl_ptr %= runtime->buffer_size;
1190 runtime->avail -= count1;
1191 result += count1;
1192 count -= count1;
1194 __end:
1195 count1 = runtime->avail < runtime->buffer_size;
1196 spin_unlock_irqrestore(&runtime->lock, flags);
1197 if (count1)
1198 snd_rawmidi_output_trigger(substream, 1);
1199 return result;
1202 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1203 const unsigned char *buf, long count)
1205 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1208 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1209 size_t count, loff_t *offset)
1211 long result, timeout;
1212 int count1;
1213 struct snd_rawmidi_file *rfile;
1214 struct snd_rawmidi_runtime *runtime;
1215 struct snd_rawmidi_substream *substream;
1217 rfile = file->private_data;
1218 substream = rfile->output;
1219 runtime = substream->runtime;
1220 /* we cannot put an atomic message to our buffer */
1221 if (substream->append && count > runtime->buffer_size)
1222 return -EIO;
1223 result = 0;
1224 while (count > 0) {
1225 spin_lock_irq(&runtime->lock);
1226 while (!snd_rawmidi_ready_append(substream, count)) {
1227 wait_queue_t wait;
1228 if (file->f_flags & O_NONBLOCK) {
1229 spin_unlock_irq(&runtime->lock);
1230 return result > 0 ? result : -EAGAIN;
1232 init_waitqueue_entry(&wait, current);
1233 add_wait_queue(&runtime->sleep, &wait);
1234 set_current_state(TASK_INTERRUPTIBLE);
1235 spin_unlock_irq(&runtime->lock);
1236 timeout = schedule_timeout(30 * HZ);
1237 remove_wait_queue(&runtime->sleep, &wait);
1238 if (signal_pending(current))
1239 return result > 0 ? result : -ERESTARTSYS;
1240 if (!runtime->avail && !timeout)
1241 return result > 0 ? result : -EIO;
1242 spin_lock_irq(&runtime->lock);
1244 spin_unlock_irq(&runtime->lock);
1245 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1246 if (count1 < 0)
1247 return result > 0 ? result : count1;
1248 result += count1;
1249 buf += count1;
1250 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1251 break;
1252 count -= count1;
1254 if (file->f_flags & O_DSYNC) {
1255 spin_lock_irq(&runtime->lock);
1256 while (runtime->avail != runtime->buffer_size) {
1257 wait_queue_t wait;
1258 unsigned int last_avail = runtime->avail;
1259 init_waitqueue_entry(&wait, current);
1260 add_wait_queue(&runtime->sleep, &wait);
1261 set_current_state(TASK_INTERRUPTIBLE);
1262 spin_unlock_irq(&runtime->lock);
1263 timeout = schedule_timeout(30 * HZ);
1264 remove_wait_queue(&runtime->sleep, &wait);
1265 if (signal_pending(current))
1266 return result > 0 ? result : -ERESTARTSYS;
1267 if (runtime->avail == last_avail && !timeout)
1268 return result > 0 ? result : -EIO;
1269 spin_lock_irq(&runtime->lock);
1271 spin_unlock_irq(&runtime->lock);
1273 return result;
1276 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1278 struct snd_rawmidi_file *rfile;
1279 struct snd_rawmidi_runtime *runtime;
1280 unsigned int mask;
1282 rfile = file->private_data;
1283 if (rfile->input != NULL) {
1284 runtime = rfile->input->runtime;
1285 snd_rawmidi_input_trigger(rfile->input, 1);
1286 poll_wait(file, &runtime->sleep, wait);
1288 if (rfile->output != NULL) {
1289 runtime = rfile->output->runtime;
1290 poll_wait(file, &runtime->sleep, wait);
1292 mask = 0;
1293 if (rfile->input != NULL) {
1294 if (snd_rawmidi_ready(rfile->input))
1295 mask |= POLLIN | POLLRDNORM;
1297 if (rfile->output != NULL) {
1298 if (snd_rawmidi_ready(rfile->output))
1299 mask |= POLLOUT | POLLWRNORM;
1301 return mask;
1306 #ifdef CONFIG_COMPAT
1307 #include "rawmidi_compat.c"
1308 #else
1309 #define snd_rawmidi_ioctl_compat NULL
1310 #endif
1316 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1317 struct snd_info_buffer *buffer)
1319 struct snd_rawmidi *rmidi;
1320 struct snd_rawmidi_substream *substream;
1321 struct snd_rawmidi_runtime *runtime;
1323 rmidi = entry->private_data;
1324 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1325 mutex_lock(&rmidi->open_mutex);
1326 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1327 list_for_each_entry(substream,
1328 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1329 list) {
1330 snd_iprintf(buffer,
1331 "Output %d\n"
1332 " Tx bytes : %lu\n",
1333 substream->number,
1334 (unsigned long) substream->bytes);
1335 if (substream->opened) {
1336 snd_iprintf(buffer,
1337 " Owner PID : %d\n",
1338 pid_vnr(substream->pid));
1339 runtime = substream->runtime;
1340 snd_iprintf(buffer,
1341 " Mode : %s\n"
1342 " Buffer size : %lu\n"
1343 " Avail : %lu\n",
1344 runtime->oss ? "OSS compatible" : "native",
1345 (unsigned long) runtime->buffer_size,
1346 (unsigned long) runtime->avail);
1350 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1351 list_for_each_entry(substream,
1352 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1353 list) {
1354 snd_iprintf(buffer,
1355 "Input %d\n"
1356 " Rx bytes : %lu\n",
1357 substream->number,
1358 (unsigned long) substream->bytes);
1359 if (substream->opened) {
1360 snd_iprintf(buffer,
1361 " Owner PID : %d\n",
1362 pid_vnr(substream->pid));
1363 runtime = substream->runtime;
1364 snd_iprintf(buffer,
1365 " Buffer size : %lu\n"
1366 " Avail : %lu\n"
1367 " Overruns : %lu\n",
1368 (unsigned long) runtime->buffer_size,
1369 (unsigned long) runtime->avail,
1370 (unsigned long) runtime->xruns);
1374 mutex_unlock(&rmidi->open_mutex);
1378 * Register functions
1381 static const struct file_operations snd_rawmidi_f_ops =
1383 .owner = THIS_MODULE,
1384 .read = snd_rawmidi_read,
1385 .write = snd_rawmidi_write,
1386 .open = snd_rawmidi_open,
1387 .release = snd_rawmidi_release,
1388 .llseek = no_llseek,
1389 .poll = snd_rawmidi_poll,
1390 .unlocked_ioctl = snd_rawmidi_ioctl,
1391 .compat_ioctl = snd_rawmidi_ioctl_compat,
1394 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1395 struct snd_rawmidi_str *stream,
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);