blob: 473247c8e6d3e22c2e3466e80443e6e6e3054680 [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;
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
269 err = snd_rawmidi_runtime_create(substream);
270 if (err < 0)
271 return err;
272 err = substream->ops->open(substream);
273 if (err < 0)
274 return err;
275 substream->opened = 1;
276 if (substream->use_count++ == 0)
277 substream->active_sensing = 1;
278 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
279 substream->append = 1;
280 rmidi->streams[substream->stream].substream_opened++;
281 return 0;
282}
283
284static void close_substream(struct snd_rawmidi *rmidi,
285 struct snd_rawmidi_substream *substream,
286 int cleanup);
287
288static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
289 struct snd_rawmidi_file *rfile)
290{
291 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
292 int err;
293
294 rfile->input = rfile->output = NULL;
295 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
296 err = assign_substream(rmidi, subdevice,
297 SNDRV_RAWMIDI_STREAM_INPUT,
298 mode, &sinput);
299 if (err < 0)
300 goto __error;
301 }
302 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
303 err = assign_substream(rmidi, subdevice,
304 SNDRV_RAWMIDI_STREAM_OUTPUT,
305 mode, &soutput);
306 if (err < 0)
307 goto __error;
308 }
309
310 if (sinput) {
311 err = open_substream(rmidi, sinput, mode);
312 if (err < 0)
313 goto __error;
314 }
315 if (soutput) {
316 err = open_substream(rmidi, soutput, mode);
317 if (err < 0) {
318 if (sinput)
319 close_substream(rmidi, sinput, 0);
320 goto __error;
321 }
322 }
323
324 rfile->rmidi = rmidi;
325 rfile->input = sinput;
326 rfile->output = soutput;
327 return 0;
328
329 __error:
330 if (sinput && sinput->runtime)
331 snd_rawmidi_runtime_free(sinput);
332 if (soutput && soutput->runtime)
333 snd_rawmidi_runtime_free(soutput);
334 return err;
335}
336
337/* called from sound/core/seq/seq_midi.c */
Clemens Ladischf87135f2005-11-20 14:06:59 +0100338int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai48c9d412005-11-17 13:56:51 +0100339 int mode, struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100341 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int err;
343
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100344 if (snd_BUG_ON(!rfile))
345 return -EINVAL;
346
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100347 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100348 rmidi = snd_rawmidi_search(card, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (rmidi == NULL) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100350 mutex_unlock(&register_mutex);
351 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 if (!try_module_get(rmidi->card->module)) {
Takashi Iwaif9d20282009-02-11 14:55:59 +0100354 mutex_unlock(&register_mutex);
355 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Takashi Iwaif9d20282009-02-11 14:55:59 +0100357 mutex_unlock(&register_mutex);
358
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100359 mutex_lock(&rmidi->open_mutex);
360 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
361 mutex_unlock(&rmidi->open_mutex);
362 if (err < 0)
363 module_put(rmidi->card->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return err;
365}
366
367static int snd_rawmidi_open(struct inode *inode, struct file *file)
368{
369 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100370 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100371 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 unsigned short fflags;
373 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100374 struct snd_rawmidi *rmidi;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100375 struct snd_rawmidi_file *rawmidi_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 wait_queue_t wait;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100377 struct snd_ctl_file *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100379 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
380 return -EINVAL; /* invalid combination */
381
Clemens Ladischf1902862005-10-24 17:05:03 +0200382 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100383 rmidi = snd_lookup_minor_data(iminor(inode),
384 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200386 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100387 rmidi = snd_lookup_oss_minor_data(iminor(inode),
388 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200390 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (rmidi == NULL)
394 return -ENODEV;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100395
396 if (!try_module_get(rmidi->card->module))
397 return -ENXIO;
398
399 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 card = rmidi->card;
401 err = snd_card_file_add(card, file);
402 if (err < 0)
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100403 goto __error_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200405 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
408 if (rawmidi_file == NULL) {
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100409 err = -ENOMEM;
410 goto __error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 init_waitqueue_entry(&wait, current);
413 add_wait_queue(&rmidi->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 while (1) {
415 subdevice = -1;
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200416 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200417 list_for_each_entry(kctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (kctl->pid == current->pid) {
419 subdevice = kctl->prefer_rawmidi_subdevice;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200420 if (subdevice != -1)
421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423 }
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200424 read_unlock(&card->ctl_files_rwlock);
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100425 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (err >= 0)
427 break;
428 if (err == -EAGAIN) {
429 if (file->f_flags & O_NONBLOCK) {
430 err = -EBUSY;
431 break;
432 }
433 } else
434 break;
435 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100436 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100438 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (signal_pending(current)) {
440 err = -ERESTARTSYS;
441 break;
442 }
443 }
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100444 remove_wait_queue(&rmidi->open_wait, &wait);
445 if (err < 0) {
446 kfree(rawmidi_file);
447 goto __error;
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449#ifdef CONFIG_SND_OSSEMUL
450 if (rawmidi_file->input && rawmidi_file->input->runtime)
451 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
452 if (rawmidi_file->output && rawmidi_file->output->runtime)
453 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
454#endif
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100455 file->private_data = rawmidi_file;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100456 mutex_unlock(&rmidi->open_mutex);
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100457 return 0;
458
459 __error:
460 snd_card_file_remove(card, file);
461 __error_card:
462 mutex_unlock(&rmidi->open_mutex);
463 module_put(rmidi->card->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return err;
465}
466
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100467static void close_substream(struct snd_rawmidi *rmidi,
468 struct snd_rawmidi_substream *substream,
469 int cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100471 rmidi->streams[substream->stream].substream_opened--;
472 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;
496}
497
498static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
499{
500 struct snd_rawmidi *rmidi;
501
502 rmidi = rfile->rmidi;
503 mutex_lock(&rmidi->open_mutex);
504 if (rfile->input) {
505 close_substream(rmidi, rfile->input, 1);
506 rfile->input = NULL;
507 }
508 if (rfile->output) {
509 close_substream(rmidi, rfile->output, 1);
510 rfile->output = NULL;
511 }
512 rfile->rmidi = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100513 mutex_unlock(&rmidi->open_mutex);
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100514 wake_up(&rmidi->open_wait);
515}
516
517/* called from sound/core/seq/seq_midi.c */
518int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
519{
520 struct snd_rawmidi *rmidi;
521
522 if (snd_BUG_ON(!rfile))
523 return -ENXIO;
524
525 rmidi = rfile->rmidi;
526 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 module_put(rmidi->card->module);
528 return 0;
529}
530
531static int snd_rawmidi_release(struct inode *inode, struct file *file)
532{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100533 struct snd_rawmidi_file *rfile;
534 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 rfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 rmidi = rfile->rmidi;
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100538 rawmidi_release_priv(rfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 kfree(rfile);
540 snd_card_file_remove(rmidi->card, file);
Takashi Iwai9a1b64ca2009-02-11 17:03:49 +0100541 module_put(rmidi->card->module);
542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Adrian Bunk18612042005-11-23 13:14:50 +0100545static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
546 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100548 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (substream == NULL)
551 return -ENODEV;
552 rmidi = substream->rmidi;
553 memset(info, 0, sizeof(*info));
554 info->card = rmidi->card->number;
555 info->device = rmidi->device;
556 info->subdevice = substream->number;
557 info->stream = substream->stream;
558 info->flags = rmidi->info_flags;
559 strcpy(info->id, rmidi->id);
560 strcpy(info->name, rmidi->name);
561 strcpy(info->subname, substream->name);
562 info->subdevices_count = substream->pstr->substream_count;
563 info->subdevices_avail = (substream->pstr->substream_count -
564 substream->pstr->substream_opened);
565 return 0;
566}
567
Takashi Iwai48c9d412005-11-17 13:56:51 +0100568static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
569 struct snd_rawmidi_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100571 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int err;
573 if ((err = snd_rawmidi_info(substream, &info)) < 0)
574 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100575 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return -EFAULT;
577 return 0;
578}
579
Takashi Iwai48c9d412005-11-17 13:56:51 +0100580int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100582 struct snd_rawmidi *rmidi;
583 struct snd_rawmidi_str *pstr;
584 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100585
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100586 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100587 rmidi = snd_rawmidi_search(card, info->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100588 mutex_unlock(&register_mutex);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100589 if (!rmidi)
590 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (info->stream < 0 || info->stream > 1)
592 return -EINVAL;
593 pstr = &rmidi->streams[info->stream];
594 if (pstr->substream_count == 0)
595 return -ENOENT;
596 if (info->subdevice >= pstr->substream_count)
597 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200598 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if ((unsigned int)substream->number == info->subdevice)
600 return snd_rawmidi_info(substream, info);
601 }
602 return -ENXIO;
603}
604
Takashi Iwai48c9d412005-11-17 13:56:51 +0100605static int snd_rawmidi_info_select_user(struct snd_card *card,
606 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100609 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (get_user(info.device, &_info->device))
611 return -EFAULT;
612 if (get_user(info.stream, &_info->stream))
613 return -EFAULT;
614 if (get_user(info.subdevice, &_info->subdevice))
615 return -EFAULT;
616 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
617 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100618 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return -EFAULT;
620 return 0;
621}
622
Takashi Iwai48c9d412005-11-17 13:56:51 +0100623int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
624 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100627 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (substream->append && substream->use_count > 1)
630 return -EBUSY;
631 snd_rawmidi_drain_output(substream);
632 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
633 return -EINVAL;
634 }
635 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
636 return -EINVAL;
637 }
638 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800639 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
640 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return -ENOMEM;
642 kfree(runtime->buffer);
643 runtime->buffer = newbuf;
644 runtime->buffer_size = params->buffer_size;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100645 runtime->avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 runtime->avail_min = params->avail_min;
648 substream->active_sensing = !params->no_active_sensing;
649 return 0;
650}
651
Takashi Iwai48c9d412005-11-17 13:56:51 +0100652int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
653 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100656 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 snd_rawmidi_drain_input(substream);
659 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
660 return -EINVAL;
661 }
662 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
663 return -EINVAL;
664 }
665 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800666 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
667 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -ENOMEM;
669 kfree(runtime->buffer);
670 runtime->buffer = newbuf;
671 runtime->buffer_size = params->buffer_size;
672 }
673 runtime->avail_min = params->avail_min;
674 return 0;
675}
676
Takashi Iwai48c9d412005-11-17 13:56:51 +0100677static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
678 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100680 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 memset(status, 0, sizeof(*status));
683 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
684 spin_lock_irq(&runtime->lock);
685 status->avail = runtime->avail;
686 spin_unlock_irq(&runtime->lock);
687 return 0;
688}
689
Takashi Iwai48c9d412005-11-17 13:56:51 +0100690static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
691 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100693 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 memset(status, 0, sizeof(*status));
696 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
697 spin_lock_irq(&runtime->lock);
698 status->avail = runtime->avail;
699 status->xruns = runtime->xruns;
700 runtime->xruns = 0;
701 spin_unlock_irq(&runtime->lock);
702 return 0;
703}
704
705static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
706{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100707 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 void __user *argp = (void __user *)arg;
709
710 rfile = file->private_data;
711 if (((cmd >> 8) & 0xff) != 'W')
712 return -ENOTTY;
713 switch (cmd) {
714 case SNDRV_RAWMIDI_IOCTL_PVERSION:
715 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
716 case SNDRV_RAWMIDI_IOCTL_INFO:
717 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100718 int stream;
719 struct snd_rawmidi_info __user *info = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (get_user(stream, &info->stream))
721 return -EFAULT;
722 switch (stream) {
723 case SNDRV_RAWMIDI_STREAM_INPUT:
724 return snd_rawmidi_info_user(rfile->input, info);
725 case SNDRV_RAWMIDI_STREAM_OUTPUT:
726 return snd_rawmidi_info_user(rfile->output, info);
727 default:
728 return -EINVAL;
729 }
730 }
731 case SNDRV_RAWMIDI_IOCTL_PARAMS:
732 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100733 struct snd_rawmidi_params params;
734 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -EFAULT;
736 switch (params.stream) {
737 case SNDRV_RAWMIDI_STREAM_OUTPUT:
738 if (rfile->output == NULL)
739 return -EINVAL;
740 return snd_rawmidi_output_params(rfile->output, &params);
741 case SNDRV_RAWMIDI_STREAM_INPUT:
742 if (rfile->input == NULL)
743 return -EINVAL;
744 return snd_rawmidi_input_params(rfile->input, &params);
745 default:
746 return -EINVAL;
747 }
748 }
749 case SNDRV_RAWMIDI_IOCTL_STATUS:
750 {
751 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100752 struct snd_rawmidi_status status;
753 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return -EFAULT;
755 switch (status.stream) {
756 case SNDRV_RAWMIDI_STREAM_OUTPUT:
757 if (rfile->output == NULL)
758 return -EINVAL;
759 err = snd_rawmidi_output_status(rfile->output, &status);
760 break;
761 case SNDRV_RAWMIDI_STREAM_INPUT:
762 if (rfile->input == NULL)
763 return -EINVAL;
764 err = snd_rawmidi_input_status(rfile->input, &status);
765 break;
766 default:
767 return -EINVAL;
768 }
769 if (err < 0)
770 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100771 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -EFAULT;
773 return 0;
774 }
775 case SNDRV_RAWMIDI_IOCTL_DROP:
776 {
777 int val;
778 if (get_user(val, (int __user *) argp))
779 return -EFAULT;
780 switch (val) {
781 case SNDRV_RAWMIDI_STREAM_OUTPUT:
782 if (rfile->output == NULL)
783 return -EINVAL;
784 return snd_rawmidi_drop_output(rfile->output);
785 default:
786 return -EINVAL;
787 }
788 }
789 case SNDRV_RAWMIDI_IOCTL_DRAIN:
790 {
791 int val;
792 if (get_user(val, (int __user *) argp))
793 return -EFAULT;
794 switch (val) {
795 case SNDRV_RAWMIDI_STREAM_OUTPUT:
796 if (rfile->output == NULL)
797 return -EINVAL;
798 return snd_rawmidi_drain_output(rfile->output);
799 case SNDRV_RAWMIDI_STREAM_INPUT:
800 if (rfile->input == NULL)
801 return -EINVAL;
802 return snd_rawmidi_drain_input(rfile->input);
803 default:
804 return -EINVAL;
805 }
806 }
807#ifdef CONFIG_SND_DEBUG
808 default:
809 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
810#endif
811 }
812 return -ENOTTY;
813}
814
Takashi Iwai48c9d412005-11-17 13:56:51 +0100815static int snd_rawmidi_control_ioctl(struct snd_card *card,
816 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 unsigned int cmd,
818 unsigned long arg)
819{
820 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 switch (cmd) {
823 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
824 {
825 int device;
826
827 if (get_user(device, (int __user *)argp))
828 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100829 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 device = device < 0 ? 0 : device + 1;
831 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100832 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 break;
834 device++;
835 }
836 if (device == SNDRV_RAWMIDI_DEVICES)
837 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100838 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (put_user(device, (int __user *)argp))
840 return -EFAULT;
841 return 0;
842 }
843 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
844 {
845 int val;
846
847 if (get_user(val, (int __user *)argp))
848 return -EFAULT;
849 control->prefer_rawmidi_subdevice = val;
850 return 0;
851 }
852 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
853 return snd_rawmidi_info_select_user(card, argp);
854 }
855 return -ENOIOCTLCMD;
856}
857
858/**
859 * snd_rawmidi_receive - receive the input data from the device
860 * @substream: the rawmidi substream
861 * @buffer: the buffer pointer
862 * @count: the data size to read
863 *
864 * Reads the data from the internal buffer.
865 *
866 * Returns the size of read data, or a negative error code on failure.
867 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100868int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
869 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 unsigned long flags;
872 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100873 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Takashi Iwai219df322008-11-03 08:17:05 +0100875 if (!substream->opened)
876 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (runtime->buffer == NULL) {
878 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
879 return -EINVAL;
880 }
881 spin_lock_irqsave(&runtime->lock, flags);
882 if (count == 1) { /* special case, faster code */
883 substream->bytes++;
884 if (runtime->avail < runtime->buffer_size) {
885 runtime->buffer[runtime->hw_ptr++] = buffer[0];
886 runtime->hw_ptr %= runtime->buffer_size;
887 runtime->avail++;
888 result++;
889 } else {
890 runtime->xruns++;
891 }
892 } else {
893 substream->bytes += count;
894 count1 = runtime->buffer_size - runtime->hw_ptr;
895 if (count1 > count)
896 count1 = count;
897 if (count1 > (int)(runtime->buffer_size - runtime->avail))
898 count1 = runtime->buffer_size - runtime->avail;
899 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
900 runtime->hw_ptr += count1;
901 runtime->hw_ptr %= runtime->buffer_size;
902 runtime->avail += count1;
903 count -= count1;
904 result += count1;
905 if (count > 0) {
906 buffer += count1;
907 count1 = count;
908 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
909 count1 = runtime->buffer_size - runtime->avail;
910 runtime->xruns += count - count1;
911 }
912 if (count1 > 0) {
913 memcpy(runtime->buffer, buffer, count1);
914 runtime->hw_ptr = count1;
915 runtime->avail += count1;
916 result += count1;
917 }
918 }
919 }
920 if (result > 0) {
921 if (runtime->event)
Takashi Iwai1f041282008-12-18 12:17:55 +0100922 tasklet_schedule(&runtime->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 else if (snd_rawmidi_ready(substream))
924 wake_up(&runtime->sleep);
925 }
926 spin_unlock_irqrestore(&runtime->lock, flags);
927 return result;
928}
929
Takashi Iwai48c9d412005-11-17 13:56:51 +0100930static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100931 unsigned char __user *userbuf,
932 unsigned char *kernelbuf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 unsigned long flags;
935 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100936 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 while (count > 0 && runtime->avail) {
939 count1 = runtime->buffer_size - runtime->appl_ptr;
940 if (count1 > count)
941 count1 = count;
942 spin_lock_irqsave(&runtime->lock, flags);
943 if (count1 > (int)runtime->avail)
944 count1 = runtime->avail;
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100945 if (kernelbuf)
946 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
947 if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 spin_unlock_irqrestore(&runtime->lock, flags);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100949 if (copy_to_user(userbuf + result,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 runtime->buffer + runtime->appl_ptr, count1)) {
951 return result > 0 ? result : -EFAULT;
952 }
953 spin_lock_irqsave(&runtime->lock, flags);
954 }
955 runtime->appl_ptr += count1;
956 runtime->appl_ptr %= runtime->buffer_size;
957 runtime->avail -= count1;
958 spin_unlock_irqrestore(&runtime->lock, flags);
959 result += count1;
960 count -= count1;
961 }
962 return result;
963}
964
Takashi Iwai48c9d412005-11-17 13:56:51 +0100965long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
966 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 snd_rawmidi_input_trigger(substream, 1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100969 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Takashi Iwai48c9d412005-11-17 13:56:51 +0100972static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
973 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 long result;
976 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100977 struct snd_rawmidi_file *rfile;
978 struct snd_rawmidi_substream *substream;
979 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 rfile = file->private_data;
982 substream = rfile->input;
983 if (substream == NULL)
984 return -EIO;
985 runtime = substream->runtime;
986 snd_rawmidi_input_trigger(substream, 1);
987 result = 0;
988 while (count > 0) {
989 spin_lock_irq(&runtime->lock);
990 while (!snd_rawmidi_ready(substream)) {
991 wait_queue_t wait;
992 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
993 spin_unlock_irq(&runtime->lock);
994 return result > 0 ? result : -EAGAIN;
995 }
996 init_waitqueue_entry(&wait, current);
997 add_wait_queue(&runtime->sleep, &wait);
998 set_current_state(TASK_INTERRUPTIBLE);
999 spin_unlock_irq(&runtime->lock);
1000 schedule();
1001 remove_wait_queue(&runtime->sleep, &wait);
1002 if (signal_pending(current))
1003 return result > 0 ? result : -ERESTARTSYS;
1004 if (!runtime->avail)
1005 return result > 0 ? result : -EIO;
1006 spin_lock_irq(&runtime->lock);
1007 }
1008 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001009 count1 = snd_rawmidi_kernel_read1(substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001010 (unsigned char __user *)buf,
1011 NULL/*kernelbuf*/,
1012 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (count1 < 0)
1014 return result > 0 ? result : count1;
1015 result += count1;
1016 buf += count1;
1017 count -= count1;
1018 }
1019 return result;
1020}
1021
1022/**
1023 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1024 * @substream: the rawmidi substream
1025 *
1026 * Returns 1 if the internal output buffer is empty, 0 if not.
1027 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001028int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001030 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 int result;
1032 unsigned long flags;
1033
1034 if (runtime->buffer == NULL) {
1035 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1036 return 1;
1037 }
1038 spin_lock_irqsave(&runtime->lock, flags);
1039 result = runtime->avail >= runtime->buffer_size;
1040 spin_unlock_irqrestore(&runtime->lock, flags);
1041 return result;
1042}
1043
1044/**
1045 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1046 * @substream: the rawmidi substream
1047 * @buffer: the buffer pointer
1048 * @count: data size to transfer
1049 *
1050 * Copies data from the internal output buffer to the given buffer.
1051 *
1052 * Call this in the interrupt handler when the midi output is ready,
1053 * and call snd_rawmidi_transmit_ack() after the transmission is
1054 * finished.
1055 *
1056 * Returns the size of copied data, or a negative error code on failure.
1057 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001058int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1059 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 unsigned long flags;
1062 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001063 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 if (runtime->buffer == NULL) {
1066 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1067 return -EINVAL;
1068 }
1069 result = 0;
1070 spin_lock_irqsave(&runtime->lock, flags);
1071 if (runtime->avail >= runtime->buffer_size) {
1072 /* warning: lowlevel layer MUST trigger down the hardware */
1073 goto __skip;
1074 }
1075 if (count == 1) { /* special case, faster code */
1076 *buffer = runtime->buffer[runtime->hw_ptr];
1077 result++;
1078 } else {
1079 count1 = runtime->buffer_size - runtime->hw_ptr;
1080 if (count1 > count)
1081 count1 = count;
1082 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1083 count1 = runtime->buffer_size - runtime->avail;
1084 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1085 count -= count1;
1086 result += count1;
1087 if (count > 0) {
1088 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1089 count = runtime->buffer_size - runtime->avail - count1;
1090 memcpy(buffer + count1, runtime->buffer, count);
1091 result += count;
1092 }
1093 }
1094 __skip:
1095 spin_unlock_irqrestore(&runtime->lock, flags);
1096 return result;
1097}
1098
1099/**
1100 * snd_rawmidi_transmit_ack - acknowledge the transmission
1101 * @substream: the rawmidi substream
1102 * @count: the tranferred count
1103 *
1104 * Advances the hardware pointer for the internal output buffer with
1105 * the given size and updates the condition.
1106 * Call after the transmission is finished.
1107 *
1108 * Returns the advanced size if successful, or a negative error code on failure.
1109 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001110int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001113 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 if (runtime->buffer == NULL) {
1116 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1117 return -EINVAL;
1118 }
1119 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001120 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 runtime->hw_ptr += count;
1122 runtime->hw_ptr %= runtime->buffer_size;
1123 runtime->avail += count;
1124 substream->bytes += count;
1125 if (count > 0) {
1126 if (runtime->drain || snd_rawmidi_ready(substream))
1127 wake_up(&runtime->sleep);
1128 }
1129 spin_unlock_irqrestore(&runtime->lock, flags);
1130 return count;
1131}
1132
1133/**
1134 * snd_rawmidi_transmit - copy from the buffer to the device
1135 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001136 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 * @count: the data size to transfer
1138 *
1139 * Copies data from the buffer to the device and advances the pointer.
1140 *
1141 * Returns the copied size if successful, or a negative error code on failure.
1142 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001143int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1144 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Takashi Iwai219df322008-11-03 08:17:05 +01001146 if (!substream->opened)
1147 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1149 if (count < 0)
1150 return count;
1151 return snd_rawmidi_transmit_ack(substream, count);
1152}
1153
Takashi Iwai48c9d412005-11-17 13:56:51 +01001154static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001155 const unsigned char __user *userbuf,
1156 const unsigned char *kernelbuf,
1157 long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 unsigned long flags;
1160 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001161 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001163 if (snd_BUG_ON(!kernelbuf && !userbuf))
1164 return -EINVAL;
1165 if (snd_BUG_ON(!runtime->buffer))
1166 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 result = 0;
1169 spin_lock_irqsave(&runtime->lock, flags);
1170 if (substream->append) {
1171 if ((long)runtime->avail < count) {
1172 spin_unlock_irqrestore(&runtime->lock, flags);
1173 return -EAGAIN;
1174 }
1175 }
1176 while (count > 0 && runtime->avail > 0) {
1177 count1 = runtime->buffer_size - runtime->appl_ptr;
1178 if (count1 > count)
1179 count1 = count;
1180 if (count1 > (long)runtime->avail)
1181 count1 = runtime->avail;
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001182 if (kernelbuf)
1183 memcpy(runtime->buffer + runtime->appl_ptr,
1184 kernelbuf + result, count1);
1185 else if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 spin_unlock_irqrestore(&runtime->lock, flags);
1187 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001188 userbuf + result, count1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 spin_lock_irqsave(&runtime->lock, flags);
1190 result = result > 0 ? result : -EFAULT;
1191 goto __end;
1192 }
1193 spin_lock_irqsave(&runtime->lock, flags);
1194 }
1195 runtime->appl_ptr += count1;
1196 runtime->appl_ptr %= runtime->buffer_size;
1197 runtime->avail -= count1;
1198 result += count1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 count -= count1;
1200 }
1201 __end:
1202 count1 = runtime->avail < runtime->buffer_size;
1203 spin_unlock_irqrestore(&runtime->lock, flags);
1204 if (count1)
1205 snd_rawmidi_output_trigger(substream, 1);
1206 return result;
1207}
1208
Takashi Iwai48c9d412005-11-17 13:56:51 +01001209long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1210 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001212 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Takashi Iwai48c9d412005-11-17 13:56:51 +01001215static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1216 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 long result, timeout;
1219 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001220 struct snd_rawmidi_file *rfile;
1221 struct snd_rawmidi_runtime *runtime;
1222 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 rfile = file->private_data;
1225 substream = rfile->output;
1226 runtime = substream->runtime;
1227 /* we cannot put an atomic message to our buffer */
1228 if (substream->append && count > runtime->buffer_size)
1229 return -EIO;
1230 result = 0;
1231 while (count > 0) {
1232 spin_lock_irq(&runtime->lock);
1233 while (!snd_rawmidi_ready_append(substream, count)) {
1234 wait_queue_t wait;
1235 if (file->f_flags & O_NONBLOCK) {
1236 spin_unlock_irq(&runtime->lock);
1237 return result > 0 ? result : -EAGAIN;
1238 }
1239 init_waitqueue_entry(&wait, current);
1240 add_wait_queue(&runtime->sleep, &wait);
1241 set_current_state(TASK_INTERRUPTIBLE);
1242 spin_unlock_irq(&runtime->lock);
1243 timeout = schedule_timeout(30 * HZ);
1244 remove_wait_queue(&runtime->sleep, &wait);
1245 if (signal_pending(current))
1246 return result > 0 ? result : -ERESTARTSYS;
1247 if (!runtime->avail && !timeout)
1248 return result > 0 ? result : -EIO;
1249 spin_lock_irq(&runtime->lock);
1250 }
1251 spin_unlock_irq(&runtime->lock);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001252 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (count1 < 0)
1254 return result > 0 ? result : count1;
1255 result += count1;
1256 buf += count1;
1257 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1258 break;
1259 count -= count1;
1260 }
1261 if (file->f_flags & O_SYNC) {
1262 spin_lock_irq(&runtime->lock);
1263 while (runtime->avail != runtime->buffer_size) {
1264 wait_queue_t wait;
1265 unsigned int last_avail = runtime->avail;
1266 init_waitqueue_entry(&wait, current);
1267 add_wait_queue(&runtime->sleep, &wait);
1268 set_current_state(TASK_INTERRUPTIBLE);
1269 spin_unlock_irq(&runtime->lock);
1270 timeout = schedule_timeout(30 * HZ);
1271 remove_wait_queue(&runtime->sleep, &wait);
1272 if (signal_pending(current))
1273 return result > 0 ? result : -ERESTARTSYS;
1274 if (runtime->avail == last_avail && !timeout)
1275 return result > 0 ? result : -EIO;
1276 spin_lock_irq(&runtime->lock);
1277 }
1278 spin_unlock_irq(&runtime->lock);
1279 }
1280 return result;
1281}
1282
1283static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1284{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001285 struct snd_rawmidi_file *rfile;
1286 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 unsigned int mask;
1288
1289 rfile = file->private_data;
1290 if (rfile->input != NULL) {
1291 runtime = rfile->input->runtime;
1292 snd_rawmidi_input_trigger(rfile->input, 1);
1293 poll_wait(file, &runtime->sleep, wait);
1294 }
1295 if (rfile->output != NULL) {
1296 runtime = rfile->output->runtime;
1297 poll_wait(file, &runtime->sleep, wait);
1298 }
1299 mask = 0;
1300 if (rfile->input != NULL) {
1301 if (snd_rawmidi_ready(rfile->input))
1302 mask |= POLLIN | POLLRDNORM;
1303 }
1304 if (rfile->output != NULL) {
1305 if (snd_rawmidi_ready(rfile->output))
1306 mask |= POLLOUT | POLLWRNORM;
1307 }
1308 return mask;
1309}
1310
1311/*
1312 */
1313#ifdef CONFIG_COMPAT
1314#include "rawmidi_compat.c"
1315#else
1316#define snd_rawmidi_ioctl_compat NULL
1317#endif
1318
1319/*
1320
1321 */
1322
Takashi Iwai48c9d412005-11-17 13:56:51 +01001323static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1324 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001326 struct snd_rawmidi *rmidi;
1327 struct snd_rawmidi_substream *substream;
1328 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 rmidi = entry->private_data;
1331 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001332 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001334 list_for_each_entry(substream,
1335 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1336 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 snd_iprintf(buffer,
1338 "Output %d\n"
1339 " Tx bytes : %lu\n",
1340 substream->number,
1341 (unsigned long) substream->bytes);
1342 if (substream->opened) {
1343 runtime = substream->runtime;
1344 snd_iprintf(buffer,
1345 " Mode : %s\n"
1346 " Buffer size : %lu\n"
1347 " Avail : %lu\n",
1348 runtime->oss ? "OSS compatible" : "native",
1349 (unsigned long) runtime->buffer_size,
1350 (unsigned long) runtime->avail);
1351 }
1352 }
1353 }
1354 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001355 list_for_each_entry(substream,
1356 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1357 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 snd_iprintf(buffer,
1359 "Input %d\n"
1360 " Rx bytes : %lu\n",
1361 substream->number,
1362 (unsigned long) substream->bytes);
1363 if (substream->opened) {
1364 runtime = substream->runtime;
1365 snd_iprintf(buffer,
1366 " Buffer size : %lu\n"
1367 " Avail : %lu\n"
1368 " Overruns : %lu\n",
1369 (unsigned long) runtime->buffer_size,
1370 (unsigned long) runtime->avail,
1371 (unsigned long) runtime->xruns);
1372 }
1373 }
1374 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001375 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
1378/*
1379 * Register functions
1380 */
1381
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001382static const struct file_operations snd_rawmidi_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 .owner = THIS_MODULE,
1385 .read = snd_rawmidi_read,
1386 .write = snd_rawmidi_write,
1387 .open = snd_rawmidi_open,
1388 .release = snd_rawmidi_release,
1389 .poll = snd_rawmidi_poll,
1390 .unlocked_ioctl = snd_rawmidi_ioctl,
1391 .compat_ioctl = snd_rawmidi_ioctl_compat,
1392};
1393
Takashi Iwai48c9d412005-11-17 13:56:51 +01001394static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1395 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 int direction,
1397 int count)
1398{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001399 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int idx;
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001403 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001404 if (substream == NULL) {
1405 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 substream->stream = direction;
1409 substream->number = idx;
1410 substream->rmidi = rmidi;
1411 substream->pstr = stream;
1412 list_add_tail(&substream->list, &stream->substreams);
1413 stream->substream_count++;
1414 }
1415 return 0;
1416}
1417
1418/**
1419 * snd_rawmidi_new - create a rawmidi instance
1420 * @card: the card instance
1421 * @id: the id string
1422 * @device: the device index
1423 * @output_count: the number of output streams
1424 * @input_count: the number of input streams
1425 * @rrawmidi: the pointer to store the new rawmidi instance
1426 *
1427 * Creates a new rawmidi instance.
1428 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1429 *
1430 * Returns zero if successful, or a negative error code on failure.
1431 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001432int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int output_count, int input_count,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001434 struct snd_rawmidi ** rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001436 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001438 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 .dev_free = snd_rawmidi_dev_free,
1440 .dev_register = snd_rawmidi_dev_register,
1441 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 };
1443
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001444 if (snd_BUG_ON(!card))
1445 return -ENXIO;
1446 if (rrawmidi)
1447 *rrawmidi = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001448 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001449 if (rmidi == NULL) {
1450 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 rmidi->card = card;
1454 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001455 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001457 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1458 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (id != NULL)
1461 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001462 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1463 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1464 SNDRV_RAWMIDI_STREAM_INPUT,
1465 input_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 snd_rawmidi_free(rmidi);
1467 return err;
1468 }
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001469 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1470 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1471 SNDRV_RAWMIDI_STREAM_OUTPUT,
1472 output_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 snd_rawmidi_free(rmidi);
1474 return err;
1475 }
1476 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1477 snd_rawmidi_free(rmidi);
1478 return err;
1479 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001480 if (rrawmidi)
1481 *rrawmidi = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return 0;
1483}
1484
Takashi Iwai48c9d412005-11-17 13:56:51 +01001485static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001487 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001490 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 list_del(&substream->list);
1492 kfree(substream);
1493 }
1494}
1495
Takashi Iwai48c9d412005-11-17 13:56:51 +01001496static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001498 if (!rmidi)
1499 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +02001500
1501 snd_info_free_entry(rmidi->proc_entry);
1502 rmidi->proc_entry = NULL;
1503 mutex_lock(&register_mutex);
1504 if (rmidi->ops && rmidi->ops->dev_unregister)
1505 rmidi->ops->dev_unregister(rmidi);
1506 mutex_unlock(&register_mutex);
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1509 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1510 if (rmidi->private_free)
1511 rmidi->private_free(rmidi);
1512 kfree(rmidi);
1513 return 0;
1514}
1515
Takashi Iwai48c9d412005-11-17 13:56:51 +01001516static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001518 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return snd_rawmidi_free(rmidi);
1520}
1521
1522#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
Takashi Iwai48c9d412005-11-17 13:56:51 +01001523static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001525 struct snd_rawmidi *rmidi = device->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 rmidi->seq_dev = NULL;
1527}
1528#endif
1529
Takashi Iwai48c9d412005-11-17 13:56:51 +01001530static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001532 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001533 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001535 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1538 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001539 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001540 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001541 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return -EBUSY;
1543 }
Clemens Ladischf87135f2005-11-20 14:06:59 +01001544 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1546 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1547 rmidi->card, rmidi->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001548 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001550 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001551 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return err;
1553 }
1554 if (rmidi->ops && rmidi->ops->dev_register &&
1555 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1556 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001557 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001558 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return err;
1560 }
1561#ifdef CONFIG_SND_OSSEMUL
1562 rmidi->ossreg = 0;
1563 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1564 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001565 rmidi->card, 0, &snd_rawmidi_f_ops,
1566 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1568 } else {
1569 rmidi->ossreg++;
1570#ifdef SNDRV_OSS_INFO_DEV_MIDI
1571 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1572#endif
1573 }
1574 }
1575 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1576 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001577 rmidi->card, 1, &snd_rawmidi_f_ops,
1578 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1580 } else {
1581 rmidi->ossreg++;
1582 }
1583 }
1584#endif /* CONFIG_SND_OSSEMUL */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001585 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 sprintf(name, "midi%d", rmidi->device);
1587 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1588 if (entry) {
1589 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 entry->c.text.read = snd_rawmidi_proc_info_read;
1591 if (snd_info_register(entry) < 0) {
1592 snd_info_free_entry(entry);
1593 entry = NULL;
1594 }
1595 }
1596 rmidi->proc_entry = entry;
1597#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1598 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1599 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1600 rmidi->seq_dev->private_data = rmidi;
1601 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1602 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1603 snd_device_register(rmidi->card, rmidi->seq_dev);
1604 }
1605 }
1606#endif
1607 return 0;
1608}
1609
Takashi Iwai48c9d412005-11-17 13:56:51 +01001610static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001612 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001614 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001615 list_del_init(&rmidi->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616#ifdef CONFIG_SND_OSSEMUL
1617 if (rmidi->ossreg) {
1618 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1619 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1620#ifdef SNDRV_OSS_INFO_DEV_MIDI
1621 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1622#endif
1623 }
1624 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1625 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1626 rmidi->ossreg = 0;
1627 }
1628#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001630 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
1634/**
1635 * snd_rawmidi_set_ops - set the rawmidi operators
1636 * @rmidi: the rawmidi instance
1637 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1638 * @ops: the operator table
1639 *
1640 * Sets the rawmidi operators for the given stream direction.
1641 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001642void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1643 struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001645 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Johannes Berg9244b2c2006-10-05 16:02:22 +02001647 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
1651/*
1652 * ENTRY functions
1653 */
1654
1655static int __init alsa_rawmidi_init(void)
1656{
1657
1658 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1659 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1660#ifdef CONFIG_SND_OSSEMUL
1661 { int i;
1662 /* check device map table */
1663 for (i = 0; i < SNDRV_CARDS; i++) {
1664 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1665 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1666 midi_map[i] = 0;
1667 }
1668 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1669 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1670 amidi_map[i] = 1;
1671 }
1672 }
1673 }
1674#endif /* CONFIG_SND_OSSEMUL */
1675 return 0;
1676}
1677
1678static void __exit alsa_rawmidi_exit(void)
1679{
1680 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1681 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1682}
1683
1684module_init(alsa_rawmidi_init)
1685module_exit(alsa_rawmidi_exit)
1686
1687EXPORT_SYMBOL(snd_rawmidi_output_params);
1688EXPORT_SYMBOL(snd_rawmidi_input_params);
1689EXPORT_SYMBOL(snd_rawmidi_drop_output);
1690EXPORT_SYMBOL(snd_rawmidi_drain_output);
1691EXPORT_SYMBOL(snd_rawmidi_drain_input);
1692EXPORT_SYMBOL(snd_rawmidi_receive);
1693EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1694EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1695EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1696EXPORT_SYMBOL(snd_rawmidi_transmit);
1697EXPORT_SYMBOL(snd_rawmidi_new);
1698EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699EXPORT_SYMBOL(snd_rawmidi_info_select);
1700EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1701EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1702EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1703EXPORT_SYMBOL(snd_rawmidi_kernel_write);