blob: cb157c2377df3608dbc3c3e8b0f5eff4d889c5e3 [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
Clemens Ladischf87135f2005-11-20 14:06:59 +010059static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
60{
Clemens Ladischf87135f2005-11-20 14:06:59 +010061 struct snd_rawmidi *rawmidi;
62
Johannes Berg9244b2c2006-10-05 16:02:22 +020063 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
Clemens Ladischf87135f2005-11-20 14:06:59 +010064 if (rawmidi->card == card && rawmidi->device == device)
65 return rawmidi;
Clemens Ladischf87135f2005-11-20 14:06:59 +010066 return NULL;
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static inline unsigned short snd_rawmidi_file_flags(struct file *file)
70{
71 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
72 case FMODE_WRITE:
73 return SNDRV_RAWMIDI_LFLG_OUTPUT;
74 case FMODE_READ:
75 return SNDRV_RAWMIDI_LFLG_INPUT;
76 default:
77 return SNDRV_RAWMIDI_LFLG_OPEN;
78 }
79}
80
Takashi Iwai48c9d412005-11-17 13:56:51 +010081static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Takashi Iwai48c9d412005-11-17 13:56:51 +010083 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return runtime->avail >= runtime->avail_min;
85}
86
Takashi Iwai48c9d412005-11-17 13:56:51 +010087static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
88 size_t count)
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 (!substream->append || runtime->avail >= count);
93}
94
Takashi Iwaib3c705a2011-06-14 14:37:06 +020095static void snd_rawmidi_input_event_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Takashi Iwaib3c705a2011-06-14 14:37:06 +020097 struct snd_rawmidi_runtime *runtime =
98 container_of(work, struct snd_rawmidi_runtime, event_work);
99 if (runtime->event)
100 runtime->event(runtime->substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Takashi Iwai48c9d412005-11-17 13:56:51 +0100103static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100105 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Takashi Iwaica2c0962005-09-09 14:20:23 +0200107 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -ENOMEM;
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200109 runtime->substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 spin_lock_init(&runtime->lock);
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700111 mutex_init(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 init_waitqueue_head(&runtime->sleep);
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200113 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 runtime->event = NULL;
115 runtime->buffer_size = PAGE_SIZE;
116 runtime->avail_min = 1;
117 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
118 runtime->avail = 0;
119 else
120 runtime->avail = runtime->buffer_size;
121 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
122 kfree(runtime);
123 return -ENOMEM;
124 }
125 runtime->appl_ptr = runtime->hw_ptr = 0;
126 substream->runtime = runtime;
127 return 0;
128}
129
Takashi Iwai48c9d412005-11-17 13:56:51 +0100130static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100132 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 kfree(runtime->buffer);
135 kfree(runtime);
136 substream->runtime = NULL;
137 return 0;
138}
139
Takashi Iwai48c9d412005-11-17 13:56:51 +0100140static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Takashi Iwai219df322008-11-03 08:17:05 +0100142 if (!substream->opened)
143 return;
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200144 substream->ops->trigger(substream, up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Takashi Iwai48c9d412005-11-17 13:56:51 +0100147static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Takashi Iwai219df322008-11-03 08:17:05 +0100149 if (!substream->opened)
150 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 substream->ops->trigger(substream, up);
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200152 if (!up)
153 cancel_work_sync(&substream->runtime->event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Takashi Iwai48c9d412005-11-17 13:56:51 +0100156int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100159 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 snd_rawmidi_output_trigger(substream, 0);
162 runtime->drain = 0;
163 spin_lock_irqsave(&runtime->lock, flags);
164 runtime->appl_ptr = runtime->hw_ptr = 0;
165 runtime->avail = runtime->buffer_size;
166 spin_unlock_irqrestore(&runtime->lock, flags);
167 return 0;
168}
169
Takashi Iwai48c9d412005-11-17 13:56:51 +0100170int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 int err;
173 long timeout;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100174 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 err = 0;
177 runtime->drain = 1;
178 timeout = wait_event_interruptible_timeout(runtime->sleep,
179 (runtime->avail >= runtime->buffer_size),
180 10*HZ);
181 if (signal_pending(current))
182 err = -ERESTARTSYS;
183 if (runtime->avail < runtime->buffer_size && !timeout) {
184 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
185 err = -EIO;
186 }
187 runtime->drain = 0;
188 if (err != -ERESTARTSYS) {
189 /* we need wait a while to make sure that Tx FIFOs are empty */
190 if (substream->ops->drain)
191 substream->ops->drain(substream);
192 else
193 msleep(50);
194 snd_rawmidi_drop_output(substream);
195 }
196 return err;
197}
198
Takashi Iwai48c9d412005-11-17 13:56:51 +0100199int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100202 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 snd_rawmidi_input_trigger(substream, 0);
205 runtime->drain = 0;
206 spin_lock_irqsave(&runtime->lock, flags);
207 runtime->appl_ptr = runtime->hw_ptr = 0;
208 runtime->avail = 0;
209 spin_unlock_irqrestore(&runtime->lock, flags);
210 return 0;
211}
212
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100213/* look for an available substream for the given stream direction;
214 * if a specific subdevice is given, try to assign it
215 */
216static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
217 int stream, int mode,
218 struct snd_rawmidi_substream **sub_ret)
219{
220 struct snd_rawmidi_substream *substream;
221 struct snd_rawmidi_str *s = &rmidi->streams[stream];
222 static unsigned int info_flags[2] = {
223 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
224 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
225 };
226
227 if (!(rmidi->info_flags & info_flags[stream]))
228 return -ENXIO;
229 if (subdevice >= 0 && subdevice >= s->substream_count)
230 return -ENODEV;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100231
232 list_for_each_entry(substream, &s->substreams, list) {
233 if (substream->opened) {
234 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
Clemens Ladisch16fb1092009-10-21 09:10:16 +0200235 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
236 !substream->append)
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100237 continue;
238 }
239 if (subdevice < 0 || subdevice == substream->number) {
240 *sub_ret = substream;
241 return 0;
242 }
243 }
244 return -EAGAIN;
245}
246
247/* open and do ref-counting for the given substream */
248static int open_substream(struct snd_rawmidi *rmidi,
249 struct snd_rawmidi_substream *substream,
250 int mode)
251{
252 int err;
253
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200254 if (substream->use_count == 0) {
255 err = snd_rawmidi_runtime_create(substream);
256 if (err < 0)
257 return err;
258 err = substream->ops->open(substream);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200259 if (err < 0) {
260 snd_rawmidi_runtime_free(substream);
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200261 return err;
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200262 }
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200263 substream->opened = 1;
Clemens Ladisch2d4b8422009-07-13 13:52:46 +0200264 substream->active_sensing = 0;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200265 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
266 substream->append = 1;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100267 substream->pid = get_pid(task_pid(current));
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200268 rmidi->streams[substream->stream].substream_opened++;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200269 }
270 substream->use_count++;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100271 return 0;
272}
273
274static void close_substream(struct snd_rawmidi *rmidi,
275 struct snd_rawmidi_substream *substream,
276 int cleanup);
277
278static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
279 struct snd_rawmidi_file *rfile)
280{
281 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
282 int err;
283
284 rfile->input = rfile->output = NULL;
285 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
286 err = assign_substream(rmidi, subdevice,
287 SNDRV_RAWMIDI_STREAM_INPUT,
288 mode, &sinput);
289 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200290 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100291 }
292 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
293 err = assign_substream(rmidi, subdevice,
294 SNDRV_RAWMIDI_STREAM_OUTPUT,
295 mode, &soutput);
296 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200297 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100298 }
299
300 if (sinput) {
301 err = open_substream(rmidi, sinput, mode);
302 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200303 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100304 }
305 if (soutput) {
306 err = open_substream(rmidi, soutput, mode);
307 if (err < 0) {
308 if (sinput)
309 close_substream(rmidi, sinput, 0);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200310 return err;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100311 }
312 }
313
314 rfile->rmidi = rmidi;
315 rfile->input = sinput;
316 rfile->output = soutput;
317 return 0;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100318}
319
320/* called from sound/core/seq/seq_midi.c */
Clemens Ladischf87135f2005-11-20 14:06:59 +0100321int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai48c9d412005-11-17 13:56:51 +0100322 int mode, struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100324 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int err;
326
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100327 if (snd_BUG_ON(!rfile))
328 return -EINVAL;
329
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100330 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100331 rmidi = snd_rawmidi_search(card, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (rmidi == NULL) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100333 mutex_unlock(&register_mutex);
334 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336 if (!try_module_get(rmidi->card->module)) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100337 mutex_unlock(&register_mutex);
338 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Takashi Iwaif9d20282009-02-11 14:55:59 +0100340 mutex_unlock(&register_mutex);
341
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100342 mutex_lock(&rmidi->open_mutex);
343 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
344 mutex_unlock(&rmidi->open_mutex);
345 if (err < 0)
346 module_put(rmidi->card->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return err;
348}
349
350static int snd_rawmidi_open(struct inode *inode, struct file *file)
351{
352 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100353 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100354 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 unsigned short fflags;
356 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100357 struct snd_rawmidi *rmidi;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100358 struct snd_rawmidi_file *rawmidi_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 wait_queue_t wait;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100360 struct snd_ctl_file *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100362 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
363 return -EINVAL; /* invalid combination */
364
Takashi Iwai02f48652010-04-13 11:49:04 +0200365 err = nonseekable_open(inode, file);
366 if (err < 0)
367 return err;
368
Clemens Ladischf1902862005-10-24 17:05:03 +0200369 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100370 rmidi = snd_lookup_minor_data(iminor(inode),
371 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200373 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100374 rmidi = snd_lookup_oss_minor_data(iminor(inode),
375 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200377 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (rmidi == NULL)
381 return -ENODEV;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100382
Takashi Iwai320e1282012-11-07 12:42:47 +0100383 if (!try_module_get(rmidi->card->module)) {
384 snd_card_unref(rmidi->card);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100385 return -ENXIO;
Takashi Iwai320e1282012-11-07 12:42:47 +0100386 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100387
388 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 card = rmidi->card;
390 err = snd_card_file_add(card, file);
391 if (err < 0)
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100392 goto __error_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200394 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
397 if (rawmidi_file == NULL) {
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100398 err = -ENOMEM;
399 goto __error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 init_waitqueue_entry(&wait, current);
402 add_wait_queue(&rmidi->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 while (1) {
404 subdevice = -1;
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200405 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200406 list_for_each_entry(kctl, &card->ctl_files, list) {
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100407 if (kctl->pid == task_pid(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 subdevice = kctl->prefer_rawmidi_subdevice;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200409 if (subdevice != -1)
410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 }
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200413 read_unlock(&card->ctl_files_rwlock);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100414 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (err >= 0)
416 break;
417 if (err == -EAGAIN) {
418 if (file->f_flags & O_NONBLOCK) {
419 err = -EBUSY;
420 break;
421 }
422 } else
423 break;
424 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100425 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100427 mutex_lock(&rmidi->open_mutex);
Takashi Iwai80d04292012-11-07 12:42:48 +0100428 if (rmidi->card->shutdown) {
429 err = -ENODEV;
430 break;
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (signal_pending(current)) {
433 err = -ERESTARTSYS;
434 break;
435 }
436 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100437 remove_wait_queue(&rmidi->open_wait, &wait);
438 if (err < 0) {
439 kfree(rawmidi_file);
440 goto __error;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#ifdef CONFIG_SND_OSSEMUL
443 if (rawmidi_file->input && rawmidi_file->input->runtime)
444 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
445 if (rawmidi_file->output && rawmidi_file->output->runtime)
446 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
447#endif
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100448 file->private_data = rawmidi_file;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100449 mutex_unlock(&rmidi->open_mutex);
Takashi Iwai320e1282012-11-07 12:42:47 +0100450 snd_card_unref(rmidi->card);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100451 return 0;
452
453 __error:
454 snd_card_file_remove(card, file);
455 __error_card:
456 mutex_unlock(&rmidi->open_mutex);
457 module_put(rmidi->card->module);
Takashi Iwai320e1282012-11-07 12:42:47 +0100458 snd_card_unref(rmidi->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return err;
460}
461
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100462static void close_substream(struct snd_rawmidi *rmidi,
463 struct snd_rawmidi_substream *substream,
464 int cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100466 if (--substream->use_count)
467 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100469 if (cleanup) {
470 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
471 snd_rawmidi_input_trigger(substream, 0);
472 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (substream->active_sensing) {
474 unsigned char buf = 0xfe;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100475 /* sending single active sensing message
476 * to shut the device up
477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 snd_rawmidi_kernel_write(substream, &buf, 1);
479 }
480 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
481 snd_rawmidi_output_trigger(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100484 substream->ops->close(substream);
485 if (substream->runtime->private_free)
486 substream->runtime->private_free(substream);
487 snd_rawmidi_runtime_free(substream);
488 substream->opened = 0;
489 substream->append = 0;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100490 put_pid(substream->pid);
491 substream->pid = NULL;
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200492 rmidi->streams[substream->stream].substream_opened--;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100493}
494
495static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
496{
497 struct snd_rawmidi *rmidi;
498
499 rmidi = rfile->rmidi;
500 mutex_lock(&rmidi->open_mutex);
501 if (rfile->input) {
502 close_substream(rmidi, rfile->input, 1);
503 rfile->input = NULL;
504 }
505 if (rfile->output) {
506 close_substream(rmidi, rfile->output, 1);
507 rfile->output = NULL;
508 }
509 rfile->rmidi = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100510 mutex_unlock(&rmidi->open_mutex);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100511 wake_up(&rmidi->open_wait);
512}
513
514/* called from sound/core/seq/seq_midi.c */
515int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
516{
517 struct snd_rawmidi *rmidi;
518
519 if (snd_BUG_ON(!rfile))
520 return -ENXIO;
521
522 rmidi = rfile->rmidi;
523 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 module_put(rmidi->card->module);
525 return 0;
526}
527
528static int snd_rawmidi_release(struct inode *inode, struct file *file)
529{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100530 struct snd_rawmidi_file *rfile;
531 struct snd_rawmidi *rmidi;
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200532 struct module *module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 rfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 rmidi = rfile->rmidi;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100536 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 kfree(rfile);
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200538 module = rmidi->card->module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 snd_card_file_remove(rmidi->card, file);
Clemens Ladischaa73aec2010-10-15 12:06:18 +0200540 module_put(module);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Adrian Bunk18612042005-11-23 13:14:50 +0100544static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
545 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100547 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if (substream == NULL)
550 return -ENODEV;
551 rmidi = substream->rmidi;
552 memset(info, 0, sizeof(*info));
553 info->card = rmidi->card->number;
554 info->device = rmidi->device;
555 info->subdevice = substream->number;
556 info->stream = substream->stream;
557 info->flags = rmidi->info_flags;
558 strcpy(info->id, rmidi->id);
559 strcpy(info->name, rmidi->name);
560 strcpy(info->subname, substream->name);
561 info->subdevices_count = substream->pstr->substream_count;
562 info->subdevices_avail = (substream->pstr->substream_count -
563 substream->pstr->substream_opened);
564 return 0;
565}
566
Takashi Iwai48c9d412005-11-17 13:56:51 +0100567static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
568 struct snd_rawmidi_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100570 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 int err;
572 if ((err = snd_rawmidi_info(substream, &info)) < 0)
573 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100574 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -EFAULT;
576 return 0;
577}
578
Takashi Iwai48c9d412005-11-17 13:56:51 +0100579int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100581 struct snd_rawmidi *rmidi;
582 struct snd_rawmidi_str *pstr;
583 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100584
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100585 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100586 rmidi = snd_rawmidi_search(card, info->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100587 mutex_unlock(&register_mutex);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100588 if (!rmidi)
589 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (info->stream < 0 || info->stream > 1)
591 return -EINVAL;
592 pstr = &rmidi->streams[info->stream];
593 if (pstr->substream_count == 0)
594 return -ENOENT;
595 if (info->subdevice >= pstr->substream_count)
596 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200597 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if ((unsigned int)substream->number == info->subdevice)
599 return snd_rawmidi_info(substream, info);
600 }
601 return -ENXIO;
602}
603
Takashi Iwai48c9d412005-11-17 13:56:51 +0100604static int snd_rawmidi_info_select_user(struct snd_card *card,
605 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100608 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (get_user(info.device, &_info->device))
610 return -EFAULT;
611 if (get_user(info.stream, &_info->stream))
612 return -EFAULT;
613 if (get_user(info.subdevice, &_info->subdevice))
614 return -EFAULT;
615 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
616 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100617 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return -EFAULT;
619 return 0;
620}
621
Takashi Iwai48c9d412005-11-17 13:56:51 +0100622int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
623 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 char *newbuf;
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700626 char *oldbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100627 struct snd_rawmidi_runtime *runtime = substream->runtime;
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700628 unsigned long flags;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (substream->append && substream->use_count > 1)
631 return -EBUSY;
632 snd_rawmidi_drain_output(substream);
633 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
634 return -EINVAL;
635 }
636 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
637 return -EINVAL;
638 }
639 if (params->buffer_size != runtime->buffer_size) {
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700640 mutex_lock(&runtime->realloc_mutex);
641 newbuf = __krealloc(runtime->buffer, params->buffer_size,
Takashi Iwai3101ba02011-07-12 08:05:16 +0200642 GFP_KERNEL);
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700643 if (!newbuf) {
644 mutex_unlock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return -ENOMEM;
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700646 }
647 spin_lock_irqsave(&runtime->lock, flags);
648 oldbuf = runtime->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 runtime->buffer = newbuf;
650 runtime->buffer_size = params->buffer_size;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100651 runtime->avail = runtime->buffer_size;
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700652 spin_unlock_irqrestore(&runtime->lock, flags);
653 if (oldbuf != newbuf)
654 kfree(oldbuf);
655 mutex_unlock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 runtime->avail_min = params->avail_min;
658 substream->active_sensing = !params->no_active_sensing;
659 return 0;
660}
661
Takashi Iwai48c9d412005-11-17 13:56:51 +0100662int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
663 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 char *newbuf;
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700666 char *oldbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100667 struct snd_rawmidi_runtime *runtime = substream->runtime;
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700668 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 snd_rawmidi_drain_input(substream);
671 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
672 return -EINVAL;
673 }
674 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
675 return -EINVAL;
676 }
677 if (params->buffer_size != runtime->buffer_size) {
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700678 mutex_lock(&runtime->realloc_mutex);
679 newbuf = __krealloc(runtime->buffer, params->buffer_size,
Takashi Iwai3101ba02011-07-12 08:05:16 +0200680 GFP_KERNEL);
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700681 if (!newbuf) {
682 mutex_unlock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return -ENOMEM;
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700684 }
685 spin_lock_irqsave(&runtime->lock, flags);
686 oldbuf = runtime->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 runtime->buffer = newbuf;
688 runtime->buffer_size = params->buffer_size;
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700689 spin_unlock_irqrestore(&runtime->lock, flags);
690 if (oldbuf != newbuf)
691 kfree(oldbuf);
692 mutex_unlock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 runtime->avail_min = params->avail_min;
695 return 0;
696}
697
Takashi Iwai48c9d412005-11-17 13:56:51 +0100698static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
699 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100701 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 memset(status, 0, sizeof(*status));
704 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
705 spin_lock_irq(&runtime->lock);
706 status->avail = runtime->avail;
707 spin_unlock_irq(&runtime->lock);
708 return 0;
709}
710
Takashi Iwai48c9d412005-11-17 13:56:51 +0100711static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
712 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100714 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 memset(status, 0, sizeof(*status));
717 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
718 spin_lock_irq(&runtime->lock);
719 status->avail = runtime->avail;
720 status->xruns = runtime->xruns;
721 runtime->xruns = 0;
722 spin_unlock_irq(&runtime->lock);
723 return 0;
724}
725
726static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
727{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100728 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 void __user *argp = (void __user *)arg;
730
731 rfile = file->private_data;
732 if (((cmd >> 8) & 0xff) != 'W')
733 return -ENOTTY;
734 switch (cmd) {
735 case SNDRV_RAWMIDI_IOCTL_PVERSION:
736 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
737 case SNDRV_RAWMIDI_IOCTL_INFO:
738 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100739 int stream;
740 struct snd_rawmidi_info __user *info = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (get_user(stream, &info->stream))
742 return -EFAULT;
743 switch (stream) {
744 case SNDRV_RAWMIDI_STREAM_INPUT:
745 return snd_rawmidi_info_user(rfile->input, info);
746 case SNDRV_RAWMIDI_STREAM_OUTPUT:
747 return snd_rawmidi_info_user(rfile->output, info);
748 default:
749 return -EINVAL;
750 }
751 }
752 case SNDRV_RAWMIDI_IOCTL_PARAMS:
753 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100754 struct snd_rawmidi_params params;
755 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return -EFAULT;
757 switch (params.stream) {
758 case SNDRV_RAWMIDI_STREAM_OUTPUT:
759 if (rfile->output == NULL)
760 return -EINVAL;
761 return snd_rawmidi_output_params(rfile->output, &params);
762 case SNDRV_RAWMIDI_STREAM_INPUT:
763 if (rfile->input == NULL)
764 return -EINVAL;
765 return snd_rawmidi_input_params(rfile->input, &params);
766 default:
767 return -EINVAL;
768 }
769 }
770 case SNDRV_RAWMIDI_IOCTL_STATUS:
771 {
772 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100773 struct snd_rawmidi_status status;
774 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return -EFAULT;
776 switch (status.stream) {
777 case SNDRV_RAWMIDI_STREAM_OUTPUT:
778 if (rfile->output == NULL)
779 return -EINVAL;
780 err = snd_rawmidi_output_status(rfile->output, &status);
781 break;
782 case SNDRV_RAWMIDI_STREAM_INPUT:
783 if (rfile->input == NULL)
784 return -EINVAL;
785 err = snd_rawmidi_input_status(rfile->input, &status);
786 break;
787 default:
788 return -EINVAL;
789 }
790 if (err < 0)
791 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100792 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return -EFAULT;
794 return 0;
795 }
796 case SNDRV_RAWMIDI_IOCTL_DROP:
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_drop_output(rfile->output);
806 default:
807 return -EINVAL;
808 }
809 }
810 case SNDRV_RAWMIDI_IOCTL_DRAIN:
811 {
812 int val;
813 if (get_user(val, (int __user *) argp))
814 return -EFAULT;
815 switch (val) {
816 case SNDRV_RAWMIDI_STREAM_OUTPUT:
817 if (rfile->output == NULL)
818 return -EINVAL;
819 return snd_rawmidi_drain_output(rfile->output);
820 case SNDRV_RAWMIDI_STREAM_INPUT:
821 if (rfile->input == NULL)
822 return -EINVAL;
823 return snd_rawmidi_drain_input(rfile->input);
824 default:
825 return -EINVAL;
826 }
827 }
828#ifdef CONFIG_SND_DEBUG
829 default:
830 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
831#endif
832 }
833 return -ENOTTY;
834}
835
Takashi Iwai48c9d412005-11-17 13:56:51 +0100836static int snd_rawmidi_control_ioctl(struct snd_card *card,
837 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 unsigned int cmd,
839 unsigned long arg)
840{
841 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 switch (cmd) {
844 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
845 {
846 int device;
847
848 if (get_user(device, (int __user *)argp))
849 return -EFAULT;
Dan Carpentera7a13d02010-09-09 00:11:41 +0200850 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
851 device = SNDRV_RAWMIDI_DEVICES - 1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100852 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 device = device < 0 ? 0 : device + 1;
854 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100855 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 break;
857 device++;
858 }
859 if (device == SNDRV_RAWMIDI_DEVICES)
860 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100861 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (put_user(device, (int __user *)argp))
863 return -EFAULT;
864 return 0;
865 }
866 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
867 {
868 int val;
869
870 if (get_user(val, (int __user *)argp))
871 return -EFAULT;
872 control->prefer_rawmidi_subdevice = val;
873 return 0;
874 }
875 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
876 return snd_rawmidi_info_select_user(card, argp);
877 }
878 return -ENOIOCTLCMD;
879}
880
881/**
882 * snd_rawmidi_receive - receive the input data from the device
883 * @substream: the rawmidi substream
884 * @buffer: the buffer pointer
885 * @count: the data size to read
886 *
887 * Reads the data from the internal buffer.
888 *
889 * Returns the size of read data, or a negative error code on failure.
890 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100891int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
892 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 unsigned long flags;
895 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100896 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Takashi Iwai219df322008-11-03 08:17:05 +0100898 if (!substream->opened)
899 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (runtime->buffer == NULL) {
901 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
902 return -EINVAL;
903 }
904 spin_lock_irqsave(&runtime->lock, flags);
905 if (count == 1) { /* special case, faster code */
906 substream->bytes++;
907 if (runtime->avail < runtime->buffer_size) {
908 runtime->buffer[runtime->hw_ptr++] = buffer[0];
909 runtime->hw_ptr %= runtime->buffer_size;
910 runtime->avail++;
911 result++;
912 } else {
913 runtime->xruns++;
914 }
915 } else {
916 substream->bytes += count;
917 count1 = runtime->buffer_size - runtime->hw_ptr;
918 if (count1 > count)
919 count1 = count;
920 if (count1 > (int)(runtime->buffer_size - runtime->avail))
921 count1 = runtime->buffer_size - runtime->avail;
922 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
923 runtime->hw_ptr += count1;
924 runtime->hw_ptr %= runtime->buffer_size;
925 runtime->avail += count1;
926 count -= count1;
927 result += count1;
928 if (count > 0) {
929 buffer += count1;
930 count1 = count;
931 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
932 count1 = runtime->buffer_size - runtime->avail;
933 runtime->xruns += count - count1;
934 }
935 if (count1 > 0) {
936 memcpy(runtime->buffer, buffer, count1);
937 runtime->hw_ptr = count1;
938 runtime->avail += count1;
939 result += count1;
940 }
941 }
942 }
943 if (result > 0) {
944 if (runtime->event)
Takashi Iwaib3c705a2011-06-14 14:37:06 +0200945 schedule_work(&runtime->event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 else if (snd_rawmidi_ready(substream))
947 wake_up(&runtime->sleep);
948 }
949 spin_unlock_irqrestore(&runtime->lock, flags);
950 return result;
951}
952
Takashi Iwai48c9d412005-11-17 13:56:51 +0100953static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100954 unsigned char __user *userbuf,
955 unsigned char *kernelbuf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 unsigned long flags;
958 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100959 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700961 if (userbuf)
962 mutex_lock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 while (count > 0 && runtime->avail) {
964 count1 = runtime->buffer_size - runtime->appl_ptr;
965 if (count1 > count)
966 count1 = count;
967 spin_lock_irqsave(&runtime->lock, flags);
968 if (count1 > (int)runtime->avail)
969 count1 = runtime->avail;
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100970 if (kernelbuf)
971 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
972 if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 spin_unlock_irqrestore(&runtime->lock, flags);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100974 if (copy_to_user(userbuf + result,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 runtime->buffer + runtime->appl_ptr, count1)) {
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700976 mutex_unlock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return result > 0 ? result : -EFAULT;
978 }
979 spin_lock_irqsave(&runtime->lock, flags);
980 }
981 runtime->appl_ptr += count1;
982 runtime->appl_ptr %= runtime->buffer_size;
983 runtime->avail -= count1;
984 spin_unlock_irqrestore(&runtime->lock, flags);
985 result += count1;
986 count -= count1;
987 }
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -0700988 if (userbuf)
989 mutex_unlock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return result;
991}
992
Takashi Iwai48c9d412005-11-17 13:56:51 +0100993long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
994 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 snd_rawmidi_input_trigger(substream, 1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100997 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
Takashi Iwai48c9d412005-11-17 13:56:51 +01001000static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1001 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 long result;
1004 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001005 struct snd_rawmidi_file *rfile;
1006 struct snd_rawmidi_substream *substream;
1007 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 rfile = file->private_data;
1010 substream = rfile->input;
1011 if (substream == NULL)
1012 return -EIO;
1013 runtime = substream->runtime;
1014 snd_rawmidi_input_trigger(substream, 1);
1015 result = 0;
1016 while (count > 0) {
1017 spin_lock_irq(&runtime->lock);
1018 while (!snd_rawmidi_ready(substream)) {
1019 wait_queue_t wait;
1020 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1021 spin_unlock_irq(&runtime->lock);
1022 return result > 0 ? result : -EAGAIN;
1023 }
1024 init_waitqueue_entry(&wait, current);
1025 add_wait_queue(&runtime->sleep, &wait);
1026 set_current_state(TASK_INTERRUPTIBLE);
1027 spin_unlock_irq(&runtime->lock);
1028 schedule();
1029 remove_wait_queue(&runtime->sleep, &wait);
Takashi Iwai80d04292012-11-07 12:42:48 +01001030 if (rfile->rmidi->card->shutdown)
1031 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (signal_pending(current))
1033 return result > 0 ? result : -ERESTARTSYS;
1034 if (!runtime->avail)
1035 return result > 0 ? result : -EIO;
1036 spin_lock_irq(&runtime->lock);
1037 }
1038 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001039 count1 = snd_rawmidi_kernel_read1(substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001040 (unsigned char __user *)buf,
1041 NULL/*kernelbuf*/,
1042 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (count1 < 0)
1044 return result > 0 ? result : count1;
1045 result += count1;
1046 buf += count1;
1047 count -= count1;
1048 }
1049 return result;
1050}
1051
1052/**
1053 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1054 * @substream: the rawmidi substream
1055 *
1056 * Returns 1 if the internal output buffer is empty, 0 if not.
1057 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001058int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001060 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 int result;
1062 unsigned long flags;
1063
1064 if (runtime->buffer == NULL) {
1065 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1066 return 1;
1067 }
1068 spin_lock_irqsave(&runtime->lock, flags);
1069 result = runtime->avail >= runtime->buffer_size;
1070 spin_unlock_irqrestore(&runtime->lock, flags);
1071 return result;
1072}
1073
1074/**
1075 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1076 * @substream: the rawmidi substream
1077 * @buffer: the buffer pointer
1078 * @count: data size to transfer
1079 *
1080 * Copies data from the internal output buffer to the given buffer.
1081 *
1082 * Call this in the interrupt handler when the midi output is ready,
1083 * and call snd_rawmidi_transmit_ack() after the transmission is
1084 * finished.
1085 *
1086 * Returns the size of copied data, or a negative error code on failure.
1087 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001088int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1089 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 unsigned long flags;
1092 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001093 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 if (runtime->buffer == NULL) {
1096 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1097 return -EINVAL;
1098 }
1099 result = 0;
1100 spin_lock_irqsave(&runtime->lock, flags);
1101 if (runtime->avail >= runtime->buffer_size) {
1102 /* warning: lowlevel layer MUST trigger down the hardware */
1103 goto __skip;
1104 }
1105 if (count == 1) { /* special case, faster code */
1106 *buffer = runtime->buffer[runtime->hw_ptr];
1107 result++;
1108 } else {
1109 count1 = runtime->buffer_size - runtime->hw_ptr;
1110 if (count1 > count)
1111 count1 = count;
1112 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1113 count1 = runtime->buffer_size - runtime->avail;
1114 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1115 count -= count1;
1116 result += count1;
1117 if (count > 0) {
1118 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1119 count = runtime->buffer_size - runtime->avail - count1;
1120 memcpy(buffer + count1, runtime->buffer, count);
1121 result += count;
1122 }
1123 }
1124 __skip:
1125 spin_unlock_irqrestore(&runtime->lock, flags);
1126 return result;
1127}
1128
1129/**
1130 * snd_rawmidi_transmit_ack - acknowledge the transmission
1131 * @substream: the rawmidi substream
1132 * @count: the tranferred count
1133 *
1134 * Advances the hardware pointer for the internal output buffer with
1135 * the given size and updates the condition.
1136 * Call after the transmission is finished.
1137 *
1138 * Returns the advanced size if successful, or a negative error code on failure.
1139 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001140int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001143 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 if (runtime->buffer == NULL) {
1146 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1147 return -EINVAL;
1148 }
1149 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001150 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 runtime->hw_ptr += count;
1152 runtime->hw_ptr %= runtime->buffer_size;
1153 runtime->avail += count;
1154 substream->bytes += count;
1155 if (count > 0) {
1156 if (runtime->drain || snd_rawmidi_ready(substream))
1157 wake_up(&runtime->sleep);
1158 }
1159 spin_unlock_irqrestore(&runtime->lock, flags);
1160 return count;
1161}
1162
1163/**
1164 * snd_rawmidi_transmit - copy from the buffer to the device
1165 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001166 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 * @count: the data size to transfer
1168 *
1169 * Copies data from the buffer to the device and advances the pointer.
1170 *
1171 * Returns the copied size if successful, or a negative error code on failure.
1172 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001173int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1174 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Takashi Iwai219df322008-11-03 08:17:05 +01001176 if (!substream->opened)
1177 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1179 if (count < 0)
1180 return count;
1181 return snd_rawmidi_transmit_ack(substream, count);
1182}
1183
Takashi Iwai48c9d412005-11-17 13:56:51 +01001184static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001185 const unsigned char __user *userbuf,
1186 const unsigned char *kernelbuf,
1187 long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 unsigned long flags;
1190 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001191 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001193 if (snd_BUG_ON(!kernelbuf && !userbuf))
1194 return -EINVAL;
1195 if (snd_BUG_ON(!runtime->buffer))
1196 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 result = 0;
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -07001199 if (userbuf)
1200 mutex_lock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 spin_lock_irqsave(&runtime->lock, flags);
1202 if (substream->append) {
1203 if ((long)runtime->avail < count) {
1204 spin_unlock_irqrestore(&runtime->lock, flags);
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -07001205 if (userbuf)
1206 mutex_unlock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return -EAGAIN;
1208 }
1209 }
1210 while (count > 0 && runtime->avail > 0) {
1211 count1 = runtime->buffer_size - runtime->appl_ptr;
1212 if (count1 > count)
1213 count1 = count;
1214 if (count1 > (long)runtime->avail)
1215 count1 = runtime->avail;
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001216 if (kernelbuf)
1217 memcpy(runtime->buffer + runtime->appl_ptr,
1218 kernelbuf + result, count1);
1219 else if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 spin_unlock_irqrestore(&runtime->lock, flags);
1221 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001222 userbuf + result, count1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 spin_lock_irqsave(&runtime->lock, flags);
1224 result = result > 0 ? result : -EFAULT;
1225 goto __end;
1226 }
1227 spin_lock_irqsave(&runtime->lock, flags);
1228 }
1229 runtime->appl_ptr += count1;
1230 runtime->appl_ptr %= runtime->buffer_size;
1231 runtime->avail -= count1;
1232 result += count1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 count -= count1;
1234 }
1235 __end:
1236 count1 = runtime->avail < runtime->buffer_size;
1237 spin_unlock_irqrestore(&runtime->lock, flags);
Daniel Rosenberg5ca98ba2017-10-31 16:55:26 -07001238 if (userbuf)
1239 mutex_unlock(&runtime->realloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (count1)
1241 snd_rawmidi_output_trigger(substream, 1);
1242 return result;
1243}
1244
Takashi Iwai48c9d412005-11-17 13:56:51 +01001245long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1246 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001248 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Takashi Iwai48c9d412005-11-17 13:56:51 +01001251static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1252 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 long result, timeout;
1255 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001256 struct snd_rawmidi_file *rfile;
1257 struct snd_rawmidi_runtime *runtime;
1258 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 rfile = file->private_data;
1261 substream = rfile->output;
1262 runtime = substream->runtime;
1263 /* we cannot put an atomic message to our buffer */
1264 if (substream->append && count > runtime->buffer_size)
1265 return -EIO;
1266 result = 0;
1267 while (count > 0) {
1268 spin_lock_irq(&runtime->lock);
1269 while (!snd_rawmidi_ready_append(substream, count)) {
1270 wait_queue_t wait;
1271 if (file->f_flags & O_NONBLOCK) {
1272 spin_unlock_irq(&runtime->lock);
1273 return result > 0 ? result : -EAGAIN;
1274 }
1275 init_waitqueue_entry(&wait, current);
1276 add_wait_queue(&runtime->sleep, &wait);
1277 set_current_state(TASK_INTERRUPTIBLE);
1278 spin_unlock_irq(&runtime->lock);
1279 timeout = schedule_timeout(30 * HZ);
1280 remove_wait_queue(&runtime->sleep, &wait);
Takashi Iwai80d04292012-11-07 12:42:48 +01001281 if (rfile->rmidi->card->shutdown)
1282 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (signal_pending(current))
1284 return result > 0 ? result : -ERESTARTSYS;
1285 if (!runtime->avail && !timeout)
1286 return result > 0 ? result : -EIO;
1287 spin_lock_irq(&runtime->lock);
1288 }
1289 spin_unlock_irq(&runtime->lock);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001290 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (count1 < 0)
1292 return result > 0 ? result : count1;
1293 result += count1;
1294 buf += count1;
1295 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1296 break;
1297 count -= count1;
1298 }
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001299 if (file->f_flags & O_DSYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 spin_lock_irq(&runtime->lock);
1301 while (runtime->avail != runtime->buffer_size) {
1302 wait_queue_t wait;
1303 unsigned int last_avail = runtime->avail;
1304 init_waitqueue_entry(&wait, current);
1305 add_wait_queue(&runtime->sleep, &wait);
1306 set_current_state(TASK_INTERRUPTIBLE);
1307 spin_unlock_irq(&runtime->lock);
1308 timeout = schedule_timeout(30 * HZ);
1309 remove_wait_queue(&runtime->sleep, &wait);
1310 if (signal_pending(current))
1311 return result > 0 ? result : -ERESTARTSYS;
1312 if (runtime->avail == last_avail && !timeout)
1313 return result > 0 ? result : -EIO;
1314 spin_lock_irq(&runtime->lock);
1315 }
1316 spin_unlock_irq(&runtime->lock);
1317 }
1318 return result;
1319}
1320
1321static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1322{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001323 struct snd_rawmidi_file *rfile;
1324 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 unsigned int mask;
1326
1327 rfile = file->private_data;
1328 if (rfile->input != NULL) {
1329 runtime = rfile->input->runtime;
1330 snd_rawmidi_input_trigger(rfile->input, 1);
1331 poll_wait(file, &runtime->sleep, wait);
1332 }
1333 if (rfile->output != NULL) {
1334 runtime = rfile->output->runtime;
1335 poll_wait(file, &runtime->sleep, wait);
1336 }
1337 mask = 0;
1338 if (rfile->input != NULL) {
1339 if (snd_rawmidi_ready(rfile->input))
1340 mask |= POLLIN | POLLRDNORM;
1341 }
1342 if (rfile->output != NULL) {
1343 if (snd_rawmidi_ready(rfile->output))
1344 mask |= POLLOUT | POLLWRNORM;
1345 }
1346 return mask;
1347}
1348
1349/*
1350 */
1351#ifdef CONFIG_COMPAT
1352#include "rawmidi_compat.c"
1353#else
1354#define snd_rawmidi_ioctl_compat NULL
1355#endif
1356
1357/*
1358
1359 */
1360
Takashi Iwai48c9d412005-11-17 13:56:51 +01001361static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1362 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001364 struct snd_rawmidi *rmidi;
1365 struct snd_rawmidi_substream *substream;
1366 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 rmidi = entry->private_data;
1369 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001370 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001372 list_for_each_entry(substream,
1373 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1374 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 snd_iprintf(buffer,
1376 "Output %d\n"
1377 " Tx bytes : %lu\n",
1378 substream->number,
1379 (unsigned long) substream->bytes);
1380 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001381 snd_iprintf(buffer,
1382 " Owner PID : %d\n",
1383 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 runtime = substream->runtime;
1385 snd_iprintf(buffer,
1386 " Mode : %s\n"
1387 " Buffer size : %lu\n"
1388 " Avail : %lu\n",
1389 runtime->oss ? "OSS compatible" : "native",
1390 (unsigned long) runtime->buffer_size,
1391 (unsigned long) runtime->avail);
1392 }
1393 }
1394 }
1395 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001396 list_for_each_entry(substream,
1397 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1398 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 snd_iprintf(buffer,
1400 "Input %d\n"
1401 " Rx bytes : %lu\n",
1402 substream->number,
1403 (unsigned long) substream->bytes);
1404 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001405 snd_iprintf(buffer,
1406 " Owner PID : %d\n",
1407 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 runtime = substream->runtime;
1409 snd_iprintf(buffer,
1410 " Buffer size : %lu\n"
1411 " Avail : %lu\n"
1412 " Overruns : %lu\n",
1413 (unsigned long) runtime->buffer_size,
1414 (unsigned long) runtime->avail,
1415 (unsigned long) runtime->xruns);
1416 }
1417 }
1418 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001419 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
1422/*
1423 * Register functions
1424 */
1425
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001426static const struct file_operations snd_rawmidi_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 .owner = THIS_MODULE,
1429 .read = snd_rawmidi_read,
1430 .write = snd_rawmidi_write,
1431 .open = snd_rawmidi_open,
1432 .release = snd_rawmidi_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001433 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 .poll = snd_rawmidi_poll,
1435 .unlocked_ioctl = snd_rawmidi_ioctl,
1436 .compat_ioctl = snd_rawmidi_ioctl_compat,
1437};
1438
Takashi Iwai48c9d412005-11-17 13:56:51 +01001439static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1440 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 int direction,
1442 int count)
1443{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001444 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int idx;
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001448 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001449 if (substream == NULL) {
1450 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 substream->stream = direction;
1454 substream->number = idx;
1455 substream->rmidi = rmidi;
1456 substream->pstr = stream;
1457 list_add_tail(&substream->list, &stream->substreams);
1458 stream->substream_count++;
1459 }
1460 return 0;
1461}
1462
1463/**
1464 * snd_rawmidi_new - create a rawmidi instance
1465 * @card: the card instance
1466 * @id: the id string
1467 * @device: the device index
1468 * @output_count: the number of output streams
1469 * @input_count: the number of input streams
1470 * @rrawmidi: the pointer to store the new rawmidi instance
1471 *
1472 * Creates a new rawmidi instance.
1473 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1474 *
1475 * Returns zero if successful, or a negative error code on failure.
1476 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001477int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 int output_count, int input_count,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001479 struct snd_rawmidi ** rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001481 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001483 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 .dev_free = snd_rawmidi_dev_free,
1485 .dev_register = snd_rawmidi_dev_register,
1486 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 };
1488
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001489 if (snd_BUG_ON(!card))
1490 return -ENXIO;
1491 if (rrawmidi)
1492 *rrawmidi = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001493 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001494 if (rmidi == NULL) {
1495 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 rmidi->card = card;
1499 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001500 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001502 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1503 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (id != NULL)
1506 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001507 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1508 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1509 SNDRV_RAWMIDI_STREAM_INPUT,
1510 input_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 snd_rawmidi_free(rmidi);
1512 return err;
1513 }
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001514 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1515 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1516 SNDRV_RAWMIDI_STREAM_OUTPUT,
1517 output_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 snd_rawmidi_free(rmidi);
1519 return err;
1520 }
1521 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1522 snd_rawmidi_free(rmidi);
1523 return err;
1524 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001525 if (rrawmidi)
1526 *rrawmidi = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return 0;
1528}
1529
Takashi Iwai48c9d412005-11-17 13:56:51 +01001530static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001532 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001535 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 list_del(&substream->list);
1537 kfree(substream);
1538 }
1539}
1540
Takashi Iwai48c9d412005-11-17 13:56:51 +01001541static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001543 if (!rmidi)
1544 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +02001545
1546 snd_info_free_entry(rmidi->proc_entry);
1547 rmidi->proc_entry = NULL;
1548 mutex_lock(&register_mutex);
1549 if (rmidi->ops && rmidi->ops->dev_unregister)
1550 rmidi->ops->dev_unregister(rmidi);
1551 mutex_unlock(&register_mutex);
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1554 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1555 if (rmidi->private_free)
1556 rmidi->private_free(rmidi);
1557 kfree(rmidi);
1558 return 0;
1559}
1560
Takashi Iwai48c9d412005-11-17 13:56:51 +01001561static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001563 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return snd_rawmidi_free(rmidi);
1565}
1566
1567#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
Takashi Iwai48c9d412005-11-17 13:56:51 +01001568static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001570 struct snd_rawmidi *rmidi = device->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 rmidi->seq_dev = NULL;
1572}
1573#endif
1574
Takashi Iwai48c9d412005-11-17 13:56:51 +01001575static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001577 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001578 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001580 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1583 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001584 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001585 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001586 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return -EBUSY;
1588 }
Clemens Ladischf87135f2005-11-20 14:06:59 +01001589 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1591 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1592 rmidi->card, rmidi->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001593 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001595 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001596 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return err;
1598 }
1599 if (rmidi->ops && rmidi->ops->dev_register &&
1600 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1601 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001602 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001603 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return err;
1605 }
1606#ifdef CONFIG_SND_OSSEMUL
1607 rmidi->ossreg = 0;
1608 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1609 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001610 rmidi->card, 0, &snd_rawmidi_f_ops,
1611 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1613 } else {
1614 rmidi->ossreg++;
1615#ifdef SNDRV_OSS_INFO_DEV_MIDI
1616 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1617#endif
1618 }
1619 }
1620 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1621 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001622 rmidi->card, 1, &snd_rawmidi_f_ops,
1623 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1625 } else {
1626 rmidi->ossreg++;
1627 }
1628 }
1629#endif /* CONFIG_SND_OSSEMUL */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001630 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 sprintf(name, "midi%d", rmidi->device);
1632 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1633 if (entry) {
1634 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 entry->c.text.read = snd_rawmidi_proc_info_read;
1636 if (snd_info_register(entry) < 0) {
1637 snd_info_free_entry(entry);
1638 entry = NULL;
1639 }
1640 }
1641 rmidi->proc_entry = entry;
1642#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1643 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1644 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1645 rmidi->seq_dev->private_data = rmidi;
1646 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1647 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1648 snd_device_register(rmidi->card, rmidi->seq_dev);
1649 }
1650 }
1651#endif
1652 return 0;
1653}
1654
Takashi Iwai48c9d412005-11-17 13:56:51 +01001655static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001657 struct snd_rawmidi *rmidi = device->device_data;
Takashi Iwai80d04292012-11-07 12:42:48 +01001658 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001660 mutex_lock(&register_mutex);
Takashi Iwai80d04292012-11-07 12:42:48 +01001661 mutex_lock(&rmidi->open_mutex);
1662 wake_up(&rmidi->open_wait);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001663 list_del_init(&rmidi->list);
Takashi Iwai80d04292012-11-07 12:42:48 +01001664 for (dir = 0; dir < 2; dir++) {
1665 struct snd_rawmidi_substream *s;
1666 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1667 if (s->runtime)
1668 wake_up(&s->runtime->sleep);
1669 }
1670 }
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672#ifdef CONFIG_SND_OSSEMUL
1673 if (rmidi->ossreg) {
1674 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1675 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1676#ifdef SNDRV_OSS_INFO_DEV_MIDI
1677 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1678#endif
1679 }
1680 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1681 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1682 rmidi->ossreg = 0;
1683 }
1684#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Takashi Iwai80d04292012-11-07 12:42:48 +01001686 mutex_unlock(&rmidi->open_mutex);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001687 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
1691/**
1692 * snd_rawmidi_set_ops - set the rawmidi operators
1693 * @rmidi: the rawmidi instance
1694 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1695 * @ops: the operator table
1696 *
1697 * Sets the rawmidi operators for the given stream direction.
1698 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001699void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1700 struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001702 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Johannes Berg9244b2c2006-10-05 16:02:22 +02001704 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
1708/*
1709 * ENTRY functions
1710 */
1711
1712static int __init alsa_rawmidi_init(void)
1713{
1714
1715 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1716 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1717#ifdef CONFIG_SND_OSSEMUL
1718 { int i;
1719 /* check device map table */
1720 for (i = 0; i < SNDRV_CARDS; i++) {
1721 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1722 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1723 midi_map[i] = 0;
1724 }
1725 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1726 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1727 amidi_map[i] = 1;
1728 }
1729 }
1730 }
1731#endif /* CONFIG_SND_OSSEMUL */
1732 return 0;
1733}
1734
1735static void __exit alsa_rawmidi_exit(void)
1736{
1737 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1738 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1739}
1740
1741module_init(alsa_rawmidi_init)
1742module_exit(alsa_rawmidi_exit)
1743
1744EXPORT_SYMBOL(snd_rawmidi_output_params);
1745EXPORT_SYMBOL(snd_rawmidi_input_params);
1746EXPORT_SYMBOL(snd_rawmidi_drop_output);
1747EXPORT_SYMBOL(snd_rawmidi_drain_output);
1748EXPORT_SYMBOL(snd_rawmidi_drain_input);
1749EXPORT_SYMBOL(snd_rawmidi_receive);
1750EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1751EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1752EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1753EXPORT_SYMBOL(snd_rawmidi_transmit);
1754EXPORT_SYMBOL(snd_rawmidi_new);
1755EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756EXPORT_SYMBOL(snd_rawmidi_info_select);
1757EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1758EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1759EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1760EXPORT_SYMBOL(snd_rawmidi_kernel_write);