]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - sound/core/rawmidi.c
Revert "SAUCE: pci: prevent sk hynix nvme from entering D3"
[mirror_ubuntu-bionic-kernel.git] / sound / core / rawmidi.c
1 /*
2 * Abstract layer for MIDI v1.0 stream
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 *
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
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 <linux/nospec.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>
38
39 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
40 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
41 MODULE_LICENSE("GPL");
42
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 */
51
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);
56
57 static LIST_HEAD(snd_rawmidi_devices);
58 static DEFINE_MUTEX(register_mutex);
59
60 #define rmidi_err(rmidi, fmt, args...) \
61 dev_err(&(rmidi)->dev, fmt, ##args)
62 #define rmidi_warn(rmidi, fmt, args...) \
63 dev_warn(&(rmidi)->dev, fmt, ##args)
64 #define rmidi_dbg(rmidi, fmt, args...) \
65 dev_dbg(&(rmidi)->dev, fmt, ##args)
66
67 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
68 {
69 struct snd_rawmidi *rawmidi;
70
71 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
72 if (rawmidi->card == card && rawmidi->device == device)
73 return rawmidi;
74 return NULL;
75 }
76
77 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
78 {
79 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
80 case FMODE_WRITE:
81 return SNDRV_RAWMIDI_LFLG_OUTPUT;
82 case FMODE_READ:
83 return SNDRV_RAWMIDI_LFLG_INPUT;
84 default:
85 return SNDRV_RAWMIDI_LFLG_OPEN;
86 }
87 }
88
89 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
90 {
91 struct snd_rawmidi_runtime *runtime = substream->runtime;
92 return runtime->avail >= runtime->avail_min;
93 }
94
95 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
96 size_t count)
97 {
98 struct snd_rawmidi_runtime *runtime = substream->runtime;
99 return runtime->avail >= runtime->avail_min &&
100 (!substream->append || runtime->avail >= count);
101 }
102
103 static void snd_rawmidi_input_event_work(struct work_struct *work)
104 {
105 struct snd_rawmidi_runtime *runtime =
106 container_of(work, struct snd_rawmidi_runtime, event_work);
107 if (runtime->event)
108 runtime->event(runtime->substream);
109 }
110
111 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
112 {
113 struct snd_rawmidi_runtime *runtime;
114
115 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
116 return -ENOMEM;
117 runtime->substream = substream;
118 spin_lock_init(&runtime->lock);
119 init_waitqueue_head(&runtime->sleep);
120 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
121 runtime->event = NULL;
122 runtime->buffer_size = PAGE_SIZE;
123 runtime->avail_min = 1;
124 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
125 runtime->avail = 0;
126 else
127 runtime->avail = runtime->buffer_size;
128 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
129 kfree(runtime);
130 return -ENOMEM;
131 }
132 runtime->appl_ptr = runtime->hw_ptr = 0;
133 substream->runtime = runtime;
134 return 0;
135 }
136
137 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
138 {
139 struct snd_rawmidi_runtime *runtime = substream->runtime;
140
141 kfree(runtime->buffer);
142 kfree(runtime);
143 substream->runtime = NULL;
144 return 0;
145 }
146
147 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
148 {
149 if (!substream->opened)
150 return;
151 substream->ops->trigger(substream, up);
152 }
153
154 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
155 {
156 if (!substream->opened)
157 return;
158 substream->ops->trigger(substream, up);
159 if (!up)
160 cancel_work_sync(&substream->runtime->event_work);
161 }
162
163 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
164 {
165 unsigned long flags;
166 struct snd_rawmidi_runtime *runtime = substream->runtime;
167
168 snd_rawmidi_output_trigger(substream, 0);
169 runtime->drain = 0;
170 spin_lock_irqsave(&runtime->lock, flags);
171 runtime->appl_ptr = runtime->hw_ptr = 0;
172 runtime->avail = runtime->buffer_size;
173 spin_unlock_irqrestore(&runtime->lock, flags);
174 return 0;
175 }
176 EXPORT_SYMBOL(snd_rawmidi_drop_output);
177
178 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
179 {
180 int err;
181 long timeout;
182 struct snd_rawmidi_runtime *runtime = substream->runtime;
183
184 err = 0;
185 runtime->drain = 1;
186 timeout = wait_event_interruptible_timeout(runtime->sleep,
187 (runtime->avail >= runtime->buffer_size),
188 10*HZ);
189 if (signal_pending(current))
190 err = -ERESTARTSYS;
191 if (runtime->avail < runtime->buffer_size && !timeout) {
192 rmidi_warn(substream->rmidi,
193 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
194 (long)runtime->avail, (long)runtime->buffer_size);
195 err = -EIO;
196 }
197 runtime->drain = 0;
198 if (err != -ERESTARTSYS) {
199 /* we need wait a while to make sure that Tx FIFOs are empty */
200 if (substream->ops->drain)
201 substream->ops->drain(substream);
202 else
203 msleep(50);
204 snd_rawmidi_drop_output(substream);
205 }
206 return err;
207 }
208 EXPORT_SYMBOL(snd_rawmidi_drain_output);
209
210 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
211 {
212 unsigned long flags;
213 struct snd_rawmidi_runtime *runtime = substream->runtime;
214
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;
222 }
223 EXPORT_SYMBOL(snd_rawmidi_drain_input);
224
225 /* look for an available substream for the given stream direction;
226 * if a specific subdevice is given, try to assign it
227 */
228 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
229 int stream, int mode,
230 struct snd_rawmidi_substream **sub_ret)
231 {
232 struct snd_rawmidi_substream *substream;
233 struct snd_rawmidi_str *s = &rmidi->streams[stream];
234 static unsigned int info_flags[2] = {
235 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
236 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
237 };
238
239 if (!(rmidi->info_flags & info_flags[stream]))
240 return -ENXIO;
241 if (subdevice >= 0 && subdevice >= s->substream_count)
242 return -ENODEV;
243
244 list_for_each_entry(substream, &s->substreams, list) {
245 if (substream->opened) {
246 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
247 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
248 !substream->append)
249 continue;
250 }
251 if (subdevice < 0 || subdevice == substream->number) {
252 *sub_ret = substream;
253 return 0;
254 }
255 }
256 return -EAGAIN;
257 }
258
259 /* open and do ref-counting for the given substream */
260 static int open_substream(struct snd_rawmidi *rmidi,
261 struct snd_rawmidi_substream *substream,
262 int mode)
263 {
264 int err;
265
266 if (substream->use_count == 0) {
267 err = snd_rawmidi_runtime_create(substream);
268 if (err < 0)
269 return err;
270 err = substream->ops->open(substream);
271 if (err < 0) {
272 snd_rawmidi_runtime_free(substream);
273 return err;
274 }
275 substream->opened = 1;
276 substream->active_sensing = 0;
277 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
278 substream->append = 1;
279 substream->pid = get_pid(task_pid(current));
280 rmidi->streams[substream->stream].substream_opened++;
281 }
282 substream->use_count++;
283 return 0;
284 }
285
286 static void close_substream(struct snd_rawmidi *rmidi,
287 struct snd_rawmidi_substream *substream,
288 int cleanup);
289
290 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
291 struct snd_rawmidi_file *rfile)
292 {
293 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
294 int err;
295
296 rfile->input = rfile->output = NULL;
297 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
298 err = assign_substream(rmidi, subdevice,
299 SNDRV_RAWMIDI_STREAM_INPUT,
300 mode, &sinput);
301 if (err < 0)
302 return err;
303 }
304 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
305 err = assign_substream(rmidi, subdevice,
306 SNDRV_RAWMIDI_STREAM_OUTPUT,
307 mode, &soutput);
308 if (err < 0)
309 return err;
310 }
311
312 if (sinput) {
313 err = open_substream(rmidi, sinput, mode);
314 if (err < 0)
315 return err;
316 }
317 if (soutput) {
318 err = open_substream(rmidi, soutput, mode);
319 if (err < 0) {
320 if (sinput)
321 close_substream(rmidi, sinput, 0);
322 return err;
323 }
324 }
325
326 rfile->rmidi = rmidi;
327 rfile->input = sinput;
328 rfile->output = soutput;
329 return 0;
330 }
331
332 /* called from sound/core/seq/seq_midi.c */
333 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
334 int mode, struct snd_rawmidi_file * rfile)
335 {
336 struct snd_rawmidi *rmidi;
337 int err;
338
339 if (snd_BUG_ON(!rfile))
340 return -EINVAL;
341
342 mutex_lock(&register_mutex);
343 rmidi = snd_rawmidi_search(card, device);
344 if (rmidi == NULL) {
345 mutex_unlock(&register_mutex);
346 return -ENODEV;
347 }
348 if (!try_module_get(rmidi->card->module)) {
349 mutex_unlock(&register_mutex);
350 return -ENXIO;
351 }
352 mutex_unlock(&register_mutex);
353
354 mutex_lock(&rmidi->open_mutex);
355 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
356 mutex_unlock(&rmidi->open_mutex);
357 if (err < 0)
358 module_put(rmidi->card->module);
359 return err;
360 }
361 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
362
363 static int snd_rawmidi_open(struct inode *inode, struct file *file)
364 {
365 int maj = imajor(inode);
366 struct snd_card *card;
367 int subdevice;
368 unsigned short fflags;
369 int err;
370 struct snd_rawmidi *rmidi;
371 struct snd_rawmidi_file *rawmidi_file = NULL;
372 wait_queue_entry_t wait;
373
374 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
375 return -EINVAL; /* invalid combination */
376
377 err = nonseekable_open(inode, file);
378 if (err < 0)
379 return err;
380
381 if (maj == snd_major) {
382 rmidi = snd_lookup_minor_data(iminor(inode),
383 SNDRV_DEVICE_TYPE_RAWMIDI);
384 #ifdef CONFIG_SND_OSSEMUL
385 } else if (maj == SOUND_MAJOR) {
386 rmidi = snd_lookup_oss_minor_data(iminor(inode),
387 SNDRV_OSS_DEVICE_TYPE_MIDI);
388 #endif
389 } else
390 return -ENXIO;
391
392 if (rmidi == NULL)
393 return -ENODEV;
394
395 if (!try_module_get(rmidi->card->module)) {
396 snd_card_unref(rmidi->card);
397 return -ENXIO;
398 }
399
400 mutex_lock(&rmidi->open_mutex);
401 card = rmidi->card;
402 err = snd_card_file_add(card, file);
403 if (err < 0)
404 goto __error_card;
405 fflags = snd_rawmidi_file_flags(file);
406 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
407 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
408 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
409 if (rawmidi_file == NULL) {
410 err = -ENOMEM;
411 goto __error;
412 }
413 init_waitqueue_entry(&wait, current);
414 add_wait_queue(&rmidi->open_wait, &wait);
415 while (1) {
416 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
417 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
418 if (err >= 0)
419 break;
420 if (err == -EAGAIN) {
421 if (file->f_flags & O_NONBLOCK) {
422 err = -EBUSY;
423 break;
424 }
425 } else
426 break;
427 set_current_state(TASK_INTERRUPTIBLE);
428 mutex_unlock(&rmidi->open_mutex);
429 schedule();
430 mutex_lock(&rmidi->open_mutex);
431 if (rmidi->card->shutdown) {
432 err = -ENODEV;
433 break;
434 }
435 if (signal_pending(current)) {
436 err = -ERESTARTSYS;
437 break;
438 }
439 }
440 remove_wait_queue(&rmidi->open_wait, &wait);
441 if (err < 0) {
442 kfree(rawmidi_file);
443 goto __error;
444 }
445 #ifdef CONFIG_SND_OSSEMUL
446 if (rawmidi_file->input && rawmidi_file->input->runtime)
447 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
448 if (rawmidi_file->output && rawmidi_file->output->runtime)
449 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
450 #endif
451 file->private_data = rawmidi_file;
452 mutex_unlock(&rmidi->open_mutex);
453 snd_card_unref(rmidi->card);
454 return 0;
455
456 __error:
457 snd_card_file_remove(card, file);
458 __error_card:
459 mutex_unlock(&rmidi->open_mutex);
460 module_put(rmidi->card->module);
461 snd_card_unref(rmidi->card);
462 return err;
463 }
464
465 static void close_substream(struct snd_rawmidi *rmidi,
466 struct snd_rawmidi_substream *substream,
467 int cleanup)
468 {
469 if (--substream->use_count)
470 return;
471
472 if (cleanup) {
473 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
474 snd_rawmidi_input_trigger(substream, 0);
475 else {
476 if (substream->active_sensing) {
477 unsigned char buf = 0xfe;
478 /* sending single active sensing message
479 * to shut the device up
480 */
481 snd_rawmidi_kernel_write(substream, &buf, 1);
482 }
483 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
484 snd_rawmidi_output_trigger(substream, 0);
485 }
486 }
487 substream->ops->close(substream);
488 if (substream->runtime->private_free)
489 substream->runtime->private_free(substream);
490 snd_rawmidi_runtime_free(substream);
491 substream->opened = 0;
492 substream->append = 0;
493 put_pid(substream->pid);
494 substream->pid = NULL;
495 rmidi->streams[substream->stream].substream_opened--;
496 }
497
498 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
499 {
500 struct snd_rawmidi *rmidi;
501
502 rmidi = rfile->rmidi;
503 mutex_lock(&rmidi->open_mutex);
504 if (rfile->input) {
505 close_substream(rmidi, rfile->input, 1);
506 rfile->input = NULL;
507 }
508 if (rfile->output) {
509 close_substream(rmidi, rfile->output, 1);
510 rfile->output = NULL;
511 }
512 rfile->rmidi = NULL;
513 mutex_unlock(&rmidi->open_mutex);
514 wake_up(&rmidi->open_wait);
515 }
516
517 /* called from sound/core/seq/seq_midi.c */
518 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
519 {
520 struct snd_rawmidi *rmidi;
521
522 if (snd_BUG_ON(!rfile))
523 return -ENXIO;
524
525 rmidi = rfile->rmidi;
526 rawmidi_release_priv(rfile);
527 module_put(rmidi->card->module);
528 return 0;
529 }
530 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
531
532 static int snd_rawmidi_release(struct inode *inode, struct file *file)
533 {
534 struct snd_rawmidi_file *rfile;
535 struct snd_rawmidi *rmidi;
536 struct module *module;
537
538 rfile = file->private_data;
539 rmidi = rfile->rmidi;
540 rawmidi_release_priv(rfile);
541 kfree(rfile);
542 module = rmidi->card->module;
543 snd_card_file_remove(rmidi->card, file);
544 module_put(module);
545 return 0;
546 }
547
548 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
549 struct snd_rawmidi_info *info)
550 {
551 struct snd_rawmidi *rmidi;
552
553 if (substream == NULL)
554 return -ENODEV;
555 rmidi = substream->rmidi;
556 memset(info, 0, sizeof(*info));
557 info->card = rmidi->card->number;
558 info->device = rmidi->device;
559 info->subdevice = substream->number;
560 info->stream = substream->stream;
561 info->flags = rmidi->info_flags;
562 strcpy(info->id, rmidi->id);
563 strcpy(info->name, rmidi->name);
564 strcpy(info->subname, substream->name);
565 info->subdevices_count = substream->pstr->substream_count;
566 info->subdevices_avail = (substream->pstr->substream_count -
567 substream->pstr->substream_opened);
568 return 0;
569 }
570
571 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
572 struct snd_rawmidi_info __user * _info)
573 {
574 struct snd_rawmidi_info info;
575 int err;
576 if ((err = snd_rawmidi_info(substream, &info)) < 0)
577 return err;
578 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
579 return -EFAULT;
580 return 0;
581 }
582
583 static int __snd_rawmidi_info_select(struct snd_card *card,
584 struct snd_rawmidi_info *info)
585 {
586 struct snd_rawmidi *rmidi;
587 struct snd_rawmidi_str *pstr;
588 struct snd_rawmidi_substream *substream;
589
590 rmidi = snd_rawmidi_search(card, info->device);
591 if (!rmidi)
592 return -ENXIO;
593 if (info->stream < 0 || info->stream > 1)
594 return -EINVAL;
595 info->stream = array_index_nospec(info->stream, 2);
596 pstr = &rmidi->streams[info->stream];
597 if (pstr->substream_count == 0)
598 return -ENOENT;
599 if (info->subdevice >= pstr->substream_count)
600 return -ENXIO;
601 list_for_each_entry(substream, &pstr->substreams, list) {
602 if ((unsigned int)substream->number == info->subdevice)
603 return snd_rawmidi_info(substream, info);
604 }
605 return -ENXIO;
606 }
607
608 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
609 {
610 int ret;
611
612 mutex_lock(&register_mutex);
613 ret = __snd_rawmidi_info_select(card, info);
614 mutex_unlock(&register_mutex);
615 return ret;
616 }
617 EXPORT_SYMBOL(snd_rawmidi_info_select);
618
619 static int snd_rawmidi_info_select_user(struct snd_card *card,
620 struct snd_rawmidi_info __user *_info)
621 {
622 int err;
623 struct snd_rawmidi_info info;
624 if (get_user(info.device, &_info->device))
625 return -EFAULT;
626 if (get_user(info.stream, &_info->stream))
627 return -EFAULT;
628 if (get_user(info.subdevice, &_info->subdevice))
629 return -EFAULT;
630 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
631 return err;
632 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
633 return -EFAULT;
634 return 0;
635 }
636
637 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
638 struct snd_rawmidi_params * params)
639 {
640 char *newbuf, *oldbuf;
641 struct snd_rawmidi_runtime *runtime = substream->runtime;
642
643 if (substream->append && substream->use_count > 1)
644 return -EBUSY;
645 snd_rawmidi_drain_output(substream);
646 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
647 return -EINVAL;
648 }
649 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
650 return -EINVAL;
651 }
652 if (params->buffer_size != runtime->buffer_size) {
653 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
654 if (!newbuf)
655 return -ENOMEM;
656 spin_lock_irq(&runtime->lock);
657 oldbuf = runtime->buffer;
658 runtime->buffer = newbuf;
659 runtime->buffer_size = params->buffer_size;
660 runtime->avail = runtime->buffer_size;
661 runtime->appl_ptr = runtime->hw_ptr = 0;
662 spin_unlock_irq(&runtime->lock);
663 kfree(oldbuf);
664 }
665 runtime->avail_min = params->avail_min;
666 substream->active_sensing = !params->no_active_sensing;
667 return 0;
668 }
669 EXPORT_SYMBOL(snd_rawmidi_output_params);
670
671 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
672 struct snd_rawmidi_params * params)
673 {
674 char *newbuf, *oldbuf;
675 struct snd_rawmidi_runtime *runtime = substream->runtime;
676
677 snd_rawmidi_drain_input(substream);
678 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
679 return -EINVAL;
680 }
681 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
682 return -EINVAL;
683 }
684 if (params->buffer_size != runtime->buffer_size) {
685 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
686 if (!newbuf)
687 return -ENOMEM;
688 spin_lock_irq(&runtime->lock);
689 oldbuf = runtime->buffer;
690 runtime->buffer = newbuf;
691 runtime->buffer_size = params->buffer_size;
692 runtime->appl_ptr = runtime->hw_ptr = 0;
693 spin_unlock_irq(&runtime->lock);
694 kfree(oldbuf);
695 }
696 runtime->avail_min = params->avail_min;
697 return 0;
698 }
699 EXPORT_SYMBOL(snd_rawmidi_input_params);
700
701 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
702 struct snd_rawmidi_status * status)
703 {
704 struct snd_rawmidi_runtime *runtime = substream->runtime;
705
706 memset(status, 0, sizeof(*status));
707 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
708 spin_lock_irq(&runtime->lock);
709 status->avail = runtime->avail;
710 spin_unlock_irq(&runtime->lock);
711 return 0;
712 }
713
714 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
715 struct snd_rawmidi_status * status)
716 {
717 struct snd_rawmidi_runtime *runtime = substream->runtime;
718
719 memset(status, 0, sizeof(*status));
720 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
721 spin_lock_irq(&runtime->lock);
722 status->avail = runtime->avail;
723 status->xruns = runtime->xruns;
724 runtime->xruns = 0;
725 spin_unlock_irq(&runtime->lock);
726 return 0;
727 }
728
729 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
730 {
731 struct snd_rawmidi_file *rfile;
732 void __user *argp = (void __user *)arg;
733
734 rfile = file->private_data;
735 if (((cmd >> 8) & 0xff) != 'W')
736 return -ENOTTY;
737 switch (cmd) {
738 case SNDRV_RAWMIDI_IOCTL_PVERSION:
739 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
740 case SNDRV_RAWMIDI_IOCTL_INFO:
741 {
742 int stream;
743 struct snd_rawmidi_info __user *info = argp;
744 if (get_user(stream, &info->stream))
745 return -EFAULT;
746 switch (stream) {
747 case SNDRV_RAWMIDI_STREAM_INPUT:
748 return snd_rawmidi_info_user(rfile->input, info);
749 case SNDRV_RAWMIDI_STREAM_OUTPUT:
750 return snd_rawmidi_info_user(rfile->output, info);
751 default:
752 return -EINVAL;
753 }
754 }
755 case SNDRV_RAWMIDI_IOCTL_PARAMS:
756 {
757 struct snd_rawmidi_params params;
758 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
759 return -EFAULT;
760 switch (params.stream) {
761 case SNDRV_RAWMIDI_STREAM_OUTPUT:
762 if (rfile->output == NULL)
763 return -EINVAL;
764 return snd_rawmidi_output_params(rfile->output, &params);
765 case SNDRV_RAWMIDI_STREAM_INPUT:
766 if (rfile->input == NULL)
767 return -EINVAL;
768 return snd_rawmidi_input_params(rfile->input, &params);
769 default:
770 return -EINVAL;
771 }
772 }
773 case SNDRV_RAWMIDI_IOCTL_STATUS:
774 {
775 int err = 0;
776 struct snd_rawmidi_status status;
777 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
778 return -EFAULT;
779 switch (status.stream) {
780 case SNDRV_RAWMIDI_STREAM_OUTPUT:
781 if (rfile->output == NULL)
782 return -EINVAL;
783 err = snd_rawmidi_output_status(rfile->output, &status);
784 break;
785 case SNDRV_RAWMIDI_STREAM_INPUT:
786 if (rfile->input == NULL)
787 return -EINVAL;
788 err = snd_rawmidi_input_status(rfile->input, &status);
789 break;
790 default:
791 return -EINVAL;
792 }
793 if (err < 0)
794 return err;
795 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
796 return -EFAULT;
797 return 0;
798 }
799 case SNDRV_RAWMIDI_IOCTL_DROP:
800 {
801 int val;
802 if (get_user(val, (int __user *) argp))
803 return -EFAULT;
804 switch (val) {
805 case SNDRV_RAWMIDI_STREAM_OUTPUT:
806 if (rfile->output == NULL)
807 return -EINVAL;
808 return snd_rawmidi_drop_output(rfile->output);
809 default:
810 return -EINVAL;
811 }
812 }
813 case SNDRV_RAWMIDI_IOCTL_DRAIN:
814 {
815 int val;
816 if (get_user(val, (int __user *) argp))
817 return -EFAULT;
818 switch (val) {
819 case SNDRV_RAWMIDI_STREAM_OUTPUT:
820 if (rfile->output == NULL)
821 return -EINVAL;
822 return snd_rawmidi_drain_output(rfile->output);
823 case SNDRV_RAWMIDI_STREAM_INPUT:
824 if (rfile->input == NULL)
825 return -EINVAL;
826 return snd_rawmidi_drain_input(rfile->input);
827 default:
828 return -EINVAL;
829 }
830 }
831 default:
832 rmidi_dbg(rfile->rmidi,
833 "rawmidi: unknown command = 0x%x\n", cmd);
834 }
835 return -ENOTTY;
836 }
837
838 static int snd_rawmidi_control_ioctl(struct snd_card *card,
839 struct snd_ctl_file *control,
840 unsigned int cmd,
841 unsigned long arg)
842 {
843 void __user *argp = (void __user *)arg;
844
845 switch (cmd) {
846 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
847 {
848 int device;
849
850 if (get_user(device, (int __user *)argp))
851 return -EFAULT;
852 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
853 device = SNDRV_RAWMIDI_DEVICES - 1;
854 mutex_lock(&register_mutex);
855 device = device < 0 ? 0 : device + 1;
856 while (device < SNDRV_RAWMIDI_DEVICES) {
857 if (snd_rawmidi_search(card, device))
858 break;
859 device++;
860 }
861 if (device == SNDRV_RAWMIDI_DEVICES)
862 device = -1;
863 mutex_unlock(&register_mutex);
864 if (put_user(device, (int __user *)argp))
865 return -EFAULT;
866 return 0;
867 }
868 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
869 {
870 int val;
871
872 if (get_user(val, (int __user *)argp))
873 return -EFAULT;
874 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
875 return 0;
876 }
877 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
878 return snd_rawmidi_info_select_user(card, argp);
879 }
880 return -ENOIOCTLCMD;
881 }
882
883 /**
884 * snd_rawmidi_receive - receive the input data from the device
885 * @substream: the rawmidi substream
886 * @buffer: the buffer pointer
887 * @count: the data size to read
888 *
889 * Reads the data from the internal buffer.
890 *
891 * Return: The size of read data, or a negative error code on failure.
892 */
893 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
894 const unsigned char *buffer, int count)
895 {
896 unsigned long flags;
897 int result = 0, count1;
898 struct snd_rawmidi_runtime *runtime = substream->runtime;
899
900 if (!substream->opened)
901 return -EBADFD;
902 if (runtime->buffer == NULL) {
903 rmidi_dbg(substream->rmidi,
904 "snd_rawmidi_receive: input is not active!!!\n");
905 return -EINVAL;
906 }
907 spin_lock_irqsave(&runtime->lock, flags);
908 if (count == 1) { /* special case, faster code */
909 substream->bytes++;
910 if (runtime->avail < runtime->buffer_size) {
911 runtime->buffer[runtime->hw_ptr++] = buffer[0];
912 runtime->hw_ptr %= runtime->buffer_size;
913 runtime->avail++;
914 result++;
915 } else {
916 runtime->xruns++;
917 }
918 } else {
919 substream->bytes += count;
920 count1 = runtime->buffer_size - runtime->hw_ptr;
921 if (count1 > count)
922 count1 = count;
923 if (count1 > (int)(runtime->buffer_size - runtime->avail))
924 count1 = runtime->buffer_size - runtime->avail;
925 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
926 runtime->hw_ptr += count1;
927 runtime->hw_ptr %= runtime->buffer_size;
928 runtime->avail += count1;
929 count -= count1;
930 result += count1;
931 if (count > 0) {
932 buffer += count1;
933 count1 = count;
934 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
935 count1 = runtime->buffer_size - runtime->avail;
936 runtime->xruns += count - count1;
937 }
938 if (count1 > 0) {
939 memcpy(runtime->buffer, buffer, count1);
940 runtime->hw_ptr = count1;
941 runtime->avail += count1;
942 result += count1;
943 }
944 }
945 }
946 if (result > 0) {
947 if (runtime->event)
948 schedule_work(&runtime->event_work);
949 else if (snd_rawmidi_ready(substream))
950 wake_up(&runtime->sleep);
951 }
952 spin_unlock_irqrestore(&runtime->lock, flags);
953 return result;
954 }
955 EXPORT_SYMBOL(snd_rawmidi_receive);
956
957 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
958 unsigned char __user *userbuf,
959 unsigned char *kernelbuf, long count)
960 {
961 unsigned long flags;
962 long result = 0, count1;
963 struct snd_rawmidi_runtime *runtime = substream->runtime;
964 unsigned long appl_ptr;
965
966 spin_lock_irqsave(&runtime->lock, flags);
967 while (count > 0 && runtime->avail) {
968 count1 = runtime->buffer_size - runtime->appl_ptr;
969 if (count1 > count)
970 count1 = count;
971 if (count1 > (int)runtime->avail)
972 count1 = runtime->avail;
973
974 /* update runtime->appl_ptr before unlocking for userbuf */
975 appl_ptr = runtime->appl_ptr;
976 runtime->appl_ptr += count1;
977 runtime->appl_ptr %= runtime->buffer_size;
978 runtime->avail -= count1;
979
980 if (kernelbuf)
981 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
982 if (userbuf) {
983 spin_unlock_irqrestore(&runtime->lock, flags);
984 if (copy_to_user(userbuf + result,
985 runtime->buffer + appl_ptr, count1)) {
986 return result > 0 ? result : -EFAULT;
987 }
988 spin_lock_irqsave(&runtime->lock, flags);
989 }
990 result += count1;
991 count -= count1;
992 }
993 spin_unlock_irqrestore(&runtime->lock, flags);
994 return result;
995 }
996
997 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
998 unsigned char *buf, long count)
999 {
1000 snd_rawmidi_input_trigger(substream, 1);
1001 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
1002 }
1003 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1004
1005 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1006 loff_t *offset)
1007 {
1008 long result;
1009 int count1;
1010 struct snd_rawmidi_file *rfile;
1011 struct snd_rawmidi_substream *substream;
1012 struct snd_rawmidi_runtime *runtime;
1013
1014 rfile = file->private_data;
1015 substream = rfile->input;
1016 if (substream == NULL)
1017 return -EIO;
1018 runtime = substream->runtime;
1019 snd_rawmidi_input_trigger(substream, 1);
1020 result = 0;
1021 while (count > 0) {
1022 spin_lock_irq(&runtime->lock);
1023 while (!snd_rawmidi_ready(substream)) {
1024 wait_queue_entry_t wait;
1025 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1026 spin_unlock_irq(&runtime->lock);
1027 return result > 0 ? result : -EAGAIN;
1028 }
1029 init_waitqueue_entry(&wait, current);
1030 add_wait_queue(&runtime->sleep, &wait);
1031 set_current_state(TASK_INTERRUPTIBLE);
1032 spin_unlock_irq(&runtime->lock);
1033 schedule();
1034 remove_wait_queue(&runtime->sleep, &wait);
1035 if (rfile->rmidi->card->shutdown)
1036 return -ENODEV;
1037 if (signal_pending(current))
1038 return result > 0 ? result : -ERESTARTSYS;
1039 if (!runtime->avail)
1040 return result > 0 ? result : -EIO;
1041 spin_lock_irq(&runtime->lock);
1042 }
1043 spin_unlock_irq(&runtime->lock);
1044 count1 = snd_rawmidi_kernel_read1(substream,
1045 (unsigned char __user *)buf,
1046 NULL/*kernelbuf*/,
1047 count);
1048 if (count1 < 0)
1049 return result > 0 ? result : count1;
1050 result += count1;
1051 buf += count1;
1052 count -= count1;
1053 }
1054 return result;
1055 }
1056
1057 /**
1058 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1059 * @substream: the rawmidi substream
1060 *
1061 * Return: 1 if the internal output buffer is empty, 0 if not.
1062 */
1063 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1064 {
1065 struct snd_rawmidi_runtime *runtime = substream->runtime;
1066 int result;
1067 unsigned long flags;
1068
1069 if (runtime->buffer == NULL) {
1070 rmidi_dbg(substream->rmidi,
1071 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1072 return 1;
1073 }
1074 spin_lock_irqsave(&runtime->lock, flags);
1075 result = runtime->avail >= runtime->buffer_size;
1076 spin_unlock_irqrestore(&runtime->lock, flags);
1077 return result;
1078 }
1079 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1080
1081 /**
1082 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1083 * @substream: the rawmidi substream
1084 * @buffer: the buffer pointer
1085 * @count: data size to transfer
1086 *
1087 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1088 */
1089 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1090 unsigned char *buffer, int count)
1091 {
1092 int result, count1;
1093 struct snd_rawmidi_runtime *runtime = substream->runtime;
1094
1095 if (runtime->buffer == NULL) {
1096 rmidi_dbg(substream->rmidi,
1097 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1098 return -EINVAL;
1099 }
1100 result = 0;
1101 if (runtime->avail >= runtime->buffer_size) {
1102 /* warning: lowlevel layer MUST trigger down the hardware */
1103 goto __skip;
1104 }
1105 if (count == 1) { /* special case, faster code */
1106 *buffer = runtime->buffer[runtime->hw_ptr];
1107 result++;
1108 } else {
1109 count1 = runtime->buffer_size - runtime->hw_ptr;
1110 if (count1 > count)
1111 count1 = count;
1112 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1113 count1 = runtime->buffer_size - runtime->avail;
1114 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1115 count -= count1;
1116 result += count1;
1117 if (count > 0) {
1118 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1119 count = runtime->buffer_size - runtime->avail - count1;
1120 memcpy(buffer + count1, runtime->buffer, count);
1121 result += count;
1122 }
1123 }
1124 __skip:
1125 return result;
1126 }
1127 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1128
1129 /**
1130 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1131 * @substream: the rawmidi substream
1132 * @buffer: the buffer pointer
1133 * @count: data size to transfer
1134 *
1135 * Copies data from the internal output buffer to the given buffer.
1136 *
1137 * Call this in the interrupt handler when the midi output is ready,
1138 * and call snd_rawmidi_transmit_ack() after the transmission is
1139 * finished.
1140 *
1141 * Return: The size of copied data, or a negative error code on failure.
1142 */
1143 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1144 unsigned char *buffer, int count)
1145 {
1146 struct snd_rawmidi_runtime *runtime = substream->runtime;
1147 int result;
1148 unsigned long flags;
1149
1150 spin_lock_irqsave(&runtime->lock, flags);
1151 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1152 spin_unlock_irqrestore(&runtime->lock, flags);
1153 return result;
1154 }
1155 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1156
1157 /**
1158 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1159 * @substream: the rawmidi substream
1160 * @count: the transferred count
1161 *
1162 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1163 */
1164 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1165 {
1166 struct snd_rawmidi_runtime *runtime = substream->runtime;
1167
1168 if (runtime->buffer == NULL) {
1169 rmidi_dbg(substream->rmidi,
1170 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1171 return -EINVAL;
1172 }
1173 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1174 runtime->hw_ptr += count;
1175 runtime->hw_ptr %= runtime->buffer_size;
1176 runtime->avail += count;
1177 substream->bytes += count;
1178 if (count > 0) {
1179 if (runtime->drain || snd_rawmidi_ready(substream))
1180 wake_up(&runtime->sleep);
1181 }
1182 return count;
1183 }
1184 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1185
1186 /**
1187 * snd_rawmidi_transmit_ack - acknowledge the transmission
1188 * @substream: the rawmidi substream
1189 * @count: the transferred count
1190 *
1191 * Advances the hardware pointer for the internal output buffer with
1192 * the given size and updates the condition.
1193 * Call after the transmission is finished.
1194 *
1195 * Return: The advanced size if successful, or a negative error code on failure.
1196 */
1197 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1198 {
1199 struct snd_rawmidi_runtime *runtime = substream->runtime;
1200 int result;
1201 unsigned long flags;
1202
1203 spin_lock_irqsave(&runtime->lock, flags);
1204 result = __snd_rawmidi_transmit_ack(substream, count);
1205 spin_unlock_irqrestore(&runtime->lock, flags);
1206 return result;
1207 }
1208 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1209
1210 /**
1211 * snd_rawmidi_transmit - copy from the buffer to the device
1212 * @substream: the rawmidi substream
1213 * @buffer: the buffer pointer
1214 * @count: the data size to transfer
1215 *
1216 * Copies data from the buffer to the device and advances the pointer.
1217 *
1218 * Return: The copied size if successful, or a negative error code on failure.
1219 */
1220 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1221 unsigned char *buffer, int count)
1222 {
1223 struct snd_rawmidi_runtime *runtime = substream->runtime;
1224 int result;
1225 unsigned long flags;
1226
1227 spin_lock_irqsave(&runtime->lock, flags);
1228 if (!substream->opened)
1229 result = -EBADFD;
1230 else {
1231 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1232 if (count <= 0)
1233 result = count;
1234 else
1235 result = __snd_rawmidi_transmit_ack(substream, count);
1236 }
1237 spin_unlock_irqrestore(&runtime->lock, flags);
1238 return result;
1239 }
1240 EXPORT_SYMBOL(snd_rawmidi_transmit);
1241
1242 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1243 const unsigned char __user *userbuf,
1244 const unsigned char *kernelbuf,
1245 long count)
1246 {
1247 unsigned long flags;
1248 long count1, result;
1249 struct snd_rawmidi_runtime *runtime = substream->runtime;
1250 unsigned long appl_ptr;
1251
1252 if (!kernelbuf && !userbuf)
1253 return -EINVAL;
1254 if (snd_BUG_ON(!runtime->buffer))
1255 return -EINVAL;
1256
1257 result = 0;
1258 spin_lock_irqsave(&runtime->lock, flags);
1259 if (substream->append) {
1260 if ((long)runtime->avail < count) {
1261 spin_unlock_irqrestore(&runtime->lock, flags);
1262 return -EAGAIN;
1263 }
1264 }
1265 while (count > 0 && runtime->avail > 0) {
1266 count1 = runtime->buffer_size - runtime->appl_ptr;
1267 if (count1 > count)
1268 count1 = count;
1269 if (count1 > (long)runtime->avail)
1270 count1 = runtime->avail;
1271
1272 /* update runtime->appl_ptr before unlocking for userbuf */
1273 appl_ptr = runtime->appl_ptr;
1274 runtime->appl_ptr += count1;
1275 runtime->appl_ptr %= runtime->buffer_size;
1276 runtime->avail -= count1;
1277
1278 if (kernelbuf)
1279 memcpy(runtime->buffer + appl_ptr,
1280 kernelbuf + result, count1);
1281 else if (userbuf) {
1282 spin_unlock_irqrestore(&runtime->lock, flags);
1283 if (copy_from_user(runtime->buffer + appl_ptr,
1284 userbuf + result, count1)) {
1285 spin_lock_irqsave(&runtime->lock, flags);
1286 result = result > 0 ? result : -EFAULT;
1287 goto __end;
1288 }
1289 spin_lock_irqsave(&runtime->lock, flags);
1290 }
1291 result += count1;
1292 count -= count1;
1293 }
1294 __end:
1295 count1 = runtime->avail < runtime->buffer_size;
1296 spin_unlock_irqrestore(&runtime->lock, flags);
1297 if (count1)
1298 snd_rawmidi_output_trigger(substream, 1);
1299 return result;
1300 }
1301
1302 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1303 const unsigned char *buf, long count)
1304 {
1305 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1306 }
1307 EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1308
1309 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1310 size_t count, loff_t *offset)
1311 {
1312 long result, timeout;
1313 int count1;
1314 struct snd_rawmidi_file *rfile;
1315 struct snd_rawmidi_runtime *runtime;
1316 struct snd_rawmidi_substream *substream;
1317
1318 rfile = file->private_data;
1319 substream = rfile->output;
1320 runtime = substream->runtime;
1321 /* we cannot put an atomic message to our buffer */
1322 if (substream->append && count > runtime->buffer_size)
1323 return -EIO;
1324 result = 0;
1325 while (count > 0) {
1326 spin_lock_irq(&runtime->lock);
1327 while (!snd_rawmidi_ready_append(substream, count)) {
1328 wait_queue_entry_t wait;
1329 if (file->f_flags & O_NONBLOCK) {
1330 spin_unlock_irq(&runtime->lock);
1331 return result > 0 ? result : -EAGAIN;
1332 }
1333 init_waitqueue_entry(&wait, current);
1334 add_wait_queue(&runtime->sleep, &wait);
1335 set_current_state(TASK_INTERRUPTIBLE);
1336 spin_unlock_irq(&runtime->lock);
1337 timeout = schedule_timeout(30 * HZ);
1338 remove_wait_queue(&runtime->sleep, &wait);
1339 if (rfile->rmidi->card->shutdown)
1340 return -ENODEV;
1341 if (signal_pending(current))
1342 return result > 0 ? result : -ERESTARTSYS;
1343 if (!runtime->avail && !timeout)
1344 return result > 0 ? result : -EIO;
1345 spin_lock_irq(&runtime->lock);
1346 }
1347 spin_unlock_irq(&runtime->lock);
1348 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1349 if (count1 < 0)
1350 return result > 0 ? result : count1;
1351 result += count1;
1352 buf += count1;
1353 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1354 break;
1355 count -= count1;
1356 }
1357 if (file->f_flags & O_DSYNC) {
1358 spin_lock_irq(&runtime->lock);
1359 while (runtime->avail != runtime->buffer_size) {
1360 wait_queue_entry_t wait;
1361 unsigned int last_avail = runtime->avail;
1362 init_waitqueue_entry(&wait, current);
1363 add_wait_queue(&runtime->sleep, &wait);
1364 set_current_state(TASK_INTERRUPTIBLE);
1365 spin_unlock_irq(&runtime->lock);
1366 timeout = schedule_timeout(30 * HZ);
1367 remove_wait_queue(&runtime->sleep, &wait);
1368 if (signal_pending(current))
1369 return result > 0 ? result : -ERESTARTSYS;
1370 if (runtime->avail == last_avail && !timeout)
1371 return result > 0 ? result : -EIO;
1372 spin_lock_irq(&runtime->lock);
1373 }
1374 spin_unlock_irq(&runtime->lock);
1375 }
1376 return result;
1377 }
1378
1379 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1380 {
1381 struct snd_rawmidi_file *rfile;
1382 struct snd_rawmidi_runtime *runtime;
1383 unsigned int mask;
1384
1385 rfile = file->private_data;
1386 if (rfile->input != NULL) {
1387 runtime = rfile->input->runtime;
1388 snd_rawmidi_input_trigger(rfile->input, 1);
1389 poll_wait(file, &runtime->sleep, wait);
1390 }
1391 if (rfile->output != NULL) {
1392 runtime = rfile->output->runtime;
1393 poll_wait(file, &runtime->sleep, wait);
1394 }
1395 mask = 0;
1396 if (rfile->input != NULL) {
1397 if (snd_rawmidi_ready(rfile->input))
1398 mask |= POLLIN | POLLRDNORM;
1399 }
1400 if (rfile->output != NULL) {
1401 if (snd_rawmidi_ready(rfile->output))
1402 mask |= POLLOUT | POLLWRNORM;
1403 }
1404 return mask;
1405 }
1406
1407 /*
1408 */
1409 #ifdef CONFIG_COMPAT
1410 #include "rawmidi_compat.c"
1411 #else
1412 #define snd_rawmidi_ioctl_compat NULL
1413 #endif
1414
1415 /*
1416
1417 */
1418
1419 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1420 struct snd_info_buffer *buffer)
1421 {
1422 struct snd_rawmidi *rmidi;
1423 struct snd_rawmidi_substream *substream;
1424 struct snd_rawmidi_runtime *runtime;
1425
1426 rmidi = entry->private_data;
1427 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1428 mutex_lock(&rmidi->open_mutex);
1429 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1430 list_for_each_entry(substream,
1431 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1432 list) {
1433 snd_iprintf(buffer,
1434 "Output %d\n"
1435 " Tx bytes : %lu\n",
1436 substream->number,
1437 (unsigned long) substream->bytes);
1438 if (substream->opened) {
1439 snd_iprintf(buffer,
1440 " Owner PID : %d\n",
1441 pid_vnr(substream->pid));
1442 runtime = substream->runtime;
1443 snd_iprintf(buffer,
1444 " Mode : %s\n"
1445 " Buffer size : %lu\n"
1446 " Avail : %lu\n",
1447 runtime->oss ? "OSS compatible" : "native",
1448 (unsigned long) runtime->buffer_size,
1449 (unsigned long) runtime->avail);
1450 }
1451 }
1452 }
1453 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1454 list_for_each_entry(substream,
1455 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1456 list) {
1457 snd_iprintf(buffer,
1458 "Input %d\n"
1459 " Rx bytes : %lu\n",
1460 substream->number,
1461 (unsigned long) substream->bytes);
1462 if (substream->opened) {
1463 snd_iprintf(buffer,
1464 " Owner PID : %d\n",
1465 pid_vnr(substream->pid));
1466 runtime = substream->runtime;
1467 snd_iprintf(buffer,
1468 " Buffer size : %lu\n"
1469 " Avail : %lu\n"
1470 " Overruns : %lu\n",
1471 (unsigned long) runtime->buffer_size,
1472 (unsigned long) runtime->avail,
1473 (unsigned long) runtime->xruns);
1474 }
1475 }
1476 }
1477 mutex_unlock(&rmidi->open_mutex);
1478 }
1479
1480 /*
1481 * Register functions
1482 */
1483
1484 static const struct file_operations snd_rawmidi_f_ops =
1485 {
1486 .owner = THIS_MODULE,
1487 .read = snd_rawmidi_read,
1488 .write = snd_rawmidi_write,
1489 .open = snd_rawmidi_open,
1490 .release = snd_rawmidi_release,
1491 .llseek = no_llseek,
1492 .poll = snd_rawmidi_poll,
1493 .unlocked_ioctl = snd_rawmidi_ioctl,
1494 .compat_ioctl = snd_rawmidi_ioctl_compat,
1495 };
1496
1497 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1498 struct snd_rawmidi_str *stream,
1499 int direction,
1500 int count)
1501 {
1502 struct snd_rawmidi_substream *substream;
1503 int idx;
1504
1505 for (idx = 0; idx < count; idx++) {
1506 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1507 if (!substream)
1508 return -ENOMEM;
1509 substream->stream = direction;
1510 substream->number = idx;
1511 substream->rmidi = rmidi;
1512 substream->pstr = stream;
1513 list_add_tail(&substream->list, &stream->substreams);
1514 stream->substream_count++;
1515 }
1516 return 0;
1517 }
1518
1519 static void release_rawmidi_device(struct device *dev)
1520 {
1521 kfree(container_of(dev, struct snd_rawmidi, dev));
1522 }
1523
1524 /**
1525 * snd_rawmidi_new - create a rawmidi instance
1526 * @card: the card instance
1527 * @id: the id string
1528 * @device: the device index
1529 * @output_count: the number of output streams
1530 * @input_count: the number of input streams
1531 * @rrawmidi: the pointer to store the new rawmidi instance
1532 *
1533 * Creates a new rawmidi instance.
1534 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1535 *
1536 * Return: Zero if successful, or a negative error code on failure.
1537 */
1538 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1539 int output_count, int input_count,
1540 struct snd_rawmidi ** rrawmidi)
1541 {
1542 struct snd_rawmidi *rmidi;
1543 int err;
1544 static struct snd_device_ops ops = {
1545 .dev_free = snd_rawmidi_dev_free,
1546 .dev_register = snd_rawmidi_dev_register,
1547 .dev_disconnect = snd_rawmidi_dev_disconnect,
1548 };
1549
1550 if (snd_BUG_ON(!card))
1551 return -ENXIO;
1552 if (rrawmidi)
1553 *rrawmidi = NULL;
1554 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1555 if (!rmidi)
1556 return -ENOMEM;
1557 rmidi->card = card;
1558 rmidi->device = device;
1559 mutex_init(&rmidi->open_mutex);
1560 init_waitqueue_head(&rmidi->open_wait);
1561 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1562 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1563
1564 if (id != NULL)
1565 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1566
1567 snd_device_initialize(&rmidi->dev, card);
1568 rmidi->dev.release = release_rawmidi_device;
1569 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1570
1571 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1572 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1573 SNDRV_RAWMIDI_STREAM_INPUT,
1574 input_count)) < 0) {
1575 snd_rawmidi_free(rmidi);
1576 return err;
1577 }
1578 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1579 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1580 SNDRV_RAWMIDI_STREAM_OUTPUT,
1581 output_count)) < 0) {
1582 snd_rawmidi_free(rmidi);
1583 return err;
1584 }
1585 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1586 snd_rawmidi_free(rmidi);
1587 return err;
1588 }
1589 if (rrawmidi)
1590 *rrawmidi = rmidi;
1591 return 0;
1592 }
1593 EXPORT_SYMBOL(snd_rawmidi_new);
1594
1595 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1596 {
1597 struct snd_rawmidi_substream *substream;
1598
1599 while (!list_empty(&stream->substreams)) {
1600 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1601 list_del(&substream->list);
1602 kfree(substream);
1603 }
1604 }
1605
1606 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1607 {
1608 if (!rmidi)
1609 return 0;
1610
1611 snd_info_free_entry(rmidi->proc_entry);
1612 rmidi->proc_entry = NULL;
1613 mutex_lock(&register_mutex);
1614 if (rmidi->ops && rmidi->ops->dev_unregister)
1615 rmidi->ops->dev_unregister(rmidi);
1616 mutex_unlock(&register_mutex);
1617
1618 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1619 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1620 if (rmidi->private_free)
1621 rmidi->private_free(rmidi);
1622 put_device(&rmidi->dev);
1623 return 0;
1624 }
1625
1626 static int snd_rawmidi_dev_free(struct snd_device *device)
1627 {
1628 struct snd_rawmidi *rmidi = device->device_data;
1629 return snd_rawmidi_free(rmidi);
1630 }
1631
1632 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1633 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1634 {
1635 struct snd_rawmidi *rmidi = device->private_data;
1636 rmidi->seq_dev = NULL;
1637 }
1638 #endif
1639
1640 static int snd_rawmidi_dev_register(struct snd_device *device)
1641 {
1642 int err;
1643 struct snd_info_entry *entry;
1644 char name[16];
1645 struct snd_rawmidi *rmidi = device->device_data;
1646
1647 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1648 return -ENOMEM;
1649 mutex_lock(&register_mutex);
1650 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1651 mutex_unlock(&register_mutex);
1652 return -EBUSY;
1653 }
1654 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1655 mutex_unlock(&register_mutex);
1656 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1657 rmidi->card, rmidi->device,
1658 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
1659 if (err < 0) {
1660 rmidi_err(rmidi, "unable to register\n");
1661 mutex_lock(&register_mutex);
1662 list_del(&rmidi->list);
1663 mutex_unlock(&register_mutex);
1664 return err;
1665 }
1666 if (rmidi->ops && rmidi->ops->dev_register &&
1667 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1668 snd_unregister_device(&rmidi->dev);
1669 mutex_lock(&register_mutex);
1670 list_del(&rmidi->list);
1671 mutex_unlock(&register_mutex);
1672 return err;
1673 }
1674 #ifdef CONFIG_SND_OSSEMUL
1675 rmidi->ossreg = 0;
1676 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1677 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1678 rmidi->card, 0, &snd_rawmidi_f_ops,
1679 rmidi) < 0) {
1680 rmidi_err(rmidi,
1681 "unable to register OSS rawmidi device %i:%i\n",
1682 rmidi->card->number, 0);
1683 } else {
1684 rmidi->ossreg++;
1685 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1686 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1687 #endif
1688 }
1689 }
1690 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1691 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1692 rmidi->card, 1, &snd_rawmidi_f_ops,
1693 rmidi) < 0) {
1694 rmidi_err(rmidi,
1695 "unable to register OSS rawmidi device %i:%i\n",
1696 rmidi->card->number, 1);
1697 } else {
1698 rmidi->ossreg++;
1699 }
1700 }
1701 #endif /* CONFIG_SND_OSSEMUL */
1702 sprintf(name, "midi%d", rmidi->device);
1703 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1704 if (entry) {
1705 entry->private_data = rmidi;
1706 entry->c.text.read = snd_rawmidi_proc_info_read;
1707 if (snd_info_register(entry) < 0) {
1708 snd_info_free_entry(entry);
1709 entry = NULL;
1710 }
1711 }
1712 rmidi->proc_entry = entry;
1713 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1714 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1715 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1716 rmidi->seq_dev->private_data = rmidi;
1717 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1718 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1719 snd_device_register(rmidi->card, rmidi->seq_dev);
1720 }
1721 }
1722 #endif
1723 return 0;
1724 }
1725
1726 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1727 {
1728 struct snd_rawmidi *rmidi = device->device_data;
1729 int dir;
1730
1731 mutex_lock(&register_mutex);
1732 mutex_lock(&rmidi->open_mutex);
1733 wake_up(&rmidi->open_wait);
1734 list_del_init(&rmidi->list);
1735 for (dir = 0; dir < 2; dir++) {
1736 struct snd_rawmidi_substream *s;
1737 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1738 if (s->runtime)
1739 wake_up(&s->runtime->sleep);
1740 }
1741 }
1742
1743 #ifdef CONFIG_SND_OSSEMUL
1744 if (rmidi->ossreg) {
1745 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1746 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1747 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1748 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1749 #endif
1750 }
1751 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1752 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1753 rmidi->ossreg = 0;
1754 }
1755 #endif /* CONFIG_SND_OSSEMUL */
1756 snd_unregister_device(&rmidi->dev);
1757 mutex_unlock(&rmidi->open_mutex);
1758 mutex_unlock(&register_mutex);
1759 return 0;
1760 }
1761
1762 /**
1763 * snd_rawmidi_set_ops - set the rawmidi operators
1764 * @rmidi: the rawmidi instance
1765 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1766 * @ops: the operator table
1767 *
1768 * Sets the rawmidi operators for the given stream direction.
1769 */
1770 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1771 const struct snd_rawmidi_ops *ops)
1772 {
1773 struct snd_rawmidi_substream *substream;
1774
1775 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1776 substream->ops = ops;
1777 }
1778 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1779
1780 /*
1781 * ENTRY functions
1782 */
1783
1784 static int __init alsa_rawmidi_init(void)
1785 {
1786
1787 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1788 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1789 #ifdef CONFIG_SND_OSSEMUL
1790 { int i;
1791 /* check device map table */
1792 for (i = 0; i < SNDRV_CARDS; i++) {
1793 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1794 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1795 i, midi_map[i]);
1796 midi_map[i] = 0;
1797 }
1798 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1799 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1800 i, amidi_map[i]);
1801 amidi_map[i] = 1;
1802 }
1803 }
1804 }
1805 #endif /* CONFIG_SND_OSSEMUL */
1806 return 0;
1807 }
1808
1809 static void __exit alsa_rawmidi_exit(void)
1810 {
1811 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1812 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1813 }
1814
1815 module_init(alsa_rawmidi_init)
1816 module_exit(alsa_rawmidi_exit)