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