blob: 801c86133937ebc9a4ad56d3d7e7eaf6f0662d9e [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>
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...) \
60 dev_err((rmidi)->card->dev, fmt, ##args)
61#define rmidi_warn(rmidi, fmt, args...) \
62 dev_warn((rmidi)->card->dev, fmt, ##args)
63#define rmidi_dbg(rmidi, fmt, args...) \
64 dev_dbg((rmidi)->card->dev, fmt, ##args)
65
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return runtime->avail >= runtime->avail_min;
92}
93
Takashi Iwai48c9d412005-11-17 13:56:51 +010094static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
95 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Takashi Iwai48c9d412005-11-17 13:56:51 +010097 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return runtime->avail >= runtime->avail_min &&
99 (!substream->append || runtime->avail >= count);
100}
101
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200102static void snd_rawmidi_input_event_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200104 struct snd_rawmidi_runtime *runtime =
105 container_of(work, struct snd_rawmidi_runtime, event_work);
106 if (runtime->event)
107 runtime->event(runtime->substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Takashi Iwai48c9d412005-11-17 13:56:51 +0100110static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100112 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Takashi Iwaica2c0962005-09-09 14:20:23 +0200114 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return -ENOMEM;
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200116 runtime->substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 spin_lock_init(&runtime->lock);
118 init_waitqueue_head(&runtime->sleep);
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200119 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 runtime->event = NULL;
121 runtime->buffer_size = PAGE_SIZE;
122 runtime->avail_min = 1;
123 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
124 runtime->avail = 0;
125 else
126 runtime->avail = runtime->buffer_size;
127 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
128 kfree(runtime);
129 return -ENOMEM;
130 }
131 runtime->appl_ptr = runtime->hw_ptr = 0;
132 substream->runtime = runtime;
133 return 0;
134}
135
Takashi Iwai48c9d412005-11-17 13:56:51 +0100136static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100138 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 kfree(runtime->buffer);
141 kfree(runtime);
142 substream->runtime = NULL;
143 return 0;
144}
145
Takashi Iwai48c9d412005-11-17 13:56:51 +0100146static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Takashi Iwai219df322008-11-03 08:17:05 +0100148 if (!substream->opened)
149 return;
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200150 substream->ops->trigger(substream, up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Takashi Iwai48c9d412005-11-17 13:56:51 +0100153static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Takashi Iwai219df322008-11-03 08:17:05 +0100155 if (!substream->opened)
156 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 substream->ops->trigger(substream, up);
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200158 if (!up)
159 cancel_work_sync(&substream->runtime->event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Takashi Iwai48c9d412005-11-17 13:56:51 +0100162int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100165 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 snd_rawmidi_output_trigger(substream, 0);
168 runtime->drain = 0;
169 spin_lock_irqsave(&runtime->lock, flags);
170 runtime->appl_ptr = runtime->hw_ptr = 0;
171 runtime->avail = runtime->buffer_size;
172 spin_unlock_irqrestore(&runtime->lock, flags);
173 return 0;
174}
175
Takashi Iwai48c9d412005-11-17 13:56:51 +0100176int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 int err;
179 long timeout;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100180 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 err = 0;
183 runtime->drain = 1;
184 timeout = wait_event_interruptible_timeout(runtime->sleep,
185 (runtime->avail >= runtime->buffer_size),
186 10*HZ);
187 if (signal_pending(current))
188 err = -ERESTARTSYS;
189 if (runtime->avail < runtime->buffer_size && !timeout) {
Takashi Iwaica20d292014-02-04 18:21:39 +0100190 rmidi_warn(substream->rmidi,
191 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
192 (long)runtime->avail, (long)runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 err = -EIO;
194 }
195 runtime->drain = 0;
196 if (err != -ERESTARTSYS) {
197 /* we need wait a while to make sure that Tx FIFOs are empty */
198 if (substream->ops->drain)
199 substream->ops->drain(substream);
200 else
201 msleep(50);
202 snd_rawmidi_drop_output(substream);
203 }
204 return err;
205}
206
Takashi Iwai48c9d412005-11-17 13:56:51 +0100207int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100210 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 snd_rawmidi_input_trigger(substream, 0);
213 runtime->drain = 0;
214 spin_lock_irqsave(&runtime->lock, flags);
215 runtime->appl_ptr = runtime->hw_ptr = 0;
216 runtime->avail = 0;
217 spin_unlock_irqrestore(&runtime->lock, flags);
218 return 0;
219}
220
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100221/* look for an available substream for the given stream direction;
222 * if a specific subdevice is given, try to assign it
223 */
224static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
225 int stream, int mode,
226 struct snd_rawmidi_substream **sub_ret)
227{
228 struct snd_rawmidi_substream *substream;
229 struct snd_rawmidi_str *s = &rmidi->streams[stream];
230 static unsigned int info_flags[2] = {
231 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
232 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
233 };
234
235 if (!(rmidi->info_flags & info_flags[stream]))
236 return -ENXIO;
237 if (subdevice >= 0 && subdevice >= s->substream_count)
238 return -ENODEV;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100239
240 list_for_each_entry(substream, &s->substreams, list) {
241 if (substream->opened) {
242 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
Clemens Ladisch16fb1092009-10-21 09:10:16 +0200243 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
244 !substream->append)
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100245 continue;
246 }
247 if (subdevice < 0 || subdevice == substream->number) {
248 *sub_ret = substream;
249 return 0;
250 }
251 }
252 return -EAGAIN;
253}
254
255/* open and do ref-counting for the given substream */
256static int open_substream(struct snd_rawmidi *rmidi,
257 struct snd_rawmidi_substream *substream,
258 int mode)
259{
260 int err;
261
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200262 if (substream->use_count == 0) {
263 err = snd_rawmidi_runtime_create(substream);
264 if (err < 0)
265 return err;
266 err = substream->ops->open(substream);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200267 if (err < 0) {
268 snd_rawmidi_runtime_free(substream);
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200269 return err;
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200270 }
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200271 substream->opened = 1;
Clemens Ladisch2d4b8422009-07-13 13:52:46 +0200272 substream->active_sensing = 0;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200273 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
274 substream->append = 1;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100275 substream->pid = get_pid(task_pid(current));
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200276 rmidi->streams[substream->stream].substream_opened++;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200277 }
278 substream->use_count++;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100279 return 0;
280}
281
282static void close_substream(struct snd_rawmidi *rmidi,
283 struct snd_rawmidi_substream *substream,
284 int cleanup);
285
286static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
287 struct snd_rawmidi_file *rfile)
288{
289 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
290 int err;
291
292 rfile->input = rfile->output = NULL;
293 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
294 err = assign_substream(rmidi, subdevice,
295 SNDRV_RAWMIDI_STREAM_INPUT,
296 mode, &sinput);
297 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200298 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100299 }
300 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
301 err = assign_substream(rmidi, subdevice,
302 SNDRV_RAWMIDI_STREAM_OUTPUT,
303 mode, &soutput);
304 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200305 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100306 }
307
308 if (sinput) {
309 err = open_substream(rmidi, sinput, mode);
310 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200311 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100312 }
313 if (soutput) {
314 err = open_substream(rmidi, soutput, mode);
315 if (err < 0) {
316 if (sinput)
317 close_substream(rmidi, sinput, 0);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200318 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100319 }
320 }
321
322 rfile->rmidi = rmidi;
323 rfile->input = sinput;
324 rfile->output = soutput;
325 return 0;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100326}
327
328/* called from sound/core/seq/seq_midi.c */
Clemens Ladischf87135f2005-11-20 14:06:59 +0100329int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai48c9d412005-11-17 13:56:51 +0100330 int mode, struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100332 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int err;
334
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100335 if (snd_BUG_ON(!rfile))
336 return -EINVAL;
337
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100338 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100339 rmidi = snd_rawmidi_search(card, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (rmidi == NULL) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100341 mutex_unlock(&register_mutex);
342 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 if (!try_module_get(rmidi->card->module)) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100345 mutex_unlock(&register_mutex);
346 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Takashi Iwaif9d20282009-02-11 14:55:59 +0100348 mutex_unlock(&register_mutex);
349
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100350 mutex_lock(&rmidi->open_mutex);
351 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
352 mutex_unlock(&rmidi->open_mutex);
353 if (err < 0)
354 module_put(rmidi->card->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return err;
356}
357
358static int snd_rawmidi_open(struct inode *inode, struct file *file)
359{
360 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100361 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100362 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 unsigned short fflags;
364 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100365 struct snd_rawmidi *rmidi;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100366 struct snd_rawmidi_file *rawmidi_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 wait_queue_t wait;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100368 struct snd_ctl_file *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100370 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
371 return -EINVAL; /* invalid combination */
372
Takashi Iwai02f48652010-04-13 11:49:04 +0200373 err = nonseekable_open(inode, file);
374 if (err < 0)
375 return err;
376
Clemens Ladischf1902862005-10-24 17:05:03 +0200377 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100378 rmidi = snd_lookup_minor_data(iminor(inode),
379 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200381 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100382 rmidi = snd_lookup_oss_minor_data(iminor(inode),
383 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200385 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (rmidi == NULL)
389 return -ENODEV;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100390
Takashi Iwaia0830db2012-10-16 13:05:59 +0200391 if (!try_module_get(rmidi->card->module)) {
392 snd_card_unref(rmidi->card);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100393 return -ENXIO;
Takashi Iwaia0830db2012-10-16 13:05:59 +0200394 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100395
396 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 card = rmidi->card;
398 err = snd_card_file_add(card, file);
399 if (err < 0)
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100400 goto __error_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200402 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
405 if (rawmidi_file == NULL) {
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100406 err = -ENOMEM;
407 goto __error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 init_waitqueue_entry(&wait, current);
410 add_wait_queue(&rmidi->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 while (1) {
412 subdevice = -1;
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200413 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200414 list_for_each_entry(kctl, &card->ctl_files, list) {
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100415 if (kctl->pid == task_pid(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 subdevice = kctl->prefer_rawmidi_subdevice;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200417 if (subdevice != -1)
418 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 }
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200421 read_unlock(&card->ctl_files_rwlock);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100422 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (err >= 0)
424 break;
425 if (err == -EAGAIN) {
426 if (file->f_flags & O_NONBLOCK) {
427 err = -EBUSY;
428 break;
429 }
430 } else
431 break;
432 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100433 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100435 mutex_lock(&rmidi->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +0200436 if (rmidi->card->shutdown) {
437 err = -ENODEV;
438 break;
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (signal_pending(current)) {
441 err = -ERESTARTSYS;
442 break;
443 }
444 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100445 remove_wait_queue(&rmidi->open_wait, &wait);
446 if (err < 0) {
447 kfree(rawmidi_file);
448 goto __error;
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#ifdef CONFIG_SND_OSSEMUL
451 if (rawmidi_file->input && rawmidi_file->input->runtime)
452 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
453 if (rawmidi_file->output && rawmidi_file->output->runtime)
454 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
455#endif
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100456 file->private_data = rawmidi_file;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100457 mutex_unlock(&rmidi->open_mutex);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200458 snd_card_unref(rmidi->card);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100459 return 0;
460
461 __error:
462 snd_card_file_remove(card, file);
463 __error_card:
464 mutex_unlock(&rmidi->open_mutex);
465 module_put(rmidi->card->module);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200466 snd_card_unref(rmidi->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return err;
468}
469
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100470static void close_substream(struct snd_rawmidi *rmidi,
471 struct snd_rawmidi_substream *substream,
472 int cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100474 if (--substream->use_count)
475 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100477 if (cleanup) {
478 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
479 snd_rawmidi_input_trigger(substream, 0);
480 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (substream->active_sensing) {
482 unsigned char buf = 0xfe;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100483 /* sending single active sensing message
484 * to shut the device up
485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 snd_rawmidi_kernel_write(substream, &buf, 1);
487 }
488 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
489 snd_rawmidi_output_trigger(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100492 substream->ops->close(substream);
493 if (substream->runtime->private_free)
494 substream->runtime->private_free(substream);
495 snd_rawmidi_runtime_free(substream);
496 substream->opened = 0;
497 substream->append = 0;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100498 put_pid(substream->pid);
499 substream->pid = NULL;
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200500 rmidi->streams[substream->stream].substream_opened--;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100501}
502
503static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
504{
505 struct snd_rawmidi *rmidi;
506
507 rmidi = rfile->rmidi;
508 mutex_lock(&rmidi->open_mutex);
509 if (rfile->input) {
510 close_substream(rmidi, rfile->input, 1);
511 rfile->input = NULL;
512 }
513 if (rfile->output) {
514 close_substream(rmidi, rfile->output, 1);
515 rfile->output = NULL;
516 }
517 rfile->rmidi = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100518 mutex_unlock(&rmidi->open_mutex);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100519 wake_up(&rmidi->open_wait);
520}
521
522/* called from sound/core/seq/seq_midi.c */
523int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
524{
525 struct snd_rawmidi *rmidi;
526
527 if (snd_BUG_ON(!rfile))
528 return -ENXIO;
529
530 rmidi = rfile->rmidi;
531 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 module_put(rmidi->card->module);
533 return 0;
534}
535
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 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,
576 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;
580 if ((err = snd_rawmidi_info(substream, &info)) < 0)
581 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100582 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -EFAULT;
584 return 0;
585}
586
Takashi Iwai48c9d412005-11-17 13:56:51 +0100587int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100589 struct snd_rawmidi *rmidi;
590 struct snd_rawmidi_str *pstr;
591 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100592
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100593 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100594 rmidi = snd_rawmidi_search(card, info->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100595 mutex_unlock(&register_mutex);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100596 if (!rmidi)
597 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (info->stream < 0 || info->stream > 1)
599 return -EINVAL;
600 pstr = &rmidi->streams[info->stream];
601 if (pstr->substream_count == 0)
602 return -ENOENT;
603 if (info->subdevice >= pstr->substream_count)
604 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200605 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if ((unsigned int)substream->number == info->subdevice)
607 return snd_rawmidi_info(substream, info);
608 }
609 return -ENXIO;
610}
611
Takashi Iwai48c9d412005-11-17 13:56:51 +0100612static int snd_rawmidi_info_select_user(struct snd_card *card,
613 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100616 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (get_user(info.device, &_info->device))
618 return -EFAULT;
619 if (get_user(info.stream, &_info->stream))
620 return -EFAULT;
621 if (get_user(info.subdevice, &_info->subdevice))
622 return -EFAULT;
623 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
624 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100625 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return -EFAULT;
627 return 0;
628}
629
Takashi Iwai48c9d412005-11-17 13:56:51 +0100630int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
631 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100634 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 if (substream->append && substream->use_count > 1)
637 return -EBUSY;
638 snd_rawmidi_drain_output(substream);
639 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
640 return -EINVAL;
641 }
642 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
643 return -EINVAL;
644 }
645 if (params->buffer_size != runtime->buffer_size) {
Takashi Iwai3101ba02011-07-12 08:05:16 +0200646 newbuf = krealloc(runtime->buffer, params->buffer_size,
647 GFP_KERNEL);
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800648 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 runtime->buffer = newbuf;
651 runtime->buffer_size = params->buffer_size;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100652 runtime->avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 runtime->avail_min = params->avail_min;
655 substream->active_sensing = !params->no_active_sensing;
656 return 0;
657}
658
Takashi Iwai48c9d412005-11-17 13:56:51 +0100659int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
660 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100663 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 snd_rawmidi_drain_input(substream);
666 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
667 return -EINVAL;
668 }
669 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
670 return -EINVAL;
671 }
672 if (params->buffer_size != runtime->buffer_size) {
Takashi Iwai3101ba02011-07-12 08:05:16 +0200673 newbuf = krealloc(runtime->buffer, params->buffer_size,
674 GFP_KERNEL);
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800675 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 runtime->buffer = newbuf;
678 runtime->buffer_size = params->buffer_size;
679 }
680 runtime->avail_min = params->avail_min;
681 return 0;
682}
683
Takashi Iwai48c9d412005-11-17 13:56:51 +0100684static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
685 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100687 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 memset(status, 0, sizeof(*status));
690 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
691 spin_lock_irq(&runtime->lock);
692 status->avail = runtime->avail;
693 spin_unlock_irq(&runtime->lock);
694 return 0;
695}
696
Takashi Iwai48c9d412005-11-17 13:56:51 +0100697static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
698 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100700 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 memset(status, 0, sizeof(*status));
703 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
704 spin_lock_irq(&runtime->lock);
705 status->avail = runtime->avail;
706 status->xruns = runtime->xruns;
707 runtime->xruns = 0;
708 spin_unlock_irq(&runtime->lock);
709 return 0;
710}
711
712static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
713{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100714 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 void __user *argp = (void __user *)arg;
716
717 rfile = file->private_data;
718 if (((cmd >> 8) & 0xff) != 'W')
719 return -ENOTTY;
720 switch (cmd) {
721 case SNDRV_RAWMIDI_IOCTL_PVERSION:
722 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
723 case SNDRV_RAWMIDI_IOCTL_INFO:
724 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100725 int stream;
726 struct snd_rawmidi_info __user *info = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (get_user(stream, &info->stream))
728 return -EFAULT;
729 switch (stream) {
730 case SNDRV_RAWMIDI_STREAM_INPUT:
731 return snd_rawmidi_info_user(rfile->input, info);
732 case SNDRV_RAWMIDI_STREAM_OUTPUT:
733 return snd_rawmidi_info_user(rfile->output, info);
734 default:
735 return -EINVAL;
736 }
737 }
738 case SNDRV_RAWMIDI_IOCTL_PARAMS:
739 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100740 struct snd_rawmidi_params params;
741 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return -EFAULT;
743 switch (params.stream) {
744 case SNDRV_RAWMIDI_STREAM_OUTPUT:
745 if (rfile->output == NULL)
746 return -EINVAL;
747 return snd_rawmidi_output_params(rfile->output, &params);
748 case SNDRV_RAWMIDI_STREAM_INPUT:
749 if (rfile->input == NULL)
750 return -EINVAL;
751 return snd_rawmidi_input_params(rfile->input, &params);
752 default:
753 return -EINVAL;
754 }
755 }
756 case SNDRV_RAWMIDI_IOCTL_STATUS:
757 {
758 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100759 struct snd_rawmidi_status status;
760 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return -EFAULT;
762 switch (status.stream) {
763 case SNDRV_RAWMIDI_STREAM_OUTPUT:
764 if (rfile->output == NULL)
765 return -EINVAL;
766 err = snd_rawmidi_output_status(rfile->output, &status);
767 break;
768 case SNDRV_RAWMIDI_STREAM_INPUT:
769 if (rfile->input == NULL)
770 return -EINVAL;
771 err = snd_rawmidi_input_status(rfile->input, &status);
772 break;
773 default:
774 return -EINVAL;
775 }
776 if (err < 0)
777 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100778 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -EFAULT;
780 return 0;
781 }
782 case SNDRV_RAWMIDI_IOCTL_DROP:
783 {
784 int val;
785 if (get_user(val, (int __user *) argp))
786 return -EFAULT;
787 switch (val) {
788 case SNDRV_RAWMIDI_STREAM_OUTPUT:
789 if (rfile->output == NULL)
790 return -EINVAL;
791 return snd_rawmidi_drop_output(rfile->output);
792 default:
793 return -EINVAL;
794 }
795 }
796 case SNDRV_RAWMIDI_IOCTL_DRAIN:
797 {
798 int val;
799 if (get_user(val, (int __user *) argp))
800 return -EFAULT;
801 switch (val) {
802 case SNDRV_RAWMIDI_STREAM_OUTPUT:
803 if (rfile->output == NULL)
804 return -EINVAL;
805 return snd_rawmidi_drain_output(rfile->output);
806 case SNDRV_RAWMIDI_STREAM_INPUT:
807 if (rfile->input == NULL)
808 return -EINVAL;
809 return snd_rawmidi_drain_input(rfile->input);
810 default:
811 return -EINVAL;
812 }
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 default:
Takashi Iwaica20d292014-02-04 18:21:39 +0100815 rmidi_dbg(rfile->rmidi,
816 "rawmidi: unknown command = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 return -ENOTTY;
819}
820
Takashi Iwai48c9d412005-11-17 13:56:51 +0100821static int snd_rawmidi_control_ioctl(struct snd_card *card,
822 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 unsigned int cmd,
824 unsigned long arg)
825{
826 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 switch (cmd) {
829 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
830 {
831 int device;
832
833 if (get_user(device, (int __user *)argp))
834 return -EFAULT;
Dan Carpentera7a13d02010-09-09 00:11:41 +0200835 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
836 device = SNDRV_RAWMIDI_DEVICES - 1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100837 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 device = device < 0 ? 0 : device + 1;
839 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100840 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 break;
842 device++;
843 }
844 if (device == SNDRV_RAWMIDI_DEVICES)
845 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100846 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (put_user(device, (int __user *)argp))
848 return -EFAULT;
849 return 0;
850 }
851 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
852 {
853 int val;
854
855 if (get_user(val, (int __user *)argp))
856 return -EFAULT;
857 control->prefer_rawmidi_subdevice = val;
858 return 0;
859 }
860 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
861 return snd_rawmidi_info_select_user(card, argp);
862 }
863 return -ENOIOCTLCMD;
864}
865
866/**
867 * snd_rawmidi_receive - receive the input data from the device
868 * @substream: the rawmidi substream
869 * @buffer: the buffer pointer
870 * @count: the data size to read
871 *
872 * Reads the data from the internal buffer.
873 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100874 * Return: The size of read data, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100876int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
877 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 unsigned long flags;
880 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100881 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Takashi Iwai219df322008-11-03 08:17:05 +0100883 if (!substream->opened)
884 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +0100886 rmidi_dbg(substream->rmidi,
887 "snd_rawmidi_receive: input is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -EINVAL;
889 }
890 spin_lock_irqsave(&runtime->lock, flags);
891 if (count == 1) { /* special case, faster code */
892 substream->bytes++;
893 if (runtime->avail < runtime->buffer_size) {
894 runtime->buffer[runtime->hw_ptr++] = buffer[0];
895 runtime->hw_ptr %= runtime->buffer_size;
896 runtime->avail++;
897 result++;
898 } else {
899 runtime->xruns++;
900 }
901 } else {
902 substream->bytes += count;
903 count1 = runtime->buffer_size - runtime->hw_ptr;
904 if (count1 > count)
905 count1 = count;
906 if (count1 > (int)(runtime->buffer_size - runtime->avail))
907 count1 = runtime->buffer_size - runtime->avail;
908 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
909 runtime->hw_ptr += count1;
910 runtime->hw_ptr %= runtime->buffer_size;
911 runtime->avail += count1;
912 count -= count1;
913 result += count1;
914 if (count > 0) {
915 buffer += count1;
916 count1 = count;
917 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
918 count1 = runtime->buffer_size - runtime->avail;
919 runtime->xruns += count - count1;
920 }
921 if (count1 > 0) {
922 memcpy(runtime->buffer, buffer, count1);
923 runtime->hw_ptr = count1;
924 runtime->avail += count1;
925 result += count1;
926 }
927 }
928 }
929 if (result > 0) {
930 if (runtime->event)
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200931 schedule_work(&runtime->event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 else if (snd_rawmidi_ready(substream))
933 wake_up(&runtime->sleep);
934 }
935 spin_unlock_irqrestore(&runtime->lock, flags);
936 return result;
937}
938
Takashi Iwai48c9d412005-11-17 13:56:51 +0100939static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100940 unsigned char __user *userbuf,
941 unsigned char *kernelbuf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 unsigned long flags;
944 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100945 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 while (count > 0 && runtime->avail) {
948 count1 = runtime->buffer_size - runtime->appl_ptr;
949 if (count1 > count)
950 count1 = count;
951 spin_lock_irqsave(&runtime->lock, flags);
952 if (count1 > (int)runtime->avail)
953 count1 = runtime->avail;
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100954 if (kernelbuf)
955 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
956 if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 spin_unlock_irqrestore(&runtime->lock, flags);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100958 if (copy_to_user(userbuf + result,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 runtime->buffer + runtime->appl_ptr, count1)) {
960 return result > 0 ? result : -EFAULT;
961 }
962 spin_lock_irqsave(&runtime->lock, flags);
963 }
964 runtime->appl_ptr += count1;
965 runtime->appl_ptr %= runtime->buffer_size;
966 runtime->avail -= count1;
967 spin_unlock_irqrestore(&runtime->lock, flags);
968 result += count1;
969 count -= count1;
970 }
971 return result;
972}
973
Takashi Iwai48c9d412005-11-17 13:56:51 +0100974long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
975 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 snd_rawmidi_input_trigger(substream, 1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100978 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Takashi Iwai48c9d412005-11-17 13:56:51 +0100981static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
982 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 long result;
985 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100986 struct snd_rawmidi_file *rfile;
987 struct snd_rawmidi_substream *substream;
988 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 rfile = file->private_data;
991 substream = rfile->input;
992 if (substream == NULL)
993 return -EIO;
994 runtime = substream->runtime;
995 snd_rawmidi_input_trigger(substream, 1);
996 result = 0;
997 while (count > 0) {
998 spin_lock_irq(&runtime->lock);
999 while (!snd_rawmidi_ready(substream)) {
1000 wait_queue_t wait;
1001 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1002 spin_unlock_irq(&runtime->lock);
1003 return result > 0 ? result : -EAGAIN;
1004 }
1005 init_waitqueue_entry(&wait, current);
1006 add_wait_queue(&runtime->sleep, &wait);
1007 set_current_state(TASK_INTERRUPTIBLE);
1008 spin_unlock_irq(&runtime->lock);
1009 schedule();
1010 remove_wait_queue(&runtime->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001011 if (rfile->rmidi->card->shutdown)
1012 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (signal_pending(current))
1014 return result > 0 ? result : -ERESTARTSYS;
1015 if (!runtime->avail)
1016 return result > 0 ? result : -EIO;
1017 spin_lock_irq(&runtime->lock);
1018 }
1019 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001020 count1 = snd_rawmidi_kernel_read1(substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001021 (unsigned char __user *)buf,
1022 NULL/*kernelbuf*/,
1023 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (count1 < 0)
1025 return result > 0 ? result : count1;
1026 result += count1;
1027 buf += count1;
1028 count -= count1;
1029 }
1030 return result;
1031}
1032
1033/**
1034 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1035 * @substream: the rawmidi substream
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001036 *
1037 * Return: 1 if the internal output buffer is empty, 0 if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001039int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001041 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 int result;
1043 unsigned long flags;
1044
1045 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001046 rmidi_dbg(substream->rmidi,
1047 "snd_rawmidi_transmit_empty: output is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return 1;
1049 }
1050 spin_lock_irqsave(&runtime->lock, flags);
1051 result = runtime->avail >= runtime->buffer_size;
1052 spin_unlock_irqrestore(&runtime->lock, flags);
1053 return result;
1054}
1055
1056/**
1057 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1058 * @substream: the rawmidi substream
1059 * @buffer: the buffer pointer
1060 * @count: data size to transfer
1061 *
1062 * Copies data from the internal output buffer to the given buffer.
1063 *
1064 * Call this in the interrupt handler when the midi output is ready,
1065 * and call snd_rawmidi_transmit_ack() after the transmission is
1066 * finished.
1067 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001068 * Return: The size of copied data, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001070int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1071 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 unsigned long flags;
1074 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001075 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001078 rmidi_dbg(substream->rmidi,
1079 "snd_rawmidi_transmit_peek: output is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return -EINVAL;
1081 }
1082 result = 0;
1083 spin_lock_irqsave(&runtime->lock, flags);
1084 if (runtime->avail >= runtime->buffer_size) {
1085 /* warning: lowlevel layer MUST trigger down the hardware */
1086 goto __skip;
1087 }
1088 if (count == 1) { /* special case, faster code */
1089 *buffer = runtime->buffer[runtime->hw_ptr];
1090 result++;
1091 } else {
1092 count1 = runtime->buffer_size - runtime->hw_ptr;
1093 if (count1 > count)
1094 count1 = count;
1095 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1096 count1 = runtime->buffer_size - runtime->avail;
1097 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1098 count -= count1;
1099 result += count1;
1100 if (count > 0) {
1101 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1102 count = runtime->buffer_size - runtime->avail - count1;
1103 memcpy(buffer + count1, runtime->buffer, count);
1104 result += count;
1105 }
1106 }
1107 __skip:
1108 spin_unlock_irqrestore(&runtime->lock, flags);
1109 return result;
1110}
1111
1112/**
1113 * snd_rawmidi_transmit_ack - acknowledge the transmission
1114 * @substream: the rawmidi substream
Masanari Iida0a114582014-02-09 00:47:36 +09001115 * @count: the transferred count
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *
1117 * Advances the hardware pointer for the internal output buffer with
1118 * the given size and updates the condition.
1119 * Call after the transmission is finished.
1120 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001121 * Return: The advanced size if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001123int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001126 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (runtime->buffer == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001129 rmidi_dbg(substream->rmidi,
1130 "snd_rawmidi_transmit_ack: output is not active!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return -EINVAL;
1132 }
1133 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001134 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 runtime->hw_ptr += count;
1136 runtime->hw_ptr %= runtime->buffer_size;
1137 runtime->avail += count;
1138 substream->bytes += count;
1139 if (count > 0) {
1140 if (runtime->drain || snd_rawmidi_ready(substream))
1141 wake_up(&runtime->sleep);
1142 }
1143 spin_unlock_irqrestore(&runtime->lock, flags);
1144 return count;
1145}
1146
1147/**
1148 * snd_rawmidi_transmit - copy from the buffer to the device
1149 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001150 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 * @count: the data size to transfer
1152 *
1153 * Copies data from the buffer to the device and advances the pointer.
1154 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001155 * Return: The copied size if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001157int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1158 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Takashi Iwai219df322008-11-03 08:17:05 +01001160 if (!substream->opened)
1161 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1163 if (count < 0)
1164 return count;
1165 return snd_rawmidi_transmit_ack(substream, count);
1166}
1167
Takashi Iwai48c9d412005-11-17 13:56:51 +01001168static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001169 const unsigned char __user *userbuf,
1170 const unsigned char *kernelbuf,
1171 long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 unsigned long flags;
1174 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001175 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001177 if (snd_BUG_ON(!kernelbuf && !userbuf))
1178 return -EINVAL;
1179 if (snd_BUG_ON(!runtime->buffer))
1180 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 result = 0;
1183 spin_lock_irqsave(&runtime->lock, flags);
1184 if (substream->append) {
1185 if ((long)runtime->avail < count) {
1186 spin_unlock_irqrestore(&runtime->lock, flags);
1187 return -EAGAIN;
1188 }
1189 }
1190 while (count > 0 && runtime->avail > 0) {
1191 count1 = runtime->buffer_size - runtime->appl_ptr;
1192 if (count1 > count)
1193 count1 = count;
1194 if (count1 > (long)runtime->avail)
1195 count1 = runtime->avail;
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001196 if (kernelbuf)
1197 memcpy(runtime->buffer + runtime->appl_ptr,
1198 kernelbuf + result, count1);
1199 else if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 spin_unlock_irqrestore(&runtime->lock, flags);
1201 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001202 userbuf + result, count1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 spin_lock_irqsave(&runtime->lock, flags);
1204 result = result > 0 ? result : -EFAULT;
1205 goto __end;
1206 }
1207 spin_lock_irqsave(&runtime->lock, flags);
1208 }
1209 runtime->appl_ptr += count1;
1210 runtime->appl_ptr %= runtime->buffer_size;
1211 runtime->avail -= count1;
1212 result += count1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 count -= count1;
1214 }
1215 __end:
1216 count1 = runtime->avail < runtime->buffer_size;
1217 spin_unlock_irqrestore(&runtime->lock, flags);
1218 if (count1)
1219 snd_rawmidi_output_trigger(substream, 1);
1220 return result;
1221}
1222
Takashi Iwai48c9d412005-11-17 13:56:51 +01001223long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1224 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001226 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Takashi Iwai48c9d412005-11-17 13:56:51 +01001229static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1230 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 long result, timeout;
1233 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001234 struct snd_rawmidi_file *rfile;
1235 struct snd_rawmidi_runtime *runtime;
1236 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 rfile = file->private_data;
1239 substream = rfile->output;
1240 runtime = substream->runtime;
1241 /* we cannot put an atomic message to our buffer */
1242 if (substream->append && count > runtime->buffer_size)
1243 return -EIO;
1244 result = 0;
1245 while (count > 0) {
1246 spin_lock_irq(&runtime->lock);
1247 while (!snd_rawmidi_ready_append(substream, count)) {
1248 wait_queue_t wait;
1249 if (file->f_flags & O_NONBLOCK) {
1250 spin_unlock_irq(&runtime->lock);
1251 return result > 0 ? result : -EAGAIN;
1252 }
1253 init_waitqueue_entry(&wait, current);
1254 add_wait_queue(&runtime->sleep, &wait);
1255 set_current_state(TASK_INTERRUPTIBLE);
1256 spin_unlock_irq(&runtime->lock);
1257 timeout = schedule_timeout(30 * HZ);
1258 remove_wait_queue(&runtime->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001259 if (rfile->rmidi->card->shutdown)
1260 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (signal_pending(current))
1262 return result > 0 ? result : -ERESTARTSYS;
1263 if (!runtime->avail && !timeout)
1264 return result > 0 ? result : -EIO;
1265 spin_lock_irq(&runtime->lock);
1266 }
1267 spin_unlock_irq(&runtime->lock);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001268 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (count1 < 0)
1270 return result > 0 ? result : count1;
1271 result += count1;
1272 buf += count1;
1273 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1274 break;
1275 count -= count1;
1276 }
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001277 if (file->f_flags & O_DSYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 spin_lock_irq(&runtime->lock);
1279 while (runtime->avail != runtime->buffer_size) {
1280 wait_queue_t wait;
1281 unsigned int last_avail = runtime->avail;
1282 init_waitqueue_entry(&wait, current);
1283 add_wait_queue(&runtime->sleep, &wait);
1284 set_current_state(TASK_INTERRUPTIBLE);
1285 spin_unlock_irq(&runtime->lock);
1286 timeout = schedule_timeout(30 * HZ);
1287 remove_wait_queue(&runtime->sleep, &wait);
1288 if (signal_pending(current))
1289 return result > 0 ? result : -ERESTARTSYS;
1290 if (runtime->avail == last_avail && !timeout)
1291 return result > 0 ? result : -EIO;
1292 spin_lock_irq(&runtime->lock);
1293 }
1294 spin_unlock_irq(&runtime->lock);
1295 }
1296 return result;
1297}
1298
1299static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1300{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001301 struct snd_rawmidi_file *rfile;
1302 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 unsigned int mask;
1304
1305 rfile = file->private_data;
1306 if (rfile->input != NULL) {
1307 runtime = rfile->input->runtime;
1308 snd_rawmidi_input_trigger(rfile->input, 1);
1309 poll_wait(file, &runtime->sleep, wait);
1310 }
1311 if (rfile->output != NULL) {
1312 runtime = rfile->output->runtime;
1313 poll_wait(file, &runtime->sleep, wait);
1314 }
1315 mask = 0;
1316 if (rfile->input != NULL) {
1317 if (snd_rawmidi_ready(rfile->input))
1318 mask |= POLLIN | POLLRDNORM;
1319 }
1320 if (rfile->output != NULL) {
1321 if (snd_rawmidi_ready(rfile->output))
1322 mask |= POLLOUT | POLLWRNORM;
1323 }
1324 return mask;
1325}
1326
1327/*
1328 */
1329#ifdef CONFIG_COMPAT
1330#include "rawmidi_compat.c"
1331#else
1332#define snd_rawmidi_ioctl_compat NULL
1333#endif
1334
1335/*
1336
1337 */
1338
Takashi Iwai48c9d412005-11-17 13:56:51 +01001339static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1340 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001342 struct snd_rawmidi *rmidi;
1343 struct snd_rawmidi_substream *substream;
1344 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 rmidi = entry->private_data;
1347 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001348 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001350 list_for_each_entry(substream,
1351 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1352 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 snd_iprintf(buffer,
1354 "Output %d\n"
1355 " Tx bytes : %lu\n",
1356 substream->number,
1357 (unsigned long) substream->bytes);
1358 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001359 snd_iprintf(buffer,
1360 " Owner PID : %d\n",
1361 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 runtime = substream->runtime;
1363 snd_iprintf(buffer,
1364 " Mode : %s\n"
1365 " Buffer size : %lu\n"
1366 " Avail : %lu\n",
1367 runtime->oss ? "OSS compatible" : "native",
1368 (unsigned long) runtime->buffer_size,
1369 (unsigned long) runtime->avail);
1370 }
1371 }
1372 }
1373 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001374 list_for_each_entry(substream,
1375 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1376 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 snd_iprintf(buffer,
1378 "Input %d\n"
1379 " Rx bytes : %lu\n",
1380 substream->number,
1381 (unsigned long) substream->bytes);
1382 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001383 snd_iprintf(buffer,
1384 " Owner PID : %d\n",
1385 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 runtime = substream->runtime;
1387 snd_iprintf(buffer,
1388 " Buffer size : %lu\n"
1389 " Avail : %lu\n"
1390 " Overruns : %lu\n",
1391 (unsigned long) runtime->buffer_size,
1392 (unsigned long) runtime->avail,
1393 (unsigned long) runtime->xruns);
1394 }
1395 }
1396 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001397 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400/*
1401 * Register functions
1402 */
1403
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001404static const struct file_operations snd_rawmidi_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 .owner = THIS_MODULE,
1407 .read = snd_rawmidi_read,
1408 .write = snd_rawmidi_write,
1409 .open = snd_rawmidi_open,
1410 .release = snd_rawmidi_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001411 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 .poll = snd_rawmidi_poll,
1413 .unlocked_ioctl = snd_rawmidi_ioctl,
1414 .compat_ioctl = snd_rawmidi_ioctl_compat,
1415};
1416
Takashi Iwai48c9d412005-11-17 13:56:51 +01001417static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1418 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 int direction,
1420 int count)
1421{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001422 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int idx;
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001426 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001427 if (substream == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001428 rmidi_err(rmidi, "rawmidi: cannot allocate substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 substream->stream = direction;
1432 substream->number = idx;
1433 substream->rmidi = rmidi;
1434 substream->pstr = stream;
1435 list_add_tail(&substream->list, &stream->substreams);
1436 stream->substream_count++;
1437 }
1438 return 0;
1439}
1440
1441/**
1442 * snd_rawmidi_new - create a rawmidi instance
1443 * @card: the card instance
1444 * @id: the id string
1445 * @device: the device index
1446 * @output_count: the number of output streams
1447 * @input_count: the number of input streams
1448 * @rrawmidi: the pointer to store the new rawmidi instance
1449 *
1450 * Creates a new rawmidi instance.
1451 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1452 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001453 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001455int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 int output_count, int input_count,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001457 struct snd_rawmidi ** rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001459 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001461 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 .dev_free = snd_rawmidi_dev_free,
1463 .dev_register = snd_rawmidi_dev_register,
1464 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 };
1466
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001467 if (snd_BUG_ON(!card))
1468 return -ENXIO;
1469 if (rrawmidi)
1470 *rrawmidi = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001471 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001472 if (rmidi == NULL) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001473 dev_err(card->dev, "rawmidi: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 rmidi->card = card;
1477 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001478 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001480 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1481 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (id != NULL)
1484 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001485 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1486 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1487 SNDRV_RAWMIDI_STREAM_INPUT,
1488 input_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 snd_rawmidi_free(rmidi);
1490 return err;
1491 }
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001492 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1493 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1494 SNDRV_RAWMIDI_STREAM_OUTPUT,
1495 output_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 snd_rawmidi_free(rmidi);
1497 return err;
1498 }
1499 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1500 snd_rawmidi_free(rmidi);
1501 return err;
1502 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001503 if (rrawmidi)
1504 *rrawmidi = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return 0;
1506}
1507
Takashi Iwai48c9d412005-11-17 13:56:51 +01001508static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001510 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001513 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 list_del(&substream->list);
1515 kfree(substream);
1516 }
1517}
1518
Takashi Iwai48c9d412005-11-17 13:56:51 +01001519static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001521 if (!rmidi)
1522 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +02001523
1524 snd_info_free_entry(rmidi->proc_entry);
1525 rmidi->proc_entry = NULL;
1526 mutex_lock(&register_mutex);
1527 if (rmidi->ops && rmidi->ops->dev_unregister)
1528 rmidi->ops->dev_unregister(rmidi);
1529 mutex_unlock(&register_mutex);
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1532 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1533 if (rmidi->private_free)
1534 rmidi->private_free(rmidi);
1535 kfree(rmidi);
1536 return 0;
1537}
1538
Takashi Iwai48c9d412005-11-17 13:56:51 +01001539static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001541 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return snd_rawmidi_free(rmidi);
1543}
1544
1545#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
Takashi Iwai48c9d412005-11-17 13:56:51 +01001546static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001548 struct snd_rawmidi *rmidi = device->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 rmidi->seq_dev = NULL;
1550}
1551#endif
1552
Takashi Iwai48c9d412005-11-17 13:56:51 +01001553static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001555 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001556 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001558 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1561 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001562 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001563 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001564 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return -EBUSY;
1566 }
Clemens Ladischf87135f2005-11-20 14:06:59 +01001567 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1569 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1570 rmidi->card, rmidi->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001571 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001572 rmidi_err(rmidi, "unable to register rawmidi device %i:%i\n",
1573 rmidi->card->number, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001574 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001575 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return err;
1577 }
1578 if (rmidi->ops && rmidi->ops->dev_register &&
1579 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1580 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001581 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001582 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return err;
1584 }
1585#ifdef CONFIG_SND_OSSEMUL
1586 rmidi->ossreg = 0;
1587 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1588 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001589 rmidi->card, 0, &snd_rawmidi_f_ops,
Takashi Iwai80d7d772014-02-04 13:51:45 +01001590 rmidi) < 0) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001591 rmidi_err(rmidi,
1592 "unable to register OSS rawmidi device %i:%i\n",
1593 rmidi->card->number, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 } else {
1595 rmidi->ossreg++;
1596#ifdef SNDRV_OSS_INFO_DEV_MIDI
1597 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1598#endif
1599 }
1600 }
1601 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1602 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001603 rmidi->card, 1, &snd_rawmidi_f_ops,
Takashi Iwai80d7d772014-02-04 13:51:45 +01001604 rmidi) < 0) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001605 rmidi_err(rmidi,
1606 "unable to register OSS rawmidi device %i:%i\n",
1607 rmidi->card->number, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 } else {
1609 rmidi->ossreg++;
1610 }
1611 }
1612#endif /* CONFIG_SND_OSSEMUL */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001613 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 sprintf(name, "midi%d", rmidi->device);
1615 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1616 if (entry) {
1617 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 entry->c.text.read = snd_rawmidi_proc_info_read;
1619 if (snd_info_register(entry) < 0) {
1620 snd_info_free_entry(entry);
1621 entry = NULL;
1622 }
1623 }
1624 rmidi->proc_entry = entry;
1625#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1626 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1627 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1628 rmidi->seq_dev->private_data = rmidi;
1629 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1630 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1631 snd_device_register(rmidi->card, rmidi->seq_dev);
1632 }
1633 }
1634#endif
1635 return 0;
1636}
1637
Takashi Iwai48c9d412005-11-17 13:56:51 +01001638static int snd_rawmidi_dev_disconnect(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 Iwai0914f792012-10-16 16:43:39 +02001641 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001643 mutex_lock(&register_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02001644 mutex_lock(&rmidi->open_mutex);
1645 wake_up(&rmidi->open_wait);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001646 list_del_init(&rmidi->list);
Takashi Iwai0914f792012-10-16 16:43:39 +02001647 for (dir = 0; dir < 2; dir++) {
1648 struct snd_rawmidi_substream *s;
1649 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1650 if (s->runtime)
1651 wake_up(&s->runtime->sleep);
1652 }
1653 }
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655#ifdef CONFIG_SND_OSSEMUL
1656 if (rmidi->ossreg) {
1657 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1658 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1659#ifdef SNDRV_OSS_INFO_DEV_MIDI
1660 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1661#endif
1662 }
1663 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1664 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1665 rmidi->ossreg = 0;
1666 }
1667#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Takashi Iwai0914f792012-10-16 16:43:39 +02001669 mutex_unlock(&rmidi->open_mutex);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001670 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
1674/**
1675 * snd_rawmidi_set_ops - set the rawmidi operators
1676 * @rmidi: the rawmidi instance
1677 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1678 * @ops: the operator table
1679 *
1680 * Sets the rawmidi operators for the given stream direction.
1681 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001682void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1683 struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001685 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Johannes Berg9244b2c2006-10-05 16:02:22 +02001687 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
1691/*
1692 * ENTRY functions
1693 */
1694
1695static int __init alsa_rawmidi_init(void)
1696{
1697
1698 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1699 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1700#ifdef CONFIG_SND_OSSEMUL
1701 { int i;
1702 /* check device map table */
1703 for (i = 0; i < SNDRV_CARDS; i++) {
1704 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001705 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1706 i, midi_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 midi_map[i] = 0;
1708 }
1709 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
Takashi Iwaica20d292014-02-04 18:21:39 +01001710 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1711 i, amidi_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 amidi_map[i] = 1;
1713 }
1714 }
1715 }
1716#endif /* CONFIG_SND_OSSEMUL */
1717 return 0;
1718}
1719
1720static void __exit alsa_rawmidi_exit(void)
1721{
1722 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1723 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1724}
1725
1726module_init(alsa_rawmidi_init)
1727module_exit(alsa_rawmidi_exit)
1728
1729EXPORT_SYMBOL(snd_rawmidi_output_params);
1730EXPORT_SYMBOL(snd_rawmidi_input_params);
1731EXPORT_SYMBOL(snd_rawmidi_drop_output);
1732EXPORT_SYMBOL(snd_rawmidi_drain_output);
1733EXPORT_SYMBOL(snd_rawmidi_drain_input);
1734EXPORT_SYMBOL(snd_rawmidi_receive);
1735EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1736EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1737EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1738EXPORT_SYMBOL(snd_rawmidi_transmit);
1739EXPORT_SYMBOL(snd_rawmidi_new);
1740EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741EXPORT_SYMBOL(snd_rawmidi_info_select);
1742EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1743EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1744EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1745EXPORT_SYMBOL(snd_rawmidi_kernel_write);