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