blob: 73a07dae14153695ccc2ab9b931419ed14664a94 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/sched.h>
26#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>
Gustavo A. R. Silvac84a7642019-03-20 16:15:24 -050032#include <linux/nospec.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;
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return runtime->avail >= runtime->avail_min &&
100 (!substream->append || runtime->avail >= count);
101}
102
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200103static void snd_rawmidi_input_event_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200105 struct snd_rawmidi_runtime *runtime =
106 container_of(work, struct snd_rawmidi_runtime, event_work);
107 if (runtime->event)
108 runtime->event(runtime->substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Takashi Iwai48c9d412005-11-17 13:56:51 +0100111static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100113 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Takashi Iwaica2c0962005-09-09 14:20:23 +0200115 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return -ENOMEM;
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200117 runtime->substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 spin_lock_init(&runtime->lock);
Daniel Rosenberge57f7812017-10-31 16:55:26 -0700119 mutex_init(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 init_waitqueue_head(&runtime->sleep);
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200121 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 runtime->event = NULL;
123 runtime->buffer_size = PAGE_SIZE;
124 runtime->avail_min = 1;
125 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
126 runtime->avail = 0;
127 else
128 runtime->avail = runtime->buffer_size;
129 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
130 kfree(runtime);
131 return -ENOMEM;
132 }
133 runtime->appl_ptr = runtime->hw_ptr = 0;
134 substream->runtime = runtime;
135 return 0;
136}
137
Takashi Iwai48c9d412005-11-17 13:56:51 +0100138static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100140 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 kfree(runtime->buffer);
143 kfree(runtime);
144 substream->runtime = NULL;
145 return 0;
146}
147
Takashi Iwai48c9d412005-11-17 13:56:51 +0100148static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Takashi Iwai219df322008-11-03 08:17:05 +0100150 if (!substream->opened)
151 return;
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200152 substream->ops->trigger(substream, up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Takashi Iwai48c9d412005-11-17 13:56:51 +0100155static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Takashi Iwai219df322008-11-03 08:17:05 +0100157 if (!substream->opened)
158 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 substream->ops->trigger(substream, up);
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200160 if (!up)
161 cancel_work_sync(&substream->runtime->event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Takashi Iwai48c9d412005-11-17 13:56:51 +0100164int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100167 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 snd_rawmidi_output_trigger(substream, 0);
170 runtime->drain = 0;
171 spin_lock_irqsave(&runtime->lock, flags);
172 runtime->appl_ptr = runtime->hw_ptr = 0;
173 runtime->avail = runtime->buffer_size;
174 spin_unlock_irqrestore(&runtime->lock, flags);
175 return 0;
176}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100177EXPORT_SYMBOL(snd_rawmidi_drop_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Takashi Iwai48c9d412005-11-17 13:56:51 +0100179int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int err;
182 long timeout;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100183 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 err = 0;
186 runtime->drain = 1;
187 timeout = wait_event_interruptible_timeout(runtime->sleep,
188 (runtime->avail >= runtime->buffer_size),
189 10*HZ);
190 if (signal_pending(current))
191 err = -ERESTARTSYS;
192 if (runtime->avail < runtime->buffer_size && !timeout) {
Takashi Iwaica20d292014-02-04 18:21:39 +0100193 rmidi_warn(substream->rmidi,
194 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
195 (long)runtime->avail, (long)runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 err = -EIO;
197 }
198 runtime->drain = 0;
199 if (err != -ERESTARTSYS) {
200 /* we need wait a while to make sure that Tx FIFOs are empty */
201 if (substream->ops->drain)
202 substream->ops->drain(substream);
203 else
204 msleep(50);
205 snd_rawmidi_drop_output(substream);
206 }
207 return err;
208}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100209EXPORT_SYMBOL(snd_rawmidi_drain_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Takashi Iwai48c9d412005-11-17 13:56:51 +0100211int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100214 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 snd_rawmidi_input_trigger(substream, 0);
217 runtime->drain = 0;
218 spin_lock_irqsave(&runtime->lock, flags);
219 runtime->appl_ptr = runtime->hw_ptr = 0;
220 runtime->avail = 0;
221 spin_unlock_irqrestore(&runtime->lock, flags);
222 return 0;
223}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100224EXPORT_SYMBOL(snd_rawmidi_drain_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100226/* look for an available substream for the given stream direction;
227 * if a specific subdevice is given, try to assign it
228 */
229static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
230 int stream, int mode,
231 struct snd_rawmidi_substream **sub_ret)
232{
233 struct snd_rawmidi_substream *substream;
234 struct snd_rawmidi_str *s = &rmidi->streams[stream];
235 static unsigned int info_flags[2] = {
236 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
237 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
238 };
239
240 if (!(rmidi->info_flags & info_flags[stream]))
241 return -ENXIO;
242 if (subdevice >= 0 && subdevice >= s->substream_count)
243 return -ENODEV;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100244
245 list_for_each_entry(substream, &s->substreams, list) {
246 if (substream->opened) {
247 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
Clemens Ladisch16fb1092009-10-21 09:10:16 +0200248 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
249 !substream->append)
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100250 continue;
251 }
252 if (subdevice < 0 || subdevice == substream->number) {
253 *sub_ret = substream;
254 return 0;
255 }
256 }
257 return -EAGAIN;
258}
259
260/* open and do ref-counting for the given substream */
261static int open_substream(struct snd_rawmidi *rmidi,
262 struct snd_rawmidi_substream *substream,
263 int mode)
264{
265 int err;
266
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200267 if (substream->use_count == 0) {
268 err = snd_rawmidi_runtime_create(substream);
269 if (err < 0)
270 return err;
271 err = substream->ops->open(substream);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200272 if (err < 0) {
273 snd_rawmidi_runtime_free(substream);
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200274 return err;
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200275 }
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200276 substream->opened = 1;
Clemens Ladisch2d4b8422009-07-13 13:52:46 +0200277 substream->active_sensing = 0;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200278 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
279 substream->append = 1;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100280 substream->pid = get_pid(task_pid(current));
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200281 rmidi->streams[substream->stream].substream_opened++;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200282 }
283 substream->use_count++;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100284 return 0;
285}
286
287static void close_substream(struct snd_rawmidi *rmidi,
288 struct snd_rawmidi_substream *substream,
289 int cleanup);
290
291static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
292 struct snd_rawmidi_file *rfile)
293{
294 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
295 int err;
296
297 rfile->input = rfile->output = NULL;
298 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
299 err = assign_substream(rmidi, subdevice,
300 SNDRV_RAWMIDI_STREAM_INPUT,
301 mode, &sinput);
302 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200303 return err;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100304 }
305 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
306 err = assign_substream(rmidi, subdevice,
307 SNDRV_RAWMIDI_STREAM_OUTPUT,
308 mode, &soutput);
309 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200310 return err;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100311 }
312
313 if (sinput) {
314 err = open_substream(rmidi, sinput, mode);
315 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200316 return err;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100317 }
318 if (soutput) {
319 err = open_substream(rmidi, soutput, mode);
320 if (err < 0) {
321 if (sinput)
322 close_substream(rmidi, sinput, 0);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200323 return err;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100324 }
325 }
326
327 rfile->rmidi = rmidi;
328 rfile->input = sinput;
329 rfile->output = soutput;
330 return 0;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100331}
332
333/* called from sound/core/seq/seq_midi.c */
Clemens Ladischf87135f2005-11-20 14:06:59 +0100334int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai48c9d412005-11-17 13:56:51 +0100335 int mode, struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100337 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int err;
339
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100340 if (snd_BUG_ON(!rfile))
341 return -EINVAL;
342
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100343 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100344 rmidi = snd_rawmidi_search(card, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (rmidi == NULL) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100346 mutex_unlock(&register_mutex);
347 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 if (!try_module_get(rmidi->card->module)) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100350 mutex_unlock(&register_mutex);
351 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Takashi Iwaif9d20282009-02-11 14:55:59 +0100353 mutex_unlock(&register_mutex);
354
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100355 mutex_lock(&rmidi->open_mutex);
356 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
357 mutex_unlock(&rmidi->open_mutex);
358 if (err < 0)
359 module_put(rmidi->card->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return err;
361}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100362EXPORT_SYMBOL(snd_rawmidi_kernel_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364static int snd_rawmidi_open(struct inode *inode, struct file *file)
365{
366 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100367 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100368 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 unsigned short fflags;
370 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100371 struct snd_rawmidi *rmidi;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100372 struct snd_rawmidi_file *rawmidi_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100375 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
376 return -EINVAL; /* invalid combination */
377
Takashi Iwai02f48652010-04-13 11:49:04 +0200378 err = nonseekable_open(inode, file);
379 if (err < 0)
380 return err;
381
Clemens Ladischf1902862005-10-24 17:05:03 +0200382 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100383 rmidi = snd_lookup_minor_data(iminor(inode),
384 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200386 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100387 rmidi = snd_lookup_oss_minor_data(iminor(inode),
388 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200390 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (rmidi == NULL)
394 return -ENODEV;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100395
Takashi Iwaia0830db2012-10-16 13:05:59 +0200396 if (!try_module_get(rmidi->card->module)) {
397 snd_card_unref(rmidi->card);
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100398 return -ENXIO;
Takashi Iwaia0830db2012-10-16 13:05:59 +0200399 }
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100400
401 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 card = rmidi->card;
403 err = snd_card_file_add(card, file);
404 if (err < 0)
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100405 goto __error_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200407 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
410 if (rawmidi_file == NULL) {
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100411 err = -ENOMEM;
412 goto __error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 init_waitqueue_entry(&wait, current);
415 add_wait_queue(&rmidi->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 while (1) {
Takashi Iwai23c18d42014-02-19 14:30:29 +0100417 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100418 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (err >= 0)
420 break;
421 if (err == -EAGAIN) {
422 if (file->f_flags & O_NONBLOCK) {
423 err = -EBUSY;
424 break;
425 }
426 } else
427 break;
428 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100429 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100431 mutex_lock(&rmidi->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +0200432 if (rmidi->card->shutdown) {
433 err = -ENODEV;
434 break;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (signal_pending(current)) {
437 err = -ERESTARTSYS;
438 break;
439 }
440 }
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100441 remove_wait_queue(&rmidi->open_wait, &wait);
442 if (err < 0) {
443 kfree(rawmidi_file);
444 goto __error;
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446#ifdef CONFIG_SND_OSSEMUL
447 if (rawmidi_file->input && rawmidi_file->input->runtime)
448 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
449 if (rawmidi_file->output && rawmidi_file->output->runtime)
450 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
451#endif
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100452 file->private_data = rawmidi_file;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100453 mutex_unlock(&rmidi->open_mutex);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200454 snd_card_unref(rmidi->card);
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100455 return 0;
456
457 __error:
458 snd_card_file_remove(card, file);
459 __error_card:
460 mutex_unlock(&rmidi->open_mutex);
461 module_put(rmidi->card->module);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200462 snd_card_unref(rmidi->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return err;
464}
465
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100466static void close_substream(struct snd_rawmidi *rmidi,
467 struct snd_rawmidi_substream *substream,
468 int cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100470 if (--substream->use_count)
471 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100473 if (cleanup) {
474 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
475 snd_rawmidi_input_trigger(substream, 0);
476 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (substream->active_sensing) {
478 unsigned char buf = 0xfe;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100479 /* sending single active sensing message
480 * to shut the device up
481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 snd_rawmidi_kernel_write(substream, &buf, 1);
483 }
484 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
485 snd_rawmidi_output_trigger(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100488 substream->ops->close(substream);
489 if (substream->runtime->private_free)
490 substream->runtime->private_free(substream);
491 snd_rawmidi_runtime_free(substream);
492 substream->opened = 0;
493 substream->append = 0;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100494 put_pid(substream->pid);
495 substream->pid = NULL;
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200496 rmidi->streams[substream->stream].substream_opened--;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100497}
498
499static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
500{
501 struct snd_rawmidi *rmidi;
502
503 rmidi = rfile->rmidi;
504 mutex_lock(&rmidi->open_mutex);
505 if (rfile->input) {
506 close_substream(rmidi, rfile->input, 1);
507 rfile->input = NULL;
508 }
509 if (rfile->output) {
510 close_substream(rmidi, rfile->output, 1);
511 rfile->output = NULL;
512 }
513 rfile->rmidi = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100514 mutex_unlock(&rmidi->open_mutex);
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100515 wake_up(&rmidi->open_wait);
516}
517
518/* called from sound/core/seq/seq_midi.c */
519int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
520{
521 struct snd_rawmidi *rmidi;
522
523 if (snd_BUG_ON(!rfile))
524 return -ENXIO;
525
526 rmidi = rfile->rmidi;
527 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 module_put(rmidi->card->module);
529 return 0;
530}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100531EXPORT_SYMBOL(snd_rawmidi_kernel_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533static int snd_rawmidi_release(struct inode *inode, struct file *file)
534{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100535 struct snd_rawmidi_file *rfile;
536 struct snd_rawmidi *rmidi;
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200537 struct module *module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 rfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 rmidi = rfile->rmidi;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100541 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 kfree(rfile);
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200543 module = rmidi->card->module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 snd_card_file_remove(rmidi->card, file);
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200545 module_put(module);
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Adrian Bunk18612042005-11-23 13:14:50 +0100549static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
550 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100552 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 if (substream == NULL)
555 return -ENODEV;
556 rmidi = substream->rmidi;
557 memset(info, 0, sizeof(*info));
558 info->card = rmidi->card->number;
559 info->device = rmidi->device;
560 info->subdevice = substream->number;
561 info->stream = substream->stream;
562 info->flags = rmidi->info_flags;
563 strcpy(info->id, rmidi->id);
564 strcpy(info->name, rmidi->name);
565 strcpy(info->subname, substream->name);
566 info->subdevices_count = substream->pstr->substream_count;
567 info->subdevices_avail = (substream->pstr->substream_count -
568 substream->pstr->substream_opened);
569 return 0;
570}
571
Takashi Iwai48c9d412005-11-17 13:56:51 +0100572static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
573 struct snd_rawmidi_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100575 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int err;
577 if ((err = snd_rawmidi_info(substream, &info)) < 0)
578 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100579 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return -EFAULT;
581 return 0;
582}
583
Takashi Iwaicec92442017-12-14 16:44:12 +0100584static int __snd_rawmidi_info_select(struct snd_card *card,
585 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100587 struct snd_rawmidi *rmidi;
588 struct snd_rawmidi_str *pstr;
589 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100590
Clemens Ladischf87135f2005-11-20 14:06:59 +0100591 rmidi = snd_rawmidi_search(card, info->device);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100592 if (!rmidi)
593 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (info->stream < 0 || info->stream > 1)
595 return -EINVAL;
Gustavo A. R. Silvac84a7642019-03-20 16:15:24 -0500596 info->stream = array_index_nospec(info->stream, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 pstr = &rmidi->streams[info->stream];
598 if (pstr->substream_count == 0)
599 return -ENOENT;
600 if (info->subdevice >= pstr->substream_count)
601 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200602 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if ((unsigned int)substream->number == info->subdevice)
604 return snd_rawmidi_info(substream, info);
605 }
606 return -ENXIO;
607}
Takashi Iwaicec92442017-12-14 16:44:12 +0100608
609int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
610{
611 int ret;
612
613 mutex_lock(&register_mutex);
614 ret = __snd_rawmidi_info_select(card, info);
615 mutex_unlock(&register_mutex);
616 return ret;
617}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100618EXPORT_SYMBOL(snd_rawmidi_info_select);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Takashi Iwai48c9d412005-11-17 13:56:51 +0100620static int snd_rawmidi_info_select_user(struct snd_card *card,
621 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100624 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (get_user(info.device, &_info->device))
626 return -EFAULT;
627 if (get_user(info.stream, &_info->stream))
628 return -EFAULT;
629 if (get_user(info.subdevice, &_info->subdevice))
630 return -EFAULT;
631 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
632 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100633 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return -EFAULT;
635 return 0;
636}
637
Takashi Iwai48c9d412005-11-17 13:56:51 +0100638int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
639 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Takashi Iwaic4f094d2018-07-17 17:26:43 +0200641 char *newbuf, *oldbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100642 struct snd_rawmidi_runtime *runtime = substream->runtime;
Daniel Rosenberge57f7812017-10-31 16:55:26 -0700643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (substream->append && substream->use_count > 1)
645 return -EBUSY;
646 snd_rawmidi_drain_output(substream);
647 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
648 return -EINVAL;
649 }
650 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
651 return -EINVAL;
652 }
653 if (params->buffer_size != runtime->buffer_size) {
Takashi Iwaic4f094d2018-07-17 17:26:43 +0200654 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800655 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return -ENOMEM;
Takashi Iwaic4f094d2018-07-17 17:26:43 +0200657 spin_lock_irq(&runtime->lock);
Daniel Rosenberge57f7812017-10-31 16:55:26 -0700658 oldbuf = runtime->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 runtime->buffer = newbuf;
660 runtime->buffer_size = params->buffer_size;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100661 runtime->avail = runtime->buffer_size;
Takashi Iwaic4f094d2018-07-17 17:26:43 +0200662 runtime->appl_ptr = runtime->hw_ptr = 0;
663 spin_unlock_irq(&runtime->lock);
664 kfree(oldbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 runtime->avail_min = params->avail_min;
667 substream->active_sensing = !params->no_active_sensing;
668 return 0;
669}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100670EXPORT_SYMBOL(snd_rawmidi_output_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Takashi Iwai48c9d412005-11-17 13:56:51 +0100672int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
673 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Takashi Iwaic4f094d2018-07-17 17:26:43 +0200675 char *newbuf, *oldbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100676 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 snd_rawmidi_drain_input(substream);
679 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
680 return -EINVAL;
681 }
682 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
683 return -EINVAL;
684 }
685 if (params->buffer_size != runtime->buffer_size) {
Takashi Iwaic4f094d2018-07-17 17:26:43 +0200686 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800687 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return -ENOMEM;
Takashi Iwaic4f094d2018-07-17 17:26:43 +0200689 spin_lock_irq(&runtime->lock);
Daniel Rosenberge57f7812017-10-31 16:55:26 -0700690 oldbuf = runtime->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 runtime->buffer = newbuf;
692 runtime->buffer_size = params->buffer_size;
Takashi Iwaic4f094d2018-07-17 17:26:43 +0200693 runtime->appl_ptr = runtime->hw_ptr = 0;
694 spin_unlock_irq(&runtime->lock);
695 kfree(oldbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697 runtime->avail_min = params->avail_min;
698 return 0;
699}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100700EXPORT_SYMBOL(snd_rawmidi_input_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Takashi Iwai48c9d412005-11-17 13:56:51 +0100702static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
703 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100705 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 memset(status, 0, sizeof(*status));
708 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
709 spin_lock_irq(&runtime->lock);
710 status->avail = runtime->avail;
711 spin_unlock_irq(&runtime->lock);
712 return 0;
713}
714
Takashi Iwai48c9d412005-11-17 13:56:51 +0100715static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
716 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100718 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 memset(status, 0, sizeof(*status));
721 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
722 spin_lock_irq(&runtime->lock);
723 status->avail = runtime->avail;
724 status->xruns = runtime->xruns;
725 runtime->xruns = 0;
726 spin_unlock_irq(&runtime->lock);
727 return 0;
728}
729
730static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
731{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100732 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 void __user *argp = (void __user *)arg;
734
735 rfile = file->private_data;
736 if (((cmd >> 8) & 0xff) != 'W')
737 return -ENOTTY;
738 switch (cmd) {
739 case SNDRV_RAWMIDI_IOCTL_PVERSION:
740 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
741 case SNDRV_RAWMIDI_IOCTL_INFO:
742 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100743 int stream;
744 struct snd_rawmidi_info __user *info = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (get_user(stream, &info->stream))
746 return -EFAULT;
747 switch (stream) {
748 case SNDRV_RAWMIDI_STREAM_INPUT:
749 return snd_rawmidi_info_user(rfile->input, info);
750 case SNDRV_RAWMIDI_STREAM_OUTPUT:
751 return snd_rawmidi_info_user(rfile->output, info);
752 default:
753 return -EINVAL;
754 }
755 }
756 case SNDRV_RAWMIDI_IOCTL_PARAMS:
757 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100758 struct snd_rawmidi_params params;
759 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -EFAULT;
761 switch (params.stream) {
762 case SNDRV_RAWMIDI_STREAM_OUTPUT:
763 if (rfile->output == NULL)
764 return -EINVAL;
765 return snd_rawmidi_output_params(rfile->output, &params);
766 case SNDRV_RAWMIDI_STREAM_INPUT:
767 if (rfile->input == NULL)
768 return -EINVAL;
769 return snd_rawmidi_input_params(rfile->input, &params);
770 default:
771 return -EINVAL;
772 }
773 }
774 case SNDRV_RAWMIDI_IOCTL_STATUS:
775 {
776 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100777 struct snd_rawmidi_status status;
778 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -EFAULT;
780 switch (status.stream) {
781 case SNDRV_RAWMIDI_STREAM_OUTPUT:
782 if (rfile->output == NULL)
783 return -EINVAL;
784 err = snd_rawmidi_output_status(rfile->output, &status);
785 break;
786 case SNDRV_RAWMIDI_STREAM_INPUT:
787 if (rfile->input == NULL)
788 return -EINVAL;
789 err = snd_rawmidi_input_status(rfile->input, &status);
790 break;
791 default:
792 return -EINVAL;
793 }
794 if (err < 0)
795 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100796 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -EFAULT;
798 return 0;
799 }
800 case SNDRV_RAWMIDI_IOCTL_DROP:
801 {
802 int val;
803 if (get_user(val, (int __user *) argp))
804 return -EFAULT;
805 switch (val) {
806 case SNDRV_RAWMIDI_STREAM_OUTPUT:
807 if (rfile->output == NULL)
808 return -EINVAL;
809 return snd_rawmidi_drop_output(rfile->output);
810 default:
811 return -EINVAL;
812 }
813 }
814 case SNDRV_RAWMIDI_IOCTL_DRAIN:
815 {
816 int val;
817 if (get_user(val, (int __user *) argp))
818 return -EFAULT;
819 switch (val) {
820 case SNDRV_RAWMIDI_STREAM_OUTPUT:
821 if (rfile->output == NULL)
822 return -EINVAL;
823 return snd_rawmidi_drain_output(rfile->output);
824 case SNDRV_RAWMIDI_STREAM_INPUT:
825 if (rfile->input == NULL)
826 return -EINVAL;
827 return snd_rawmidi_drain_input(rfile->input);
828 default:
829 return -EINVAL;
830 }
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 default:
Takashi Iwaica20d292014-02-04 18:21:39 +0100833 rmidi_dbg(rfile->rmidi,
834 "rawmidi: unknown command = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 return -ENOTTY;
837}
838
Takashi Iwai48c9d412005-11-17 13:56:51 +0100839static int snd_rawmidi_control_ioctl(struct snd_card *card,
840 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 unsigned int cmd,
842 unsigned long arg)
843{
844 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 switch (cmd) {
847 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
848 {
849 int device;
850
851 if (get_user(device, (int __user *)argp))
852 return -EFAULT;
Dan Carpentera7a13d02010-09-09 00:11:41 +0200853 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
854 device = SNDRV_RAWMIDI_DEVICES - 1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100855 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 device = device < 0 ? 0 : device + 1;
857 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100858 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 break;
860 device++;
861 }
862 if (device == SNDRV_RAWMIDI_DEVICES)
863 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100864 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (put_user(device, (int __user *)argp))
866 return -EFAULT;
867 return 0;
868 }
869 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
870 {
871 int val;
872
873 if (get_user(val, (int __user *)argp))
874 return -EFAULT;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100875 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 0;
877 }
878 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
879 return snd_rawmidi_info_select_user(card, argp);
880 }
881 return -ENOIOCTLCMD;
882}
883
884/**
885 * snd_rawmidi_receive - receive the input data from the device
886 * @substream: the rawmidi substream
887 * @buffer: the buffer pointer
888 * @count: the data size to read
889 *
890 * Reads the data from the internal buffer.
891 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100892 * Return: The size of read data, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100894int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
895 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 unsigned long flags;
898 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100899 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Takashi Iwai219df322008-11-03 08:17:05 +0100901 if (!substream->opened)
902 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +0100904 rmidi_dbg(substream->rmidi,
905 "snd_rawmidi_receive: input is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return -EINVAL;
907 }
908 spin_lock_irqsave(&runtime->lock, flags);
909 if (count == 1) { /* special case, faster code */
910 substream->bytes++;
911 if (runtime->avail < runtime->buffer_size) {
912 runtime->buffer[runtime->hw_ptr++] = buffer[0];
913 runtime->hw_ptr %= runtime->buffer_size;
914 runtime->avail++;
915 result++;
916 } else {
917 runtime->xruns++;
918 }
919 } else {
920 substream->bytes += count;
921 count1 = runtime->buffer_size - runtime->hw_ptr;
922 if (count1 > count)
923 count1 = count;
924 if (count1 > (int)(runtime->buffer_size - runtime->avail))
925 count1 = runtime->buffer_size - runtime->avail;
926 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
927 runtime->hw_ptr += count1;
928 runtime->hw_ptr %= runtime->buffer_size;
929 runtime->avail += count1;
930 count -= count1;
931 result += count1;
932 if (count > 0) {
933 buffer += count1;
934 count1 = count;
935 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
936 count1 = runtime->buffer_size - runtime->avail;
937 runtime->xruns += count - count1;
938 }
939 if (count1 > 0) {
940 memcpy(runtime->buffer, buffer, count1);
941 runtime->hw_ptr = count1;
942 runtime->avail += count1;
943 result += count1;
944 }
945 }
946 }
947 if (result > 0) {
948 if (runtime->event)
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200949 schedule_work(&runtime->event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 else if (snd_rawmidi_ready(substream))
951 wake_up(&runtime->sleep);
952 }
953 spin_unlock_irqrestore(&runtime->lock, flags);
954 return result;
955}
Takashi Iwai6776a5d2014-02-27 16:00:17 +0100956EXPORT_SYMBOL(snd_rawmidi_receive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Takashi Iwai48c9d412005-11-17 13:56:51 +0100958static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100959 unsigned char __user *userbuf,
960 unsigned char *kernelbuf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 unsigned long flags;
963 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100964 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai81f57752016-02-03 14:41:22 +0100965 unsigned long appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Daniel Rosenberge57f7812017-10-31 16:55:26 -0700967 if (userbuf)
968 mutex_lock(&runtime->realloc_mutex);
Dennis Cagle0377c0632018-04-10 10:47:20 -0700969 spin_lock_irqsave(&runtime->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 while (count > 0 && runtime->avail) {
971 count1 = runtime->buffer_size - runtime->appl_ptr;
972 if (count1 > count)
973 count1 = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (count1 > (int)runtime->avail)
975 count1 = runtime->avail;
Takashi Iwai81f57752016-02-03 14:41:22 +0100976
977 /* update runtime->appl_ptr before unlocking for userbuf */
978 appl_ptr = runtime->appl_ptr;
979 runtime->appl_ptr += count1;
980 runtime->appl_ptr %= runtime->buffer_size;
981 runtime->avail -= count1;
982
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100983 if (kernelbuf)
Takashi Iwai81f57752016-02-03 14:41:22 +0100984 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100985 if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 spin_unlock_irqrestore(&runtime->lock, flags);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100987 if (copy_to_user(userbuf + result,
Takashi Iwai81f57752016-02-03 14:41:22 +0100988 runtime->buffer + appl_ptr, count1)) {
Daniel Rosenberge57f7812017-10-31 16:55:26 -0700989 mutex_unlock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return result > 0 ? result : -EFAULT;
991 }
992 spin_lock_irqsave(&runtime->lock, flags);
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 result += count1;
995 count -= count1;
996 }
Takashi Iwai81f57752016-02-03 14:41:22 +0100997 spin_unlock_irqrestore(&runtime->lock, flags);
Daniel Rosenberge57f7812017-10-31 16:55:26 -0700998 if (userbuf)
999 mutex_unlock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return result;
1001}
1002
Takashi Iwai48c9d412005-11-17 13:56:51 +01001003long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
1004 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 snd_rawmidi_input_trigger(substream, 1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001007 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001009EXPORT_SYMBOL(snd_rawmidi_kernel_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Takashi Iwai48c9d412005-11-17 13:56:51 +01001011static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1012 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 long result;
1015 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001016 struct snd_rawmidi_file *rfile;
1017 struct snd_rawmidi_substream *substream;
1018 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 rfile = file->private_data;
1021 substream = rfile->input;
1022 if (substream == NULL)
1023 return -EIO;
1024 runtime = substream->runtime;
1025 snd_rawmidi_input_trigger(substream, 1);
1026 result = 0;
1027 while (count > 0) {
1028 spin_lock_irq(&runtime->lock);
1029 while (!snd_rawmidi_ready(substream)) {
1030 wait_queue_t wait;
1031 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1032 spin_unlock_irq(&runtime->lock);
1033 return result > 0 ? result : -EAGAIN;
1034 }
1035 init_waitqueue_entry(&wait, current);
1036 add_wait_queue(&runtime->sleep, &wait);
1037 set_current_state(TASK_INTERRUPTIBLE);
1038 spin_unlock_irq(&runtime->lock);
1039 schedule();
1040 remove_wait_queue(&runtime->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001041 if (rfile->rmidi->card->shutdown)
1042 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (signal_pending(current))
1044 return result > 0 ? result : -ERESTARTSYS;
1045 if (!runtime->avail)
1046 return result > 0 ? result : -EIO;
1047 spin_lock_irq(&runtime->lock);
1048 }
1049 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001050 count1 = snd_rawmidi_kernel_read1(substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001051 (unsigned char __user *)buf,
1052 NULL/*kernelbuf*/,
1053 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (count1 < 0)
1055 return result > 0 ? result : count1;
1056 result += count1;
1057 buf += count1;
1058 count -= count1;
1059 }
1060 return result;
1061}
1062
1063/**
1064 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1065 * @substream: the rawmidi substream
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001066 *
1067 * Return: 1 if the internal output buffer is empty, 0 if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001069int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001071 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 int result;
1073 unsigned long flags;
1074
1075 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001076 rmidi_dbg(substream->rmidi,
1077 "snd_rawmidi_transmit_empty: output is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return 1;
1079 }
1080 spin_lock_irqsave(&runtime->lock, flags);
1081 result = runtime->avail >= runtime->buffer_size;
1082 spin_unlock_irqrestore(&runtime->lock, flags);
1083 return result;
1084}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001085EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087/**
Takashi Iwai06ab3002016-01-31 11:57:41 +01001088 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 * @substream: the rawmidi substream
1090 * @buffer: the buffer pointer
1091 * @count: data size to transfer
1092 *
Takashi Iwai06ab3002016-01-31 11:57:41 +01001093 * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 */
Takashi Iwai06ab3002016-01-31 11:57:41 +01001095int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001096 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001099 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001102 rmidi_dbg(substream->rmidi,
1103 "snd_rawmidi_transmit_peek: output is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return -EINVAL;
1105 }
1106 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (runtime->avail >= runtime->buffer_size) {
1108 /* warning: lowlevel layer MUST trigger down the hardware */
1109 goto __skip;
1110 }
1111 if (count == 1) { /* special case, faster code */
1112 *buffer = runtime->buffer[runtime->hw_ptr];
1113 result++;
1114 } else {
1115 count1 = runtime->buffer_size - runtime->hw_ptr;
1116 if (count1 > count)
1117 count1 = count;
1118 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1119 count1 = runtime->buffer_size - runtime->avail;
1120 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1121 count -= count1;
1122 result += count1;
1123 if (count > 0) {
1124 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1125 count = runtime->buffer_size - runtime->avail - count1;
1126 memcpy(buffer + count1, runtime->buffer, count);
1127 result += count;
1128 }
1129 }
1130 __skip:
Takashi Iwai06ab3002016-01-31 11:57:41 +01001131 return result;
1132}
1133EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1134
1135/**
1136 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1137 * @substream: the rawmidi substream
1138 * @buffer: the buffer pointer
1139 * @count: data size to transfer
1140 *
1141 * Copies data from the internal output buffer to the given buffer.
1142 *
1143 * Call this in the interrupt handler when the midi output is ready,
1144 * and call snd_rawmidi_transmit_ack() after the transmission is
1145 * finished.
1146 *
1147 * Return: The size of copied data, or a negative error code on failure.
1148 */
1149int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1150 unsigned char *buffer, int count)
1151{
1152 struct snd_rawmidi_runtime *runtime = substream->runtime;
1153 int result;
1154 unsigned long flags;
1155
1156 spin_lock_irqsave(&runtime->lock, flags);
1157 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 spin_unlock_irqrestore(&runtime->lock, flags);
1159 return result;
1160}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001161EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163/**
Takashi Iwai06ab3002016-01-31 11:57:41 +01001164 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1165 * @substream: the rawmidi substream
1166 * @count: the transferred count
1167 *
1168 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1169 */
1170int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1171{
1172 struct snd_rawmidi_runtime *runtime = substream->runtime;
1173
1174 if (runtime->buffer == NULL) {
1175 rmidi_dbg(substream->rmidi,
1176 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1177 return -EINVAL;
1178 }
1179 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1180 runtime->hw_ptr += count;
1181 runtime->hw_ptr %= runtime->buffer_size;
1182 runtime->avail += count;
1183 substream->bytes += count;
1184 if (count > 0) {
1185 if (runtime->drain || snd_rawmidi_ready(substream))
1186 wake_up(&runtime->sleep);
1187 }
1188 return count;
1189}
1190EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1191
1192/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 * snd_rawmidi_transmit_ack - acknowledge the transmission
1194 * @substream: the rawmidi substream
Masanari Iida0a114582014-02-09 00:47:36 +09001195 * @count: the transferred count
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 *
1197 * Advances the hardware pointer for the internal output buffer with
1198 * the given size and updates the condition.
1199 * Call after the transmission is finished.
1200 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001201 * Return: The advanced size if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001203int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001205 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai06ab3002016-01-31 11:57:41 +01001206 int result;
1207 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai06ab3002016-01-31 11:57:41 +01001210 result = __snd_rawmidi_transmit_ack(substream, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwai06ab3002016-01-31 11:57:41 +01001212 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001214EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216/**
1217 * snd_rawmidi_transmit - copy from the buffer to the device
1218 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001219 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 * @count: the data size to transfer
1221 *
1222 * Copies data from the buffer to the device and advances the pointer.
1223 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001224 * Return: The copied size if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001226int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1227 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Takashi Iwai06ab3002016-01-31 11:57:41 +01001229 struct snd_rawmidi_runtime *runtime = substream->runtime;
1230 int result;
1231 unsigned long flags;
1232
1233 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai219df322008-11-03 08:17:05 +01001234 if (!substream->opened)
Takashi Iwai06ab3002016-01-31 11:57:41 +01001235 result = -EBADFD;
1236 else {
1237 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1238 if (count <= 0)
1239 result = count;
1240 else
1241 result = __snd_rawmidi_transmit_ack(substream, count);
1242 }
1243 spin_unlock_irqrestore(&runtime->lock, flags);
1244 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001246EXPORT_SYMBOL(snd_rawmidi_transmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Takashi Iwai48c9d412005-11-17 13:56:51 +01001248static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001249 const unsigned char __user *userbuf,
1250 const unsigned char *kernelbuf,
1251 long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 unsigned long flags;
1254 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001255 struct snd_rawmidi_runtime *runtime = substream->runtime;
Takashi Iwai81f57752016-02-03 14:41:22 +01001256 unsigned long appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Takashi Iwaicc85f7a2016-02-01 12:04:55 +01001258 if (!kernelbuf && !userbuf)
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001259 return -EINVAL;
1260 if (snd_BUG_ON(!runtime->buffer))
1261 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 result = 0;
Daniel Rosenberge57f7812017-10-31 16:55:26 -07001264 if (userbuf)
1265 mutex_lock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 spin_lock_irqsave(&runtime->lock, flags);
1267 if (substream->append) {
1268 if ((long)runtime->avail < count) {
1269 spin_unlock_irqrestore(&runtime->lock, flags);
Daniel Rosenberge57f7812017-10-31 16:55:26 -07001270 if (userbuf)
1271 mutex_unlock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return -EAGAIN;
1273 }
1274 }
1275 while (count > 0 && runtime->avail > 0) {
1276 count1 = runtime->buffer_size - runtime->appl_ptr;
1277 if (count1 > count)
1278 count1 = count;
1279 if (count1 > (long)runtime->avail)
1280 count1 = runtime->avail;
Takashi Iwai81f57752016-02-03 14:41:22 +01001281
1282 /* update runtime->appl_ptr before unlocking for userbuf */
1283 appl_ptr = runtime->appl_ptr;
1284 runtime->appl_ptr += count1;
1285 runtime->appl_ptr %= runtime->buffer_size;
1286 runtime->avail -= count1;
1287
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001288 if (kernelbuf)
Takashi Iwai81f57752016-02-03 14:41:22 +01001289 memcpy(runtime->buffer + appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001290 kernelbuf + result, count1);
1291 else if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 spin_unlock_irqrestore(&runtime->lock, flags);
Takashi Iwai81f57752016-02-03 14:41:22 +01001293 if (copy_from_user(runtime->buffer + appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001294 userbuf + result, count1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 spin_lock_irqsave(&runtime->lock, flags);
1296 result = result > 0 ? result : -EFAULT;
1297 goto __end;
1298 }
1299 spin_lock_irqsave(&runtime->lock, flags);
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 result += count1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 count -= count1;
1303 }
1304 __end:
1305 count1 = runtime->avail < runtime->buffer_size;
1306 spin_unlock_irqrestore(&runtime->lock, flags);
Daniel Rosenberge57f7812017-10-31 16:55:26 -07001307 if (userbuf)
1308 mutex_unlock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (count1)
1310 snd_rawmidi_output_trigger(substream, 1);
1311 return result;
1312}
1313
Takashi Iwai48c9d412005-11-17 13:56:51 +01001314long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1315 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001317 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001319EXPORT_SYMBOL(snd_rawmidi_kernel_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Takashi Iwai48c9d412005-11-17 13:56:51 +01001321static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1322 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 long result, timeout;
1325 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001326 struct snd_rawmidi_file *rfile;
1327 struct snd_rawmidi_runtime *runtime;
1328 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 rfile = file->private_data;
1331 substream = rfile->output;
1332 runtime = substream->runtime;
1333 /* we cannot put an atomic message to our buffer */
1334 if (substream->append && count > runtime->buffer_size)
1335 return -EIO;
1336 result = 0;
1337 while (count > 0) {
1338 spin_lock_irq(&runtime->lock);
1339 while (!snd_rawmidi_ready_append(substream, count)) {
1340 wait_queue_t wait;
1341 if (file->f_flags & O_NONBLOCK) {
1342 spin_unlock_irq(&runtime->lock);
1343 return result > 0 ? result : -EAGAIN;
1344 }
1345 init_waitqueue_entry(&wait, current);
1346 add_wait_queue(&runtime->sleep, &wait);
1347 set_current_state(TASK_INTERRUPTIBLE);
1348 spin_unlock_irq(&runtime->lock);
1349 timeout = schedule_timeout(30 * HZ);
1350 remove_wait_queue(&runtime->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001351 if (rfile->rmidi->card->shutdown)
1352 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (signal_pending(current))
1354 return result > 0 ? result : -ERESTARTSYS;
1355 if (!runtime->avail && !timeout)
1356 return result > 0 ? result : -EIO;
1357 spin_lock_irq(&runtime->lock);
1358 }
1359 spin_unlock_irq(&runtime->lock);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001360 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (count1 < 0)
1362 return result > 0 ? result : count1;
1363 result += count1;
1364 buf += count1;
1365 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1366 break;
1367 count -= count1;
1368 }
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001369 if (file->f_flags & O_DSYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 spin_lock_irq(&runtime->lock);
1371 while (runtime->avail != runtime->buffer_size) {
1372 wait_queue_t wait;
1373 unsigned int last_avail = runtime->avail;
1374 init_waitqueue_entry(&wait, current);
1375 add_wait_queue(&runtime->sleep, &wait);
1376 set_current_state(TASK_INTERRUPTIBLE);
1377 spin_unlock_irq(&runtime->lock);
1378 timeout = schedule_timeout(30 * HZ);
1379 remove_wait_queue(&runtime->sleep, &wait);
1380 if (signal_pending(current))
1381 return result > 0 ? result : -ERESTARTSYS;
1382 if (runtime->avail == last_avail && !timeout)
1383 return result > 0 ? result : -EIO;
1384 spin_lock_irq(&runtime->lock);
1385 }
1386 spin_unlock_irq(&runtime->lock);
1387 }
1388 return result;
1389}
1390
1391static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1392{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001393 struct snd_rawmidi_file *rfile;
1394 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 unsigned int mask;
1396
1397 rfile = file->private_data;
1398 if (rfile->input != NULL) {
1399 runtime = rfile->input->runtime;
1400 snd_rawmidi_input_trigger(rfile->input, 1);
1401 poll_wait(file, &runtime->sleep, wait);
1402 }
1403 if (rfile->output != NULL) {
1404 runtime = rfile->output->runtime;
1405 poll_wait(file, &runtime->sleep, wait);
1406 }
1407 mask = 0;
1408 if (rfile->input != NULL) {
1409 if (snd_rawmidi_ready(rfile->input))
1410 mask |= POLLIN | POLLRDNORM;
1411 }
1412 if (rfile->output != NULL) {
1413 if (snd_rawmidi_ready(rfile->output))
1414 mask |= POLLOUT | POLLWRNORM;
1415 }
1416 return mask;
1417}
1418
1419/*
1420 */
1421#ifdef CONFIG_COMPAT
1422#include "rawmidi_compat.c"
1423#else
1424#define snd_rawmidi_ioctl_compat NULL
1425#endif
1426
1427/*
1428
1429 */
1430
Takashi Iwai48c9d412005-11-17 13:56:51 +01001431static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1432 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001434 struct snd_rawmidi *rmidi;
1435 struct snd_rawmidi_substream *substream;
1436 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 rmidi = entry->private_data;
1439 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001440 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001442 list_for_each_entry(substream,
1443 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1444 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 snd_iprintf(buffer,
1446 "Output %d\n"
1447 " Tx bytes : %lu\n",
1448 substream->number,
1449 (unsigned long) substream->bytes);
1450 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001451 snd_iprintf(buffer,
1452 " Owner PID : %d\n",
1453 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 runtime = substream->runtime;
1455 snd_iprintf(buffer,
1456 " Mode : %s\n"
1457 " Buffer size : %lu\n"
1458 " Avail : %lu\n",
1459 runtime->oss ? "OSS compatible" : "native",
1460 (unsigned long) runtime->buffer_size,
1461 (unsigned long) runtime->avail);
1462 }
1463 }
1464 }
1465 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001466 list_for_each_entry(substream,
1467 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1468 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 snd_iprintf(buffer,
1470 "Input %d\n"
1471 " Rx bytes : %lu\n",
1472 substream->number,
1473 (unsigned long) substream->bytes);
1474 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001475 snd_iprintf(buffer,
1476 " Owner PID : %d\n",
1477 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 runtime = substream->runtime;
1479 snd_iprintf(buffer,
1480 " Buffer size : %lu\n"
1481 " Avail : %lu\n"
1482 " Overruns : %lu\n",
1483 (unsigned long) runtime->buffer_size,
1484 (unsigned long) runtime->avail,
1485 (unsigned long) runtime->xruns);
1486 }
1487 }
1488 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001489 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
1492/*
1493 * Register functions
1494 */
1495
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001496static const struct file_operations snd_rawmidi_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 .owner = THIS_MODULE,
1499 .read = snd_rawmidi_read,
1500 .write = snd_rawmidi_write,
1501 .open = snd_rawmidi_open,
1502 .release = snd_rawmidi_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001503 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 .poll = snd_rawmidi_poll,
1505 .unlocked_ioctl = snd_rawmidi_ioctl,
1506 .compat_ioctl = snd_rawmidi_ioctl_compat,
1507};
1508
Takashi Iwai48c9d412005-11-17 13:56:51 +01001509static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1510 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 int direction,
1512 int count)
1513{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001514 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 int idx;
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001518 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +01001519 if (!substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return -ENOMEM;
1521 substream->stream = direction;
1522 substream->number = idx;
1523 substream->rmidi = rmidi;
1524 substream->pstr = stream;
1525 list_add_tail(&substream->list, &stream->substreams);
1526 stream->substream_count++;
1527 }
1528 return 0;
1529}
1530
Takashi Iwaiaee50122015-01-29 17:55:52 +01001531static void release_rawmidi_device(struct device *dev)
1532{
1533 kfree(container_of(dev, struct snd_rawmidi, dev));
1534}
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536/**
1537 * snd_rawmidi_new - create a rawmidi instance
1538 * @card: the card instance
1539 * @id: the id string
1540 * @device: the device index
1541 * @output_count: the number of output streams
1542 * @input_count: the number of input streams
1543 * @rrawmidi: the pointer to store the new rawmidi instance
1544 *
1545 * Creates a new rawmidi instance.
1546 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1547 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001548 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001550int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 int output_count, int input_count,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001552 struct snd_rawmidi ** rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001554 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001556 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 .dev_free = snd_rawmidi_dev_free,
1558 .dev_register = snd_rawmidi_dev_register,
1559 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 };
1561
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001562 if (snd_BUG_ON(!card))
1563 return -ENXIO;
1564 if (rrawmidi)
1565 *rrawmidi = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001566 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +01001567 if (!rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return -ENOMEM;
1569 rmidi->card = card;
1570 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001571 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001573 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1574 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (id != NULL)
1577 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwaiaee50122015-01-29 17:55:52 +01001578
1579 snd_device_initialize(&rmidi->dev, card);
1580 rmidi->dev.release = release_rawmidi_device;
1581 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1582
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001583 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1584 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1585 SNDRV_RAWMIDI_STREAM_INPUT,
1586 input_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 snd_rawmidi_free(rmidi);
1588 return err;
1589 }
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001590 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1591 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1592 SNDRV_RAWMIDI_STREAM_OUTPUT,
1593 output_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 snd_rawmidi_free(rmidi);
1595 return err;
1596 }
1597 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1598 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return snd_rawmidi_free(rmidi);
1642}
1643
1644#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
Takashi Iwai48c9d412005-11-17 13:56:51 +01001645static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001647 struct snd_rawmidi *rmidi = device->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 rmidi->seq_dev = NULL;
1649}
1650#endif
1651
Takashi Iwai48c9d412005-11-17 13:56:51 +01001652static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001654 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001655 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001657 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1660 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001661 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001662 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001663 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return -EBUSY;
1665 }
Clemens Ladischf87135f2005-11-20 14:06:59 +01001666 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Takashi Iwai816f3182016-08-30 14:45:46 +02001667 mutex_unlock(&register_mutex);
Takashi Iwai40a4b262015-01-30 08:34:58 +01001668 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1669 rmidi->card, rmidi->device,
1670 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
Takashi Iwaiaee50122015-01-29 17:55:52 +01001671 if (err < 0) {
1672 rmidi_err(rmidi, "unable to register\n");
Takashi Iwai816f3182016-08-30 14:45:46 +02001673 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001674 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001675 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return err;
1677 }
1678 if (rmidi->ops && rmidi->ops->dev_register &&
1679 (err = rmidi->ops->dev_register(rmidi)) < 0) {
Takashi Iwai40a4b262015-01-30 08:34:58 +01001680 snd_unregister_device(&rmidi->dev);
Takashi Iwai816f3182016-08-30 14:45:46 +02001681 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001682 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001683 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return err;
1685 }
1686#ifdef CONFIG_SND_OSSEMUL
1687 rmidi->ossreg = 0;
1688 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1689 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001690 rmidi->card, 0, &snd_rawmidi_f_ops,
Takashi Iwai80d7d772014-02-04 13:51:45 +01001691 rmidi) < 0) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001692 rmidi_err(rmidi,
1693 "unable to register OSS rawmidi device %i:%i\n",
1694 rmidi->card->number, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 } else {
1696 rmidi->ossreg++;
1697#ifdef SNDRV_OSS_INFO_DEV_MIDI
1698 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1699#endif
1700 }
1701 }
1702 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1703 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001704 rmidi->card, 1, &snd_rawmidi_f_ops,
Takashi Iwai80d7d772014-02-04 13:51:45 +01001705 rmidi) < 0) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001706 rmidi_err(rmidi,
1707 "unable to register OSS rawmidi device %i:%i\n",
1708 rmidi->card->number, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 } else {
1710 rmidi->ossreg++;
1711 }
1712 }
1713#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 sprintf(name, "midi%d", rmidi->device);
1715 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1716 if (entry) {
1717 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 entry->c.text.read = snd_rawmidi_proc_info_read;
1719 if (snd_info_register(entry) < 0) {
1720 snd_info_free_entry(entry);
1721 entry = NULL;
1722 }
1723 }
1724 rmidi->proc_entry = entry;
1725#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1726 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1727 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1728 rmidi->seq_dev->private_data = rmidi;
1729 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1730 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1731 snd_device_register(rmidi->card, rmidi->seq_dev);
1732 }
1733 }
1734#endif
1735 return 0;
1736}
1737
Takashi Iwai48c9d412005-11-17 13:56:51 +01001738static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001740 struct snd_rawmidi *rmidi = device->device_data;
Takashi Iwai0914f792012-10-16 16:43:39 +02001741 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001743 mutex_lock(&register_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02001744 mutex_lock(&rmidi->open_mutex);
1745 wake_up(&rmidi->open_wait);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001746 list_del_init(&rmidi->list);
Takashi Iwai0914f792012-10-16 16:43:39 +02001747 for (dir = 0; dir < 2; dir++) {
1748 struct snd_rawmidi_substream *s;
1749 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1750 if (s->runtime)
1751 wake_up(&s->runtime->sleep);
1752 }
1753 }
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755#ifdef CONFIG_SND_OSSEMUL
1756 if (rmidi->ossreg) {
1757 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1758 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1759#ifdef SNDRV_OSS_INFO_DEV_MIDI
1760 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1761#endif
1762 }
1763 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1764 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1765 rmidi->ossreg = 0;
1766 }
1767#endif /* CONFIG_SND_OSSEMUL */
Takashi Iwai40a4b262015-01-30 08:34:58 +01001768 snd_unregister_device(&rmidi->dev);
Takashi Iwai0914f792012-10-16 16:43:39 +02001769 mutex_unlock(&rmidi->open_mutex);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001770 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
1774/**
1775 * snd_rawmidi_set_ops - set the rawmidi operators
1776 * @rmidi: the rawmidi instance
1777 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1778 * @ops: the operator table
1779 *
1780 * Sets the rawmidi operators for the given stream direction.
1781 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001782void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1783 struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001785 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Johannes Berg9244b2c2006-10-05 16:02:22 +02001787 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
Takashi Iwai6776a5d2014-02-27 16:00:17 +01001790EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792/*
1793 * ENTRY functions
1794 */
1795
1796static int __init alsa_rawmidi_init(void)
1797{
1798
1799 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1800 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1801#ifdef CONFIG_SND_OSSEMUL
1802 { int i;
1803 /* check device map table */
1804 for (i = 0; i < SNDRV_CARDS; i++) {
1805 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001806 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1807 i, midi_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 midi_map[i] = 0;
1809 }
1810 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001811 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1812 i, amidi_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 amidi_map[i] = 1;
1814 }
1815 }
1816 }
1817#endif /* CONFIG_SND_OSSEMUL */
1818 return 0;
1819}
1820
1821static void __exit alsa_rawmidi_exit(void)
1822{
1823 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1824 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1825}
1826
1827module_init(alsa_rawmidi_init)
1828module_exit(alsa_rawmidi_exit)