blob: e470c3c7d61162011b5dde5ff31c13f825e3a010 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Abstract layer for MIDI v1.0 stream
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
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
22#include <sound/driver.h>
23#include <sound/core.h>
24#include <linux/major.h>
25#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/sched.h>
27#include <linux/slab.h>
28#include <linux/time.h>
29#include <linux/wait.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/moduleparam.h>
32#include <linux/delay.h>
33#include <linux/wait.h>
34#include <sound/rawmidi.h>
35#include <sound/info.h>
36#include <sound/control.h>
37#include <sound/minors.h>
38#include <sound/initval.h>
39
40MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
41MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
42MODULE_LICENSE("GPL");
43
44#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai6581f4e2006-05-17 17:14:51 +020045static int midi_map[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
47module_param_array(midi_map, int, NULL, 0444);
48MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
49module_param_array(amidi_map, int, NULL, 0444);
50MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
51#endif /* CONFIG_SND_OSSEMUL */
52
Takashi Iwai48c9d412005-11-17 13:56:51 +010053static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
54static int snd_rawmidi_dev_free(struct snd_device *device);
55static int snd_rawmidi_dev_register(struct snd_device *device);
56static int snd_rawmidi_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Clemens Ladischf87135f2005-11-20 14:06:59 +010058static LIST_HEAD(snd_rawmidi_devices);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010059static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Clemens Ladischf87135f2005-11-20 14:06:59 +010061static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
62{
Clemens Ladischf87135f2005-11-20 14:06:59 +010063 struct snd_rawmidi *rawmidi;
64
Johannes Berg9244b2c2006-10-05 16:02:22 +020065 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
Clemens Ladischf87135f2005-11-20 14:06:59 +010066 if (rawmidi->card == card && rawmidi->device == device)
67 return rawmidi;
Clemens Ladischf87135f2005-11-20 14:06:59 +010068 return NULL;
69}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static inline unsigned short snd_rawmidi_file_flags(struct file *file)
72{
73 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
74 case FMODE_WRITE:
75 return SNDRV_RAWMIDI_LFLG_OUTPUT;
76 case FMODE_READ:
77 return SNDRV_RAWMIDI_LFLG_INPUT;
78 default:
79 return SNDRV_RAWMIDI_LFLG_OPEN;
80 }
81}
82
Takashi Iwai48c9d412005-11-17 13:56:51 +010083static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Takashi Iwai48c9d412005-11-17 13:56:51 +010085 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return runtime->avail >= runtime->avail_min;
87}
88
Takashi Iwai48c9d412005-11-17 13:56:51 +010089static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
90 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Takashi Iwai48c9d412005-11-17 13:56:51 +010092 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return runtime->avail >= runtime->avail_min &&
94 (!substream->append || runtime->avail >= count);
95}
96
97static void snd_rawmidi_input_event_tasklet(unsigned long data)
98{
Takashi Iwai48c9d412005-11-17 13:56:51 +010099 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 substream->runtime->event(substream);
101}
102
103static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
104{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100105 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 substream->ops->trigger(substream, 1);
107}
108
Takashi Iwai48c9d412005-11-17 13:56:51 +0100109static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100111 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Takashi Iwaica2c0962005-09-09 14:20:23 +0200113 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -ENOMEM;
115 spin_lock_init(&runtime->lock);
116 init_waitqueue_head(&runtime->sleep);
117 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
118 tasklet_init(&runtime->tasklet,
119 snd_rawmidi_input_event_tasklet,
120 (unsigned long)substream);
121 else
122 tasklet_init(&runtime->tasklet,
123 snd_rawmidi_output_trigger_tasklet,
124 (unsigned long)substream);
125 runtime->event = NULL;
126 runtime->buffer_size = PAGE_SIZE;
127 runtime->avail_min = 1;
128 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
129 runtime->avail = 0;
130 else
131 runtime->avail = runtime->buffer_size;
132 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
133 kfree(runtime);
134 return -ENOMEM;
135 }
136 runtime->appl_ptr = runtime->hw_ptr = 0;
137 substream->runtime = runtime;
138 return 0;
139}
140
Takashi Iwai48c9d412005-11-17 13:56:51 +0100141static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100143 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 kfree(runtime->buffer);
146 kfree(runtime);
147 substream->runtime = NULL;
148 return 0;
149}
150
Takashi Iwai48c9d412005-11-17 13:56:51 +0100151static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 if (up) {
154 tasklet_hi_schedule(&substream->runtime->tasklet);
155 } 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{
163 substream->ops->trigger(substream, up);
164 if (!up && substream->runtime->event)
165 tasklet_kill(&substream->runtime->tasklet);
166}
167
Takashi Iwai48c9d412005-11-17 13:56:51 +0100168int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100171 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 snd_rawmidi_output_trigger(substream, 0);
174 runtime->drain = 0;
175 spin_lock_irqsave(&runtime->lock, flags);
176 runtime->appl_ptr = runtime->hw_ptr = 0;
177 runtime->avail = runtime->buffer_size;
178 spin_unlock_irqrestore(&runtime->lock, flags);
179 return 0;
180}
181
Takashi Iwai48c9d412005-11-17 13:56:51 +0100182int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 int err;
185 long timeout;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100186 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 err = 0;
189 runtime->drain = 1;
190 timeout = wait_event_interruptible_timeout(runtime->sleep,
191 (runtime->avail >= runtime->buffer_size),
192 10*HZ);
193 if (signal_pending(current))
194 err = -ERESTARTSYS;
195 if (runtime->avail < runtime->buffer_size && !timeout) {
196 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
197 err = -EIO;
198 }
199 runtime->drain = 0;
200 if (err != -ERESTARTSYS) {
201 /* we need wait a while to make sure that Tx FIFOs are empty */
202 if (substream->ops->drain)
203 substream->ops->drain(substream);
204 else
205 msleep(50);
206 snd_rawmidi_drop_output(substream);
207 }
208 return err;
209}
210
Takashi Iwai48c9d412005-11-17 13:56:51 +0100211int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100214 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 snd_rawmidi_input_trigger(substream, 0);
217 runtime->drain = 0;
218 spin_lock_irqsave(&runtime->lock, flags);
219 runtime->appl_ptr = runtime->hw_ptr = 0;
220 runtime->avail = 0;
221 spin_unlock_irqrestore(&runtime->lock, flags);
222 return 0;
223}
224
Clemens Ladischf87135f2005-11-20 14:06:59 +0100225int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai48c9d412005-11-17 13:56:51 +0100226 int mode, struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100228 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct list_head *list1, *list2;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100230 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
231 struct snd_rawmidi_runtime *input = NULL, *output = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int err;
233
234 if (rfile)
235 rfile->input = rfile->output = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100236 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100237 rmidi = snd_rawmidi_search(card, device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100238 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (rmidi == NULL) {
240 err = -ENODEV;
241 goto __error1;
242 }
243 if (!try_module_get(rmidi->card->module)) {
244 err = -EFAULT;
245 goto __error1;
246 }
247 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100248 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
250 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
251 err = -ENXIO;
252 goto __error;
253 }
254 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
255 err = -ENODEV;
256 goto __error;
257 }
258 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
259 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
260 err = -EAGAIN;
261 goto __error;
262 }
263 }
264 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
265 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
266 err = -ENXIO;
267 goto __error;
268 }
269 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
270 err = -ENODEV;
271 goto __error;
272 }
273 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
274 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
275 err = -EAGAIN;
276 goto __error;
277 }
278 }
279 list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
280 while (1) {
281 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
282 sinput = NULL;
283 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
284 err = -EAGAIN;
285 goto __error;
286 }
287 break;
288 }
Takashi Iwai48c9d412005-11-17 13:56:51 +0100289 sinput = list_entry(list1, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
291 goto __nexti;
292 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
293 break;
294 __nexti:
295 list1 = list1->next;
296 }
297 list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
298 while (1) {
299 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
300 soutput = NULL;
301 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
302 err = -EAGAIN;
303 goto __error;
304 }
305 break;
306 }
Takashi Iwai48c9d412005-11-17 13:56:51 +0100307 soutput = list_entry(list2, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
309 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
310 if (soutput->opened && !soutput->append)
311 goto __nexto;
312 } else {
313 if (soutput->opened)
314 goto __nexto;
315 }
316 }
317 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
318 break;
319 __nexto:
320 list2 = list2->next;
321 }
322 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
323 if ((err = snd_rawmidi_runtime_create(sinput)) < 0)
324 goto __error;
325 input = sinput->runtime;
326 if ((err = sinput->ops->open(sinput)) < 0)
327 goto __error;
328 sinput->opened = 1;
329 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
330 } else {
331 sinput = NULL;
332 }
333 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
334 if (soutput->opened)
335 goto __skip_output;
336 if ((err = snd_rawmidi_runtime_create(soutput)) < 0) {
337 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
338 sinput->ops->close(sinput);
339 goto __error;
340 }
341 output = soutput->runtime;
342 if ((err = soutput->ops->open(soutput)) < 0) {
343 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
344 sinput->ops->close(sinput);
345 goto __error;
346 }
347 __skip_output:
348 soutput->opened = 1;
349 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
350 soutput->append = 1;
351 if (soutput->use_count++ == 0)
352 soutput->active_sensing = 1;
353 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
354 } else {
355 soutput = NULL;
356 }
357 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100358 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (rfile) {
360 rfile->rmidi = rmidi;
361 rfile->input = sinput;
362 rfile->output = soutput;
363 }
364 return 0;
365
366 __error:
367 if (input != NULL)
368 snd_rawmidi_runtime_free(sinput);
369 if (output != NULL)
370 snd_rawmidi_runtime_free(soutput);
371 module_put(rmidi->card->module);
372 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100373 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 __error1:
375 return err;
376}
377
378static int snd_rawmidi_open(struct inode *inode, struct file *file)
379{
380 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100381 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100382 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 unsigned short fflags;
384 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100385 struct snd_rawmidi *rmidi;
386 struct snd_rawmidi_file *rawmidi_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 wait_queue_t wait;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100388 struct snd_ctl_file *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Clemens Ladischf1902862005-10-24 17:05:03 +0200390 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100391 rmidi = snd_lookup_minor_data(iminor(inode),
392 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200394 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100395 rmidi = snd_lookup_oss_minor_data(iminor(inode),
396 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200398 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (rmidi == NULL)
402 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
404 return -EINVAL; /* invalid combination */
405 card = rmidi->card;
406 err = snd_card_file_add(card, file);
407 if (err < 0)
408 return -ENODEV;
409 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200410 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
412 fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
413 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
414 if (rawmidi_file == NULL) {
415 snd_card_file_remove(card, file);
416 return -ENOMEM;
417 }
418 init_waitqueue_entry(&wait, current);
419 add_wait_queue(&rmidi->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100420 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 while (1) {
422 subdevice = -1;
423 down_read(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200424 list_for_each_entry(kctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (kctl->pid == current->pid) {
426 subdevice = kctl->prefer_rawmidi_subdevice;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200427 if (subdevice != -1)
428 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 }
431 up_read(&card->controls_rwsem);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100432 err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
433 subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (err >= 0)
435 break;
436 if (err == -EAGAIN) {
437 if (file->f_flags & O_NONBLOCK) {
438 err = -EBUSY;
439 break;
440 }
441 } else
442 break;
443 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100444 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100446 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (signal_pending(current)) {
448 err = -ERESTARTSYS;
449 break;
450 }
451 }
452#ifdef CONFIG_SND_OSSEMUL
453 if (rawmidi_file->input && rawmidi_file->input->runtime)
454 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
455 if (rawmidi_file->output && rawmidi_file->output->runtime)
456 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
457#endif
458 remove_wait_queue(&rmidi->open_wait, &wait);
459 if (err >= 0) {
460 file->private_data = rawmidi_file;
461 } else {
462 snd_card_file_remove(card, file);
463 kfree(rawmidi_file);
464 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100465 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return err;
467}
468
Takashi Iwai48c9d412005-11-17 13:56:51 +0100469int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100471 struct snd_rawmidi *rmidi;
472 struct snd_rawmidi_substream *substream;
473 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 snd_assert(rfile != NULL, return -ENXIO);
476 snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
477 rmidi = rfile->rmidi;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100478 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (rfile->input != NULL) {
480 substream = rfile->input;
481 rfile->input = NULL;
482 runtime = substream->runtime;
483 snd_rawmidi_input_trigger(substream, 0);
484 substream->ops->close(substream);
485 if (runtime->private_free != NULL)
486 runtime->private_free(substream);
487 snd_rawmidi_runtime_free(substream);
488 substream->opened = 0;
489 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
490 }
491 if (rfile->output != NULL) {
492 substream = rfile->output;
493 rfile->output = NULL;
494 if (--substream->use_count == 0) {
495 runtime = substream->runtime;
496 if (substream->active_sensing) {
497 unsigned char buf = 0xfe;
498 /* sending single active sensing message to shut the device up */
499 snd_rawmidi_kernel_write(substream, &buf, 1);
500 }
501 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
502 snd_rawmidi_output_trigger(substream, 0);
503 substream->ops->close(substream);
504 if (runtime->private_free != NULL)
505 runtime->private_free(substream);
506 snd_rawmidi_runtime_free(substream);
507 substream->opened = 0;
508 substream->append = 0;
509 }
510 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
511 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100512 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 module_put(rmidi->card->module);
514 return 0;
515}
516
517static int snd_rawmidi_release(struct inode *inode, struct file *file)
518{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100519 struct snd_rawmidi_file *rfile;
520 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int err;
522
523 rfile = file->private_data;
524 err = snd_rawmidi_kernel_release(rfile);
525 rmidi = rfile->rmidi;
526 wake_up(&rmidi->open_wait);
527 kfree(rfile);
528 snd_card_file_remove(rmidi->card, file);
529 return err;
530}
531
Adrian Bunk18612042005-11-23 13:14:50 +0100532static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
533 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100535 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (substream == NULL)
538 return -ENODEV;
539 rmidi = substream->rmidi;
540 memset(info, 0, sizeof(*info));
541 info->card = rmidi->card->number;
542 info->device = rmidi->device;
543 info->subdevice = substream->number;
544 info->stream = substream->stream;
545 info->flags = rmidi->info_flags;
546 strcpy(info->id, rmidi->id);
547 strcpy(info->name, rmidi->name);
548 strcpy(info->subname, substream->name);
549 info->subdevices_count = substream->pstr->substream_count;
550 info->subdevices_avail = (substream->pstr->substream_count -
551 substream->pstr->substream_opened);
552 return 0;
553}
554
Takashi Iwai48c9d412005-11-17 13:56:51 +0100555static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
556 struct snd_rawmidi_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100558 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int err;
560 if ((err = snd_rawmidi_info(substream, &info)) < 0)
561 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100562 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return -EFAULT;
564 return 0;
565}
566
Takashi Iwai48c9d412005-11-17 13:56:51 +0100567int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100569 struct snd_rawmidi *rmidi;
570 struct snd_rawmidi_str *pstr;
571 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100572
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100573 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100574 rmidi = snd_rawmidi_search(card, info->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100575 mutex_unlock(&register_mutex);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100576 if (!rmidi)
577 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (info->stream < 0 || info->stream > 1)
579 return -EINVAL;
580 pstr = &rmidi->streams[info->stream];
581 if (pstr->substream_count == 0)
582 return -ENOENT;
583 if (info->subdevice >= pstr->substream_count)
584 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200585 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if ((unsigned int)substream->number == info->subdevice)
587 return snd_rawmidi_info(substream, info);
588 }
589 return -ENXIO;
590}
591
Takashi Iwai48c9d412005-11-17 13:56:51 +0100592static int snd_rawmidi_info_select_user(struct snd_card *card,
593 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100596 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (get_user(info.device, &_info->device))
598 return -EFAULT;
599 if (get_user(info.stream, &_info->stream))
600 return -EFAULT;
601 if (get_user(info.subdevice, &_info->subdevice))
602 return -EFAULT;
603 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
604 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100605 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return -EFAULT;
607 return 0;
608}
609
Takashi Iwai48c9d412005-11-17 13:56:51 +0100610int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
611 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100614 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (substream->append && substream->use_count > 1)
617 return -EBUSY;
618 snd_rawmidi_drain_output(substream);
619 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
620 return -EINVAL;
621 }
622 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
623 return -EINVAL;
624 }
625 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800626 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
627 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -ENOMEM;
629 kfree(runtime->buffer);
630 runtime->buffer = newbuf;
631 runtime->buffer_size = params->buffer_size;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100632 runtime->avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634 runtime->avail_min = params->avail_min;
635 substream->active_sensing = !params->no_active_sensing;
636 return 0;
637}
638
Takashi Iwai48c9d412005-11-17 13:56:51 +0100639int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
640 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100643 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 snd_rawmidi_drain_input(substream);
646 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
647 return -EINVAL;
648 }
649 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
650 return -EINVAL;
651 }
652 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800653 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
654 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -ENOMEM;
656 kfree(runtime->buffer);
657 runtime->buffer = newbuf;
658 runtime->buffer_size = params->buffer_size;
659 }
660 runtime->avail_min = params->avail_min;
661 return 0;
662}
663
Takashi Iwai48c9d412005-11-17 13:56:51 +0100664static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
665 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100667 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 memset(status, 0, sizeof(*status));
670 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
671 spin_lock_irq(&runtime->lock);
672 status->avail = runtime->avail;
673 spin_unlock_irq(&runtime->lock);
674 return 0;
675}
676
Takashi Iwai48c9d412005-11-17 13:56:51 +0100677static int snd_rawmidi_input_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_INPUT;
684 spin_lock_irq(&runtime->lock);
685 status->avail = runtime->avail;
686 status->xruns = runtime->xruns;
687 runtime->xruns = 0;
688 spin_unlock_irq(&runtime->lock);
689 return 0;
690}
691
692static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
693{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100694 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 void __user *argp = (void __user *)arg;
696
697 rfile = file->private_data;
698 if (((cmd >> 8) & 0xff) != 'W')
699 return -ENOTTY;
700 switch (cmd) {
701 case SNDRV_RAWMIDI_IOCTL_PVERSION:
702 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
703 case SNDRV_RAWMIDI_IOCTL_INFO:
704 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100705 int stream;
706 struct snd_rawmidi_info __user *info = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (get_user(stream, &info->stream))
708 return -EFAULT;
709 switch (stream) {
710 case SNDRV_RAWMIDI_STREAM_INPUT:
711 return snd_rawmidi_info_user(rfile->input, info);
712 case SNDRV_RAWMIDI_STREAM_OUTPUT:
713 return snd_rawmidi_info_user(rfile->output, info);
714 default:
715 return -EINVAL;
716 }
717 }
718 case SNDRV_RAWMIDI_IOCTL_PARAMS:
719 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100720 struct snd_rawmidi_params params;
721 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -EFAULT;
723 switch (params.stream) {
724 case SNDRV_RAWMIDI_STREAM_OUTPUT:
725 if (rfile->output == NULL)
726 return -EINVAL;
727 return snd_rawmidi_output_params(rfile->output, &params);
728 case SNDRV_RAWMIDI_STREAM_INPUT:
729 if (rfile->input == NULL)
730 return -EINVAL;
731 return snd_rawmidi_input_params(rfile->input, &params);
732 default:
733 return -EINVAL;
734 }
735 }
736 case SNDRV_RAWMIDI_IOCTL_STATUS:
737 {
738 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100739 struct snd_rawmidi_status status;
740 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -EFAULT;
742 switch (status.stream) {
743 case SNDRV_RAWMIDI_STREAM_OUTPUT:
744 if (rfile->output == NULL)
745 return -EINVAL;
746 err = snd_rawmidi_output_status(rfile->output, &status);
747 break;
748 case SNDRV_RAWMIDI_STREAM_INPUT:
749 if (rfile->input == NULL)
750 return -EINVAL;
751 err = snd_rawmidi_input_status(rfile->input, &status);
752 break;
753 default:
754 return -EINVAL;
755 }
756 if (err < 0)
757 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100758 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return -EFAULT;
760 return 0;
761 }
762 case SNDRV_RAWMIDI_IOCTL_DROP:
763 {
764 int val;
765 if (get_user(val, (int __user *) argp))
766 return -EFAULT;
767 switch (val) {
768 case SNDRV_RAWMIDI_STREAM_OUTPUT:
769 if (rfile->output == NULL)
770 return -EINVAL;
771 return snd_rawmidi_drop_output(rfile->output);
772 default:
773 return -EINVAL;
774 }
775 }
776 case SNDRV_RAWMIDI_IOCTL_DRAIN:
777 {
778 int val;
779 if (get_user(val, (int __user *) argp))
780 return -EFAULT;
781 switch (val) {
782 case SNDRV_RAWMIDI_STREAM_OUTPUT:
783 if (rfile->output == NULL)
784 return -EINVAL;
785 return snd_rawmidi_drain_output(rfile->output);
786 case SNDRV_RAWMIDI_STREAM_INPUT:
787 if (rfile->input == NULL)
788 return -EINVAL;
789 return snd_rawmidi_drain_input(rfile->input);
790 default:
791 return -EINVAL;
792 }
793 }
794#ifdef CONFIG_SND_DEBUG
795 default:
796 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
797#endif
798 }
799 return -ENOTTY;
800}
801
Takashi Iwai48c9d412005-11-17 13:56:51 +0100802static int snd_rawmidi_control_ioctl(struct snd_card *card,
803 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 unsigned int cmd,
805 unsigned long arg)
806{
807 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 switch (cmd) {
810 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
811 {
812 int device;
813
814 if (get_user(device, (int __user *)argp))
815 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100816 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 device = device < 0 ? 0 : device + 1;
818 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100819 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 break;
821 device++;
822 }
823 if (device == SNDRV_RAWMIDI_DEVICES)
824 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100825 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (put_user(device, (int __user *)argp))
827 return -EFAULT;
828 return 0;
829 }
830 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
831 {
832 int val;
833
834 if (get_user(val, (int __user *)argp))
835 return -EFAULT;
836 control->prefer_rawmidi_subdevice = val;
837 return 0;
838 }
839 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
840 return snd_rawmidi_info_select_user(card, argp);
841 }
842 return -ENOIOCTLCMD;
843}
844
845/**
846 * snd_rawmidi_receive - receive the input data from the device
847 * @substream: the rawmidi substream
848 * @buffer: the buffer pointer
849 * @count: the data size to read
850 *
851 * Reads the data from the internal buffer.
852 *
853 * Returns the size of read data, or a negative error code on failure.
854 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100855int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
856 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 unsigned long flags;
859 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100860 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 if (runtime->buffer == NULL) {
863 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
864 return -EINVAL;
865 }
866 spin_lock_irqsave(&runtime->lock, flags);
867 if (count == 1) { /* special case, faster code */
868 substream->bytes++;
869 if (runtime->avail < runtime->buffer_size) {
870 runtime->buffer[runtime->hw_ptr++] = buffer[0];
871 runtime->hw_ptr %= runtime->buffer_size;
872 runtime->avail++;
873 result++;
874 } else {
875 runtime->xruns++;
876 }
877 } else {
878 substream->bytes += count;
879 count1 = runtime->buffer_size - runtime->hw_ptr;
880 if (count1 > count)
881 count1 = count;
882 if (count1 > (int)(runtime->buffer_size - runtime->avail))
883 count1 = runtime->buffer_size - runtime->avail;
884 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
885 runtime->hw_ptr += count1;
886 runtime->hw_ptr %= runtime->buffer_size;
887 runtime->avail += count1;
888 count -= count1;
889 result += count1;
890 if (count > 0) {
891 buffer += count1;
892 count1 = count;
893 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
894 count1 = runtime->buffer_size - runtime->avail;
895 runtime->xruns += count - count1;
896 }
897 if (count1 > 0) {
898 memcpy(runtime->buffer, buffer, count1);
899 runtime->hw_ptr = count1;
900 runtime->avail += count1;
901 result += count1;
902 }
903 }
904 }
905 if (result > 0) {
906 if (runtime->event)
907 tasklet_hi_schedule(&runtime->tasklet);
908 else if (snd_rawmidi_ready(substream))
909 wake_up(&runtime->sleep);
910 }
911 spin_unlock_irqrestore(&runtime->lock, flags);
912 return result;
913}
914
Takashi Iwai48c9d412005-11-17 13:56:51 +0100915static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 unsigned char *buf, long count, int kernel)
917{
918 unsigned long flags;
919 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100920 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 while (count > 0 && runtime->avail) {
923 count1 = runtime->buffer_size - runtime->appl_ptr;
924 if (count1 > count)
925 count1 = count;
926 spin_lock_irqsave(&runtime->lock, flags);
927 if (count1 > (int)runtime->avail)
928 count1 = runtime->avail;
929 if (kernel) {
930 memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
931 } else {
932 spin_unlock_irqrestore(&runtime->lock, flags);
933 if (copy_to_user((char __user *)buf + result,
934 runtime->buffer + runtime->appl_ptr, count1)) {
935 return result > 0 ? result : -EFAULT;
936 }
937 spin_lock_irqsave(&runtime->lock, flags);
938 }
939 runtime->appl_ptr += count1;
940 runtime->appl_ptr %= runtime->buffer_size;
941 runtime->avail -= count1;
942 spin_unlock_irqrestore(&runtime->lock, flags);
943 result += count1;
944 count -= count1;
945 }
946 return result;
947}
948
Takashi Iwai48c9d412005-11-17 13:56:51 +0100949long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
950 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 snd_rawmidi_input_trigger(substream, 1);
953 return snd_rawmidi_kernel_read1(substream, buf, count, 1);
954}
955
Takashi Iwai48c9d412005-11-17 13:56:51 +0100956static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
957 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 long result;
960 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100961 struct snd_rawmidi_file *rfile;
962 struct snd_rawmidi_substream *substream;
963 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 rfile = file->private_data;
966 substream = rfile->input;
967 if (substream == NULL)
968 return -EIO;
969 runtime = substream->runtime;
970 snd_rawmidi_input_trigger(substream, 1);
971 result = 0;
972 while (count > 0) {
973 spin_lock_irq(&runtime->lock);
974 while (!snd_rawmidi_ready(substream)) {
975 wait_queue_t wait;
976 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
977 spin_unlock_irq(&runtime->lock);
978 return result > 0 ? result : -EAGAIN;
979 }
980 init_waitqueue_entry(&wait, current);
981 add_wait_queue(&runtime->sleep, &wait);
982 set_current_state(TASK_INTERRUPTIBLE);
983 spin_unlock_irq(&runtime->lock);
984 schedule();
985 remove_wait_queue(&runtime->sleep, &wait);
986 if (signal_pending(current))
987 return result > 0 ? result : -ERESTARTSYS;
988 if (!runtime->avail)
989 return result > 0 ? result : -EIO;
990 spin_lock_irq(&runtime->lock);
991 }
992 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +0200993 count1 = snd_rawmidi_kernel_read1(substream,
994 (unsigned char __force *)buf,
995 count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (count1 < 0)
997 return result > 0 ? result : count1;
998 result += count1;
999 buf += count1;
1000 count -= count1;
1001 }
1002 return result;
1003}
1004
1005/**
1006 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1007 * @substream: the rawmidi substream
1008 *
1009 * Returns 1 if the internal output buffer is empty, 0 if not.
1010 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001011int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001013 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int result;
1015 unsigned long flags;
1016
1017 if (runtime->buffer == NULL) {
1018 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1019 return 1;
1020 }
1021 spin_lock_irqsave(&runtime->lock, flags);
1022 result = runtime->avail >= runtime->buffer_size;
1023 spin_unlock_irqrestore(&runtime->lock, flags);
1024 return result;
1025}
1026
1027/**
1028 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1029 * @substream: the rawmidi substream
1030 * @buffer: the buffer pointer
1031 * @count: data size to transfer
1032 *
1033 * Copies data from the internal output buffer to the given buffer.
1034 *
1035 * Call this in the interrupt handler when the midi output is ready,
1036 * and call snd_rawmidi_transmit_ack() after the transmission is
1037 * finished.
1038 *
1039 * Returns the size of copied data, or a negative error code on failure.
1040 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001041int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1042 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 unsigned long flags;
1045 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001046 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 if (runtime->buffer == NULL) {
1049 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1050 return -EINVAL;
1051 }
1052 result = 0;
1053 spin_lock_irqsave(&runtime->lock, flags);
1054 if (runtime->avail >= runtime->buffer_size) {
1055 /* warning: lowlevel layer MUST trigger down the hardware */
1056 goto __skip;
1057 }
1058 if (count == 1) { /* special case, faster code */
1059 *buffer = runtime->buffer[runtime->hw_ptr];
1060 result++;
1061 } else {
1062 count1 = runtime->buffer_size - runtime->hw_ptr;
1063 if (count1 > count)
1064 count1 = count;
1065 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1066 count1 = runtime->buffer_size - runtime->avail;
1067 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1068 count -= count1;
1069 result += count1;
1070 if (count > 0) {
1071 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1072 count = runtime->buffer_size - runtime->avail - count1;
1073 memcpy(buffer + count1, runtime->buffer, count);
1074 result += count;
1075 }
1076 }
1077 __skip:
1078 spin_unlock_irqrestore(&runtime->lock, flags);
1079 return result;
1080}
1081
1082/**
1083 * snd_rawmidi_transmit_ack - acknowledge the transmission
1084 * @substream: the rawmidi substream
1085 * @count: the tranferred count
1086 *
1087 * Advances the hardware pointer for the internal output buffer with
1088 * the given size and updates the condition.
1089 * Call after the transmission is finished.
1090 *
1091 * Returns the advanced size if successful, or a negative error code on failure.
1092 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001093int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001096 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 if (runtime->buffer == NULL) {
1099 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1100 return -EINVAL;
1101 }
1102 spin_lock_irqsave(&runtime->lock, flags);
1103 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1104 runtime->hw_ptr += count;
1105 runtime->hw_ptr %= runtime->buffer_size;
1106 runtime->avail += count;
1107 substream->bytes += count;
1108 if (count > 0) {
1109 if (runtime->drain || snd_rawmidi_ready(substream))
1110 wake_up(&runtime->sleep);
1111 }
1112 spin_unlock_irqrestore(&runtime->lock, flags);
1113 return count;
1114}
1115
1116/**
1117 * snd_rawmidi_transmit - copy from the buffer to the device
1118 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001119 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 * @count: the data size to transfer
1121 *
1122 * Copies data from the buffer to the device and advances the pointer.
1123 *
1124 * Returns the copied size if successful, or a negative error code on failure.
1125 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001126int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1127 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1130 if (count < 0)
1131 return count;
1132 return snd_rawmidi_transmit_ack(substream, count);
1133}
1134
Takashi Iwai48c9d412005-11-17 13:56:51 +01001135static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1136 const unsigned char *buf, long count, int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 unsigned long flags;
1139 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001140 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 snd_assert(buf != NULL, return -EINVAL);
1143 snd_assert(runtime->buffer != NULL, return -EINVAL);
1144
1145 result = 0;
1146 spin_lock_irqsave(&runtime->lock, flags);
1147 if (substream->append) {
1148 if ((long)runtime->avail < count) {
1149 spin_unlock_irqrestore(&runtime->lock, flags);
1150 return -EAGAIN;
1151 }
1152 }
1153 while (count > 0 && runtime->avail > 0) {
1154 count1 = runtime->buffer_size - runtime->appl_ptr;
1155 if (count1 > count)
1156 count1 = count;
1157 if (count1 > (long)runtime->avail)
1158 count1 = runtime->avail;
1159 if (kernel) {
1160 memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1161 } else {
1162 spin_unlock_irqrestore(&runtime->lock, flags);
1163 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1164 (char __user *)buf, count1)) {
1165 spin_lock_irqsave(&runtime->lock, flags);
1166 result = result > 0 ? result : -EFAULT;
1167 goto __end;
1168 }
1169 spin_lock_irqsave(&runtime->lock, flags);
1170 }
1171 runtime->appl_ptr += count1;
1172 runtime->appl_ptr %= runtime->buffer_size;
1173 runtime->avail -= count1;
1174 result += count1;
1175 buf += count1;
1176 count -= count1;
1177 }
1178 __end:
1179 count1 = runtime->avail < runtime->buffer_size;
1180 spin_unlock_irqrestore(&runtime->lock, flags);
1181 if (count1)
1182 snd_rawmidi_output_trigger(substream, 1);
1183 return result;
1184}
1185
Takashi Iwai48c9d412005-11-17 13:56:51 +01001186long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1187 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1190}
1191
Takashi Iwai48c9d412005-11-17 13:56:51 +01001192static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1193 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 long result, timeout;
1196 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001197 struct snd_rawmidi_file *rfile;
1198 struct snd_rawmidi_runtime *runtime;
1199 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 rfile = file->private_data;
1202 substream = rfile->output;
1203 runtime = substream->runtime;
1204 /* we cannot put an atomic message to our buffer */
1205 if (substream->append && count > runtime->buffer_size)
1206 return -EIO;
1207 result = 0;
1208 while (count > 0) {
1209 spin_lock_irq(&runtime->lock);
1210 while (!snd_rawmidi_ready_append(substream, count)) {
1211 wait_queue_t wait;
1212 if (file->f_flags & O_NONBLOCK) {
1213 spin_unlock_irq(&runtime->lock);
1214 return result > 0 ? result : -EAGAIN;
1215 }
1216 init_waitqueue_entry(&wait, current);
1217 add_wait_queue(&runtime->sleep, &wait);
1218 set_current_state(TASK_INTERRUPTIBLE);
1219 spin_unlock_irq(&runtime->lock);
1220 timeout = schedule_timeout(30 * HZ);
1221 remove_wait_queue(&runtime->sleep, &wait);
1222 if (signal_pending(current))
1223 return result > 0 ? result : -ERESTARTSYS;
1224 if (!runtime->avail && !timeout)
1225 return result > 0 ? result : -EIO;
1226 spin_lock_irq(&runtime->lock);
1227 }
1228 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001229 count1 = snd_rawmidi_kernel_write1(substream,
1230 (unsigned char __force *)buf,
1231 count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (count1 < 0)
1233 return result > 0 ? result : count1;
1234 result += count1;
1235 buf += count1;
1236 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1237 break;
1238 count -= count1;
1239 }
1240 if (file->f_flags & O_SYNC) {
1241 spin_lock_irq(&runtime->lock);
1242 while (runtime->avail != runtime->buffer_size) {
1243 wait_queue_t wait;
1244 unsigned int last_avail = runtime->avail;
1245 init_waitqueue_entry(&wait, current);
1246 add_wait_queue(&runtime->sleep, &wait);
1247 set_current_state(TASK_INTERRUPTIBLE);
1248 spin_unlock_irq(&runtime->lock);
1249 timeout = schedule_timeout(30 * HZ);
1250 remove_wait_queue(&runtime->sleep, &wait);
1251 if (signal_pending(current))
1252 return result > 0 ? result : -ERESTARTSYS;
1253 if (runtime->avail == last_avail && !timeout)
1254 return result > 0 ? result : -EIO;
1255 spin_lock_irq(&runtime->lock);
1256 }
1257 spin_unlock_irq(&runtime->lock);
1258 }
1259 return result;
1260}
1261
1262static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1263{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001264 struct snd_rawmidi_file *rfile;
1265 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 unsigned int mask;
1267
1268 rfile = file->private_data;
1269 if (rfile->input != NULL) {
1270 runtime = rfile->input->runtime;
1271 snd_rawmidi_input_trigger(rfile->input, 1);
1272 poll_wait(file, &runtime->sleep, wait);
1273 }
1274 if (rfile->output != NULL) {
1275 runtime = rfile->output->runtime;
1276 poll_wait(file, &runtime->sleep, wait);
1277 }
1278 mask = 0;
1279 if (rfile->input != NULL) {
1280 if (snd_rawmidi_ready(rfile->input))
1281 mask |= POLLIN | POLLRDNORM;
1282 }
1283 if (rfile->output != NULL) {
1284 if (snd_rawmidi_ready(rfile->output))
1285 mask |= POLLOUT | POLLWRNORM;
1286 }
1287 return mask;
1288}
1289
1290/*
1291 */
1292#ifdef CONFIG_COMPAT
1293#include "rawmidi_compat.c"
1294#else
1295#define snd_rawmidi_ioctl_compat NULL
1296#endif
1297
1298/*
1299
1300 */
1301
Takashi Iwai48c9d412005-11-17 13:56:51 +01001302static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1303 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001305 struct snd_rawmidi *rmidi;
1306 struct snd_rawmidi_substream *substream;
1307 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 rmidi = entry->private_data;
1310 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001311 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001313 list_for_each_entry(substream,
1314 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1315 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 snd_iprintf(buffer,
1317 "Output %d\n"
1318 " Tx bytes : %lu\n",
1319 substream->number,
1320 (unsigned long) substream->bytes);
1321 if (substream->opened) {
1322 runtime = substream->runtime;
1323 snd_iprintf(buffer,
1324 " Mode : %s\n"
1325 " Buffer size : %lu\n"
1326 " Avail : %lu\n",
1327 runtime->oss ? "OSS compatible" : "native",
1328 (unsigned long) runtime->buffer_size,
1329 (unsigned long) runtime->avail);
1330 }
1331 }
1332 }
1333 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001334 list_for_each_entry(substream,
1335 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1336 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 snd_iprintf(buffer,
1338 "Input %d\n"
1339 " Rx bytes : %lu\n",
1340 substream->number,
1341 (unsigned long) substream->bytes);
1342 if (substream->opened) {
1343 runtime = substream->runtime;
1344 snd_iprintf(buffer,
1345 " Buffer size : %lu\n"
1346 " Avail : %lu\n"
1347 " Overruns : %lu\n",
1348 (unsigned long) runtime->buffer_size,
1349 (unsigned long) runtime->avail,
1350 (unsigned long) runtime->xruns);
1351 }
1352 }
1353 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001354 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
1357/*
1358 * Register functions
1359 */
1360
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001361static const struct file_operations snd_rawmidi_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 .owner = THIS_MODULE,
1364 .read = snd_rawmidi_read,
1365 .write = snd_rawmidi_write,
1366 .open = snd_rawmidi_open,
1367 .release = snd_rawmidi_release,
1368 .poll = snd_rawmidi_poll,
1369 .unlocked_ioctl = snd_rawmidi_ioctl,
1370 .compat_ioctl = snd_rawmidi_ioctl_compat,
1371};
1372
Takashi Iwai48c9d412005-11-17 13:56:51 +01001373static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1374 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 int direction,
1376 int count)
1377{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001378 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 int idx;
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001382 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001383 if (substream == NULL) {
1384 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 substream->stream = direction;
1388 substream->number = idx;
1389 substream->rmidi = rmidi;
1390 substream->pstr = stream;
1391 list_add_tail(&substream->list, &stream->substreams);
1392 stream->substream_count++;
1393 }
1394 return 0;
1395}
1396
1397/**
1398 * snd_rawmidi_new - create a rawmidi instance
1399 * @card: the card instance
1400 * @id: the id string
1401 * @device: the device index
1402 * @output_count: the number of output streams
1403 * @input_count: the number of input streams
1404 * @rrawmidi: the pointer to store the new rawmidi instance
1405 *
1406 * Creates a new rawmidi instance.
1407 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1408 *
1409 * Returns zero if successful, or a negative error code on failure.
1410 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001411int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 int output_count, int input_count,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001413 struct snd_rawmidi ** rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001415 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001417 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 .dev_free = snd_rawmidi_dev_free,
1419 .dev_register = snd_rawmidi_dev_register,
1420 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 };
1422
1423 snd_assert(rrawmidi != NULL, return -EINVAL);
1424 *rrawmidi = NULL;
1425 snd_assert(card != NULL, return -ENXIO);
Takashi Iwaica2c0962005-09-09 14:20:23 +02001426 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001427 if (rmidi == NULL) {
1428 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 rmidi->card = card;
1432 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001433 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001435 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1436 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (id != NULL)
1439 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001440 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1441 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1442 SNDRV_RAWMIDI_STREAM_INPUT,
1443 input_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 snd_rawmidi_free(rmidi);
1445 return err;
1446 }
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001447 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1448 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1449 SNDRV_RAWMIDI_STREAM_OUTPUT,
1450 output_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 snd_rawmidi_free(rmidi);
1452 return err;
1453 }
1454 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1455 snd_rawmidi_free(rmidi);
1456 return err;
1457 }
1458 *rrawmidi = rmidi;
1459 return 0;
1460}
1461
Takashi Iwai48c9d412005-11-17 13:56:51 +01001462static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001464 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001467 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 list_del(&substream->list);
1469 kfree(substream);
1470 }
1471}
1472
Takashi Iwai48c9d412005-11-17 13:56:51 +01001473static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 snd_assert(rmidi != NULL, return -ENXIO);
Takashi Iwaic4614822006-06-23 14:38:23 +02001476
1477 snd_info_free_entry(rmidi->proc_entry);
1478 rmidi->proc_entry = NULL;
1479 mutex_lock(&register_mutex);
1480 if (rmidi->ops && rmidi->ops->dev_unregister)
1481 rmidi->ops->dev_unregister(rmidi);
1482 mutex_unlock(&register_mutex);
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1485 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1486 if (rmidi->private_free)
1487 rmidi->private_free(rmidi);
1488 kfree(rmidi);
1489 return 0;
1490}
1491
Takashi Iwai48c9d412005-11-17 13:56:51 +01001492static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001494 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return snd_rawmidi_free(rmidi);
1496}
1497
1498#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
Takashi Iwai48c9d412005-11-17 13:56:51 +01001499static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001501 struct snd_rawmidi *rmidi = device->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 rmidi->seq_dev = NULL;
1503}
1504#endif
1505
Takashi Iwai48c9d412005-11-17 13:56:51 +01001506static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001508 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001509 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001511 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1514 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001515 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001516 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001517 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return -EBUSY;
1519 }
Clemens Ladischf87135f2005-11-20 14:06:59 +01001520 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1522 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1523 rmidi->card, rmidi->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001524 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001526 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001527 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return err;
1529 }
1530 if (rmidi->ops && rmidi->ops->dev_register &&
1531 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1532 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001533 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001534 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return err;
1536 }
1537#ifdef CONFIG_SND_OSSEMUL
1538 rmidi->ossreg = 0;
1539 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1540 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001541 rmidi->card, 0, &snd_rawmidi_f_ops,
1542 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1544 } else {
1545 rmidi->ossreg++;
1546#ifdef SNDRV_OSS_INFO_DEV_MIDI
1547 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1548#endif
1549 }
1550 }
1551 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1552 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001553 rmidi->card, 1, &snd_rawmidi_f_ops,
1554 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1556 } else {
1557 rmidi->ossreg++;
1558 }
1559 }
1560#endif /* CONFIG_SND_OSSEMUL */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001561 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 sprintf(name, "midi%d", rmidi->device);
1563 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1564 if (entry) {
1565 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 entry->c.text.read = snd_rawmidi_proc_info_read;
1567 if (snd_info_register(entry) < 0) {
1568 snd_info_free_entry(entry);
1569 entry = NULL;
1570 }
1571 }
1572 rmidi->proc_entry = entry;
1573#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1574 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1575 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1576 rmidi->seq_dev->private_data = rmidi;
1577 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1578 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1579 snd_device_register(rmidi->card, rmidi->seq_dev);
1580 }
1581 }
1582#endif
1583 return 0;
1584}
1585
Takashi Iwai48c9d412005-11-17 13:56:51 +01001586static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001588 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001590 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001591 list_del_init(&rmidi->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592#ifdef CONFIG_SND_OSSEMUL
1593 if (rmidi->ossreg) {
1594 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1595 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1596#ifdef SNDRV_OSS_INFO_DEV_MIDI
1597 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1598#endif
1599 }
1600 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1601 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1602 rmidi->ossreg = 0;
1603 }
1604#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001606 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
1610/**
1611 * snd_rawmidi_set_ops - set the rawmidi operators
1612 * @rmidi: the rawmidi instance
1613 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1614 * @ops: the operator table
1615 *
1616 * Sets the rawmidi operators for the given stream direction.
1617 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001618void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1619 struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001621 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Johannes Berg9244b2c2006-10-05 16:02:22 +02001623 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627/*
1628 * ENTRY functions
1629 */
1630
1631static int __init alsa_rawmidi_init(void)
1632{
1633
1634 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1635 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1636#ifdef CONFIG_SND_OSSEMUL
1637 { int i;
1638 /* check device map table */
1639 for (i = 0; i < SNDRV_CARDS; i++) {
1640 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1641 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1642 midi_map[i] = 0;
1643 }
1644 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1645 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1646 amidi_map[i] = 1;
1647 }
1648 }
1649 }
1650#endif /* CONFIG_SND_OSSEMUL */
1651 return 0;
1652}
1653
1654static void __exit alsa_rawmidi_exit(void)
1655{
1656 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1657 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1658}
1659
1660module_init(alsa_rawmidi_init)
1661module_exit(alsa_rawmidi_exit)
1662
1663EXPORT_SYMBOL(snd_rawmidi_output_params);
1664EXPORT_SYMBOL(snd_rawmidi_input_params);
1665EXPORT_SYMBOL(snd_rawmidi_drop_output);
1666EXPORT_SYMBOL(snd_rawmidi_drain_output);
1667EXPORT_SYMBOL(snd_rawmidi_drain_input);
1668EXPORT_SYMBOL(snd_rawmidi_receive);
1669EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1670EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1671EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1672EXPORT_SYMBOL(snd_rawmidi_transmit);
1673EXPORT_SYMBOL(snd_rawmidi_new);
1674EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675EXPORT_SYMBOL(snd_rawmidi_info_select);
1676EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1677EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1678EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1679EXPORT_SYMBOL(snd_rawmidi_kernel_write);