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