blob: 14dec13ecde9567b395ec7aaa90e41912547e062 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/rawmidi.h>
33#include <sound/info.h>
34#include <sound/control.h>
35#include <sound/minors.h>
36#include <sound/initval.h>
37
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020038MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
40MODULE_LICENSE("GPL");
41
42#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai6581f4e2006-05-17 17:14:51 +020043static int midi_map[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
45module_param_array(midi_map, int, NULL, 0444);
46MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
47module_param_array(amidi_map, int, NULL, 0444);
48MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
49#endif /* CONFIG_SND_OSSEMUL */
50
Takashi Iwai48c9d412005-11-17 13:56:51 +010051static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
52static int snd_rawmidi_dev_free(struct snd_device *device);
53static int snd_rawmidi_dev_register(struct snd_device *device);
54static int snd_rawmidi_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Clemens Ladischf87135f2005-11-20 14:06:59 +010056static LIST_HEAD(snd_rawmidi_devices);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010057static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Takashi Iwaica20d292014-02-04 18:21:39 +010059#define rmidi_err(rmidi, fmt, args...) \
Takashi Iwai07cc3e82015-01-30 15:31:52 +010060 dev_err(&(rmidi)->dev, fmt, ##args)
Takashi Iwaica20d292014-02-04 18:21:39 +010061#define rmidi_warn(rmidi, fmt, args...) \
Takashi Iwai07cc3e82015-01-30 15:31:52 +010062 dev_warn(&(rmidi)->dev, fmt, ##args)
Takashi Iwaica20d292014-02-04 18:21:39 +010063#define rmidi_dbg(rmidi, fmt, args...) \
Takashi Iwai07cc3e82015-01-30 15:31:52 +010064 dev_dbg(&(rmidi)->dev, fmt, ##args)
Takashi Iwaica20d292014-02-04 18:21:39 +010065
Clemens Ladischf87135f2005-11-20 14:06:59 +010066static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
67{
Clemens Ladischf87135f2005-11-20 14:06:59 +010068 struct snd_rawmidi *rawmidi;
69
Johannes Berg9244b2c2006-10-05 16:02:22 +020070 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
Clemens Ladischf87135f2005-11-20 14:06:59 +010071 if (rawmidi->card == card && rawmidi->device == device)
72 return rawmidi;
Clemens Ladischf87135f2005-11-20 14:06:59 +010073 return NULL;
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static inline unsigned short snd_rawmidi_file_flags(struct file *file)
77{
78 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
79 case FMODE_WRITE:
80 return SNDRV_RAWMIDI_LFLG_OUTPUT;
81 case FMODE_READ:
82 return SNDRV_RAWMIDI_LFLG_INPUT;
83 default:
84 return SNDRV_RAWMIDI_LFLG_OPEN;
85 }
86}
87
Takashi Iwai48c9d412005-11-17 13:56:51 +010088static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Takashi Iwai48c9d412005-11-17 13:56:51 +010090 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai5bed9132018-07-17 22:32:52 +020091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return runtime->avail >= runtime->avail_min;
93}
94
Takashi Iwai48c9d412005-11-17 13:56:51 +010095static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
96 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Takashi Iwai48c9d412005-11-17 13:56:51 +010098 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai5bed9132018-07-17 22:32:52 +020099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return runtime->avail >= runtime->avail_min &&
101 (!substream->append || runtime->avail >= count);
102}
103
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200104static void snd_rawmidi_input_event_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200106 struct snd_rawmidi_runtime *runtime =
107 container_of(work, struct snd_rawmidi_runtime, event_work);
Takashi Iwai5bed9132018-07-17 22:32:52 +0200108
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200109 if (runtime->event)
110 runtime->event(runtime->substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Takashi Iwai48c9d412005-11-17 13:56:51 +0100113static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100115 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Takashi Iwai5bed9132018-07-17 22:32:52 +0200117 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
118 if (!runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return -ENOMEM;
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200120 runtime->substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 spin_lock_init(&runtime->lock);
122 init_waitqueue_head(&runtime->sleep);
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200123 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 runtime->event = NULL;
125 runtime->buffer_size = PAGE_SIZE;
126 runtime->avail_min = 1;
127 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
128 runtime->avail = 0;
129 else
130 runtime->avail = runtime->buffer_size;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200131 runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL);
132 if (!runtime->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 kfree(runtime);
134 return -ENOMEM;
135 }
136 runtime->appl_ptr = runtime->hw_ptr = 0;
137 substream->runtime = runtime;
138 return 0;
139}
140
Takashi Iwai48c9d412005-11-17 13:56:51 +0100141static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100143 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 kfree(runtime->buffer);
146 kfree(runtime);
147 substream->runtime = NULL;
148 return 0;
149}
150
Takashi Iwai5bed9132018-07-17 22:32:52 +0200151static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Takashi Iwai219df322008-11-03 08:17:05 +0100153 if (!substream->opened)
154 return;
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200155 substream->ops->trigger(substream, up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Takashi Iwai48c9d412005-11-17 13:56:51 +0100158static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Takashi Iwai219df322008-11-03 08:17:05 +0100160 if (!substream->opened)
161 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 substream->ops->trigger(substream, up);
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200163 if (!up)
164 cancel_work_sync(&substream->runtime->event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Takashi Iwai48c9d412005-11-17 13:56:51 +0100167int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100170 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 snd_rawmidi_output_trigger(substream, 0);
173 runtime->drain = 0;
174 spin_lock_irqsave(&runtime->lock, flags);
175 runtime->appl_ptr = runtime->hw_ptr = 0;
176 runtime->avail = runtime->buffer_size;
177 spin_unlock_irqrestore(&runtime->lock, flags);
178 return 0;
179}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100180EXPORT_SYMBOL(snd_rawmidi_drop_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Takashi Iwai48c9d412005-11-17 13:56:51 +0100182int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 int err;
185 long timeout;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100186 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 err = 0;
189 runtime->drain = 1;
190 timeout = wait_event_interruptible_timeout(runtime->sleep,
191 (runtime->avail >= runtime->buffer_size),
192 10*HZ);
193 if (signal_pending(current))
194 err = -ERESTARTSYS;
195 if (runtime->avail < runtime->buffer_size && !timeout) {
Takashi Iwaica20d292014-02-04 18:21:39 +0100196 rmidi_warn(substream->rmidi,
197 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
198 (long)runtime->avail, (long)runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 err = -EIO;
200 }
201 runtime->drain = 0;
202 if (err != -ERESTARTSYS) {
203 /* we need wait a while to make sure that Tx FIFOs are empty */
204 if (substream->ops->drain)
205 substream->ops->drain(substream);
206 else
207 msleep(50);
208 snd_rawmidi_drop_output(substream);
209 }
210 return err;
211}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100212EXPORT_SYMBOL(snd_rawmidi_drain_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Takashi Iwai48c9d412005-11-17 13:56:51 +0100214int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100217 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 snd_rawmidi_input_trigger(substream, 0);
220 runtime->drain = 0;
221 spin_lock_irqsave(&runtime->lock, flags);
222 runtime->appl_ptr = runtime->hw_ptr = 0;
223 runtime->avail = 0;
224 spin_unlock_irqrestore(&runtime->lock, flags);
225 return 0;
226}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100227EXPORT_SYMBOL(snd_rawmidi_drain_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100229/* look for an available substream for the given stream direction;
230 * if a specific subdevice is given, try to assign it
231 */
232static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
233 int stream, int mode,
234 struct snd_rawmidi_substream **sub_ret)
235{
236 struct snd_rawmidi_substream *substream;
237 struct snd_rawmidi_str *s = &rmidi->streams[stream];
238 static unsigned int info_flags[2] = {
239 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
240 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
241 };
242
243 if (!(rmidi->info_flags & info_flags[stream]))
244 return -ENXIO;
245 if (subdevice >= 0 && subdevice >= s->substream_count)
246 return -ENODEV;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100247
248 list_for_each_entry(substream, &s->substreams, list) {
249 if (substream->opened) {
250 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
Clemens Ladisch16fb1092009-10-21 09:10:16 +0200251 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
252 !substream->append)
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100253 continue;
254 }
255 if (subdevice < 0 || subdevice == substream->number) {
256 *sub_ret = substream;
257 return 0;
258 }
259 }
260 return -EAGAIN;
261}
262
263/* open and do ref-counting for the given substream */
264static int open_substream(struct snd_rawmidi *rmidi,
265 struct snd_rawmidi_substream *substream,
266 int mode)
267{
268 int err;
269
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200270 if (substream->use_count == 0) {
271 err = snd_rawmidi_runtime_create(substream);
272 if (err < 0)
273 return err;
274 err = substream->ops->open(substream);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200275 if (err < 0) {
276 snd_rawmidi_runtime_free(substream);
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200277 return err;
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200278 }
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200279 substream->opened = 1;
Clemens Ladisch2d4b8422009-07-13 13:52:46 +0200280 substream->active_sensing = 0;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200281 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
282 substream->append = 1;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100283 substream->pid = get_pid(task_pid(current));
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200284 rmidi->streams[substream->stream].substream_opened++;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200285 }
286 substream->use_count++;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100287 return 0;
288}
289
290static void close_substream(struct snd_rawmidi *rmidi,
291 struct snd_rawmidi_substream *substream,
292 int cleanup);
293
294static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
295 struct snd_rawmidi_file *rfile)
296{
297 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
298 int err;
299
300 rfile->input = rfile->output = NULL;
301 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
302 err = assign_substream(rmidi, subdevice,
303 SNDRV_RAWMIDI_STREAM_INPUT,
304 mode, &sinput);
305 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200306 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100307 }
308 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
309 err = assign_substream(rmidi, subdevice,
310 SNDRV_RAWMIDI_STREAM_OUTPUT,
311 mode, &soutput);
312 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200313 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100314 }
315
316 if (sinput) {
317 err = open_substream(rmidi, sinput, mode);
318 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200319 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100320 }
321 if (soutput) {
322 err = open_substream(rmidi, soutput, mode);
323 if (err < 0) {
324 if (sinput)
325 close_substream(rmidi, sinput, 0);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200326 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100327 }
328 }
329
330 rfile->rmidi = rmidi;
331 rfile->input = sinput;
332 rfile->output = soutput;
333 return 0;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100334}
335
336/* called from sound/core/seq/seq_midi.c */
Clemens Ladischf87135f2005-11-20 14:06:59 +0100337int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200338 int mode, struct snd_rawmidi_file *rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100340 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int err;
342
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100343 if (snd_BUG_ON(!rfile))
344 return -EINVAL;
345
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100346 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100347 rmidi = snd_rawmidi_search(card, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (rmidi == NULL) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100349 mutex_unlock(&register_mutex);
350 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352 if (!try_module_get(rmidi->card->module)) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100353 mutex_unlock(&register_mutex);
354 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
Takashi Iwaif9d20282009-02-11 14:55:59 +0100356 mutex_unlock(&register_mutex);
357
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100358 mutex_lock(&rmidi->open_mutex);
359 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
360 mutex_unlock(&rmidi->open_mutex);
361 if (err < 0)
362 module_put(rmidi->card->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return err;
364}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100365EXPORT_SYMBOL(snd_rawmidi_kernel_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367static int snd_rawmidi_open(struct inode *inode, struct file *file)
368{
369 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100370 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100371 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 unsigned short fflags;
373 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100374 struct snd_rawmidi *rmidi;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100375 struct snd_rawmidi_file *rawmidi_file = NULL;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200376 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Takashi Iwai5bed9132018-07-17 22:32:52 +0200378 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100379 return -EINVAL; /* invalid combination */
380
Takashi Iwai02f48652010-04-13 11:49:04 +0200381 err = nonseekable_open(inode, file);
382 if (err < 0)
383 return err;
384
Clemens Ladischf1902862005-10-24 17:05:03 +0200385 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100386 rmidi = snd_lookup_minor_data(iminor(inode),
387 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200389 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100390 rmidi = snd_lookup_oss_minor_data(iminor(inode),
391 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200393 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (rmidi == NULL)
397 return -ENODEV;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100398
Takashi Iwaia0830db2012-10-16 13:05:59 +0200399 if (!try_module_get(rmidi->card->module)) {
400 snd_card_unref(rmidi->card);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100401 return -ENXIO;
Takashi Iwaia0830db2012-10-16 13:05:59 +0200402 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100403
404 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 card = rmidi->card;
406 err = snd_card_file_add(card, file);
407 if (err < 0)
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100408 goto __error_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200410 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
413 if (rawmidi_file == NULL) {
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100414 err = -ENOMEM;
415 goto __error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 init_waitqueue_entry(&wait, current);
418 add_wait_queue(&rmidi->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 while (1) {
Takashi Iwai23c18d42014-02-19 14:30:29 +0100420 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100421 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (err >= 0)
423 break;
424 if (err == -EAGAIN) {
425 if (file->f_flags & O_NONBLOCK) {
426 err = -EBUSY;
427 break;
428 }
429 } else
430 break;
431 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100432 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100434 mutex_lock(&rmidi->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +0200435 if (rmidi->card->shutdown) {
436 err = -ENODEV;
437 break;
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (signal_pending(current)) {
440 err = -ERESTARTSYS;
441 break;
442 }
443 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100444 remove_wait_queue(&rmidi->open_wait, &wait);
445 if (err < 0) {
446 kfree(rawmidi_file);
447 goto __error;
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449#ifdef CONFIG_SND_OSSEMUL
450 if (rawmidi_file->input && rawmidi_file->input->runtime)
451 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
452 if (rawmidi_file->output && rawmidi_file->output->runtime)
453 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
454#endif
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100455 file->private_data = rawmidi_file;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100456 mutex_unlock(&rmidi->open_mutex);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200457 snd_card_unref(rmidi->card);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100458 return 0;
459
460 __error:
461 snd_card_file_remove(card, file);
462 __error_card:
463 mutex_unlock(&rmidi->open_mutex);
464 module_put(rmidi->card->module);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200465 snd_card_unref(rmidi->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return err;
467}
468
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100469static void close_substream(struct snd_rawmidi *rmidi,
470 struct snd_rawmidi_substream *substream,
471 int cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100473 if (--substream->use_count)
474 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100476 if (cleanup) {
477 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
478 snd_rawmidi_input_trigger(substream, 0);
479 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (substream->active_sensing) {
481 unsigned char buf = 0xfe;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100482 /* sending single active sensing message
483 * to shut the device up
484 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 snd_rawmidi_kernel_write(substream, &buf, 1);
486 }
487 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
488 snd_rawmidi_output_trigger(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100491 substream->ops->close(substream);
492 if (substream->runtime->private_free)
493 substream->runtime->private_free(substream);
494 snd_rawmidi_runtime_free(substream);
495 substream->opened = 0;
496 substream->append = 0;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100497 put_pid(substream->pid);
498 substream->pid = NULL;
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200499 rmidi->streams[substream->stream].substream_opened--;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100500}
501
502static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
503{
504 struct snd_rawmidi *rmidi;
505
506 rmidi = rfile->rmidi;
507 mutex_lock(&rmidi->open_mutex);
508 if (rfile->input) {
509 close_substream(rmidi, rfile->input, 1);
510 rfile->input = NULL;
511 }
512 if (rfile->output) {
513 close_substream(rmidi, rfile->output, 1);
514 rfile->output = NULL;
515 }
516 rfile->rmidi = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100517 mutex_unlock(&rmidi->open_mutex);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100518 wake_up(&rmidi->open_wait);
519}
520
521/* called from sound/core/seq/seq_midi.c */
522int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
523{
524 struct snd_rawmidi *rmidi;
525
526 if (snd_BUG_ON(!rfile))
527 return -ENXIO;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200528
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100529 rmidi = rfile->rmidi;
530 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 module_put(rmidi->card->module);
532 return 0;
533}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100534EXPORT_SYMBOL(snd_rawmidi_kernel_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536static int snd_rawmidi_release(struct inode *inode, struct file *file)
537{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100538 struct snd_rawmidi_file *rfile;
539 struct snd_rawmidi *rmidi;
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200540 struct module *module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 rfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 rmidi = rfile->rmidi;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100544 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 kfree(rfile);
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200546 module = rmidi->card->module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 snd_card_file_remove(rmidi->card, file);
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200548 module_put(module);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Adrian Bunk18612042005-11-23 13:14:50 +0100552static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
553 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100555 struct snd_rawmidi *rmidi;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (substream == NULL)
558 return -ENODEV;
559 rmidi = substream->rmidi;
560 memset(info, 0, sizeof(*info));
561 info->card = rmidi->card->number;
562 info->device = rmidi->device;
563 info->subdevice = substream->number;
564 info->stream = substream->stream;
565 info->flags = rmidi->info_flags;
566 strcpy(info->id, rmidi->id);
567 strcpy(info->name, rmidi->name);
568 strcpy(info->subname, substream->name);
569 info->subdevices_count = substream->pstr->substream_count;
570 info->subdevices_avail = (substream->pstr->substream_count -
571 substream->pstr->substream_opened);
572 return 0;
573}
574
Takashi Iwai48c9d412005-11-17 13:56:51 +0100575static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200576 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100578 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int err;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200580
581 err = snd_rawmidi_info(substream, &info);
582 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100584 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -EFAULT;
586 return 0;
587}
588
Takashi Iwaic1cfd902017-12-14 16:44:12 +0100589static int __snd_rawmidi_info_select(struct snd_card *card,
590 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100592 struct snd_rawmidi *rmidi;
593 struct snd_rawmidi_str *pstr;
594 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100595
Clemens Ladischf87135f2005-11-20 14:06:59 +0100596 rmidi = snd_rawmidi_search(card, info->device);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100597 if (!rmidi)
598 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (info->stream < 0 || info->stream > 1)
600 return -EINVAL;
601 pstr = &rmidi->streams[info->stream];
602 if (pstr->substream_count == 0)
603 return -ENOENT;
604 if (info->subdevice >= pstr->substream_count)
605 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200606 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if ((unsigned int)substream->number == info->subdevice)
608 return snd_rawmidi_info(substream, info);
609 }
610 return -ENXIO;
611}
Takashi Iwaic1cfd902017-12-14 16:44:12 +0100612
613int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
614{
615 int ret;
616
617 mutex_lock(&register_mutex);
618 ret = __snd_rawmidi_info_select(card, info);
619 mutex_unlock(&register_mutex);
620 return ret;
621}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100622EXPORT_SYMBOL(snd_rawmidi_info_select);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Takashi Iwai48c9d412005-11-17 13:56:51 +0100624static int snd_rawmidi_info_select_user(struct snd_card *card,
625 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100628 struct snd_rawmidi_info info;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (get_user(info.device, &_info->device))
631 return -EFAULT;
632 if (get_user(info.stream, &_info->stream))
633 return -EFAULT;
634 if (get_user(info.subdevice, &_info->subdevice))
635 return -EFAULT;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200636 err = snd_rawmidi_info_select(card, &info);
637 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100639 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return -EFAULT;
641 return 0;
642}
643
Takashi Iwai48c9d412005-11-17 13:56:51 +0100644int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200645 struct snd_rawmidi_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Takashi Iwai39675f72018-07-17 17:26:43 +0200647 char *newbuf, *oldbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100648 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (substream->append && substream->use_count > 1)
651 return -EBUSY;
652 snd_rawmidi_drain_output(substream);
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 Iwai39675f72018-07-17 17:26:43 +0200658 newbuf = kmalloc(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;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100665 runtime->avail = runtime->buffer_size;
Takashi Iwai39675f72018-07-17 17:26:43 +0200666 runtime->appl_ptr = runtime->hw_ptr = 0;
667 spin_unlock_irq(&runtime->lock);
668 kfree(oldbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670 runtime->avail_min = params->avail_min;
671 substream->active_sensing = !params->no_active_sensing;
672 return 0;
673}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100674EXPORT_SYMBOL(snd_rawmidi_output_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Takashi Iwai48c9d412005-11-17 13:56:51 +0100676int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200677 struct snd_rawmidi_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Takashi Iwai39675f72018-07-17 17:26:43 +0200679 char *newbuf, *oldbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100680 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 snd_rawmidi_drain_input(substream);
Takashi Iwai5bed9132018-07-17 22:32:52 +0200683 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return -EINVAL;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200685 if (params->avail_min < 1 || params->avail_min > params->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (params->buffer_size != runtime->buffer_size) {
Takashi Iwai39675f72018-07-17 17:26:43 +0200688 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800689 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -ENOMEM;
Takashi Iwai39675f72018-07-17 17:26:43 +0200691 spin_lock_irq(&runtime->lock);
692 oldbuf = runtime->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 runtime->buffer = newbuf;
694 runtime->buffer_size = params->buffer_size;
Takashi Iwai39675f72018-07-17 17:26:43 +0200695 runtime->appl_ptr = runtime->hw_ptr = 0;
696 spin_unlock_irq(&runtime->lock);
697 kfree(oldbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 runtime->avail_min = params->avail_min;
700 return 0;
701}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100702EXPORT_SYMBOL(snd_rawmidi_input_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Takashi Iwai48c9d412005-11-17 13:56:51 +0100704static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200705 struct snd_rawmidi_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100707 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 memset(status, 0, sizeof(*status));
710 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
711 spin_lock_irq(&runtime->lock);
712 status->avail = runtime->avail;
713 spin_unlock_irq(&runtime->lock);
714 return 0;
715}
716
Takashi Iwai48c9d412005-11-17 13:56:51 +0100717static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
Takashi Iwai5bed9132018-07-17 22:32:52 +0200718 struct snd_rawmidi_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100720 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 memset(status, 0, sizeof(*status));
723 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
724 spin_lock_irq(&runtime->lock);
725 status->avail = runtime->avail;
726 status->xruns = runtime->xruns;
727 runtime->xruns = 0;
728 spin_unlock_irq(&runtime->lock);
729 return 0;
730}
731
732static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
733{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100734 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 void __user *argp = (void __user *)arg;
736
737 rfile = file->private_data;
738 if (((cmd >> 8) & 0xff) != 'W')
739 return -ENOTTY;
740 switch (cmd) {
741 case SNDRV_RAWMIDI_IOCTL_PVERSION:
742 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
743 case SNDRV_RAWMIDI_IOCTL_INFO:
744 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100745 int stream;
746 struct snd_rawmidi_info __user *info = argp;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (get_user(stream, &info->stream))
749 return -EFAULT;
750 switch (stream) {
751 case SNDRV_RAWMIDI_STREAM_INPUT:
752 return snd_rawmidi_info_user(rfile->input, info);
753 case SNDRV_RAWMIDI_STREAM_OUTPUT:
754 return snd_rawmidi_info_user(rfile->output, info);
755 default:
756 return -EINVAL;
757 }
758 }
759 case SNDRV_RAWMIDI_IOCTL_PARAMS:
760 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100761 struct snd_rawmidi_params params;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200762
Takashi Iwai48c9d412005-11-17 13:56:51 +0100763 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return -EFAULT;
765 switch (params.stream) {
766 case SNDRV_RAWMIDI_STREAM_OUTPUT:
767 if (rfile->output == NULL)
768 return -EINVAL;
769 return snd_rawmidi_output_params(rfile->output, &params);
770 case SNDRV_RAWMIDI_STREAM_INPUT:
771 if (rfile->input == NULL)
772 return -EINVAL;
773 return snd_rawmidi_input_params(rfile->input, &params);
774 default:
775 return -EINVAL;
776 }
777 }
778 case SNDRV_RAWMIDI_IOCTL_STATUS:
779 {
780 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100781 struct snd_rawmidi_status status;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200782
Takashi Iwai48c9d412005-11-17 13:56:51 +0100783 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return -EFAULT;
785 switch (status.stream) {
786 case SNDRV_RAWMIDI_STREAM_OUTPUT:
787 if (rfile->output == NULL)
788 return -EINVAL;
789 err = snd_rawmidi_output_status(rfile->output, &status);
790 break;
791 case SNDRV_RAWMIDI_STREAM_INPUT:
792 if (rfile->input == NULL)
793 return -EINVAL;
794 err = snd_rawmidi_input_status(rfile->input, &status);
795 break;
796 default:
797 return -EINVAL;
798 }
799 if (err < 0)
800 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100801 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return -EFAULT;
803 return 0;
804 }
805 case SNDRV_RAWMIDI_IOCTL_DROP:
806 {
807 int val;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (get_user(val, (int __user *) argp))
810 return -EFAULT;
811 switch (val) {
812 case SNDRV_RAWMIDI_STREAM_OUTPUT:
813 if (rfile->output == NULL)
814 return -EINVAL;
815 return snd_rawmidi_drop_output(rfile->output);
816 default:
817 return -EINVAL;
818 }
819 }
820 case SNDRV_RAWMIDI_IOCTL_DRAIN:
821 {
822 int val;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (get_user(val, (int __user *) argp))
825 return -EFAULT;
826 switch (val) {
827 case SNDRV_RAWMIDI_STREAM_OUTPUT:
828 if (rfile->output == NULL)
829 return -EINVAL;
830 return snd_rawmidi_drain_output(rfile->output);
831 case SNDRV_RAWMIDI_STREAM_INPUT:
832 if (rfile->input == NULL)
833 return -EINVAL;
834 return snd_rawmidi_drain_input(rfile->input);
835 default:
836 return -EINVAL;
837 }
838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 default:
Takashi Iwaica20d292014-02-04 18:21:39 +0100840 rmidi_dbg(rfile->rmidi,
841 "rawmidi: unknown command = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 return -ENOTTY;
844}
845
Takashi Iwai48c9d412005-11-17 13:56:51 +0100846static int snd_rawmidi_control_ioctl(struct snd_card *card,
847 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 unsigned int cmd,
849 unsigned long arg)
850{
851 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 switch (cmd) {
854 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
855 {
856 int device;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (get_user(device, (int __user *)argp))
859 return -EFAULT;
Dan Carpentera7a13d02010-09-09 00:11:41 +0200860 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
861 device = SNDRV_RAWMIDI_DEVICES - 1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100862 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 device = device < 0 ? 0 : device + 1;
864 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100865 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 break;
867 device++;
868 }
869 if (device == SNDRV_RAWMIDI_DEVICES)
870 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100871 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (put_user(device, (int __user *)argp))
873 return -EFAULT;
874 return 0;
875 }
876 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
877 {
878 int val;
Takashi Iwai5bed9132018-07-17 22:32:52 +0200879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (get_user(val, (int __user *)argp))
881 return -EFAULT;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100882 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return 0;
884 }
885 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
886 return snd_rawmidi_info_select_user(card, argp);
887 }
888 return -ENOIOCTLCMD;
889}
890
891/**
892 * snd_rawmidi_receive - receive the input data from the device
893 * @substream: the rawmidi substream
894 * @buffer: the buffer pointer
895 * @count: the data size to read
896 *
897 * Reads the data from the internal buffer.
898 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100899 * Return: The size of read data, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100901int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
902 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 unsigned long flags;
905 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100906 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Takashi Iwai219df322008-11-03 08:17:05 +0100908 if (!substream->opened)
909 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +0100911 rmidi_dbg(substream->rmidi,
912 "snd_rawmidi_receive: input is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -EINVAL;
914 }
915 spin_lock_irqsave(&runtime->lock, flags);
916 if (count == 1) { /* special case, faster code */
917 substream->bytes++;
918 if (runtime->avail < runtime->buffer_size) {
919 runtime->buffer[runtime->hw_ptr++] = buffer[0];
920 runtime->hw_ptr %= runtime->buffer_size;
921 runtime->avail++;
922 result++;
923 } else {
924 runtime->xruns++;
925 }
926 } else {
927 substream->bytes += count;
928 count1 = runtime->buffer_size - runtime->hw_ptr;
929 if (count1 > count)
930 count1 = count;
931 if (count1 > (int)(runtime->buffer_size - runtime->avail))
932 count1 = runtime->buffer_size - runtime->avail;
933 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
934 runtime->hw_ptr += count1;
935 runtime->hw_ptr %= runtime->buffer_size;
936 runtime->avail += count1;
937 count -= count1;
938 result += count1;
939 if (count > 0) {
940 buffer += count1;
941 count1 = count;
942 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
943 count1 = runtime->buffer_size - runtime->avail;
944 runtime->xruns += count - count1;
945 }
946 if (count1 > 0) {
947 memcpy(runtime->buffer, buffer, count1);
948 runtime->hw_ptr = count1;
949 runtime->avail += count1;
950 result += count1;
951 }
952 }
953 }
954 if (result > 0) {
955 if (runtime->event)
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200956 schedule_work(&runtime->event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 else if (snd_rawmidi_ready(substream))
958 wake_up(&runtime->sleep);
959 }
960 spin_unlock_irqrestore(&runtime->lock, flags);
961 return result;
962}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100963EXPORT_SYMBOL(snd_rawmidi_receive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Takashi Iwai48c9d412005-11-17 13:56:51 +0100965static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100966 unsigned char __user *userbuf,
967 unsigned char *kernelbuf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 unsigned long flags;
970 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100971 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai81f57752016-02-03 14:41:22 +0100972 unsigned long appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Takashi Iwai81f57752016-02-03 14:41:22 +0100974 spin_lock_irqsave(&runtime->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 while (count > 0 && runtime->avail) {
976 count1 = runtime->buffer_size - runtime->appl_ptr;
977 if (count1 > count)
978 count1 = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (count1 > (int)runtime->avail)
980 count1 = runtime->avail;
Takashi Iwai81f57752016-02-03 14:41:22 +0100981
982 /* update runtime->appl_ptr before unlocking for userbuf */
983 appl_ptr = runtime->appl_ptr;
984 runtime->appl_ptr += count1;
985 runtime->appl_ptr %= runtime->buffer_size;
986 runtime->avail -= count1;
987
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100988 if (kernelbuf)
Takashi Iwai81f57752016-02-03 14:41:22 +0100989 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100990 if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 spin_unlock_irqrestore(&runtime->lock, flags);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100992 if (copy_to_user(userbuf + result,
Takashi Iwai81f57752016-02-03 14:41:22 +0100993 runtime->buffer + appl_ptr, count1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return result > 0 ? result : -EFAULT;
995 }
996 spin_lock_irqsave(&runtime->lock, flags);
997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 result += count1;
999 count -= count1;
1000 }
Takashi Iwai81f57752016-02-03 14:41:22 +01001001 spin_unlock_irqrestore(&runtime->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return result;
1003}
1004
Takashi Iwai48c9d412005-11-17 13:56:51 +01001005long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
1006 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 snd_rawmidi_input_trigger(substream, 1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001009 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001011EXPORT_SYMBOL(snd_rawmidi_kernel_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Takashi Iwai48c9d412005-11-17 13:56:51 +01001013static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1014 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 long result;
1017 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001018 struct snd_rawmidi_file *rfile;
1019 struct snd_rawmidi_substream *substream;
1020 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 rfile = file->private_data;
1023 substream = rfile->input;
1024 if (substream == NULL)
1025 return -EIO;
1026 runtime = substream->runtime;
1027 snd_rawmidi_input_trigger(substream, 1);
1028 result = 0;
1029 while (count > 0) {
1030 spin_lock_irq(&runtime->lock);
1031 while (!snd_rawmidi_ready(substream)) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02001032 wait_queue_entry_t wait;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1035 spin_unlock_irq(&runtime->lock);
1036 return result > 0 ? result : -EAGAIN;
1037 }
1038 init_waitqueue_entry(&wait, current);
1039 add_wait_queue(&runtime->sleep, &wait);
1040 set_current_state(TASK_INTERRUPTIBLE);
1041 spin_unlock_irq(&runtime->lock);
1042 schedule();
1043 remove_wait_queue(&runtime->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001044 if (rfile->rmidi->card->shutdown)
1045 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (signal_pending(current))
1047 return result > 0 ? result : -ERESTARTSYS;
1048 if (!runtime->avail)
1049 return result > 0 ? result : -EIO;
1050 spin_lock_irq(&runtime->lock);
1051 }
1052 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001053 count1 = snd_rawmidi_kernel_read1(substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001054 (unsigned char __user *)buf,
1055 NULL/*kernelbuf*/,
1056 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (count1 < 0)
1058 return result > 0 ? result : count1;
1059 result += count1;
1060 buf += count1;
1061 count -= count1;
1062 }
1063 return result;
1064}
1065
1066/**
1067 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1068 * @substream: the rawmidi substream
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001069 *
1070 * Return: 1 if the internal output buffer is empty, 0 if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001072int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001074 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 int result;
1076 unsigned long flags;
1077
1078 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001079 rmidi_dbg(substream->rmidi,
1080 "snd_rawmidi_transmit_empty: output is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 1;
1082 }
1083 spin_lock_irqsave(&runtime->lock, flags);
1084 result = runtime->avail >= runtime->buffer_size;
1085 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwai5bed9132018-07-17 22:32:52 +02001086 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001088EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090/**
Takashi Iwai06ab3002016-01-31 11:57:41 +01001091 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 * @substream: the rawmidi substream
1093 * @buffer: the buffer pointer
1094 * @count: data size to transfer
1095 *
Takashi Iwai06ab3002016-01-31 11:57:41 +01001096 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 */
Takashi Iwai06ab3002016-01-31 11:57:41 +01001098int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001099 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001102 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001105 rmidi_dbg(substream->rmidi,
1106 "snd_rawmidi_transmit_peek: output is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return -EINVAL;
1108 }
1109 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (runtime->avail >= runtime->buffer_size) {
1111 /* warning: lowlevel layer MUST trigger down the hardware */
1112 goto __skip;
1113 }
1114 if (count == 1) { /* special case, faster code */
1115 *buffer = runtime->buffer[runtime->hw_ptr];
1116 result++;
1117 } else {
1118 count1 = runtime->buffer_size - runtime->hw_ptr;
1119 if (count1 > count)
1120 count1 = count;
1121 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1122 count1 = runtime->buffer_size - runtime->avail;
1123 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1124 count -= count1;
1125 result += count1;
1126 if (count > 0) {
1127 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1128 count = runtime->buffer_size - runtime->avail - count1;
1129 memcpy(buffer + count1, runtime->buffer, count);
1130 result += count;
1131 }
1132 }
1133 __skip:
Takashi Iwai06ab3002016-01-31 11:57:41 +01001134 return result;
1135}
1136EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1137
1138/**
1139 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1140 * @substream: the rawmidi substream
1141 * @buffer: the buffer pointer
1142 * @count: data size to transfer
1143 *
1144 * Copies data from the internal output buffer to the given buffer.
1145 *
1146 * Call this in the interrupt handler when the midi output is ready,
1147 * and call snd_rawmidi_transmit_ack() after the transmission is
1148 * finished.
1149 *
1150 * Return: The size of copied data, or a negative error code on failure.
1151 */
1152int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1153 unsigned char *buffer, int count)
1154{
1155 struct snd_rawmidi_runtime *runtime = substream->runtime;
1156 int result;
1157 unsigned long flags;
1158
1159 spin_lock_irqsave(&runtime->lock, flags);
1160 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 spin_unlock_irqrestore(&runtime->lock, flags);
1162 return result;
1163}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001164EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166/**
Takashi Iwai06ab3002016-01-31 11:57:41 +01001167 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1168 * @substream: the rawmidi substream
1169 * @count: the transferred count
1170 *
1171 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1172 */
1173int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1174{
1175 struct snd_rawmidi_runtime *runtime = substream->runtime;
1176
1177 if (runtime->buffer == NULL) {
1178 rmidi_dbg(substream->rmidi,
1179 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1180 return -EINVAL;
1181 }
1182 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1183 runtime->hw_ptr += count;
1184 runtime->hw_ptr %= runtime->buffer_size;
1185 runtime->avail += count;
1186 substream->bytes += count;
1187 if (count > 0) {
1188 if (runtime->drain || snd_rawmidi_ready(substream))
1189 wake_up(&runtime->sleep);
1190 }
1191 return count;
1192}
1193EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1194
1195/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 * snd_rawmidi_transmit_ack - acknowledge the transmission
1197 * @substream: the rawmidi substream
Masanari Iida0a114582014-02-09 00:47:36 +09001198 * @count: the transferred count
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 *
1200 * Advances the hardware pointer for the internal output buffer with
1201 * the given size and updates the condition.
1202 * Call after the transmission is finished.
1203 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001204 * Return: The advanced size if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001206int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001208 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai06ab3002016-01-31 11:57:41 +01001209 int result;
1210 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai06ab3002016-01-31 11:57:41 +01001213 result = __snd_rawmidi_transmit_ack(substream, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwai06ab3002016-01-31 11:57:41 +01001215 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001217EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219/**
1220 * snd_rawmidi_transmit - copy from the buffer to the device
1221 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001222 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 * @count: the data size to transfer
Takashi Iwai5bed9132018-07-17 22:32:52 +02001224 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 * Copies data from the buffer to the device and advances the pointer.
1226 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001227 * Return: The copied size if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001229int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1230 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Takashi Iwai06ab3002016-01-31 11:57:41 +01001232 struct snd_rawmidi_runtime *runtime = substream->runtime;
1233 int result;
1234 unsigned long flags;
1235
1236 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai219df322008-11-03 08:17:05 +01001237 if (!substream->opened)
Takashi Iwai06ab3002016-01-31 11:57:41 +01001238 result = -EBADFD;
1239 else {
1240 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1241 if (count <= 0)
1242 result = count;
1243 else
1244 result = __snd_rawmidi_transmit_ack(substream, count);
1245 }
1246 spin_unlock_irqrestore(&runtime->lock, flags);
1247 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001249EXPORT_SYMBOL(snd_rawmidi_transmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Takashi Iwai48c9d412005-11-17 13:56:51 +01001251static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001252 const unsigned char __user *userbuf,
1253 const unsigned char *kernelbuf,
1254 long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 unsigned long flags;
1257 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001258 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai81f57752016-02-03 14:41:22 +01001259 unsigned long appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Takashi Iwaicc85f7a2016-02-01 12:04:55 +01001261 if (!kernelbuf && !userbuf)
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001262 return -EINVAL;
1263 if (snd_BUG_ON(!runtime->buffer))
1264 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 result = 0;
1267 spin_lock_irqsave(&runtime->lock, flags);
1268 if (substream->append) {
1269 if ((long)runtime->avail < count) {
1270 spin_unlock_irqrestore(&runtime->lock, flags);
1271 return -EAGAIN;
1272 }
1273 }
1274 while (count > 0 && runtime->avail > 0) {
1275 count1 = runtime->buffer_size - runtime->appl_ptr;
1276 if (count1 > count)
1277 count1 = count;
1278 if (count1 > (long)runtime->avail)
1279 count1 = runtime->avail;
Takashi Iwai81f57752016-02-03 14:41:22 +01001280
1281 /* update runtime->appl_ptr before unlocking for userbuf */
1282 appl_ptr = runtime->appl_ptr;
1283 runtime->appl_ptr += count1;
1284 runtime->appl_ptr %= runtime->buffer_size;
1285 runtime->avail -= count1;
1286
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001287 if (kernelbuf)
Takashi Iwai81f57752016-02-03 14:41:22 +01001288 memcpy(runtime->buffer + appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001289 kernelbuf + result, count1);
1290 else if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwai81f57752016-02-03 14:41:22 +01001292 if (copy_from_user(runtime->buffer + appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001293 userbuf + result, count1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 spin_lock_irqsave(&runtime->lock, flags);
1295 result = result > 0 ? result : -EFAULT;
1296 goto __end;
1297 }
1298 spin_lock_irqsave(&runtime->lock, flags);
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 result += count1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 count -= count1;
1302 }
1303 __end:
1304 count1 = runtime->avail < runtime->buffer_size;
1305 spin_unlock_irqrestore(&runtime->lock, flags);
1306 if (count1)
1307 snd_rawmidi_output_trigger(substream, 1);
1308 return result;
1309}
1310
Takashi Iwai48c9d412005-11-17 13:56:51 +01001311long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1312 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001314 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001316EXPORT_SYMBOL(snd_rawmidi_kernel_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Takashi Iwai48c9d412005-11-17 13:56:51 +01001318static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1319 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 long result, timeout;
1322 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001323 struct snd_rawmidi_file *rfile;
1324 struct snd_rawmidi_runtime *runtime;
1325 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 rfile = file->private_data;
1328 substream = rfile->output;
1329 runtime = substream->runtime;
1330 /* we cannot put an atomic message to our buffer */
1331 if (substream->append && count > runtime->buffer_size)
1332 return -EIO;
1333 result = 0;
1334 while (count > 0) {
1335 spin_lock_irq(&runtime->lock);
1336 while (!snd_rawmidi_ready_append(substream, count)) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02001337 wait_queue_entry_t wait;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (file->f_flags & O_NONBLOCK) {
1340 spin_unlock_irq(&runtime->lock);
1341 return result > 0 ? result : -EAGAIN;
1342 }
1343 init_waitqueue_entry(&wait, current);
1344 add_wait_queue(&runtime->sleep, &wait);
1345 set_current_state(TASK_INTERRUPTIBLE);
1346 spin_unlock_irq(&runtime->lock);
1347 timeout = schedule_timeout(30 * HZ);
1348 remove_wait_queue(&runtime->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001349 if (rfile->rmidi->card->shutdown)
1350 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (signal_pending(current))
1352 return result > 0 ? result : -ERESTARTSYS;
1353 if (!runtime->avail && !timeout)
1354 return result > 0 ? result : -EIO;
1355 spin_lock_irq(&runtime->lock);
1356 }
1357 spin_unlock_irq(&runtime->lock);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001358 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (count1 < 0)
1360 return result > 0 ? result : count1;
1361 result += count1;
1362 buf += count1;
1363 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1364 break;
1365 count -= count1;
1366 }
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001367 if (file->f_flags & O_DSYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 spin_lock_irq(&runtime->lock);
1369 while (runtime->avail != runtime->buffer_size) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02001370 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 unsigned int last_avail = runtime->avail;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 init_waitqueue_entry(&wait, current);
1374 add_wait_queue(&runtime->sleep, &wait);
1375 set_current_state(TASK_INTERRUPTIBLE);
1376 spin_unlock_irq(&runtime->lock);
1377 timeout = schedule_timeout(30 * HZ);
1378 remove_wait_queue(&runtime->sleep, &wait);
1379 if (signal_pending(current))
1380 return result > 0 ? result : -ERESTARTSYS;
1381 if (runtime->avail == last_avail && !timeout)
1382 return result > 0 ? result : -EIO;
1383 spin_lock_irq(&runtime->lock);
1384 }
1385 spin_unlock_irq(&runtime->lock);
1386 }
1387 return result;
1388}
1389
Takashi Iwai5bed9132018-07-17 22:32:52 +02001390static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001392 struct snd_rawmidi_file *rfile;
1393 struct snd_rawmidi_runtime *runtime;
Al Viro680ef722017-07-02 23:27:36 -04001394 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 rfile = file->private_data;
1397 if (rfile->input != NULL) {
1398 runtime = rfile->input->runtime;
1399 snd_rawmidi_input_trigger(rfile->input, 1);
1400 poll_wait(file, &runtime->sleep, wait);
1401 }
1402 if (rfile->output != NULL) {
1403 runtime = rfile->output->runtime;
1404 poll_wait(file, &runtime->sleep, wait);
1405 }
1406 mask = 0;
1407 if (rfile->input != NULL) {
1408 if (snd_rawmidi_ready(rfile->input))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001409 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 if (rfile->output != NULL) {
1412 if (snd_rawmidi_ready(rfile->output))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001413 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415 return mask;
1416}
1417
1418/*
1419 */
1420#ifdef CONFIG_COMPAT
1421#include "rawmidi_compat.c"
1422#else
1423#define snd_rawmidi_ioctl_compat NULL
1424#endif
1425
1426/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 */
1428
Takashi Iwai48c9d412005-11-17 13:56:51 +01001429static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1430 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001432 struct snd_rawmidi *rmidi;
1433 struct snd_rawmidi_substream *substream;
1434 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 rmidi = entry->private_data;
1437 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001438 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001440 list_for_each_entry(substream,
1441 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1442 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 snd_iprintf(buffer,
1444 "Output %d\n"
1445 " Tx bytes : %lu\n",
1446 substream->number,
1447 (unsigned long) substream->bytes);
1448 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001449 snd_iprintf(buffer,
1450 " Owner PID : %d\n",
1451 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 runtime = substream->runtime;
1453 snd_iprintf(buffer,
1454 " Mode : %s\n"
1455 " Buffer size : %lu\n"
1456 " Avail : %lu\n",
1457 runtime->oss ? "OSS compatible" : "native",
1458 (unsigned long) runtime->buffer_size,
1459 (unsigned long) runtime->avail);
1460 }
1461 }
1462 }
1463 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001464 list_for_each_entry(substream,
1465 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1466 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 snd_iprintf(buffer,
1468 "Input %d\n"
1469 " Rx bytes : %lu\n",
1470 substream->number,
1471 (unsigned long) substream->bytes);
1472 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001473 snd_iprintf(buffer,
1474 " Owner PID : %d\n",
1475 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 runtime = substream->runtime;
1477 snd_iprintf(buffer,
1478 " Buffer size : %lu\n"
1479 " Avail : %lu\n"
1480 " Overruns : %lu\n",
1481 (unsigned long) runtime->buffer_size,
1482 (unsigned long) runtime->avail,
1483 (unsigned long) runtime->xruns);
1484 }
1485 }
1486 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001487 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
1490/*
1491 * Register functions
1492 */
1493
Takashi Iwai5bed9132018-07-17 22:32:52 +02001494static const struct file_operations snd_rawmidi_f_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 .owner = THIS_MODULE,
1496 .read = snd_rawmidi_read,
1497 .write = snd_rawmidi_write,
1498 .open = snd_rawmidi_open,
1499 .release = snd_rawmidi_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001500 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 .poll = snd_rawmidi_poll,
1502 .unlocked_ioctl = snd_rawmidi_ioctl,
1503 .compat_ioctl = snd_rawmidi_ioctl_compat,
1504};
1505
Takashi Iwai48c9d412005-11-17 13:56:51 +01001506static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1507 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 int direction,
1509 int count)
1510{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001511 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 int idx;
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001515 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +01001516 if (!substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return -ENOMEM;
1518 substream->stream = direction;
1519 substream->number = idx;
1520 substream->rmidi = rmidi;
1521 substream->pstr = stream;
1522 list_add_tail(&substream->list, &stream->substreams);
1523 stream->substream_count++;
1524 }
1525 return 0;
1526}
1527
Takashi Iwaiaee50122015-01-29 17:55:52 +01001528static void release_rawmidi_device(struct device *dev)
1529{
1530 kfree(container_of(dev, struct snd_rawmidi, dev));
1531}
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533/**
1534 * snd_rawmidi_new - create a rawmidi instance
1535 * @card: the card instance
1536 * @id: the id string
1537 * @device: the device index
1538 * @output_count: the number of output streams
1539 * @input_count: the number of input streams
1540 * @rrawmidi: the pointer to store the new rawmidi instance
1541 *
1542 * Creates a new rawmidi instance.
1543 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1544 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001545 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001547int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 int output_count, int input_count,
Takashi Iwai5bed9132018-07-17 22:32:52 +02001549 struct snd_rawmidi **rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001551 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001553 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 .dev_free = snd_rawmidi_dev_free,
1555 .dev_register = snd_rawmidi_dev_register,
1556 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 };
1558
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001559 if (snd_BUG_ON(!card))
1560 return -ENXIO;
1561 if (rrawmidi)
1562 *rrawmidi = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001563 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +01001564 if (!rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return -ENOMEM;
1566 rmidi->card = card;
1567 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001568 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001570 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1571 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (id != NULL)
1574 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwaiaee50122015-01-29 17:55:52 +01001575
1576 snd_device_initialize(&rmidi->dev, card);
1577 rmidi->dev.release = release_rawmidi_device;
1578 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1579
Takashi Iwai5bed9132018-07-17 22:32:52 +02001580 err = snd_rawmidi_alloc_substreams(rmidi,
1581 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1582 SNDRV_RAWMIDI_STREAM_INPUT,
1583 input_count);
1584 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 snd_rawmidi_free(rmidi);
1586 return err;
1587 }
Takashi Iwai5bed9132018-07-17 22:32:52 +02001588 err = snd_rawmidi_alloc_substreams(rmidi,
1589 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1590 SNDRV_RAWMIDI_STREAM_OUTPUT,
1591 output_count);
1592 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 snd_rawmidi_free(rmidi);
1594 return err;
1595 }
Takashi Iwai5bed9132018-07-17 22:32:52 +02001596 err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops);
1597 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 snd_rawmidi_free(rmidi);
1599 return err;
1600 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001601 if (rrawmidi)
1602 *rrawmidi = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return 0;
1604}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001605EXPORT_SYMBOL(snd_rawmidi_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Takashi Iwai48c9d412005-11-17 13:56:51 +01001607static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001609 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001612 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 list_del(&substream->list);
1614 kfree(substream);
1615 }
1616}
1617
Takashi Iwai48c9d412005-11-17 13:56:51 +01001618static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001620 if (!rmidi)
1621 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +02001622
1623 snd_info_free_entry(rmidi->proc_entry);
1624 rmidi->proc_entry = NULL;
1625 mutex_lock(&register_mutex);
1626 if (rmidi->ops && rmidi->ops->dev_unregister)
1627 rmidi->ops->dev_unregister(rmidi);
1628 mutex_unlock(&register_mutex);
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1631 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1632 if (rmidi->private_free)
1633 rmidi->private_free(rmidi);
Takashi Iwaiaee50122015-01-29 17:55:52 +01001634 put_device(&rmidi->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 0;
1636}
1637
Takashi Iwai48c9d412005-11-17 13:56:51 +01001638static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001640 struct snd_rawmidi *rmidi = device->device_data;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return snd_rawmidi_free(rmidi);
1643}
1644
Takashi Iwai111b0cd2017-06-09 15:11:58 +02001645#if IS_ENABLED(CONFIG_SND_SEQUENCER)
Takashi Iwai48c9d412005-11-17 13:56:51 +01001646static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001648 struct snd_rawmidi *rmidi = device->private_data;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 rmidi->seq_dev = NULL;
1651}
1652#endif
1653
Takashi Iwai48c9d412005-11-17 13:56:51 +01001654static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001656 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001657 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001659 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1662 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001663 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001664 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001665 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return -EBUSY;
1667 }
Clemens Ladischf87135f2005-11-20 14:06:59 +01001668 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Takashi Iwai816f3182016-08-30 14:45:46 +02001669 mutex_unlock(&register_mutex);
Takashi Iwai40a4b262015-01-30 08:34:58 +01001670 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1671 rmidi->card, rmidi->device,
1672 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
Takashi Iwaiaee50122015-01-29 17:55:52 +01001673 if (err < 0) {
1674 rmidi_err(rmidi, "unable to register\n");
Takashi Iwai816f3182016-08-30 14:45:46 +02001675 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001676 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001677 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return err;
1679 }
1680 if (rmidi->ops && rmidi->ops->dev_register &&
1681 (err = rmidi->ops->dev_register(rmidi)) < 0) {
Takashi Iwai40a4b262015-01-30 08:34:58 +01001682 snd_unregister_device(&rmidi->dev);
Takashi Iwai816f3182016-08-30 14:45:46 +02001683 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001684 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001685 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return err;
1687 }
1688#ifdef CONFIG_SND_OSSEMUL
1689 rmidi->ossreg = 0;
1690 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1691 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001692 rmidi->card, 0, &snd_rawmidi_f_ops,
Takashi Iwai80d7d772014-02-04 13:51:45 +01001693 rmidi) < 0) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001694 rmidi_err(rmidi,
1695 "unable to register OSS rawmidi device %i:%i\n",
1696 rmidi->card->number, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 } else {
1698 rmidi->ossreg++;
1699#ifdef SNDRV_OSS_INFO_DEV_MIDI
1700 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1701#endif
1702 }
1703 }
1704 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1705 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001706 rmidi->card, 1, &snd_rawmidi_f_ops,
Takashi Iwai80d7d772014-02-04 13:51:45 +01001707 rmidi) < 0) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001708 rmidi_err(rmidi,
1709 "unable to register OSS rawmidi device %i:%i\n",
1710 rmidi->card->number, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 } else {
1712 rmidi->ossreg++;
1713 }
1714 }
1715#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 sprintf(name, "midi%d", rmidi->device);
1717 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1718 if (entry) {
1719 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 entry->c.text.read = snd_rawmidi_proc_info_read;
1721 if (snd_info_register(entry) < 0) {
1722 snd_info_free_entry(entry);
1723 entry = NULL;
1724 }
1725 }
1726 rmidi->proc_entry = entry;
Takashi Iwai111b0cd2017-06-09 15:11:58 +02001727#if IS_ENABLED(CONFIG_SND_SEQUENCER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1729 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1730 rmidi->seq_dev->private_data = rmidi;
1731 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1732 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1733 snd_device_register(rmidi->card, rmidi->seq_dev);
1734 }
1735 }
1736#endif
1737 return 0;
1738}
1739
Takashi Iwai48c9d412005-11-17 13:56:51 +01001740static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001742 struct snd_rawmidi *rmidi = device->device_data;
Takashi Iwai0914f792012-10-16 16:43:39 +02001743 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001745 mutex_lock(&register_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02001746 mutex_lock(&rmidi->open_mutex);
1747 wake_up(&rmidi->open_wait);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001748 list_del_init(&rmidi->list);
Takashi Iwai0914f792012-10-16 16:43:39 +02001749 for (dir = 0; dir < 2; dir++) {
1750 struct snd_rawmidi_substream *s;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001751
Takashi Iwai0914f792012-10-16 16:43:39 +02001752 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1753 if (s->runtime)
1754 wake_up(&s->runtime->sleep);
1755 }
1756 }
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758#ifdef CONFIG_SND_OSSEMUL
1759 if (rmidi->ossreg) {
1760 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1761 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1762#ifdef SNDRV_OSS_INFO_DEV_MIDI
1763 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1764#endif
1765 }
1766 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1767 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1768 rmidi->ossreg = 0;
1769 }
1770#endif /* CONFIG_SND_OSSEMUL */
Takashi Iwai40a4b262015-01-30 08:34:58 +01001771 snd_unregister_device(&rmidi->dev);
Takashi Iwai0914f792012-10-16 16:43:39 +02001772 mutex_unlock(&rmidi->open_mutex);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001773 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
1777/**
1778 * snd_rawmidi_set_ops - set the rawmidi operators
1779 * @rmidi: the rawmidi instance
1780 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1781 * @ops: the operator table
1782 *
1783 * Sets the rawmidi operators for the given stream direction.
1784 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001785void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
Takashi Iwai6ba79b82017-01-05 17:01:14 +01001786 const struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001788 struct snd_rawmidi_substream *substream;
Takashi Iwai5bed9132018-07-17 22:32:52 +02001789
Johannes Berg9244b2c2006-10-05 16:02:22 +02001790 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001793EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795/*
1796 * ENTRY functions
1797 */
1798
1799static int __init alsa_rawmidi_init(void)
1800{
1801
1802 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1803 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1804#ifdef CONFIG_SND_OSSEMUL
1805 { int i;
1806 /* check device map table */
1807 for (i = 0; i < SNDRV_CARDS; i++) {
1808 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001809 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1810 i, midi_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 midi_map[i] = 0;
1812 }
1813 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001814 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1815 i, amidi_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 amidi_map[i] = 1;
1817 }
1818 }
1819 }
1820#endif /* CONFIG_SND_OSSEMUL */
1821 return 0;
1822}
1823
1824static void __exit alsa_rawmidi_exit(void)
1825{
1826 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1827 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1828}
1829
1830module_init(alsa_rawmidi_init)
1831module_exit(alsa_rawmidi_exit)