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