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