Linux 4.18.10
[linux/fpc-iii.git] / sound / core / rawmidi.c
blobb53026a72e734e29f63a76eb711458013fa70290
1 /*
2 * Abstract layer for MIDI v1.0 stream
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/core.h>
23 #include <linux/major.h>
24 #include <linux/init.h>
25 #include <linux/sched/signal.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/wait.h>
29 #include <linux/mutex.h>
30 #include <linux/module.h>
31 #include <linux/delay.h>
32 #include <sound/rawmidi.h>
33 #include <sound/info.h>
34 #include <sound/control.h>
35 #include <sound/minors.h>
36 #include <sound/initval.h>
38 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
39 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
40 MODULE_LICENSE("GPL");
42 #ifdef CONFIG_SND_OSSEMUL
43 static int midi_map[SNDRV_CARDS];
44 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
45 module_param_array(midi_map, int, NULL, 0444);
46 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
47 module_param_array(amidi_map, int, NULL, 0444);
48 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
49 #endif /* CONFIG_SND_OSSEMUL */
51 static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
52 static int snd_rawmidi_dev_free(struct snd_device *device);
53 static int snd_rawmidi_dev_register(struct snd_device *device);
54 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
56 static LIST_HEAD(snd_rawmidi_devices);
57 static DEFINE_MUTEX(register_mutex);
59 #define rmidi_err(rmidi, fmt, args...) \
60 dev_err(&(rmidi)->dev, fmt, ##args)
61 #define rmidi_warn(rmidi, fmt, args...) \
62 dev_warn(&(rmidi)->dev, fmt, ##args)
63 #define rmidi_dbg(rmidi, fmt, args...) \
64 dev_dbg(&(rmidi)->dev, fmt, ##args)
66 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
68 struct snd_rawmidi *rawmidi;
70 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
71 if (rawmidi->card == card && rawmidi->device == device)
72 return rawmidi;
73 return NULL;
76 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
78 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
79 case FMODE_WRITE:
80 return SNDRV_RAWMIDI_LFLG_OUTPUT;
81 case FMODE_READ:
82 return SNDRV_RAWMIDI_LFLG_INPUT;
83 default:
84 return SNDRV_RAWMIDI_LFLG_OPEN;
88 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
90 struct snd_rawmidi_runtime *runtime = substream->runtime;
91 return runtime->avail >= runtime->avail_min;
94 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
95 size_t count)
97 struct snd_rawmidi_runtime *runtime = substream->runtime;
98 return runtime->avail >= runtime->avail_min &&
99 (!substream->append || runtime->avail >= count);
102 static void snd_rawmidi_input_event_work(struct work_struct *work)
104 struct snd_rawmidi_runtime *runtime =
105 container_of(work, struct snd_rawmidi_runtime, event_work);
106 if (runtime->event)
107 runtime->event(runtime->substream);
110 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
112 struct snd_rawmidi_runtime *runtime;
114 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
115 return -ENOMEM;
116 runtime->substream = substream;
117 spin_lock_init(&runtime->lock);
118 init_waitqueue_head(&runtime->sleep);
119 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
120 runtime->event = NULL;
121 runtime->buffer_size = PAGE_SIZE;
122 runtime->avail_min = 1;
123 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
124 runtime->avail = 0;
125 else
126 runtime->avail = runtime->buffer_size;
127 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
128 kfree(runtime);
129 return -ENOMEM;
131 runtime->appl_ptr = runtime->hw_ptr = 0;
132 substream->runtime = runtime;
133 return 0;
136 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
138 struct snd_rawmidi_runtime *runtime = substream->runtime;
140 kfree(runtime->buffer);
141 kfree(runtime);
142 substream->runtime = NULL;
143 return 0;
146 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
148 if (!substream->opened)
149 return;
150 substream->ops->trigger(substream, up);
153 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
155 if (!substream->opened)
156 return;
157 substream->ops->trigger(substream, up);
158 if (!up)
159 cancel_work_sync(&substream->runtime->event_work);
162 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
164 unsigned long flags;
165 struct snd_rawmidi_runtime *runtime = substream->runtime;
167 snd_rawmidi_output_trigger(substream, 0);
168 runtime->drain = 0;
169 spin_lock_irqsave(&runtime->lock, flags);
170 runtime->appl_ptr = runtime->hw_ptr = 0;
171 runtime->avail = runtime->buffer_size;
172 spin_unlock_irqrestore(&runtime->lock, flags);
173 return 0;
175 EXPORT_SYMBOL(snd_rawmidi_drop_output);
177 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
179 int err;
180 long timeout;
181 struct snd_rawmidi_runtime *runtime = substream->runtime;
183 err = 0;
184 runtime->drain = 1;
185 timeout = wait_event_interruptible_timeout(runtime->sleep,
186 (runtime->avail >= runtime->buffer_size),
187 10*HZ);
188 if (signal_pending(current))
189 err = -ERESTARTSYS;
190 if (runtime->avail < runtime->buffer_size && !timeout) {
191 rmidi_warn(substream->rmidi,
192 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
193 (long)runtime->avail, (long)runtime->buffer_size);
194 err = -EIO;
196 runtime->drain = 0;
197 if (err != -ERESTARTSYS) {
198 /* we need wait a while to make sure that Tx FIFOs are empty */
199 if (substream->ops->drain)
200 substream->ops->drain(substream);
201 else
202 msleep(50);
203 snd_rawmidi_drop_output(substream);
205 return err;
207 EXPORT_SYMBOL(snd_rawmidi_drain_output);
209 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
211 unsigned long flags;
212 struct snd_rawmidi_runtime *runtime = substream->runtime;
214 snd_rawmidi_input_trigger(substream, 0);
215 runtime->drain = 0;
216 spin_lock_irqsave(&runtime->lock, flags);
217 runtime->appl_ptr = runtime->hw_ptr = 0;
218 runtime->avail = 0;
219 spin_unlock_irqrestore(&runtime->lock, flags);
220 return 0;
222 EXPORT_SYMBOL(snd_rawmidi_drain_input);
224 /* look for an available substream for the given stream direction;
225 * if a specific subdevice is given, try to assign it
227 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
228 int stream, int mode,
229 struct snd_rawmidi_substream **sub_ret)
231 struct snd_rawmidi_substream *substream;
232 struct snd_rawmidi_str *s = &rmidi->streams[stream];
233 static unsigned int info_flags[2] = {
234 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
235 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
238 if (!(rmidi->info_flags & info_flags[stream]))
239 return -ENXIO;
240 if (subdevice >= 0 && subdevice >= s->substream_count)
241 return -ENODEV;
243 list_for_each_entry(substream, &s->substreams, list) {
244 if (substream->opened) {
245 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
246 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
247 !substream->append)
248 continue;
250 if (subdevice < 0 || subdevice == substream->number) {
251 *sub_ret = substream;
252 return 0;
255 return -EAGAIN;
258 /* open and do ref-counting for the given substream */
259 static int open_substream(struct snd_rawmidi *rmidi,
260 struct snd_rawmidi_substream *substream,
261 int mode)
263 int err;
265 if (substream->use_count == 0) {
266 err = snd_rawmidi_runtime_create(substream);
267 if (err < 0)
268 return err;
269 err = substream->ops->open(substream);
270 if (err < 0) {
271 snd_rawmidi_runtime_free(substream);
272 return err;
274 substream->opened = 1;
275 substream->active_sensing = 0;
276 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
277 substream->append = 1;
278 substream->pid = get_pid(task_pid(current));
279 rmidi->streams[substream->stream].substream_opened++;
281 substream->use_count++;
282 return 0;
285 static void close_substream(struct snd_rawmidi *rmidi,
286 struct snd_rawmidi_substream *substream,
287 int cleanup);
289 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
290 struct snd_rawmidi_file *rfile)
292 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
293 int err;
295 rfile->input = rfile->output = NULL;
296 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
297 err = assign_substream(rmidi, subdevice,
298 SNDRV_RAWMIDI_STREAM_INPUT,
299 mode, &sinput);
300 if (err < 0)
301 return err;
303 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
304 err = assign_substream(rmidi, subdevice,
305 SNDRV_RAWMIDI_STREAM_OUTPUT,
306 mode, &soutput);
307 if (err < 0)
308 return err;
311 if (sinput) {
312 err = open_substream(rmidi, sinput, mode);
313 if (err < 0)
314 return err;
316 if (soutput) {
317 err = open_substream(rmidi, soutput, mode);
318 if (err < 0) {
319 if (sinput)
320 close_substream(rmidi, sinput, 0);
321 return err;
325 rfile->rmidi = rmidi;
326 rfile->input = sinput;
327 rfile->output = soutput;
328 return 0;
331 /* called from sound/core/seq/seq_midi.c */
332 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
333 int mode, struct snd_rawmidi_file * rfile)
335 struct snd_rawmidi *rmidi;
336 int err;
338 if (snd_BUG_ON(!rfile))
339 return -EINVAL;
341 mutex_lock(&register_mutex);
342 rmidi = snd_rawmidi_search(card, device);
343 if (rmidi == NULL) {
344 mutex_unlock(&register_mutex);
345 return -ENODEV;
347 if (!try_module_get(rmidi->card->module)) {
348 mutex_unlock(&register_mutex);
349 return -ENXIO;
351 mutex_unlock(&register_mutex);
353 mutex_lock(&rmidi->open_mutex);
354 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
355 mutex_unlock(&rmidi->open_mutex);
356 if (err < 0)
357 module_put(rmidi->card->module);
358 return err;
360 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
362 static int snd_rawmidi_open(struct inode *inode, struct file *file)
364 int maj = imajor(inode);
365 struct snd_card *card;
366 int subdevice;
367 unsigned short fflags;
368 int err;
369 struct snd_rawmidi *rmidi;
370 struct snd_rawmidi_file *rawmidi_file = NULL;
371 wait_queue_entry_t wait;
373 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
374 return -EINVAL; /* invalid combination */
376 err = nonseekable_open(inode, file);
377 if (err < 0)
378 return err;
380 if (maj == snd_major) {
381 rmidi = snd_lookup_minor_data(iminor(inode),
382 SNDRV_DEVICE_TYPE_RAWMIDI);
383 #ifdef CONFIG_SND_OSSEMUL
384 } else if (maj == SOUND_MAJOR) {
385 rmidi = snd_lookup_oss_minor_data(iminor(inode),
386 SNDRV_OSS_DEVICE_TYPE_MIDI);
387 #endif
388 } else
389 return -ENXIO;
391 if (rmidi == NULL)
392 return -ENODEV;
394 if (!try_module_get(rmidi->card->module)) {
395 snd_card_unref(rmidi->card);
396 return -ENXIO;
399 mutex_lock(&rmidi->open_mutex);
400 card = rmidi->card;
401 err = snd_card_file_add(card, file);
402 if (err < 0)
403 goto __error_card;
404 fflags = snd_rawmidi_file_flags(file);
405 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
406 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
407 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
408 if (rawmidi_file == NULL) {
409 err = -ENOMEM;
410 goto __error;
412 init_waitqueue_entry(&wait, current);
413 add_wait_queue(&rmidi->open_wait, &wait);
414 while (1) {
415 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
416 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
417 if (err >= 0)
418 break;
419 if (err == -EAGAIN) {
420 if (file->f_flags & O_NONBLOCK) {
421 err = -EBUSY;
422 break;
424 } else
425 break;
426 set_current_state(TASK_INTERRUPTIBLE);
427 mutex_unlock(&rmidi->open_mutex);
428 schedule();
429 mutex_lock(&rmidi->open_mutex);
430 if (rmidi->card->shutdown) {
431 err = -ENODEV;
432 break;
434 if (signal_pending(current)) {
435 err = -ERESTARTSYS;
436 break;
439 remove_wait_queue(&rmidi->open_wait, &wait);
440 if (err < 0) {
441 kfree(rawmidi_file);
442 goto __error;
444 #ifdef CONFIG_SND_OSSEMUL
445 if (rawmidi_file->input && rawmidi_file->input->runtime)
446 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
447 if (rawmidi_file->output && rawmidi_file->output->runtime)
448 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
449 #endif
450 file->private_data = rawmidi_file;
451 mutex_unlock(&rmidi->open_mutex);
452 snd_card_unref(rmidi->card);
453 return 0;
455 __error:
456 snd_card_file_remove(card, file);
457 __error_card:
458 mutex_unlock(&rmidi->open_mutex);
459 module_put(rmidi->card->module);
460 snd_card_unref(rmidi->card);
461 return err;
464 static void close_substream(struct snd_rawmidi *rmidi,
465 struct snd_rawmidi_substream *substream,
466 int cleanup)
468 if (--substream->use_count)
469 return;
471 if (cleanup) {
472 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
473 snd_rawmidi_input_trigger(substream, 0);
474 else {
475 if (substream->active_sensing) {
476 unsigned char buf = 0xfe;
477 /* sending single active sensing message
478 * to shut the device up
480 snd_rawmidi_kernel_write(substream, &buf, 1);
482 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
483 snd_rawmidi_output_trigger(substream, 0);
486 substream->ops->close(substream);
487 if (substream->runtime->private_free)
488 substream->runtime->private_free(substream);
489 snd_rawmidi_runtime_free(substream);
490 substream->opened = 0;
491 substream->append = 0;
492 put_pid(substream->pid);
493 substream->pid = NULL;
494 rmidi->streams[substream->stream].substream_opened--;
497 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
499 struct snd_rawmidi *rmidi;
501 rmidi = rfile->rmidi;
502 mutex_lock(&rmidi->open_mutex);
503 if (rfile->input) {
504 close_substream(rmidi, rfile->input, 1);
505 rfile->input = NULL;
507 if (rfile->output) {
508 close_substream(rmidi, rfile->output, 1);
509 rfile->output = NULL;
511 rfile->rmidi = NULL;
512 mutex_unlock(&rmidi->open_mutex);
513 wake_up(&rmidi->open_wait);
516 /* called from sound/core/seq/seq_midi.c */
517 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
519 struct snd_rawmidi *rmidi;
521 if (snd_BUG_ON(!rfile))
522 return -ENXIO;
524 rmidi = rfile->rmidi;
525 rawmidi_release_priv(rfile);
526 module_put(rmidi->card->module);
527 return 0;
529 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
531 static int snd_rawmidi_release(struct inode *inode, struct file *file)
533 struct snd_rawmidi_file *rfile;
534 struct snd_rawmidi *rmidi;
535 struct module *module;
537 rfile = file->private_data;
538 rmidi = rfile->rmidi;
539 rawmidi_release_priv(rfile);
540 kfree(rfile);
541 module = rmidi->card->module;
542 snd_card_file_remove(rmidi->card, file);
543 module_put(module);
544 return 0;
547 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
548 struct snd_rawmidi_info *info)
550 struct snd_rawmidi *rmidi;
552 if (substream == NULL)
553 return -ENODEV;
554 rmidi = substream->rmidi;
555 memset(info, 0, sizeof(*info));
556 info->card = rmidi->card->number;
557 info->device = rmidi->device;
558 info->subdevice = substream->number;
559 info->stream = substream->stream;
560 info->flags = rmidi->info_flags;
561 strcpy(info->id, rmidi->id);
562 strcpy(info->name, rmidi->name);
563 strcpy(info->subname, substream->name);
564 info->subdevices_count = substream->pstr->substream_count;
565 info->subdevices_avail = (substream->pstr->substream_count -
566 substream->pstr->substream_opened);
567 return 0;
570 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
571 struct snd_rawmidi_info __user * _info)
573 struct snd_rawmidi_info info;
574 int err;
575 if ((err = snd_rawmidi_info(substream, &info)) < 0)
576 return err;
577 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
578 return -EFAULT;
579 return 0;
582 static int __snd_rawmidi_info_select(struct snd_card *card,
583 struct snd_rawmidi_info *info)
585 struct snd_rawmidi *rmidi;
586 struct snd_rawmidi_str *pstr;
587 struct snd_rawmidi_substream *substream;
589 rmidi = snd_rawmidi_search(card, info->device);
590 if (!rmidi)
591 return -ENXIO;
592 if (info->stream < 0 || info->stream > 1)
593 return -EINVAL;
594 pstr = &rmidi->streams[info->stream];
595 if (pstr->substream_count == 0)
596 return -ENOENT;
597 if (info->subdevice >= pstr->substream_count)
598 return -ENXIO;
599 list_for_each_entry(substream, &pstr->substreams, list) {
600 if ((unsigned int)substream->number == info->subdevice)
601 return snd_rawmidi_info(substream, info);
603 return -ENXIO;
606 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
608 int ret;
610 mutex_lock(&register_mutex);
611 ret = __snd_rawmidi_info_select(card, info);
612 mutex_unlock(&register_mutex);
613 return ret;
615 EXPORT_SYMBOL(snd_rawmidi_info_select);
617 static int snd_rawmidi_info_select_user(struct snd_card *card,
618 struct snd_rawmidi_info __user *_info)
620 int err;
621 struct snd_rawmidi_info info;
622 if (get_user(info.device, &_info->device))
623 return -EFAULT;
624 if (get_user(info.stream, &_info->stream))
625 return -EFAULT;
626 if (get_user(info.subdevice, &_info->subdevice))
627 return -EFAULT;
628 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
629 return err;
630 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
631 return -EFAULT;
632 return 0;
635 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
636 struct snd_rawmidi_params * params)
638 char *newbuf, *oldbuf;
639 struct snd_rawmidi_runtime *runtime = substream->runtime;
641 if (substream->append && substream->use_count > 1)
642 return -EBUSY;
643 snd_rawmidi_drain_output(substream);
644 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
645 return -EINVAL;
647 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
648 return -EINVAL;
650 if (params->buffer_size != runtime->buffer_size) {
651 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
652 if (!newbuf)
653 return -ENOMEM;
654 spin_lock_irq(&runtime->lock);
655 oldbuf = runtime->buffer;
656 runtime->buffer = newbuf;
657 runtime->buffer_size = params->buffer_size;
658 runtime->avail = runtime->buffer_size;
659 runtime->appl_ptr = runtime->hw_ptr = 0;
660 spin_unlock_irq(&runtime->lock);
661 kfree(oldbuf);
663 runtime->avail_min = params->avail_min;
664 substream->active_sensing = !params->no_active_sensing;
665 return 0;
667 EXPORT_SYMBOL(snd_rawmidi_output_params);
669 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
670 struct snd_rawmidi_params * params)
672 char *newbuf, *oldbuf;
673 struct snd_rawmidi_runtime *runtime = substream->runtime;
675 snd_rawmidi_drain_input(substream);
676 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
677 return -EINVAL;
679 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
680 return -EINVAL;
682 if (params->buffer_size != runtime->buffer_size) {
683 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
684 if (!newbuf)
685 return -ENOMEM;
686 spin_lock_irq(&runtime->lock);
687 oldbuf = runtime->buffer;
688 runtime->buffer = newbuf;
689 runtime->buffer_size = params->buffer_size;
690 runtime->appl_ptr = runtime->hw_ptr = 0;
691 spin_unlock_irq(&runtime->lock);
692 kfree(oldbuf);
694 runtime->avail_min = params->avail_min;
695 return 0;
697 EXPORT_SYMBOL(snd_rawmidi_input_params);
699 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
700 struct snd_rawmidi_status * status)
702 struct snd_rawmidi_runtime *runtime = substream->runtime;
704 memset(status, 0, sizeof(*status));
705 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
706 spin_lock_irq(&runtime->lock);
707 status->avail = runtime->avail;
708 spin_unlock_irq(&runtime->lock);
709 return 0;
712 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
713 struct snd_rawmidi_status * status)
715 struct snd_rawmidi_runtime *runtime = substream->runtime;
717 memset(status, 0, sizeof(*status));
718 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
719 spin_lock_irq(&runtime->lock);
720 status->avail = runtime->avail;
721 status->xruns = runtime->xruns;
722 runtime->xruns = 0;
723 spin_unlock_irq(&runtime->lock);
724 return 0;
727 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
729 struct snd_rawmidi_file *rfile;
730 void __user *argp = (void __user *)arg;
732 rfile = file->private_data;
733 if (((cmd >> 8) & 0xff) != 'W')
734 return -ENOTTY;
735 switch (cmd) {
736 case SNDRV_RAWMIDI_IOCTL_PVERSION:
737 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
738 case SNDRV_RAWMIDI_IOCTL_INFO:
740 int stream;
741 struct snd_rawmidi_info __user *info = argp;
742 if (get_user(stream, &info->stream))
743 return -EFAULT;
744 switch (stream) {
745 case SNDRV_RAWMIDI_STREAM_INPUT:
746 return snd_rawmidi_info_user(rfile->input, info);
747 case SNDRV_RAWMIDI_STREAM_OUTPUT:
748 return snd_rawmidi_info_user(rfile->output, info);
749 default:
750 return -EINVAL;
753 case SNDRV_RAWMIDI_IOCTL_PARAMS:
755 struct snd_rawmidi_params params;
756 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
757 return -EFAULT;
758 switch (params.stream) {
759 case SNDRV_RAWMIDI_STREAM_OUTPUT:
760 if (rfile->output == NULL)
761 return -EINVAL;
762 return snd_rawmidi_output_params(rfile->output, &params);
763 case SNDRV_RAWMIDI_STREAM_INPUT:
764 if (rfile->input == NULL)
765 return -EINVAL;
766 return snd_rawmidi_input_params(rfile->input, &params);
767 default:
768 return -EINVAL;
771 case SNDRV_RAWMIDI_IOCTL_STATUS:
773 int err = 0;
774 struct snd_rawmidi_status status;
775 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
776 return -EFAULT;
777 switch (status.stream) {
778 case SNDRV_RAWMIDI_STREAM_OUTPUT:
779 if (rfile->output == NULL)
780 return -EINVAL;
781 err = snd_rawmidi_output_status(rfile->output, &status);
782 break;
783 case SNDRV_RAWMIDI_STREAM_INPUT:
784 if (rfile->input == NULL)
785 return -EINVAL;
786 err = snd_rawmidi_input_status(rfile->input, &status);
787 break;
788 default:
789 return -EINVAL;
791 if (err < 0)
792 return err;
793 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
794 return -EFAULT;
795 return 0;
797 case SNDRV_RAWMIDI_IOCTL_DROP:
799 int val;
800 if (get_user(val, (int __user *) argp))
801 return -EFAULT;
802 switch (val) {
803 case SNDRV_RAWMIDI_STREAM_OUTPUT:
804 if (rfile->output == NULL)
805 return -EINVAL;
806 return snd_rawmidi_drop_output(rfile->output);
807 default:
808 return -EINVAL;
811 case SNDRV_RAWMIDI_IOCTL_DRAIN:
813 int val;
814 if (get_user(val, (int __user *) argp))
815 return -EFAULT;
816 switch (val) {
817 case SNDRV_RAWMIDI_STREAM_OUTPUT:
818 if (rfile->output == NULL)
819 return -EINVAL;
820 return snd_rawmidi_drain_output(rfile->output);
821 case SNDRV_RAWMIDI_STREAM_INPUT:
822 if (rfile->input == NULL)
823 return -EINVAL;
824 return snd_rawmidi_drain_input(rfile->input);
825 default:
826 return -EINVAL;
829 default:
830 rmidi_dbg(rfile->rmidi,
831 "rawmidi: unknown command = 0x%x\n", cmd);
833 return -ENOTTY;
836 static int snd_rawmidi_control_ioctl(struct snd_card *card,
837 struct snd_ctl_file *control,
838 unsigned int cmd,
839 unsigned long arg)
841 void __user *argp = (void __user *)arg;
843 switch (cmd) {
844 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
846 int device;
848 if (get_user(device, (int __user *)argp))
849 return -EFAULT;
850 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
851 device = SNDRV_RAWMIDI_DEVICES - 1;
852 mutex_lock(&register_mutex);
853 device = device < 0 ? 0 : device + 1;
854 while (device < SNDRV_RAWMIDI_DEVICES) {
855 if (snd_rawmidi_search(card, device))
856 break;
857 device++;
859 if (device == SNDRV_RAWMIDI_DEVICES)
860 device = -1;
861 mutex_unlock(&register_mutex);
862 if (put_user(device, (int __user *)argp))
863 return -EFAULT;
864 return 0;
866 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
868 int val;
870 if (get_user(val, (int __user *)argp))
871 return -EFAULT;
872 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
873 return 0;
875 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
876 return snd_rawmidi_info_select_user(card, argp);
878 return -ENOIOCTLCMD;
882 * snd_rawmidi_receive - receive the input data from the device
883 * @substream: the rawmidi substream
884 * @buffer: the buffer pointer
885 * @count: the data size to read
887 * Reads the data from the internal buffer.
889 * Return: The size of read data, or a negative error code on failure.
891 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
892 const unsigned char *buffer, int count)
894 unsigned long flags;
895 int result = 0, count1;
896 struct snd_rawmidi_runtime *runtime = substream->runtime;
898 if (!substream->opened)
899 return -EBADFD;
900 if (runtime->buffer == NULL) {
901 rmidi_dbg(substream->rmidi,
902 "snd_rawmidi_receive: input is not active!!!\n");
903 return -EINVAL;
905 spin_lock_irqsave(&runtime->lock, flags);
906 if (count == 1) { /* special case, faster code */
907 substream->bytes++;
908 if (runtime->avail < runtime->buffer_size) {
909 runtime->buffer[runtime->hw_ptr++] = buffer[0];
910 runtime->hw_ptr %= runtime->buffer_size;
911 runtime->avail++;
912 result++;
913 } else {
914 runtime->xruns++;
916 } else {
917 substream->bytes += count;
918 count1 = runtime->buffer_size - runtime->hw_ptr;
919 if (count1 > count)
920 count1 = count;
921 if (count1 > (int)(runtime->buffer_size - runtime->avail))
922 count1 = runtime->buffer_size - runtime->avail;
923 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
924 runtime->hw_ptr += count1;
925 runtime->hw_ptr %= runtime->buffer_size;
926 runtime->avail += count1;
927 count -= count1;
928 result += count1;
929 if (count > 0) {
930 buffer += count1;
931 count1 = count;
932 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
933 count1 = runtime->buffer_size - runtime->avail;
934 runtime->xruns += count - count1;
936 if (count1 > 0) {
937 memcpy(runtime->buffer, buffer, count1);
938 runtime->hw_ptr = count1;
939 runtime->avail += count1;
940 result += count1;
944 if (result > 0) {
945 if (runtime->event)
946 schedule_work(&runtime->event_work);
947 else if (snd_rawmidi_ready(substream))
948 wake_up(&runtime->sleep);
950 spin_unlock_irqrestore(&runtime->lock, flags);
951 return result;
953 EXPORT_SYMBOL(snd_rawmidi_receive);
955 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
956 unsigned char __user *userbuf,
957 unsigned char *kernelbuf, long count)
959 unsigned long flags;
960 long result = 0, count1;
961 struct snd_rawmidi_runtime *runtime = substream->runtime;
962 unsigned long appl_ptr;
964 spin_lock_irqsave(&runtime->lock, flags);
965 while (count > 0 && runtime->avail) {
966 count1 = runtime->buffer_size - runtime->appl_ptr;
967 if (count1 > count)
968 count1 = count;
969 if (count1 > (int)runtime->avail)
970 count1 = runtime->avail;
972 /* update runtime->appl_ptr before unlocking for userbuf */
973 appl_ptr = runtime->appl_ptr;
974 runtime->appl_ptr += count1;
975 runtime->appl_ptr %= runtime->buffer_size;
976 runtime->avail -= count1;
978 if (kernelbuf)
979 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
980 if (userbuf) {
981 spin_unlock_irqrestore(&runtime->lock, flags);
982 if (copy_to_user(userbuf + result,
983 runtime->buffer + appl_ptr, count1)) {
984 return result > 0 ? result : -EFAULT;
986 spin_lock_irqsave(&runtime->lock, flags);
988 result += count1;
989 count -= count1;
991 spin_unlock_irqrestore(&runtime->lock, flags);
992 return result;
995 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
996 unsigned char *buf, long count)
998 snd_rawmidi_input_trigger(substream, 1);
999 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
1001 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1003 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1004 loff_t *offset)
1006 long result;
1007 int count1;
1008 struct snd_rawmidi_file *rfile;
1009 struct snd_rawmidi_substream *substream;
1010 struct snd_rawmidi_runtime *runtime;
1012 rfile = file->private_data;
1013 substream = rfile->input;
1014 if (substream == NULL)
1015 return -EIO;
1016 runtime = substream->runtime;
1017 snd_rawmidi_input_trigger(substream, 1);
1018 result = 0;
1019 while (count > 0) {
1020 spin_lock_irq(&runtime->lock);
1021 while (!snd_rawmidi_ready(substream)) {
1022 wait_queue_entry_t wait;
1023 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1024 spin_unlock_irq(&runtime->lock);
1025 return result > 0 ? result : -EAGAIN;
1027 init_waitqueue_entry(&wait, current);
1028 add_wait_queue(&runtime->sleep, &wait);
1029 set_current_state(TASK_INTERRUPTIBLE);
1030 spin_unlock_irq(&runtime->lock);
1031 schedule();
1032 remove_wait_queue(&runtime->sleep, &wait);
1033 if (rfile->rmidi->card->shutdown)
1034 return -ENODEV;
1035 if (signal_pending(current))
1036 return result > 0 ? result : -ERESTARTSYS;
1037 if (!runtime->avail)
1038 return result > 0 ? result : -EIO;
1039 spin_lock_irq(&runtime->lock);
1041 spin_unlock_irq(&runtime->lock);
1042 count1 = snd_rawmidi_kernel_read1(substream,
1043 (unsigned char __user *)buf,
1044 NULL/*kernelbuf*/,
1045 count);
1046 if (count1 < 0)
1047 return result > 0 ? result : count1;
1048 result += count1;
1049 buf += count1;
1050 count -= count1;
1052 return result;
1056 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1057 * @substream: the rawmidi substream
1059 * Return: 1 if the internal output buffer is empty, 0 if not.
1061 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1063 struct snd_rawmidi_runtime *runtime = substream->runtime;
1064 int result;
1065 unsigned long flags;
1067 if (runtime->buffer == NULL) {
1068 rmidi_dbg(substream->rmidi,
1069 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1070 return 1;
1072 spin_lock_irqsave(&runtime->lock, flags);
1073 result = runtime->avail >= runtime->buffer_size;
1074 spin_unlock_irqrestore(&runtime->lock, flags);
1075 return result;
1077 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1080 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1081 * @substream: the rawmidi substream
1082 * @buffer: the buffer pointer
1083 * @count: data size to transfer
1085 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1087 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1088 unsigned char *buffer, int count)
1090 int result, count1;
1091 struct snd_rawmidi_runtime *runtime = substream->runtime;
1093 if (runtime->buffer == NULL) {
1094 rmidi_dbg(substream->rmidi,
1095 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1096 return -EINVAL;
1098 result = 0;
1099 if (runtime->avail >= runtime->buffer_size) {
1100 /* warning: lowlevel layer MUST trigger down the hardware */
1101 goto __skip;
1103 if (count == 1) { /* special case, faster code */
1104 *buffer = runtime->buffer[runtime->hw_ptr];
1105 result++;
1106 } else {
1107 count1 = runtime->buffer_size - runtime->hw_ptr;
1108 if (count1 > count)
1109 count1 = count;
1110 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1111 count1 = runtime->buffer_size - runtime->avail;
1112 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1113 count -= count1;
1114 result += count1;
1115 if (count > 0) {
1116 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1117 count = runtime->buffer_size - runtime->avail - count1;
1118 memcpy(buffer + count1, runtime->buffer, count);
1119 result += count;
1122 __skip:
1123 return result;
1125 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1128 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1129 * @substream: the rawmidi substream
1130 * @buffer: the buffer pointer
1131 * @count: data size to transfer
1133 * Copies data from the internal output buffer to the given buffer.
1135 * Call this in the interrupt handler when the midi output is ready,
1136 * and call snd_rawmidi_transmit_ack() after the transmission is
1137 * finished.
1139 * Return: The size of copied data, or a negative error code on failure.
1141 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1142 unsigned char *buffer, int count)
1144 struct snd_rawmidi_runtime *runtime = substream->runtime;
1145 int result;
1146 unsigned long flags;
1148 spin_lock_irqsave(&runtime->lock, flags);
1149 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1150 spin_unlock_irqrestore(&runtime->lock, flags);
1151 return result;
1153 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1156 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1157 * @substream: the rawmidi substream
1158 * @count: the transferred count
1160 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1162 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1164 struct snd_rawmidi_runtime *runtime = substream->runtime;
1166 if (runtime->buffer == NULL) {
1167 rmidi_dbg(substream->rmidi,
1168 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1169 return -EINVAL;
1171 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1172 runtime->hw_ptr += count;
1173 runtime->hw_ptr %= runtime->buffer_size;
1174 runtime->avail += count;
1175 substream->bytes += count;
1176 if (count > 0) {
1177 if (runtime->drain || snd_rawmidi_ready(substream))
1178 wake_up(&runtime->sleep);
1180 return count;
1182 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1185 * snd_rawmidi_transmit_ack - acknowledge the transmission
1186 * @substream: the rawmidi substream
1187 * @count: the transferred count
1189 * Advances the hardware pointer for the internal output buffer with
1190 * the given size and updates the condition.
1191 * Call after the transmission is finished.
1193 * Return: The advanced size if successful, or a negative error code on failure.
1195 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1197 struct snd_rawmidi_runtime *runtime = substream->runtime;
1198 int result;
1199 unsigned long flags;
1201 spin_lock_irqsave(&runtime->lock, flags);
1202 result = __snd_rawmidi_transmit_ack(substream, count);
1203 spin_unlock_irqrestore(&runtime->lock, flags);
1204 return result;
1206 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1209 * snd_rawmidi_transmit - copy from the buffer to the device
1210 * @substream: the rawmidi substream
1211 * @buffer: the buffer pointer
1212 * @count: the data size to transfer
1214 * Copies data from the buffer to the device and advances the pointer.
1216 * Return: The copied size if successful, or a negative error code on failure.
1218 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1219 unsigned char *buffer, int count)
1221 struct snd_rawmidi_runtime *runtime = substream->runtime;
1222 int result;
1223 unsigned long flags;
1225 spin_lock_irqsave(&runtime->lock, flags);
1226 if (!substream->opened)
1227 result = -EBADFD;
1228 else {
1229 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1230 if (count <= 0)
1231 result = count;
1232 else
1233 result = __snd_rawmidi_transmit_ack(substream, count);
1235 spin_unlock_irqrestore(&runtime->lock, flags);
1236 return result;
1238 EXPORT_SYMBOL(snd_rawmidi_transmit);
1240 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1241 const unsigned char __user *userbuf,
1242 const unsigned char *kernelbuf,
1243 long count)
1245 unsigned long flags;
1246 long count1, result;
1247 struct snd_rawmidi_runtime *runtime = substream->runtime;
1248 unsigned long appl_ptr;
1250 if (!kernelbuf && !userbuf)
1251 return -EINVAL;
1252 if (snd_BUG_ON(!runtime->buffer))
1253 return -EINVAL;
1255 result = 0;
1256 spin_lock_irqsave(&runtime->lock, flags);
1257 if (substream->append) {
1258 if ((long)runtime->avail < count) {
1259 spin_unlock_irqrestore(&runtime->lock, flags);
1260 return -EAGAIN;
1263 while (count > 0 && runtime->avail > 0) {
1264 count1 = runtime->buffer_size - runtime->appl_ptr;
1265 if (count1 > count)
1266 count1 = count;
1267 if (count1 > (long)runtime->avail)
1268 count1 = runtime->avail;
1270 /* update runtime->appl_ptr before unlocking for userbuf */
1271 appl_ptr = runtime->appl_ptr;
1272 runtime->appl_ptr += count1;
1273 runtime->appl_ptr %= runtime->buffer_size;
1274 runtime->avail -= count1;
1276 if (kernelbuf)
1277 memcpy(runtime->buffer + appl_ptr,
1278 kernelbuf + result, count1);
1279 else if (userbuf) {
1280 spin_unlock_irqrestore(&runtime->lock, flags);
1281 if (copy_from_user(runtime->buffer + appl_ptr,
1282 userbuf + result, count1)) {
1283 spin_lock_irqsave(&runtime->lock, flags);
1284 result = result > 0 ? result : -EFAULT;
1285 goto __end;
1287 spin_lock_irqsave(&runtime->lock, flags);
1289 result += count1;
1290 count -= count1;
1292 __end:
1293 count1 = runtime->avail < runtime->buffer_size;
1294 spin_unlock_irqrestore(&runtime->lock, flags);
1295 if (count1)
1296 snd_rawmidi_output_trigger(substream, 1);
1297 return result;
1300 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1301 const unsigned char *buf, long count)
1303 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1305 EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1307 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1308 size_t count, loff_t *offset)
1310 long result, timeout;
1311 int count1;
1312 struct snd_rawmidi_file *rfile;
1313 struct snd_rawmidi_runtime *runtime;
1314 struct snd_rawmidi_substream *substream;
1316 rfile = file->private_data;
1317 substream = rfile->output;
1318 runtime = substream->runtime;
1319 /* we cannot put an atomic message to our buffer */
1320 if (substream->append && count > runtime->buffer_size)
1321 return -EIO;
1322 result = 0;
1323 while (count > 0) {
1324 spin_lock_irq(&runtime->lock);
1325 while (!snd_rawmidi_ready_append(substream, count)) {
1326 wait_queue_entry_t wait;
1327 if (file->f_flags & O_NONBLOCK) {
1328 spin_unlock_irq(&runtime->lock);
1329 return result > 0 ? result : -EAGAIN;
1331 init_waitqueue_entry(&wait, current);
1332 add_wait_queue(&runtime->sleep, &wait);
1333 set_current_state(TASK_INTERRUPTIBLE);
1334 spin_unlock_irq(&runtime->lock);
1335 timeout = schedule_timeout(30 * HZ);
1336 remove_wait_queue(&runtime->sleep, &wait);
1337 if (rfile->rmidi->card->shutdown)
1338 return -ENODEV;
1339 if (signal_pending(current))
1340 return result > 0 ? result : -ERESTARTSYS;
1341 if (!runtime->avail && !timeout)
1342 return result > 0 ? result : -EIO;
1343 spin_lock_irq(&runtime->lock);
1345 spin_unlock_irq(&runtime->lock);
1346 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1347 if (count1 < 0)
1348 return result > 0 ? result : count1;
1349 result += count1;
1350 buf += count1;
1351 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1352 break;
1353 count -= count1;
1355 if (file->f_flags & O_DSYNC) {
1356 spin_lock_irq(&runtime->lock);
1357 while (runtime->avail != runtime->buffer_size) {
1358 wait_queue_entry_t wait;
1359 unsigned int last_avail = runtime->avail;
1360 init_waitqueue_entry(&wait, current);
1361 add_wait_queue(&runtime->sleep, &wait);
1362 set_current_state(TASK_INTERRUPTIBLE);
1363 spin_unlock_irq(&runtime->lock);
1364 timeout = schedule_timeout(30 * HZ);
1365 remove_wait_queue(&runtime->sleep, &wait);
1366 if (signal_pending(current))
1367 return result > 0 ? result : -ERESTARTSYS;
1368 if (runtime->avail == last_avail && !timeout)
1369 return result > 0 ? result : -EIO;
1370 spin_lock_irq(&runtime->lock);
1372 spin_unlock_irq(&runtime->lock);
1374 return result;
1377 static __poll_t snd_rawmidi_poll(struct file *file, poll_table * wait)
1379 struct snd_rawmidi_file *rfile;
1380 struct snd_rawmidi_runtime *runtime;
1381 __poll_t mask;
1383 rfile = file->private_data;
1384 if (rfile->input != NULL) {
1385 runtime = rfile->input->runtime;
1386 snd_rawmidi_input_trigger(rfile->input, 1);
1387 poll_wait(file, &runtime->sleep, wait);
1389 if (rfile->output != NULL) {
1390 runtime = rfile->output->runtime;
1391 poll_wait(file, &runtime->sleep, wait);
1393 mask = 0;
1394 if (rfile->input != NULL) {
1395 if (snd_rawmidi_ready(rfile->input))
1396 mask |= EPOLLIN | EPOLLRDNORM;
1398 if (rfile->output != NULL) {
1399 if (snd_rawmidi_ready(rfile->output))
1400 mask |= EPOLLOUT | EPOLLWRNORM;
1402 return mask;
1407 #ifdef CONFIG_COMPAT
1408 #include "rawmidi_compat.c"
1409 #else
1410 #define snd_rawmidi_ioctl_compat NULL
1411 #endif
1417 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1418 struct snd_info_buffer *buffer)
1420 struct snd_rawmidi *rmidi;
1421 struct snd_rawmidi_substream *substream;
1422 struct snd_rawmidi_runtime *runtime;
1424 rmidi = entry->private_data;
1425 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1426 mutex_lock(&rmidi->open_mutex);
1427 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1428 list_for_each_entry(substream,
1429 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1430 list) {
1431 snd_iprintf(buffer,
1432 "Output %d\n"
1433 " Tx bytes : %lu\n",
1434 substream->number,
1435 (unsigned long) substream->bytes);
1436 if (substream->opened) {
1437 snd_iprintf(buffer,
1438 " Owner PID : %d\n",
1439 pid_vnr(substream->pid));
1440 runtime = substream->runtime;
1441 snd_iprintf(buffer,
1442 " Mode : %s\n"
1443 " Buffer size : %lu\n"
1444 " Avail : %lu\n",
1445 runtime->oss ? "OSS compatible" : "native",
1446 (unsigned long) runtime->buffer_size,
1447 (unsigned long) runtime->avail);
1451 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1452 list_for_each_entry(substream,
1453 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1454 list) {
1455 snd_iprintf(buffer,
1456 "Input %d\n"
1457 " Rx bytes : %lu\n",
1458 substream->number,
1459 (unsigned long) substream->bytes);
1460 if (substream->opened) {
1461 snd_iprintf(buffer,
1462 " Owner PID : %d\n",
1463 pid_vnr(substream->pid));
1464 runtime = substream->runtime;
1465 snd_iprintf(buffer,
1466 " Buffer size : %lu\n"
1467 " Avail : %lu\n"
1468 " Overruns : %lu\n",
1469 (unsigned long) runtime->buffer_size,
1470 (unsigned long) runtime->avail,
1471 (unsigned long) runtime->xruns);
1475 mutex_unlock(&rmidi->open_mutex);
1479 * Register functions
1482 static const struct file_operations snd_rawmidi_f_ops =
1484 .owner = THIS_MODULE,
1485 .read = snd_rawmidi_read,
1486 .write = snd_rawmidi_write,
1487 .open = snd_rawmidi_open,
1488 .release = snd_rawmidi_release,
1489 .llseek = no_llseek,
1490 .poll = snd_rawmidi_poll,
1491 .unlocked_ioctl = snd_rawmidi_ioctl,
1492 .compat_ioctl = snd_rawmidi_ioctl_compat,
1495 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1496 struct snd_rawmidi_str *stream,
1497 int direction,
1498 int count)
1500 struct snd_rawmidi_substream *substream;
1501 int idx;
1503 for (idx = 0; idx < count; idx++) {
1504 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1505 if (!substream)
1506 return -ENOMEM;
1507 substream->stream = direction;
1508 substream->number = idx;
1509 substream->rmidi = rmidi;
1510 substream->pstr = stream;
1511 list_add_tail(&substream->list, &stream->substreams);
1512 stream->substream_count++;
1514 return 0;
1517 static void release_rawmidi_device(struct device *dev)
1519 kfree(container_of(dev, struct snd_rawmidi, dev));
1523 * snd_rawmidi_new - create a rawmidi instance
1524 * @card: the card instance
1525 * @id: the id string
1526 * @device: the device index
1527 * @output_count: the number of output streams
1528 * @input_count: the number of input streams
1529 * @rrawmidi: the pointer to store the new rawmidi instance
1531 * Creates a new rawmidi instance.
1532 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1534 * Return: Zero if successful, or a negative error code on failure.
1536 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1537 int output_count, int input_count,
1538 struct snd_rawmidi ** rrawmidi)
1540 struct snd_rawmidi *rmidi;
1541 int err;
1542 static struct snd_device_ops ops = {
1543 .dev_free = snd_rawmidi_dev_free,
1544 .dev_register = snd_rawmidi_dev_register,
1545 .dev_disconnect = snd_rawmidi_dev_disconnect,
1548 if (snd_BUG_ON(!card))
1549 return -ENXIO;
1550 if (rrawmidi)
1551 *rrawmidi = NULL;
1552 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1553 if (!rmidi)
1554 return -ENOMEM;
1555 rmidi->card = card;
1556 rmidi->device = device;
1557 mutex_init(&rmidi->open_mutex);
1558 init_waitqueue_head(&rmidi->open_wait);
1559 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1560 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1562 if (id != NULL)
1563 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1565 snd_device_initialize(&rmidi->dev, card);
1566 rmidi->dev.release = release_rawmidi_device;
1567 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1569 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1570 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1571 SNDRV_RAWMIDI_STREAM_INPUT,
1572 input_count)) < 0) {
1573 snd_rawmidi_free(rmidi);
1574 return err;
1576 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1577 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1578 SNDRV_RAWMIDI_STREAM_OUTPUT,
1579 output_count)) < 0) {
1580 snd_rawmidi_free(rmidi);
1581 return err;
1583 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1584 snd_rawmidi_free(rmidi);
1585 return err;
1587 if (rrawmidi)
1588 *rrawmidi = rmidi;
1589 return 0;
1591 EXPORT_SYMBOL(snd_rawmidi_new);
1593 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1595 struct snd_rawmidi_substream *substream;
1597 while (!list_empty(&stream->substreams)) {
1598 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1599 list_del(&substream->list);
1600 kfree(substream);
1604 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1606 if (!rmidi)
1607 return 0;
1609 snd_info_free_entry(rmidi->proc_entry);
1610 rmidi->proc_entry = NULL;
1611 mutex_lock(&register_mutex);
1612 if (rmidi->ops && rmidi->ops->dev_unregister)
1613 rmidi->ops->dev_unregister(rmidi);
1614 mutex_unlock(&register_mutex);
1616 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1617 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1618 if (rmidi->private_free)
1619 rmidi->private_free(rmidi);
1620 put_device(&rmidi->dev);
1621 return 0;
1624 static int snd_rawmidi_dev_free(struct snd_device *device)
1626 struct snd_rawmidi *rmidi = device->device_data;
1627 return snd_rawmidi_free(rmidi);
1630 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1631 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1633 struct snd_rawmidi *rmidi = device->private_data;
1634 rmidi->seq_dev = NULL;
1636 #endif
1638 static int snd_rawmidi_dev_register(struct snd_device *device)
1640 int err;
1641 struct snd_info_entry *entry;
1642 char name[16];
1643 struct snd_rawmidi *rmidi = device->device_data;
1645 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1646 return -ENOMEM;
1647 mutex_lock(&register_mutex);
1648 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1649 mutex_unlock(&register_mutex);
1650 return -EBUSY;
1652 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1653 mutex_unlock(&register_mutex);
1654 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1655 rmidi->card, rmidi->device,
1656 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
1657 if (err < 0) {
1658 rmidi_err(rmidi, "unable to register\n");
1659 mutex_lock(&register_mutex);
1660 list_del(&rmidi->list);
1661 mutex_unlock(&register_mutex);
1662 return err;
1664 if (rmidi->ops && rmidi->ops->dev_register &&
1665 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1666 snd_unregister_device(&rmidi->dev);
1667 mutex_lock(&register_mutex);
1668 list_del(&rmidi->list);
1669 mutex_unlock(&register_mutex);
1670 return err;
1672 #ifdef CONFIG_SND_OSSEMUL
1673 rmidi->ossreg = 0;
1674 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1675 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1676 rmidi->card, 0, &snd_rawmidi_f_ops,
1677 rmidi) < 0) {
1678 rmidi_err(rmidi,
1679 "unable to register OSS rawmidi device %i:%i\n",
1680 rmidi->card->number, 0);
1681 } else {
1682 rmidi->ossreg++;
1683 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1684 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1685 #endif
1688 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1689 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1690 rmidi->card, 1, &snd_rawmidi_f_ops,
1691 rmidi) < 0) {
1692 rmidi_err(rmidi,
1693 "unable to register OSS rawmidi device %i:%i\n",
1694 rmidi->card->number, 1);
1695 } else {
1696 rmidi->ossreg++;
1699 #endif /* CONFIG_SND_OSSEMUL */
1700 sprintf(name, "midi%d", rmidi->device);
1701 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1702 if (entry) {
1703 entry->private_data = rmidi;
1704 entry->c.text.read = snd_rawmidi_proc_info_read;
1705 if (snd_info_register(entry) < 0) {
1706 snd_info_free_entry(entry);
1707 entry = NULL;
1710 rmidi->proc_entry = entry;
1711 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1712 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1713 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1714 rmidi->seq_dev->private_data = rmidi;
1715 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1716 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1717 snd_device_register(rmidi->card, rmidi->seq_dev);
1720 #endif
1721 return 0;
1724 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1726 struct snd_rawmidi *rmidi = device->device_data;
1727 int dir;
1729 mutex_lock(&register_mutex);
1730 mutex_lock(&rmidi->open_mutex);
1731 wake_up(&rmidi->open_wait);
1732 list_del_init(&rmidi->list);
1733 for (dir = 0; dir < 2; dir++) {
1734 struct snd_rawmidi_substream *s;
1735 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1736 if (s->runtime)
1737 wake_up(&s->runtime->sleep);
1741 #ifdef CONFIG_SND_OSSEMUL
1742 if (rmidi->ossreg) {
1743 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1744 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1745 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1746 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1747 #endif
1749 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1750 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1751 rmidi->ossreg = 0;
1753 #endif /* CONFIG_SND_OSSEMUL */
1754 snd_unregister_device(&rmidi->dev);
1755 mutex_unlock(&rmidi->open_mutex);
1756 mutex_unlock(&register_mutex);
1757 return 0;
1761 * snd_rawmidi_set_ops - set the rawmidi operators
1762 * @rmidi: the rawmidi instance
1763 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1764 * @ops: the operator table
1766 * Sets the rawmidi operators for the given stream direction.
1768 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1769 const struct snd_rawmidi_ops *ops)
1771 struct snd_rawmidi_substream *substream;
1773 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1774 substream->ops = ops;
1776 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1779 * ENTRY functions
1782 static int __init alsa_rawmidi_init(void)
1785 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1786 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1787 #ifdef CONFIG_SND_OSSEMUL
1788 { int i;
1789 /* check device map table */
1790 for (i = 0; i < SNDRV_CARDS; i++) {
1791 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1792 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1793 i, midi_map[i]);
1794 midi_map[i] = 0;
1796 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1797 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1798 i, amidi_map[i]);
1799 amidi_map[i] = 1;
1803 #endif /* CONFIG_SND_OSSEMUL */
1804 return 0;
1807 static void __exit alsa_rawmidi_exit(void)
1809 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1810 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1813 module_init(alsa_rawmidi_init)
1814 module_exit(alsa_rawmidi_exit)