blob: d14dcbb6dbca70a6789b0b56144cd1d99f85f361 [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>
26#include <linux/smp_lock.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/time.h>
30#include <linux/wait.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010031#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/moduleparam.h>
33#include <linux/delay.h>
34#include <linux/wait.h>
35#include <sound/rawmidi.h>
36#include <sound/info.h>
37#include <sound/control.h>
38#include <sound/minors.h>
39#include <sound/initval.h>
40
41MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
42MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
43MODULE_LICENSE("GPL");
44
45#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai6581f4e2006-05-17 17:14:51 +020046static int midi_map[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
48module_param_array(midi_map, int, NULL, 0444);
49MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
50module_param_array(amidi_map, int, NULL, 0444);
51MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
52#endif /* CONFIG_SND_OSSEMUL */
53
Takashi Iwai48c9d412005-11-17 13:56:51 +010054static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
55static int snd_rawmidi_dev_free(struct snd_device *device);
56static int snd_rawmidi_dev_register(struct snd_device *device);
57static int snd_rawmidi_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Clemens Ladischf87135f2005-11-20 14:06:59 +010059static LIST_HEAD(snd_rawmidi_devices);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010060static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Clemens Ladischf87135f2005-11-20 14:06:59 +010062static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
63{
Clemens Ladischf87135f2005-11-20 14:06:59 +010064 struct snd_rawmidi *rawmidi;
65
Johannes Berg9244b2c2006-10-05 16:02:22 +020066 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
Clemens Ladischf87135f2005-11-20 14:06:59 +010067 if (rawmidi->card == card && rawmidi->device == device)
68 return rawmidi;
Clemens Ladischf87135f2005-11-20 14:06:59 +010069 return NULL;
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static inline unsigned short snd_rawmidi_file_flags(struct file *file)
73{
74 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
75 case FMODE_WRITE:
76 return SNDRV_RAWMIDI_LFLG_OUTPUT;
77 case FMODE_READ:
78 return SNDRV_RAWMIDI_LFLG_INPUT;
79 default:
80 return SNDRV_RAWMIDI_LFLG_OPEN;
81 }
82}
83
Takashi Iwai48c9d412005-11-17 13:56:51 +010084static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Takashi Iwai48c9d412005-11-17 13:56:51 +010086 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return runtime->avail >= runtime->avail_min;
88}
89
Takashi Iwai48c9d412005-11-17 13:56:51 +010090static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
91 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Takashi Iwai48c9d412005-11-17 13:56:51 +010093 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return runtime->avail >= runtime->avail_min &&
95 (!substream->append || runtime->avail >= count);
96}
97
98static void snd_rawmidi_input_event_tasklet(unsigned long data)
99{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100100 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 substream->runtime->event(substream);
102}
103
104static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
105{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100106 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 substream->ops->trigger(substream, 1);
108}
109
Takashi Iwai48c9d412005-11-17 13:56:51 +0100110static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100112 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Takashi Iwaica2c0962005-09-09 14:20:23 +0200114 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return -ENOMEM;
116 spin_lock_init(&runtime->lock);
117 init_waitqueue_head(&runtime->sleep);
118 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
119 tasklet_init(&runtime->tasklet,
120 snd_rawmidi_input_event_tasklet,
121 (unsigned long)substream);
122 else
123 tasklet_init(&runtime->tasklet,
124 snd_rawmidi_output_trigger_tasklet,
125 (unsigned long)substream);
126 runtime->event = NULL;
127 runtime->buffer_size = PAGE_SIZE;
128 runtime->avail_min = 1;
129 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
130 runtime->avail = 0;
131 else
132 runtime->avail = runtime->buffer_size;
133 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
134 kfree(runtime);
135 return -ENOMEM;
136 }
137 runtime->appl_ptr = runtime->hw_ptr = 0;
138 substream->runtime = runtime;
139 return 0;
140}
141
Takashi Iwai48c9d412005-11-17 13:56:51 +0100142static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100144 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 kfree(runtime->buffer);
147 kfree(runtime);
148 substream->runtime = NULL;
149 return 0;
150}
151
Takashi Iwai48c9d412005-11-17 13:56:51 +0100152static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 if (up) {
155 tasklet_hi_schedule(&substream->runtime->tasklet);
156 } else {
157 tasklet_kill(&substream->runtime->tasklet);
158 substream->ops->trigger(substream, 0);
159 }
160}
161
Takashi Iwai48c9d412005-11-17 13:56:51 +0100162static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 substream->ops->trigger(substream, up);
165 if (!up && substream->runtime->event)
166 tasklet_kill(&substream->runtime->tasklet);
167}
168
Takashi Iwai48c9d412005-11-17 13:56:51 +0100169int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100172 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 snd_rawmidi_output_trigger(substream, 0);
175 runtime->drain = 0;
176 spin_lock_irqsave(&runtime->lock, flags);
177 runtime->appl_ptr = runtime->hw_ptr = 0;
178 runtime->avail = runtime->buffer_size;
179 spin_unlock_irqrestore(&runtime->lock, flags);
180 return 0;
181}
182
Takashi Iwai48c9d412005-11-17 13:56:51 +0100183int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int err;
186 long timeout;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100187 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 err = 0;
190 runtime->drain = 1;
191 timeout = wait_event_interruptible_timeout(runtime->sleep,
192 (runtime->avail >= runtime->buffer_size),
193 10*HZ);
194 if (signal_pending(current))
195 err = -ERESTARTSYS;
196 if (runtime->avail < runtime->buffer_size && !timeout) {
197 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
198 err = -EIO;
199 }
200 runtime->drain = 0;
201 if (err != -ERESTARTSYS) {
202 /* we need wait a while to make sure that Tx FIFOs are empty */
203 if (substream->ops->drain)
204 substream->ops->drain(substream);
205 else
206 msleep(50);
207 snd_rawmidi_drop_output(substream);
208 }
209 return err;
210}
211
Takashi Iwai48c9d412005-11-17 13:56:51 +0100212int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100215 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 snd_rawmidi_input_trigger(substream, 0);
218 runtime->drain = 0;
219 spin_lock_irqsave(&runtime->lock, flags);
220 runtime->appl_ptr = runtime->hw_ptr = 0;
221 runtime->avail = 0;
222 spin_unlock_irqrestore(&runtime->lock, flags);
223 return 0;
224}
225
Clemens Ladischf87135f2005-11-20 14:06:59 +0100226int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai48c9d412005-11-17 13:56:51 +0100227 int mode, struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100229 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct list_head *list1, *list2;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100231 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
232 struct snd_rawmidi_runtime *input = NULL, *output = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int err;
234
235 if (rfile)
236 rfile->input = rfile->output = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100237 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100238 rmidi = snd_rawmidi_search(card, device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100239 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (rmidi == NULL) {
241 err = -ENODEV;
242 goto __error1;
243 }
244 if (!try_module_get(rmidi->card->module)) {
245 err = -EFAULT;
246 goto __error1;
247 }
248 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100249 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
251 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
252 err = -ENXIO;
253 goto __error;
254 }
255 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
256 err = -ENODEV;
257 goto __error;
258 }
259 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
260 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
261 err = -EAGAIN;
262 goto __error;
263 }
264 }
265 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
266 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
267 err = -ENXIO;
268 goto __error;
269 }
270 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
271 err = -ENODEV;
272 goto __error;
273 }
274 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
275 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
276 err = -EAGAIN;
277 goto __error;
278 }
279 }
280 list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
281 while (1) {
282 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
283 sinput = NULL;
284 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
285 err = -EAGAIN;
286 goto __error;
287 }
288 break;
289 }
Takashi Iwai48c9d412005-11-17 13:56:51 +0100290 sinput = list_entry(list1, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
292 goto __nexti;
293 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
294 break;
295 __nexti:
296 list1 = list1->next;
297 }
298 list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
299 while (1) {
300 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
301 soutput = NULL;
302 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
303 err = -EAGAIN;
304 goto __error;
305 }
306 break;
307 }
Takashi Iwai48c9d412005-11-17 13:56:51 +0100308 soutput = list_entry(list2, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
310 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
311 if (soutput->opened && !soutput->append)
312 goto __nexto;
313 } else {
314 if (soutput->opened)
315 goto __nexto;
316 }
317 }
318 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
319 break;
320 __nexto:
321 list2 = list2->next;
322 }
323 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
324 if ((err = snd_rawmidi_runtime_create(sinput)) < 0)
325 goto __error;
326 input = sinput->runtime;
327 if ((err = sinput->ops->open(sinput)) < 0)
328 goto __error;
329 sinput->opened = 1;
330 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
331 } else {
332 sinput = NULL;
333 }
334 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
335 if (soutput->opened)
336 goto __skip_output;
337 if ((err = snd_rawmidi_runtime_create(soutput)) < 0) {
338 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
339 sinput->ops->close(sinput);
340 goto __error;
341 }
342 output = soutput->runtime;
343 if ((err = soutput->ops->open(soutput)) < 0) {
344 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
345 sinput->ops->close(sinput);
346 goto __error;
347 }
348 __skip_output:
349 soutput->opened = 1;
350 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
351 soutput->append = 1;
352 if (soutput->use_count++ == 0)
353 soutput->active_sensing = 1;
354 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
355 } else {
356 soutput = NULL;
357 }
358 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100359 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (rfile) {
361 rfile->rmidi = rmidi;
362 rfile->input = sinput;
363 rfile->output = soutput;
364 }
365 return 0;
366
367 __error:
368 if (input != NULL)
369 snd_rawmidi_runtime_free(sinput);
370 if (output != NULL)
371 snd_rawmidi_runtime_free(soutput);
372 module_put(rmidi->card->module);
373 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100374 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 __error1:
376 return err;
377}
378
379static int snd_rawmidi_open(struct inode *inode, struct file *file)
380{
381 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100382 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100383 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unsigned short fflags;
385 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100386 struct snd_rawmidi *rmidi;
387 struct snd_rawmidi_file *rawmidi_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 wait_queue_t wait;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100389 struct snd_ctl_file *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Clemens Ladischf1902862005-10-24 17:05:03 +0200391 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100392 rmidi = snd_lookup_minor_data(iminor(inode),
393 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200395 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100396 rmidi = snd_lookup_oss_minor_data(iminor(inode),
397 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200399 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (rmidi == NULL)
403 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
405 return -EINVAL; /* invalid combination */
406 card = rmidi->card;
407 err = snd_card_file_add(card, file);
408 if (err < 0)
409 return -ENODEV;
410 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200411 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
413 fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
414 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
415 if (rawmidi_file == NULL) {
416 snd_card_file_remove(card, file);
417 return -ENOMEM;
418 }
419 init_waitqueue_entry(&wait, current);
420 add_wait_queue(&rmidi->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100421 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 while (1) {
423 subdevice = -1;
424 down_read(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200425 list_for_each_entry(kctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (kctl->pid == current->pid) {
427 subdevice = kctl->prefer_rawmidi_subdevice;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200428 if (subdevice != -1)
429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431 }
432 up_read(&card->controls_rwsem);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100433 err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
434 subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (err >= 0)
436 break;
437 if (err == -EAGAIN) {
438 if (file->f_flags & O_NONBLOCK) {
439 err = -EBUSY;
440 break;
441 }
442 } else
443 break;
444 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100445 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100447 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (signal_pending(current)) {
449 err = -ERESTARTSYS;
450 break;
451 }
452 }
453#ifdef CONFIG_SND_OSSEMUL
454 if (rawmidi_file->input && rawmidi_file->input->runtime)
455 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
456 if (rawmidi_file->output && rawmidi_file->output->runtime)
457 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
458#endif
459 remove_wait_queue(&rmidi->open_wait, &wait);
460 if (err >= 0) {
461 file->private_data = rawmidi_file;
462 } else {
463 snd_card_file_remove(card, file);
464 kfree(rawmidi_file);
465 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100466 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return err;
468}
469
Takashi Iwai48c9d412005-11-17 13:56:51 +0100470int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100472 struct snd_rawmidi *rmidi;
473 struct snd_rawmidi_substream *substream;
474 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 snd_assert(rfile != NULL, return -ENXIO);
477 snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
478 rmidi = rfile->rmidi;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100479 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (rfile->input != NULL) {
481 substream = rfile->input;
482 rfile->input = NULL;
483 runtime = substream->runtime;
484 snd_rawmidi_input_trigger(substream, 0);
485 substream->ops->close(substream);
486 if (runtime->private_free != NULL)
487 runtime->private_free(substream);
488 snd_rawmidi_runtime_free(substream);
489 substream->opened = 0;
490 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
491 }
492 if (rfile->output != NULL) {
493 substream = rfile->output;
494 rfile->output = NULL;
495 if (--substream->use_count == 0) {
496 runtime = substream->runtime;
497 if (substream->active_sensing) {
498 unsigned char buf = 0xfe;
499 /* sending single active sensing message to shut the device up */
500 snd_rawmidi_kernel_write(substream, &buf, 1);
501 }
502 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
503 snd_rawmidi_output_trigger(substream, 0);
504 substream->ops->close(substream);
505 if (runtime->private_free != NULL)
506 runtime->private_free(substream);
507 snd_rawmidi_runtime_free(substream);
508 substream->opened = 0;
509 substream->append = 0;
510 }
511 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
512 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100513 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 module_put(rmidi->card->module);
515 return 0;
516}
517
518static int snd_rawmidi_release(struct inode *inode, struct file *file)
519{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100520 struct snd_rawmidi_file *rfile;
521 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int err;
523
524 rfile = file->private_data;
525 err = snd_rawmidi_kernel_release(rfile);
526 rmidi = rfile->rmidi;
527 wake_up(&rmidi->open_wait);
528 kfree(rfile);
529 snd_card_file_remove(rmidi->card, file);
530 return err;
531}
532
Adrian Bunk18612042005-11-23 13:14:50 +0100533static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
534 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100536 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (substream == NULL)
539 return -ENODEV;
540 rmidi = substream->rmidi;
541 memset(info, 0, sizeof(*info));
542 info->card = rmidi->card->number;
543 info->device = rmidi->device;
544 info->subdevice = substream->number;
545 info->stream = substream->stream;
546 info->flags = rmidi->info_flags;
547 strcpy(info->id, rmidi->id);
548 strcpy(info->name, rmidi->name);
549 strcpy(info->subname, substream->name);
550 info->subdevices_count = substream->pstr->substream_count;
551 info->subdevices_avail = (substream->pstr->substream_count -
552 substream->pstr->substream_opened);
553 return 0;
554}
555
Takashi Iwai48c9d412005-11-17 13:56:51 +0100556static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
557 struct snd_rawmidi_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100559 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int err;
561 if ((err = snd_rawmidi_info(substream, &info)) < 0)
562 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100563 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return -EFAULT;
565 return 0;
566}
567
Takashi Iwai48c9d412005-11-17 13:56:51 +0100568int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100570 struct snd_rawmidi *rmidi;
571 struct snd_rawmidi_str *pstr;
572 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100573
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100574 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100575 rmidi = snd_rawmidi_search(card, info->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100576 mutex_unlock(&register_mutex);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100577 if (!rmidi)
578 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (info->stream < 0 || info->stream > 1)
580 return -EINVAL;
581 pstr = &rmidi->streams[info->stream];
582 if (pstr->substream_count == 0)
583 return -ENOENT;
584 if (info->subdevice >= pstr->substream_count)
585 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200586 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if ((unsigned int)substream->number == info->subdevice)
588 return snd_rawmidi_info(substream, info);
589 }
590 return -ENXIO;
591}
592
Takashi Iwai48c9d412005-11-17 13:56:51 +0100593static int snd_rawmidi_info_select_user(struct snd_card *card,
594 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100597 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (get_user(info.device, &_info->device))
599 return -EFAULT;
600 if (get_user(info.stream, &_info->stream))
601 return -EFAULT;
602 if (get_user(info.subdevice, &_info->subdevice))
603 return -EFAULT;
604 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
605 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100606 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return -EFAULT;
608 return 0;
609}
610
Takashi Iwai48c9d412005-11-17 13:56:51 +0100611int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
612 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100615 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (substream->append && substream->use_count > 1)
618 return -EBUSY;
619 snd_rawmidi_drain_output(substream);
620 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
621 return -EINVAL;
622 }
623 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
624 return -EINVAL;
625 }
626 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800627 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
628 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -ENOMEM;
630 kfree(runtime->buffer);
631 runtime->buffer = newbuf;
632 runtime->buffer_size = params->buffer_size;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100633 runtime->avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635 runtime->avail_min = params->avail_min;
636 substream->active_sensing = !params->no_active_sensing;
637 return 0;
638}
639
Takashi Iwai48c9d412005-11-17 13:56:51 +0100640int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
641 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100644 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 snd_rawmidi_drain_input(substream);
647 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
648 return -EINVAL;
649 }
650 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
651 return -EINVAL;
652 }
653 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800654 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
655 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return -ENOMEM;
657 kfree(runtime->buffer);
658 runtime->buffer = newbuf;
659 runtime->buffer_size = params->buffer_size;
660 }
661 runtime->avail_min = params->avail_min;
662 return 0;
663}
664
Takashi Iwai48c9d412005-11-17 13:56:51 +0100665static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
666 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100668 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 memset(status, 0, sizeof(*status));
671 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
672 spin_lock_irq(&runtime->lock);
673 status->avail = runtime->avail;
674 spin_unlock_irq(&runtime->lock);
675 return 0;
676}
677
Takashi Iwai48c9d412005-11-17 13:56:51 +0100678static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
679 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100681 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 memset(status, 0, sizeof(*status));
684 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
685 spin_lock_irq(&runtime->lock);
686 status->avail = runtime->avail;
687 status->xruns = runtime->xruns;
688 runtime->xruns = 0;
689 spin_unlock_irq(&runtime->lock);
690 return 0;
691}
692
693static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
694{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100695 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 void __user *argp = (void __user *)arg;
697
698 rfile = file->private_data;
699 if (((cmd >> 8) & 0xff) != 'W')
700 return -ENOTTY;
701 switch (cmd) {
702 case SNDRV_RAWMIDI_IOCTL_PVERSION:
703 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
704 case SNDRV_RAWMIDI_IOCTL_INFO:
705 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100706 int stream;
707 struct snd_rawmidi_info __user *info = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (get_user(stream, &info->stream))
709 return -EFAULT;
710 switch (stream) {
711 case SNDRV_RAWMIDI_STREAM_INPUT:
712 return snd_rawmidi_info_user(rfile->input, info);
713 case SNDRV_RAWMIDI_STREAM_OUTPUT:
714 return snd_rawmidi_info_user(rfile->output, info);
715 default:
716 return -EINVAL;
717 }
718 }
719 case SNDRV_RAWMIDI_IOCTL_PARAMS:
720 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100721 struct snd_rawmidi_params params;
722 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return -EFAULT;
724 switch (params.stream) {
725 case SNDRV_RAWMIDI_STREAM_OUTPUT:
726 if (rfile->output == NULL)
727 return -EINVAL;
728 return snd_rawmidi_output_params(rfile->output, &params);
729 case SNDRV_RAWMIDI_STREAM_INPUT:
730 if (rfile->input == NULL)
731 return -EINVAL;
732 return snd_rawmidi_input_params(rfile->input, &params);
733 default:
734 return -EINVAL;
735 }
736 }
737 case SNDRV_RAWMIDI_IOCTL_STATUS:
738 {
739 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100740 struct snd_rawmidi_status status;
741 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return -EFAULT;
743 switch (status.stream) {
744 case SNDRV_RAWMIDI_STREAM_OUTPUT:
745 if (rfile->output == NULL)
746 return -EINVAL;
747 err = snd_rawmidi_output_status(rfile->output, &status);
748 break;
749 case SNDRV_RAWMIDI_STREAM_INPUT:
750 if (rfile->input == NULL)
751 return -EINVAL;
752 err = snd_rawmidi_input_status(rfile->input, &status);
753 break;
754 default:
755 return -EINVAL;
756 }
757 if (err < 0)
758 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100759 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -EFAULT;
761 return 0;
762 }
763 case SNDRV_RAWMIDI_IOCTL_DROP:
764 {
765 int val;
766 if (get_user(val, (int __user *) argp))
767 return -EFAULT;
768 switch (val) {
769 case SNDRV_RAWMIDI_STREAM_OUTPUT:
770 if (rfile->output == NULL)
771 return -EINVAL;
772 return snd_rawmidi_drop_output(rfile->output);
773 default:
774 return -EINVAL;
775 }
776 }
777 case SNDRV_RAWMIDI_IOCTL_DRAIN:
778 {
779 int val;
780 if (get_user(val, (int __user *) argp))
781 return -EFAULT;
782 switch (val) {
783 case SNDRV_RAWMIDI_STREAM_OUTPUT:
784 if (rfile->output == NULL)
785 return -EINVAL;
786 return snd_rawmidi_drain_output(rfile->output);
787 case SNDRV_RAWMIDI_STREAM_INPUT:
788 if (rfile->input == NULL)
789 return -EINVAL;
790 return snd_rawmidi_drain_input(rfile->input);
791 default:
792 return -EINVAL;
793 }
794 }
795#ifdef CONFIG_SND_DEBUG
796 default:
797 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
798#endif
799 }
800 return -ENOTTY;
801}
802
Takashi Iwai48c9d412005-11-17 13:56:51 +0100803static int snd_rawmidi_control_ioctl(struct snd_card *card,
804 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 unsigned int cmd,
806 unsigned long arg)
807{
808 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 switch (cmd) {
811 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
812 {
813 int device;
814
815 if (get_user(device, (int __user *)argp))
816 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100817 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 device = device < 0 ? 0 : device + 1;
819 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100820 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 break;
822 device++;
823 }
824 if (device == SNDRV_RAWMIDI_DEVICES)
825 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100826 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (put_user(device, (int __user *)argp))
828 return -EFAULT;
829 return 0;
830 }
831 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
832 {
833 int val;
834
835 if (get_user(val, (int __user *)argp))
836 return -EFAULT;
837 control->prefer_rawmidi_subdevice = val;
838 return 0;
839 }
840 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
841 return snd_rawmidi_info_select_user(card, argp);
842 }
843 return -ENOIOCTLCMD;
844}
845
846/**
847 * snd_rawmidi_receive - receive the input data from the device
848 * @substream: the rawmidi substream
849 * @buffer: the buffer pointer
850 * @count: the data size to read
851 *
852 * Reads the data from the internal buffer.
853 *
854 * Returns the size of read data, or a negative error code on failure.
855 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100856int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
857 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 unsigned long flags;
860 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100861 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 if (runtime->buffer == NULL) {
864 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
865 return -EINVAL;
866 }
867 spin_lock_irqsave(&runtime->lock, flags);
868 if (count == 1) { /* special case, faster code */
869 substream->bytes++;
870 if (runtime->avail < runtime->buffer_size) {
871 runtime->buffer[runtime->hw_ptr++] = buffer[0];
872 runtime->hw_ptr %= runtime->buffer_size;
873 runtime->avail++;
874 result++;
875 } else {
876 runtime->xruns++;
877 }
878 } else {
879 substream->bytes += count;
880 count1 = runtime->buffer_size - runtime->hw_ptr;
881 if (count1 > count)
882 count1 = count;
883 if (count1 > (int)(runtime->buffer_size - runtime->avail))
884 count1 = runtime->buffer_size - runtime->avail;
885 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
886 runtime->hw_ptr += count1;
887 runtime->hw_ptr %= runtime->buffer_size;
888 runtime->avail += count1;
889 count -= count1;
890 result += count1;
891 if (count > 0) {
892 buffer += count1;
893 count1 = count;
894 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
895 count1 = runtime->buffer_size - runtime->avail;
896 runtime->xruns += count - count1;
897 }
898 if (count1 > 0) {
899 memcpy(runtime->buffer, buffer, count1);
900 runtime->hw_ptr = count1;
901 runtime->avail += count1;
902 result += count1;
903 }
904 }
905 }
906 if (result > 0) {
907 if (runtime->event)
908 tasklet_hi_schedule(&runtime->tasklet);
909 else if (snd_rawmidi_ready(substream))
910 wake_up(&runtime->sleep);
911 }
912 spin_unlock_irqrestore(&runtime->lock, flags);
913 return result;
914}
915
Takashi Iwai48c9d412005-11-17 13:56:51 +0100916static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 unsigned char *buf, long count, int kernel)
918{
919 unsigned long flags;
920 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100921 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 while (count > 0 && runtime->avail) {
924 count1 = runtime->buffer_size - runtime->appl_ptr;
925 if (count1 > count)
926 count1 = count;
927 spin_lock_irqsave(&runtime->lock, flags);
928 if (count1 > (int)runtime->avail)
929 count1 = runtime->avail;
930 if (kernel) {
931 memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
932 } else {
933 spin_unlock_irqrestore(&runtime->lock, flags);
934 if (copy_to_user((char __user *)buf + result,
935 runtime->buffer + runtime->appl_ptr, count1)) {
936 return result > 0 ? result : -EFAULT;
937 }
938 spin_lock_irqsave(&runtime->lock, flags);
939 }
940 runtime->appl_ptr += count1;
941 runtime->appl_ptr %= runtime->buffer_size;
942 runtime->avail -= count1;
943 spin_unlock_irqrestore(&runtime->lock, flags);
944 result += count1;
945 count -= count1;
946 }
947 return result;
948}
949
Takashi Iwai48c9d412005-11-17 13:56:51 +0100950long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
951 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 snd_rawmidi_input_trigger(substream, 1);
954 return snd_rawmidi_kernel_read1(substream, buf, count, 1);
955}
956
Takashi Iwai48c9d412005-11-17 13:56:51 +0100957static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
958 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 long result;
961 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100962 struct snd_rawmidi_file *rfile;
963 struct snd_rawmidi_substream *substream;
964 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 rfile = file->private_data;
967 substream = rfile->input;
968 if (substream == NULL)
969 return -EIO;
970 runtime = substream->runtime;
971 snd_rawmidi_input_trigger(substream, 1);
972 result = 0;
973 while (count > 0) {
974 spin_lock_irq(&runtime->lock);
975 while (!snd_rawmidi_ready(substream)) {
976 wait_queue_t wait;
977 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
978 spin_unlock_irq(&runtime->lock);
979 return result > 0 ? result : -EAGAIN;
980 }
981 init_waitqueue_entry(&wait, current);
982 add_wait_queue(&runtime->sleep, &wait);
983 set_current_state(TASK_INTERRUPTIBLE);
984 spin_unlock_irq(&runtime->lock);
985 schedule();
986 remove_wait_queue(&runtime->sleep, &wait);
987 if (signal_pending(current))
988 return result > 0 ? result : -ERESTARTSYS;
989 if (!runtime->avail)
990 return result > 0 ? result : -EIO;
991 spin_lock_irq(&runtime->lock);
992 }
993 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +0200994 count1 = snd_rawmidi_kernel_read1(substream,
995 (unsigned char __force *)buf,
996 count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (count1 < 0)
998 return result > 0 ? result : count1;
999 result += count1;
1000 buf += count1;
1001 count -= count1;
1002 }
1003 return result;
1004}
1005
1006/**
1007 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1008 * @substream: the rawmidi substream
1009 *
1010 * Returns 1 if the internal output buffer is empty, 0 if not.
1011 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001012int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001014 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int result;
1016 unsigned long flags;
1017
1018 if (runtime->buffer == NULL) {
1019 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1020 return 1;
1021 }
1022 spin_lock_irqsave(&runtime->lock, flags);
1023 result = runtime->avail >= runtime->buffer_size;
1024 spin_unlock_irqrestore(&runtime->lock, flags);
1025 return result;
1026}
1027
1028/**
1029 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1030 * @substream: the rawmidi substream
1031 * @buffer: the buffer pointer
1032 * @count: data size to transfer
1033 *
1034 * Copies data from the internal output buffer to the given buffer.
1035 *
1036 * Call this in the interrupt handler when the midi output is ready,
1037 * and call snd_rawmidi_transmit_ack() after the transmission is
1038 * finished.
1039 *
1040 * Returns the size of copied data, or a negative error code on failure.
1041 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001042int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1043 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 unsigned long flags;
1046 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001047 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 if (runtime->buffer == NULL) {
1050 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1051 return -EINVAL;
1052 }
1053 result = 0;
1054 spin_lock_irqsave(&runtime->lock, flags);
1055 if (runtime->avail >= runtime->buffer_size) {
1056 /* warning: lowlevel layer MUST trigger down the hardware */
1057 goto __skip;
1058 }
1059 if (count == 1) { /* special case, faster code */
1060 *buffer = runtime->buffer[runtime->hw_ptr];
1061 result++;
1062 } else {
1063 count1 = runtime->buffer_size - runtime->hw_ptr;
1064 if (count1 > count)
1065 count1 = count;
1066 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1067 count1 = runtime->buffer_size - runtime->avail;
1068 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1069 count -= count1;
1070 result += count1;
1071 if (count > 0) {
1072 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1073 count = runtime->buffer_size - runtime->avail - count1;
1074 memcpy(buffer + count1, runtime->buffer, count);
1075 result += count;
1076 }
1077 }
1078 __skip:
1079 spin_unlock_irqrestore(&runtime->lock, flags);
1080 return result;
1081}
1082
1083/**
1084 * snd_rawmidi_transmit_ack - acknowledge the transmission
1085 * @substream: the rawmidi substream
1086 * @count: the tranferred count
1087 *
1088 * Advances the hardware pointer for the internal output buffer with
1089 * the given size and updates the condition.
1090 * Call after the transmission is finished.
1091 *
1092 * Returns the advanced size if successful, or a negative error code on failure.
1093 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001094int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001097 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 if (runtime->buffer == NULL) {
1100 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1101 return -EINVAL;
1102 }
1103 spin_lock_irqsave(&runtime->lock, flags);
1104 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1105 runtime->hw_ptr += count;
1106 runtime->hw_ptr %= runtime->buffer_size;
1107 runtime->avail += count;
1108 substream->bytes += count;
1109 if (count > 0) {
1110 if (runtime->drain || snd_rawmidi_ready(substream))
1111 wake_up(&runtime->sleep);
1112 }
1113 spin_unlock_irqrestore(&runtime->lock, flags);
1114 return count;
1115}
1116
1117/**
1118 * snd_rawmidi_transmit - copy from the buffer to the device
1119 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001120 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 * @count: the data size to transfer
1122 *
1123 * Copies data from the buffer to the device and advances the pointer.
1124 *
1125 * Returns the copied size if successful, or a negative error code on failure.
1126 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001127int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1128 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1131 if (count < 0)
1132 return count;
1133 return snd_rawmidi_transmit_ack(substream, count);
1134}
1135
Takashi Iwai48c9d412005-11-17 13:56:51 +01001136static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1137 const unsigned char *buf, long count, int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 unsigned long flags;
1140 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001141 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 snd_assert(buf != NULL, return -EINVAL);
1144 snd_assert(runtime->buffer != NULL, return -EINVAL);
1145
1146 result = 0;
1147 spin_lock_irqsave(&runtime->lock, flags);
1148 if (substream->append) {
1149 if ((long)runtime->avail < count) {
1150 spin_unlock_irqrestore(&runtime->lock, flags);
1151 return -EAGAIN;
1152 }
1153 }
1154 while (count > 0 && runtime->avail > 0) {
1155 count1 = runtime->buffer_size - runtime->appl_ptr;
1156 if (count1 > count)
1157 count1 = count;
1158 if (count1 > (long)runtime->avail)
1159 count1 = runtime->avail;
1160 if (kernel) {
1161 memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1162 } else {
1163 spin_unlock_irqrestore(&runtime->lock, flags);
1164 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1165 (char __user *)buf, count1)) {
1166 spin_lock_irqsave(&runtime->lock, flags);
1167 result = result > 0 ? result : -EFAULT;
1168 goto __end;
1169 }
1170 spin_lock_irqsave(&runtime->lock, flags);
1171 }
1172 runtime->appl_ptr += count1;
1173 runtime->appl_ptr %= runtime->buffer_size;
1174 runtime->avail -= count1;
1175 result += count1;
1176 buf += count1;
1177 count -= count1;
1178 }
1179 __end:
1180 count1 = runtime->avail < runtime->buffer_size;
1181 spin_unlock_irqrestore(&runtime->lock, flags);
1182 if (count1)
1183 snd_rawmidi_output_trigger(substream, 1);
1184 return result;
1185}
1186
Takashi Iwai48c9d412005-11-17 13:56:51 +01001187long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1188 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1191}
1192
Takashi Iwai48c9d412005-11-17 13:56:51 +01001193static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1194 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 long result, timeout;
1197 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001198 struct snd_rawmidi_file *rfile;
1199 struct snd_rawmidi_runtime *runtime;
1200 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 rfile = file->private_data;
1203 substream = rfile->output;
1204 runtime = substream->runtime;
1205 /* we cannot put an atomic message to our buffer */
1206 if (substream->append && count > runtime->buffer_size)
1207 return -EIO;
1208 result = 0;
1209 while (count > 0) {
1210 spin_lock_irq(&runtime->lock);
1211 while (!snd_rawmidi_ready_append(substream, count)) {
1212 wait_queue_t wait;
1213 if (file->f_flags & O_NONBLOCK) {
1214 spin_unlock_irq(&runtime->lock);
1215 return result > 0 ? result : -EAGAIN;
1216 }
1217 init_waitqueue_entry(&wait, current);
1218 add_wait_queue(&runtime->sleep, &wait);
1219 set_current_state(TASK_INTERRUPTIBLE);
1220 spin_unlock_irq(&runtime->lock);
1221 timeout = schedule_timeout(30 * HZ);
1222 remove_wait_queue(&runtime->sleep, &wait);
1223 if (signal_pending(current))
1224 return result > 0 ? result : -ERESTARTSYS;
1225 if (!runtime->avail && !timeout)
1226 return result > 0 ? result : -EIO;
1227 spin_lock_irq(&runtime->lock);
1228 }
1229 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001230 count1 = snd_rawmidi_kernel_write1(substream,
1231 (unsigned char __force *)buf,
1232 count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (count1 < 0)
1234 return result > 0 ? result : count1;
1235 result += count1;
1236 buf += count1;
1237 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1238 break;
1239 count -= count1;
1240 }
1241 if (file->f_flags & O_SYNC) {
1242 spin_lock_irq(&runtime->lock);
1243 while (runtime->avail != runtime->buffer_size) {
1244 wait_queue_t wait;
1245 unsigned int last_avail = runtime->avail;
1246 init_waitqueue_entry(&wait, current);
1247 add_wait_queue(&runtime->sleep, &wait);
1248 set_current_state(TASK_INTERRUPTIBLE);
1249 spin_unlock_irq(&runtime->lock);
1250 timeout = schedule_timeout(30 * HZ);
1251 remove_wait_queue(&runtime->sleep, &wait);
1252 if (signal_pending(current))
1253 return result > 0 ? result : -ERESTARTSYS;
1254 if (runtime->avail == last_avail && !timeout)
1255 return result > 0 ? result : -EIO;
1256 spin_lock_irq(&runtime->lock);
1257 }
1258 spin_unlock_irq(&runtime->lock);
1259 }
1260 return result;
1261}
1262
1263static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1264{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001265 struct snd_rawmidi_file *rfile;
1266 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 unsigned int mask;
1268
1269 rfile = file->private_data;
1270 if (rfile->input != NULL) {
1271 runtime = rfile->input->runtime;
1272 snd_rawmidi_input_trigger(rfile->input, 1);
1273 poll_wait(file, &runtime->sleep, wait);
1274 }
1275 if (rfile->output != NULL) {
1276 runtime = rfile->output->runtime;
1277 poll_wait(file, &runtime->sleep, wait);
1278 }
1279 mask = 0;
1280 if (rfile->input != NULL) {
1281 if (snd_rawmidi_ready(rfile->input))
1282 mask |= POLLIN | POLLRDNORM;
1283 }
1284 if (rfile->output != NULL) {
1285 if (snd_rawmidi_ready(rfile->output))
1286 mask |= POLLOUT | POLLWRNORM;
1287 }
1288 return mask;
1289}
1290
1291/*
1292 */
1293#ifdef CONFIG_COMPAT
1294#include "rawmidi_compat.c"
1295#else
1296#define snd_rawmidi_ioctl_compat NULL
1297#endif
1298
1299/*
1300
1301 */
1302
Takashi Iwai48c9d412005-11-17 13:56:51 +01001303static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1304 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001306 struct snd_rawmidi *rmidi;
1307 struct snd_rawmidi_substream *substream;
1308 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 rmidi = entry->private_data;
1311 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001312 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001314 list_for_each_entry(substream,
1315 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1316 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 snd_iprintf(buffer,
1318 "Output %d\n"
1319 " Tx bytes : %lu\n",
1320 substream->number,
1321 (unsigned long) substream->bytes);
1322 if (substream->opened) {
1323 runtime = substream->runtime;
1324 snd_iprintf(buffer,
1325 " Mode : %s\n"
1326 " Buffer size : %lu\n"
1327 " Avail : %lu\n",
1328 runtime->oss ? "OSS compatible" : "native",
1329 (unsigned long) runtime->buffer_size,
1330 (unsigned long) runtime->avail);
1331 }
1332 }
1333 }
1334 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001335 list_for_each_entry(substream,
1336 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1337 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 snd_iprintf(buffer,
1339 "Input %d\n"
1340 " Rx bytes : %lu\n",
1341 substream->number,
1342 (unsigned long) substream->bytes);
1343 if (substream->opened) {
1344 runtime = substream->runtime;
1345 snd_iprintf(buffer,
1346 " Buffer size : %lu\n"
1347 " Avail : %lu\n"
1348 " Overruns : %lu\n",
1349 (unsigned long) runtime->buffer_size,
1350 (unsigned long) runtime->avail,
1351 (unsigned long) runtime->xruns);
1352 }
1353 }
1354 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001355 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
1358/*
1359 * Register functions
1360 */
1361
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001362static const struct file_operations snd_rawmidi_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 .owner = THIS_MODULE,
1365 .read = snd_rawmidi_read,
1366 .write = snd_rawmidi_write,
1367 .open = snd_rawmidi_open,
1368 .release = snd_rawmidi_release,
1369 .poll = snd_rawmidi_poll,
1370 .unlocked_ioctl = snd_rawmidi_ioctl,
1371 .compat_ioctl = snd_rawmidi_ioctl_compat,
1372};
1373
Takashi Iwai48c9d412005-11-17 13:56:51 +01001374static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1375 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 int direction,
1377 int count)
1378{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001379 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 int idx;
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001383 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001384 if (substream == NULL) {
1385 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 substream->stream = direction;
1389 substream->number = idx;
1390 substream->rmidi = rmidi;
1391 substream->pstr = stream;
1392 list_add_tail(&substream->list, &stream->substreams);
1393 stream->substream_count++;
1394 }
1395 return 0;
1396}
1397
1398/**
1399 * snd_rawmidi_new - create a rawmidi instance
1400 * @card: the card instance
1401 * @id: the id string
1402 * @device: the device index
1403 * @output_count: the number of output streams
1404 * @input_count: the number of input streams
1405 * @rrawmidi: the pointer to store the new rawmidi instance
1406 *
1407 * Creates a new rawmidi instance.
1408 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1409 *
1410 * Returns zero if successful, or a negative error code on failure.
1411 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001412int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 int output_count, int input_count,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001414 struct snd_rawmidi ** rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001416 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001418 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 .dev_free = snd_rawmidi_dev_free,
1420 .dev_register = snd_rawmidi_dev_register,
1421 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 };
1423
1424 snd_assert(rrawmidi != NULL, return -EINVAL);
1425 *rrawmidi = NULL;
1426 snd_assert(card != NULL, return -ENXIO);
Takashi Iwaica2c0962005-09-09 14:20:23 +02001427 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001428 if (rmidi == NULL) {
1429 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 rmidi->card = card;
1433 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001434 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001436 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1437 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (id != NULL)
1440 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001441 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1442 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1443 SNDRV_RAWMIDI_STREAM_INPUT,
1444 input_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 snd_rawmidi_free(rmidi);
1446 return err;
1447 }
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001448 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1449 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1450 SNDRV_RAWMIDI_STREAM_OUTPUT,
1451 output_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 snd_rawmidi_free(rmidi);
1453 return err;
1454 }
1455 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1456 snd_rawmidi_free(rmidi);
1457 return err;
1458 }
1459 *rrawmidi = rmidi;
1460 return 0;
1461}
1462
Takashi Iwai48c9d412005-11-17 13:56:51 +01001463static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001465 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001468 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 list_del(&substream->list);
1470 kfree(substream);
1471 }
1472}
1473
Takashi Iwai48c9d412005-11-17 13:56:51 +01001474static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 snd_assert(rmidi != NULL, return -ENXIO);
Takashi Iwaic4614822006-06-23 14:38:23 +02001477
1478 snd_info_free_entry(rmidi->proc_entry);
1479 rmidi->proc_entry = NULL;
1480 mutex_lock(&register_mutex);
1481 if (rmidi->ops && rmidi->ops->dev_unregister)
1482 rmidi->ops->dev_unregister(rmidi);
1483 mutex_unlock(&register_mutex);
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1486 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1487 if (rmidi->private_free)
1488 rmidi->private_free(rmidi);
1489 kfree(rmidi);
1490 return 0;
1491}
1492
Takashi Iwai48c9d412005-11-17 13:56:51 +01001493static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001495 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return snd_rawmidi_free(rmidi);
1497}
1498
1499#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
Takashi Iwai48c9d412005-11-17 13:56:51 +01001500static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001502 struct snd_rawmidi *rmidi = device->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 rmidi->seq_dev = NULL;
1504}
1505#endif
1506
Takashi Iwai48c9d412005-11-17 13:56:51 +01001507static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001509 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001510 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001512 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1515 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001516 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001517 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001518 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return -EBUSY;
1520 }
Clemens Ladischf87135f2005-11-20 14:06:59 +01001521 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1523 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1524 rmidi->card, rmidi->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001525 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001527 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001528 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return err;
1530 }
1531 if (rmidi->ops && rmidi->ops->dev_register &&
1532 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1533 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001534 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001535 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return err;
1537 }
1538#ifdef CONFIG_SND_OSSEMUL
1539 rmidi->ossreg = 0;
1540 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1541 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001542 rmidi->card, 0, &snd_rawmidi_f_ops,
1543 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1545 } else {
1546 rmidi->ossreg++;
1547#ifdef SNDRV_OSS_INFO_DEV_MIDI
1548 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1549#endif
1550 }
1551 }
1552 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1553 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001554 rmidi->card, 1, &snd_rawmidi_f_ops,
1555 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1557 } else {
1558 rmidi->ossreg++;
1559 }
1560 }
1561#endif /* CONFIG_SND_OSSEMUL */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001562 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 sprintf(name, "midi%d", rmidi->device);
1564 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1565 if (entry) {
1566 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 entry->c.text.read = snd_rawmidi_proc_info_read;
1568 if (snd_info_register(entry) < 0) {
1569 snd_info_free_entry(entry);
1570 entry = NULL;
1571 }
1572 }
1573 rmidi->proc_entry = entry;
1574#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1575 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1576 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1577 rmidi->seq_dev->private_data = rmidi;
1578 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1579 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1580 snd_device_register(rmidi->card, rmidi->seq_dev);
1581 }
1582 }
1583#endif
1584 return 0;
1585}
1586
Takashi Iwai48c9d412005-11-17 13:56:51 +01001587static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001589 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001591 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001592 list_del_init(&rmidi->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593#ifdef CONFIG_SND_OSSEMUL
1594 if (rmidi->ossreg) {
1595 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1596 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1597#ifdef SNDRV_OSS_INFO_DEV_MIDI
1598 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1599#endif
1600 }
1601 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1602 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1603 rmidi->ossreg = 0;
1604 }
1605#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001607 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
1611/**
1612 * snd_rawmidi_set_ops - set the rawmidi operators
1613 * @rmidi: the rawmidi instance
1614 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1615 * @ops: the operator table
1616 *
1617 * Sets the rawmidi operators for the given stream direction.
1618 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001619void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1620 struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001622 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Johannes Berg9244b2c2006-10-05 16:02:22 +02001624 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628/*
1629 * ENTRY functions
1630 */
1631
1632static int __init alsa_rawmidi_init(void)
1633{
1634
1635 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1636 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1637#ifdef CONFIG_SND_OSSEMUL
1638 { int i;
1639 /* check device map table */
1640 for (i = 0; i < SNDRV_CARDS; i++) {
1641 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1642 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1643 midi_map[i] = 0;
1644 }
1645 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1646 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1647 amidi_map[i] = 1;
1648 }
1649 }
1650 }
1651#endif /* CONFIG_SND_OSSEMUL */
1652 return 0;
1653}
1654
1655static void __exit alsa_rawmidi_exit(void)
1656{
1657 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1658 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1659}
1660
1661module_init(alsa_rawmidi_init)
1662module_exit(alsa_rawmidi_exit)
1663
1664EXPORT_SYMBOL(snd_rawmidi_output_params);
1665EXPORT_SYMBOL(snd_rawmidi_input_params);
1666EXPORT_SYMBOL(snd_rawmidi_drop_output);
1667EXPORT_SYMBOL(snd_rawmidi_drain_output);
1668EXPORT_SYMBOL(snd_rawmidi_drain_input);
1669EXPORT_SYMBOL(snd_rawmidi_receive);
1670EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1671EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1672EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1673EXPORT_SYMBOL(snd_rawmidi_transmit);
1674EXPORT_SYMBOL(snd_rawmidi_new);
1675EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676EXPORT_SYMBOL(snd_rawmidi_info_select);
1677EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1678EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1679EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1680EXPORT_SYMBOL(snd_rawmidi_kernel_write);