blob: 3071e6f5801e24ef6c12a062bde725d80df1b002 [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 Iwai9a1b64c2009-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;
245 if (s->substream_opened >= s->substream_count)
246 return -EAGAIN;
247
248 list_for_each_entry(substream, &s->substreams, list) {
249 if (substream->opened) {
250 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
251 !(mode & SNDRV_RAWMIDI_LFLG_APPEND))
252 continue;
253 }
254 if (subdevice < 0 || subdevice == substream->number) {
255 *sub_ret = substream;
256 return 0;
257 }
258 }
259 return -EAGAIN;
260}
261
262/* open and do ref-counting for the given substream */
263static int open_substream(struct snd_rawmidi *rmidi,
264 struct snd_rawmidi_substream *substream,
265 int mode)
266{
267 int err;
268
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200269 if (substream->use_count == 0) {
270 err = snd_rawmidi_runtime_create(substream);
271 if (err < 0)
272 return err;
273 err = substream->ops->open(substream);
274 if (err < 0)
275 return err;
276 substream->opened = 1;
Clemens Ladisch2d4b8422009-07-13 13:52:46 +0200277 substream->active_sensing = 0;
Clemens Ladisch8579d2d2009-10-21 09:09:38 +0200278 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
279 substream->append = 1;
280 }
281 substream->use_count++;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100282 rmidi->streams[substream->stream].substream_opened++;
283 return 0;
284}
285
286static void close_substream(struct snd_rawmidi *rmidi,
287 struct snd_rawmidi_substream *substream,
288 int cleanup);
289
290static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
291 struct snd_rawmidi_file *rfile)
292{
293 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
294 int err;
295
296 rfile->input = rfile->output = NULL;
297 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
298 err = assign_substream(rmidi, subdevice,
299 SNDRV_RAWMIDI_STREAM_INPUT,
300 mode, &sinput);
301 if (err < 0)
302 goto __error;
303 }
304 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
305 err = assign_substream(rmidi, subdevice,
306 SNDRV_RAWMIDI_STREAM_OUTPUT,
307 mode, &soutput);
308 if (err < 0)
309 goto __error;
310 }
311
312 if (sinput) {
313 err = open_substream(rmidi, sinput, mode);
314 if (err < 0)
315 goto __error;
316 }
317 if (soutput) {
318 err = open_substream(rmidi, soutput, mode);
319 if (err < 0) {
320 if (sinput)
321 close_substream(rmidi, sinput, 0);
322 goto __error;
323 }
324 }
325
326 rfile->rmidi = rmidi;
327 rfile->input = sinput;
328 rfile->output = soutput;
329 return 0;
330
331 __error:
332 if (sinput && sinput->runtime)
333 snd_rawmidi_runtime_free(sinput);
334 if (soutput && soutput->runtime)
335 snd_rawmidi_runtime_free(soutput);
336 return err;
337}
338
339/* called from sound/core/seq/seq_midi.c */
Clemens Ladischf87135f2005-11-20 14:06:59 +0100340int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai48c9d412005-11-17 13:56:51 +0100341 int mode, struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100343 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int err;
345
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100346 if (snd_BUG_ON(!rfile))
347 return -EINVAL;
348
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100349 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100350 rmidi = snd_rawmidi_search(card, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (rmidi == NULL) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100352 mutex_unlock(&register_mutex);
353 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355 if (!try_module_get(rmidi->card->module)) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100356 mutex_unlock(&register_mutex);
357 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Takashi Iwaif9d20282009-02-11 14:55:59 +0100359 mutex_unlock(&register_mutex);
360
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100361 mutex_lock(&rmidi->open_mutex);
362 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
363 mutex_unlock(&rmidi->open_mutex);
364 if (err < 0)
365 module_put(rmidi->card->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return err;
367}
368
369static int snd_rawmidi_open(struct inode *inode, struct file *file)
370{
371 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100372 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100373 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unsigned short fflags;
375 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100376 struct snd_rawmidi *rmidi;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100377 struct snd_rawmidi_file *rawmidi_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 wait_queue_t wait;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100379 struct snd_ctl_file *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100381 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
382 return -EINVAL; /* invalid combination */
383
Clemens Ladischf1902862005-10-24 17:05:03 +0200384 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100385 rmidi = snd_lookup_minor_data(iminor(inode),
386 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200388 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100389 rmidi = snd_lookup_oss_minor_data(iminor(inode),
390 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200392 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (rmidi == NULL)
396 return -ENODEV;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100397
398 if (!try_module_get(rmidi->card->module))
399 return -ENXIO;
400
401 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 card = rmidi->card;
403 err = snd_card_file_add(card, file);
404 if (err < 0)
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100405 goto __error_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200407 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
410 if (rawmidi_file == NULL) {
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100411 err = -ENOMEM;
412 goto __error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 init_waitqueue_entry(&wait, current);
415 add_wait_queue(&rmidi->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 while (1) {
417 subdevice = -1;
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200418 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200419 list_for_each_entry(kctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (kctl->pid == current->pid) {
421 subdevice = kctl->prefer_rawmidi_subdevice;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200422 if (subdevice != -1)
423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 }
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200426 read_unlock(&card->ctl_files_rwlock);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100427 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (err >= 0)
429 break;
430 if (err == -EAGAIN) {
431 if (file->f_flags & O_NONBLOCK) {
432 err = -EBUSY;
433 break;
434 }
435 } else
436 break;
437 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100438 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100440 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (signal_pending(current)) {
442 err = -ERESTARTSYS;
443 break;
444 }
445 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100446 remove_wait_queue(&rmidi->open_wait, &wait);
447 if (err < 0) {
448 kfree(rawmidi_file);
449 goto __error;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451#ifdef CONFIG_SND_OSSEMUL
452 if (rawmidi_file->input && rawmidi_file->input->runtime)
453 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
454 if (rawmidi_file->output && rawmidi_file->output->runtime)
455 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
456#endif
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100457 file->private_data = rawmidi_file;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100458 mutex_unlock(&rmidi->open_mutex);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100459 return 0;
460
461 __error:
462 snd_card_file_remove(card, file);
463 __error_card:
464 mutex_unlock(&rmidi->open_mutex);
465 module_put(rmidi->card->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return err;
467}
468
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100469static void close_substream(struct snd_rawmidi *rmidi,
470 struct snd_rawmidi_substream *substream,
471 int cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100473 rmidi->streams[substream->stream].substream_opened--;
474 if (--substream->use_count)
475 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100477 if (cleanup) {
478 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
479 snd_rawmidi_input_trigger(substream, 0);
480 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (substream->active_sensing) {
482 unsigned char buf = 0xfe;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100483 /* sending single active sensing message
484 * to shut the device up
485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 snd_rawmidi_kernel_write(substream, &buf, 1);
487 }
488 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
489 snd_rawmidi_output_trigger(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100492 substream->ops->close(substream);
493 if (substream->runtime->private_free)
494 substream->runtime->private_free(substream);
495 snd_rawmidi_runtime_free(substream);
496 substream->opened = 0;
497 substream->append = 0;
498}
499
500static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
501{
502 struct snd_rawmidi *rmidi;
503
504 rmidi = rfile->rmidi;
505 mutex_lock(&rmidi->open_mutex);
506 if (rfile->input) {
507 close_substream(rmidi, rfile->input, 1);
508 rfile->input = NULL;
509 }
510 if (rfile->output) {
511 close_substream(rmidi, rfile->output, 1);
512 rfile->output = NULL;
513 }
514 rfile->rmidi = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100515 mutex_unlock(&rmidi->open_mutex);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100516 wake_up(&rmidi->open_wait);
517}
518
519/* called from sound/core/seq/seq_midi.c */
520int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
521{
522 struct snd_rawmidi *rmidi;
523
524 if (snd_BUG_ON(!rfile))
525 return -ENXIO;
526
527 rmidi = rfile->rmidi;
528 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 module_put(rmidi->card->module);
530 return 0;
531}
532
533static int snd_rawmidi_release(struct inode *inode, struct file *file)
534{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100535 struct snd_rawmidi_file *rfile;
536 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 rfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 rmidi = rfile->rmidi;
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100540 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 kfree(rfile);
542 snd_card_file_remove(rmidi->card, file);
Takashi Iwai9a1b64c2009-02-11 17:03:49 +0100543 module_put(rmidi->card->module);
544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Adrian Bunk18612042005-11-23 13:14:50 +0100547static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
548 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100550 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (substream == NULL)
553 return -ENODEV;
554 rmidi = substream->rmidi;
555 memset(info, 0, sizeof(*info));
556 info->card = rmidi->card->number;
557 info->device = rmidi->device;
558 info->subdevice = substream->number;
559 info->stream = substream->stream;
560 info->flags = rmidi->info_flags;
561 strcpy(info->id, rmidi->id);
562 strcpy(info->name, rmidi->name);
563 strcpy(info->subname, substream->name);
564 info->subdevices_count = substream->pstr->substream_count;
565 info->subdevices_avail = (substream->pstr->substream_count -
566 substream->pstr->substream_opened);
567 return 0;
568}
569
Takashi Iwai48c9d412005-11-17 13:56:51 +0100570static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
571 struct snd_rawmidi_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100573 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 int err;
575 if ((err = snd_rawmidi_info(substream, &info)) < 0)
576 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100577 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return -EFAULT;
579 return 0;
580}
581
Takashi Iwai48c9d412005-11-17 13:56:51 +0100582int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100584 struct snd_rawmidi *rmidi;
585 struct snd_rawmidi_str *pstr;
586 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100587
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100588 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100589 rmidi = snd_rawmidi_search(card, info->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100590 mutex_unlock(&register_mutex);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100591 if (!rmidi)
592 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (info->stream < 0 || info->stream > 1)
594 return -EINVAL;
595 pstr = &rmidi->streams[info->stream];
596 if (pstr->substream_count == 0)
597 return -ENOENT;
598 if (info->subdevice >= pstr->substream_count)
599 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200600 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if ((unsigned int)substream->number == info->subdevice)
602 return snd_rawmidi_info(substream, info);
603 }
604 return -ENXIO;
605}
606
Takashi Iwai48c9d412005-11-17 13:56:51 +0100607static int snd_rawmidi_info_select_user(struct snd_card *card,
608 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100611 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (get_user(info.device, &_info->device))
613 return -EFAULT;
614 if (get_user(info.stream, &_info->stream))
615 return -EFAULT;
616 if (get_user(info.subdevice, &_info->subdevice))
617 return -EFAULT;
618 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
619 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100620 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return -EFAULT;
622 return 0;
623}
624
Takashi Iwai48c9d412005-11-17 13:56:51 +0100625int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
626 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100629 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 if (substream->append && substream->use_count > 1)
632 return -EBUSY;
633 snd_rawmidi_drain_output(substream);
634 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
635 return -EINVAL;
636 }
637 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
638 return -EINVAL;
639 }
640 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800641 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
642 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -ENOMEM;
644 kfree(runtime->buffer);
645 runtime->buffer = newbuf;
646 runtime->buffer_size = params->buffer_size;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100647 runtime->avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 runtime->avail_min = params->avail_min;
650 substream->active_sensing = !params->no_active_sensing;
651 return 0;
652}
653
Takashi Iwai48c9d412005-11-17 13:56:51 +0100654int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
655 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100658 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 snd_rawmidi_drain_input(substream);
661 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
662 return -EINVAL;
663 }
664 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
665 return -EINVAL;
666 }
667 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800668 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
669 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -ENOMEM;
671 kfree(runtime->buffer);
672 runtime->buffer = newbuf;
673 runtime->buffer_size = params->buffer_size;
674 }
675 runtime->avail_min = params->avail_min;
676 return 0;
677}
678
Takashi Iwai48c9d412005-11-17 13:56:51 +0100679static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
680 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100682 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 memset(status, 0, sizeof(*status));
685 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
686 spin_lock_irq(&runtime->lock);
687 status->avail = runtime->avail;
688 spin_unlock_irq(&runtime->lock);
689 return 0;
690}
691
Takashi Iwai48c9d412005-11-17 13:56:51 +0100692static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
693 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100695 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 memset(status, 0, sizeof(*status));
698 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
699 spin_lock_irq(&runtime->lock);
700 status->avail = runtime->avail;
701 status->xruns = runtime->xruns;
702 runtime->xruns = 0;
703 spin_unlock_irq(&runtime->lock);
704 return 0;
705}
706
707static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
708{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100709 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 void __user *argp = (void __user *)arg;
711
712 rfile = file->private_data;
713 if (((cmd >> 8) & 0xff) != 'W')
714 return -ENOTTY;
715 switch (cmd) {
716 case SNDRV_RAWMIDI_IOCTL_PVERSION:
717 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
718 case SNDRV_RAWMIDI_IOCTL_INFO:
719 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100720 int stream;
721 struct snd_rawmidi_info __user *info = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (get_user(stream, &info->stream))
723 return -EFAULT;
724 switch (stream) {
725 case SNDRV_RAWMIDI_STREAM_INPUT:
726 return snd_rawmidi_info_user(rfile->input, info);
727 case SNDRV_RAWMIDI_STREAM_OUTPUT:
728 return snd_rawmidi_info_user(rfile->output, info);
729 default:
730 return -EINVAL;
731 }
732 }
733 case SNDRV_RAWMIDI_IOCTL_PARAMS:
734 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100735 struct snd_rawmidi_params params;
736 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -EFAULT;
738 switch (params.stream) {
739 case SNDRV_RAWMIDI_STREAM_OUTPUT:
740 if (rfile->output == NULL)
741 return -EINVAL;
742 return snd_rawmidi_output_params(rfile->output, &params);
743 case SNDRV_RAWMIDI_STREAM_INPUT:
744 if (rfile->input == NULL)
745 return -EINVAL;
746 return snd_rawmidi_input_params(rfile->input, &params);
747 default:
748 return -EINVAL;
749 }
750 }
751 case SNDRV_RAWMIDI_IOCTL_STATUS:
752 {
753 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100754 struct snd_rawmidi_status status;
755 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return -EFAULT;
757 switch (status.stream) {
758 case SNDRV_RAWMIDI_STREAM_OUTPUT:
759 if (rfile->output == NULL)
760 return -EINVAL;
761 err = snd_rawmidi_output_status(rfile->output, &status);
762 break;
763 case SNDRV_RAWMIDI_STREAM_INPUT:
764 if (rfile->input == NULL)
765 return -EINVAL;
766 err = snd_rawmidi_input_status(rfile->input, &status);
767 break;
768 default:
769 return -EINVAL;
770 }
771 if (err < 0)
772 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100773 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return -EFAULT;
775 return 0;
776 }
777 case SNDRV_RAWMIDI_IOCTL_DROP:
778 {
779 int val;
780 if (get_user(val, (int __user *) argp))
781 return -EFAULT;
782 switch (val) {
783 case SNDRV_RAWMIDI_STREAM_OUTPUT:
784 if (rfile->output == NULL)
785 return -EINVAL;
786 return snd_rawmidi_drop_output(rfile->output);
787 default:
788 return -EINVAL;
789 }
790 }
791 case SNDRV_RAWMIDI_IOCTL_DRAIN:
792 {
793 int val;
794 if (get_user(val, (int __user *) argp))
795 return -EFAULT;
796 switch (val) {
797 case SNDRV_RAWMIDI_STREAM_OUTPUT:
798 if (rfile->output == NULL)
799 return -EINVAL;
800 return snd_rawmidi_drain_output(rfile->output);
801 case SNDRV_RAWMIDI_STREAM_INPUT:
802 if (rfile->input == NULL)
803 return -EINVAL;
804 return snd_rawmidi_drain_input(rfile->input);
805 default:
806 return -EINVAL;
807 }
808 }
809#ifdef CONFIG_SND_DEBUG
810 default:
811 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
812#endif
813 }
814 return -ENOTTY;
815}
816
Takashi Iwai48c9d412005-11-17 13:56:51 +0100817static int snd_rawmidi_control_ioctl(struct snd_card *card,
818 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 unsigned int cmd,
820 unsigned long arg)
821{
822 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 switch (cmd) {
825 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
826 {
827 int device;
828
829 if (get_user(device, (int __user *)argp))
830 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100831 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 device = device < 0 ? 0 : device + 1;
833 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100834 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 break;
836 device++;
837 }
838 if (device == SNDRV_RAWMIDI_DEVICES)
839 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100840 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (put_user(device, (int __user *)argp))
842 return -EFAULT;
843 return 0;
844 }
845 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
846 {
847 int val;
848
849 if (get_user(val, (int __user *)argp))
850 return -EFAULT;
851 control->prefer_rawmidi_subdevice = val;
852 return 0;
853 }
854 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
855 return snd_rawmidi_info_select_user(card, argp);
856 }
857 return -ENOIOCTLCMD;
858}
859
860/**
861 * snd_rawmidi_receive - receive the input data from the device
862 * @substream: the rawmidi substream
863 * @buffer: the buffer pointer
864 * @count: the data size to read
865 *
866 * Reads the data from the internal buffer.
867 *
868 * Returns the size of read data, or a negative error code on failure.
869 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100870int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
871 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 unsigned long flags;
874 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100875 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Takashi Iwai219df322008-11-03 08:17:05 +0100877 if (!substream->opened)
878 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (runtime->buffer == NULL) {
880 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
881 return -EINVAL;
882 }
883 spin_lock_irqsave(&runtime->lock, flags);
884 if (count == 1) { /* special case, faster code */
885 substream->bytes++;
886 if (runtime->avail < runtime->buffer_size) {
887 runtime->buffer[runtime->hw_ptr++] = buffer[0];
888 runtime->hw_ptr %= runtime->buffer_size;
889 runtime->avail++;
890 result++;
891 } else {
892 runtime->xruns++;
893 }
894 } else {
895 substream->bytes += count;
896 count1 = runtime->buffer_size - runtime->hw_ptr;
897 if (count1 > count)
898 count1 = count;
899 if (count1 > (int)(runtime->buffer_size - runtime->avail))
900 count1 = runtime->buffer_size - runtime->avail;
901 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
902 runtime->hw_ptr += count1;
903 runtime->hw_ptr %= runtime->buffer_size;
904 runtime->avail += count1;
905 count -= count1;
906 result += count1;
907 if (count > 0) {
908 buffer += count1;
909 count1 = count;
910 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
911 count1 = runtime->buffer_size - runtime->avail;
912 runtime->xruns += count - count1;
913 }
914 if (count1 > 0) {
915 memcpy(runtime->buffer, buffer, count1);
916 runtime->hw_ptr = count1;
917 runtime->avail += count1;
918 result += count1;
919 }
920 }
921 }
922 if (result > 0) {
923 if (runtime->event)
Takashi Iwai1f041282008-12-18 12:17:55 +0100924 tasklet_schedule(&runtime->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 else if (snd_rawmidi_ready(substream))
926 wake_up(&runtime->sleep);
927 }
928 spin_unlock_irqrestore(&runtime->lock, flags);
929 return result;
930}
931
Takashi Iwai48c9d412005-11-17 13:56:51 +0100932static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100933 unsigned char __user *userbuf,
934 unsigned char *kernelbuf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 unsigned long flags;
937 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100938 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 while (count > 0 && runtime->avail) {
941 count1 = runtime->buffer_size - runtime->appl_ptr;
942 if (count1 > count)
943 count1 = count;
944 spin_lock_irqsave(&runtime->lock, flags);
945 if (count1 > (int)runtime->avail)
946 count1 = runtime->avail;
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100947 if (kernelbuf)
948 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
949 if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 spin_unlock_irqrestore(&runtime->lock, flags);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100951 if (copy_to_user(userbuf + result,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 runtime->buffer + runtime->appl_ptr, count1)) {
953 return result > 0 ? result : -EFAULT;
954 }
955 spin_lock_irqsave(&runtime->lock, flags);
956 }
957 runtime->appl_ptr += count1;
958 runtime->appl_ptr %= runtime->buffer_size;
959 runtime->avail -= count1;
960 spin_unlock_irqrestore(&runtime->lock, flags);
961 result += count1;
962 count -= count1;
963 }
964 return result;
965}
966
Takashi Iwai48c9d412005-11-17 13:56:51 +0100967long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
968 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 snd_rawmidi_input_trigger(substream, 1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100971 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Takashi Iwai48c9d412005-11-17 13:56:51 +0100974static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
975 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 long result;
978 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100979 struct snd_rawmidi_file *rfile;
980 struct snd_rawmidi_substream *substream;
981 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 rfile = file->private_data;
984 substream = rfile->input;
985 if (substream == NULL)
986 return -EIO;
987 runtime = substream->runtime;
988 snd_rawmidi_input_trigger(substream, 1);
989 result = 0;
990 while (count > 0) {
991 spin_lock_irq(&runtime->lock);
992 while (!snd_rawmidi_ready(substream)) {
993 wait_queue_t wait;
994 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
995 spin_unlock_irq(&runtime->lock);
996 return result > 0 ? result : -EAGAIN;
997 }
998 init_waitqueue_entry(&wait, current);
999 add_wait_queue(&runtime->sleep, &wait);
1000 set_current_state(TASK_INTERRUPTIBLE);
1001 spin_unlock_irq(&runtime->lock);
1002 schedule();
1003 remove_wait_queue(&runtime->sleep, &wait);
1004 if (signal_pending(current))
1005 return result > 0 ? result : -ERESTARTSYS;
1006 if (!runtime->avail)
1007 return result > 0 ? result : -EIO;
1008 spin_lock_irq(&runtime->lock);
1009 }
1010 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001011 count1 = snd_rawmidi_kernel_read1(substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001012 (unsigned char __user *)buf,
1013 NULL/*kernelbuf*/,
1014 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (count1 < 0)
1016 return result > 0 ? result : count1;
1017 result += count1;
1018 buf += count1;
1019 count -= count1;
1020 }
1021 return result;
1022}
1023
1024/**
1025 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1026 * @substream: the rawmidi substream
1027 *
1028 * Returns 1 if the internal output buffer is empty, 0 if not.
1029 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001030int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001032 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 int result;
1034 unsigned long flags;
1035
1036 if (runtime->buffer == NULL) {
1037 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1038 return 1;
1039 }
1040 spin_lock_irqsave(&runtime->lock, flags);
1041 result = runtime->avail >= runtime->buffer_size;
1042 spin_unlock_irqrestore(&runtime->lock, flags);
1043 return result;
1044}
1045
1046/**
1047 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1048 * @substream: the rawmidi substream
1049 * @buffer: the buffer pointer
1050 * @count: data size to transfer
1051 *
1052 * Copies data from the internal output buffer to the given buffer.
1053 *
1054 * Call this in the interrupt handler when the midi output is ready,
1055 * and call snd_rawmidi_transmit_ack() after the transmission is
1056 * finished.
1057 *
1058 * Returns the size of copied data, or a negative error code on failure.
1059 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001060int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1061 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 unsigned long flags;
1064 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001065 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 if (runtime->buffer == NULL) {
1068 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1069 return -EINVAL;
1070 }
1071 result = 0;
1072 spin_lock_irqsave(&runtime->lock, flags);
1073 if (runtime->avail >= runtime->buffer_size) {
1074 /* warning: lowlevel layer MUST trigger down the hardware */
1075 goto __skip;
1076 }
1077 if (count == 1) { /* special case, faster code */
1078 *buffer = runtime->buffer[runtime->hw_ptr];
1079 result++;
1080 } else {
1081 count1 = runtime->buffer_size - runtime->hw_ptr;
1082 if (count1 > count)
1083 count1 = count;
1084 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1085 count1 = runtime->buffer_size - runtime->avail;
1086 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1087 count -= count1;
1088 result += count1;
1089 if (count > 0) {
1090 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1091 count = runtime->buffer_size - runtime->avail - count1;
1092 memcpy(buffer + count1, runtime->buffer, count);
1093 result += count;
1094 }
1095 }
1096 __skip:
1097 spin_unlock_irqrestore(&runtime->lock, flags);
1098 return result;
1099}
1100
1101/**
1102 * snd_rawmidi_transmit_ack - acknowledge the transmission
1103 * @substream: the rawmidi substream
1104 * @count: the tranferred count
1105 *
1106 * Advances the hardware pointer for the internal output buffer with
1107 * the given size and updates the condition.
1108 * Call after the transmission is finished.
1109 *
1110 * Returns the advanced size if successful, or a negative error code on failure.
1111 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001112int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001115 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (runtime->buffer == NULL) {
1118 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1119 return -EINVAL;
1120 }
1121 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001122 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 runtime->hw_ptr += count;
1124 runtime->hw_ptr %= runtime->buffer_size;
1125 runtime->avail += count;
1126 substream->bytes += count;
1127 if (count > 0) {
1128 if (runtime->drain || snd_rawmidi_ready(substream))
1129 wake_up(&runtime->sleep);
1130 }
1131 spin_unlock_irqrestore(&runtime->lock, flags);
1132 return count;
1133}
1134
1135/**
1136 * snd_rawmidi_transmit - copy from the buffer to the device
1137 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001138 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * @count: the data size to transfer
1140 *
1141 * Copies data from the buffer to the device and advances the pointer.
1142 *
1143 * Returns the copied size if successful, or a negative error code on failure.
1144 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001145int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1146 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Takashi Iwai219df322008-11-03 08:17:05 +01001148 if (!substream->opened)
1149 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1151 if (count < 0)
1152 return count;
1153 return snd_rawmidi_transmit_ack(substream, count);
1154}
1155
Takashi Iwai48c9d412005-11-17 13:56:51 +01001156static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001157 const unsigned char __user *userbuf,
1158 const unsigned char *kernelbuf,
1159 long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 unsigned long flags;
1162 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001163 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001165 if (snd_BUG_ON(!kernelbuf && !userbuf))
1166 return -EINVAL;
1167 if (snd_BUG_ON(!runtime->buffer))
1168 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 result = 0;
1171 spin_lock_irqsave(&runtime->lock, flags);
1172 if (substream->append) {
1173 if ((long)runtime->avail < count) {
1174 spin_unlock_irqrestore(&runtime->lock, flags);
1175 return -EAGAIN;
1176 }
1177 }
1178 while (count > 0 && runtime->avail > 0) {
1179 count1 = runtime->buffer_size - runtime->appl_ptr;
1180 if (count1 > count)
1181 count1 = count;
1182 if (count1 > (long)runtime->avail)
1183 count1 = runtime->avail;
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001184 if (kernelbuf)
1185 memcpy(runtime->buffer + runtime->appl_ptr,
1186 kernelbuf + result, count1);
1187 else if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 spin_unlock_irqrestore(&runtime->lock, flags);
1189 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001190 userbuf + result, count1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 spin_lock_irqsave(&runtime->lock, flags);
1192 result = result > 0 ? result : -EFAULT;
1193 goto __end;
1194 }
1195 spin_lock_irqsave(&runtime->lock, flags);
1196 }
1197 runtime->appl_ptr += count1;
1198 runtime->appl_ptr %= runtime->buffer_size;
1199 runtime->avail -= count1;
1200 result += count1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 count -= count1;
1202 }
1203 __end:
1204 count1 = runtime->avail < runtime->buffer_size;
1205 spin_unlock_irqrestore(&runtime->lock, flags);
1206 if (count1)
1207 snd_rawmidi_output_trigger(substream, 1);
1208 return result;
1209}
1210
Takashi Iwai48c9d412005-11-17 13:56:51 +01001211long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1212 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001214 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Takashi Iwai48c9d412005-11-17 13:56:51 +01001217static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1218 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 long result, timeout;
1221 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001222 struct snd_rawmidi_file *rfile;
1223 struct snd_rawmidi_runtime *runtime;
1224 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 rfile = file->private_data;
1227 substream = rfile->output;
1228 runtime = substream->runtime;
1229 /* we cannot put an atomic message to our buffer */
1230 if (substream->append && count > runtime->buffer_size)
1231 return -EIO;
1232 result = 0;
1233 while (count > 0) {
1234 spin_lock_irq(&runtime->lock);
1235 while (!snd_rawmidi_ready_append(substream, count)) {
1236 wait_queue_t wait;
1237 if (file->f_flags & O_NONBLOCK) {
1238 spin_unlock_irq(&runtime->lock);
1239 return result > 0 ? result : -EAGAIN;
1240 }
1241 init_waitqueue_entry(&wait, current);
1242 add_wait_queue(&runtime->sleep, &wait);
1243 set_current_state(TASK_INTERRUPTIBLE);
1244 spin_unlock_irq(&runtime->lock);
1245 timeout = schedule_timeout(30 * HZ);
1246 remove_wait_queue(&runtime->sleep, &wait);
1247 if (signal_pending(current))
1248 return result > 0 ? result : -ERESTARTSYS;
1249 if (!runtime->avail && !timeout)
1250 return result > 0 ? result : -EIO;
1251 spin_lock_irq(&runtime->lock);
1252 }
1253 spin_unlock_irq(&runtime->lock);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001254 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (count1 < 0)
1256 return result > 0 ? result : count1;
1257 result += count1;
1258 buf += count1;
1259 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1260 break;
1261 count -= count1;
1262 }
1263 if (file->f_flags & O_SYNC) {
1264 spin_lock_irq(&runtime->lock);
1265 while (runtime->avail != runtime->buffer_size) {
1266 wait_queue_t wait;
1267 unsigned int last_avail = runtime->avail;
1268 init_waitqueue_entry(&wait, current);
1269 add_wait_queue(&runtime->sleep, &wait);
1270 set_current_state(TASK_INTERRUPTIBLE);
1271 spin_unlock_irq(&runtime->lock);
1272 timeout = schedule_timeout(30 * HZ);
1273 remove_wait_queue(&runtime->sleep, &wait);
1274 if (signal_pending(current))
1275 return result > 0 ? result : -ERESTARTSYS;
1276 if (runtime->avail == last_avail && !timeout)
1277 return result > 0 ? result : -EIO;
1278 spin_lock_irq(&runtime->lock);
1279 }
1280 spin_unlock_irq(&runtime->lock);
1281 }
1282 return result;
1283}
1284
1285static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1286{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001287 struct snd_rawmidi_file *rfile;
1288 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 unsigned int mask;
1290
1291 rfile = file->private_data;
1292 if (rfile->input != NULL) {
1293 runtime = rfile->input->runtime;
1294 snd_rawmidi_input_trigger(rfile->input, 1);
1295 poll_wait(file, &runtime->sleep, wait);
1296 }
1297 if (rfile->output != NULL) {
1298 runtime = rfile->output->runtime;
1299 poll_wait(file, &runtime->sleep, wait);
1300 }
1301 mask = 0;
1302 if (rfile->input != NULL) {
1303 if (snd_rawmidi_ready(rfile->input))
1304 mask |= POLLIN | POLLRDNORM;
1305 }
1306 if (rfile->output != NULL) {
1307 if (snd_rawmidi_ready(rfile->output))
1308 mask |= POLLOUT | POLLWRNORM;
1309 }
1310 return mask;
1311}
1312
1313/*
1314 */
1315#ifdef CONFIG_COMPAT
1316#include "rawmidi_compat.c"
1317#else
1318#define snd_rawmidi_ioctl_compat NULL
1319#endif
1320
1321/*
1322
1323 */
1324
Takashi Iwai48c9d412005-11-17 13:56:51 +01001325static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1326 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001328 struct snd_rawmidi *rmidi;
1329 struct snd_rawmidi_substream *substream;
1330 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 rmidi = entry->private_data;
1333 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001334 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001336 list_for_each_entry(substream,
1337 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1338 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 snd_iprintf(buffer,
1340 "Output %d\n"
1341 " Tx bytes : %lu\n",
1342 substream->number,
1343 (unsigned long) substream->bytes);
1344 if (substream->opened) {
1345 runtime = substream->runtime;
1346 snd_iprintf(buffer,
1347 " Mode : %s\n"
1348 " Buffer size : %lu\n"
1349 " Avail : %lu\n",
1350 runtime->oss ? "OSS compatible" : "native",
1351 (unsigned long) runtime->buffer_size,
1352 (unsigned long) runtime->avail);
1353 }
1354 }
1355 }
1356 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001357 list_for_each_entry(substream,
1358 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1359 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 snd_iprintf(buffer,
1361 "Input %d\n"
1362 " Rx bytes : %lu\n",
1363 substream->number,
1364 (unsigned long) substream->bytes);
1365 if (substream->opened) {
1366 runtime = substream->runtime;
1367 snd_iprintf(buffer,
1368 " Buffer size : %lu\n"
1369 " Avail : %lu\n"
1370 " Overruns : %lu\n",
1371 (unsigned long) runtime->buffer_size,
1372 (unsigned long) runtime->avail,
1373 (unsigned long) runtime->xruns);
1374 }
1375 }
1376 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001377 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
1380/*
1381 * Register functions
1382 */
1383
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001384static const struct file_operations snd_rawmidi_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 .owner = THIS_MODULE,
1387 .read = snd_rawmidi_read,
1388 .write = snd_rawmidi_write,
1389 .open = snd_rawmidi_open,
1390 .release = snd_rawmidi_release,
1391 .poll = snd_rawmidi_poll,
1392 .unlocked_ioctl = snd_rawmidi_ioctl,
1393 .compat_ioctl = snd_rawmidi_ioctl_compat,
1394};
1395
Takashi Iwai48c9d412005-11-17 13:56:51 +01001396static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1397 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int direction,
1399 int count)
1400{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001401 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 int idx;
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001405 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001406 if (substream == NULL) {
1407 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 substream->stream = direction;
1411 substream->number = idx;
1412 substream->rmidi = rmidi;
1413 substream->pstr = stream;
1414 list_add_tail(&substream->list, &stream->substreams);
1415 stream->substream_count++;
1416 }
1417 return 0;
1418}
1419
1420/**
1421 * snd_rawmidi_new - create a rawmidi instance
1422 * @card: the card instance
1423 * @id: the id string
1424 * @device: the device index
1425 * @output_count: the number of output streams
1426 * @input_count: the number of input streams
1427 * @rrawmidi: the pointer to store the new rawmidi instance
1428 *
1429 * Creates a new rawmidi instance.
1430 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1431 *
1432 * Returns zero if successful, or a negative error code on failure.
1433 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001434int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int output_count, int input_count,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001436 struct snd_rawmidi ** rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001438 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001440 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 .dev_free = snd_rawmidi_dev_free,
1442 .dev_register = snd_rawmidi_dev_register,
1443 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 };
1445
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001446 if (snd_BUG_ON(!card))
1447 return -ENXIO;
1448 if (rrawmidi)
1449 *rrawmidi = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001450 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001451 if (rmidi == NULL) {
1452 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 rmidi->card = card;
1456 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001457 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001459 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1460 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (id != NULL)
1463 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001464 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1465 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1466 SNDRV_RAWMIDI_STREAM_INPUT,
1467 input_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 snd_rawmidi_free(rmidi);
1469 return err;
1470 }
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001471 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1472 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1473 SNDRV_RAWMIDI_STREAM_OUTPUT,
1474 output_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 snd_rawmidi_free(rmidi);
1476 return err;
1477 }
1478 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1479 snd_rawmidi_free(rmidi);
1480 return err;
1481 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001482 if (rrawmidi)
1483 *rrawmidi = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return 0;
1485}
1486
Takashi Iwai48c9d412005-11-17 13:56:51 +01001487static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001489 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001492 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 list_del(&substream->list);
1494 kfree(substream);
1495 }
1496}
1497
Takashi Iwai48c9d412005-11-17 13:56:51 +01001498static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001500 if (!rmidi)
1501 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +02001502
1503 snd_info_free_entry(rmidi->proc_entry);
1504 rmidi->proc_entry = NULL;
1505 mutex_lock(&register_mutex);
1506 if (rmidi->ops && rmidi->ops->dev_unregister)
1507 rmidi->ops->dev_unregister(rmidi);
1508 mutex_unlock(&register_mutex);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1511 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1512 if (rmidi->private_free)
1513 rmidi->private_free(rmidi);
1514 kfree(rmidi);
1515 return 0;
1516}
1517
Takashi Iwai48c9d412005-11-17 13:56:51 +01001518static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001520 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return snd_rawmidi_free(rmidi);
1522}
1523
1524#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
Takashi Iwai48c9d412005-11-17 13:56:51 +01001525static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001527 struct snd_rawmidi *rmidi = device->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 rmidi->seq_dev = NULL;
1529}
1530#endif
1531
Takashi Iwai48c9d412005-11-17 13:56:51 +01001532static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001534 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001535 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001537 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1540 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001541 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001542 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001543 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return -EBUSY;
1545 }
Clemens Ladischf87135f2005-11-20 14:06:59 +01001546 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1548 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1549 rmidi->card, rmidi->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001550 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001552 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001553 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return err;
1555 }
1556 if (rmidi->ops && rmidi->ops->dev_register &&
1557 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1558 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001559 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001560 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return err;
1562 }
1563#ifdef CONFIG_SND_OSSEMUL
1564 rmidi->ossreg = 0;
1565 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1566 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001567 rmidi->card, 0, &snd_rawmidi_f_ops,
1568 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1570 } else {
1571 rmidi->ossreg++;
1572#ifdef SNDRV_OSS_INFO_DEV_MIDI
1573 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1574#endif
1575 }
1576 }
1577 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1578 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001579 rmidi->card, 1, &snd_rawmidi_f_ops,
1580 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1582 } else {
1583 rmidi->ossreg++;
1584 }
1585 }
1586#endif /* CONFIG_SND_OSSEMUL */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001587 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 sprintf(name, "midi%d", rmidi->device);
1589 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1590 if (entry) {
1591 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 entry->c.text.read = snd_rawmidi_proc_info_read;
1593 if (snd_info_register(entry) < 0) {
1594 snd_info_free_entry(entry);
1595 entry = NULL;
1596 }
1597 }
1598 rmidi->proc_entry = entry;
1599#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1600 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1601 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1602 rmidi->seq_dev->private_data = rmidi;
1603 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1604 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1605 snd_device_register(rmidi->card, rmidi->seq_dev);
1606 }
1607 }
1608#endif
1609 return 0;
1610}
1611
Takashi Iwai48c9d412005-11-17 13:56:51 +01001612static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001614 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001616 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001617 list_del_init(&rmidi->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618#ifdef CONFIG_SND_OSSEMUL
1619 if (rmidi->ossreg) {
1620 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1621 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1622#ifdef SNDRV_OSS_INFO_DEV_MIDI
1623 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1624#endif
1625 }
1626 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1627 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1628 rmidi->ossreg = 0;
1629 }
1630#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001632 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636/**
1637 * snd_rawmidi_set_ops - set the rawmidi operators
1638 * @rmidi: the rawmidi instance
1639 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1640 * @ops: the operator table
1641 *
1642 * Sets the rawmidi operators for the given stream direction.
1643 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001644void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1645 struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001647 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Johannes Berg9244b2c2006-10-05 16:02:22 +02001649 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
1653/*
1654 * ENTRY functions
1655 */
1656
1657static int __init alsa_rawmidi_init(void)
1658{
1659
1660 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1661 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1662#ifdef CONFIG_SND_OSSEMUL
1663 { int i;
1664 /* check device map table */
1665 for (i = 0; i < SNDRV_CARDS; i++) {
1666 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1667 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1668 midi_map[i] = 0;
1669 }
1670 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1671 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1672 amidi_map[i] = 1;
1673 }
1674 }
1675 }
1676#endif /* CONFIG_SND_OSSEMUL */
1677 return 0;
1678}
1679
1680static void __exit alsa_rawmidi_exit(void)
1681{
1682 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1683 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1684}
1685
1686module_init(alsa_rawmidi_init)
1687module_exit(alsa_rawmidi_exit)
1688
1689EXPORT_SYMBOL(snd_rawmidi_output_params);
1690EXPORT_SYMBOL(snd_rawmidi_input_params);
1691EXPORT_SYMBOL(snd_rawmidi_drop_output);
1692EXPORT_SYMBOL(snd_rawmidi_drain_output);
1693EXPORT_SYMBOL(snd_rawmidi_drain_input);
1694EXPORT_SYMBOL(snd_rawmidi_receive);
1695EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1696EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1697EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1698EXPORT_SYMBOL(snd_rawmidi_transmit);
1699EXPORT_SYMBOL(snd_rawmidi_new);
1700EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701EXPORT_SYMBOL(snd_rawmidi_info_select);
1702EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1703EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1704EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1705EXPORT_SYMBOL(snd_rawmidi_kernel_write);