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