x86: arch/x86/mm/init_32.c cleanup
[wrt350n-kernel.git] / sound / core / rawmidi.c
blobb8e700b94e5962b24a9355be54f2548753633eb7
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/driver.h>
23 #include <sound/core.h>
24 #include <linux/major.h>
25 #include <linux/init.h>
26 #include <linux/sched.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_tasklet(unsigned long data)
98 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
99 substream->runtime->event(substream);
102 static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
104 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
105 substream->ops->trigger(substream, 1);
108 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
110 struct snd_rawmidi_runtime *runtime;
112 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
113 return -ENOMEM;
114 spin_lock_init(&runtime->lock);
115 init_waitqueue_head(&runtime->sleep);
116 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
117 tasklet_init(&runtime->tasklet,
118 snd_rawmidi_input_event_tasklet,
119 (unsigned long)substream);
120 else
121 tasklet_init(&runtime->tasklet,
122 snd_rawmidi_output_trigger_tasklet,
123 (unsigned long)substream);
124 runtime->event = NULL;
125 runtime->buffer_size = PAGE_SIZE;
126 runtime->avail_min = 1;
127 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
128 runtime->avail = 0;
129 else
130 runtime->avail = runtime->buffer_size;
131 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
132 kfree(runtime);
133 return -ENOMEM;
135 runtime->appl_ptr = runtime->hw_ptr = 0;
136 substream->runtime = runtime;
137 return 0;
140 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
142 struct snd_rawmidi_runtime *runtime = substream->runtime;
144 kfree(runtime->buffer);
145 kfree(runtime);
146 substream->runtime = NULL;
147 return 0;
150 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
152 if (up) {
153 tasklet_hi_schedule(&substream->runtime->tasklet);
154 } else {
155 tasklet_kill(&substream->runtime->tasklet);
156 substream->ops->trigger(substream, 0);
160 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
162 substream->ops->trigger(substream, up);
163 if (!up && substream->runtime->event)
164 tasklet_kill(&substream->runtime->tasklet);
167 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
169 unsigned long flags;
170 struct snd_rawmidi_runtime *runtime = substream->runtime;
172 snd_rawmidi_output_trigger(substream, 0);
173 runtime->drain = 0;
174 spin_lock_irqsave(&runtime->lock, flags);
175 runtime->appl_ptr = runtime->hw_ptr = 0;
176 runtime->avail = runtime->buffer_size;
177 spin_unlock_irqrestore(&runtime->lock, flags);
178 return 0;
181 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
183 int err;
184 long timeout;
185 struct snd_rawmidi_runtime *runtime = substream->runtime;
187 err = 0;
188 runtime->drain = 1;
189 timeout = wait_event_interruptible_timeout(runtime->sleep,
190 (runtime->avail >= runtime->buffer_size),
191 10*HZ);
192 if (signal_pending(current))
193 err = -ERESTARTSYS;
194 if (runtime->avail < runtime->buffer_size && !timeout) {
195 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
196 err = -EIO;
198 runtime->drain = 0;
199 if (err != -ERESTARTSYS) {
200 /* we need wait a while to make sure that Tx FIFOs are empty */
201 if (substream->ops->drain)
202 substream->ops->drain(substream);
203 else
204 msleep(50);
205 snd_rawmidi_drop_output(substream);
207 return err;
210 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
212 unsigned long flags;
213 struct snd_rawmidi_runtime *runtime = substream->runtime;
215 snd_rawmidi_input_trigger(substream, 0);
216 runtime->drain = 0;
217 spin_lock_irqsave(&runtime->lock, flags);
218 runtime->appl_ptr = runtime->hw_ptr = 0;
219 runtime->avail = 0;
220 spin_unlock_irqrestore(&runtime->lock, flags);
221 return 0;
224 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
225 int mode, struct snd_rawmidi_file * rfile)
227 struct snd_rawmidi *rmidi;
228 struct list_head *list1, *list2;
229 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
230 struct snd_rawmidi_runtime *input = NULL, *output = NULL;
231 int err;
233 if (rfile)
234 rfile->input = rfile->output = NULL;
235 mutex_lock(&register_mutex);
236 rmidi = snd_rawmidi_search(card, device);
237 mutex_unlock(&register_mutex);
238 if (rmidi == NULL) {
239 err = -ENODEV;
240 goto __error1;
242 if (!try_module_get(rmidi->card->module)) {
243 err = -EFAULT;
244 goto __error1;
246 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
247 mutex_lock(&rmidi->open_mutex);
248 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
249 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
250 err = -ENXIO;
251 goto __error;
253 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
254 err = -ENODEV;
255 goto __error;
257 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
258 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
259 err = -EAGAIN;
260 goto __error;
263 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
264 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
265 err = -ENXIO;
266 goto __error;
268 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
269 err = -ENODEV;
270 goto __error;
272 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
273 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
274 err = -EAGAIN;
275 goto __error;
278 list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
279 while (1) {
280 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
281 sinput = NULL;
282 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
283 err = -EAGAIN;
284 goto __error;
286 break;
288 sinput = list_entry(list1, struct snd_rawmidi_substream, list);
289 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
290 goto __nexti;
291 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
292 break;
293 __nexti:
294 list1 = list1->next;
296 list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
297 while (1) {
298 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
299 soutput = NULL;
300 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
301 err = -EAGAIN;
302 goto __error;
304 break;
306 soutput = list_entry(list2, struct snd_rawmidi_substream, list);
307 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
308 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
309 if (soutput->opened && !soutput->append)
310 goto __nexto;
311 } else {
312 if (soutput->opened)
313 goto __nexto;
316 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
317 break;
318 __nexto:
319 list2 = list2->next;
321 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
322 if ((err = snd_rawmidi_runtime_create(sinput)) < 0)
323 goto __error;
324 input = sinput->runtime;
325 if ((err = sinput->ops->open(sinput)) < 0)
326 goto __error;
327 sinput->opened = 1;
328 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
329 } else {
330 sinput = NULL;
332 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
333 if (soutput->opened)
334 goto __skip_output;
335 if ((err = snd_rawmidi_runtime_create(soutput)) < 0) {
336 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
337 sinput->ops->close(sinput);
338 goto __error;
340 output = soutput->runtime;
341 if ((err = soutput->ops->open(soutput)) < 0) {
342 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
343 sinput->ops->close(sinput);
344 goto __error;
346 __skip_output:
347 soutput->opened = 1;
348 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
349 soutput->append = 1;
350 if (soutput->use_count++ == 0)
351 soutput->active_sensing = 1;
352 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
353 } else {
354 soutput = NULL;
356 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
357 mutex_unlock(&rmidi->open_mutex);
358 if (rfile) {
359 rfile->rmidi = rmidi;
360 rfile->input = sinput;
361 rfile->output = soutput;
363 return 0;
365 __error:
366 if (input != NULL)
367 snd_rawmidi_runtime_free(sinput);
368 if (output != NULL)
369 snd_rawmidi_runtime_free(soutput);
370 module_put(rmidi->card->module);
371 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
372 mutex_unlock(&rmidi->open_mutex);
373 __error1:
374 return err;
377 static int snd_rawmidi_open(struct inode *inode, struct file *file)
379 int maj = imajor(inode);
380 struct snd_card *card;
381 int subdevice;
382 unsigned short fflags;
383 int err;
384 struct snd_rawmidi *rmidi;
385 struct snd_rawmidi_file *rawmidi_file;
386 wait_queue_t wait;
387 struct snd_ctl_file *kctl;
389 if (maj == snd_major) {
390 rmidi = snd_lookup_minor_data(iminor(inode),
391 SNDRV_DEVICE_TYPE_RAWMIDI);
392 #ifdef CONFIG_SND_OSSEMUL
393 } else if (maj == SOUND_MAJOR) {
394 rmidi = snd_lookup_oss_minor_data(iminor(inode),
395 SNDRV_OSS_DEVICE_TYPE_MIDI);
396 #endif
397 } else
398 return -ENXIO;
400 if (rmidi == NULL)
401 return -ENODEV;
402 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
403 return -EINVAL; /* invalid combination */
404 card = rmidi->card;
405 err = snd_card_file_add(card, file);
406 if (err < 0)
407 return -ENODEV;
408 fflags = snd_rawmidi_file_flags(file);
409 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
410 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
411 fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
412 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
413 if (rawmidi_file == NULL) {
414 snd_card_file_remove(card, file);
415 return -ENOMEM;
417 init_waitqueue_entry(&wait, current);
418 add_wait_queue(&rmidi->open_wait, &wait);
419 mutex_lock(&rmidi->open_mutex);
420 while (1) {
421 subdevice = -1;
422 down_read(&card->controls_rwsem);
423 list_for_each_entry(kctl, &card->ctl_files, list) {
424 if (kctl->pid == current->pid) {
425 subdevice = kctl->prefer_rawmidi_subdevice;
426 if (subdevice != -1)
427 break;
430 up_read(&card->controls_rwsem);
431 err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
432 subdevice, fflags, rawmidi_file);
433 if (err >= 0)
434 break;
435 if (err == -EAGAIN) {
436 if (file->f_flags & O_NONBLOCK) {
437 err = -EBUSY;
438 break;
440 } else
441 break;
442 set_current_state(TASK_INTERRUPTIBLE);
443 mutex_unlock(&rmidi->open_mutex);
444 schedule();
445 mutex_lock(&rmidi->open_mutex);
446 if (signal_pending(current)) {
447 err = -ERESTARTSYS;
448 break;
451 #ifdef CONFIG_SND_OSSEMUL
452 if (rawmidi_file->input && rawmidi_file->input->runtime)
453 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
454 if (rawmidi_file->output && rawmidi_file->output->runtime)
455 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
456 #endif
457 remove_wait_queue(&rmidi->open_wait, &wait);
458 if (err >= 0) {
459 file->private_data = rawmidi_file;
460 } else {
461 snd_card_file_remove(card, file);
462 kfree(rawmidi_file);
464 mutex_unlock(&rmidi->open_mutex);
465 return err;
468 int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
470 struct snd_rawmidi *rmidi;
471 struct snd_rawmidi_substream *substream;
472 struct snd_rawmidi_runtime *runtime;
474 snd_assert(rfile != NULL, return -ENXIO);
475 snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
476 rmidi = rfile->rmidi;
477 mutex_lock(&rmidi->open_mutex);
478 if (rfile->input != NULL) {
479 substream = rfile->input;
480 rfile->input = NULL;
481 runtime = substream->runtime;
482 snd_rawmidi_input_trigger(substream, 0);
483 substream->ops->close(substream);
484 if (runtime->private_free != NULL)
485 runtime->private_free(substream);
486 snd_rawmidi_runtime_free(substream);
487 substream->opened = 0;
488 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
490 if (rfile->output != NULL) {
491 substream = rfile->output;
492 rfile->output = NULL;
493 if (--substream->use_count == 0) {
494 runtime = substream->runtime;
495 if (substream->active_sensing) {
496 unsigned char buf = 0xfe;
497 /* sending single active sensing message to shut the device up */
498 snd_rawmidi_kernel_write(substream, &buf, 1);
500 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
501 snd_rawmidi_output_trigger(substream, 0);
502 substream->ops->close(substream);
503 if (runtime->private_free != NULL)
504 runtime->private_free(substream);
505 snd_rawmidi_runtime_free(substream);
506 substream->opened = 0;
507 substream->append = 0;
509 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
511 mutex_unlock(&rmidi->open_mutex);
512 module_put(rmidi->card->module);
513 return 0;
516 static int snd_rawmidi_release(struct inode *inode, struct file *file)
518 struct snd_rawmidi_file *rfile;
519 struct snd_rawmidi *rmidi;
520 int err;
522 rfile = file->private_data;
523 err = snd_rawmidi_kernel_release(rfile);
524 rmidi = rfile->rmidi;
525 wake_up(&rmidi->open_wait);
526 kfree(rfile);
527 snd_card_file_remove(rmidi->card, file);
528 return err;
531 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
532 struct snd_rawmidi_info *info)
534 struct snd_rawmidi *rmidi;
536 if (substream == NULL)
537 return -ENODEV;
538 rmidi = substream->rmidi;
539 memset(info, 0, sizeof(*info));
540 info->card = rmidi->card->number;
541 info->device = rmidi->device;
542 info->subdevice = substream->number;
543 info->stream = substream->stream;
544 info->flags = rmidi->info_flags;
545 strcpy(info->id, rmidi->id);
546 strcpy(info->name, rmidi->name);
547 strcpy(info->subname, substream->name);
548 info->subdevices_count = substream->pstr->substream_count;
549 info->subdevices_avail = (substream->pstr->substream_count -
550 substream->pstr->substream_opened);
551 return 0;
554 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
555 struct snd_rawmidi_info __user * _info)
557 struct snd_rawmidi_info info;
558 int err;
559 if ((err = snd_rawmidi_info(substream, &info)) < 0)
560 return err;
561 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
562 return -EFAULT;
563 return 0;
566 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
568 struct snd_rawmidi *rmidi;
569 struct snd_rawmidi_str *pstr;
570 struct snd_rawmidi_substream *substream;
572 mutex_lock(&register_mutex);
573 rmidi = snd_rawmidi_search(card, info->device);
574 mutex_unlock(&register_mutex);
575 if (!rmidi)
576 return -ENXIO;
577 if (info->stream < 0 || info->stream > 1)
578 return -EINVAL;
579 pstr = &rmidi->streams[info->stream];
580 if (pstr->substream_count == 0)
581 return -ENOENT;
582 if (info->subdevice >= pstr->substream_count)
583 return -ENXIO;
584 list_for_each_entry(substream, &pstr->substreams, list) {
585 if ((unsigned int)substream->number == info->subdevice)
586 return snd_rawmidi_info(substream, info);
588 return -ENXIO;
591 static int snd_rawmidi_info_select_user(struct snd_card *card,
592 struct snd_rawmidi_info __user *_info)
594 int err;
595 struct snd_rawmidi_info info;
596 if (get_user(info.device, &_info->device))
597 return -EFAULT;
598 if (get_user(info.stream, &_info->stream))
599 return -EFAULT;
600 if (get_user(info.subdevice, &_info->subdevice))
601 return -EFAULT;
602 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
603 return err;
604 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
605 return -EFAULT;
606 return 0;
609 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
610 struct snd_rawmidi_params * params)
612 char *newbuf;
613 struct snd_rawmidi_runtime *runtime = substream->runtime;
615 if (substream->append && substream->use_count > 1)
616 return -EBUSY;
617 snd_rawmidi_drain_output(substream);
618 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
619 return -EINVAL;
621 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
622 return -EINVAL;
624 if (params->buffer_size != runtime->buffer_size) {
625 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
626 if (!newbuf)
627 return -ENOMEM;
628 kfree(runtime->buffer);
629 runtime->buffer = newbuf;
630 runtime->buffer_size = params->buffer_size;
631 runtime->avail = runtime->buffer_size;
633 runtime->avail_min = params->avail_min;
634 substream->active_sensing = !params->no_active_sensing;
635 return 0;
638 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
639 struct snd_rawmidi_params * params)
641 char *newbuf;
642 struct snd_rawmidi_runtime *runtime = substream->runtime;
644 snd_rawmidi_drain_input(substream);
645 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
646 return -EINVAL;
648 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
649 return -EINVAL;
651 if (params->buffer_size != runtime->buffer_size) {
652 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
653 if (!newbuf)
654 return -ENOMEM;
655 kfree(runtime->buffer);
656 runtime->buffer = newbuf;
657 runtime->buffer_size = params->buffer_size;
659 runtime->avail_min = params->avail_min;
660 return 0;
663 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
664 struct snd_rawmidi_status * status)
666 struct snd_rawmidi_runtime *runtime = substream->runtime;
668 memset(status, 0, sizeof(*status));
669 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
670 spin_lock_irq(&runtime->lock);
671 status->avail = runtime->avail;
672 spin_unlock_irq(&runtime->lock);
673 return 0;
676 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
677 struct snd_rawmidi_status * status)
679 struct snd_rawmidi_runtime *runtime = substream->runtime;
681 memset(status, 0, sizeof(*status));
682 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
683 spin_lock_irq(&runtime->lock);
684 status->avail = runtime->avail;
685 status->xruns = runtime->xruns;
686 runtime->xruns = 0;
687 spin_unlock_irq(&runtime->lock);
688 return 0;
691 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
693 struct snd_rawmidi_file *rfile;
694 void __user *argp = (void __user *)arg;
696 rfile = file->private_data;
697 if (((cmd >> 8) & 0xff) != 'W')
698 return -ENOTTY;
699 switch (cmd) {
700 case SNDRV_RAWMIDI_IOCTL_PVERSION:
701 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
702 case SNDRV_RAWMIDI_IOCTL_INFO:
704 int stream;
705 struct snd_rawmidi_info __user *info = argp;
706 if (get_user(stream, &info->stream))
707 return -EFAULT;
708 switch (stream) {
709 case SNDRV_RAWMIDI_STREAM_INPUT:
710 return snd_rawmidi_info_user(rfile->input, info);
711 case SNDRV_RAWMIDI_STREAM_OUTPUT:
712 return snd_rawmidi_info_user(rfile->output, info);
713 default:
714 return -EINVAL;
717 case SNDRV_RAWMIDI_IOCTL_PARAMS:
719 struct snd_rawmidi_params params;
720 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
721 return -EFAULT;
722 switch (params.stream) {
723 case SNDRV_RAWMIDI_STREAM_OUTPUT:
724 if (rfile->output == NULL)
725 return -EINVAL;
726 return snd_rawmidi_output_params(rfile->output, &params);
727 case SNDRV_RAWMIDI_STREAM_INPUT:
728 if (rfile->input == NULL)
729 return -EINVAL;
730 return snd_rawmidi_input_params(rfile->input, &params);
731 default:
732 return -EINVAL;
735 case SNDRV_RAWMIDI_IOCTL_STATUS:
737 int err = 0;
738 struct snd_rawmidi_status status;
739 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
740 return -EFAULT;
741 switch (status.stream) {
742 case SNDRV_RAWMIDI_STREAM_OUTPUT:
743 if (rfile->output == NULL)
744 return -EINVAL;
745 err = snd_rawmidi_output_status(rfile->output, &status);
746 break;
747 case SNDRV_RAWMIDI_STREAM_INPUT:
748 if (rfile->input == NULL)
749 return -EINVAL;
750 err = snd_rawmidi_input_status(rfile->input, &status);
751 break;
752 default:
753 return -EINVAL;
755 if (err < 0)
756 return err;
757 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
758 return -EFAULT;
759 return 0;
761 case SNDRV_RAWMIDI_IOCTL_DROP:
763 int val;
764 if (get_user(val, (int __user *) argp))
765 return -EFAULT;
766 switch (val) {
767 case SNDRV_RAWMIDI_STREAM_OUTPUT:
768 if (rfile->output == NULL)
769 return -EINVAL;
770 return snd_rawmidi_drop_output(rfile->output);
771 default:
772 return -EINVAL;
775 case SNDRV_RAWMIDI_IOCTL_DRAIN:
777 int val;
778 if (get_user(val, (int __user *) argp))
779 return -EFAULT;
780 switch (val) {
781 case SNDRV_RAWMIDI_STREAM_OUTPUT:
782 if (rfile->output == NULL)
783 return -EINVAL;
784 return snd_rawmidi_drain_output(rfile->output);
785 case SNDRV_RAWMIDI_STREAM_INPUT:
786 if (rfile->input == NULL)
787 return -EINVAL;
788 return snd_rawmidi_drain_input(rfile->input);
789 default:
790 return -EINVAL;
793 #ifdef CONFIG_SND_DEBUG
794 default:
795 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
796 #endif
798 return -ENOTTY;
801 static int snd_rawmidi_control_ioctl(struct snd_card *card,
802 struct snd_ctl_file *control,
803 unsigned int cmd,
804 unsigned long arg)
806 void __user *argp = (void __user *)arg;
808 switch (cmd) {
809 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
811 int device;
813 if (get_user(device, (int __user *)argp))
814 return -EFAULT;
815 mutex_lock(&register_mutex);
816 device = device < 0 ? 0 : device + 1;
817 while (device < SNDRV_RAWMIDI_DEVICES) {
818 if (snd_rawmidi_search(card, device))
819 break;
820 device++;
822 if (device == SNDRV_RAWMIDI_DEVICES)
823 device = -1;
824 mutex_unlock(&register_mutex);
825 if (put_user(device, (int __user *)argp))
826 return -EFAULT;
827 return 0;
829 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
831 int val;
833 if (get_user(val, (int __user *)argp))
834 return -EFAULT;
835 control->prefer_rawmidi_subdevice = val;
836 return 0;
838 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
839 return snd_rawmidi_info_select_user(card, argp);
841 return -ENOIOCTLCMD;
845 * snd_rawmidi_receive - receive the input data from the device
846 * @substream: the rawmidi substream
847 * @buffer: the buffer pointer
848 * @count: the data size to read
850 * Reads the data from the internal buffer.
852 * Returns the size of read data, or a negative error code on failure.
854 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
855 const unsigned char *buffer, int count)
857 unsigned long flags;
858 int result = 0, count1;
859 struct snd_rawmidi_runtime *runtime = substream->runtime;
861 if (runtime->buffer == NULL) {
862 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
863 return -EINVAL;
865 spin_lock_irqsave(&runtime->lock, flags);
866 if (count == 1) { /* special case, faster code */
867 substream->bytes++;
868 if (runtime->avail < runtime->buffer_size) {
869 runtime->buffer[runtime->hw_ptr++] = buffer[0];
870 runtime->hw_ptr %= runtime->buffer_size;
871 runtime->avail++;
872 result++;
873 } else {
874 runtime->xruns++;
876 } else {
877 substream->bytes += count;
878 count1 = runtime->buffer_size - runtime->hw_ptr;
879 if (count1 > count)
880 count1 = count;
881 if (count1 > (int)(runtime->buffer_size - runtime->avail))
882 count1 = runtime->buffer_size - runtime->avail;
883 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
884 runtime->hw_ptr += count1;
885 runtime->hw_ptr %= runtime->buffer_size;
886 runtime->avail += count1;
887 count -= count1;
888 result += count1;
889 if (count > 0) {
890 buffer += count1;
891 count1 = count;
892 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
893 count1 = runtime->buffer_size - runtime->avail;
894 runtime->xruns += count - count1;
896 if (count1 > 0) {
897 memcpy(runtime->buffer, buffer, count1);
898 runtime->hw_ptr = count1;
899 runtime->avail += count1;
900 result += count1;
904 if (result > 0) {
905 if (runtime->event)
906 tasklet_hi_schedule(&runtime->tasklet);
907 else if (snd_rawmidi_ready(substream))
908 wake_up(&runtime->sleep);
910 spin_unlock_irqrestore(&runtime->lock, flags);
911 return result;
914 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
915 unsigned char *buf, long count, int kernel)
917 unsigned long flags;
918 long result = 0, count1;
919 struct snd_rawmidi_runtime *runtime = substream->runtime;
921 while (count > 0 && runtime->avail) {
922 count1 = runtime->buffer_size - runtime->appl_ptr;
923 if (count1 > count)
924 count1 = count;
925 spin_lock_irqsave(&runtime->lock, flags);
926 if (count1 > (int)runtime->avail)
927 count1 = runtime->avail;
928 if (kernel) {
929 memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
930 } else {
931 spin_unlock_irqrestore(&runtime->lock, flags);
932 if (copy_to_user((char __user *)buf + result,
933 runtime->buffer + runtime->appl_ptr, count1)) {
934 return result > 0 ? result : -EFAULT;
936 spin_lock_irqsave(&runtime->lock, flags);
938 runtime->appl_ptr += count1;
939 runtime->appl_ptr %= runtime->buffer_size;
940 runtime->avail -= count1;
941 spin_unlock_irqrestore(&runtime->lock, flags);
942 result += count1;
943 count -= count1;
945 return result;
948 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
949 unsigned char *buf, long count)
951 snd_rawmidi_input_trigger(substream, 1);
952 return snd_rawmidi_kernel_read1(substream, buf, count, 1);
955 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
956 loff_t *offset)
958 long result;
959 int count1;
960 struct snd_rawmidi_file *rfile;
961 struct snd_rawmidi_substream *substream;
962 struct snd_rawmidi_runtime *runtime;
964 rfile = file->private_data;
965 substream = rfile->input;
966 if (substream == NULL)
967 return -EIO;
968 runtime = substream->runtime;
969 snd_rawmidi_input_trigger(substream, 1);
970 result = 0;
971 while (count > 0) {
972 spin_lock_irq(&runtime->lock);
973 while (!snd_rawmidi_ready(substream)) {
974 wait_queue_t wait;
975 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
976 spin_unlock_irq(&runtime->lock);
977 return result > 0 ? result : -EAGAIN;
979 init_waitqueue_entry(&wait, current);
980 add_wait_queue(&runtime->sleep, &wait);
981 set_current_state(TASK_INTERRUPTIBLE);
982 spin_unlock_irq(&runtime->lock);
983 schedule();
984 remove_wait_queue(&runtime->sleep, &wait);
985 if (signal_pending(current))
986 return result > 0 ? result : -ERESTARTSYS;
987 if (!runtime->avail)
988 return result > 0 ? result : -EIO;
989 spin_lock_irq(&runtime->lock);
991 spin_unlock_irq(&runtime->lock);
992 count1 = snd_rawmidi_kernel_read1(substream,
993 (unsigned char __force *)buf,
994 count, 0);
995 if (count1 < 0)
996 return result > 0 ? result : count1;
997 result += count1;
998 buf += count1;
999 count -= count1;
1001 return result;
1005 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1006 * @substream: the rawmidi substream
1008 * Returns 1 if the internal output buffer is empty, 0 if not.
1010 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1012 struct snd_rawmidi_runtime *runtime = substream->runtime;
1013 int result;
1014 unsigned long flags;
1016 if (runtime->buffer == NULL) {
1017 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1018 return 1;
1020 spin_lock_irqsave(&runtime->lock, flags);
1021 result = runtime->avail >= runtime->buffer_size;
1022 spin_unlock_irqrestore(&runtime->lock, flags);
1023 return result;
1027 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1028 * @substream: the rawmidi substream
1029 * @buffer: the buffer pointer
1030 * @count: data size to transfer
1032 * Copies data from the internal output buffer to the given buffer.
1034 * Call this in the interrupt handler when the midi output is ready,
1035 * and call snd_rawmidi_transmit_ack() after the transmission is
1036 * finished.
1038 * Returns the size of copied data, or a negative error code on failure.
1040 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1041 unsigned char *buffer, int count)
1043 unsigned long flags;
1044 int result, count1;
1045 struct snd_rawmidi_runtime *runtime = substream->runtime;
1047 if (runtime->buffer == NULL) {
1048 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1049 return -EINVAL;
1051 result = 0;
1052 spin_lock_irqsave(&runtime->lock, flags);
1053 if (runtime->avail >= runtime->buffer_size) {
1054 /* warning: lowlevel layer MUST trigger down the hardware */
1055 goto __skip;
1057 if (count == 1) { /* special case, faster code */
1058 *buffer = runtime->buffer[runtime->hw_ptr];
1059 result++;
1060 } else {
1061 count1 = runtime->buffer_size - runtime->hw_ptr;
1062 if (count1 > count)
1063 count1 = count;
1064 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1065 count1 = runtime->buffer_size - runtime->avail;
1066 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1067 count -= count1;
1068 result += count1;
1069 if (count > 0) {
1070 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1071 count = runtime->buffer_size - runtime->avail - count1;
1072 memcpy(buffer + count1, runtime->buffer, count);
1073 result += count;
1076 __skip:
1077 spin_unlock_irqrestore(&runtime->lock, flags);
1078 return result;
1082 * snd_rawmidi_transmit_ack - acknowledge the transmission
1083 * @substream: the rawmidi substream
1084 * @count: the tranferred count
1086 * Advances the hardware pointer for the internal output buffer with
1087 * the given size and updates the condition.
1088 * Call after the transmission is finished.
1090 * Returns the advanced size if successful, or a negative error code on failure.
1092 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1094 unsigned long flags;
1095 struct snd_rawmidi_runtime *runtime = substream->runtime;
1097 if (runtime->buffer == NULL) {
1098 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1099 return -EINVAL;
1101 spin_lock_irqsave(&runtime->lock, flags);
1102 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1103 runtime->hw_ptr += count;
1104 runtime->hw_ptr %= runtime->buffer_size;
1105 runtime->avail += count;
1106 substream->bytes += count;
1107 if (count > 0) {
1108 if (runtime->drain || snd_rawmidi_ready(substream))
1109 wake_up(&runtime->sleep);
1111 spin_unlock_irqrestore(&runtime->lock, flags);
1112 return count;
1116 * snd_rawmidi_transmit - copy from the buffer to the device
1117 * @substream: the rawmidi substream
1118 * @buffer: the buffer pointer
1119 * @count: the data size to transfer
1121 * Copies data from the buffer to the device and advances the pointer.
1123 * Returns the copied size if successful, or a negative error code on failure.
1125 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1126 unsigned char *buffer, int count)
1128 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1129 if (count < 0)
1130 return count;
1131 return snd_rawmidi_transmit_ack(substream, count);
1134 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1135 const unsigned char *buf, long count, int kernel)
1137 unsigned long flags;
1138 long count1, result;
1139 struct snd_rawmidi_runtime *runtime = substream->runtime;
1141 snd_assert(buf != NULL, return -EINVAL);
1142 snd_assert(runtime->buffer != NULL, return -EINVAL);
1144 result = 0;
1145 spin_lock_irqsave(&runtime->lock, flags);
1146 if (substream->append) {
1147 if ((long)runtime->avail < count) {
1148 spin_unlock_irqrestore(&runtime->lock, flags);
1149 return -EAGAIN;
1152 while (count > 0 && runtime->avail > 0) {
1153 count1 = runtime->buffer_size - runtime->appl_ptr;
1154 if (count1 > count)
1155 count1 = count;
1156 if (count1 > (long)runtime->avail)
1157 count1 = runtime->avail;
1158 if (kernel) {
1159 memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1160 } else {
1161 spin_unlock_irqrestore(&runtime->lock, flags);
1162 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1163 (char __user *)buf, count1)) {
1164 spin_lock_irqsave(&runtime->lock, flags);
1165 result = result > 0 ? result : -EFAULT;
1166 goto __end;
1168 spin_lock_irqsave(&runtime->lock, flags);
1170 runtime->appl_ptr += count1;
1171 runtime->appl_ptr %= runtime->buffer_size;
1172 runtime->avail -= count1;
1173 result += count1;
1174 buf += count1;
1175 count -= count1;
1177 __end:
1178 count1 = runtime->avail < runtime->buffer_size;
1179 spin_unlock_irqrestore(&runtime->lock, flags);
1180 if (count1)
1181 snd_rawmidi_output_trigger(substream, 1);
1182 return result;
1185 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1186 const unsigned char *buf, long count)
1188 return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1191 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1192 size_t count, loff_t *offset)
1194 long result, timeout;
1195 int count1;
1196 struct snd_rawmidi_file *rfile;
1197 struct snd_rawmidi_runtime *runtime;
1198 struct snd_rawmidi_substream *substream;
1200 rfile = file->private_data;
1201 substream = rfile->output;
1202 runtime = substream->runtime;
1203 /* we cannot put an atomic message to our buffer */
1204 if (substream->append && count > runtime->buffer_size)
1205 return -EIO;
1206 result = 0;
1207 while (count > 0) {
1208 spin_lock_irq(&runtime->lock);
1209 while (!snd_rawmidi_ready_append(substream, count)) {
1210 wait_queue_t wait;
1211 if (file->f_flags & O_NONBLOCK) {
1212 spin_unlock_irq(&runtime->lock);
1213 return result > 0 ? result : -EAGAIN;
1215 init_waitqueue_entry(&wait, current);
1216 add_wait_queue(&runtime->sleep, &wait);
1217 set_current_state(TASK_INTERRUPTIBLE);
1218 spin_unlock_irq(&runtime->lock);
1219 timeout = schedule_timeout(30 * HZ);
1220 remove_wait_queue(&runtime->sleep, &wait);
1221 if (signal_pending(current))
1222 return result > 0 ? result : -ERESTARTSYS;
1223 if (!runtime->avail && !timeout)
1224 return result > 0 ? result : -EIO;
1225 spin_lock_irq(&runtime->lock);
1227 spin_unlock_irq(&runtime->lock);
1228 count1 = snd_rawmidi_kernel_write1(substream,
1229 (unsigned char __force *)buf,
1230 count, 0);
1231 if (count1 < 0)
1232 return result > 0 ? result : count1;
1233 result += count1;
1234 buf += count1;
1235 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1236 break;
1237 count -= count1;
1239 if (file->f_flags & O_SYNC) {
1240 spin_lock_irq(&runtime->lock);
1241 while (runtime->avail != runtime->buffer_size) {
1242 wait_queue_t wait;
1243 unsigned int last_avail = runtime->avail;
1244 init_waitqueue_entry(&wait, current);
1245 add_wait_queue(&runtime->sleep, &wait);
1246 set_current_state(TASK_INTERRUPTIBLE);
1247 spin_unlock_irq(&runtime->lock);
1248 timeout = schedule_timeout(30 * HZ);
1249 remove_wait_queue(&runtime->sleep, &wait);
1250 if (signal_pending(current))
1251 return result > 0 ? result : -ERESTARTSYS;
1252 if (runtime->avail == last_avail && !timeout)
1253 return result > 0 ? result : -EIO;
1254 spin_lock_irq(&runtime->lock);
1256 spin_unlock_irq(&runtime->lock);
1258 return result;
1261 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1263 struct snd_rawmidi_file *rfile;
1264 struct snd_rawmidi_runtime *runtime;
1265 unsigned int mask;
1267 rfile = file->private_data;
1268 if (rfile->input != NULL) {
1269 runtime = rfile->input->runtime;
1270 snd_rawmidi_input_trigger(rfile->input, 1);
1271 poll_wait(file, &runtime->sleep, wait);
1273 if (rfile->output != NULL) {
1274 runtime = rfile->output->runtime;
1275 poll_wait(file, &runtime->sleep, wait);
1277 mask = 0;
1278 if (rfile->input != NULL) {
1279 if (snd_rawmidi_ready(rfile->input))
1280 mask |= POLLIN | POLLRDNORM;
1282 if (rfile->output != NULL) {
1283 if (snd_rawmidi_ready(rfile->output))
1284 mask |= POLLOUT | POLLWRNORM;
1286 return mask;
1291 #ifdef CONFIG_COMPAT
1292 #include "rawmidi_compat.c"
1293 #else
1294 #define snd_rawmidi_ioctl_compat NULL
1295 #endif
1301 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1302 struct snd_info_buffer *buffer)
1304 struct snd_rawmidi *rmidi;
1305 struct snd_rawmidi_substream *substream;
1306 struct snd_rawmidi_runtime *runtime;
1308 rmidi = entry->private_data;
1309 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1310 mutex_lock(&rmidi->open_mutex);
1311 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1312 list_for_each_entry(substream,
1313 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1314 list) {
1315 snd_iprintf(buffer,
1316 "Output %d\n"
1317 " Tx bytes : %lu\n",
1318 substream->number,
1319 (unsigned long) substream->bytes);
1320 if (substream->opened) {
1321 runtime = substream->runtime;
1322 snd_iprintf(buffer,
1323 " Mode : %s\n"
1324 " Buffer size : %lu\n"
1325 " Avail : %lu\n",
1326 runtime->oss ? "OSS compatible" : "native",
1327 (unsigned long) runtime->buffer_size,
1328 (unsigned long) runtime->avail);
1332 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1333 list_for_each_entry(substream,
1334 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1335 list) {
1336 snd_iprintf(buffer,
1337 "Input %d\n"
1338 " Rx bytes : %lu\n",
1339 substream->number,
1340 (unsigned long) substream->bytes);
1341 if (substream->opened) {
1342 runtime = substream->runtime;
1343 snd_iprintf(buffer,
1344 " Buffer size : %lu\n"
1345 " Avail : %lu\n"
1346 " Overruns : %lu\n",
1347 (unsigned long) runtime->buffer_size,
1348 (unsigned long) runtime->avail,
1349 (unsigned long) runtime->xruns);
1353 mutex_unlock(&rmidi->open_mutex);
1357 * Register functions
1360 static const struct file_operations snd_rawmidi_f_ops =
1362 .owner = THIS_MODULE,
1363 .read = snd_rawmidi_read,
1364 .write = snd_rawmidi_write,
1365 .open = snd_rawmidi_open,
1366 .release = snd_rawmidi_release,
1367 .poll = snd_rawmidi_poll,
1368 .unlocked_ioctl = snd_rawmidi_ioctl,
1369 .compat_ioctl = snd_rawmidi_ioctl_compat,
1372 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1373 struct snd_rawmidi_str *stream,
1374 int direction,
1375 int count)
1377 struct snd_rawmidi_substream *substream;
1378 int idx;
1380 for (idx = 0; idx < count; idx++) {
1381 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1382 if (substream == NULL) {
1383 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
1384 return -ENOMEM;
1386 substream->stream = direction;
1387 substream->number = idx;
1388 substream->rmidi = rmidi;
1389 substream->pstr = stream;
1390 list_add_tail(&substream->list, &stream->substreams);
1391 stream->substream_count++;
1393 return 0;
1397 * snd_rawmidi_new - create a rawmidi instance
1398 * @card: the card instance
1399 * @id: the id string
1400 * @device: the device index
1401 * @output_count: the number of output streams
1402 * @input_count: the number of input streams
1403 * @rrawmidi: the pointer to store the new rawmidi instance
1405 * Creates a new rawmidi instance.
1406 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1408 * Returns zero if successful, or a negative error code on failure.
1410 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1411 int output_count, int input_count,
1412 struct snd_rawmidi ** rrawmidi)
1414 struct snd_rawmidi *rmidi;
1415 int err;
1416 static struct snd_device_ops ops = {
1417 .dev_free = snd_rawmidi_dev_free,
1418 .dev_register = snd_rawmidi_dev_register,
1419 .dev_disconnect = snd_rawmidi_dev_disconnect,
1422 snd_assert(rrawmidi != NULL, return -EINVAL);
1423 *rrawmidi = NULL;
1424 snd_assert(card != NULL, return -ENXIO);
1425 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1426 if (rmidi == NULL) {
1427 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
1428 return -ENOMEM;
1430 rmidi->card = card;
1431 rmidi->device = device;
1432 mutex_init(&rmidi->open_mutex);
1433 init_waitqueue_head(&rmidi->open_wait);
1434 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1435 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1437 if (id != NULL)
1438 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1439 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1440 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1441 SNDRV_RAWMIDI_STREAM_INPUT,
1442 input_count)) < 0) {
1443 snd_rawmidi_free(rmidi);
1444 return err;
1446 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1447 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1448 SNDRV_RAWMIDI_STREAM_OUTPUT,
1449 output_count)) < 0) {
1450 snd_rawmidi_free(rmidi);
1451 return err;
1453 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1454 snd_rawmidi_free(rmidi);
1455 return err;
1457 *rrawmidi = rmidi;
1458 return 0;
1461 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1463 struct snd_rawmidi_substream *substream;
1465 while (!list_empty(&stream->substreams)) {
1466 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1467 list_del(&substream->list);
1468 kfree(substream);
1472 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1474 snd_assert(rmidi != NULL, return -ENXIO);
1476 snd_info_free_entry(rmidi->proc_entry);
1477 rmidi->proc_entry = NULL;
1478 mutex_lock(&register_mutex);
1479 if (rmidi->ops && rmidi->ops->dev_unregister)
1480 rmidi->ops->dev_unregister(rmidi);
1481 mutex_unlock(&register_mutex);
1483 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1484 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1485 if (rmidi->private_free)
1486 rmidi->private_free(rmidi);
1487 kfree(rmidi);
1488 return 0;
1491 static int snd_rawmidi_dev_free(struct snd_device *device)
1493 struct snd_rawmidi *rmidi = device->device_data;
1494 return snd_rawmidi_free(rmidi);
1497 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1498 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1500 struct snd_rawmidi *rmidi = device->private_data;
1501 rmidi->seq_dev = NULL;
1503 #endif
1505 static int snd_rawmidi_dev_register(struct snd_device *device)
1507 int err;
1508 struct snd_info_entry *entry;
1509 char name[16];
1510 struct snd_rawmidi *rmidi = device->device_data;
1512 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1513 return -ENOMEM;
1514 mutex_lock(&register_mutex);
1515 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1516 mutex_unlock(&register_mutex);
1517 return -EBUSY;
1519 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1520 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1521 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1522 rmidi->card, rmidi->device,
1523 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1524 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
1525 list_del(&rmidi->list);
1526 mutex_unlock(&register_mutex);
1527 return err;
1529 if (rmidi->ops && rmidi->ops->dev_register &&
1530 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1531 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1532 list_del(&rmidi->list);
1533 mutex_unlock(&register_mutex);
1534 return err;
1536 #ifdef CONFIG_SND_OSSEMUL
1537 rmidi->ossreg = 0;
1538 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1539 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1540 rmidi->card, 0, &snd_rawmidi_f_ops,
1541 rmidi, name) < 0) {
1542 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1543 } else {
1544 rmidi->ossreg++;
1545 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1546 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1547 #endif
1550 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1551 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1552 rmidi->card, 1, &snd_rawmidi_f_ops,
1553 rmidi, name) < 0) {
1554 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1555 } else {
1556 rmidi->ossreg++;
1559 #endif /* CONFIG_SND_OSSEMUL */
1560 mutex_unlock(&register_mutex);
1561 sprintf(name, "midi%d", rmidi->device);
1562 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1563 if (entry) {
1564 entry->private_data = rmidi;
1565 entry->c.text.read = snd_rawmidi_proc_info_read;
1566 if (snd_info_register(entry) < 0) {
1567 snd_info_free_entry(entry);
1568 entry = NULL;
1571 rmidi->proc_entry = entry;
1572 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1573 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1574 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1575 rmidi->seq_dev->private_data = rmidi;
1576 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1577 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1578 snd_device_register(rmidi->card, rmidi->seq_dev);
1581 #endif
1582 return 0;
1585 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1587 struct snd_rawmidi *rmidi = device->device_data;
1589 mutex_lock(&register_mutex);
1590 list_del_init(&rmidi->list);
1591 #ifdef CONFIG_SND_OSSEMUL
1592 if (rmidi->ossreg) {
1593 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1594 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1595 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1596 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1597 #endif
1599 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1600 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1601 rmidi->ossreg = 0;
1603 #endif /* CONFIG_SND_OSSEMUL */
1604 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1605 mutex_unlock(&register_mutex);
1606 return 0;
1610 * snd_rawmidi_set_ops - set the rawmidi operators
1611 * @rmidi: the rawmidi instance
1612 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1613 * @ops: the operator table
1615 * Sets the rawmidi operators for the given stream direction.
1617 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1618 struct snd_rawmidi_ops *ops)
1620 struct snd_rawmidi_substream *substream;
1622 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1623 substream->ops = ops;
1627 * ENTRY functions
1630 static int __init alsa_rawmidi_init(void)
1633 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1634 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1635 #ifdef CONFIG_SND_OSSEMUL
1636 { int i;
1637 /* check device map table */
1638 for (i = 0; i < SNDRV_CARDS; i++) {
1639 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1640 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1641 midi_map[i] = 0;
1643 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1644 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1645 amidi_map[i] = 1;
1649 #endif /* CONFIG_SND_OSSEMUL */
1650 return 0;
1653 static void __exit alsa_rawmidi_exit(void)
1655 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1656 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1659 module_init(alsa_rawmidi_init)
1660 module_exit(alsa_rawmidi_exit)
1662 EXPORT_SYMBOL(snd_rawmidi_output_params);
1663 EXPORT_SYMBOL(snd_rawmidi_input_params);
1664 EXPORT_SYMBOL(snd_rawmidi_drop_output);
1665 EXPORT_SYMBOL(snd_rawmidi_drain_output);
1666 EXPORT_SYMBOL(snd_rawmidi_drain_input);
1667 EXPORT_SYMBOL(snd_rawmidi_receive);
1668 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1669 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1670 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1671 EXPORT_SYMBOL(snd_rawmidi_transmit);
1672 EXPORT_SYMBOL(snd_rawmidi_new);
1673 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1674 EXPORT_SYMBOL(snd_rawmidi_info_select);
1675 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1676 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1677 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1678 EXPORT_SYMBOL(snd_rawmidi_kernel_write);