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