blob: befa9809ff001ec514a0512d560ce079041d86cc [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Abstract layer for MIDI v1.0 stream
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <sound/core.h>
8#include <linux/major.h>
9#include <linux/init.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010010#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/slab.h>
12#include <linux/time.h>
13#include <linux/wait.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010014#include <linux/mutex.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040015#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/delay.h>
Takashi Iwaief4db232018-07-17 23:12:33 +020017#include <linux/mm.h>
Gustavo A. R. Silva2b1d9c82019-03-20 16:15:24 -050018#include <linux/nospec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020025MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070026MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
27MODULE_LICENSE("GPL");
28
29#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai6581f4e2006-05-17 17:14:51 +020030static int midi_map[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
32module_param_array(midi_map, int, NULL, 0444);
33MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
34module_param_array(amidi_map, int, NULL, 0444);
35MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
36#endif /* CONFIG_SND_OSSEMUL */
37
Pierre-Louis Bossart93fcef82020-09-02 16:21:30 -050038static int snd_rawmidi_free(struct snd_rawmidi *rmidi);
Takashi Iwai48c9d412005-11-17 13:56:51 +010039static int snd_rawmidi_dev_free(struct snd_device *device);
40static int snd_rawmidi_dev_register(struct snd_device *device);
41static int snd_rawmidi_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Clemens Ladischf87135f2005-11-20 14:06:59 +010043static LIST_HEAD(snd_rawmidi_devices);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010044static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Takashi Iwaica20d292014-02-04 18:21:39 +010046#define rmidi_err(rmidi, fmt, args...) \
Takashi Iwai07cc3e82015-01-30 15:31:52 +010047 dev_err(&(rmidi)->dev, fmt, ##args)
Takashi Iwaica20d292014-02-04 18:21:39 +010048#define rmidi_warn(rmidi, fmt, args...) \
Takashi Iwai07cc3e82015-01-30 15:31:52 +010049 dev_warn(&(rmidi)->dev, fmt, ##args)
Takashi Iwaica20d292014-02-04 18:21:39 +010050#define rmidi_dbg(rmidi, fmt, args...) \
Takashi Iwai07cc3e82015-01-30 15:31:52 +010051 dev_dbg(&(rmidi)->dev, fmt, ##args)
Takashi Iwaica20d292014-02-04 18:21:39 +010052
Baolin Wangd9e55822018-04-24 20:06:12 +080053struct 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
64struct 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
Clemens Ladischf87135f2005-11-20 14:06:59 +010076static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
77{
Clemens Ladischf87135f2005-11-20 14:06:59 +010078 struct snd_rawmidi *rawmidi;
79
Johannes Berg9244b2c2006-10-05 16:02:22 +020080 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
Clemens Ladischf87135f2005-11-20 14:06:59 +010081 if (rawmidi->card == card && rawmidi->device == device)
82 return rawmidi;
Clemens Ladischf87135f2005-11-20 14:06:59 +010083 return NULL;
84}
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static 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
Takashi Iwai88a06d62020-12-06 09:35:27 +010098static inline bool __snd_rawmidi_ready(struct snd_rawmidi_runtime *runtime)
99{
100 return runtime->avail >= runtime->avail_min;
101}
102
103static bool snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100105 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai88a06d62020-12-06 09:35:27 +0100106 unsigned long flags;
107 bool ready;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200108
Takashi Iwai88a06d62020-12-06 09:35:27 +0100109 spin_lock_irqsave(&runtime->lock, flags);
110 ready = __snd_rawmidi_ready(runtime);
111 spin_unlock_irqrestore(&runtime->lock, flags);
112 return ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Takashi Iwai48c9d412005-11-17 13:56:51 +0100115static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
116 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100118 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return runtime->avail >= runtime->avail_min &&
121 (!substream->append || runtime->avail >= count);
122}
123
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200124static void snd_rawmidi_input_event_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200126 struct snd_rawmidi_runtime *runtime =
127 container_of(work, struct snd_rawmidi_runtime, event_work);
Takashi Iwai5bed9132018-07-17 22:32:52 +0200128
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200129 if (runtime->event)
130 runtime->event(runtime->substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Takashi Iwaic1f6e3c2020-05-07 13:44:56 +0200133/* buffer refcount management: call with runtime->lock held */
134static inline void snd_rawmidi_buffer_ref(struct snd_rawmidi_runtime *runtime)
135{
136 runtime->buffer_ref++;
137}
138
139static inline void snd_rawmidi_buffer_unref(struct snd_rawmidi_runtime *runtime)
140{
141 runtime->buffer_ref--;
142}
143
Takashi Iwai48c9d412005-11-17 13:56:51 +0100144static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100146 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Takashi Iwai5bed9132018-07-17 22:32:52 +0200148 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
149 if (!runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return -ENOMEM;
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200151 runtime->substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 spin_lock_init(&runtime->lock);
153 init_waitqueue_head(&runtime->sleep);
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200154 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 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;
Takashi Iwai5a7b44a2018-09-03 15:16:43 +0200162 runtime->buffer = kvzalloc(runtime->buffer_size, GFP_KERNEL);
Takashi Iwai5bed9132018-07-17 22:32:52 +0200163 if (!runtime->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 kfree(runtime);
165 return -ENOMEM;
166 }
167 runtime->appl_ptr = runtime->hw_ptr = 0;
168 substream->runtime = runtime;
169 return 0;
170}
171
Takashi Iwai48c9d412005-11-17 13:56:51 +0100172static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100174 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Takashi Iwaief4db232018-07-17 23:12:33 +0200176 kvfree(runtime->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 kfree(runtime);
178 substream->runtime = NULL;
179 return 0;
180}
181
Takashi Iwai5bed9132018-07-17 22:32:52 +0200182static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Takashi Iwai219df322008-11-03 08:17:05 +0100184 if (!substream->opened)
185 return;
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200186 substream->ops->trigger(substream, up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Takashi Iwai48c9d412005-11-17 13:56:51 +0100189static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Takashi Iwai219df322008-11-03 08:17:05 +0100191 if (!substream->opened)
192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 substream->ops->trigger(substream, up);
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200194 if (!up)
195 cancel_work_sync(&substream->runtime->event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Takashi Iwaif5beb592018-07-17 23:07:29 +0200198static 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
206static void reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
207 bool is_input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwaif5beb592018-07-17 23:07:29 +0200212 __reset_runtime_ptrs(runtime, is_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwaif5beb592018-07-17 23:07:29 +0200214}
215
216int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
217{
218 snd_rawmidi_output_trigger(substream, 0);
219 reset_runtime_ptrs(substream->runtime, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 0;
221}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100222EXPORT_SYMBOL(snd_rawmidi_drop_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Takashi Iwai48c9d412005-11-17 13:56:51 +0100224int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 int err;
227 long timeout;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100228 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
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) {
Takashi Iwaica20d292014-02-04 18:21:39 +0100238 rmidi_warn(substream->rmidi,
239 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
240 (long)runtime->avail, (long)runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 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}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100254EXPORT_SYMBOL(snd_rawmidi_drain_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Takashi Iwai48c9d412005-11-17 13:56:51 +0100256int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 snd_rawmidi_input_trigger(substream, 0);
Takashi Iwaif5beb592018-07-17 23:07:29 +0200259 reset_runtime_ptrs(substream->runtime, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
261}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100262EXPORT_SYMBOL(snd_rawmidi_drain_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100264/* look for an available substream for the given stream direction;
265 * if a specific subdevice is given, try to assign it
266 */
267static 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];
Takashi Iwaiedb87ed2020-01-05 15:47:56 +0100273 static const unsigned int info_flags[2] = {
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100274 [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;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100282
283 list_for_each_entry(substream, &s->substreams, list) {
284 if (substream->opened) {
285 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
Clemens Ladisch16fb1092009-10-21 09:10:16 +0200286 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
287 !substream->append)
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100288 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 */
299static int open_substream(struct snd_rawmidi *rmidi,
300 struct snd_rawmidi_substream *substream,
301 int mode)
302{
303 int err;
304
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200305 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);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200310 if (err < 0) {
311 snd_rawmidi_runtime_free(substream);
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200312 return err;
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200313 }
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200314 substream->opened = 1;
Clemens Ladisch2d4b8422009-07-13 13:52:46 +0200315 substream->active_sensing = 0;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200316 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
317 substream->append = 1;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100318 substream->pid = get_pid(task_pid(current));
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200319 rmidi->streams[substream->stream].substream_opened++;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200320 }
321 substream->use_count++;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100322 return 0;
323}
324
325static void close_substream(struct snd_rawmidi *rmidi,
326 struct snd_rawmidi_substream *substream,
327 int cleanup);
328
329static 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)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200341 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100342 }
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)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200348 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100349 }
350
351 if (sinput) {
352 err = open_substream(rmidi, sinput, mode);
353 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200354 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100355 }
356 if (soutput) {
357 err = open_substream(rmidi, soutput, mode);
358 if (err < 0) {
359 if (sinput)
360 close_substream(rmidi, sinput, 0);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200361 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100362 }
363 }
364
365 rfile->rmidi = rmidi;
366 rfile->input = sinput;
367 rfile->output = soutput;
368 return 0;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100369}
370
371/* called from sound/core/seq/seq_midi.c */
Clemens Ladischf87135f2005-11-20 14:06:59 +0100372int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200373 int mode, struct snd_rawmidi_file *rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100375 struct snd_rawmidi *rmidi;
Takashi Iwai7fdc9b02018-07-17 22:45:50 +0200376 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100378 if (snd_BUG_ON(!rfile))
379 return -EINVAL;
380
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100381 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100382 rmidi = snd_rawmidi_search(card, device);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +0200383 if (!rmidi)
384 err = -ENODEV;
385 else if (!try_module_get(rmidi->card->module))
386 err = -ENXIO;
Takashi Iwaif9d20282009-02-11 14:55:59 +0100387 mutex_unlock(&register_mutex);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +0200388 if (err < 0)
389 return err;
Takashi Iwaif9d20282009-02-11 14:55:59 +0100390
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100391 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return err;
397}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100398EXPORT_SYMBOL(snd_rawmidi_kernel_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400static int snd_rawmidi_open(struct inode *inode, struct file *file)
401{
402 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100403 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100404 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 unsigned short fflags;
406 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100407 struct snd_rawmidi *rmidi;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100408 struct snd_rawmidi_file *rawmidi_file = NULL;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200409 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Takashi Iwai5bed9132018-07-17 22:32:52 +0200411 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100412 return -EINVAL; /* invalid combination */
413
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300414 err = stream_open(inode, file);
Takashi Iwai02f48652010-04-13 11:49:04 +0200415 if (err < 0)
416 return err;
417
Clemens Ladischf1902862005-10-24 17:05:03 +0200418 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100419 rmidi = snd_lookup_minor_data(iminor(inode),
420 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200422 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100423 rmidi = snd_lookup_oss_minor_data(iminor(inode),
424 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200426 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (rmidi == NULL)
430 return -ENODEV;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100431
Takashi Iwaia0830db2012-10-16 13:05:59 +0200432 if (!try_module_get(rmidi->card->module)) {
433 snd_card_unref(rmidi->card);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100434 return -ENXIO;
Takashi Iwaia0830db2012-10-16 13:05:59 +0200435 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100436
437 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 card = rmidi->card;
439 err = snd_card_file_add(card, file);
440 if (err < 0)
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100441 goto __error_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200443 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
446 if (rawmidi_file == NULL) {
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100447 err = -ENOMEM;
448 goto __error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Jaroslav Kysela39a8fc42021-12-18 13:39:25 +0100450 rawmidi_file->user_pversion = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 init_waitqueue_entry(&wait, current);
452 add_wait_queue(&rmidi->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 while (1) {
Takashi Iwai23c18d42014-02-19 14:30:29 +0100454 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100455 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (err >= 0)
457 break;
458 if (err == -EAGAIN) {
459 if (file->f_flags & O_NONBLOCK) {
460 err = -EBUSY;
461 break;
462 }
463 } else
464 break;
465 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100466 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100468 mutex_lock(&rmidi->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +0200469 if (rmidi->card->shutdown) {
470 err = -ENODEV;
471 break;
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (signal_pending(current)) {
474 err = -ERESTARTSYS;
475 break;
476 }
477 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100478 remove_wait_queue(&rmidi->open_wait, &wait);
479 if (err < 0) {
480 kfree(rawmidi_file);
481 goto __error;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#ifdef CONFIG_SND_OSSEMUL
484 if (rawmidi_file->input && rawmidi_file->input->runtime)
485 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
486 if (rawmidi_file->output && rawmidi_file->output->runtime)
487 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
488#endif
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100489 file->private_data = rawmidi_file;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100490 mutex_unlock(&rmidi->open_mutex);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200491 snd_card_unref(rmidi->card);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100492 return 0;
493
494 __error:
495 snd_card_file_remove(card, file);
496 __error_card:
497 mutex_unlock(&rmidi->open_mutex);
498 module_put(rmidi->card->module);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200499 snd_card_unref(rmidi->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return err;
501}
502
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100503static void close_substream(struct snd_rawmidi *rmidi,
504 struct snd_rawmidi_substream *substream,
505 int cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100507 if (--substream->use_count)
508 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100510 if (cleanup) {
511 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
512 snd_rawmidi_input_trigger(substream, 0);
513 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (substream->active_sensing) {
515 unsigned char buf = 0xfe;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100516 /* sending single active sensing message
517 * to shut the device up
518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 snd_rawmidi_kernel_write(substream, &buf, 1);
520 }
521 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
522 snd_rawmidi_output_trigger(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100525 substream->ops->close(substream);
526 if (substream->runtime->private_free)
527 substream->runtime->private_free(substream);
528 snd_rawmidi_runtime_free(substream);
529 substream->opened = 0;
530 substream->append = 0;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100531 put_pid(substream->pid);
532 substream->pid = NULL;
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200533 rmidi->streams[substream->stream].substream_opened--;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100534}
535
536static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
537{
538 struct snd_rawmidi *rmidi;
539
540 rmidi = rfile->rmidi;
541 mutex_lock(&rmidi->open_mutex);
542 if (rfile->input) {
543 close_substream(rmidi, rfile->input, 1);
544 rfile->input = NULL;
545 }
546 if (rfile->output) {
547 close_substream(rmidi, rfile->output, 1);
548 rfile->output = NULL;
549 }
550 rfile->rmidi = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100551 mutex_unlock(&rmidi->open_mutex);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100552 wake_up(&rmidi->open_wait);
553}
554
555/* called from sound/core/seq/seq_midi.c */
556int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
557{
558 struct snd_rawmidi *rmidi;
559
560 if (snd_BUG_ON(!rfile))
561 return -ENXIO;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200562
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100563 rmidi = rfile->rmidi;
564 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 module_put(rmidi->card->module);
566 return 0;
567}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100568EXPORT_SYMBOL(snd_rawmidi_kernel_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570static int snd_rawmidi_release(struct inode *inode, struct file *file)
571{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100572 struct snd_rawmidi_file *rfile;
573 struct snd_rawmidi *rmidi;
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200574 struct module *module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 rfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 rmidi = rfile->rmidi;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100578 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 kfree(rfile);
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200580 module = rmidi->card->module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 snd_card_file_remove(rmidi->card, file);
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200582 module_put(module);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Adrian Bunk18612042005-11-23 13:14:50 +0100586static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
587 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100589 struct snd_rawmidi *rmidi;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (substream == NULL)
592 return -ENODEV;
593 rmidi = substream->rmidi;
594 memset(info, 0, sizeof(*info));
595 info->card = rmidi->card->number;
596 info->device = rmidi->device;
597 info->subdevice = substream->number;
598 info->stream = substream->stream;
599 info->flags = rmidi->info_flags;
600 strcpy(info->id, rmidi->id);
601 strcpy(info->name, rmidi->name);
602 strcpy(info->subname, substream->name);
603 info->subdevices_count = substream->pstr->substream_count;
604 info->subdevices_avail = (substream->pstr->substream_count -
605 substream->pstr->substream_opened);
606 return 0;
607}
608
Takashi Iwai48c9d412005-11-17 13:56:51 +0100609static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200610 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100612 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int err;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200614
615 err = snd_rawmidi_info(substream, &info);
616 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100618 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return -EFAULT;
620 return 0;
621}
622
Takashi Iwaic1cfd902017-12-14 16:44:12 +0100623static int __snd_rawmidi_info_select(struct snd_card *card,
624 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100626 struct snd_rawmidi *rmidi;
627 struct snd_rawmidi_str *pstr;
628 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100629
Clemens Ladischf87135f2005-11-20 14:06:59 +0100630 rmidi = snd_rawmidi_search(card, info->device);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100631 if (!rmidi)
632 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (info->stream < 0 || info->stream > 1)
634 return -EINVAL;
Gustavo A. R. Silva2b1d9c82019-03-20 16:15:24 -0500635 info->stream = array_index_nospec(info->stream, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 pstr = &rmidi->streams[info->stream];
637 if (pstr->substream_count == 0)
638 return -ENOENT;
639 if (info->subdevice >= pstr->substream_count)
640 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200641 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if ((unsigned int)substream->number == info->subdevice)
643 return snd_rawmidi_info(substream, info);
644 }
645 return -ENXIO;
646}
Takashi Iwaic1cfd902017-12-14 16:44:12 +0100647
648int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
649{
650 int ret;
651
652 mutex_lock(&register_mutex);
653 ret = __snd_rawmidi_info_select(card, info);
654 mutex_unlock(&register_mutex);
655 return ret;
656}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100657EXPORT_SYMBOL(snd_rawmidi_info_select);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Takashi Iwai48c9d412005-11-17 13:56:51 +0100659static int snd_rawmidi_info_select_user(struct snd_card *card,
660 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100663 struct snd_rawmidi_info info;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (get_user(info.device, &_info->device))
666 return -EFAULT;
667 if (get_user(info.stream, &_info->stream))
668 return -EFAULT;
669 if (get_user(info.subdevice, &_info->subdevice))
670 return -EFAULT;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200671 err = snd_rawmidi_info_select(card, &info);
672 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100674 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -EFAULT;
676 return 0;
677}
678
Takashi Iwaif5beb592018-07-17 23:07:29 +0200679static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime,
680 struct snd_rawmidi_params *params,
681 bool is_input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Takashi Iwai39675f72018-07-17 17:26:43 +0200683 char *newbuf, *oldbuf;
David Henningsson08fdced2021-05-15 09:15:33 +0200684 unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200685
Takashi Iwai5bed9132018-07-17 22:32:52 +0200686 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -EINVAL;
David Henningsson08fdced2021-05-15 09:15:33 +0200688 if (framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP && (params->buffer_size & 0x1f) != 0)
689 return -EINVAL;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200690 if (params->avail_min < 1 || params->avail_min > params->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (params->buffer_size != runtime->buffer_size) {
Takashi Iwai5a7b44a2018-09-03 15:16:43 +0200693 newbuf = kvzalloc(params->buffer_size, GFP_KERNEL);
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800694 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -ENOMEM;
Takashi Iwai39675f72018-07-17 17:26:43 +0200696 spin_lock_irq(&runtime->lock);
Takashi Iwaic1f6e3c2020-05-07 13:44:56 +0200697 if (runtime->buffer_ref) {
698 spin_unlock_irq(&runtime->lock);
699 kvfree(newbuf);
700 return -EBUSY;
701 }
Takashi Iwai39675f72018-07-17 17:26:43 +0200702 oldbuf = runtime->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 runtime->buffer = newbuf;
704 runtime->buffer_size = params->buffer_size;
Takashi Iwaif5beb592018-07-17 23:07:29 +0200705 __reset_runtime_ptrs(runtime, is_input);
Takashi Iwai39675f72018-07-17 17:26:43 +0200706 spin_unlock_irq(&runtime->lock);
Takashi Iwaief4db232018-07-17 23:12:33 +0200707 kvfree(oldbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709 runtime->avail_min = params->avail_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return 0;
711}
Takashi Iwaif5beb592018-07-17 23:07:29 +0200712
713int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
714 struct snd_rawmidi_params *params)
715{
716 if (substream->append && substream->use_count > 1)
717 return -EBUSY;
718 snd_rawmidi_drain_output(substream);
719 substream->active_sensing = !params->no_active_sensing;
720 return resize_runtime_buffer(substream->runtime, params, false);
721}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100722EXPORT_SYMBOL(snd_rawmidi_output_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Takashi Iwai48c9d412005-11-17 13:56:51 +0100724int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200725 struct snd_rawmidi_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
David Henningsson08fdced2021-05-15 09:15:33 +0200727 unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK;
728 unsigned int clock_type = params->mode & SNDRV_RAWMIDI_MODE_CLOCK_MASK;
729 int err;
730
731 if (framing == SNDRV_RAWMIDI_MODE_FRAMING_NONE && clock_type != SNDRV_RAWMIDI_MODE_CLOCK_NONE)
732 return -EINVAL;
733 else if (clock_type > SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC_RAW)
734 return -EINVAL;
735 if (framing > SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP)
736 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 snd_rawmidi_drain_input(substream);
David Henningsson08fdced2021-05-15 09:15:33 +0200738 err = resize_runtime_buffer(substream->runtime, params, true);
739 if (err < 0)
740 return err;
741
742 substream->framing = framing;
743 substream->clock_type = clock_type;
744 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100746EXPORT_SYMBOL(snd_rawmidi_input_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Takashi Iwai48c9d412005-11-17 13:56:51 +0100748static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
Baolin Wangd9e55822018-04-24 20:06:12 +0800749 struct snd_rawmidi_status64 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100751 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 memset(status, 0, sizeof(*status));
754 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
755 spin_lock_irq(&runtime->lock);
756 status->avail = runtime->avail;
757 spin_unlock_irq(&runtime->lock);
758 return 0;
759}
760
Takashi Iwai48c9d412005-11-17 13:56:51 +0100761static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
Baolin Wangd9e55822018-04-24 20:06:12 +0800762 struct snd_rawmidi_status64 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100764 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 memset(status, 0, sizeof(*status));
767 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
768 spin_lock_irq(&runtime->lock);
769 status->avail = runtime->avail;
770 status->xruns = runtime->xruns;
771 runtime->xruns = 0;
772 spin_unlock_irq(&runtime->lock);
773 return 0;
774}
775
Baolin Wangd9e55822018-04-24 20:06:12 +0800776static int snd_rawmidi_ioctl_status32(struct snd_rawmidi_file *rfile,
777 struct snd_rawmidi_status32 __user *argp)
778{
779 int err = 0;
780 struct snd_rawmidi_status32 __user *status = argp;
781 struct snd_rawmidi_status32 status32;
782 struct snd_rawmidi_status64 status64;
783
784 if (copy_from_user(&status32, argp,
785 sizeof(struct snd_rawmidi_status32)))
786 return -EFAULT;
787
788 switch (status32.stream) {
789 case SNDRV_RAWMIDI_STREAM_OUTPUT:
790 if (rfile->output == NULL)
791 return -EINVAL;
792 err = snd_rawmidi_output_status(rfile->output, &status64);
793 break;
794 case SNDRV_RAWMIDI_STREAM_INPUT:
795 if (rfile->input == NULL)
796 return -EINVAL;
797 err = snd_rawmidi_input_status(rfile->input, &status64);
798 break;
799 default:
800 return -EINVAL;
801 }
802 if (err < 0)
803 return err;
804
805 status32 = (struct snd_rawmidi_status32) {
806 .stream = status64.stream,
807 .tstamp_sec = status64.tstamp_sec,
808 .tstamp_nsec = status64.tstamp_nsec,
809 .avail = status64.avail,
810 .xruns = status64.xruns,
811 };
812
813 if (copy_to_user(status, &status32, sizeof(*status)))
814 return -EFAULT;
815
816 return 0;
817}
818
819static int snd_rawmidi_ioctl_status64(struct snd_rawmidi_file *rfile,
820 struct snd_rawmidi_status64 __user *argp)
821{
822 int err = 0;
823 struct snd_rawmidi_status64 status;
824
825 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status64)))
826 return -EFAULT;
827
828 switch (status.stream) {
829 case SNDRV_RAWMIDI_STREAM_OUTPUT:
830 if (rfile->output == NULL)
831 return -EINVAL;
832 err = snd_rawmidi_output_status(rfile->output, &status);
833 break;
834 case SNDRV_RAWMIDI_STREAM_INPUT:
835 if (rfile->input == NULL)
836 return -EINVAL;
837 err = snd_rawmidi_input_status(rfile->input, &status);
838 break;
839 default:
840 return -EINVAL;
841 }
842 if (err < 0)
843 return err;
844 if (copy_to_user(argp, &status,
845 sizeof(struct snd_rawmidi_status64)))
846 return -EFAULT;
847 return 0;
848}
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
851{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100852 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 void __user *argp = (void __user *)arg;
854
855 rfile = file->private_data;
856 if (((cmd >> 8) & 0xff) != 'W')
857 return -ENOTTY;
858 switch (cmd) {
859 case SNDRV_RAWMIDI_IOCTL_PVERSION:
860 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
861 case SNDRV_RAWMIDI_IOCTL_INFO:
862 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100863 int stream;
864 struct snd_rawmidi_info __user *info = argp;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (get_user(stream, &info->stream))
867 return -EFAULT;
868 switch (stream) {
869 case SNDRV_RAWMIDI_STREAM_INPUT:
870 return snd_rawmidi_info_user(rfile->input, info);
871 case SNDRV_RAWMIDI_STREAM_OUTPUT:
872 return snd_rawmidi_info_user(rfile->output, info);
873 default:
874 return -EINVAL;
875 }
876 }
Jaroslav Kysela09d23172021-09-20 19:18:50 +0200877 case SNDRV_RAWMIDI_IOCTL_USER_PVERSION:
878 if (get_user(rfile->user_pversion, (unsigned int __user *)arg))
879 return -EFAULT;
880 return 0;
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 case SNDRV_RAWMIDI_IOCTL_PARAMS:
883 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100884 struct snd_rawmidi_params params;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200885
Takashi Iwai48c9d412005-11-17 13:56:51 +0100886 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -EFAULT;
Jaroslav Kysela09d23172021-09-20 19:18:50 +0200888 if (rfile->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 2)) {
889 params.mode = 0;
890 memset(params.reserved, 0, sizeof(params.reserved));
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 switch (params.stream) {
893 case SNDRV_RAWMIDI_STREAM_OUTPUT:
894 if (rfile->output == NULL)
895 return -EINVAL;
896 return snd_rawmidi_output_params(rfile->output, &params);
897 case SNDRV_RAWMIDI_STREAM_INPUT:
898 if (rfile->input == NULL)
899 return -EINVAL;
900 return snd_rawmidi_input_params(rfile->input, &params);
901 default:
902 return -EINVAL;
903 }
904 }
Baolin Wangd9e55822018-04-24 20:06:12 +0800905 case SNDRV_RAWMIDI_IOCTL_STATUS32:
906 return snd_rawmidi_ioctl_status32(rfile, argp);
907 case SNDRV_RAWMIDI_IOCTL_STATUS64:
908 return snd_rawmidi_ioctl_status64(rfile, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 case SNDRV_RAWMIDI_IOCTL_DROP:
910 {
911 int val;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (get_user(val, (int __user *) argp))
914 return -EFAULT;
915 switch (val) {
916 case SNDRV_RAWMIDI_STREAM_OUTPUT:
917 if (rfile->output == NULL)
918 return -EINVAL;
919 return snd_rawmidi_drop_output(rfile->output);
920 default:
921 return -EINVAL;
922 }
923 }
924 case SNDRV_RAWMIDI_IOCTL_DRAIN:
925 {
926 int val;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (get_user(val, (int __user *) argp))
929 return -EFAULT;
930 switch (val) {
931 case SNDRV_RAWMIDI_STREAM_OUTPUT:
932 if (rfile->output == NULL)
933 return -EINVAL;
934 return snd_rawmidi_drain_output(rfile->output);
935 case SNDRV_RAWMIDI_STREAM_INPUT:
936 if (rfile->input == NULL)
937 return -EINVAL;
938 return snd_rawmidi_drain_input(rfile->input);
939 default:
940 return -EINVAL;
941 }
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 default:
Takashi Iwaica20d292014-02-04 18:21:39 +0100944 rmidi_dbg(rfile->rmidi,
945 "rawmidi: unknown command = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 return -ENOTTY;
948}
949
Takashi Iwai48c9d412005-11-17 13:56:51 +0100950static int snd_rawmidi_control_ioctl(struct snd_card *card,
951 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 unsigned int cmd,
953 unsigned long arg)
954{
955 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 switch (cmd) {
958 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
959 {
960 int device;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (get_user(device, (int __user *)argp))
963 return -EFAULT;
Dan Carpentera7a13d02010-09-09 00:11:41 +0200964 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
965 device = SNDRV_RAWMIDI_DEVICES - 1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100966 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 device = device < 0 ? 0 : device + 1;
968 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100969 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 break;
971 device++;
972 }
973 if (device == SNDRV_RAWMIDI_DEVICES)
974 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100975 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (put_user(device, (int __user *)argp))
977 return -EFAULT;
978 return 0;
979 }
980 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
981 {
982 int val;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (get_user(val, (int __user *)argp))
985 return -EFAULT;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100986 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return 0;
988 }
989 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
990 return snd_rawmidi_info_select_user(card, argp);
991 }
992 return -ENOIOCTLCMD;
993}
994
David Henningsson08fdced2021-05-15 09:15:33 +0200995static int receive_with_tstamp_framing(struct snd_rawmidi_substream *substream,
996 const unsigned char *buffer, int src_count, const struct timespec64 *tstamp)
997{
998 struct snd_rawmidi_runtime *runtime = substream->runtime;
999 struct snd_rawmidi_framing_tstamp *dest_ptr;
1000 struct snd_rawmidi_framing_tstamp frame = { .tv_sec = tstamp->tv_sec, .tv_nsec = tstamp->tv_nsec };
1001 int dest_frames = 0;
1002 int orig_count = src_count;
1003 int frame_size = sizeof(struct snd_rawmidi_framing_tstamp);
1004
1005 BUILD_BUG_ON(frame_size != 0x20);
1006 if (snd_BUG_ON((runtime->hw_ptr & 0x1f) != 0))
1007 return -EINVAL;
1008
1009 while (src_count > 0) {
1010 if ((int)(runtime->buffer_size - runtime->avail) < frame_size) {
1011 runtime->xruns += src_count;
1012 break;
1013 }
1014 if (src_count >= SNDRV_RAWMIDI_FRAMING_DATA_LENGTH)
1015 frame.length = SNDRV_RAWMIDI_FRAMING_DATA_LENGTH;
1016 else {
1017 frame.length = src_count;
1018 memset(frame.data, 0, SNDRV_RAWMIDI_FRAMING_DATA_LENGTH);
1019 }
1020 memcpy(frame.data, buffer, frame.length);
1021 buffer += frame.length;
1022 src_count -= frame.length;
1023 dest_ptr = (struct snd_rawmidi_framing_tstamp *) (runtime->buffer + runtime->hw_ptr);
1024 *dest_ptr = frame;
1025 runtime->avail += frame_size;
1026 runtime->hw_ptr += frame_size;
1027 runtime->hw_ptr %= runtime->buffer_size;
1028 dest_frames++;
1029 }
1030 return orig_count - src_count;
1031}
1032
1033static struct timespec64 get_framing_tstamp(struct snd_rawmidi_substream *substream)
1034{
1035 struct timespec64 ts64 = {0, 0};
1036
1037 switch (substream->clock_type) {
1038 case SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC_RAW:
1039 ktime_get_raw_ts64(&ts64);
1040 break;
1041 case SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC:
1042 ktime_get_ts64(&ts64);
1043 break;
1044 case SNDRV_RAWMIDI_MODE_CLOCK_REALTIME:
1045 ktime_get_real_ts64(&ts64);
1046 break;
1047 }
1048 return ts64;
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/**
1052 * snd_rawmidi_receive - receive the input data from the device
1053 * @substream: the rawmidi substream
1054 * @buffer: the buffer pointer
1055 * @count: the data size to read
1056 *
1057 * Reads the data from the internal buffer.
1058 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001059 * Return: The size of read data, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001061int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
1062 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 unsigned long flags;
David Henningsson08fdced2021-05-15 09:15:33 +02001065 struct timespec64 ts64 = get_framing_tstamp(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001067 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Takashi Iwai219df322008-11-03 08:17:05 +01001069 if (!substream->opened)
1070 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001072 rmidi_dbg(substream->rmidi,
1073 "snd_rawmidi_receive: input is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return -EINVAL;
1075 }
David Henningsson08fdced2021-05-15 09:15:33 +02001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 spin_lock_irqsave(&runtime->lock, flags);
David Henningsson08fdced2021-05-15 09:15:33 +02001078 if (substream->framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) {
1079 result = receive_with_tstamp_framing(substream, buffer, count, &ts64);
1080 } else if (count == 1) { /* special case, faster code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 substream->bytes++;
1082 if (runtime->avail < runtime->buffer_size) {
1083 runtime->buffer[runtime->hw_ptr++] = buffer[0];
1084 runtime->hw_ptr %= runtime->buffer_size;
1085 runtime->avail++;
1086 result++;
1087 } else {
1088 runtime->xruns++;
1089 }
1090 } else {
1091 substream->bytes += count;
1092 count1 = runtime->buffer_size - runtime->hw_ptr;
1093 if (count1 > count)
1094 count1 = count;
1095 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1096 count1 = runtime->buffer_size - runtime->avail;
1097 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
1098 runtime->hw_ptr += count1;
1099 runtime->hw_ptr %= runtime->buffer_size;
1100 runtime->avail += count1;
1101 count -= count1;
1102 result += count1;
1103 if (count > 0) {
1104 buffer += count1;
1105 count1 = count;
1106 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
1107 count1 = runtime->buffer_size - runtime->avail;
1108 runtime->xruns += count - count1;
1109 }
1110 if (count1 > 0) {
1111 memcpy(runtime->buffer, buffer, count1);
1112 runtime->hw_ptr = count1;
1113 runtime->avail += count1;
1114 result += count1;
1115 }
1116 }
1117 }
1118 if (result > 0) {
1119 if (runtime->event)
Takashi Iwaib3c705a2011-06-14 14:37:06 +02001120 schedule_work(&runtime->event_work);
Takashi Iwai88a06d62020-12-06 09:35:27 +01001121 else if (__snd_rawmidi_ready(runtime))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 wake_up(&runtime->sleep);
1123 }
1124 spin_unlock_irqrestore(&runtime->lock, flags);
1125 return result;
1126}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001127EXPORT_SYMBOL(snd_rawmidi_receive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Takashi Iwai48c9d412005-11-17 13:56:51 +01001129static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001130 unsigned char __user *userbuf,
1131 unsigned char *kernelbuf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 unsigned long flags;
1134 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001135 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai81f57752016-02-03 14:41:22 +01001136 unsigned long appl_ptr;
Takashi Iwaic1f6e3c2020-05-07 13:44:56 +02001137 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Takashi Iwai81f57752016-02-03 14:41:22 +01001139 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwaic1f6e3c2020-05-07 13:44:56 +02001140 snd_rawmidi_buffer_ref(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 while (count > 0 && runtime->avail) {
1142 count1 = runtime->buffer_size - runtime->appl_ptr;
1143 if (count1 > count)
1144 count1 = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (count1 > (int)runtime->avail)
1146 count1 = runtime->avail;
Takashi Iwai81f57752016-02-03 14:41:22 +01001147
1148 /* update runtime->appl_ptr before unlocking for userbuf */
1149 appl_ptr = runtime->appl_ptr;
1150 runtime->appl_ptr += count1;
1151 runtime->appl_ptr %= runtime->buffer_size;
1152 runtime->avail -= count1;
1153
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001154 if (kernelbuf)
Takashi Iwai81f57752016-02-03 14:41:22 +01001155 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001156 if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 spin_unlock_irqrestore(&runtime->lock, flags);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001158 if (copy_to_user(userbuf + result,
Takashi Iwaic1f6e3c2020-05-07 13:44:56 +02001159 runtime->buffer + appl_ptr, count1))
1160 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwaic1f6e3c2020-05-07 13:44:56 +02001162 if (err)
1163 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 result += count1;
1166 count -= count1;
1167 }
Takashi Iwaic1f6e3c2020-05-07 13:44:56 +02001168 out:
1169 snd_rawmidi_buffer_unref(runtime);
Takashi Iwai81f57752016-02-03 14:41:22 +01001170 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwaic1f6e3c2020-05-07 13:44:56 +02001171 return result > 0 ? result : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
Takashi Iwai48c9d412005-11-17 13:56:51 +01001174long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
1175 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 snd_rawmidi_input_trigger(substream, 1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001178 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001180EXPORT_SYMBOL(snd_rawmidi_kernel_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Takashi Iwai48c9d412005-11-17 13:56:51 +01001182static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1183 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 long result;
1186 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001187 struct snd_rawmidi_file *rfile;
1188 struct snd_rawmidi_substream *substream;
1189 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 rfile = file->private_data;
1192 substream = rfile->input;
1193 if (substream == NULL)
1194 return -EIO;
1195 runtime = substream->runtime;
1196 snd_rawmidi_input_trigger(substream, 1);
1197 result = 0;
1198 while (count > 0) {
1199 spin_lock_irq(&runtime->lock);
Takashi Iwai88a06d62020-12-06 09:35:27 +01001200 while (!__snd_rawmidi_ready(runtime)) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02001201 wait_queue_entry_t wait;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1204 spin_unlock_irq(&runtime->lock);
1205 return result > 0 ? result : -EAGAIN;
1206 }
1207 init_waitqueue_entry(&wait, current);
1208 add_wait_queue(&runtime->sleep, &wait);
1209 set_current_state(TASK_INTERRUPTIBLE);
1210 spin_unlock_irq(&runtime->lock);
1211 schedule();
1212 remove_wait_queue(&runtime->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001213 if (rfile->rmidi->card->shutdown)
1214 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (signal_pending(current))
1216 return result > 0 ? result : -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 spin_lock_irq(&runtime->lock);
Takashi Iwai88a06d62020-12-06 09:35:27 +01001218 if (!runtime->avail) {
1219 spin_unlock_irq(&runtime->lock);
1220 return result > 0 ? result : -EIO;
1221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001224 count1 = snd_rawmidi_kernel_read1(substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001225 (unsigned char __user *)buf,
1226 NULL/*kernelbuf*/,
1227 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (count1 < 0)
1229 return result > 0 ? result : count1;
1230 result += count1;
1231 buf += count1;
1232 count -= count1;
1233 }
1234 return result;
1235}
1236
1237/**
1238 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1239 * @substream: the rawmidi substream
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001240 *
1241 * Return: 1 if the internal output buffer is empty, 0 if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001243int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001245 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int result;
1247 unsigned long flags;
1248
1249 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001250 rmidi_dbg(substream->rmidi,
1251 "snd_rawmidi_transmit_empty: output is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return 1;
1253 }
1254 spin_lock_irqsave(&runtime->lock, flags);
1255 result = runtime->avail >= runtime->buffer_size;
1256 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwai5bed9132018-07-17 22:32:52 +02001257 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001259EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261/**
Takashi Iwai06ab3002016-01-31 11:57:41 +01001262 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 * @substream: the rawmidi substream
1264 * @buffer: the buffer pointer
1265 * @count: data size to transfer
1266 *
Takashi Iwai06ab3002016-01-31 11:57:41 +01001267 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 */
Takashi Iwai06ab3002016-01-31 11:57:41 +01001269int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001270 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001273 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001276 rmidi_dbg(substream->rmidi,
1277 "snd_rawmidi_transmit_peek: output is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return -EINVAL;
1279 }
1280 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (runtime->avail >= runtime->buffer_size) {
1282 /* warning: lowlevel layer MUST trigger down the hardware */
1283 goto __skip;
1284 }
1285 if (count == 1) { /* special case, faster code */
1286 *buffer = runtime->buffer[runtime->hw_ptr];
1287 result++;
1288 } else {
1289 count1 = runtime->buffer_size - runtime->hw_ptr;
1290 if (count1 > count)
1291 count1 = count;
1292 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1293 count1 = runtime->buffer_size - runtime->avail;
1294 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1295 count -= count1;
1296 result += count1;
1297 if (count > 0) {
1298 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1299 count = runtime->buffer_size - runtime->avail - count1;
1300 memcpy(buffer + count1, runtime->buffer, count);
1301 result += count;
1302 }
1303 }
1304 __skip:
Takashi Iwai06ab3002016-01-31 11:57:41 +01001305 return result;
1306}
1307EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1308
1309/**
1310 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1311 * @substream: the rawmidi substream
1312 * @buffer: the buffer pointer
1313 * @count: data size to transfer
1314 *
1315 * Copies data from the internal output buffer to the given buffer.
1316 *
1317 * Call this in the interrupt handler when the midi output is ready,
1318 * and call snd_rawmidi_transmit_ack() after the transmission is
1319 * finished.
1320 *
1321 * Return: The size of copied data, or a negative error code on failure.
1322 */
1323int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1324 unsigned char *buffer, int count)
1325{
1326 struct snd_rawmidi_runtime *runtime = substream->runtime;
1327 int result;
1328 unsigned long flags;
1329
1330 spin_lock_irqsave(&runtime->lock, flags);
1331 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 spin_unlock_irqrestore(&runtime->lock, flags);
1333 return result;
1334}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001335EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337/**
Takashi Iwai06ab3002016-01-31 11:57:41 +01001338 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1339 * @substream: the rawmidi substream
1340 * @count: the transferred count
1341 *
1342 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1343 */
1344int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1345{
1346 struct snd_rawmidi_runtime *runtime = substream->runtime;
1347
1348 if (runtime->buffer == NULL) {
1349 rmidi_dbg(substream->rmidi,
1350 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1351 return -EINVAL;
1352 }
1353 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1354 runtime->hw_ptr += count;
1355 runtime->hw_ptr %= runtime->buffer_size;
1356 runtime->avail += count;
1357 substream->bytes += count;
1358 if (count > 0) {
Takashi Iwai88a06d62020-12-06 09:35:27 +01001359 if (runtime->drain || __snd_rawmidi_ready(runtime))
Takashi Iwai06ab3002016-01-31 11:57:41 +01001360 wake_up(&runtime->sleep);
1361 }
1362 return count;
1363}
1364EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1365
1366/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 * snd_rawmidi_transmit_ack - acknowledge the transmission
1368 * @substream: the rawmidi substream
Masanari Iida0a114582014-02-09 00:47:36 +09001369 * @count: the transferred count
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 *
1371 * Advances the hardware pointer for the internal output buffer with
1372 * the given size and updates the condition.
1373 * Call after the transmission is finished.
1374 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001375 * Return: The advanced size if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001377int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001379 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai06ab3002016-01-31 11:57:41 +01001380 int result;
1381 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai06ab3002016-01-31 11:57:41 +01001384 result = __snd_rawmidi_transmit_ack(substream, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwai06ab3002016-01-31 11:57:41 +01001386 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001388EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390/**
1391 * snd_rawmidi_transmit - copy from the buffer to the device
1392 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001393 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 * @count: the data size to transfer
Takashi Iwai5bed9132018-07-17 22:32:52 +02001395 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 * Copies data from the buffer to the device and advances the pointer.
1397 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001398 * Return: The copied size if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001400int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1401 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Takashi Iwai06ab3002016-01-31 11:57:41 +01001403 struct snd_rawmidi_runtime *runtime = substream->runtime;
1404 int result;
1405 unsigned long flags;
1406
1407 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai219df322008-11-03 08:17:05 +01001408 if (!substream->opened)
Takashi Iwai06ab3002016-01-31 11:57:41 +01001409 result = -EBADFD;
1410 else {
1411 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1412 if (count <= 0)
1413 result = count;
1414 else
1415 result = __snd_rawmidi_transmit_ack(substream, count);
1416 }
1417 spin_unlock_irqrestore(&runtime->lock, flags);
1418 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001420EXPORT_SYMBOL(snd_rawmidi_transmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Takashi Iwai6aea5702018-09-13 08:20:43 +02001422/**
1423 * snd_rawmidi_proceed - Discard the all pending bytes and proceed
1424 * @substream: rawmidi substream
1425 *
1426 * Return: the number of discarded bytes
1427 */
1428int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream)
1429{
1430 struct snd_rawmidi_runtime *runtime = substream->runtime;
1431 unsigned long flags;
1432 int count = 0;
1433
1434 spin_lock_irqsave(&runtime->lock, flags);
1435 if (runtime->avail < runtime->buffer_size) {
1436 count = runtime->buffer_size - runtime->avail;
1437 __snd_rawmidi_transmit_ack(substream, count);
1438 }
1439 spin_unlock_irqrestore(&runtime->lock, flags);
1440 return count;
1441}
1442EXPORT_SYMBOL(snd_rawmidi_proceed);
1443
Takashi Iwai48c9d412005-11-17 13:56:51 +01001444static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001445 const unsigned char __user *userbuf,
1446 const unsigned char *kernelbuf,
1447 long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 unsigned long flags;
1450 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001451 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai81f57752016-02-03 14:41:22 +01001452 unsigned long appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Takashi Iwaicc85f7a2016-02-01 12:04:55 +01001454 if (!kernelbuf && !userbuf)
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001455 return -EINVAL;
1456 if (snd_BUG_ON(!runtime->buffer))
1457 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 result = 0;
1460 spin_lock_irqsave(&runtime->lock, flags);
1461 if (substream->append) {
1462 if ((long)runtime->avail < count) {
1463 spin_unlock_irqrestore(&runtime->lock, flags);
1464 return -EAGAIN;
1465 }
1466 }
Takashi Iwaic1f6e3c2020-05-07 13:44:56 +02001467 snd_rawmidi_buffer_ref(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 while (count > 0 && runtime->avail > 0) {
1469 count1 = runtime->buffer_size - runtime->appl_ptr;
1470 if (count1 > count)
1471 count1 = count;
1472 if (count1 > (long)runtime->avail)
1473 count1 = runtime->avail;
Takashi Iwai81f57752016-02-03 14:41:22 +01001474
1475 /* update runtime->appl_ptr before unlocking for userbuf */
1476 appl_ptr = runtime->appl_ptr;
1477 runtime->appl_ptr += count1;
1478 runtime->appl_ptr %= runtime->buffer_size;
1479 runtime->avail -= count1;
1480
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001481 if (kernelbuf)
Takashi Iwai81f57752016-02-03 14:41:22 +01001482 memcpy(runtime->buffer + appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001483 kernelbuf + result, count1);
1484 else if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwai81f57752016-02-03 14:41:22 +01001486 if (copy_from_user(runtime->buffer + appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001487 userbuf + result, count1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 spin_lock_irqsave(&runtime->lock, flags);
1489 result = result > 0 ? result : -EFAULT;
1490 goto __end;
1491 }
1492 spin_lock_irqsave(&runtime->lock, flags);
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 result += count1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 count -= count1;
1496 }
1497 __end:
1498 count1 = runtime->avail < runtime->buffer_size;
Takashi Iwaic1f6e3c2020-05-07 13:44:56 +02001499 snd_rawmidi_buffer_unref(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 spin_unlock_irqrestore(&runtime->lock, flags);
1501 if (count1)
1502 snd_rawmidi_output_trigger(substream, 1);
1503 return result;
1504}
1505
Takashi Iwai48c9d412005-11-17 13:56:51 +01001506long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1507 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001509 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001511EXPORT_SYMBOL(snd_rawmidi_kernel_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Takashi Iwai48c9d412005-11-17 13:56:51 +01001513static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1514 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 long result, timeout;
1517 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001518 struct snd_rawmidi_file *rfile;
1519 struct snd_rawmidi_runtime *runtime;
1520 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 rfile = file->private_data;
1523 substream = rfile->output;
1524 runtime = substream->runtime;
1525 /* we cannot put an atomic message to our buffer */
1526 if (substream->append && count > runtime->buffer_size)
1527 return -EIO;
1528 result = 0;
1529 while (count > 0) {
1530 spin_lock_irq(&runtime->lock);
1531 while (!snd_rawmidi_ready_append(substream, count)) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02001532 wait_queue_entry_t wait;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (file->f_flags & O_NONBLOCK) {
1535 spin_unlock_irq(&runtime->lock);
1536 return result > 0 ? result : -EAGAIN;
1537 }
1538 init_waitqueue_entry(&wait, current);
1539 add_wait_queue(&runtime->sleep, &wait);
1540 set_current_state(TASK_INTERRUPTIBLE);
1541 spin_unlock_irq(&runtime->lock);
1542 timeout = schedule_timeout(30 * HZ);
1543 remove_wait_queue(&runtime->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001544 if (rfile->rmidi->card->shutdown)
1545 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (signal_pending(current))
1547 return result > 0 ? result : -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 spin_lock_irq(&runtime->lock);
Takashi Iwai88a06d62020-12-06 09:35:27 +01001549 if (!runtime->avail && !timeout) {
1550 spin_unlock_irq(&runtime->lock);
1551 return result > 0 ? result : -EIO;
1552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
1554 spin_unlock_irq(&runtime->lock);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001555 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (count1 < 0)
1557 return result > 0 ? result : count1;
1558 result += count1;
1559 buf += count1;
1560 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1561 break;
1562 count -= count1;
1563 }
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001564 if (file->f_flags & O_DSYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 spin_lock_irq(&runtime->lock);
1566 while (runtime->avail != runtime->buffer_size) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02001567 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 unsigned int last_avail = runtime->avail;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 init_waitqueue_entry(&wait, current);
1571 add_wait_queue(&runtime->sleep, &wait);
1572 set_current_state(TASK_INTERRUPTIBLE);
1573 spin_unlock_irq(&runtime->lock);
1574 timeout = schedule_timeout(30 * HZ);
1575 remove_wait_queue(&runtime->sleep, &wait);
1576 if (signal_pending(current))
1577 return result > 0 ? result : -ERESTARTSYS;
1578 if (runtime->avail == last_avail && !timeout)
1579 return result > 0 ? result : -EIO;
1580 spin_lock_irq(&runtime->lock);
1581 }
1582 spin_unlock_irq(&runtime->lock);
1583 }
1584 return result;
1585}
1586
Takashi Iwai5bed9132018-07-17 22:32:52 +02001587static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001589 struct snd_rawmidi_file *rfile;
1590 struct snd_rawmidi_runtime *runtime;
Al Viro680ef722017-07-02 23:27:36 -04001591 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 rfile = file->private_data;
1594 if (rfile->input != NULL) {
1595 runtime = rfile->input->runtime;
1596 snd_rawmidi_input_trigger(rfile->input, 1);
1597 poll_wait(file, &runtime->sleep, wait);
1598 }
1599 if (rfile->output != NULL) {
1600 runtime = rfile->output->runtime;
1601 poll_wait(file, &runtime->sleep, wait);
1602 }
1603 mask = 0;
1604 if (rfile->input != NULL) {
1605 if (snd_rawmidi_ready(rfile->input))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001606 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608 if (rfile->output != NULL) {
1609 if (snd_rawmidi_ready(rfile->output))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001610 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612 return mask;
1613}
1614
1615/*
1616 */
1617#ifdef CONFIG_COMPAT
1618#include "rawmidi_compat.c"
1619#else
1620#define snd_rawmidi_ioctl_compat NULL
1621#endif
1622
1623/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 */
1625
Takashi Iwai48c9d412005-11-17 13:56:51 +01001626static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1627 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001629 struct snd_rawmidi *rmidi;
1630 struct snd_rawmidi_substream *substream;
1631 struct snd_rawmidi_runtime *runtime;
Takashi Iwai88a06d62020-12-06 09:35:27 +01001632 unsigned long buffer_size, avail, xruns;
David Henningsson08fdced2021-05-15 09:15:33 +02001633 unsigned int clock_type;
1634 static const char *clock_names[4] = { "none", "realtime", "monotonic", "monotonic raw" };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 rmidi = entry->private_data;
1637 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001638 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001640 list_for_each_entry(substream,
1641 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1642 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 snd_iprintf(buffer,
1644 "Output %d\n"
1645 " Tx bytes : %lu\n",
1646 substream->number,
1647 (unsigned long) substream->bytes);
1648 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001649 snd_iprintf(buffer,
1650 " Owner PID : %d\n",
1651 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 runtime = substream->runtime;
Takashi Iwai88a06d62020-12-06 09:35:27 +01001653 spin_lock_irq(&runtime->lock);
1654 buffer_size = runtime->buffer_size;
1655 avail = runtime->avail;
1656 spin_unlock_irq(&runtime->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 snd_iprintf(buffer,
1658 " Mode : %s\n"
1659 " Buffer size : %lu\n"
1660 " Avail : %lu\n",
1661 runtime->oss ? "OSS compatible" : "native",
Takashi Iwai88a06d62020-12-06 09:35:27 +01001662 buffer_size, avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664 }
1665 }
1666 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001667 list_for_each_entry(substream,
1668 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1669 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 snd_iprintf(buffer,
1671 "Input %d\n"
1672 " Rx bytes : %lu\n",
1673 substream->number,
1674 (unsigned long) substream->bytes);
1675 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001676 snd_iprintf(buffer,
1677 " Owner PID : %d\n",
1678 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 runtime = substream->runtime;
Takashi Iwai88a06d62020-12-06 09:35:27 +01001680 spin_lock_irq(&runtime->lock);
1681 buffer_size = runtime->buffer_size;
1682 avail = runtime->avail;
1683 xruns = runtime->xruns;
1684 spin_unlock_irq(&runtime->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 snd_iprintf(buffer,
1686 " Buffer size : %lu\n"
1687 " Avail : %lu\n"
1688 " Overruns : %lu\n",
Takashi Iwai88a06d62020-12-06 09:35:27 +01001689 buffer_size, avail, xruns);
David Henningsson08fdced2021-05-15 09:15:33 +02001690 if (substream->framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) {
1691 clock_type = substream->clock_type >> SNDRV_RAWMIDI_MODE_CLOCK_SHIFT;
Colin Ian Kinga6de7b32021-05-19 11:54:24 +01001692 if (!snd_BUG_ON(clock_type >= ARRAY_SIZE(clock_names)))
David Henningsson08fdced2021-05-15 09:15:33 +02001693 snd_iprintf(buffer,
1694 " Framing : tstamp\n"
1695 " Clock type : %s\n",
1696 clock_names[clock_type]);
1697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699 }
1700 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001701 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
1704/*
1705 * Register functions
1706 */
1707
Takashi Iwai5bed9132018-07-17 22:32:52 +02001708static const struct file_operations snd_rawmidi_f_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 .owner = THIS_MODULE,
1710 .read = snd_rawmidi_read,
1711 .write = snd_rawmidi_write,
1712 .open = snd_rawmidi_open,
1713 .release = snd_rawmidi_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001714 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 .poll = snd_rawmidi_poll,
1716 .unlocked_ioctl = snd_rawmidi_ioctl,
1717 .compat_ioctl = snd_rawmidi_ioctl_compat,
1718};
1719
Takashi Iwai48c9d412005-11-17 13:56:51 +01001720static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1721 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 int direction,
1723 int count)
1724{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001725 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 int idx;
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001729 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +01001730 if (!substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return -ENOMEM;
1732 substream->stream = direction;
1733 substream->number = idx;
1734 substream->rmidi = rmidi;
1735 substream->pstr = stream;
1736 list_add_tail(&substream->list, &stream->substreams);
1737 stream->substream_count++;
1738 }
1739 return 0;
1740}
1741
Takashi Iwaiaee50122015-01-29 17:55:52 +01001742static void release_rawmidi_device(struct device *dev)
1743{
1744 kfree(container_of(dev, struct snd_rawmidi, dev));
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747/**
1748 * snd_rawmidi_new - create a rawmidi instance
1749 * @card: the card instance
1750 * @id: the id string
1751 * @device: the device index
1752 * @output_count: the number of output streams
1753 * @input_count: the number of input streams
1754 * @rrawmidi: the pointer to store the new rawmidi instance
1755 *
1756 * Creates a new rawmidi instance.
1757 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1758 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001759 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001761int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 int output_count, int input_count,
Takashi Iwai5bed9132018-07-17 22:32:52 +02001763 struct snd_rawmidi **rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001765 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 int err;
Takashi Iwaif15ee212020-01-03 09:16:20 +01001767 static const struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 .dev_free = snd_rawmidi_dev_free,
1769 .dev_register = snd_rawmidi_dev_register,
1770 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 };
1772
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001773 if (snd_BUG_ON(!card))
1774 return -ENXIO;
1775 if (rrawmidi)
1776 *rrawmidi = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001777 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +01001778 if (!rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return -ENOMEM;
1780 rmidi->card = card;
1781 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001782 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001784 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1785 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (id != NULL)
Joe Perches75b1a8f2021-01-04 09:17:34 -08001788 strscpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwaiaee50122015-01-29 17:55:52 +01001789
1790 snd_device_initialize(&rmidi->dev, card);
1791 rmidi->dev.release = release_rawmidi_device;
1792 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1793
Takashi Iwai5bed9132018-07-17 22:32:52 +02001794 err = snd_rawmidi_alloc_substreams(rmidi,
1795 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1796 SNDRV_RAWMIDI_STREAM_INPUT,
1797 input_count);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001798 if (err < 0)
1799 goto error;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001800 err = snd_rawmidi_alloc_substreams(rmidi,
1801 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1802 SNDRV_RAWMIDI_STREAM_OUTPUT,
1803 output_count);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001804 if (err < 0)
1805 goto error;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001806 err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001807 if (err < 0)
1808 goto error;
1809
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001810 if (rrawmidi)
1811 *rrawmidi = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return 0;
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001813
1814 error:
1815 snd_rawmidi_free(rmidi);
1816 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001818EXPORT_SYMBOL(snd_rawmidi_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Takashi Iwai48c9d412005-11-17 13:56:51 +01001820static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001822 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001825 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 list_del(&substream->list);
1827 kfree(substream);
1828 }
1829}
1830
Takashi Iwai48c9d412005-11-17 13:56:51 +01001831static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001833 if (!rmidi)
1834 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +02001835
1836 snd_info_free_entry(rmidi->proc_entry);
1837 rmidi->proc_entry = NULL;
1838 mutex_lock(&register_mutex);
1839 if (rmidi->ops && rmidi->ops->dev_unregister)
1840 rmidi->ops->dev_unregister(rmidi);
1841 mutex_unlock(&register_mutex);
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1844 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1845 if (rmidi->private_free)
1846 rmidi->private_free(rmidi);
Takashi Iwaiaee50122015-01-29 17:55:52 +01001847 put_device(&rmidi->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return 0;
1849}
1850
Takashi Iwai48c9d412005-11-17 13:56:51 +01001851static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001853 struct snd_rawmidi *rmidi = device->device_data;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return snd_rawmidi_free(rmidi);
1856}
1857
Takashi Iwai111b0cd2017-06-09 15:11:58 +02001858#if IS_ENABLED(CONFIG_SND_SEQUENCER)
Takashi Iwai48c9d412005-11-17 13:56:51 +01001859static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001861 struct snd_rawmidi *rmidi = device->private_data;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 rmidi->seq_dev = NULL;
1864}
1865#endif
1866
Takashi Iwai48c9d412005-11-17 13:56:51 +01001867static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001869 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001870 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001872 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1875 return -ENOMEM;
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001876 err = 0;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001877 mutex_lock(&register_mutex);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001878 if (snd_rawmidi_search(rmidi->card, rmidi->device))
1879 err = -EBUSY;
1880 else
1881 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Takashi Iwai816f3182016-08-30 14:45:46 +02001882 mutex_unlock(&register_mutex);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001883 if (err < 0)
1884 return err;
1885
Takashi Iwai40a4b262015-01-30 08:34:58 +01001886 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1887 rmidi->card, rmidi->device,
1888 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
Takashi Iwaiaee50122015-01-29 17:55:52 +01001889 if (err < 0) {
1890 rmidi_err(rmidi, "unable to register\n");
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001891 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001893 if (rmidi->ops && rmidi->ops->dev_register) {
1894 err = rmidi->ops->dev_register(rmidi);
1895 if (err < 0)
1896 goto error_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898#ifdef CONFIG_SND_OSSEMUL
1899 rmidi->ossreg = 0;
1900 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1901 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001902 rmidi->card, 0, &snd_rawmidi_f_ops,
Takashi Iwai80d7d772014-02-04 13:51:45 +01001903 rmidi) < 0) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001904 rmidi_err(rmidi,
1905 "unable to register OSS rawmidi device %i:%i\n",
1906 rmidi->card->number, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 } else {
1908 rmidi->ossreg++;
1909#ifdef SNDRV_OSS_INFO_DEV_MIDI
1910 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1911#endif
1912 }
1913 }
1914 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1915 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001916 rmidi->card, 1, &snd_rawmidi_f_ops,
Takashi Iwai80d7d772014-02-04 13:51:45 +01001917 rmidi) < 0) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001918 rmidi_err(rmidi,
1919 "unable to register OSS rawmidi device %i:%i\n",
1920 rmidi->card->number, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 } else {
1922 rmidi->ossreg++;
1923 }
1924 }
1925#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 sprintf(name, "midi%d", rmidi->device);
1927 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1928 if (entry) {
1929 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 entry->c.text.read = snd_rawmidi_proc_info_read;
1931 if (snd_info_register(entry) < 0) {
1932 snd_info_free_entry(entry);
1933 entry = NULL;
1934 }
1935 }
1936 rmidi->proc_entry = entry;
Takashi Iwai111b0cd2017-06-09 15:11:58 +02001937#if IS_ENABLED(CONFIG_SND_SEQUENCER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1939 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1940 rmidi->seq_dev->private_data = rmidi;
1941 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1942 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1943 snd_device_register(rmidi->card, rmidi->seq_dev);
1944 }
1945 }
1946#endif
1947 return 0;
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001948
1949 error_unregister:
1950 snd_unregister_device(&rmidi->dev);
1951 error:
1952 mutex_lock(&register_mutex);
1953 list_del(&rmidi->list);
1954 mutex_unlock(&register_mutex);
1955 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Takashi Iwai48c9d412005-11-17 13:56:51 +01001958static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001960 struct snd_rawmidi *rmidi = device->device_data;
Takashi Iwai0914f792012-10-16 16:43:39 +02001961 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001963 mutex_lock(&register_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02001964 mutex_lock(&rmidi->open_mutex);
1965 wake_up(&rmidi->open_wait);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001966 list_del_init(&rmidi->list);
Takashi Iwai0914f792012-10-16 16:43:39 +02001967 for (dir = 0; dir < 2; dir++) {
1968 struct snd_rawmidi_substream *s;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001969
Takashi Iwai0914f792012-10-16 16:43:39 +02001970 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1971 if (s->runtime)
1972 wake_up(&s->runtime->sleep);
1973 }
1974 }
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976#ifdef CONFIG_SND_OSSEMUL
1977 if (rmidi->ossreg) {
1978 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1979 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1980#ifdef SNDRV_OSS_INFO_DEV_MIDI
1981 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1982#endif
1983 }
1984 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1985 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1986 rmidi->ossreg = 0;
1987 }
1988#endif /* CONFIG_SND_OSSEMUL */
Takashi Iwai40a4b262015-01-30 08:34:58 +01001989 snd_unregister_device(&rmidi->dev);
Takashi Iwai0914f792012-10-16 16:43:39 +02001990 mutex_unlock(&rmidi->open_mutex);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001991 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
1995/**
1996 * snd_rawmidi_set_ops - set the rawmidi operators
1997 * @rmidi: the rawmidi instance
1998 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1999 * @ops: the operator table
2000 *
2001 * Sets the rawmidi operators for the given stream direction.
2002 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01002003void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
Takashi Iwai6ba79b82017-01-05 17:01:14 +01002004 const struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Takashi Iwai48c9d412005-11-17 13:56:51 +01002006 struct snd_rawmidi_substream *substream;
Takashi Iwai5bed9132018-07-17 22:32:52 +02002007
Johannes Berg9244b2c2006-10-05 16:02:22 +02002008 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01002011EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013/*
2014 * ENTRY functions
2015 */
2016
2017static int __init alsa_rawmidi_init(void)
2018{
2019
2020 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
2021 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
2022#ifdef CONFIG_SND_OSSEMUL
2023 { int i;
2024 /* check device map table */
2025 for (i = 0; i < SNDRV_CARDS; i++) {
2026 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
Takashi Iwaica20d292014-02-04 18:21:39 +01002027 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
2028 i, midi_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 midi_map[i] = 0;
2030 }
2031 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
Takashi Iwaica20d292014-02-04 18:21:39 +01002032 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
2033 i, amidi_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 amidi_map[i] = 1;
2035 }
2036 }
2037 }
2038#endif /* CONFIG_SND_OSSEMUL */
2039 return 0;
2040}
2041
2042static void __exit alsa_rawmidi_exit(void)
2043{
2044 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
2045 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
2046}
2047
2048module_init(alsa_rawmidi_init)
2049module_exit(alsa_rawmidi_exit)