blob: ee601d7f092694aecd7e853845b4e3f73cd0f261 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Abstract layer for MIDI v1.0 stream
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <sound/core.h>
23#include <linux/major.h>
24#include <linux/init.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010025#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/slab.h>
27#include <linux/time.h>
28#include <linux/wait.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010029#include <linux/mutex.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040030#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/delay.h>
Takashi Iwaief4db232018-07-17 23:12:33 +020032#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/rawmidi.h>
34#include <sound/info.h>
35#include <sound/control.h>
36#include <sound/minors.h>
37#include <sound/initval.h>
38
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020039MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
41MODULE_LICENSE("GPL");
42
43#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai6581f4e2006-05-17 17:14:51 +020044static int midi_map[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
46module_param_array(midi_map, int, NULL, 0444);
47MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
48module_param_array(amidi_map, int, NULL, 0444);
49MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
50#endif /* CONFIG_SND_OSSEMUL */
51
Takashi Iwai48c9d412005-11-17 13:56:51 +010052static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
53static int snd_rawmidi_dev_free(struct snd_device *device);
54static int snd_rawmidi_dev_register(struct snd_device *device);
55static int snd_rawmidi_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Clemens Ladischf87135f2005-11-20 14:06:59 +010057static LIST_HEAD(snd_rawmidi_devices);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010058static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Takashi Iwaica20d292014-02-04 18:21:39 +010060#define rmidi_err(rmidi, fmt, args...) \
Takashi Iwai07cc3e82015-01-30 15:31:52 +010061 dev_err(&(rmidi)->dev, fmt, ##args)
Takashi Iwaica20d292014-02-04 18:21:39 +010062#define rmidi_warn(rmidi, fmt, args...) \
Takashi Iwai07cc3e82015-01-30 15:31:52 +010063 dev_warn(&(rmidi)->dev, fmt, ##args)
Takashi Iwaica20d292014-02-04 18:21:39 +010064#define rmidi_dbg(rmidi, fmt, args...) \
Takashi Iwai07cc3e82015-01-30 15:31:52 +010065 dev_dbg(&(rmidi)->dev, fmt, ##args)
Takashi Iwaica20d292014-02-04 18:21:39 +010066
Clemens Ladischf87135f2005-11-20 14:06:59 +010067static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
68{
Clemens Ladischf87135f2005-11-20 14:06:59 +010069 struct snd_rawmidi *rawmidi;
70
Johannes Berg9244b2c2006-10-05 16:02:22 +020071 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
Clemens Ladischf87135f2005-11-20 14:06:59 +010072 if (rawmidi->card == card && rawmidi->device == device)
73 return rawmidi;
Clemens Ladischf87135f2005-11-20 14:06:59 +010074 return NULL;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static inline unsigned short snd_rawmidi_file_flags(struct file *file)
78{
79 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
80 case FMODE_WRITE:
81 return SNDRV_RAWMIDI_LFLG_OUTPUT;
82 case FMODE_READ:
83 return SNDRV_RAWMIDI_LFLG_INPUT;
84 default:
85 return SNDRV_RAWMIDI_LFLG_OPEN;
86 }
87}
88
Takashi Iwai48c9d412005-11-17 13:56:51 +010089static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Takashi Iwai48c9d412005-11-17 13:56:51 +010091 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai5bed9132018-07-17 22:32:52 +020092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return runtime->avail >= runtime->avail_min;
94}
95
Takashi Iwai48c9d412005-11-17 13:56:51 +010096static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
97 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Takashi Iwai48c9d412005-11-17 13:56:51 +010099 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return runtime->avail >= runtime->avail_min &&
102 (!substream->append || runtime->avail >= count);
103}
104
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200105static void snd_rawmidi_input_event_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200107 struct snd_rawmidi_runtime *runtime =
108 container_of(work, struct snd_rawmidi_runtime, event_work);
Takashi Iwai5bed9132018-07-17 22:32:52 +0200109
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200110 if (runtime->event)
111 runtime->event(runtime->substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Takashi Iwai48c9d412005-11-17 13:56:51 +0100114static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100116 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Takashi Iwai5bed9132018-07-17 22:32:52 +0200118 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
119 if (!runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return -ENOMEM;
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200121 runtime->substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 spin_lock_init(&runtime->lock);
123 init_waitqueue_head(&runtime->sleep);
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200124 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 runtime->event = NULL;
126 runtime->buffer_size = PAGE_SIZE;
127 runtime->avail_min = 1;
128 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
129 runtime->avail = 0;
130 else
131 runtime->avail = runtime->buffer_size;
Takashi Iwai5a7b44a2018-09-03 15:16:43 +0200132 runtime->buffer = kvzalloc(runtime->buffer_size, GFP_KERNEL);
Takashi Iwai5bed9132018-07-17 22:32:52 +0200133 if (!runtime->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 kfree(runtime);
135 return -ENOMEM;
136 }
137 runtime->appl_ptr = runtime->hw_ptr = 0;
138 substream->runtime = runtime;
139 return 0;
140}
141
Takashi Iwai48c9d412005-11-17 13:56:51 +0100142static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100144 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Takashi Iwaief4db232018-07-17 23:12:33 +0200146 kvfree(runtime->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 kfree(runtime);
148 substream->runtime = NULL;
149 return 0;
150}
151
Takashi Iwai5bed9132018-07-17 22:32:52 +0200152static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Takashi Iwai219df322008-11-03 08:17:05 +0100154 if (!substream->opened)
155 return;
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200156 substream->ops->trigger(substream, up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Takashi Iwai48c9d412005-11-17 13:56:51 +0100159static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Takashi Iwai219df322008-11-03 08:17:05 +0100161 if (!substream->opened)
162 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 substream->ops->trigger(substream, up);
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200164 if (!up)
165 cancel_work_sync(&substream->runtime->event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Takashi Iwaif5beb592018-07-17 23:07:29 +0200168static void __reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
169 bool is_input)
170{
171 runtime->drain = 0;
172 runtime->appl_ptr = runtime->hw_ptr = 0;
173 runtime->avail = is_input ? 0 : runtime->buffer_size;
174}
175
176static void reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
177 bool is_input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwaif5beb592018-07-17 23:07:29 +0200182 __reset_runtime_ptrs(runtime, is_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwaif5beb592018-07-17 23:07:29 +0200184}
185
186int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
187{
188 snd_rawmidi_output_trigger(substream, 0);
189 reset_runtime_ptrs(substream->runtime, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
191}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100192EXPORT_SYMBOL(snd_rawmidi_drop_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Takashi Iwai48c9d412005-11-17 13:56:51 +0100194int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 int err;
197 long timeout;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100198 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 err = 0;
201 runtime->drain = 1;
202 timeout = wait_event_interruptible_timeout(runtime->sleep,
203 (runtime->avail >= runtime->buffer_size),
204 10*HZ);
205 if (signal_pending(current))
206 err = -ERESTARTSYS;
207 if (runtime->avail < runtime->buffer_size && !timeout) {
Takashi Iwaica20d292014-02-04 18:21:39 +0100208 rmidi_warn(substream->rmidi,
209 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
210 (long)runtime->avail, (long)runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 err = -EIO;
212 }
213 runtime->drain = 0;
214 if (err != -ERESTARTSYS) {
215 /* we need wait a while to make sure that Tx FIFOs are empty */
216 if (substream->ops->drain)
217 substream->ops->drain(substream);
218 else
219 msleep(50);
220 snd_rawmidi_drop_output(substream);
221 }
222 return err;
223}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100224EXPORT_SYMBOL(snd_rawmidi_drain_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Takashi Iwai48c9d412005-11-17 13:56:51 +0100226int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 snd_rawmidi_input_trigger(substream, 0);
Takashi Iwaif5beb592018-07-17 23:07:29 +0200229 reset_runtime_ptrs(substream->runtime, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
231}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100232EXPORT_SYMBOL(snd_rawmidi_drain_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100234/* look for an available substream for the given stream direction;
235 * if a specific subdevice is given, try to assign it
236 */
237static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
238 int stream, int mode,
239 struct snd_rawmidi_substream **sub_ret)
240{
241 struct snd_rawmidi_substream *substream;
242 struct snd_rawmidi_str *s = &rmidi->streams[stream];
243 static unsigned int info_flags[2] = {
244 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
245 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
246 };
247
248 if (!(rmidi->info_flags & info_flags[stream]))
249 return -ENXIO;
250 if (subdevice >= 0 && subdevice >= s->substream_count)
251 return -ENODEV;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100252
253 list_for_each_entry(substream, &s->substreams, list) {
254 if (substream->opened) {
255 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
Clemens Ladisch16fb1092009-10-21 09:10:16 +0200256 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
257 !substream->append)
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100258 continue;
259 }
260 if (subdevice < 0 || subdevice == substream->number) {
261 *sub_ret = substream;
262 return 0;
263 }
264 }
265 return -EAGAIN;
266}
267
268/* open and do ref-counting for the given substream */
269static int open_substream(struct snd_rawmidi *rmidi,
270 struct snd_rawmidi_substream *substream,
271 int mode)
272{
273 int err;
274
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200275 if (substream->use_count == 0) {
276 err = snd_rawmidi_runtime_create(substream);
277 if (err < 0)
278 return err;
279 err = substream->ops->open(substream);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200280 if (err < 0) {
281 snd_rawmidi_runtime_free(substream);
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200282 return err;
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200283 }
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200284 substream->opened = 1;
Clemens Ladisch2d4b8422009-07-13 13:52:46 +0200285 substream->active_sensing = 0;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200286 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
287 substream->append = 1;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100288 substream->pid = get_pid(task_pid(current));
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200289 rmidi->streams[substream->stream].substream_opened++;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200290 }
291 substream->use_count++;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100292 return 0;
293}
294
295static void close_substream(struct snd_rawmidi *rmidi,
296 struct snd_rawmidi_substream *substream,
297 int cleanup);
298
299static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
300 struct snd_rawmidi_file *rfile)
301{
302 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
303 int err;
304
305 rfile->input = rfile->output = NULL;
306 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
307 err = assign_substream(rmidi, subdevice,
308 SNDRV_RAWMIDI_STREAM_INPUT,
309 mode, &sinput);
310 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200311 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100312 }
313 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
314 err = assign_substream(rmidi, subdevice,
315 SNDRV_RAWMIDI_STREAM_OUTPUT,
316 mode, &soutput);
317 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200318 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100319 }
320
321 if (sinput) {
322 err = open_substream(rmidi, sinput, mode);
323 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200324 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100325 }
326 if (soutput) {
327 err = open_substream(rmidi, soutput, mode);
328 if (err < 0) {
329 if (sinput)
330 close_substream(rmidi, sinput, 0);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200331 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100332 }
333 }
334
335 rfile->rmidi = rmidi;
336 rfile->input = sinput;
337 rfile->output = soutput;
338 return 0;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100339}
340
341/* called from sound/core/seq/seq_midi.c */
Clemens Ladischf87135f2005-11-20 14:06:59 +0100342int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200343 int mode, struct snd_rawmidi_file *rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100345 struct snd_rawmidi *rmidi;
Takashi Iwai7fdc9b02018-07-17 22:45:50 +0200346 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100348 if (snd_BUG_ON(!rfile))
349 return -EINVAL;
350
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100351 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100352 rmidi = snd_rawmidi_search(card, device);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +0200353 if (!rmidi)
354 err = -ENODEV;
355 else if (!try_module_get(rmidi->card->module))
356 err = -ENXIO;
Takashi Iwaif9d20282009-02-11 14:55:59 +0100357 mutex_unlock(&register_mutex);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +0200358 if (err < 0)
359 return err;
Takashi Iwaif9d20282009-02-11 14:55:59 +0100360
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100361 mutex_lock(&rmidi->open_mutex);
362 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
363 mutex_unlock(&rmidi->open_mutex);
364 if (err < 0)
365 module_put(rmidi->card->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return err;
367}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100368EXPORT_SYMBOL(snd_rawmidi_kernel_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370static int snd_rawmidi_open(struct inode *inode, struct file *file)
371{
372 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100373 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100374 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 unsigned short fflags;
376 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100377 struct snd_rawmidi *rmidi;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100378 struct snd_rawmidi_file *rawmidi_file = NULL;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200379 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Takashi Iwai5bed9132018-07-17 22:32:52 +0200381 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100382 return -EINVAL; /* invalid combination */
383
Takashi Iwai02f48652010-04-13 11:49:04 +0200384 err = nonseekable_open(inode, file);
385 if (err < 0)
386 return err;
387
Clemens Ladischf1902862005-10-24 17:05:03 +0200388 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100389 rmidi = snd_lookup_minor_data(iminor(inode),
390 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200392 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100393 rmidi = snd_lookup_oss_minor_data(iminor(inode),
394 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200396 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (rmidi == NULL)
400 return -ENODEV;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100401
Takashi Iwaia0830db2012-10-16 13:05:59 +0200402 if (!try_module_get(rmidi->card->module)) {
403 snd_card_unref(rmidi->card);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100404 return -ENXIO;
Takashi Iwaia0830db2012-10-16 13:05:59 +0200405 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100406
407 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 card = rmidi->card;
409 err = snd_card_file_add(card, file);
410 if (err < 0)
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100411 goto __error_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200413 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
416 if (rawmidi_file == NULL) {
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100417 err = -ENOMEM;
418 goto __error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 init_waitqueue_entry(&wait, current);
421 add_wait_queue(&rmidi->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 while (1) {
Takashi Iwai23c18d42014-02-19 14:30:29 +0100423 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100424 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (err >= 0)
426 break;
427 if (err == -EAGAIN) {
428 if (file->f_flags & O_NONBLOCK) {
429 err = -EBUSY;
430 break;
431 }
432 } else
433 break;
434 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100435 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100437 mutex_lock(&rmidi->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +0200438 if (rmidi->card->shutdown) {
439 err = -ENODEV;
440 break;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (signal_pending(current)) {
443 err = -ERESTARTSYS;
444 break;
445 }
446 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100447 remove_wait_queue(&rmidi->open_wait, &wait);
448 if (err < 0) {
449 kfree(rawmidi_file);
450 goto __error;
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452#ifdef CONFIG_SND_OSSEMUL
453 if (rawmidi_file->input && rawmidi_file->input->runtime)
454 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
455 if (rawmidi_file->output && rawmidi_file->output->runtime)
456 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
457#endif
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100458 file->private_data = rawmidi_file;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100459 mutex_unlock(&rmidi->open_mutex);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200460 snd_card_unref(rmidi->card);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100461 return 0;
462
463 __error:
464 snd_card_file_remove(card, file);
465 __error_card:
466 mutex_unlock(&rmidi->open_mutex);
467 module_put(rmidi->card->module);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200468 snd_card_unref(rmidi->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return err;
470}
471
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100472static void close_substream(struct snd_rawmidi *rmidi,
473 struct snd_rawmidi_substream *substream,
474 int cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100476 if (--substream->use_count)
477 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100479 if (cleanup) {
480 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
481 snd_rawmidi_input_trigger(substream, 0);
482 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (substream->active_sensing) {
484 unsigned char buf = 0xfe;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100485 /* sending single active sensing message
486 * to shut the device up
487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 snd_rawmidi_kernel_write(substream, &buf, 1);
489 }
490 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
491 snd_rawmidi_output_trigger(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100494 substream->ops->close(substream);
495 if (substream->runtime->private_free)
496 substream->runtime->private_free(substream);
497 snd_rawmidi_runtime_free(substream);
498 substream->opened = 0;
499 substream->append = 0;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100500 put_pid(substream->pid);
501 substream->pid = NULL;
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200502 rmidi->streams[substream->stream].substream_opened--;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100503}
504
505static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
506{
507 struct snd_rawmidi *rmidi;
508
509 rmidi = rfile->rmidi;
510 mutex_lock(&rmidi->open_mutex);
511 if (rfile->input) {
512 close_substream(rmidi, rfile->input, 1);
513 rfile->input = NULL;
514 }
515 if (rfile->output) {
516 close_substream(rmidi, rfile->output, 1);
517 rfile->output = NULL;
518 }
519 rfile->rmidi = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100520 mutex_unlock(&rmidi->open_mutex);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100521 wake_up(&rmidi->open_wait);
522}
523
524/* called from sound/core/seq/seq_midi.c */
525int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
526{
527 struct snd_rawmidi *rmidi;
528
529 if (snd_BUG_ON(!rfile))
530 return -ENXIO;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200531
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100532 rmidi = rfile->rmidi;
533 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 module_put(rmidi->card->module);
535 return 0;
536}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100537EXPORT_SYMBOL(snd_rawmidi_kernel_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539static int snd_rawmidi_release(struct inode *inode, struct file *file)
540{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100541 struct snd_rawmidi_file *rfile;
542 struct snd_rawmidi *rmidi;
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200543 struct module *module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 rfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 rmidi = rfile->rmidi;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100547 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 kfree(rfile);
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200549 module = rmidi->card->module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 snd_card_file_remove(rmidi->card, file);
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200551 module_put(module);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Adrian Bunk18612042005-11-23 13:14:50 +0100555static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
556 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100558 struct snd_rawmidi *rmidi;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (substream == NULL)
561 return -ENODEV;
562 rmidi = substream->rmidi;
563 memset(info, 0, sizeof(*info));
564 info->card = rmidi->card->number;
565 info->device = rmidi->device;
566 info->subdevice = substream->number;
567 info->stream = substream->stream;
568 info->flags = rmidi->info_flags;
569 strcpy(info->id, rmidi->id);
570 strcpy(info->name, rmidi->name);
571 strcpy(info->subname, substream->name);
572 info->subdevices_count = substream->pstr->substream_count;
573 info->subdevices_avail = (substream->pstr->substream_count -
574 substream->pstr->substream_opened);
575 return 0;
576}
577
Takashi Iwai48c9d412005-11-17 13:56:51 +0100578static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200579 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100581 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int err;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200583
584 err = snd_rawmidi_info(substream, &info);
585 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100587 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return -EFAULT;
589 return 0;
590}
591
Takashi Iwaic1cfd902017-12-14 16:44:12 +0100592static int __snd_rawmidi_info_select(struct snd_card *card,
593 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100595 struct snd_rawmidi *rmidi;
596 struct snd_rawmidi_str *pstr;
597 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100598
Clemens Ladischf87135f2005-11-20 14:06:59 +0100599 rmidi = snd_rawmidi_search(card, info->device);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100600 if (!rmidi)
601 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (info->stream < 0 || info->stream > 1)
603 return -EINVAL;
604 pstr = &rmidi->streams[info->stream];
605 if (pstr->substream_count == 0)
606 return -ENOENT;
607 if (info->subdevice >= pstr->substream_count)
608 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200609 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if ((unsigned int)substream->number == info->subdevice)
611 return snd_rawmidi_info(substream, info);
612 }
613 return -ENXIO;
614}
Takashi Iwaic1cfd902017-12-14 16:44:12 +0100615
616int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
617{
618 int ret;
619
620 mutex_lock(&register_mutex);
621 ret = __snd_rawmidi_info_select(card, info);
622 mutex_unlock(&register_mutex);
623 return ret;
624}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100625EXPORT_SYMBOL(snd_rawmidi_info_select);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Takashi Iwai48c9d412005-11-17 13:56:51 +0100627static int snd_rawmidi_info_select_user(struct snd_card *card,
628 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100631 struct snd_rawmidi_info info;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (get_user(info.device, &_info->device))
634 return -EFAULT;
635 if (get_user(info.stream, &_info->stream))
636 return -EFAULT;
637 if (get_user(info.subdevice, &_info->subdevice))
638 return -EFAULT;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200639 err = snd_rawmidi_info_select(card, &info);
640 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100642 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -EFAULT;
644 return 0;
645}
646
Takashi Iwaif5beb592018-07-17 23:07:29 +0200647static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime,
648 struct snd_rawmidi_params *params,
649 bool is_input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Takashi Iwai39675f72018-07-17 17:26:43 +0200651 char *newbuf, *oldbuf;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200652
Takashi Iwai5bed9132018-07-17 22:32:52 +0200653 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EINVAL;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200655 if (params->avail_min < 1 || params->avail_min > params->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (params->buffer_size != runtime->buffer_size) {
Takashi Iwai5a7b44a2018-09-03 15:16:43 +0200658 newbuf = kvzalloc(params->buffer_size, GFP_KERNEL);
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800659 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return -ENOMEM;
Takashi Iwai39675f72018-07-17 17:26:43 +0200661 spin_lock_irq(&runtime->lock);
662 oldbuf = runtime->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 runtime->buffer = newbuf;
664 runtime->buffer_size = params->buffer_size;
Takashi Iwaif5beb592018-07-17 23:07:29 +0200665 __reset_runtime_ptrs(runtime, is_input);
Takashi Iwai39675f72018-07-17 17:26:43 +0200666 spin_unlock_irq(&runtime->lock);
Takashi Iwaief4db232018-07-17 23:12:33 +0200667 kvfree(oldbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669 runtime->avail_min = params->avail_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 0;
671}
Takashi Iwaif5beb592018-07-17 23:07:29 +0200672
673int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
674 struct snd_rawmidi_params *params)
675{
676 if (substream->append && substream->use_count > 1)
677 return -EBUSY;
678 snd_rawmidi_drain_output(substream);
679 substream->active_sensing = !params->no_active_sensing;
680 return resize_runtime_buffer(substream->runtime, params, false);
681}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100682EXPORT_SYMBOL(snd_rawmidi_output_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Takashi Iwai48c9d412005-11-17 13:56:51 +0100684int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200685 struct snd_rawmidi_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 snd_rawmidi_drain_input(substream);
Takashi Iwaif5beb592018-07-17 23:07:29 +0200688 return resize_runtime_buffer(substream->runtime, params, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100690EXPORT_SYMBOL(snd_rawmidi_input_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Takashi Iwai48c9d412005-11-17 13:56:51 +0100692static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200693 struct snd_rawmidi_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100695 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 memset(status, 0, sizeof(*status));
698 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
699 spin_lock_irq(&runtime->lock);
700 status->avail = runtime->avail;
701 spin_unlock_irq(&runtime->lock);
702 return 0;
703}
704
Takashi Iwai48c9d412005-11-17 13:56:51 +0100705static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200706 struct snd_rawmidi_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100708 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 memset(status, 0, sizeof(*status));
711 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
712 spin_lock_irq(&runtime->lock);
713 status->avail = runtime->avail;
714 status->xruns = runtime->xruns;
715 runtime->xruns = 0;
716 spin_unlock_irq(&runtime->lock);
717 return 0;
718}
719
720static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
721{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100722 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 void __user *argp = (void __user *)arg;
724
725 rfile = file->private_data;
726 if (((cmd >> 8) & 0xff) != 'W')
727 return -ENOTTY;
728 switch (cmd) {
729 case SNDRV_RAWMIDI_IOCTL_PVERSION:
730 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
731 case SNDRV_RAWMIDI_IOCTL_INFO:
732 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100733 int stream;
734 struct snd_rawmidi_info __user *info = argp;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (get_user(stream, &info->stream))
737 return -EFAULT;
738 switch (stream) {
739 case SNDRV_RAWMIDI_STREAM_INPUT:
740 return snd_rawmidi_info_user(rfile->input, info);
741 case SNDRV_RAWMIDI_STREAM_OUTPUT:
742 return snd_rawmidi_info_user(rfile->output, info);
743 default:
744 return -EINVAL;
745 }
746 }
747 case SNDRV_RAWMIDI_IOCTL_PARAMS:
748 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100749 struct snd_rawmidi_params params;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200750
Takashi Iwai48c9d412005-11-17 13:56:51 +0100751 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return -EFAULT;
753 switch (params.stream) {
754 case SNDRV_RAWMIDI_STREAM_OUTPUT:
755 if (rfile->output == NULL)
756 return -EINVAL;
757 return snd_rawmidi_output_params(rfile->output, &params);
758 case SNDRV_RAWMIDI_STREAM_INPUT:
759 if (rfile->input == NULL)
760 return -EINVAL;
761 return snd_rawmidi_input_params(rfile->input, &params);
762 default:
763 return -EINVAL;
764 }
765 }
766 case SNDRV_RAWMIDI_IOCTL_STATUS:
767 {
768 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100769 struct snd_rawmidi_status status;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200770
Takashi Iwai48c9d412005-11-17 13:56:51 +0100771 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -EFAULT;
773 switch (status.stream) {
774 case SNDRV_RAWMIDI_STREAM_OUTPUT:
775 if (rfile->output == NULL)
776 return -EINVAL;
777 err = snd_rawmidi_output_status(rfile->output, &status);
778 break;
779 case SNDRV_RAWMIDI_STREAM_INPUT:
780 if (rfile->input == NULL)
781 return -EINVAL;
782 err = snd_rawmidi_input_status(rfile->input, &status);
783 break;
784 default:
785 return -EINVAL;
786 }
787 if (err < 0)
788 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100789 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return -EFAULT;
791 return 0;
792 }
793 case SNDRV_RAWMIDI_IOCTL_DROP:
794 {
795 int val;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (get_user(val, (int __user *) argp))
798 return -EFAULT;
799 switch (val) {
800 case SNDRV_RAWMIDI_STREAM_OUTPUT:
801 if (rfile->output == NULL)
802 return -EINVAL;
803 return snd_rawmidi_drop_output(rfile->output);
804 default:
805 return -EINVAL;
806 }
807 }
808 case SNDRV_RAWMIDI_IOCTL_DRAIN:
809 {
810 int val;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (get_user(val, (int __user *) argp))
813 return -EFAULT;
814 switch (val) {
815 case SNDRV_RAWMIDI_STREAM_OUTPUT:
816 if (rfile->output == NULL)
817 return -EINVAL;
818 return snd_rawmidi_drain_output(rfile->output);
819 case SNDRV_RAWMIDI_STREAM_INPUT:
820 if (rfile->input == NULL)
821 return -EINVAL;
822 return snd_rawmidi_drain_input(rfile->input);
823 default:
824 return -EINVAL;
825 }
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 default:
Takashi Iwaica20d292014-02-04 18:21:39 +0100828 rmidi_dbg(rfile->rmidi,
829 "rawmidi: unknown command = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 return -ENOTTY;
832}
833
Takashi Iwai48c9d412005-11-17 13:56:51 +0100834static int snd_rawmidi_control_ioctl(struct snd_card *card,
835 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 unsigned int cmd,
837 unsigned long arg)
838{
839 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 switch (cmd) {
842 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
843 {
844 int device;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (get_user(device, (int __user *)argp))
847 return -EFAULT;
Dan Carpentera7a13d02010-09-09 00:11:41 +0200848 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
849 device = SNDRV_RAWMIDI_DEVICES - 1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100850 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 device = device < 0 ? 0 : device + 1;
852 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100853 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 break;
855 device++;
856 }
857 if (device == SNDRV_RAWMIDI_DEVICES)
858 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100859 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (put_user(device, (int __user *)argp))
861 return -EFAULT;
862 return 0;
863 }
864 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
865 {
866 int val;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (get_user(val, (int __user *)argp))
869 return -EFAULT;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100870 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 0;
872 }
873 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
874 return snd_rawmidi_info_select_user(card, argp);
875 }
876 return -ENOIOCTLCMD;
877}
878
879/**
880 * snd_rawmidi_receive - receive the input data from the device
881 * @substream: the rawmidi substream
882 * @buffer: the buffer pointer
883 * @count: the data size to read
884 *
885 * Reads the data from the internal buffer.
886 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100887 * Return: The size of read data, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100889int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
890 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 unsigned long flags;
893 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100894 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Takashi Iwai219df322008-11-03 08:17:05 +0100896 if (!substream->opened)
897 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +0100899 rmidi_dbg(substream->rmidi,
900 "snd_rawmidi_receive: input is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -EINVAL;
902 }
903 spin_lock_irqsave(&runtime->lock, flags);
904 if (count == 1) { /* special case, faster code */
905 substream->bytes++;
906 if (runtime->avail < runtime->buffer_size) {
907 runtime->buffer[runtime->hw_ptr++] = buffer[0];
908 runtime->hw_ptr %= runtime->buffer_size;
909 runtime->avail++;
910 result++;
911 } else {
912 runtime->xruns++;
913 }
914 } else {
915 substream->bytes += count;
916 count1 = runtime->buffer_size - runtime->hw_ptr;
917 if (count1 > count)
918 count1 = count;
919 if (count1 > (int)(runtime->buffer_size - runtime->avail))
920 count1 = runtime->buffer_size - runtime->avail;
921 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
922 runtime->hw_ptr += count1;
923 runtime->hw_ptr %= runtime->buffer_size;
924 runtime->avail += count1;
925 count -= count1;
926 result += count1;
927 if (count > 0) {
928 buffer += count1;
929 count1 = count;
930 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
931 count1 = runtime->buffer_size - runtime->avail;
932 runtime->xruns += count - count1;
933 }
934 if (count1 > 0) {
935 memcpy(runtime->buffer, buffer, count1);
936 runtime->hw_ptr = count1;
937 runtime->avail += count1;
938 result += count1;
939 }
940 }
941 }
942 if (result > 0) {
943 if (runtime->event)
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200944 schedule_work(&runtime->event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 else if (snd_rawmidi_ready(substream))
946 wake_up(&runtime->sleep);
947 }
948 spin_unlock_irqrestore(&runtime->lock, flags);
949 return result;
950}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100951EXPORT_SYMBOL(snd_rawmidi_receive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Takashi Iwai48c9d412005-11-17 13:56:51 +0100953static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100954 unsigned char __user *userbuf,
955 unsigned char *kernelbuf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 unsigned long flags;
958 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100959 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai81f57752016-02-03 14:41:22 +0100960 unsigned long appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Takashi Iwai81f57752016-02-03 14:41:22 +0100962 spin_lock_irqsave(&runtime->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 while (count > 0 && runtime->avail) {
964 count1 = runtime->buffer_size - runtime->appl_ptr;
965 if (count1 > count)
966 count1 = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (count1 > (int)runtime->avail)
968 count1 = runtime->avail;
Takashi Iwai81f57752016-02-03 14:41:22 +0100969
970 /* update runtime->appl_ptr before unlocking for userbuf */
971 appl_ptr = runtime->appl_ptr;
972 runtime->appl_ptr += count1;
973 runtime->appl_ptr %= runtime->buffer_size;
974 runtime->avail -= count1;
975
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100976 if (kernelbuf)
Takashi Iwai81f57752016-02-03 14:41:22 +0100977 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100978 if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 spin_unlock_irqrestore(&runtime->lock, flags);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100980 if (copy_to_user(userbuf + result,
Takashi Iwai81f57752016-02-03 14:41:22 +0100981 runtime->buffer + appl_ptr, count1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return result > 0 ? result : -EFAULT;
983 }
984 spin_lock_irqsave(&runtime->lock, flags);
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 result += count1;
987 count -= count1;
988 }
Takashi Iwai81f57752016-02-03 14:41:22 +0100989 spin_unlock_irqrestore(&runtime->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return result;
991}
992
Takashi Iwai48c9d412005-11-17 13:56:51 +0100993long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
994 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 snd_rawmidi_input_trigger(substream, 1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100997 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100999EXPORT_SYMBOL(snd_rawmidi_kernel_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Takashi Iwai48c9d412005-11-17 13:56:51 +01001001static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1002 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 long result;
1005 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001006 struct snd_rawmidi_file *rfile;
1007 struct snd_rawmidi_substream *substream;
1008 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 rfile = file->private_data;
1011 substream = rfile->input;
1012 if (substream == NULL)
1013 return -EIO;
1014 runtime = substream->runtime;
1015 snd_rawmidi_input_trigger(substream, 1);
1016 result = 0;
1017 while (count > 0) {
1018 spin_lock_irq(&runtime->lock);
1019 while (!snd_rawmidi_ready(substream)) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02001020 wait_queue_entry_t wait;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1023 spin_unlock_irq(&runtime->lock);
1024 return result > 0 ? result : -EAGAIN;
1025 }
1026 init_waitqueue_entry(&wait, current);
1027 add_wait_queue(&runtime->sleep, &wait);
1028 set_current_state(TASK_INTERRUPTIBLE);
1029 spin_unlock_irq(&runtime->lock);
1030 schedule();
1031 remove_wait_queue(&runtime->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001032 if (rfile->rmidi->card->shutdown)
1033 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (signal_pending(current))
1035 return result > 0 ? result : -ERESTARTSYS;
1036 if (!runtime->avail)
1037 return result > 0 ? result : -EIO;
1038 spin_lock_irq(&runtime->lock);
1039 }
1040 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001041 count1 = snd_rawmidi_kernel_read1(substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001042 (unsigned char __user *)buf,
1043 NULL/*kernelbuf*/,
1044 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (count1 < 0)
1046 return result > 0 ? result : count1;
1047 result += count1;
1048 buf += count1;
1049 count -= count1;
1050 }
1051 return result;
1052}
1053
1054/**
1055 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1056 * @substream: the rawmidi substream
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001057 *
1058 * Return: 1 if the internal output buffer is empty, 0 if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001060int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001062 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 int result;
1064 unsigned long flags;
1065
1066 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001067 rmidi_dbg(substream->rmidi,
1068 "snd_rawmidi_transmit_empty: output is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return 1;
1070 }
1071 spin_lock_irqsave(&runtime->lock, flags);
1072 result = runtime->avail >= runtime->buffer_size;
1073 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwai5bed9132018-07-17 22:32:52 +02001074 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001076EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078/**
Takashi Iwai06ab3002016-01-31 11:57:41 +01001079 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 * @substream: the rawmidi substream
1081 * @buffer: the buffer pointer
1082 * @count: data size to transfer
1083 *
Takashi Iwai06ab3002016-01-31 11:57:41 +01001084 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
Takashi Iwai06ab3002016-01-31 11:57:41 +01001086int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001087 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001090 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001093 rmidi_dbg(substream->rmidi,
1094 "snd_rawmidi_transmit_peek: output is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return -EINVAL;
1096 }
1097 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (runtime->avail >= runtime->buffer_size) {
1099 /* warning: lowlevel layer MUST trigger down the hardware */
1100 goto __skip;
1101 }
1102 if (count == 1) { /* special case, faster code */
1103 *buffer = runtime->buffer[runtime->hw_ptr];
1104 result++;
1105 } else {
1106 count1 = runtime->buffer_size - runtime->hw_ptr;
1107 if (count1 > count)
1108 count1 = count;
1109 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1110 count1 = runtime->buffer_size - runtime->avail;
1111 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1112 count -= count1;
1113 result += count1;
1114 if (count > 0) {
1115 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1116 count = runtime->buffer_size - runtime->avail - count1;
1117 memcpy(buffer + count1, runtime->buffer, count);
1118 result += count;
1119 }
1120 }
1121 __skip:
Takashi Iwai06ab3002016-01-31 11:57:41 +01001122 return result;
1123}
1124EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1125
1126/**
1127 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1128 * @substream: the rawmidi substream
1129 * @buffer: the buffer pointer
1130 * @count: data size to transfer
1131 *
1132 * Copies data from the internal output buffer to the given buffer.
1133 *
1134 * Call this in the interrupt handler when the midi output is ready,
1135 * and call snd_rawmidi_transmit_ack() after the transmission is
1136 * finished.
1137 *
1138 * Return: The size of copied data, or a negative error code on failure.
1139 */
1140int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1141 unsigned char *buffer, int count)
1142{
1143 struct snd_rawmidi_runtime *runtime = substream->runtime;
1144 int result;
1145 unsigned long flags;
1146
1147 spin_lock_irqsave(&runtime->lock, flags);
1148 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 spin_unlock_irqrestore(&runtime->lock, flags);
1150 return result;
1151}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001152EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154/**
Takashi Iwai06ab3002016-01-31 11:57:41 +01001155 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1156 * @substream: the rawmidi substream
1157 * @count: the transferred count
1158 *
1159 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1160 */
1161int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1162{
1163 struct snd_rawmidi_runtime *runtime = substream->runtime;
1164
1165 if (runtime->buffer == NULL) {
1166 rmidi_dbg(substream->rmidi,
1167 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1168 return -EINVAL;
1169 }
1170 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1171 runtime->hw_ptr += count;
1172 runtime->hw_ptr %= runtime->buffer_size;
1173 runtime->avail += count;
1174 substream->bytes += count;
1175 if (count > 0) {
1176 if (runtime->drain || snd_rawmidi_ready(substream))
1177 wake_up(&runtime->sleep);
1178 }
1179 return count;
1180}
1181EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1182
1183/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 * snd_rawmidi_transmit_ack - acknowledge the transmission
1185 * @substream: the rawmidi substream
Masanari Iida0a114582014-02-09 00:47:36 +09001186 * @count: the transferred count
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 *
1188 * Advances the hardware pointer for the internal output buffer with
1189 * the given size and updates the condition.
1190 * Call after the transmission is finished.
1191 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001192 * Return: The advanced size if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001194int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001196 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai06ab3002016-01-31 11:57:41 +01001197 int result;
1198 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai06ab3002016-01-31 11:57:41 +01001201 result = __snd_rawmidi_transmit_ack(substream, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwai06ab3002016-01-31 11:57:41 +01001203 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001205EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207/**
1208 * snd_rawmidi_transmit - copy from the buffer to the device
1209 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001210 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 * @count: the data size to transfer
Takashi Iwai5bed9132018-07-17 22:32:52 +02001212 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 * Copies data from the buffer to the device and advances the pointer.
1214 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001215 * Return: The copied size if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001217int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1218 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Takashi Iwai06ab3002016-01-31 11:57:41 +01001220 struct snd_rawmidi_runtime *runtime = substream->runtime;
1221 int result;
1222 unsigned long flags;
1223
1224 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai219df322008-11-03 08:17:05 +01001225 if (!substream->opened)
Takashi Iwai06ab3002016-01-31 11:57:41 +01001226 result = -EBADFD;
1227 else {
1228 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1229 if (count <= 0)
1230 result = count;
1231 else
1232 result = __snd_rawmidi_transmit_ack(substream, count);
1233 }
1234 spin_unlock_irqrestore(&runtime->lock, flags);
1235 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001237EXPORT_SYMBOL(snd_rawmidi_transmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Takashi Iwai6aea5702018-09-13 08:20:43 +02001239/**
1240 * snd_rawmidi_proceed - Discard the all pending bytes and proceed
1241 * @substream: rawmidi substream
1242 *
1243 * Return: the number of discarded bytes
1244 */
1245int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream)
1246{
1247 struct snd_rawmidi_runtime *runtime = substream->runtime;
1248 unsigned long flags;
1249 int count = 0;
1250
1251 spin_lock_irqsave(&runtime->lock, flags);
1252 if (runtime->avail < runtime->buffer_size) {
1253 count = runtime->buffer_size - runtime->avail;
1254 __snd_rawmidi_transmit_ack(substream, count);
1255 }
1256 spin_unlock_irqrestore(&runtime->lock, flags);
1257 return count;
1258}
1259EXPORT_SYMBOL(snd_rawmidi_proceed);
1260
Takashi Iwai48c9d412005-11-17 13:56:51 +01001261static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001262 const unsigned char __user *userbuf,
1263 const unsigned char *kernelbuf,
1264 long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 unsigned long flags;
1267 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001268 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai81f57752016-02-03 14:41:22 +01001269 unsigned long appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Takashi Iwaicc85f7a2016-02-01 12:04:55 +01001271 if (!kernelbuf && !userbuf)
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001272 return -EINVAL;
1273 if (snd_BUG_ON(!runtime->buffer))
1274 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 result = 0;
1277 spin_lock_irqsave(&runtime->lock, flags);
1278 if (substream->append) {
1279 if ((long)runtime->avail < count) {
1280 spin_unlock_irqrestore(&runtime->lock, flags);
1281 return -EAGAIN;
1282 }
1283 }
1284 while (count > 0 && runtime->avail > 0) {
1285 count1 = runtime->buffer_size - runtime->appl_ptr;
1286 if (count1 > count)
1287 count1 = count;
1288 if (count1 > (long)runtime->avail)
1289 count1 = runtime->avail;
Takashi Iwai81f57752016-02-03 14:41:22 +01001290
1291 /* update runtime->appl_ptr before unlocking for userbuf */
1292 appl_ptr = runtime->appl_ptr;
1293 runtime->appl_ptr += count1;
1294 runtime->appl_ptr %= runtime->buffer_size;
1295 runtime->avail -= count1;
1296
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001297 if (kernelbuf)
Takashi Iwai81f57752016-02-03 14:41:22 +01001298 memcpy(runtime->buffer + appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001299 kernelbuf + result, count1);
1300 else if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwai81f57752016-02-03 14:41:22 +01001302 if (copy_from_user(runtime->buffer + appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001303 userbuf + result, count1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 spin_lock_irqsave(&runtime->lock, flags);
1305 result = result > 0 ? result : -EFAULT;
1306 goto __end;
1307 }
1308 spin_lock_irqsave(&runtime->lock, flags);
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 result += count1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 count -= count1;
1312 }
1313 __end:
1314 count1 = runtime->avail < runtime->buffer_size;
1315 spin_unlock_irqrestore(&runtime->lock, flags);
1316 if (count1)
1317 snd_rawmidi_output_trigger(substream, 1);
1318 return result;
1319}
1320
Takashi Iwai48c9d412005-11-17 13:56:51 +01001321long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1322 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001324 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001326EXPORT_SYMBOL(snd_rawmidi_kernel_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Takashi Iwai48c9d412005-11-17 13:56:51 +01001328static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1329 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 long result, timeout;
1332 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001333 struct snd_rawmidi_file *rfile;
1334 struct snd_rawmidi_runtime *runtime;
1335 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 rfile = file->private_data;
1338 substream = rfile->output;
1339 runtime = substream->runtime;
1340 /* we cannot put an atomic message to our buffer */
1341 if (substream->append && count > runtime->buffer_size)
1342 return -EIO;
1343 result = 0;
1344 while (count > 0) {
1345 spin_lock_irq(&runtime->lock);
1346 while (!snd_rawmidi_ready_append(substream, count)) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02001347 wait_queue_entry_t wait;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (file->f_flags & O_NONBLOCK) {
1350 spin_unlock_irq(&runtime->lock);
1351 return result > 0 ? result : -EAGAIN;
1352 }
1353 init_waitqueue_entry(&wait, current);
1354 add_wait_queue(&runtime->sleep, &wait);
1355 set_current_state(TASK_INTERRUPTIBLE);
1356 spin_unlock_irq(&runtime->lock);
1357 timeout = schedule_timeout(30 * HZ);
1358 remove_wait_queue(&runtime->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001359 if (rfile->rmidi->card->shutdown)
1360 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (signal_pending(current))
1362 return result > 0 ? result : -ERESTARTSYS;
1363 if (!runtime->avail && !timeout)
1364 return result > 0 ? result : -EIO;
1365 spin_lock_irq(&runtime->lock);
1366 }
1367 spin_unlock_irq(&runtime->lock);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001368 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (count1 < 0)
1370 return result > 0 ? result : count1;
1371 result += count1;
1372 buf += count1;
1373 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1374 break;
1375 count -= count1;
1376 }
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001377 if (file->f_flags & O_DSYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 spin_lock_irq(&runtime->lock);
1379 while (runtime->avail != runtime->buffer_size) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02001380 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 unsigned int last_avail = runtime->avail;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 init_waitqueue_entry(&wait, current);
1384 add_wait_queue(&runtime->sleep, &wait);
1385 set_current_state(TASK_INTERRUPTIBLE);
1386 spin_unlock_irq(&runtime->lock);
1387 timeout = schedule_timeout(30 * HZ);
1388 remove_wait_queue(&runtime->sleep, &wait);
1389 if (signal_pending(current))
1390 return result > 0 ? result : -ERESTARTSYS;
1391 if (runtime->avail == last_avail && !timeout)
1392 return result > 0 ? result : -EIO;
1393 spin_lock_irq(&runtime->lock);
1394 }
1395 spin_unlock_irq(&runtime->lock);
1396 }
1397 return result;
1398}
1399
Takashi Iwai5bed9132018-07-17 22:32:52 +02001400static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001402 struct snd_rawmidi_file *rfile;
1403 struct snd_rawmidi_runtime *runtime;
Al Viro680ef722017-07-02 23:27:36 -04001404 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 rfile = file->private_data;
1407 if (rfile->input != NULL) {
1408 runtime = rfile->input->runtime;
1409 snd_rawmidi_input_trigger(rfile->input, 1);
1410 poll_wait(file, &runtime->sleep, wait);
1411 }
1412 if (rfile->output != NULL) {
1413 runtime = rfile->output->runtime;
1414 poll_wait(file, &runtime->sleep, wait);
1415 }
1416 mask = 0;
1417 if (rfile->input != NULL) {
1418 if (snd_rawmidi_ready(rfile->input))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001419 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421 if (rfile->output != NULL) {
1422 if (snd_rawmidi_ready(rfile->output))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001423 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425 return mask;
1426}
1427
1428/*
1429 */
1430#ifdef CONFIG_COMPAT
1431#include "rawmidi_compat.c"
1432#else
1433#define snd_rawmidi_ioctl_compat NULL
1434#endif
1435
1436/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 */
1438
Takashi Iwai48c9d412005-11-17 13:56:51 +01001439static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1440 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001442 struct snd_rawmidi *rmidi;
1443 struct snd_rawmidi_substream *substream;
1444 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 rmidi = entry->private_data;
1447 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001448 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001450 list_for_each_entry(substream,
1451 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1452 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 snd_iprintf(buffer,
1454 "Output %d\n"
1455 " Tx bytes : %lu\n",
1456 substream->number,
1457 (unsigned long) substream->bytes);
1458 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001459 snd_iprintf(buffer,
1460 " Owner PID : %d\n",
1461 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 runtime = substream->runtime;
1463 snd_iprintf(buffer,
1464 " Mode : %s\n"
1465 " Buffer size : %lu\n"
1466 " Avail : %lu\n",
1467 runtime->oss ? "OSS compatible" : "native",
1468 (unsigned long) runtime->buffer_size,
1469 (unsigned long) runtime->avail);
1470 }
1471 }
1472 }
1473 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001474 list_for_each_entry(substream,
1475 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1476 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 snd_iprintf(buffer,
1478 "Input %d\n"
1479 " Rx bytes : %lu\n",
1480 substream->number,
1481 (unsigned long) substream->bytes);
1482 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001483 snd_iprintf(buffer,
1484 " Owner PID : %d\n",
1485 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 runtime = substream->runtime;
1487 snd_iprintf(buffer,
1488 " Buffer size : %lu\n"
1489 " Avail : %lu\n"
1490 " Overruns : %lu\n",
1491 (unsigned long) runtime->buffer_size,
1492 (unsigned long) runtime->avail,
1493 (unsigned long) runtime->xruns);
1494 }
1495 }
1496 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001497 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500/*
1501 * Register functions
1502 */
1503
Takashi Iwai5bed9132018-07-17 22:32:52 +02001504static const struct file_operations snd_rawmidi_f_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 .owner = THIS_MODULE,
1506 .read = snd_rawmidi_read,
1507 .write = snd_rawmidi_write,
1508 .open = snd_rawmidi_open,
1509 .release = snd_rawmidi_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001510 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 .poll = snd_rawmidi_poll,
1512 .unlocked_ioctl = snd_rawmidi_ioctl,
1513 .compat_ioctl = snd_rawmidi_ioctl_compat,
1514};
1515
Takashi Iwai48c9d412005-11-17 13:56:51 +01001516static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1517 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 int direction,
1519 int count)
1520{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001521 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 int idx;
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001525 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +01001526 if (!substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return -ENOMEM;
1528 substream->stream = direction;
1529 substream->number = idx;
1530 substream->rmidi = rmidi;
1531 substream->pstr = stream;
1532 list_add_tail(&substream->list, &stream->substreams);
1533 stream->substream_count++;
1534 }
1535 return 0;
1536}
1537
Takashi Iwaiaee50122015-01-29 17:55:52 +01001538static void release_rawmidi_device(struct device *dev)
1539{
1540 kfree(container_of(dev, struct snd_rawmidi, dev));
1541}
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543/**
1544 * snd_rawmidi_new - create a rawmidi instance
1545 * @card: the card instance
1546 * @id: the id string
1547 * @device: the device index
1548 * @output_count: the number of output streams
1549 * @input_count: the number of input streams
1550 * @rrawmidi: the pointer to store the new rawmidi instance
1551 *
1552 * Creates a new rawmidi instance.
1553 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1554 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001555 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001557int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int output_count, int input_count,
Takashi Iwai5bed9132018-07-17 22:32:52 +02001559 struct snd_rawmidi **rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001561 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001563 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 .dev_free = snd_rawmidi_dev_free,
1565 .dev_register = snd_rawmidi_dev_register,
1566 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 };
1568
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001569 if (snd_BUG_ON(!card))
1570 return -ENXIO;
1571 if (rrawmidi)
1572 *rrawmidi = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001573 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +01001574 if (!rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return -ENOMEM;
1576 rmidi->card = card;
1577 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001578 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001580 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1581 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (id != NULL)
1584 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwaiaee50122015-01-29 17:55:52 +01001585
1586 snd_device_initialize(&rmidi->dev, card);
1587 rmidi->dev.release = release_rawmidi_device;
1588 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1589
Takashi Iwai5bed9132018-07-17 22:32:52 +02001590 err = snd_rawmidi_alloc_substreams(rmidi,
1591 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1592 SNDRV_RAWMIDI_STREAM_INPUT,
1593 input_count);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001594 if (err < 0)
1595 goto error;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001596 err = snd_rawmidi_alloc_substreams(rmidi,
1597 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1598 SNDRV_RAWMIDI_STREAM_OUTPUT,
1599 output_count);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001600 if (err < 0)
1601 goto error;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001602 err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001603 if (err < 0)
1604 goto error;
1605
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001606 if (rrawmidi)
1607 *rrawmidi = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return 0;
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001609
1610 error:
1611 snd_rawmidi_free(rmidi);
1612 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001614EXPORT_SYMBOL(snd_rawmidi_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Takashi Iwai48c9d412005-11-17 13:56:51 +01001616static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001618 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001621 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 list_del(&substream->list);
1623 kfree(substream);
1624 }
1625}
1626
Takashi Iwai48c9d412005-11-17 13:56:51 +01001627static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001629 if (!rmidi)
1630 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +02001631
1632 snd_info_free_entry(rmidi->proc_entry);
1633 rmidi->proc_entry = NULL;
1634 mutex_lock(&register_mutex);
1635 if (rmidi->ops && rmidi->ops->dev_unregister)
1636 rmidi->ops->dev_unregister(rmidi);
1637 mutex_unlock(&register_mutex);
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1640 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1641 if (rmidi->private_free)
1642 rmidi->private_free(rmidi);
Takashi Iwaiaee50122015-01-29 17:55:52 +01001643 put_device(&rmidi->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
1645}
1646
Takashi Iwai48c9d412005-11-17 13:56:51 +01001647static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001649 struct snd_rawmidi *rmidi = device->device_data;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return snd_rawmidi_free(rmidi);
1652}
1653
Takashi Iwai111b0cd2017-06-09 15:11:58 +02001654#if IS_ENABLED(CONFIG_SND_SEQUENCER)
Takashi Iwai48c9d412005-11-17 13:56:51 +01001655static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001657 struct snd_rawmidi *rmidi = device->private_data;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 rmidi->seq_dev = NULL;
1660}
1661#endif
1662
Takashi Iwai48c9d412005-11-17 13:56:51 +01001663static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001665 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001666 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001668 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1671 return -ENOMEM;
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001672 err = 0;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001673 mutex_lock(&register_mutex);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001674 if (snd_rawmidi_search(rmidi->card, rmidi->device))
1675 err = -EBUSY;
1676 else
1677 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Takashi Iwai816f3182016-08-30 14:45:46 +02001678 mutex_unlock(&register_mutex);
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001679 if (err < 0)
1680 return err;
1681
Takashi Iwai40a4b262015-01-30 08:34:58 +01001682 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1683 rmidi->card, rmidi->device,
1684 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
Takashi Iwaiaee50122015-01-29 17:55:52 +01001685 if (err < 0) {
1686 rmidi_err(rmidi, "unable to register\n");
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001687 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001689 if (rmidi->ops && rmidi->ops->dev_register) {
1690 err = rmidi->ops->dev_register(rmidi);
1691 if (err < 0)
1692 goto error_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694#ifdef CONFIG_SND_OSSEMUL
1695 rmidi->ossreg = 0;
1696 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1697 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001698 rmidi->card, 0, &snd_rawmidi_f_ops,
Takashi Iwai80d7d772014-02-04 13:51:45 +01001699 rmidi) < 0) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001700 rmidi_err(rmidi,
1701 "unable to register OSS rawmidi device %i:%i\n",
1702 rmidi->card->number, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 } else {
1704 rmidi->ossreg++;
1705#ifdef SNDRV_OSS_INFO_DEV_MIDI
1706 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1707#endif
1708 }
1709 }
1710 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1711 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001712 rmidi->card, 1, &snd_rawmidi_f_ops,
Takashi Iwai80d7d772014-02-04 13:51:45 +01001713 rmidi) < 0) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001714 rmidi_err(rmidi,
1715 "unable to register OSS rawmidi device %i:%i\n",
1716 rmidi->card->number, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 } else {
1718 rmidi->ossreg++;
1719 }
1720 }
1721#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 sprintf(name, "midi%d", rmidi->device);
1723 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1724 if (entry) {
1725 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 entry->c.text.read = snd_rawmidi_proc_info_read;
1727 if (snd_info_register(entry) < 0) {
1728 snd_info_free_entry(entry);
1729 entry = NULL;
1730 }
1731 }
1732 rmidi->proc_entry = entry;
Takashi Iwai111b0cd2017-06-09 15:11:58 +02001733#if IS_ENABLED(CONFIG_SND_SEQUENCER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1735 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1736 rmidi->seq_dev->private_data = rmidi;
1737 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1738 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1739 snd_device_register(rmidi->card, rmidi->seq_dev);
1740 }
1741 }
1742#endif
1743 return 0;
Takashi Iwai7fdc9b02018-07-17 22:45:50 +02001744
1745 error_unregister:
1746 snd_unregister_device(&rmidi->dev);
1747 error:
1748 mutex_lock(&register_mutex);
1749 list_del(&rmidi->list);
1750 mutex_unlock(&register_mutex);
1751 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Takashi Iwai48c9d412005-11-17 13:56:51 +01001754static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001756 struct snd_rawmidi *rmidi = device->device_data;
Takashi Iwai0914f792012-10-16 16:43:39 +02001757 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001759 mutex_lock(&register_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02001760 mutex_lock(&rmidi->open_mutex);
1761 wake_up(&rmidi->open_wait);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001762 list_del_init(&rmidi->list);
Takashi Iwai0914f792012-10-16 16:43:39 +02001763 for (dir = 0; dir < 2; dir++) {
1764 struct snd_rawmidi_substream *s;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001765
Takashi Iwai0914f792012-10-16 16:43:39 +02001766 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1767 if (s->runtime)
1768 wake_up(&s->runtime->sleep);
1769 }
1770 }
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772#ifdef CONFIG_SND_OSSEMUL
1773 if (rmidi->ossreg) {
1774 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1775 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1776#ifdef SNDRV_OSS_INFO_DEV_MIDI
1777 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1778#endif
1779 }
1780 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1781 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1782 rmidi->ossreg = 0;
1783 }
1784#endif /* CONFIG_SND_OSSEMUL */
Takashi Iwai40a4b262015-01-30 08:34:58 +01001785 snd_unregister_device(&rmidi->dev);
Takashi Iwai0914f792012-10-16 16:43:39 +02001786 mutex_unlock(&rmidi->open_mutex);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001787 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
1791/**
1792 * snd_rawmidi_set_ops - set the rawmidi operators
1793 * @rmidi: the rawmidi instance
1794 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1795 * @ops: the operator table
1796 *
1797 * Sets the rawmidi operators for the given stream direction.
1798 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001799void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
Takashi Iwai6ba79b82017-01-05 17:01:14 +01001800 const struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001802 struct snd_rawmidi_substream *substream;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001803
Johannes Berg9244b2c2006-10-05 16:02:22 +02001804 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001807EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809/*
1810 * ENTRY functions
1811 */
1812
1813static int __init alsa_rawmidi_init(void)
1814{
1815
1816 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1817 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1818#ifdef CONFIG_SND_OSSEMUL
1819 { int i;
1820 /* check device map table */
1821 for (i = 0; i < SNDRV_CARDS; i++) {
1822 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001823 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1824 i, midi_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 midi_map[i] = 0;
1826 }
1827 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001828 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1829 i, amidi_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 amidi_map[i] = 1;
1831 }
1832 }
1833 }
1834#endif /* CONFIG_SND_OSSEMUL */
1835 return 0;
1836}
1837
1838static void __exit alsa_rawmidi_exit(void)
1839{
1840 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1841 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1842}
1843
1844module_init(alsa_rawmidi_init)
1845module_exit(alsa_rawmidi_exit)