1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
38MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
39MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
40MODULE_LICENSE("GPL");
41
42#ifdef CONFIG_SND_OSSEMUL
43static int midi_map[SNDRV_CARDS];
44static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
45module_param_array(midi_map, int, NULL, 0444);
46MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
47module_param_array(amidi_map, int, NULL, 0444);
48MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
49#endif
50
51static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
52static int snd_rawmidi_dev_free(struct snd_device *device);
53static int snd_rawmidi_dev_register(struct snd_device *device);
54static int snd_rawmidi_dev_disconnect(struct snd_device *device);
55
56static LIST_HEAD(snd_rawmidi_devices);
57static DEFINE_MUTEX(register_mutex);
58
59#define rmidi_err(rmidi, fmt, args...) \
60 dev_err(&(rmidi)->dev, fmt, ##args)
61#define rmidi_warn(rmidi, fmt, args...) \
62 dev_warn(&(rmidi)->dev, fmt, ##args)
63#define rmidi_dbg(rmidi, fmt, args...) \
64 dev_dbg(&(rmidi)->dev, fmt, ##args)
65
66static 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
76static 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
88static 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
94static 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
102static 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
110static 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
136static 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
146static 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
153static 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
162int 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}
175EXPORT_SYMBOL(snd_rawmidi_drop_output);
176
177int 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
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}
207EXPORT_SYMBOL(snd_rawmidi_drain_output);
208
209int 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}
222EXPORT_SYMBOL(snd_rawmidi_drain_input);
223
224
225
226
227static 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
259static 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
285static void close_substream(struct snd_rawmidi *rmidi,
286 struct snd_rawmidi_substream *substream,
287 int cleanup);
288
289static 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
332int 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}
360EXPORT_SYMBOL(snd_rawmidi_kernel_open);
361
362static 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
373 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
374 return -EINVAL;
375
376 err = nonseekable_open(inode, file);
377 if (err < 0)
378 return err;
379
380 if (maj == snd_major) {
381 rmidi = snd_lookup_minor_data(iminor(inode),
382 SNDRV_DEVICE_TYPE_RAWMIDI);
383#ifdef CONFIG_SND_OSSEMUL
384 } else if (maj == SOUND_MAJOR) {
385 rmidi = snd_lookup_oss_minor_data(iminor(inode),
386 SNDRV_OSS_DEVICE_TYPE_MIDI);
387#endif
388 } else
389 return -ENXIO;
390
391 if (rmidi == NULL)
392 return -ENODEV;
393
394 if (!try_module_get(rmidi->card->module)) {
395 snd_card_unref(rmidi->card);
396 return -ENXIO;
397 }
398
399 mutex_lock(&rmidi->open_mutex);
400 card = rmidi->card;
401 err = snd_card_file_add(card, file);
402 if (err < 0)
403 goto __error_card;
404 fflags = snd_rawmidi_file_flags(file);
405 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR)
406 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
407 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
408 if (rawmidi_file == NULL) {
409 err = -ENOMEM;
410 goto __error;
411 }
412 init_waitqueue_entry(&wait, current);
413 add_wait_queue(&rmidi->open_wait, &wait);
414 while (1) {
415 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
416 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
417 if (err >= 0)
418 break;
419 if (err == -EAGAIN) {
420 if (file->f_flags & O_NONBLOCK) {
421 err = -EBUSY;
422 break;
423 }
424 } else
425 break;
426 set_current_state(TASK_INTERRUPTIBLE);
427 mutex_unlock(&rmidi->open_mutex);
428 schedule();
429 mutex_lock(&rmidi->open_mutex);
430 if (rmidi->card->shutdown) {
431 err = -ENODEV;
432 break;
433 }
434 if (signal_pending(current)) {
435 err = -ERESTARTSYS;
436 break;
437 }
438 }
439 remove_wait_queue(&rmidi->open_wait, &wait);
440 if (err < 0) {
441 kfree(rawmidi_file);
442 goto __error;
443 }
444#ifdef CONFIG_SND_OSSEMUL
445 if (rawmidi_file->input && rawmidi_file->input->runtime)
446 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
447 if (rawmidi_file->output && rawmidi_file->output->runtime)
448 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
449#endif
450 file->private_data = rawmidi_file;
451 mutex_unlock(&rmidi->open_mutex);
452 snd_card_unref(rmidi->card);
453 return 0;
454
455 __error:
456 snd_card_file_remove(card, file);
457 __error_card:
458 mutex_unlock(&rmidi->open_mutex);
459 module_put(rmidi->card->module);
460 snd_card_unref(rmidi->card);
461 return err;
462}
463
464static void close_substream(struct snd_rawmidi *rmidi,
465 struct snd_rawmidi_substream *substream,
466 int cleanup)
467{
468 if (--substream->use_count)
469 return;
470
471 if (cleanup) {
472 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
473 snd_rawmidi_input_trigger(substream, 0);
474 else {
475 if (substream->active_sensing) {
476 unsigned char buf = 0xfe;
477
478
479
480 snd_rawmidi_kernel_write(substream, &buf, 1);
481 }
482 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
483 snd_rawmidi_output_trigger(substream, 0);
484 }
485 }
486 substream->ops->close(substream);
487 if (substream->runtime->private_free)
488 substream->runtime->private_free(substream);
489 snd_rawmidi_runtime_free(substream);
490 substream->opened = 0;
491 substream->append = 0;
492 put_pid(substream->pid);
493 substream->pid = NULL;
494 rmidi->streams[substream->stream].substream_opened--;
495}
496
497static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
498{
499 struct snd_rawmidi *rmidi;
500
501 rmidi = rfile->rmidi;
502 mutex_lock(&rmidi->open_mutex);
503 if (rfile->input) {
504 close_substream(rmidi, rfile->input, 1);
505 rfile->input = NULL;
506 }
507 if (rfile->output) {
508 close_substream(rmidi, rfile->output, 1);
509 rfile->output = NULL;
510 }
511 rfile->rmidi = NULL;
512 mutex_unlock(&rmidi->open_mutex);
513 wake_up(&rmidi->open_wait);
514}
515
516
517int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
518{
519 struct snd_rawmidi *rmidi;
520
521 if (snd_BUG_ON(!rfile))
522 return -ENXIO;
523
524 rmidi = rfile->rmidi;
525 rawmidi_release_priv(rfile);
526 module_put(rmidi->card->module);
527 return 0;
528}
529EXPORT_SYMBOL(snd_rawmidi_kernel_release);
530
531static int snd_rawmidi_release(struct inode *inode, struct file *file)
532{
533 struct snd_rawmidi_file *rfile;
534 struct snd_rawmidi *rmidi;
535 struct module *module;
536
537 rfile = file->private_data;
538 rmidi = rfile->rmidi;
539 rawmidi_release_priv(rfile);
540 kfree(rfile);
541 module = rmidi->card->module;
542 snd_card_file_remove(rmidi->card, file);
543 module_put(module);
544 return 0;
545}
546
547static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
548 struct snd_rawmidi_info *info)
549{
550 struct snd_rawmidi *rmidi;
551
552 if (substream == NULL)
553 return -ENODEV;
554 rmidi = substream->rmidi;
555 memset(info, 0, sizeof(*info));
556 info->card = rmidi->card->number;
557 info->device = rmidi->device;
558 info->subdevice = substream->number;
559 info->stream = substream->stream;
560 info->flags = rmidi->info_flags;
561 strcpy(info->id, rmidi->id);
562 strcpy(info->name, rmidi->name);
563 strcpy(info->subname, substream->name);
564 info->subdevices_count = substream->pstr->substream_count;
565 info->subdevices_avail = (substream->pstr->substream_count -
566 substream->pstr->substream_opened);
567 return 0;
568}
569
570static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
571 struct snd_rawmidi_info __user * _info)
572{
573 struct snd_rawmidi_info info;
574 int err;
575 if ((err = snd_rawmidi_info(substream, &info)) < 0)
576 return err;
577 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
578 return -EFAULT;
579 return 0;
580}
581
582static int __snd_rawmidi_info_select(struct snd_card *card,
583 struct snd_rawmidi_info *info)
584{
585 struct snd_rawmidi *rmidi;
586 struct snd_rawmidi_str *pstr;
587 struct snd_rawmidi_substream *substream;
588
589 rmidi = snd_rawmidi_search(card, info->device);
590 if (!rmidi)
591 return -ENXIO;
592 if (info->stream < 0 || info->stream > 1)
593 return -EINVAL;
594 pstr = &rmidi->streams[info->stream];
595 if (pstr->substream_count == 0)
596 return -ENOENT;
597 if (info->subdevice >= pstr->substream_count)
598 return -ENXIO;
599 list_for_each_entry(substream, &pstr->substreams, list) {
600 if ((unsigned int)substream->number == info->subdevice)
601 return snd_rawmidi_info(substream, info);
602 }
603 return -ENXIO;
604}
605
606int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
607{
608 int ret;
609
610 mutex_lock(®ister_mutex);
611 ret = __snd_rawmidi_info_select(card, info);
612 mutex_unlock(®ister_mutex);
613 return ret;
614}
615EXPORT_SYMBOL(snd_rawmidi_info_select);
616
617static int snd_rawmidi_info_select_user(struct snd_card *card,
618 struct snd_rawmidi_info __user *_info)
619{
620 int err;
621 struct snd_rawmidi_info info;
622 if (get_user(info.device, &_info->device))
623 return -EFAULT;
624 if (get_user(info.stream, &_info->stream))
625 return -EFAULT;
626 if (get_user(info.subdevice, &_info->subdevice))
627 return -EFAULT;
628 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
629 return err;
630 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
631 return -EFAULT;
632 return 0;
633}
634
635int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
636 struct snd_rawmidi_params * params)
637{
638 char *newbuf, *oldbuf;
639 struct snd_rawmidi_runtime *runtime = substream->runtime;
640
641 if (substream->append && substream->use_count > 1)
642 return -EBUSY;
643 snd_rawmidi_drain_output(substream);
644 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
645 return -EINVAL;
646 }
647 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
648 return -EINVAL;
649 }
650 if (params->buffer_size != runtime->buffer_size) {
651 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
652 if (!newbuf)
653 return -ENOMEM;
654 spin_lock_irq(&runtime->lock);
655 oldbuf = runtime->buffer;
656 runtime->buffer = newbuf;
657 runtime->buffer_size = params->buffer_size;
658 runtime->avail = runtime->buffer_size;
659 runtime->appl_ptr = runtime->hw_ptr = 0;
660 spin_unlock_irq(&runtime->lock);
661 kfree(oldbuf);
662 }
663 runtime->avail_min = params->avail_min;
664 substream->active_sensing = !params->no_active_sensing;
665 return 0;
666}
667EXPORT_SYMBOL(snd_rawmidi_output_params);
668
669int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
670 struct snd_rawmidi_params * params)
671{
672 char *newbuf, *oldbuf;
673 struct snd_rawmidi_runtime *runtime = substream->runtime;
674
675 snd_rawmidi_drain_input(substream);
676 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
677 return -EINVAL;
678 }
679 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
680 return -EINVAL;
681 }
682 if (params->buffer_size != runtime->buffer_size) {
683 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
684 if (!newbuf)
685 return -ENOMEM;
686 spin_lock_irq(&runtime->lock);
687 oldbuf = runtime->buffer;
688 runtime->buffer = newbuf;
689 runtime->buffer_size = params->buffer_size;
690 runtime->appl_ptr = runtime->hw_ptr = 0;
691 spin_unlock_irq(&runtime->lock);
692 kfree(oldbuf);
693 }
694 runtime->avail_min = params->avail_min;
695 return 0;
696}
697EXPORT_SYMBOL(snd_rawmidi_input_params);
698
699static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
700 struct snd_rawmidi_status * status)
701{
702 struct snd_rawmidi_runtime *runtime = substream->runtime;
703
704 memset(status, 0, sizeof(*status));
705 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
706 spin_lock_irq(&runtime->lock);
707 status->avail = runtime->avail;
708 spin_unlock_irq(&runtime->lock);
709 return 0;
710}
711
712static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
713 struct snd_rawmidi_status * status)
714{
715 struct snd_rawmidi_runtime *runtime = substream->runtime;
716
717 memset(status, 0, sizeof(*status));
718 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
719 spin_lock_irq(&runtime->lock);
720 status->avail = runtime->avail;
721 status->xruns = runtime->xruns;
722 runtime->xruns = 0;
723 spin_unlock_irq(&runtime->lock);
724 return 0;
725}
726
727static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
728{
729 struct snd_rawmidi_file *rfile;
730 void __user *argp = (void __user *)arg;
731
732 rfile = file->private_data;
733 if (((cmd >> 8) & 0xff) != 'W')
734 return -ENOTTY;
735 switch (cmd) {
736 case SNDRV_RAWMIDI_IOCTL_PVERSION:
737 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
738 case SNDRV_RAWMIDI_IOCTL_INFO:
739 {
740 int stream;
741 struct snd_rawmidi_info __user *info = argp;
742 if (get_user(stream, &info->stream))
743 return -EFAULT;
744 switch (stream) {
745 case SNDRV_RAWMIDI_STREAM_INPUT:
746 return snd_rawmidi_info_user(rfile->input, info);
747 case SNDRV_RAWMIDI_STREAM_OUTPUT:
748 return snd_rawmidi_info_user(rfile->output, info);
749 default:
750 return -EINVAL;
751 }
752 }
753 case SNDRV_RAWMIDI_IOCTL_PARAMS:
754 {
755 struct snd_rawmidi_params params;
756 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
757 return -EFAULT;
758 switch (params.stream) {
759 case SNDRV_RAWMIDI_STREAM_OUTPUT:
760 if (rfile->output == NULL)
761 return -EINVAL;
762 return snd_rawmidi_output_params(rfile->output, ¶ms);
763 case SNDRV_RAWMIDI_STREAM_INPUT:
764 if (rfile->input == NULL)
765 return -EINVAL;
766 return snd_rawmidi_input_params(rfile->input, ¶ms);
767 default:
768 return -EINVAL;
769 }
770 }
771 case SNDRV_RAWMIDI_IOCTL_STATUS:
772 {
773 int err = 0;
774 struct snd_rawmidi_status status;
775 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
776 return -EFAULT;
777 switch (status.stream) {
778 case SNDRV_RAWMIDI_STREAM_OUTPUT:
779 if (rfile->output == NULL)
780 return -EINVAL;
781 err = snd_rawmidi_output_status(rfile->output, &status);
782 break;
783 case SNDRV_RAWMIDI_STREAM_INPUT:
784 if (rfile->input == NULL)
785 return -EINVAL;
786 err = snd_rawmidi_input_status(rfile->input, &status);
787 break;
788 default:
789 return -EINVAL;
790 }
791 if (err < 0)
792 return err;
793 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
794 return -EFAULT;
795 return 0;
796 }
797 case SNDRV_RAWMIDI_IOCTL_DROP:
798 {
799 int val;
800 if (get_user(val, (int __user *) argp))
801 return -EFAULT;
802 switch (val) {
803 case SNDRV_RAWMIDI_STREAM_OUTPUT:
804 if (rfile->output == NULL)
805 return -EINVAL;
806 return snd_rawmidi_drop_output(rfile->output);
807 default:
808 return -EINVAL;
809 }
810 }
811 case SNDRV_RAWMIDI_IOCTL_DRAIN:
812 {
813 int val;
814 if (get_user(val, (int __user *) argp))
815 return -EFAULT;
816 switch (val) {
817 case SNDRV_RAWMIDI_STREAM_OUTPUT:
818 if (rfile->output == NULL)
819 return -EINVAL;
820 return snd_rawmidi_drain_output(rfile->output);
821 case SNDRV_RAWMIDI_STREAM_INPUT:
822 if (rfile->input == NULL)
823 return -EINVAL;
824 return snd_rawmidi_drain_input(rfile->input);
825 default:
826 return -EINVAL;
827 }
828 }
829 default:
830 rmidi_dbg(rfile->rmidi,
831 "rawmidi: unknown command = 0x%x\n", cmd);
832 }
833 return -ENOTTY;
834}
835
836static int snd_rawmidi_control_ioctl(struct snd_card *card,
837 struct snd_ctl_file *control,
838 unsigned int cmd,
839 unsigned long arg)
840{
841 void __user *argp = (void __user *)arg;
842
843 switch (cmd) {
844 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
845 {
846 int device;
847
848 if (get_user(device, (int __user *)argp))
849 return -EFAULT;
850 if (device >= SNDRV_RAWMIDI_DEVICES)
851 device = SNDRV_RAWMIDI_DEVICES - 1;
852 mutex_lock(®ister_mutex);
853 device = device < 0 ? 0 : device + 1;
854 while (device < SNDRV_RAWMIDI_DEVICES) {
855 if (snd_rawmidi_search(card, device))
856 break;
857 device++;
858 }
859 if (device == SNDRV_RAWMIDI_DEVICES)
860 device = -1;
861 mutex_unlock(®ister_mutex);
862 if (put_user(device, (int __user *)argp))
863 return -EFAULT;
864 return 0;
865 }
866 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
867 {
868 int val;
869
870 if (get_user(val, (int __user *)argp))
871 return -EFAULT;
872 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
873 return 0;
874 }
875 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
876 return snd_rawmidi_info_select_user(card, argp);
877 }
878 return -ENOIOCTLCMD;
879}
880
881
882
883
884
885
886
887
888
889
890
891int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
892 const unsigned char *buffer, int count)
893{
894 unsigned long flags;
895 int result = 0, count1;
896 struct snd_rawmidi_runtime *runtime = substream->runtime;
897
898 if (!substream->opened)
899 return -EBADFD;
900 if (runtime->buffer == NULL) {
901 rmidi_dbg(substream->rmidi,
902 "snd_rawmidi_receive: input is not active!!!\n");
903 return -EINVAL;
904 }
905 spin_lock_irqsave(&runtime->lock, flags);
906 if (count == 1) {
907 substream->bytes++;
908 if (runtime->avail < runtime->buffer_size) {
909 runtime->buffer[runtime->hw_ptr++] = buffer[0];
910 runtime->hw_ptr %= runtime->buffer_size;
911 runtime->avail++;
912 result++;
913 } else {
914 runtime->xruns++;
915 }
916 } else {
917 substream->bytes += count;
918 count1 = runtime->buffer_size - runtime->hw_ptr;
919 if (count1 > count)
920 count1 = count;
921 if (count1 > (int)(runtime->buffer_size - runtime->avail))
922 count1 = runtime->buffer_size - runtime->avail;
923 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
924 runtime->hw_ptr += count1;
925 runtime->hw_ptr %= runtime->buffer_size;
926 runtime->avail += count1;
927 count -= count1;
928 result += count1;
929 if (count > 0) {
930 buffer += count1;
931 count1 = count;
932 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
933 count1 = runtime->buffer_size - runtime->avail;
934 runtime->xruns += count - count1;
935 }
936 if (count1 > 0) {
937 memcpy(runtime->buffer, buffer, count1);
938 runtime->hw_ptr = count1;
939 runtime->avail += count1;
940 result += count1;
941 }
942 }
943 }
944 if (result > 0) {
945 if (runtime->event)
946 schedule_work(&runtime->event_work);
947 else if (snd_rawmidi_ready(substream))
948 wake_up(&runtime->sleep);
949 }
950 spin_unlock_irqrestore(&runtime->lock, flags);
951 return result;
952}
953EXPORT_SYMBOL(snd_rawmidi_receive);
954
955static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
956 unsigned char __user *userbuf,
957 unsigned char *kernelbuf, long count)
958{
959 unsigned long flags;
960 long result = 0, count1;
961 struct snd_rawmidi_runtime *runtime = substream->runtime;
962 unsigned long appl_ptr;
963
964 spin_lock_irqsave(&runtime->lock, flags);
965 while (count > 0 && runtime->avail) {
966 count1 = runtime->buffer_size - runtime->appl_ptr;
967 if (count1 > count)
968 count1 = count;
969 if (count1 > (int)runtime->avail)
970 count1 = runtime->avail;
971
972
973 appl_ptr = runtime->appl_ptr;
974 runtime->appl_ptr += count1;
975 runtime->appl_ptr %= runtime->buffer_size;
976 runtime->avail -= count1;
977
978 if (kernelbuf)
979 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
980 if (userbuf) {
981 spin_unlock_irqrestore(&runtime->lock, flags);
982 if (copy_to_user(userbuf + result,
983 runtime->buffer + appl_ptr, count1)) {
984 return result > 0 ? result : -EFAULT;
985 }
986 spin_lock_irqsave(&runtime->lock, flags);
987 }
988 result += count1;
989 count -= count1;
990 }
991 spin_unlock_irqrestore(&runtime->lock, flags);
992 return result;
993}
994
995long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
996 unsigned char *buf, long count)
997{
998 snd_rawmidi_input_trigger(substream, 1);
999 return snd_rawmidi_kernel_read1(substream, NULL, buf, count);
1000}
1001EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1002
1003static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1004 loff_t *offset)
1005{
1006 long result;
1007 int count1;
1008 struct snd_rawmidi_file *rfile;
1009 struct snd_rawmidi_substream *substream;
1010 struct snd_rawmidi_runtime *runtime;
1011
1012 rfile = file->private_data;
1013 substream = rfile->input;
1014 if (substream == NULL)
1015 return -EIO;
1016 runtime = substream->runtime;
1017 snd_rawmidi_input_trigger(substream, 1);
1018 result = 0;
1019 while (count > 0) {
1020 spin_lock_irq(&runtime->lock);
1021 while (!snd_rawmidi_ready(substream)) {
1022 wait_queue_t wait;
1023 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1024 spin_unlock_irq(&runtime->lock);
1025 return result > 0 ? result : -EAGAIN;
1026 }
1027 init_waitqueue_entry(&wait, current);
1028 add_wait_queue(&runtime->sleep, &wait);
1029 set_current_state(TASK_INTERRUPTIBLE);
1030 spin_unlock_irq(&runtime->lock);
1031 schedule();
1032 remove_wait_queue(&runtime->sleep, &wait);
1033 if (rfile->rmidi->card->shutdown)
1034 return -ENODEV;
1035 if (signal_pending(current))
1036 return result > 0 ? result : -ERESTARTSYS;
1037 if (!runtime->avail)
1038 return result > 0 ? result : -EIO;
1039 spin_lock_irq(&runtime->lock);
1040 }
1041 spin_unlock_irq(&runtime->lock);
1042 count1 = snd_rawmidi_kernel_read1(substream,
1043 (unsigned char __user *)buf,
1044 NULL,
1045 count);
1046 if (count1 < 0)
1047 return result > 0 ? result : count1;
1048 result += count1;
1049 buf += count1;
1050 count -= count1;
1051 }
1052 return result;
1053}
1054
1055
1056
1057
1058
1059
1060
1061int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1062{
1063 struct snd_rawmidi_runtime *runtime = substream->runtime;
1064 int result;
1065 unsigned long flags;
1066
1067 if (runtime->buffer == NULL) {
1068 rmidi_dbg(substream->rmidi,
1069 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1070 return 1;
1071 }
1072 spin_lock_irqsave(&runtime->lock, flags);
1073 result = runtime->avail >= runtime->buffer_size;
1074 spin_unlock_irqrestore(&runtime->lock, flags);
1075 return result;
1076}
1077EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1088 unsigned char *buffer, int count)
1089{
1090 int result, count1;
1091 struct snd_rawmidi_runtime *runtime = substream->runtime;
1092
1093 if (runtime->buffer == NULL) {
1094 rmidi_dbg(substream->rmidi,
1095 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1096 return -EINVAL;
1097 }
1098 result = 0;
1099 if (runtime->avail >= runtime->buffer_size) {
1100
1101 goto __skip;
1102 }
1103 if (count == 1) {
1104 *buffer = runtime->buffer[runtime->hw_ptr];
1105 result++;
1106 } else {
1107 count1 = runtime->buffer_size - runtime->hw_ptr;
1108 if (count1 > count)
1109 count1 = count;
1110 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1111 count1 = runtime->buffer_size - runtime->avail;
1112 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1113 count -= count1;
1114 result += count1;
1115 if (count > 0) {
1116 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1117 count = runtime->buffer_size - runtime->avail - count1;
1118 memcpy(buffer + count1, runtime->buffer, count);
1119 result += count;
1120 }
1121 }
1122 __skip:
1123 return result;
1124}
1125EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1142 unsigned char *buffer, int count)
1143{
1144 struct snd_rawmidi_runtime *runtime = substream->runtime;
1145 int result;
1146 unsigned long flags;
1147
1148 spin_lock_irqsave(&runtime->lock, flags);
1149 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1150 spin_unlock_irqrestore(&runtime->lock, flags);
1151 return result;
1152}
1153EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1154
1155
1156
1157
1158
1159
1160
1161
1162int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1163{
1164 struct snd_rawmidi_runtime *runtime = substream->runtime;
1165
1166 if (runtime->buffer == NULL) {
1167 rmidi_dbg(substream->rmidi,
1168 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1169 return -EINVAL;
1170 }
1171 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1172 runtime->hw_ptr += count;
1173 runtime->hw_ptr %= runtime->buffer_size;
1174 runtime->avail += count;
1175 substream->bytes += count;
1176 if (count > 0) {
1177 if (runtime->drain || snd_rawmidi_ready(substream))
1178 wake_up(&runtime->sleep);
1179 }
1180 return count;
1181}
1182EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1196{
1197 struct snd_rawmidi_runtime *runtime = substream->runtime;
1198 int result;
1199 unsigned long flags;
1200
1201 spin_lock_irqsave(&runtime->lock, flags);
1202 result = __snd_rawmidi_transmit_ack(substream, count);
1203 spin_unlock_irqrestore(&runtime->lock, flags);
1204 return result;
1205}
1206EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1219 unsigned char *buffer, int count)
1220{
1221 struct snd_rawmidi_runtime *runtime = substream->runtime;
1222 int result;
1223 unsigned long flags;
1224
1225 spin_lock_irqsave(&runtime->lock, flags);
1226 if (!substream->opened)
1227 result = -EBADFD;
1228 else {
1229 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1230 if (count <= 0)
1231 result = count;
1232 else
1233 result = __snd_rawmidi_transmit_ack(substream, count);
1234 }
1235 spin_unlock_irqrestore(&runtime->lock, flags);
1236 return result;
1237}
1238EXPORT_SYMBOL(snd_rawmidi_transmit);
1239
1240static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1241 const unsigned char __user *userbuf,
1242 const unsigned char *kernelbuf,
1243 long count)
1244{
1245 unsigned long flags;
1246 long count1, result;
1247 struct snd_rawmidi_runtime *runtime = substream->runtime;
1248 unsigned long appl_ptr;
1249
1250 if (!kernelbuf && !userbuf)
1251 return -EINVAL;
1252 if (snd_BUG_ON(!runtime->buffer))
1253 return -EINVAL;
1254
1255 result = 0;
1256 spin_lock_irqsave(&runtime->lock, flags);
1257 if (substream->append) {
1258 if ((long)runtime->avail < count) {
1259 spin_unlock_irqrestore(&runtime->lock, flags);
1260 return -EAGAIN;
1261 }
1262 }
1263 while (count > 0 && runtime->avail > 0) {
1264 count1 = runtime->buffer_size - runtime->appl_ptr;
1265 if (count1 > count)
1266 count1 = count;
1267 if (count1 > (long)runtime->avail)
1268 count1 = runtime->avail;
1269
1270
1271 appl_ptr = runtime->appl_ptr;
1272 runtime->appl_ptr += count1;
1273 runtime->appl_ptr %= runtime->buffer_size;
1274 runtime->avail -= count1;
1275
1276 if (kernelbuf)
1277 memcpy(runtime->buffer + appl_ptr,
1278 kernelbuf + result, count1);
1279 else if (userbuf) {
1280 spin_unlock_irqrestore(&runtime->lock, flags);
1281 if (copy_from_user(runtime->buffer + appl_ptr,
1282 userbuf + result, count1)) {
1283 spin_lock_irqsave(&runtime->lock, flags);
1284 result = result > 0 ? result : -EFAULT;
1285 goto __end;
1286 }
1287 spin_lock_irqsave(&runtime->lock, flags);
1288 }
1289 result += count1;
1290 count -= count1;
1291 }
1292 __end:
1293 count1 = runtime->avail < runtime->buffer_size;
1294 spin_unlock_irqrestore(&runtime->lock, flags);
1295 if (count1)
1296 snd_rawmidi_output_trigger(substream, 1);
1297 return result;
1298}
1299
1300long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1301 const unsigned char *buf, long count)
1302{
1303 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1304}
1305EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1306
1307static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1308 size_t count, loff_t *offset)
1309{
1310 long result, timeout;
1311 int count1;
1312 struct snd_rawmidi_file *rfile;
1313 struct snd_rawmidi_runtime *runtime;
1314 struct snd_rawmidi_substream *substream;
1315
1316 rfile = file->private_data;
1317 substream = rfile->output;
1318 runtime = substream->runtime;
1319
1320 if (substream->append && count > runtime->buffer_size)
1321 return -EIO;
1322 result = 0;
1323 while (count > 0) {
1324 spin_lock_irq(&runtime->lock);
1325 while (!snd_rawmidi_ready_append(substream, count)) {
1326 wait_queue_t wait;
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_t wait;
1359 unsigned int last_avail = runtime->avail;
1360 init_waitqueue_entry(&wait, current);
1361 add_wait_queue(&runtime->sleep, &wait);
1362 set_current_state(TASK_INTERRUPTIBLE);
1363 spin_unlock_irq(&runtime->lock);
1364 timeout = schedule_timeout(30 * HZ);
1365 remove_wait_queue(&runtime->sleep, &wait);
1366 if (signal_pending(current))
1367 return result > 0 ? result : -ERESTARTSYS;
1368 if (runtime->avail == last_avail && !timeout)
1369 return result > 0 ? result : -EIO;
1370 spin_lock_irq(&runtime->lock);
1371 }
1372 spin_unlock_irq(&runtime->lock);
1373 }
1374 return result;
1375}
1376
1377static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1378{
1379 struct snd_rawmidi_file *rfile;
1380 struct snd_rawmidi_runtime *runtime;
1381 unsigned int mask;
1382
1383 rfile = file->private_data;
1384 if (rfile->input != NULL) {
1385 runtime = rfile->input->runtime;
1386 snd_rawmidi_input_trigger(rfile->input, 1);
1387 poll_wait(file, &runtime->sleep, wait);
1388 }
1389 if (rfile->output != NULL) {
1390 runtime = rfile->output->runtime;
1391 poll_wait(file, &runtime->sleep, wait);
1392 }
1393 mask = 0;
1394 if (rfile->input != NULL) {
1395 if (snd_rawmidi_ready(rfile->input))
1396 mask |= POLLIN | POLLRDNORM;
1397 }
1398 if (rfile->output != NULL) {
1399 if (snd_rawmidi_ready(rfile->output))
1400 mask |= POLLOUT | POLLWRNORM;
1401 }
1402 return mask;
1403}
1404
1405
1406
1407#ifdef CONFIG_COMPAT
1408#include "rawmidi_compat.c"
1409#else
1410#define snd_rawmidi_ioctl_compat NULL
1411#endif
1412
1413
1414
1415
1416
1417static 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
1480
1481
1482static const struct file_operations snd_rawmidi_f_ops =
1483{
1484 .owner = THIS_MODULE,
1485 .read = snd_rawmidi_read,
1486 .write = snd_rawmidi_write,
1487 .open = snd_rawmidi_open,
1488 .release = snd_rawmidi_release,
1489 .llseek = no_llseek,
1490 .poll = snd_rawmidi_poll,
1491 .unlocked_ioctl = snd_rawmidi_ioctl,
1492 .compat_ioctl = snd_rawmidi_ioctl_compat,
1493};
1494
1495static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1496 struct snd_rawmidi_str *stream,
1497 int direction,
1498 int count)
1499{
1500 struct snd_rawmidi_substream *substream;
1501 int idx;
1502
1503 for (idx = 0; idx < count; idx++) {
1504 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1505 if (!substream)
1506 return -ENOMEM;
1507 substream->stream = direction;
1508 substream->number = idx;
1509 substream->rmidi = rmidi;
1510 substream->pstr = stream;
1511 list_add_tail(&substream->list, &stream->substreams);
1512 stream->substream_count++;
1513 }
1514 return 0;
1515}
1516
1517static void release_rawmidi_device(struct device *dev)
1518{
1519 kfree(container_of(dev, struct snd_rawmidi, dev));
1520}
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1537 int output_count, int input_count,
1538 struct snd_rawmidi ** rrawmidi)
1539{
1540 struct snd_rawmidi *rmidi;
1541 int err;
1542 static struct snd_device_ops ops = {
1543 .dev_free = snd_rawmidi_dev_free,
1544 .dev_register = snd_rawmidi_dev_register,
1545 .dev_disconnect = snd_rawmidi_dev_disconnect,
1546 };
1547
1548 if (snd_BUG_ON(!card))
1549 return -ENXIO;
1550 if (rrawmidi)
1551 *rrawmidi = NULL;
1552 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1553 if (!rmidi)
1554 return -ENOMEM;
1555 rmidi->card = card;
1556 rmidi->device = device;
1557 mutex_init(&rmidi->open_mutex);
1558 init_waitqueue_head(&rmidi->open_wait);
1559 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1560 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1561
1562 if (id != NULL)
1563 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1564
1565 snd_device_initialize(&rmidi->dev, card);
1566 rmidi->dev.release = release_rawmidi_device;
1567 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1568
1569 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1570 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1571 SNDRV_RAWMIDI_STREAM_INPUT,
1572 input_count)) < 0) {
1573 snd_rawmidi_free(rmidi);
1574 return err;
1575 }
1576 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1577 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1578 SNDRV_RAWMIDI_STREAM_OUTPUT,
1579 output_count)) < 0) {
1580 snd_rawmidi_free(rmidi);
1581 return err;
1582 }
1583 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1584 snd_rawmidi_free(rmidi);
1585 return err;
1586 }
1587 if (rrawmidi)
1588 *rrawmidi = rmidi;
1589 return 0;
1590}
1591EXPORT_SYMBOL(snd_rawmidi_new);
1592
1593static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1594{
1595 struct snd_rawmidi_substream *substream;
1596
1597 while (!list_empty(&stream->substreams)) {
1598 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1599 list_del(&substream->list);
1600 kfree(substream);
1601 }
1602}
1603
1604static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1605{
1606 if (!rmidi)
1607 return 0;
1608
1609 snd_info_free_entry(rmidi->proc_entry);
1610 rmidi->proc_entry = NULL;
1611 mutex_lock(®ister_mutex);
1612 if (rmidi->ops && rmidi->ops->dev_unregister)
1613 rmidi->ops->dev_unregister(rmidi);
1614 mutex_unlock(®ister_mutex);
1615
1616 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1617 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1618 if (rmidi->private_free)
1619 rmidi->private_free(rmidi);
1620 put_device(&rmidi->dev);
1621 return 0;
1622}
1623
1624static int snd_rawmidi_dev_free(struct snd_device *device)
1625{
1626 struct snd_rawmidi *rmidi = device->device_data;
1627 return snd_rawmidi_free(rmidi);
1628}
1629
1630#if IS_ENABLED(CONFIG_SND_SEQUENCER)
1631static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1632{
1633 struct snd_rawmidi *rmidi = device->private_data;
1634 rmidi->seq_dev = NULL;
1635}
1636#endif
1637
1638static int snd_rawmidi_dev_register(struct snd_device *device)
1639{
1640 int err;
1641 struct snd_info_entry *entry;
1642 char name[16];
1643 struct snd_rawmidi *rmidi = device->device_data;
1644
1645 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1646 return -ENOMEM;
1647 mutex_lock(®ister_mutex);
1648 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1649 mutex_unlock(®ister_mutex);
1650 return -EBUSY;
1651 }
1652 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1653 mutex_unlock(®ister_mutex);
1654 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1655 rmidi->card, rmidi->device,
1656 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
1657 if (err < 0) {
1658 rmidi_err(rmidi, "unable to register\n");
1659 mutex_lock(®ister_mutex);
1660 list_del(&rmidi->list);
1661 mutex_unlock(®ister_mutex);
1662 return err;
1663 }
1664 if (rmidi->ops && rmidi->ops->dev_register &&
1665 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1666 snd_unregister_device(&rmidi->dev);
1667 mutex_lock(®ister_mutex);
1668 list_del(&rmidi->list);
1669 mutex_unlock(®ister_mutex);
1670 return err;
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
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) {
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
1724static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1725{
1726 struct snd_rawmidi *rmidi = device->device_data;
1727 int dir;
1728
1729 mutex_lock(®ister_mutex);
1730 mutex_lock(&rmidi->open_mutex);
1731 wake_up(&rmidi->open_wait);
1732 list_del_init(&rmidi->list);
1733 for (dir = 0; dir < 2; dir++) {
1734 struct snd_rawmidi_substream *s;
1735 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1736 if (s->runtime)
1737 wake_up(&s->runtime->sleep);
1738 }
1739 }
1740
1741#ifdef CONFIG_SND_OSSEMUL
1742 if (rmidi->ossreg) {
1743 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1744 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1745#ifdef SNDRV_OSS_INFO_DEV_MIDI
1746 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1747#endif
1748 }
1749 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1750 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1751 rmidi->ossreg = 0;
1752 }
1753#endif
1754 snd_unregister_device(&rmidi->dev);
1755 mutex_unlock(&rmidi->open_mutex);
1756 mutex_unlock(®ister_mutex);
1757 return 0;
1758}
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1769 const struct snd_rawmidi_ops *ops)
1770{
1771 struct snd_rawmidi_substream *substream;
1772
1773 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1774 substream->ops = ops;
1775}
1776EXPORT_SYMBOL(snd_rawmidi_set_ops);
1777
1778
1779
1780
1781
1782static int __init alsa_rawmidi_init(void)
1783{
1784
1785 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1786 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1787#ifdef CONFIG_SND_OSSEMUL
1788 { int i;
1789
1790 for (i = 0; i < SNDRV_CARDS; i++) {
1791 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1792 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1793 i, midi_map[i]);
1794 midi_map[i] = 0;
1795 }
1796 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1797 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1798 i, amidi_map[i]);
1799 amidi_map[i] = 1;
1800 }
1801 }
1802 }
1803#endif
1804 return 0;
1805}
1806
1807static void __exit alsa_rawmidi_exit(void)
1808{
1809 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1810 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1811}
1812
1813module_init(alsa_rawmidi_init)
1814module_exit(alsa_rawmidi_exit)
1815