blob: eb68326c37d47626b53d7970fe6e88027edc0e13 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/moduleparam.h>
31#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
95static void snd_rawmidi_input_event_tasklet(unsigned long data)
96{
Takashi Iwai48c9d412005-11-17 13:56:51 +010097 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 substream->runtime->event(substream);
99}
100
101static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
102{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100103 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 substream->ops->trigger(substream, 1);
105}
106
Takashi Iwai48c9d412005-11-17 13:56:51 +0100107static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100109 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Takashi Iwaica2c0962005-09-09 14:20:23 +0200111 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -ENOMEM;
113 spin_lock_init(&runtime->lock);
114 init_waitqueue_head(&runtime->sleep);
115 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
116 tasklet_init(&runtime->tasklet,
117 snd_rawmidi_input_event_tasklet,
118 (unsigned long)substream);
119 else
120 tasklet_init(&runtime->tasklet,
121 snd_rawmidi_output_trigger_tasklet,
122 (unsigned long)substream);
123 runtime->event = NULL;
124 runtime->buffer_size = PAGE_SIZE;
125 runtime->avail_min = 1;
126 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
127 runtime->avail = 0;
128 else
129 runtime->avail = runtime->buffer_size;
130 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
131 kfree(runtime);
132 return -ENOMEM;
133 }
134 runtime->appl_ptr = runtime->hw_ptr = 0;
135 substream->runtime = runtime;
136 return 0;
137}
138
Takashi Iwai48c9d412005-11-17 13:56:51 +0100139static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100141 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 kfree(runtime->buffer);
144 kfree(runtime);
145 substream->runtime = NULL;
146 return 0;
147}
148
Takashi Iwai48c9d412005-11-17 13:56:51 +0100149static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Takashi Iwai219df322008-11-03 08:17:05 +0100151 if (!substream->opened)
152 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (up) {
Takashi Iwai1f041282008-12-18 12:17:55 +0100154 tasklet_schedule(&substream->runtime->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 } else {
156 tasklet_kill(&substream->runtime->tasklet);
157 substream->ops->trigger(substream, 0);
158 }
159}
160
Takashi Iwai48c9d412005-11-17 13:56:51 +0100161static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Takashi Iwai219df322008-11-03 08:17:05 +0100163 if (!substream->opened)
164 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 substream->ops->trigger(substream, up);
166 if (!up && substream->runtime->event)
167 tasklet_kill(&substream->runtime->tasklet);
168}
169
Takashi Iwai48c9d412005-11-17 13:56:51 +0100170int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100173 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 snd_rawmidi_output_trigger(substream, 0);
176 runtime->drain = 0;
177 spin_lock_irqsave(&runtime->lock, flags);
178 runtime->appl_ptr = runtime->hw_ptr = 0;
179 runtime->avail = runtime->buffer_size;
180 spin_unlock_irqrestore(&runtime->lock, flags);
181 return 0;
182}
183
Takashi Iwai48c9d412005-11-17 13:56:51 +0100184int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 int err;
187 long timeout;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100188 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 err = 0;
191 runtime->drain = 1;
192 timeout = wait_event_interruptible_timeout(runtime->sleep,
193 (runtime->avail >= runtime->buffer_size),
194 10*HZ);
195 if (signal_pending(current))
196 err = -ERESTARTSYS;
197 if (runtime->avail < runtime->buffer_size && !timeout) {
198 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
199 err = -EIO;
200 }
201 runtime->drain = 0;
202 if (err != -ERESTARTSYS) {
203 /* we need wait a while to make sure that Tx FIFOs are empty */
204 if (substream->ops->drain)
205 substream->ops->drain(substream);
206 else
207 msleep(50);
208 snd_rawmidi_drop_output(substream);
209 }
210 return err;
211}
212
Takashi Iwai48c9d412005-11-17 13:56:51 +0100213int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100216 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 snd_rawmidi_input_trigger(substream, 0);
219 runtime->drain = 0;
220 spin_lock_irqsave(&runtime->lock, flags);
221 runtime->appl_ptr = runtime->hw_ptr = 0;
222 runtime->avail = 0;
223 spin_unlock_irqrestore(&runtime->lock, flags);
224 return 0;
225}
226
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100227/* look for an available substream for the given stream direction;
228 * if a specific subdevice is given, try to assign it
229 */
230static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
231 int stream, int mode,
232 struct snd_rawmidi_substream **sub_ret)
233{
234 struct snd_rawmidi_substream *substream;
235 struct snd_rawmidi_str *s = &rmidi->streams[stream];
236 static unsigned int info_flags[2] = {
237 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
238 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
239 };
240
241 if (!(rmidi->info_flags & info_flags[stream]))
242 return -ENXIO;
243 if (subdevice >= 0 && subdevice >= s->substream_count)
244 return -ENODEV;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100245
246 list_for_each_entry(substream, &s->substreams, list) {
247 if (substream->opened) {
248 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
Clemens Ladisch16fb1092009-10-21 09:10:16 +0200249 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
250 !substream->append)
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100251 continue;
252 }
253 if (subdevice < 0 || subdevice == substream->number) {
254 *sub_ret = substream;
255 return 0;
256 }
257 }
258 return -EAGAIN;
259}
260
261/* open and do ref-counting for the given substream */
262static int open_substream(struct snd_rawmidi *rmidi,
263 struct snd_rawmidi_substream *substream,
264 int mode)
265{
266 int err;
267
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200268 if (substream->use_count == 0) {
269 err = snd_rawmidi_runtime_create(substream);
270 if (err < 0)
271 return err;
272 err = substream->ops->open(substream);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200273 if (err < 0) {
274 snd_rawmidi_runtime_free(substream);
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200275 return err;
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200276 }
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200277 substream->opened = 1;
Clemens Ladisch2d4b8422009-07-13 13:52:46 +0200278 substream->active_sensing = 0;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200279 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
280 substream->append = 1;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100281 substream->pid = get_pid(task_pid(current));
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200282 rmidi->streams[substream->stream].substream_opened++;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200283 }
284 substream->use_count++;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100285 return 0;
286}
287
288static void close_substream(struct snd_rawmidi *rmidi,
289 struct snd_rawmidi_substream *substream,
290 int cleanup);
291
292static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
293 struct snd_rawmidi_file *rfile)
294{
295 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
296 int err;
297
298 rfile->input = rfile->output = NULL;
299 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
300 err = assign_substream(rmidi, subdevice,
301 SNDRV_RAWMIDI_STREAM_INPUT,
302 mode, &sinput);
303 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200304 return err;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100305 }
306 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
307 err = assign_substream(rmidi, subdevice,
308 SNDRV_RAWMIDI_STREAM_OUTPUT,
309 mode, &soutput);
310 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200311 return err;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100312 }
313
314 if (sinput) {
315 err = open_substream(rmidi, sinput, mode);
316 if (err < 0)
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200317 return err;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100318 }
319 if (soutput) {
320 err = open_substream(rmidi, soutput, mode);
321 if (err < 0) {
322 if (sinput)
323 close_substream(rmidi, sinput, 0);
Clemens Ladischb7fe7502009-10-21 09:11:43 +0200324 return err;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100325 }
326 }
327
328 rfile->rmidi = rmidi;
329 rfile->input = sinput;
330 rfile->output = soutput;
331 return 0;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100332}
333
334/* called from sound/core/seq/seq_midi.c */
Clemens Ladischf87135f2005-11-20 14:06:59 +0100335int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai48c9d412005-11-17 13:56:51 +0100336 int mode, struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100338 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 int err;
340
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100341 if (snd_BUG_ON(!rfile))
342 return -EINVAL;
343
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100344 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100345 rmidi = snd_rawmidi_search(card, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (rmidi == NULL) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100347 mutex_unlock(&register_mutex);
348 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 if (!try_module_get(rmidi->card->module)) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100351 mutex_unlock(&register_mutex);
352 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Takashi Iwaif9d20282009-02-11 14:55:59 +0100354 mutex_unlock(&register_mutex);
355
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100356 mutex_lock(&rmidi->open_mutex);
357 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
358 mutex_unlock(&rmidi->open_mutex);
359 if (err < 0)
360 module_put(rmidi->card->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return err;
362}
363
364static int snd_rawmidi_open(struct inode *inode, struct file *file)
365{
366 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100367 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100368 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 unsigned short fflags;
370 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100371 struct snd_rawmidi *rmidi;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100372 struct snd_rawmidi_file *rawmidi_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 wait_queue_t wait;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100374 struct snd_ctl_file *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100376 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
377 return -EINVAL; /* invalid combination */
378
Takashi Iwai02f48652010-04-13 11:49:04 +0200379 err = nonseekable_open(inode, file);
380 if (err < 0)
381 return err;
382
Clemens Ladischf1902862005-10-24 17:05:03 +0200383 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100384 rmidi = snd_lookup_minor_data(iminor(inode),
385 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200387 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100388 rmidi = snd_lookup_oss_minor_data(iminor(inode),
389 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200391 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (rmidi == NULL)
395 return -ENODEV;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100396
397 if (!try_module_get(rmidi->card->module))
398 return -ENXIO;
399
400 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 card = rmidi->card;
402 err = snd_card_file_add(card, file);
403 if (err < 0)
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100404 goto __error_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200406 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
409 if (rawmidi_file == NULL) {
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100410 err = -ENOMEM;
411 goto __error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 init_waitqueue_entry(&wait, current);
414 add_wait_queue(&rmidi->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 while (1) {
416 subdevice = -1;
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200417 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200418 list_for_each_entry(kctl, &card->ctl_files, list) {
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100419 if (kctl->pid == task_pid(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 subdevice = kctl->prefer_rawmidi_subdevice;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200421 if (subdevice != -1)
422 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 }
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200425 read_unlock(&card->ctl_files_rwlock);
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100426 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (err >= 0)
428 break;
429 if (err == -EAGAIN) {
430 if (file->f_flags & O_NONBLOCK) {
431 err = -EBUSY;
432 break;
433 }
434 } else
435 break;
436 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100437 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100439 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (signal_pending(current)) {
441 err = -ERESTARTSYS;
442 break;
443 }
444 }
Takashi Iwai9a1b64ca2009-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 Iwai9a1b64ca2009-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 Iwai9a1b64ca2009-02-11 17:03:49 +0100458 return 0;
459
460 __error:
461 snd_card_file_remove(card, file);
462 __error_card:
463 mutex_unlock(&rmidi->open_mutex);
464 module_put(rmidi->card->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return err;
466}
467
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100468static void close_substream(struct snd_rawmidi *rmidi,
469 struct snd_rawmidi_substream *substream,
470 int cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100472 if (--substream->use_count)
473 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100475 if (cleanup) {
476 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
477 snd_rawmidi_input_trigger(substream, 0);
478 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (substream->active_sensing) {
480 unsigned char buf = 0xfe;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100481 /* sending single active sensing message
482 * to shut the device up
483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 snd_rawmidi_kernel_write(substream, &buf, 1);
485 }
486 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
487 snd_rawmidi_output_trigger(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100490 substream->ops->close(substream);
491 if (substream->runtime->private_free)
492 substream->runtime->private_free(substream);
493 snd_rawmidi_runtime_free(substream);
494 substream->opened = 0;
495 substream->append = 0;
Clemens Ladisch7584af12009-11-10 10:14:04 +0100496 put_pid(substream->pid);
497 substream->pid = NULL;
Clemens Ladisch91d12c42009-10-21 09:12:26 +0200498 rmidi->streams[substream->stream].substream_opened--;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100499}
500
501static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
502{
503 struct snd_rawmidi *rmidi;
504
505 rmidi = rfile->rmidi;
506 mutex_lock(&rmidi->open_mutex);
507 if (rfile->input) {
508 close_substream(rmidi, rfile->input, 1);
509 rfile->input = NULL;
510 }
511 if (rfile->output) {
512 close_substream(rmidi, rfile->output, 1);
513 rfile->output = NULL;
514 }
515 rfile->rmidi = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100516 mutex_unlock(&rmidi->open_mutex);
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100517 wake_up(&rmidi->open_wait);
518}
519
520/* called from sound/core/seq/seq_midi.c */
521int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
522{
523 struct snd_rawmidi *rmidi;
524
525 if (snd_BUG_ON(!rfile))
526 return -ENXIO;
527
528 rmidi = rfile->rmidi;
529 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 module_put(rmidi->card->module);
531 return 0;
532}
533
534static int snd_rawmidi_release(struct inode *inode, struct file *file)
535{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100536 struct snd_rawmidi_file *rfile;
537 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 rfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 rmidi = rfile->rmidi;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100541 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 kfree(rfile);
543 snd_card_file_remove(rmidi->card, file);
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100544 module_put(rmidi->card->module);
545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Adrian Bunk18612042005-11-23 13:14:50 +0100548static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
549 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100551 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 if (substream == NULL)
554 return -ENODEV;
555 rmidi = substream->rmidi;
556 memset(info, 0, sizeof(*info));
557 info->card = rmidi->card->number;
558 info->device = rmidi->device;
559 info->subdevice = substream->number;
560 info->stream = substream->stream;
561 info->flags = rmidi->info_flags;
562 strcpy(info->id, rmidi->id);
563 strcpy(info->name, rmidi->name);
564 strcpy(info->subname, substream->name);
565 info->subdevices_count = substream->pstr->substream_count;
566 info->subdevices_avail = (substream->pstr->substream_count -
567 substream->pstr->substream_opened);
568 return 0;
569}
570
Takashi Iwai48c9d412005-11-17 13:56:51 +0100571static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
572 struct snd_rawmidi_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100574 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int err;
576 if ((err = snd_rawmidi_info(substream, &info)) < 0)
577 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100578 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return -EFAULT;
580 return 0;
581}
582
Takashi Iwai48c9d412005-11-17 13:56:51 +0100583int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100585 struct snd_rawmidi *rmidi;
586 struct snd_rawmidi_str *pstr;
587 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100588
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100589 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100590 rmidi = snd_rawmidi_search(card, info->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100591 mutex_unlock(&register_mutex);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100592 if (!rmidi)
593 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (info->stream < 0 || info->stream > 1)
595 return -EINVAL;
596 pstr = &rmidi->streams[info->stream];
597 if (pstr->substream_count == 0)
598 return -ENOENT;
599 if (info->subdevice >= pstr->substream_count)
600 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200601 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if ((unsigned int)substream->number == info->subdevice)
603 return snd_rawmidi_info(substream, info);
604 }
605 return -ENXIO;
606}
607
Takashi Iwai48c9d412005-11-17 13:56:51 +0100608static int snd_rawmidi_info_select_user(struct snd_card *card,
609 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100612 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (get_user(info.device, &_info->device))
614 return -EFAULT;
615 if (get_user(info.stream, &_info->stream))
616 return -EFAULT;
617 if (get_user(info.subdevice, &_info->subdevice))
618 return -EFAULT;
619 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
620 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100621 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return -EFAULT;
623 return 0;
624}
625
Takashi Iwai48c9d412005-11-17 13:56:51 +0100626int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
627 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100630 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if (substream->append && substream->use_count > 1)
633 return -EBUSY;
634 snd_rawmidi_drain_output(substream);
635 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
636 return -EINVAL;
637 }
638 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
639 return -EINVAL;
640 }
641 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800642 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
643 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -ENOMEM;
645 kfree(runtime->buffer);
646 runtime->buffer = newbuf;
647 runtime->buffer_size = params->buffer_size;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100648 runtime->avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 runtime->avail_min = params->avail_min;
651 substream->active_sensing = !params->no_active_sensing;
652 return 0;
653}
654
Takashi Iwai48c9d412005-11-17 13:56:51 +0100655int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
656 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100659 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 snd_rawmidi_drain_input(substream);
662 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
663 return -EINVAL;
664 }
665 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
666 return -EINVAL;
667 }
668 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800669 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
670 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return -ENOMEM;
672 kfree(runtime->buffer);
673 runtime->buffer = newbuf;
674 runtime->buffer_size = params->buffer_size;
675 }
676 runtime->avail_min = params->avail_min;
677 return 0;
678}
679
Takashi Iwai48c9d412005-11-17 13:56:51 +0100680static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
681 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100683 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 memset(status, 0, sizeof(*status));
686 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
687 spin_lock_irq(&runtime->lock);
688 status->avail = runtime->avail;
689 spin_unlock_irq(&runtime->lock);
690 return 0;
691}
692
Takashi Iwai48c9d412005-11-17 13:56:51 +0100693static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
694 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100696 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 memset(status, 0, sizeof(*status));
699 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
700 spin_lock_irq(&runtime->lock);
701 status->avail = runtime->avail;
702 status->xruns = runtime->xruns;
703 runtime->xruns = 0;
704 spin_unlock_irq(&runtime->lock);
705 return 0;
706}
707
708static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
709{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100710 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 void __user *argp = (void __user *)arg;
712
713 rfile = file->private_data;
714 if (((cmd >> 8) & 0xff) != 'W')
715 return -ENOTTY;
716 switch (cmd) {
717 case SNDRV_RAWMIDI_IOCTL_PVERSION:
718 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
719 case SNDRV_RAWMIDI_IOCTL_INFO:
720 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100721 int stream;
722 struct snd_rawmidi_info __user *info = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (get_user(stream, &info->stream))
724 return -EFAULT;
725 switch (stream) {
726 case SNDRV_RAWMIDI_STREAM_INPUT:
727 return snd_rawmidi_info_user(rfile->input, info);
728 case SNDRV_RAWMIDI_STREAM_OUTPUT:
729 return snd_rawmidi_info_user(rfile->output, info);
730 default:
731 return -EINVAL;
732 }
733 }
734 case SNDRV_RAWMIDI_IOCTL_PARAMS:
735 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100736 struct snd_rawmidi_params params;
737 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -EFAULT;
739 switch (params.stream) {
740 case SNDRV_RAWMIDI_STREAM_OUTPUT:
741 if (rfile->output == NULL)
742 return -EINVAL;
743 return snd_rawmidi_output_params(rfile->output, &params);
744 case SNDRV_RAWMIDI_STREAM_INPUT:
745 if (rfile->input == NULL)
746 return -EINVAL;
747 return snd_rawmidi_input_params(rfile->input, &params);
748 default:
749 return -EINVAL;
750 }
751 }
752 case SNDRV_RAWMIDI_IOCTL_STATUS:
753 {
754 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100755 struct snd_rawmidi_status status;
756 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return -EFAULT;
758 switch (status.stream) {
759 case SNDRV_RAWMIDI_STREAM_OUTPUT:
760 if (rfile->output == NULL)
761 return -EINVAL;
762 err = snd_rawmidi_output_status(rfile->output, &status);
763 break;
764 case SNDRV_RAWMIDI_STREAM_INPUT:
765 if (rfile->input == NULL)
766 return -EINVAL;
767 err = snd_rawmidi_input_status(rfile->input, &status);
768 break;
769 default:
770 return -EINVAL;
771 }
772 if (err < 0)
773 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100774 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return -EFAULT;
776 return 0;
777 }
778 case SNDRV_RAWMIDI_IOCTL_DROP:
779 {
780 int val;
781 if (get_user(val, (int __user *) argp))
782 return -EFAULT;
783 switch (val) {
784 case SNDRV_RAWMIDI_STREAM_OUTPUT:
785 if (rfile->output == NULL)
786 return -EINVAL;
787 return snd_rawmidi_drop_output(rfile->output);
788 default:
789 return -EINVAL;
790 }
791 }
792 case SNDRV_RAWMIDI_IOCTL_DRAIN:
793 {
794 int val;
795 if (get_user(val, (int __user *) argp))
796 return -EFAULT;
797 switch (val) {
798 case SNDRV_RAWMIDI_STREAM_OUTPUT:
799 if (rfile->output == NULL)
800 return -EINVAL;
801 return snd_rawmidi_drain_output(rfile->output);
802 case SNDRV_RAWMIDI_STREAM_INPUT:
803 if (rfile->input == NULL)
804 return -EINVAL;
805 return snd_rawmidi_drain_input(rfile->input);
806 default:
807 return -EINVAL;
808 }
809 }
810#ifdef CONFIG_SND_DEBUG
811 default:
812 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
813#endif
814 }
815 return -ENOTTY;
816}
817
Takashi Iwai48c9d412005-11-17 13:56:51 +0100818static int snd_rawmidi_control_ioctl(struct snd_card *card,
819 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 unsigned int cmd,
821 unsigned long arg)
822{
823 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 switch (cmd) {
826 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
827 {
828 int device;
829
830 if (get_user(device, (int __user *)argp))
831 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100832 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 device = device < 0 ? 0 : device + 1;
834 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100835 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
837 device++;
838 }
839 if (device == SNDRV_RAWMIDI_DEVICES)
840 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100841 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (put_user(device, (int __user *)argp))
843 return -EFAULT;
844 return 0;
845 }
846 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
847 {
848 int val;
849
850 if (get_user(val, (int __user *)argp))
851 return -EFAULT;
852 control->prefer_rawmidi_subdevice = val;
853 return 0;
854 }
855 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
856 return snd_rawmidi_info_select_user(card, argp);
857 }
858 return -ENOIOCTLCMD;
859}
860
861/**
862 * snd_rawmidi_receive - receive the input data from the device
863 * @substream: the rawmidi substream
864 * @buffer: the buffer pointer
865 * @count: the data size to read
866 *
867 * Reads the data from the internal buffer.
868 *
869 * Returns the size of read data, or a negative error code on failure.
870 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100871int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
872 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 unsigned long flags;
875 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100876 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Takashi Iwai219df322008-11-03 08:17:05 +0100878 if (!substream->opened)
879 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (runtime->buffer == NULL) {
881 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
882 return -EINVAL;
883 }
884 spin_lock_irqsave(&runtime->lock, flags);
885 if (count == 1) { /* special case, faster code */
886 substream->bytes++;
887 if (runtime->avail < runtime->buffer_size) {
888 runtime->buffer[runtime->hw_ptr++] = buffer[0];
889 runtime->hw_ptr %= runtime->buffer_size;
890 runtime->avail++;
891 result++;
892 } else {
893 runtime->xruns++;
894 }
895 } else {
896 substream->bytes += count;
897 count1 = runtime->buffer_size - runtime->hw_ptr;
898 if (count1 > count)
899 count1 = count;
900 if (count1 > (int)(runtime->buffer_size - runtime->avail))
901 count1 = runtime->buffer_size - runtime->avail;
902 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
903 runtime->hw_ptr += count1;
904 runtime->hw_ptr %= runtime->buffer_size;
905 runtime->avail += count1;
906 count -= count1;
907 result += count1;
908 if (count > 0) {
909 buffer += count1;
910 count1 = count;
911 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
912 count1 = runtime->buffer_size - runtime->avail;
913 runtime->xruns += count - count1;
914 }
915 if (count1 > 0) {
916 memcpy(runtime->buffer, buffer, count1);
917 runtime->hw_ptr = count1;
918 runtime->avail += count1;
919 result += count1;
920 }
921 }
922 }
923 if (result > 0) {
924 if (runtime->event)
Takashi Iwai1f041282008-12-18 12:17:55 +0100925 tasklet_schedule(&runtime->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 else if (snd_rawmidi_ready(substream))
927 wake_up(&runtime->sleep);
928 }
929 spin_unlock_irqrestore(&runtime->lock, flags);
930 return result;
931}
932
Takashi Iwai48c9d412005-11-17 13:56:51 +0100933static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100934 unsigned char __user *userbuf,
935 unsigned char *kernelbuf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 unsigned long flags;
938 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100939 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 while (count > 0 && runtime->avail) {
942 count1 = runtime->buffer_size - runtime->appl_ptr;
943 if (count1 > count)
944 count1 = count;
945 spin_lock_irqsave(&runtime->lock, flags);
946 if (count1 > (int)runtime->avail)
947 count1 = runtime->avail;
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100948 if (kernelbuf)
949 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
950 if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 spin_unlock_irqrestore(&runtime->lock, flags);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100952 if (copy_to_user(userbuf + result,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 runtime->buffer + runtime->appl_ptr, count1)) {
954 return result > 0 ? result : -EFAULT;
955 }
956 spin_lock_irqsave(&runtime->lock, flags);
957 }
958 runtime->appl_ptr += count1;
959 runtime->appl_ptr %= runtime->buffer_size;
960 runtime->avail -= count1;
961 spin_unlock_irqrestore(&runtime->lock, flags);
962 result += count1;
963 count -= count1;
964 }
965 return result;
966}
967
Takashi Iwai48c9d412005-11-17 13:56:51 +0100968long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
969 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 snd_rawmidi_input_trigger(substream, 1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100972 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Takashi Iwai48c9d412005-11-17 13:56:51 +0100975static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
976 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 long result;
979 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100980 struct snd_rawmidi_file *rfile;
981 struct snd_rawmidi_substream *substream;
982 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 rfile = file->private_data;
985 substream = rfile->input;
986 if (substream == NULL)
987 return -EIO;
988 runtime = substream->runtime;
989 snd_rawmidi_input_trigger(substream, 1);
990 result = 0;
991 while (count > 0) {
992 spin_lock_irq(&runtime->lock);
993 while (!snd_rawmidi_ready(substream)) {
994 wait_queue_t wait;
995 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
996 spin_unlock_irq(&runtime->lock);
997 return result > 0 ? result : -EAGAIN;
998 }
999 init_waitqueue_entry(&wait, current);
1000 add_wait_queue(&runtime->sleep, &wait);
1001 set_current_state(TASK_INTERRUPTIBLE);
1002 spin_unlock_irq(&runtime->lock);
1003 schedule();
1004 remove_wait_queue(&runtime->sleep, &wait);
1005 if (signal_pending(current))
1006 return result > 0 ? result : -ERESTARTSYS;
1007 if (!runtime->avail)
1008 return result > 0 ? result : -EIO;
1009 spin_lock_irq(&runtime->lock);
1010 }
1011 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001012 count1 = snd_rawmidi_kernel_read1(substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001013 (unsigned char __user *)buf,
1014 NULL/*kernelbuf*/,
1015 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (count1 < 0)
1017 return result > 0 ? result : count1;
1018 result += count1;
1019 buf += count1;
1020 count -= count1;
1021 }
1022 return result;
1023}
1024
1025/**
1026 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1027 * @substream: the rawmidi substream
1028 *
1029 * Returns 1 if the internal output buffer is empty, 0 if not.
1030 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001031int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001033 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 int result;
1035 unsigned long flags;
1036
1037 if (runtime->buffer == NULL) {
1038 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1039 return 1;
1040 }
1041 spin_lock_irqsave(&runtime->lock, flags);
1042 result = runtime->avail >= runtime->buffer_size;
1043 spin_unlock_irqrestore(&runtime->lock, flags);
1044 return result;
1045}
1046
1047/**
1048 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1049 * @substream: the rawmidi substream
1050 * @buffer: the buffer pointer
1051 * @count: data size to transfer
1052 *
1053 * Copies data from the internal output buffer to the given buffer.
1054 *
1055 * Call this in the interrupt handler when the midi output is ready,
1056 * and call snd_rawmidi_transmit_ack() after the transmission is
1057 * finished.
1058 *
1059 * Returns the size of copied data, or a negative error code on failure.
1060 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001061int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1062 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 unsigned long flags;
1065 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001066 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 if (runtime->buffer == NULL) {
1069 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1070 return -EINVAL;
1071 }
1072 result = 0;
1073 spin_lock_irqsave(&runtime->lock, flags);
1074 if (runtime->avail >= runtime->buffer_size) {
1075 /* warning: lowlevel layer MUST trigger down the hardware */
1076 goto __skip;
1077 }
1078 if (count == 1) { /* special case, faster code */
1079 *buffer = runtime->buffer[runtime->hw_ptr];
1080 result++;
1081 } else {
1082 count1 = runtime->buffer_size - runtime->hw_ptr;
1083 if (count1 > count)
1084 count1 = count;
1085 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1086 count1 = runtime->buffer_size - runtime->avail;
1087 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1088 count -= count1;
1089 result += count1;
1090 if (count > 0) {
1091 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1092 count = runtime->buffer_size - runtime->avail - count1;
1093 memcpy(buffer + count1, runtime->buffer, count);
1094 result += count;
1095 }
1096 }
1097 __skip:
1098 spin_unlock_irqrestore(&runtime->lock, flags);
1099 return result;
1100}
1101
1102/**
1103 * snd_rawmidi_transmit_ack - acknowledge the transmission
1104 * @substream: the rawmidi substream
1105 * @count: the tranferred count
1106 *
1107 * Advances the hardware pointer for the internal output buffer with
1108 * the given size and updates the condition.
1109 * Call after the transmission is finished.
1110 *
1111 * Returns the advanced size if successful, or a negative error code on failure.
1112 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001113int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001116 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (runtime->buffer == NULL) {
1119 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1120 return -EINVAL;
1121 }
1122 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001123 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 runtime->hw_ptr += count;
1125 runtime->hw_ptr %= runtime->buffer_size;
1126 runtime->avail += count;
1127 substream->bytes += count;
1128 if (count > 0) {
1129 if (runtime->drain || snd_rawmidi_ready(substream))
1130 wake_up(&runtime->sleep);
1131 }
1132 spin_unlock_irqrestore(&runtime->lock, flags);
1133 return count;
1134}
1135
1136/**
1137 * snd_rawmidi_transmit - copy from the buffer to the device
1138 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001139 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * @count: the data size to transfer
1141 *
1142 * Copies data from the buffer to the device and advances the pointer.
1143 *
1144 * Returns the copied size if successful, or a negative error code on failure.
1145 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001146int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1147 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Takashi Iwai219df322008-11-03 08:17:05 +01001149 if (!substream->opened)
1150 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1152 if (count < 0)
1153 return count;
1154 return snd_rawmidi_transmit_ack(substream, count);
1155}
1156
Takashi Iwai48c9d412005-11-17 13:56:51 +01001157static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001158 const unsigned char __user *userbuf,
1159 const unsigned char *kernelbuf,
1160 long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 unsigned long flags;
1163 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001164 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001166 if (snd_BUG_ON(!kernelbuf && !userbuf))
1167 return -EINVAL;
1168 if (snd_BUG_ON(!runtime->buffer))
1169 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 result = 0;
1172 spin_lock_irqsave(&runtime->lock, flags);
1173 if (substream->append) {
1174 if ((long)runtime->avail < count) {
1175 spin_unlock_irqrestore(&runtime->lock, flags);
1176 return -EAGAIN;
1177 }
1178 }
1179 while (count > 0 && runtime->avail > 0) {
1180 count1 = runtime->buffer_size - runtime->appl_ptr;
1181 if (count1 > count)
1182 count1 = count;
1183 if (count1 > (long)runtime->avail)
1184 count1 = runtime->avail;
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001185 if (kernelbuf)
1186 memcpy(runtime->buffer + runtime->appl_ptr,
1187 kernelbuf + result, count1);
1188 else if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 spin_unlock_irqrestore(&runtime->lock, flags);
1190 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001191 userbuf + result, count1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 spin_lock_irqsave(&runtime->lock, flags);
1193 result = result > 0 ? result : -EFAULT;
1194 goto __end;
1195 }
1196 spin_lock_irqsave(&runtime->lock, flags);
1197 }
1198 runtime->appl_ptr += count1;
1199 runtime->appl_ptr %= runtime->buffer_size;
1200 runtime->avail -= count1;
1201 result += count1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 count -= count1;
1203 }
1204 __end:
1205 count1 = runtime->avail < runtime->buffer_size;
1206 spin_unlock_irqrestore(&runtime->lock, flags);
1207 if (count1)
1208 snd_rawmidi_output_trigger(substream, 1);
1209 return result;
1210}
1211
Takashi Iwai48c9d412005-11-17 13:56:51 +01001212long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1213 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001215 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Takashi Iwai48c9d412005-11-17 13:56:51 +01001218static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1219 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 long result, timeout;
1222 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001223 struct snd_rawmidi_file *rfile;
1224 struct snd_rawmidi_runtime *runtime;
1225 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 rfile = file->private_data;
1228 substream = rfile->output;
1229 runtime = substream->runtime;
1230 /* we cannot put an atomic message to our buffer */
1231 if (substream->append && count > runtime->buffer_size)
1232 return -EIO;
1233 result = 0;
1234 while (count > 0) {
1235 spin_lock_irq(&runtime->lock);
1236 while (!snd_rawmidi_ready_append(substream, count)) {
1237 wait_queue_t wait;
1238 if (file->f_flags & O_NONBLOCK) {
1239 spin_unlock_irq(&runtime->lock);
1240 return result > 0 ? result : -EAGAIN;
1241 }
1242 init_waitqueue_entry(&wait, current);
1243 add_wait_queue(&runtime->sleep, &wait);
1244 set_current_state(TASK_INTERRUPTIBLE);
1245 spin_unlock_irq(&runtime->lock);
1246 timeout = schedule_timeout(30 * HZ);
1247 remove_wait_queue(&runtime->sleep, &wait);
1248 if (signal_pending(current))
1249 return result > 0 ? result : -ERESTARTSYS;
1250 if (!runtime->avail && !timeout)
1251 return result > 0 ? result : -EIO;
1252 spin_lock_irq(&runtime->lock);
1253 }
1254 spin_unlock_irq(&runtime->lock);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001255 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (count1 < 0)
1257 return result > 0 ? result : count1;
1258 result += count1;
1259 buf += count1;
1260 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1261 break;
1262 count -= count1;
1263 }
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001264 if (file->f_flags & O_DSYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 spin_lock_irq(&runtime->lock);
1266 while (runtime->avail != runtime->buffer_size) {
1267 wait_queue_t wait;
1268 unsigned int last_avail = runtime->avail;
1269 init_waitqueue_entry(&wait, current);
1270 add_wait_queue(&runtime->sleep, &wait);
1271 set_current_state(TASK_INTERRUPTIBLE);
1272 spin_unlock_irq(&runtime->lock);
1273 timeout = schedule_timeout(30 * HZ);
1274 remove_wait_queue(&runtime->sleep, &wait);
1275 if (signal_pending(current))
1276 return result > 0 ? result : -ERESTARTSYS;
1277 if (runtime->avail == last_avail && !timeout)
1278 return result > 0 ? result : -EIO;
1279 spin_lock_irq(&runtime->lock);
1280 }
1281 spin_unlock_irq(&runtime->lock);
1282 }
1283 return result;
1284}
1285
1286static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1287{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001288 struct snd_rawmidi_file *rfile;
1289 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 unsigned int mask;
1291
1292 rfile = file->private_data;
1293 if (rfile->input != NULL) {
1294 runtime = rfile->input->runtime;
1295 snd_rawmidi_input_trigger(rfile->input, 1);
1296 poll_wait(file, &runtime->sleep, wait);
1297 }
1298 if (rfile->output != NULL) {
1299 runtime = rfile->output->runtime;
1300 poll_wait(file, &runtime->sleep, wait);
1301 }
1302 mask = 0;
1303 if (rfile->input != NULL) {
1304 if (snd_rawmidi_ready(rfile->input))
1305 mask |= POLLIN | POLLRDNORM;
1306 }
1307 if (rfile->output != NULL) {
1308 if (snd_rawmidi_ready(rfile->output))
1309 mask |= POLLOUT | POLLWRNORM;
1310 }
1311 return mask;
1312}
1313
1314/*
1315 */
1316#ifdef CONFIG_COMPAT
1317#include "rawmidi_compat.c"
1318#else
1319#define snd_rawmidi_ioctl_compat NULL
1320#endif
1321
1322/*
1323
1324 */
1325
Takashi Iwai48c9d412005-11-17 13:56:51 +01001326static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1327 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001329 struct snd_rawmidi *rmidi;
1330 struct snd_rawmidi_substream *substream;
1331 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 rmidi = entry->private_data;
1334 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001335 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001337 list_for_each_entry(substream,
1338 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1339 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 snd_iprintf(buffer,
1341 "Output %d\n"
1342 " Tx bytes : %lu\n",
1343 substream->number,
1344 (unsigned long) substream->bytes);
1345 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001346 snd_iprintf(buffer,
1347 " Owner PID : %d\n",
1348 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 runtime = substream->runtime;
1350 snd_iprintf(buffer,
1351 " Mode : %s\n"
1352 " Buffer size : %lu\n"
1353 " Avail : %lu\n",
1354 runtime->oss ? "OSS compatible" : "native",
1355 (unsigned long) runtime->buffer_size,
1356 (unsigned long) runtime->avail);
1357 }
1358 }
1359 }
1360 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001361 list_for_each_entry(substream,
1362 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1363 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 snd_iprintf(buffer,
1365 "Input %d\n"
1366 " Rx bytes : %lu\n",
1367 substream->number,
1368 (unsigned long) substream->bytes);
1369 if (substream->opened) {
Clemens Ladisch7584af12009-11-10 10:14:04 +01001370 snd_iprintf(buffer,
1371 " Owner PID : %d\n",
1372 pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 runtime = substream->runtime;
1374 snd_iprintf(buffer,
1375 " Buffer size : %lu\n"
1376 " Avail : %lu\n"
1377 " Overruns : %lu\n",
1378 (unsigned long) runtime->buffer_size,
1379 (unsigned long) runtime->avail,
1380 (unsigned long) runtime->xruns);
1381 }
1382 }
1383 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001384 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
1387/*
1388 * Register functions
1389 */
1390
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001391static const struct file_operations snd_rawmidi_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 .owner = THIS_MODULE,
1394 .read = snd_rawmidi_read,
1395 .write = snd_rawmidi_write,
1396 .open = snd_rawmidi_open,
1397 .release = snd_rawmidi_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001398 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 .poll = snd_rawmidi_poll,
1400 .unlocked_ioctl = snd_rawmidi_ioctl,
1401 .compat_ioctl = snd_rawmidi_ioctl_compat,
1402};
1403
Takashi Iwai48c9d412005-11-17 13:56:51 +01001404static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1405 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int direction,
1407 int count)
1408{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001409 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 int idx;
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001413 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001414 if (substream == NULL) {
1415 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 substream->stream = direction;
1419 substream->number = idx;
1420 substream->rmidi = rmidi;
1421 substream->pstr = stream;
1422 list_add_tail(&substream->list, &stream->substreams);
1423 stream->substream_count++;
1424 }
1425 return 0;
1426}
1427
1428/**
1429 * snd_rawmidi_new - create a rawmidi instance
1430 * @card: the card instance
1431 * @id: the id string
1432 * @device: the device index
1433 * @output_count: the number of output streams
1434 * @input_count: the number of input streams
1435 * @rrawmidi: the pointer to store the new rawmidi instance
1436 *
1437 * Creates a new rawmidi instance.
1438 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1439 *
1440 * Returns zero if successful, or a negative error code on failure.
1441 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001442int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 int output_count, int input_count,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001444 struct snd_rawmidi ** rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001446 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001448 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 .dev_free = snd_rawmidi_dev_free,
1450 .dev_register = snd_rawmidi_dev_register,
1451 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 };
1453
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001454 if (snd_BUG_ON(!card))
1455 return -ENXIO;
1456 if (rrawmidi)
1457 *rrawmidi = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001458 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001459 if (rmidi == NULL) {
1460 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 rmidi->card = card;
1464 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001465 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001467 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1468 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (id != NULL)
1471 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001472 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1473 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1474 SNDRV_RAWMIDI_STREAM_INPUT,
1475 input_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 snd_rawmidi_free(rmidi);
1477 return err;
1478 }
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001479 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1480 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1481 SNDRV_RAWMIDI_STREAM_OUTPUT,
1482 output_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 snd_rawmidi_free(rmidi);
1484 return err;
1485 }
1486 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1487 snd_rawmidi_free(rmidi);
1488 return err;
1489 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001490 if (rrawmidi)
1491 *rrawmidi = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return 0;
1493}
1494
Takashi Iwai48c9d412005-11-17 13:56:51 +01001495static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001497 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001500 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 list_del(&substream->list);
1502 kfree(substream);
1503 }
1504}
1505
Takashi Iwai48c9d412005-11-17 13:56:51 +01001506static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001508 if (!rmidi)
1509 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +02001510
1511 snd_info_free_entry(rmidi->proc_entry);
1512 rmidi->proc_entry = NULL;
1513 mutex_lock(&register_mutex);
1514 if (rmidi->ops && rmidi->ops->dev_unregister)
1515 rmidi->ops->dev_unregister(rmidi);
1516 mutex_unlock(&register_mutex);
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1519 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1520 if (rmidi->private_free)
1521 rmidi->private_free(rmidi);
1522 kfree(rmidi);
1523 return 0;
1524}
1525
Takashi Iwai48c9d412005-11-17 13:56:51 +01001526static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001528 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return snd_rawmidi_free(rmidi);
1530}
1531
1532#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
Takashi Iwai48c9d412005-11-17 13:56:51 +01001533static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001535 struct snd_rawmidi *rmidi = device->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 rmidi->seq_dev = NULL;
1537}
1538#endif
1539
Takashi Iwai48c9d412005-11-17 13:56:51 +01001540static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001542 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001543 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001545 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1548 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001549 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001550 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001551 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return -EBUSY;
1553 }
Clemens Ladischf87135f2005-11-20 14:06:59 +01001554 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1556 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1557 rmidi->card, rmidi->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001558 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001560 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001561 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return err;
1563 }
1564 if (rmidi->ops && rmidi->ops->dev_register &&
1565 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1566 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001567 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001568 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return err;
1570 }
1571#ifdef CONFIG_SND_OSSEMUL
1572 rmidi->ossreg = 0;
1573 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1574 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001575 rmidi->card, 0, &snd_rawmidi_f_ops,
1576 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1578 } else {
1579 rmidi->ossreg++;
1580#ifdef SNDRV_OSS_INFO_DEV_MIDI
1581 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1582#endif
1583 }
1584 }
1585 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1586 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001587 rmidi->card, 1, &snd_rawmidi_f_ops,
1588 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1590 } else {
1591 rmidi->ossreg++;
1592 }
1593 }
1594#endif /* CONFIG_SND_OSSEMUL */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001595 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 sprintf(name, "midi%d", rmidi->device);
1597 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1598 if (entry) {
1599 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 entry->c.text.read = snd_rawmidi_proc_info_read;
1601 if (snd_info_register(entry) < 0) {
1602 snd_info_free_entry(entry);
1603 entry = NULL;
1604 }
1605 }
1606 rmidi->proc_entry = entry;
1607#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1608 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1609 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1610 rmidi->seq_dev->private_data = rmidi;
1611 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1612 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1613 snd_device_register(rmidi->card, rmidi->seq_dev);
1614 }
1615 }
1616#endif
1617 return 0;
1618}
1619
Takashi Iwai48c9d412005-11-17 13:56:51 +01001620static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001622 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001624 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001625 list_del_init(&rmidi->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626#ifdef CONFIG_SND_OSSEMUL
1627 if (rmidi->ossreg) {
1628 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1629 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1630#ifdef SNDRV_OSS_INFO_DEV_MIDI
1631 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1632#endif
1633 }
1634 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1635 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1636 rmidi->ossreg = 0;
1637 }
1638#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001640 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
1644/**
1645 * snd_rawmidi_set_ops - set the rawmidi operators
1646 * @rmidi: the rawmidi instance
1647 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1648 * @ops: the operator table
1649 *
1650 * Sets the rawmidi operators for the given stream direction.
1651 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001652void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1653 struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001655 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Johannes Berg9244b2c2006-10-05 16:02:22 +02001657 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661/*
1662 * ENTRY functions
1663 */
1664
1665static int __init alsa_rawmidi_init(void)
1666{
1667
1668 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1669 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1670#ifdef CONFIG_SND_OSSEMUL
1671 { int i;
1672 /* check device map table */
1673 for (i = 0; i < SNDRV_CARDS; i++) {
1674 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1675 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1676 midi_map[i] = 0;
1677 }
1678 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1679 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1680 amidi_map[i] = 1;
1681 }
1682 }
1683 }
1684#endif /* CONFIG_SND_OSSEMUL */
1685 return 0;
1686}
1687
1688static void __exit alsa_rawmidi_exit(void)
1689{
1690 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1691 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1692}
1693
1694module_init(alsa_rawmidi_init)
1695module_exit(alsa_rawmidi_exit)
1696
1697EXPORT_SYMBOL(snd_rawmidi_output_params);
1698EXPORT_SYMBOL(snd_rawmidi_input_params);
1699EXPORT_SYMBOL(snd_rawmidi_drop_output);
1700EXPORT_SYMBOL(snd_rawmidi_drain_output);
1701EXPORT_SYMBOL(snd_rawmidi_drain_input);
1702EXPORT_SYMBOL(snd_rawmidi_receive);
1703EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1704EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1705EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1706EXPORT_SYMBOL(snd_rawmidi_transmit);
1707EXPORT_SYMBOL(snd_rawmidi_new);
1708EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709EXPORT_SYMBOL(snd_rawmidi_info_select);
1710EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1711EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1712EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1713EXPORT_SYMBOL(snd_rawmidi_kernel_write);