[ALSA] dynamic minors (3/6): store device-specific object pointers dynamically
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / core / rawmidi.c
blobe6ee0d813783ab9574630deae48ea354baf39c69
1 /*
2 * Abstract layer for MIDI v1.0 stream
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
23 #include <sound/core.h>
24 #include <linux/major.h>
25 #include <linux/init.h>
26 #include <linux/smp_lock.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/time.h>
30 #include <linux/wait.h>
31 #include <linux/moduleparam.h>
32 #include <linux/delay.h>
33 #include <linux/wait.h>
34 #include <sound/rawmidi.h>
35 #include <sound/info.h>
36 #include <sound/control.h>
37 #include <sound/minors.h>
38 #include <sound/initval.h>
40 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
41 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
42 MODULE_LICENSE("GPL");
44 #ifdef CONFIG_SND_OSSEMUL
45 static int midi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
46 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
47 module_param_array(midi_map, int, NULL, 0444);
48 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
49 module_param_array(amidi_map, int, NULL, 0444);
50 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
51 #endif /* CONFIG_SND_OSSEMUL */
53 static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
54 static int snd_rawmidi_dev_free(struct snd_device *device);
55 static int snd_rawmidi_dev_register(struct snd_device *device);
56 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
57 static int snd_rawmidi_dev_unregister(struct snd_device *device);
59 static LIST_HEAD(snd_rawmidi_devices);
60 static DECLARE_MUTEX(register_mutex);
62 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
64 struct list_head *p;
65 struct snd_rawmidi *rawmidi;
67 list_for_each(p, &snd_rawmidi_devices) {
68 rawmidi = list_entry(p, struct snd_rawmidi, list);
69 if (rawmidi->card == card && rawmidi->device == device)
70 return rawmidi;
72 return NULL;
75 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
77 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
78 case FMODE_WRITE:
79 return SNDRV_RAWMIDI_LFLG_OUTPUT;
80 case FMODE_READ:
81 return SNDRV_RAWMIDI_LFLG_INPUT;
82 default:
83 return SNDRV_RAWMIDI_LFLG_OPEN;
87 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
89 struct snd_rawmidi_runtime *runtime = substream->runtime;
90 return runtime->avail >= runtime->avail_min;
93 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
94 size_t count)
96 struct snd_rawmidi_runtime *runtime = substream->runtime;
97 return runtime->avail >= runtime->avail_min &&
98 (!substream->append || runtime->avail >= count);
101 static void snd_rawmidi_input_event_tasklet(unsigned long data)
103 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
104 substream->runtime->event(substream);
107 static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
109 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
110 substream->ops->trigger(substream, 1);
113 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
115 struct snd_rawmidi_runtime *runtime;
117 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
118 return -ENOMEM;
119 spin_lock_init(&runtime->lock);
120 init_waitqueue_head(&runtime->sleep);
121 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
122 tasklet_init(&runtime->tasklet,
123 snd_rawmidi_input_event_tasklet,
124 (unsigned long)substream);
125 else
126 tasklet_init(&runtime->tasklet,
127 snd_rawmidi_output_trigger_tasklet,
128 (unsigned long)substream);
129 runtime->event = NULL;
130 runtime->buffer_size = PAGE_SIZE;
131 runtime->avail_min = 1;
132 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
133 runtime->avail = 0;
134 else
135 runtime->avail = runtime->buffer_size;
136 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
137 kfree(runtime);
138 return -ENOMEM;
140 runtime->appl_ptr = runtime->hw_ptr = 0;
141 substream->runtime = runtime;
142 return 0;
145 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
147 struct snd_rawmidi_runtime *runtime = substream->runtime;
149 kfree(runtime->buffer);
150 kfree(runtime);
151 substream->runtime = NULL;
152 return 0;
155 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
157 if (up) {
158 tasklet_hi_schedule(&substream->runtime->tasklet);
159 } else {
160 tasklet_kill(&substream->runtime->tasklet);
161 substream->ops->trigger(substream, 0);
165 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
167 substream->ops->trigger(substream, up);
168 if (!up && substream->runtime->event)
169 tasklet_kill(&substream->runtime->tasklet);
172 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
174 unsigned long flags;
175 struct snd_rawmidi_runtime *runtime = substream->runtime;
177 snd_rawmidi_output_trigger(substream, 0);
178 runtime->drain = 0;
179 spin_lock_irqsave(&runtime->lock, flags);
180 runtime->appl_ptr = runtime->hw_ptr = 0;
181 runtime->avail = runtime->buffer_size;
182 spin_unlock_irqrestore(&runtime->lock, flags);
183 return 0;
186 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
188 int err;
189 long timeout;
190 struct snd_rawmidi_runtime *runtime = substream->runtime;
192 err = 0;
193 runtime->drain = 1;
194 timeout = wait_event_interruptible_timeout(runtime->sleep,
195 (runtime->avail >= runtime->buffer_size),
196 10*HZ);
197 if (signal_pending(current))
198 err = -ERESTARTSYS;
199 if (runtime->avail < runtime->buffer_size && !timeout) {
200 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
201 err = -EIO;
203 runtime->drain = 0;
204 if (err != -ERESTARTSYS) {
205 /* we need wait a while to make sure that Tx FIFOs are empty */
206 if (substream->ops->drain)
207 substream->ops->drain(substream);
208 else
209 msleep(50);
210 snd_rawmidi_drop_output(substream);
212 return err;
215 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
217 unsigned long flags;
218 struct snd_rawmidi_runtime *runtime = substream->runtime;
220 snd_rawmidi_input_trigger(substream, 0);
221 runtime->drain = 0;
222 spin_lock_irqsave(&runtime->lock, flags);
223 runtime->appl_ptr = runtime->hw_ptr = 0;
224 runtime->avail = 0;
225 spin_unlock_irqrestore(&runtime->lock, flags);
226 return 0;
229 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
230 int mode, struct snd_rawmidi_file * rfile)
232 struct snd_rawmidi *rmidi;
233 struct list_head *list1, *list2;
234 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
235 struct snd_rawmidi_runtime *input = NULL, *output = NULL;
236 int err;
238 if (rfile)
239 rfile->input = rfile->output = NULL;
240 down(&register_mutex);
241 rmidi = snd_rawmidi_search(card, device);
242 up(&register_mutex);
243 if (rmidi == NULL) {
244 err = -ENODEV;
245 goto __error1;
247 if (!try_module_get(rmidi->card->module)) {
248 err = -EFAULT;
249 goto __error1;
251 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
252 down(&rmidi->open_mutex);
253 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
254 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
255 err = -ENXIO;
256 goto __error;
258 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
259 err = -ENODEV;
260 goto __error;
262 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
263 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
264 err = -EAGAIN;
265 goto __error;
268 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
269 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
270 err = -ENXIO;
271 goto __error;
273 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
274 err = -ENODEV;
275 goto __error;
277 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
278 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
279 err = -EAGAIN;
280 goto __error;
283 list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
284 while (1) {
285 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
286 sinput = NULL;
287 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
288 err = -EAGAIN;
289 goto __error;
291 break;
293 sinput = list_entry(list1, struct snd_rawmidi_substream, list);
294 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
295 goto __nexti;
296 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
297 break;
298 __nexti:
299 list1 = list1->next;
301 list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
302 while (1) {
303 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
304 soutput = NULL;
305 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
306 err = -EAGAIN;
307 goto __error;
309 break;
311 soutput = list_entry(list2, struct snd_rawmidi_substream, list);
312 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
313 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
314 if (soutput->opened && !soutput->append)
315 goto __nexto;
316 } else {
317 if (soutput->opened)
318 goto __nexto;
321 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
322 break;
323 __nexto:
324 list2 = list2->next;
326 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
327 if ((err = snd_rawmidi_runtime_create(sinput)) < 0)
328 goto __error;
329 input = sinput->runtime;
330 if ((err = sinput->ops->open(sinput)) < 0)
331 goto __error;
332 sinput->opened = 1;
333 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
334 } else {
335 sinput = NULL;
337 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
338 if (soutput->opened)
339 goto __skip_output;
340 if ((err = snd_rawmidi_runtime_create(soutput)) < 0) {
341 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
342 sinput->ops->close(sinput);
343 goto __error;
345 output = soutput->runtime;
346 if ((err = soutput->ops->open(soutput)) < 0) {
347 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
348 sinput->ops->close(sinput);
349 goto __error;
351 __skip_output:
352 soutput->opened = 1;
353 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
354 soutput->append = 1;
355 if (soutput->use_count++ == 0)
356 soutput->active_sensing = 1;
357 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
358 } else {
359 soutput = NULL;
361 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
362 up(&rmidi->open_mutex);
363 if (rfile) {
364 rfile->rmidi = rmidi;
365 rfile->input = sinput;
366 rfile->output = soutput;
368 return 0;
370 __error:
371 if (input != NULL)
372 snd_rawmidi_runtime_free(sinput);
373 if (output != NULL)
374 snd_rawmidi_runtime_free(soutput);
375 module_put(rmidi->card->module);
376 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
377 up(&rmidi->open_mutex);
378 __error1:
379 return err;
382 static int snd_rawmidi_open(struct inode *inode, struct file *file)
384 int maj = imajor(inode);
385 struct snd_card *card;
386 int subdevice;
387 unsigned short fflags;
388 int err;
389 struct snd_rawmidi *rmidi;
390 struct snd_rawmidi_file *rawmidi_file;
391 wait_queue_t wait;
392 struct list_head *list;
393 struct snd_ctl_file *kctl;
395 if (maj == snd_major) {
396 rmidi = snd_lookup_minor_data(iminor(inode),
397 SNDRV_DEVICE_TYPE_RAWMIDI);
398 #ifdef CONFIG_SND_OSSEMUL
399 } else if (maj == SOUND_MAJOR) {
400 rmidi = snd_lookup_oss_minor_data(iminor(inode),
401 SNDRV_OSS_DEVICE_TYPE_MIDI);
402 #endif
403 } else
404 return -ENXIO;
406 if (rmidi == NULL)
407 return -ENODEV;
408 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
409 return -EINVAL; /* invalid combination */
410 card = rmidi->card;
411 err = snd_card_file_add(card, file);
412 if (err < 0)
413 return -ENODEV;
414 fflags = snd_rawmidi_file_flags(file);
415 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
416 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
417 fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
418 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
419 if (rawmidi_file == NULL) {
420 snd_card_file_remove(card, file);
421 return -ENOMEM;
423 init_waitqueue_entry(&wait, current);
424 add_wait_queue(&rmidi->open_wait, &wait);
425 down(&rmidi->open_mutex);
426 while (1) {
427 subdevice = -1;
428 down_read(&card->controls_rwsem);
429 list_for_each(list, &card->ctl_files) {
430 kctl = snd_ctl_file(list);
431 if (kctl->pid == current->pid) {
432 subdevice = kctl->prefer_rawmidi_subdevice;
433 break;
436 up_read(&card->controls_rwsem);
437 err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
438 subdevice, fflags, rawmidi_file);
439 if (err >= 0)
440 break;
441 if (err == -EAGAIN) {
442 if (file->f_flags & O_NONBLOCK) {
443 err = -EBUSY;
444 break;
446 } else
447 break;
448 set_current_state(TASK_INTERRUPTIBLE);
449 up(&rmidi->open_mutex);
450 schedule();
451 down(&rmidi->open_mutex);
452 if (signal_pending(current)) {
453 err = -ERESTARTSYS;
454 break;
457 #ifdef CONFIG_SND_OSSEMUL
458 if (rawmidi_file->input && rawmidi_file->input->runtime)
459 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
460 if (rawmidi_file->output && rawmidi_file->output->runtime)
461 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
462 #endif
463 remove_wait_queue(&rmidi->open_wait, &wait);
464 if (err >= 0) {
465 file->private_data = rawmidi_file;
466 } else {
467 snd_card_file_remove(card, file);
468 kfree(rawmidi_file);
470 up(&rmidi->open_mutex);
471 return err;
474 int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
476 struct snd_rawmidi *rmidi;
477 struct snd_rawmidi_substream *substream;
478 struct snd_rawmidi_runtime *runtime;
480 snd_assert(rfile != NULL, return -ENXIO);
481 snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
482 rmidi = rfile->rmidi;
483 down(&rmidi->open_mutex);
484 if (rfile->input != NULL) {
485 substream = rfile->input;
486 rfile->input = NULL;
487 runtime = substream->runtime;
488 snd_rawmidi_input_trigger(substream, 0);
489 substream->ops->close(substream);
490 if (runtime->private_free != NULL)
491 runtime->private_free(substream);
492 snd_rawmidi_runtime_free(substream);
493 substream->opened = 0;
494 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
496 if (rfile->output != NULL) {
497 substream = rfile->output;
498 rfile->output = NULL;
499 if (--substream->use_count == 0) {
500 runtime = substream->runtime;
501 if (substream->active_sensing) {
502 unsigned char buf = 0xfe;
503 /* sending single active sensing message to shut the device up */
504 snd_rawmidi_kernel_write(substream, &buf, 1);
506 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
507 snd_rawmidi_output_trigger(substream, 0);
508 substream->ops->close(substream);
509 if (runtime->private_free != NULL)
510 runtime->private_free(substream);
511 snd_rawmidi_runtime_free(substream);
512 substream->opened = 0;
513 substream->append = 0;
515 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
517 up(&rmidi->open_mutex);
518 module_put(rmidi->card->module);
519 return 0;
522 static int snd_rawmidi_release(struct inode *inode, struct file *file)
524 struct snd_rawmidi_file *rfile;
525 struct snd_rawmidi *rmidi;
526 int err;
528 rfile = file->private_data;
529 err = snd_rawmidi_kernel_release(rfile);
530 rmidi = rfile->rmidi;
531 wake_up(&rmidi->open_wait);
532 kfree(rfile);
533 snd_card_file_remove(rmidi->card, file);
534 return err;
537 int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
538 struct snd_rawmidi_info *info)
540 struct snd_rawmidi *rmidi;
542 if (substream == NULL)
543 return -ENODEV;
544 rmidi = substream->rmidi;
545 memset(info, 0, sizeof(*info));
546 info->card = rmidi->card->number;
547 info->device = rmidi->device;
548 info->subdevice = substream->number;
549 info->stream = substream->stream;
550 info->flags = rmidi->info_flags;
551 strcpy(info->id, rmidi->id);
552 strcpy(info->name, rmidi->name);
553 strcpy(info->subname, substream->name);
554 info->subdevices_count = substream->pstr->substream_count;
555 info->subdevices_avail = (substream->pstr->substream_count -
556 substream->pstr->substream_opened);
557 return 0;
560 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
561 struct snd_rawmidi_info __user * _info)
563 struct snd_rawmidi_info info;
564 int err;
565 if ((err = snd_rawmidi_info(substream, &info)) < 0)
566 return err;
567 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
568 return -EFAULT;
569 return 0;
572 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
574 struct snd_rawmidi *rmidi;
575 struct snd_rawmidi_str *pstr;
576 struct snd_rawmidi_substream *substream;
577 struct list_head *list;
579 down(&register_mutex);
580 rmidi = snd_rawmidi_search(card, info->device);
581 up(&register_mutex);
582 if (!rmidi)
583 return -ENXIO;
584 if (info->stream < 0 || info->stream > 1)
585 return -EINVAL;
586 pstr = &rmidi->streams[info->stream];
587 if (pstr->substream_count == 0)
588 return -ENOENT;
589 if (info->subdevice >= pstr->substream_count)
590 return -ENXIO;
591 list_for_each(list, &pstr->substreams) {
592 substream = list_entry(list, struct snd_rawmidi_substream, list);
593 if ((unsigned int)substream->number == info->subdevice)
594 return snd_rawmidi_info(substream, info);
596 return -ENXIO;
599 static int snd_rawmidi_info_select_user(struct snd_card *card,
600 struct snd_rawmidi_info __user *_info)
602 int err;
603 struct snd_rawmidi_info info;
604 if (get_user(info.device, &_info->device))
605 return -EFAULT;
606 if (get_user(info.stream, &_info->stream))
607 return -EFAULT;
608 if (get_user(info.subdevice, &_info->subdevice))
609 return -EFAULT;
610 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
611 return err;
612 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
613 return -EFAULT;
614 return 0;
617 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
618 struct snd_rawmidi_params * params)
620 char *newbuf;
621 struct snd_rawmidi_runtime *runtime = substream->runtime;
623 if (substream->append && substream->use_count > 1)
624 return -EBUSY;
625 snd_rawmidi_drain_output(substream);
626 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
627 return -EINVAL;
629 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
630 return -EINVAL;
632 if (params->buffer_size != runtime->buffer_size) {
633 if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL)
634 return -ENOMEM;
635 kfree(runtime->buffer);
636 runtime->buffer = newbuf;
637 runtime->buffer_size = params->buffer_size;
639 runtime->avail_min = params->avail_min;
640 substream->active_sensing = !params->no_active_sensing;
641 return 0;
644 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
645 struct snd_rawmidi_params * params)
647 char *newbuf;
648 struct snd_rawmidi_runtime *runtime = substream->runtime;
650 snd_rawmidi_drain_input(substream);
651 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
652 return -EINVAL;
654 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
655 return -EINVAL;
657 if (params->buffer_size != runtime->buffer_size) {
658 if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL)
659 return -ENOMEM;
660 kfree(runtime->buffer);
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 down(&register_mutex);
821 device = device < 0 ? 0 : device + 1;
822 while (device < SNDRV_RAWMIDI_DEVICES) {
823 if (snd_rawmidi_search(card, device))
824 break;
825 device++;
827 if (device == SNDRV_RAWMIDI_DEVICES)
828 device = -1;
829 up(&register_mutex);
830 if (put_user(device, (int __user *)argp))
831 return -EFAULT;
832 return 0;
834 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
836 int val;
838 if (get_user(val, (int __user *)argp))
839 return -EFAULT;
840 control->prefer_rawmidi_subdevice = val;
841 return 0;
843 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
844 return snd_rawmidi_info_select_user(card, argp);
846 return -ENOIOCTLCMD;
850 * snd_rawmidi_receive - receive the input data from the device
851 * @substream: the rawmidi substream
852 * @buffer: the buffer pointer
853 * @count: the data size to read
855 * Reads the data from the internal buffer.
857 * Returns the size of read data, or a negative error code on failure.
859 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
860 const unsigned char *buffer, int count)
862 unsigned long flags;
863 int result = 0, count1;
864 struct snd_rawmidi_runtime *runtime = substream->runtime;
866 if (runtime->buffer == NULL) {
867 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
868 return -EINVAL;
870 spin_lock_irqsave(&runtime->lock, flags);
871 if (count == 1) { /* special case, faster code */
872 substream->bytes++;
873 if (runtime->avail < runtime->buffer_size) {
874 runtime->buffer[runtime->hw_ptr++] = buffer[0];
875 runtime->hw_ptr %= runtime->buffer_size;
876 runtime->avail++;
877 result++;
878 } else {
879 runtime->xruns++;
881 } else {
882 substream->bytes += count;
883 count1 = runtime->buffer_size - runtime->hw_ptr;
884 if (count1 > count)
885 count1 = count;
886 if (count1 > (int)(runtime->buffer_size - runtime->avail))
887 count1 = runtime->buffer_size - runtime->avail;
888 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
889 runtime->hw_ptr += count1;
890 runtime->hw_ptr %= runtime->buffer_size;
891 runtime->avail += count1;
892 count -= count1;
893 result += count1;
894 if (count > 0) {
895 buffer += count1;
896 count1 = count;
897 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
898 count1 = runtime->buffer_size - runtime->avail;
899 runtime->xruns += count - count1;
901 if (count1 > 0) {
902 memcpy(runtime->buffer, buffer, count1);
903 runtime->hw_ptr = count1;
904 runtime->avail += count1;
905 result += count1;
909 if (result > 0) {
910 if (runtime->event)
911 tasklet_hi_schedule(&runtime->tasklet);
912 else if (snd_rawmidi_ready(substream))
913 wake_up(&runtime->sleep);
915 spin_unlock_irqrestore(&runtime->lock, flags);
916 return result;
919 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
920 unsigned char *buf, long count, int kernel)
922 unsigned long flags;
923 long result = 0, count1;
924 struct snd_rawmidi_runtime *runtime = substream->runtime;
926 while (count > 0 && runtime->avail) {
927 count1 = runtime->buffer_size - runtime->appl_ptr;
928 if (count1 > count)
929 count1 = count;
930 spin_lock_irqsave(&runtime->lock, flags);
931 if (count1 > (int)runtime->avail)
932 count1 = runtime->avail;
933 if (kernel) {
934 memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
935 } else {
936 spin_unlock_irqrestore(&runtime->lock, flags);
937 if (copy_to_user((char __user *)buf + result,
938 runtime->buffer + runtime->appl_ptr, count1)) {
939 return result > 0 ? result : -EFAULT;
941 spin_lock_irqsave(&runtime->lock, flags);
943 runtime->appl_ptr += count1;
944 runtime->appl_ptr %= runtime->buffer_size;
945 runtime->avail -= count1;
946 spin_unlock_irqrestore(&runtime->lock, flags);
947 result += count1;
948 count -= count1;
950 return result;
953 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
954 unsigned char *buf, long count)
956 snd_rawmidi_input_trigger(substream, 1);
957 return snd_rawmidi_kernel_read1(substream, buf, count, 1);
960 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
961 loff_t *offset)
963 long result;
964 int count1;
965 struct snd_rawmidi_file *rfile;
966 struct snd_rawmidi_substream *substream;
967 struct snd_rawmidi_runtime *runtime;
969 rfile = file->private_data;
970 substream = rfile->input;
971 if (substream == NULL)
972 return -EIO;
973 runtime = substream->runtime;
974 snd_rawmidi_input_trigger(substream, 1);
975 result = 0;
976 while (count > 0) {
977 spin_lock_irq(&runtime->lock);
978 while (!snd_rawmidi_ready(substream)) {
979 wait_queue_t wait;
980 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
981 spin_unlock_irq(&runtime->lock);
982 return result > 0 ? result : -EAGAIN;
984 init_waitqueue_entry(&wait, current);
985 add_wait_queue(&runtime->sleep, &wait);
986 set_current_state(TASK_INTERRUPTIBLE);
987 spin_unlock_irq(&runtime->lock);
988 schedule();
989 remove_wait_queue(&runtime->sleep, &wait);
990 if (signal_pending(current))
991 return result > 0 ? result : -ERESTARTSYS;
992 if (!runtime->avail)
993 return result > 0 ? result : -EIO;
994 spin_lock_irq(&runtime->lock);
996 spin_unlock_irq(&runtime->lock);
997 count1 = snd_rawmidi_kernel_read1(substream,
998 (unsigned char __force *)buf,
999 count, 0);
1000 if (count1 < 0)
1001 return result > 0 ? result : count1;
1002 result += count1;
1003 buf += count1;
1004 count -= count1;
1006 return result;
1010 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1011 * @substream: the rawmidi substream
1013 * Returns 1 if the internal output buffer is empty, 0 if not.
1015 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1017 struct snd_rawmidi_runtime *runtime = substream->runtime;
1018 int result;
1019 unsigned long flags;
1021 if (runtime->buffer == NULL) {
1022 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1023 return 1;
1025 spin_lock_irqsave(&runtime->lock, flags);
1026 result = runtime->avail >= runtime->buffer_size;
1027 spin_unlock_irqrestore(&runtime->lock, flags);
1028 return result;
1032 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1033 * @substream: the rawmidi substream
1034 * @buffer: the buffer pointer
1035 * @count: data size to transfer
1037 * Copies data from the internal output buffer to the given buffer.
1039 * Call this in the interrupt handler when the midi output is ready,
1040 * and call snd_rawmidi_transmit_ack() after the transmission is
1041 * finished.
1043 * Returns the size of copied data, or a negative error code on failure.
1045 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1046 unsigned char *buffer, int count)
1048 unsigned long flags;
1049 int result, count1;
1050 struct snd_rawmidi_runtime *runtime = substream->runtime;
1052 if (runtime->buffer == NULL) {
1053 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1054 return -EINVAL;
1056 result = 0;
1057 spin_lock_irqsave(&runtime->lock, flags);
1058 if (runtime->avail >= runtime->buffer_size) {
1059 /* warning: lowlevel layer MUST trigger down the hardware */
1060 goto __skip;
1062 if (count == 1) { /* special case, faster code */
1063 *buffer = runtime->buffer[runtime->hw_ptr];
1064 result++;
1065 } else {
1066 count1 = runtime->buffer_size - runtime->hw_ptr;
1067 if (count1 > count)
1068 count1 = count;
1069 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1070 count1 = runtime->buffer_size - runtime->avail;
1071 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1072 count -= count1;
1073 result += count1;
1074 if (count > 0) {
1075 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1076 count = runtime->buffer_size - runtime->avail - count1;
1077 memcpy(buffer + count1, runtime->buffer, count);
1078 result += count;
1081 __skip:
1082 spin_unlock_irqrestore(&runtime->lock, flags);
1083 return result;
1087 * snd_rawmidi_transmit_ack - acknowledge the transmission
1088 * @substream: the rawmidi substream
1089 * @count: the tranferred count
1091 * Advances the hardware pointer for the internal output buffer with
1092 * the given size and updates the condition.
1093 * Call after the transmission is finished.
1095 * Returns the advanced size if successful, or a negative error code on failure.
1097 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1099 unsigned long flags;
1100 struct snd_rawmidi_runtime *runtime = substream->runtime;
1102 if (runtime->buffer == NULL) {
1103 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1104 return -EINVAL;
1106 spin_lock_irqsave(&runtime->lock, flags);
1107 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1108 runtime->hw_ptr += count;
1109 runtime->hw_ptr %= runtime->buffer_size;
1110 runtime->avail += count;
1111 substream->bytes += count;
1112 if (count > 0) {
1113 if (runtime->drain || snd_rawmidi_ready(substream))
1114 wake_up(&runtime->sleep);
1116 spin_unlock_irqrestore(&runtime->lock, flags);
1117 return count;
1121 * snd_rawmidi_transmit - copy from the buffer to the device
1122 * @substream: the rawmidi substream
1123 * @buffer: the buffer pointer
1124 * @count: the data size to transfer
1126 * Copies data from the buffer to the device and advances the pointer.
1128 * Returns the copied size if successful, or a negative error code on failure.
1130 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1131 unsigned char *buffer, int count)
1133 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1134 if (count < 0)
1135 return count;
1136 return snd_rawmidi_transmit_ack(substream, count);
1139 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1140 const unsigned char *buf, long count, int kernel)
1142 unsigned long flags;
1143 long count1, result;
1144 struct snd_rawmidi_runtime *runtime = substream->runtime;
1146 snd_assert(buf != NULL, return -EINVAL);
1147 snd_assert(runtime->buffer != NULL, return -EINVAL);
1149 result = 0;
1150 spin_lock_irqsave(&runtime->lock, flags);
1151 if (substream->append) {
1152 if ((long)runtime->avail < count) {
1153 spin_unlock_irqrestore(&runtime->lock, flags);
1154 return -EAGAIN;
1157 while (count > 0 && runtime->avail > 0) {
1158 count1 = runtime->buffer_size - runtime->appl_ptr;
1159 if (count1 > count)
1160 count1 = count;
1161 if (count1 > (long)runtime->avail)
1162 count1 = runtime->avail;
1163 if (kernel) {
1164 memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1165 } else {
1166 spin_unlock_irqrestore(&runtime->lock, flags);
1167 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1168 (char __user *)buf, count1)) {
1169 spin_lock_irqsave(&runtime->lock, flags);
1170 result = result > 0 ? result : -EFAULT;
1171 goto __end;
1173 spin_lock_irqsave(&runtime->lock, flags);
1175 runtime->appl_ptr += count1;
1176 runtime->appl_ptr %= runtime->buffer_size;
1177 runtime->avail -= count1;
1178 result += count1;
1179 buf += count1;
1180 count -= count1;
1182 __end:
1183 count1 = runtime->avail < runtime->buffer_size;
1184 spin_unlock_irqrestore(&runtime->lock, flags);
1185 if (count1)
1186 snd_rawmidi_output_trigger(substream, 1);
1187 return result;
1190 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1191 const unsigned char *buf, long count)
1193 return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1196 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1197 size_t count, loff_t *offset)
1199 long result, timeout;
1200 int count1;
1201 struct snd_rawmidi_file *rfile;
1202 struct snd_rawmidi_runtime *runtime;
1203 struct snd_rawmidi_substream *substream;
1205 rfile = file->private_data;
1206 substream = rfile->output;
1207 runtime = substream->runtime;
1208 /* we cannot put an atomic message to our buffer */
1209 if (substream->append && count > runtime->buffer_size)
1210 return -EIO;
1211 result = 0;
1212 while (count > 0) {
1213 spin_lock_irq(&runtime->lock);
1214 while (!snd_rawmidi_ready_append(substream, count)) {
1215 wait_queue_t wait;
1216 if (file->f_flags & O_NONBLOCK) {
1217 spin_unlock_irq(&runtime->lock);
1218 return result > 0 ? result : -EAGAIN;
1220 init_waitqueue_entry(&wait, current);
1221 add_wait_queue(&runtime->sleep, &wait);
1222 set_current_state(TASK_INTERRUPTIBLE);
1223 spin_unlock_irq(&runtime->lock);
1224 timeout = schedule_timeout(30 * HZ);
1225 remove_wait_queue(&runtime->sleep, &wait);
1226 if (signal_pending(current))
1227 return result > 0 ? result : -ERESTARTSYS;
1228 if (!runtime->avail && !timeout)
1229 return result > 0 ? result : -EIO;
1230 spin_lock_irq(&runtime->lock);
1232 spin_unlock_irq(&runtime->lock);
1233 count1 = snd_rawmidi_kernel_write1(substream,
1234 (unsigned char __force *)buf,
1235 count, 0);
1236 if (count1 < 0)
1237 return result > 0 ? result : count1;
1238 result += count1;
1239 buf += count1;
1240 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1241 break;
1242 count -= count1;
1244 if (file->f_flags & O_SYNC) {
1245 spin_lock_irq(&runtime->lock);
1246 while (runtime->avail != runtime->buffer_size) {
1247 wait_queue_t wait;
1248 unsigned int last_avail = runtime->avail;
1249 init_waitqueue_entry(&wait, current);
1250 add_wait_queue(&runtime->sleep, &wait);
1251 set_current_state(TASK_INTERRUPTIBLE);
1252 spin_unlock_irq(&runtime->lock);
1253 timeout = schedule_timeout(30 * HZ);
1254 remove_wait_queue(&runtime->sleep, &wait);
1255 if (signal_pending(current))
1256 return result > 0 ? result : -ERESTARTSYS;
1257 if (runtime->avail == last_avail && !timeout)
1258 return result > 0 ? result : -EIO;
1259 spin_lock_irq(&runtime->lock);
1261 spin_unlock_irq(&runtime->lock);
1263 return result;
1266 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1268 struct snd_rawmidi_file *rfile;
1269 struct snd_rawmidi_runtime *runtime;
1270 unsigned int mask;
1272 rfile = file->private_data;
1273 if (rfile->input != NULL) {
1274 runtime = rfile->input->runtime;
1275 snd_rawmidi_input_trigger(rfile->input, 1);
1276 poll_wait(file, &runtime->sleep, wait);
1278 if (rfile->output != NULL) {
1279 runtime = rfile->output->runtime;
1280 poll_wait(file, &runtime->sleep, wait);
1282 mask = 0;
1283 if (rfile->input != NULL) {
1284 if (snd_rawmidi_ready(rfile->input))
1285 mask |= POLLIN | POLLRDNORM;
1287 if (rfile->output != NULL) {
1288 if (snd_rawmidi_ready(rfile->output))
1289 mask |= POLLOUT | POLLWRNORM;
1291 return mask;
1296 #ifdef CONFIG_COMPAT
1297 #include "rawmidi_compat.c"
1298 #else
1299 #define snd_rawmidi_ioctl_compat NULL
1300 #endif
1306 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1307 struct snd_info_buffer *buffer)
1309 struct snd_rawmidi *rmidi;
1310 struct snd_rawmidi_substream *substream;
1311 struct snd_rawmidi_runtime *runtime;
1312 struct list_head *list;
1314 rmidi = entry->private_data;
1315 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1316 down(&rmidi->open_mutex);
1317 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1318 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
1319 substream = list_entry(list, struct snd_rawmidi_substream, list);
1320 snd_iprintf(buffer,
1321 "Output %d\n"
1322 " Tx bytes : %lu\n",
1323 substream->number,
1324 (unsigned long) substream->bytes);
1325 if (substream->opened) {
1326 runtime = substream->runtime;
1327 snd_iprintf(buffer,
1328 " Mode : %s\n"
1329 " Buffer size : %lu\n"
1330 " Avail : %lu\n",
1331 runtime->oss ? "OSS compatible" : "native",
1332 (unsigned long) runtime->buffer_size,
1333 (unsigned long) runtime->avail);
1337 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1338 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
1339 substream = list_entry(list, struct snd_rawmidi_substream, list);
1340 snd_iprintf(buffer,
1341 "Input %d\n"
1342 " Rx bytes : %lu\n",
1343 substream->number,
1344 (unsigned long) substream->bytes);
1345 if (substream->opened) {
1346 runtime = substream->runtime;
1347 snd_iprintf(buffer,
1348 " Buffer size : %lu\n"
1349 " Avail : %lu\n"
1350 " Overruns : %lu\n",
1351 (unsigned long) runtime->buffer_size,
1352 (unsigned long) runtime->avail,
1353 (unsigned long) runtime->xruns);
1357 up(&rmidi->open_mutex);
1361 * Register functions
1364 static struct file_operations snd_rawmidi_f_ops =
1366 .owner = THIS_MODULE,
1367 .read = snd_rawmidi_read,
1368 .write = snd_rawmidi_write,
1369 .open = snd_rawmidi_open,
1370 .release = snd_rawmidi_release,
1371 .poll = snd_rawmidi_poll,
1372 .unlocked_ioctl = snd_rawmidi_ioctl,
1373 .compat_ioctl = snd_rawmidi_ioctl_compat,
1376 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1377 struct snd_rawmidi_str *stream,
1378 int direction,
1379 int count)
1381 struct snd_rawmidi_substream *substream;
1382 int idx;
1384 INIT_LIST_HEAD(&stream->substreams);
1385 for (idx = 0; idx < count; idx++) {
1386 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1387 if (substream == NULL) {
1388 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1389 return -ENOMEM;
1391 substream->stream = direction;
1392 substream->number = idx;
1393 substream->rmidi = rmidi;
1394 substream->pstr = stream;
1395 list_add_tail(&substream->list, &stream->substreams);
1396 stream->substream_count++;
1398 return 0;
1402 * snd_rawmidi_new - create a rawmidi instance
1403 * @card: the card instance
1404 * @id: the id string
1405 * @device: the device index
1406 * @output_count: the number of output streams
1407 * @input_count: the number of input streams
1408 * @rrawmidi: the pointer to store the new rawmidi instance
1410 * Creates a new rawmidi instance.
1411 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1413 * Returns zero if successful, or a negative error code on failure.
1415 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1416 int output_count, int input_count,
1417 struct snd_rawmidi ** rrawmidi)
1419 struct snd_rawmidi *rmidi;
1420 int err;
1421 static struct snd_device_ops ops = {
1422 .dev_free = snd_rawmidi_dev_free,
1423 .dev_register = snd_rawmidi_dev_register,
1424 .dev_disconnect = snd_rawmidi_dev_disconnect,
1425 .dev_unregister = snd_rawmidi_dev_unregister
1428 snd_assert(rrawmidi != NULL, return -EINVAL);
1429 *rrawmidi = NULL;
1430 snd_assert(card != NULL, return -ENXIO);
1431 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1432 if (rmidi == NULL) {
1433 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1434 return -ENOMEM;
1436 rmidi->card = card;
1437 rmidi->device = device;
1438 init_MUTEX(&rmidi->open_mutex);
1439 init_waitqueue_head(&rmidi->open_wait);
1440 if (id != NULL)
1441 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1442 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1443 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1444 SNDRV_RAWMIDI_STREAM_INPUT,
1445 input_count)) < 0) {
1446 snd_rawmidi_free(rmidi);
1447 return err;
1449 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1450 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1451 SNDRV_RAWMIDI_STREAM_OUTPUT,
1452 output_count)) < 0) {
1453 snd_rawmidi_free(rmidi);
1454 return err;
1456 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1457 snd_rawmidi_free(rmidi);
1458 return err;
1460 *rrawmidi = rmidi;
1461 return 0;
1464 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1466 struct snd_rawmidi_substream *substream;
1468 while (!list_empty(&stream->substreams)) {
1469 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1470 list_del(&substream->list);
1471 kfree(substream);
1475 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1477 snd_assert(rmidi != NULL, return -ENXIO);
1478 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1479 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1480 if (rmidi->private_free)
1481 rmidi->private_free(rmidi);
1482 kfree(rmidi);
1483 return 0;
1486 static int snd_rawmidi_dev_free(struct snd_device *device)
1488 struct snd_rawmidi *rmidi = device->device_data;
1489 return snd_rawmidi_free(rmidi);
1492 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1493 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1495 struct snd_rawmidi *rmidi = device->private_data;
1496 rmidi->seq_dev = NULL;
1498 #endif
1500 static int snd_rawmidi_dev_register(struct snd_device *device)
1502 int err;
1503 struct snd_info_entry *entry;
1504 char name[16];
1505 struct snd_rawmidi *rmidi = device->device_data;
1507 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1508 return -ENOMEM;
1509 down(&register_mutex);
1510 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1511 up(&register_mutex);
1512 return -EBUSY;
1514 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1515 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1516 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1517 rmidi->card, rmidi->device,
1518 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1519 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1520 list_del(&rmidi->list);
1521 up(&register_mutex);
1522 return err;
1524 if (rmidi->ops && rmidi->ops->dev_register &&
1525 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1526 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1527 list_del(&rmidi->list);
1528 up(&register_mutex);
1529 return err;
1531 #ifdef CONFIG_SND_OSSEMUL
1532 rmidi->ossreg = 0;
1533 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1534 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1535 rmidi->card, 0, &snd_rawmidi_f_ops,
1536 rmidi, name) < 0) {
1537 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1538 } else {
1539 rmidi->ossreg++;
1540 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1541 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1542 #endif
1545 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1546 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1547 rmidi->card, 1, &snd_rawmidi_f_ops,
1548 rmidi, name) < 0) {
1549 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1550 } else {
1551 rmidi->ossreg++;
1554 #endif /* CONFIG_SND_OSSEMUL */
1555 up(&register_mutex);
1556 sprintf(name, "midi%d", rmidi->device);
1557 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1558 if (entry) {
1559 entry->private_data = rmidi;
1560 entry->c.text.read_size = 1024;
1561 entry->c.text.read = snd_rawmidi_proc_info_read;
1562 if (snd_info_register(entry) < 0) {
1563 snd_info_free_entry(entry);
1564 entry = NULL;
1567 rmidi->proc_entry = entry;
1568 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1569 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1570 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1571 rmidi->seq_dev->private_data = rmidi;
1572 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1573 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1574 snd_device_register(rmidi->card, rmidi->seq_dev);
1577 #endif
1578 return 0;
1581 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1583 struct snd_rawmidi *rmidi = device->device_data;
1585 down(&register_mutex);
1586 list_del_init(&rmidi->list);
1587 up(&register_mutex);
1588 return 0;
1591 static int snd_rawmidi_dev_unregister(struct snd_device *device)
1593 struct snd_rawmidi *rmidi = device->device_data;
1595 snd_assert(rmidi != NULL, return -ENXIO);
1596 down(&register_mutex);
1597 list_del(&rmidi->list);
1598 if (rmidi->proc_entry) {
1599 snd_info_unregister(rmidi->proc_entry);
1600 rmidi->proc_entry = NULL;
1602 #ifdef CONFIG_SND_OSSEMUL
1603 if (rmidi->ossreg) {
1604 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1605 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1606 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1607 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1608 #endif
1610 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1611 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1612 rmidi->ossreg = 0;
1614 #endif /* CONFIG_SND_OSSEMUL */
1615 if (rmidi->ops && rmidi->ops->dev_unregister)
1616 rmidi->ops->dev_unregister(rmidi);
1617 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1618 up(&register_mutex);
1619 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1620 if (rmidi->seq_dev) {
1621 snd_device_free(rmidi->card, rmidi->seq_dev);
1622 rmidi->seq_dev = NULL;
1624 #endif
1625 return snd_rawmidi_free(rmidi);
1629 * snd_rawmidi_set_ops - set the rawmidi operators
1630 * @rmidi: the rawmidi instance
1631 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1632 * @ops: the operator table
1634 * Sets the rawmidi operators for the given stream direction.
1636 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1637 struct snd_rawmidi_ops *ops)
1639 struct list_head *list;
1640 struct snd_rawmidi_substream *substream;
1642 list_for_each(list, &rmidi->streams[stream].substreams) {
1643 substream = list_entry(list, struct snd_rawmidi_substream, list);
1644 substream->ops = ops;
1649 * ENTRY functions
1652 static int __init alsa_rawmidi_init(void)
1655 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1656 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1657 #ifdef CONFIG_SND_OSSEMUL
1658 { int i;
1659 /* check device map table */
1660 for (i = 0; i < SNDRV_CARDS; i++) {
1661 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1662 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1663 midi_map[i] = 0;
1665 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1666 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1667 amidi_map[i] = 1;
1671 #endif /* CONFIG_SND_OSSEMUL */
1672 return 0;
1675 static void __exit alsa_rawmidi_exit(void)
1677 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1678 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1681 module_init(alsa_rawmidi_init)
1682 module_exit(alsa_rawmidi_exit)
1684 EXPORT_SYMBOL(snd_rawmidi_output_params);
1685 EXPORT_SYMBOL(snd_rawmidi_input_params);
1686 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1687 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1688 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1689 EXPORT_SYMBOL(snd_rawmidi_receive);
1690 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1691 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1692 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1693 EXPORT_SYMBOL(snd_rawmidi_transmit);
1694 EXPORT_SYMBOL(snd_rawmidi_new);
1695 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1696 EXPORT_SYMBOL(snd_rawmidi_info);
1697 EXPORT_SYMBOL(snd_rawmidi_info_select);
1698 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1699 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1700 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1701 EXPORT_SYMBOL(snd_rawmidi_kernel_write);