blob: b8e700b94e5962b24a9355be54f2548753633eb7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Abstract layer for MIDI v1.0 stream
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/rawmidi.h>
34#include <sound/info.h>
35#include <sound/control.h>
36#include <sound/minors.h>
37#include <sound/initval.h>
38
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020039MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
41MODULE_LICENSE("GPL");
42
43#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai6581f4e2006-05-17 17:14:51 +020044static int midi_map[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
46module_param_array(midi_map, int, NULL, 0444);
47MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
48module_param_array(amidi_map, int, NULL, 0444);
49MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
50#endif /* CONFIG_SND_OSSEMUL */
51
Takashi Iwai48c9d412005-11-17 13:56:51 +010052static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
53static int snd_rawmidi_dev_free(struct snd_device *device);
54static int snd_rawmidi_dev_register(struct snd_device *device);
55static int snd_rawmidi_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Clemens Ladischf87135f2005-11-20 14:06:59 +010057static LIST_HEAD(snd_rawmidi_devices);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010058static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Clemens Ladischf87135f2005-11-20 14:06:59 +010060static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
61{
Clemens Ladischf87135f2005-11-20 14:06:59 +010062 struct snd_rawmidi *rawmidi;
63
Johannes Berg9244b2c2006-10-05 16:02:22 +020064 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
Clemens Ladischf87135f2005-11-20 14:06:59 +010065 if (rawmidi->card == card && rawmidi->device == device)
66 return rawmidi;
Clemens Ladischf87135f2005-11-20 14:06:59 +010067 return NULL;
68}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static inline unsigned short snd_rawmidi_file_flags(struct file *file)
71{
72 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
73 case FMODE_WRITE:
74 return SNDRV_RAWMIDI_LFLG_OUTPUT;
75 case FMODE_READ:
76 return SNDRV_RAWMIDI_LFLG_INPUT;
77 default:
78 return SNDRV_RAWMIDI_LFLG_OPEN;
79 }
80}
81
Takashi Iwai48c9d412005-11-17 13:56:51 +010082static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Takashi Iwai48c9d412005-11-17 13:56:51 +010084 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return runtime->avail >= runtime->avail_min;
86}
87
Takashi Iwai48c9d412005-11-17 13:56:51 +010088static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
89 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Takashi Iwai48c9d412005-11-17 13:56:51 +010091 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return runtime->avail >= runtime->avail_min &&
93 (!substream->append || runtime->avail >= count);
94}
95
96static void snd_rawmidi_input_event_tasklet(unsigned long data)
97{
Takashi Iwai48c9d412005-11-17 13:56:51 +010098 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 substream->runtime->event(substream);
100}
101
102static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
103{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100104 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 substream->ops->trigger(substream, 1);
106}
107
Takashi Iwai48c9d412005-11-17 13:56:51 +0100108static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100110 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Takashi Iwaica2c0962005-09-09 14:20:23 +0200112 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return -ENOMEM;
114 spin_lock_init(&runtime->lock);
115 init_waitqueue_head(&runtime->sleep);
116 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
117 tasklet_init(&runtime->tasklet,
118 snd_rawmidi_input_event_tasklet,
119 (unsigned long)substream);
120 else
121 tasklet_init(&runtime->tasklet,
122 snd_rawmidi_output_trigger_tasklet,
123 (unsigned long)substream);
124 runtime->event = NULL;
125 runtime->buffer_size = PAGE_SIZE;
126 runtime->avail_min = 1;
127 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
128 runtime->avail = 0;
129 else
130 runtime->avail = runtime->buffer_size;
131 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
132 kfree(runtime);
133 return -ENOMEM;
134 }
135 runtime->appl_ptr = runtime->hw_ptr = 0;
136 substream->runtime = runtime;
137 return 0;
138}
139
Takashi Iwai48c9d412005-11-17 13:56:51 +0100140static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100142 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 kfree(runtime->buffer);
145 kfree(runtime);
146 substream->runtime = NULL;
147 return 0;
148}
149
Takashi Iwai48c9d412005-11-17 13:56:51 +0100150static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 if (up) {
153 tasklet_hi_schedule(&substream->runtime->tasklet);
154 } else {
155 tasklet_kill(&substream->runtime->tasklet);
156 substream->ops->trigger(substream, 0);
157 }
158}
159
Takashi Iwai48c9d412005-11-17 13:56:51 +0100160static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 substream->ops->trigger(substream, up);
163 if (!up && substream->runtime->event)
164 tasklet_kill(&substream->runtime->tasklet);
165}
166
Takashi Iwai48c9d412005-11-17 13:56:51 +0100167int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100170 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 snd_rawmidi_output_trigger(substream, 0);
173 runtime->drain = 0;
174 spin_lock_irqsave(&runtime->lock, flags);
175 runtime->appl_ptr = runtime->hw_ptr = 0;
176 runtime->avail = runtime->buffer_size;
177 spin_unlock_irqrestore(&runtime->lock, flags);
178 return 0;
179}
180
Takashi Iwai48c9d412005-11-17 13:56:51 +0100181int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 int err;
184 long timeout;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100185 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 err = 0;
188 runtime->drain = 1;
189 timeout = wait_event_interruptible_timeout(runtime->sleep,
190 (runtime->avail >= runtime->buffer_size),
191 10*HZ);
192 if (signal_pending(current))
193 err = -ERESTARTSYS;
194 if (runtime->avail < runtime->buffer_size && !timeout) {
195 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
196 err = -EIO;
197 }
198 runtime->drain = 0;
199 if (err != -ERESTARTSYS) {
200 /* we need wait a while to make sure that Tx FIFOs are empty */
201 if (substream->ops->drain)
202 substream->ops->drain(substream);
203 else
204 msleep(50);
205 snd_rawmidi_drop_output(substream);
206 }
207 return err;
208}
209
Takashi Iwai48c9d412005-11-17 13:56:51 +0100210int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100213 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 snd_rawmidi_input_trigger(substream, 0);
216 runtime->drain = 0;
217 spin_lock_irqsave(&runtime->lock, flags);
218 runtime->appl_ptr = runtime->hw_ptr = 0;
219 runtime->avail = 0;
220 spin_unlock_irqrestore(&runtime->lock, flags);
221 return 0;
222}
223
Clemens Ladischf87135f2005-11-20 14:06:59 +0100224int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai48c9d412005-11-17 13:56:51 +0100225 int mode, struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100227 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct list_head *list1, *list2;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100229 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
230 struct snd_rawmidi_runtime *input = NULL, *output = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int err;
232
233 if (rfile)
234 rfile->input = rfile->output = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100235 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100236 rmidi = snd_rawmidi_search(card, device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100237 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (rmidi == NULL) {
239 err = -ENODEV;
240 goto __error1;
241 }
242 if (!try_module_get(rmidi->card->module)) {
243 err = -EFAULT;
244 goto __error1;
245 }
246 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100247 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
249 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
250 err = -ENXIO;
251 goto __error;
252 }
253 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
254 err = -ENODEV;
255 goto __error;
256 }
257 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
258 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
259 err = -EAGAIN;
260 goto __error;
261 }
262 }
263 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
264 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
265 err = -ENXIO;
266 goto __error;
267 }
268 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
269 err = -ENODEV;
270 goto __error;
271 }
272 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
273 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
274 err = -EAGAIN;
275 goto __error;
276 }
277 }
278 list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
279 while (1) {
280 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
281 sinput = NULL;
282 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
283 err = -EAGAIN;
284 goto __error;
285 }
286 break;
287 }
Takashi Iwai48c9d412005-11-17 13:56:51 +0100288 sinput = list_entry(list1, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
290 goto __nexti;
291 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
292 break;
293 __nexti:
294 list1 = list1->next;
295 }
296 list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
297 while (1) {
298 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
299 soutput = NULL;
300 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
301 err = -EAGAIN;
302 goto __error;
303 }
304 break;
305 }
Takashi Iwai48c9d412005-11-17 13:56:51 +0100306 soutput = list_entry(list2, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
308 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
309 if (soutput->opened && !soutput->append)
310 goto __nexto;
311 } else {
312 if (soutput->opened)
313 goto __nexto;
314 }
315 }
316 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
317 break;
318 __nexto:
319 list2 = list2->next;
320 }
321 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
322 if ((err = snd_rawmidi_runtime_create(sinput)) < 0)
323 goto __error;
324 input = sinput->runtime;
325 if ((err = sinput->ops->open(sinput)) < 0)
326 goto __error;
327 sinput->opened = 1;
328 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
329 } else {
330 sinput = NULL;
331 }
332 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
333 if (soutput->opened)
334 goto __skip_output;
335 if ((err = snd_rawmidi_runtime_create(soutput)) < 0) {
336 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
337 sinput->ops->close(sinput);
338 goto __error;
339 }
340 output = soutput->runtime;
341 if ((err = soutput->ops->open(soutput)) < 0) {
342 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
343 sinput->ops->close(sinput);
344 goto __error;
345 }
346 __skip_output:
347 soutput->opened = 1;
348 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
349 soutput->append = 1;
350 if (soutput->use_count++ == 0)
351 soutput->active_sensing = 1;
352 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
353 } else {
354 soutput = NULL;
355 }
356 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100357 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (rfile) {
359 rfile->rmidi = rmidi;
360 rfile->input = sinput;
361 rfile->output = soutput;
362 }
363 return 0;
364
365 __error:
366 if (input != NULL)
367 snd_rawmidi_runtime_free(sinput);
368 if (output != NULL)
369 snd_rawmidi_runtime_free(soutput);
370 module_put(rmidi->card->module);
371 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100372 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 __error1:
374 return err;
375}
376
377static int snd_rawmidi_open(struct inode *inode, struct file *file)
378{
379 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100380 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100381 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unsigned short fflags;
383 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100384 struct snd_rawmidi *rmidi;
385 struct snd_rawmidi_file *rawmidi_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 wait_queue_t wait;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100387 struct snd_ctl_file *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Clemens Ladischf1902862005-10-24 17:05:03 +0200389 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100390 rmidi = snd_lookup_minor_data(iminor(inode),
391 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200393 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100394 rmidi = snd_lookup_oss_minor_data(iminor(inode),
395 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200397 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (rmidi == NULL)
401 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
403 return -EINVAL; /* invalid combination */
404 card = rmidi->card;
405 err = snd_card_file_add(card, file);
406 if (err < 0)
407 return -ENODEV;
408 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200409 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
411 fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
412 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
413 if (rawmidi_file == NULL) {
414 snd_card_file_remove(card, file);
415 return -ENOMEM;
416 }
417 init_waitqueue_entry(&wait, current);
418 add_wait_queue(&rmidi->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100419 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 while (1) {
421 subdevice = -1;
422 down_read(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200423 list_for_each_entry(kctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (kctl->pid == current->pid) {
425 subdevice = kctl->prefer_rawmidi_subdevice;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200426 if (subdevice != -1)
427 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 }
430 up_read(&card->controls_rwsem);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100431 err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
432 subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (err >= 0)
434 break;
435 if (err == -EAGAIN) {
436 if (file->f_flags & O_NONBLOCK) {
437 err = -EBUSY;
438 break;
439 }
440 } else
441 break;
442 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100443 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100445 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (signal_pending(current)) {
447 err = -ERESTARTSYS;
448 break;
449 }
450 }
451#ifdef CONFIG_SND_OSSEMUL
452 if (rawmidi_file->input && rawmidi_file->input->runtime)
453 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
454 if (rawmidi_file->output && rawmidi_file->output->runtime)
455 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
456#endif
457 remove_wait_queue(&rmidi->open_wait, &wait);
458 if (err >= 0) {
459 file->private_data = rawmidi_file;
460 } else {
461 snd_card_file_remove(card, file);
462 kfree(rawmidi_file);
463 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100464 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return err;
466}
467
Takashi Iwai48c9d412005-11-17 13:56:51 +0100468int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100470 struct snd_rawmidi *rmidi;
471 struct snd_rawmidi_substream *substream;
472 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 snd_assert(rfile != NULL, return -ENXIO);
475 snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
476 rmidi = rfile->rmidi;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100477 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (rfile->input != NULL) {
479 substream = rfile->input;
480 rfile->input = NULL;
481 runtime = substream->runtime;
482 snd_rawmidi_input_trigger(substream, 0);
483 substream->ops->close(substream);
484 if (runtime->private_free != NULL)
485 runtime->private_free(substream);
486 snd_rawmidi_runtime_free(substream);
487 substream->opened = 0;
488 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
489 }
490 if (rfile->output != NULL) {
491 substream = rfile->output;
492 rfile->output = NULL;
493 if (--substream->use_count == 0) {
494 runtime = substream->runtime;
495 if (substream->active_sensing) {
496 unsigned char buf = 0xfe;
497 /* sending single active sensing message to shut the device up */
498 snd_rawmidi_kernel_write(substream, &buf, 1);
499 }
500 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
501 snd_rawmidi_output_trigger(substream, 0);
502 substream->ops->close(substream);
503 if (runtime->private_free != NULL)
504 runtime->private_free(substream);
505 snd_rawmidi_runtime_free(substream);
506 substream->opened = 0;
507 substream->append = 0;
508 }
509 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
510 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100511 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 module_put(rmidi->card->module);
513 return 0;
514}
515
516static int snd_rawmidi_release(struct inode *inode, struct file *file)
517{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100518 struct snd_rawmidi_file *rfile;
519 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int err;
521
522 rfile = file->private_data;
523 err = snd_rawmidi_kernel_release(rfile);
524 rmidi = rfile->rmidi;
525 wake_up(&rmidi->open_wait);
526 kfree(rfile);
527 snd_card_file_remove(rmidi->card, file);
528 return err;
529}
530
Adrian Bunk18612042005-11-23 13:14:50 +0100531static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
532 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100534 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 if (substream == NULL)
537 return -ENODEV;
538 rmidi = substream->rmidi;
539 memset(info, 0, sizeof(*info));
540 info->card = rmidi->card->number;
541 info->device = rmidi->device;
542 info->subdevice = substream->number;
543 info->stream = substream->stream;
544 info->flags = rmidi->info_flags;
545 strcpy(info->id, rmidi->id);
546 strcpy(info->name, rmidi->name);
547 strcpy(info->subname, substream->name);
548 info->subdevices_count = substream->pstr->substream_count;
549 info->subdevices_avail = (substream->pstr->substream_count -
550 substream->pstr->substream_opened);
551 return 0;
552}
553
Takashi Iwai48c9d412005-11-17 13:56:51 +0100554static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
555 struct snd_rawmidi_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100557 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int err;
559 if ((err = snd_rawmidi_info(substream, &info)) < 0)
560 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100561 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return -EFAULT;
563 return 0;
564}
565
Takashi Iwai48c9d412005-11-17 13:56:51 +0100566int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100568 struct snd_rawmidi *rmidi;
569 struct snd_rawmidi_str *pstr;
570 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100571
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100572 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100573 rmidi = snd_rawmidi_search(card, info->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100574 mutex_unlock(&register_mutex);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100575 if (!rmidi)
576 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (info->stream < 0 || info->stream > 1)
578 return -EINVAL;
579 pstr = &rmidi->streams[info->stream];
580 if (pstr->substream_count == 0)
581 return -ENOENT;
582 if (info->subdevice >= pstr->substream_count)
583 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200584 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if ((unsigned int)substream->number == info->subdevice)
586 return snd_rawmidi_info(substream, info);
587 }
588 return -ENXIO;
589}
590
Takashi Iwai48c9d412005-11-17 13:56:51 +0100591static int snd_rawmidi_info_select_user(struct snd_card *card,
592 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100595 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (get_user(info.device, &_info->device))
597 return -EFAULT;
598 if (get_user(info.stream, &_info->stream))
599 return -EFAULT;
600 if (get_user(info.subdevice, &_info->subdevice))
601 return -EFAULT;
602 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
603 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100604 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return -EFAULT;
606 return 0;
607}
608
Takashi Iwai48c9d412005-11-17 13:56:51 +0100609int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
610 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100613 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (substream->append && substream->use_count > 1)
616 return -EBUSY;
617 snd_rawmidi_drain_output(substream);
618 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
619 return -EINVAL;
620 }
621 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
622 return -EINVAL;
623 }
624 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800625 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
626 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return -ENOMEM;
628 kfree(runtime->buffer);
629 runtime->buffer = newbuf;
630 runtime->buffer_size = params->buffer_size;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100631 runtime->avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 runtime->avail_min = params->avail_min;
634 substream->active_sensing = !params->no_active_sensing;
635 return 0;
636}
637
Takashi Iwai48c9d412005-11-17 13:56:51 +0100638int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
639 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100642 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 snd_rawmidi_drain_input(substream);
645 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
646 return -EINVAL;
647 }
648 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
649 return -EINVAL;
650 }
651 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800652 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
653 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -ENOMEM;
655 kfree(runtime->buffer);
656 runtime->buffer = newbuf;
657 runtime->buffer_size = params->buffer_size;
658 }
659 runtime->avail_min = params->avail_min;
660 return 0;
661}
662
Takashi Iwai48c9d412005-11-17 13:56:51 +0100663static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
664 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100666 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 memset(status, 0, sizeof(*status));
669 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
670 spin_lock_irq(&runtime->lock);
671 status->avail = runtime->avail;
672 spin_unlock_irq(&runtime->lock);
673 return 0;
674}
675
Takashi Iwai48c9d412005-11-17 13:56:51 +0100676static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
677 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100679 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 memset(status, 0, sizeof(*status));
682 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
683 spin_lock_irq(&runtime->lock);
684 status->avail = runtime->avail;
685 status->xruns = runtime->xruns;
686 runtime->xruns = 0;
687 spin_unlock_irq(&runtime->lock);
688 return 0;
689}
690
691static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
692{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100693 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 void __user *argp = (void __user *)arg;
695
696 rfile = file->private_data;
697 if (((cmd >> 8) & 0xff) != 'W')
698 return -ENOTTY;
699 switch (cmd) {
700 case SNDRV_RAWMIDI_IOCTL_PVERSION:
701 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
702 case SNDRV_RAWMIDI_IOCTL_INFO:
703 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100704 int stream;
705 struct snd_rawmidi_info __user *info = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (get_user(stream, &info->stream))
707 return -EFAULT;
708 switch (stream) {
709 case SNDRV_RAWMIDI_STREAM_INPUT:
710 return snd_rawmidi_info_user(rfile->input, info);
711 case SNDRV_RAWMIDI_STREAM_OUTPUT:
712 return snd_rawmidi_info_user(rfile->output, info);
713 default:
714 return -EINVAL;
715 }
716 }
717 case SNDRV_RAWMIDI_IOCTL_PARAMS:
718 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100719 struct snd_rawmidi_params params;
720 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -EFAULT;
722 switch (params.stream) {
723 case SNDRV_RAWMIDI_STREAM_OUTPUT:
724 if (rfile->output == NULL)
725 return -EINVAL;
726 return snd_rawmidi_output_params(rfile->output, &params);
727 case SNDRV_RAWMIDI_STREAM_INPUT:
728 if (rfile->input == NULL)
729 return -EINVAL;
730 return snd_rawmidi_input_params(rfile->input, &params);
731 default:
732 return -EINVAL;
733 }
734 }
735 case SNDRV_RAWMIDI_IOCTL_STATUS:
736 {
737 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100738 struct snd_rawmidi_status status;
739 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return -EFAULT;
741 switch (status.stream) {
742 case SNDRV_RAWMIDI_STREAM_OUTPUT:
743 if (rfile->output == NULL)
744 return -EINVAL;
745 err = snd_rawmidi_output_status(rfile->output, &status);
746 break;
747 case SNDRV_RAWMIDI_STREAM_INPUT:
748 if (rfile->input == NULL)
749 return -EINVAL;
750 err = snd_rawmidi_input_status(rfile->input, &status);
751 break;
752 default:
753 return -EINVAL;
754 }
755 if (err < 0)
756 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100757 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return -EFAULT;
759 return 0;
760 }
761 case SNDRV_RAWMIDI_IOCTL_DROP:
762 {
763 int val;
764 if (get_user(val, (int __user *) argp))
765 return -EFAULT;
766 switch (val) {
767 case SNDRV_RAWMIDI_STREAM_OUTPUT:
768 if (rfile->output == NULL)
769 return -EINVAL;
770 return snd_rawmidi_drop_output(rfile->output);
771 default:
772 return -EINVAL;
773 }
774 }
775 case SNDRV_RAWMIDI_IOCTL_DRAIN:
776 {
777 int val;
778 if (get_user(val, (int __user *) argp))
779 return -EFAULT;
780 switch (val) {
781 case SNDRV_RAWMIDI_STREAM_OUTPUT:
782 if (rfile->output == NULL)
783 return -EINVAL;
784 return snd_rawmidi_drain_output(rfile->output);
785 case SNDRV_RAWMIDI_STREAM_INPUT:
786 if (rfile->input == NULL)
787 return -EINVAL;
788 return snd_rawmidi_drain_input(rfile->input);
789 default:
790 return -EINVAL;
791 }
792 }
793#ifdef CONFIG_SND_DEBUG
794 default:
795 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
796#endif
797 }
798 return -ENOTTY;
799}
800
Takashi Iwai48c9d412005-11-17 13:56:51 +0100801static int snd_rawmidi_control_ioctl(struct snd_card *card,
802 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 unsigned int cmd,
804 unsigned long arg)
805{
806 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 switch (cmd) {
809 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
810 {
811 int device;
812
813 if (get_user(device, (int __user *)argp))
814 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100815 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 device = device < 0 ? 0 : device + 1;
817 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100818 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 break;
820 device++;
821 }
822 if (device == SNDRV_RAWMIDI_DEVICES)
823 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100824 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (put_user(device, (int __user *)argp))
826 return -EFAULT;
827 return 0;
828 }
829 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
830 {
831 int val;
832
833 if (get_user(val, (int __user *)argp))
834 return -EFAULT;
835 control->prefer_rawmidi_subdevice = val;
836 return 0;
837 }
838 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
839 return snd_rawmidi_info_select_user(card, argp);
840 }
841 return -ENOIOCTLCMD;
842}
843
844/**
845 * snd_rawmidi_receive - receive the input data from the device
846 * @substream: the rawmidi substream
847 * @buffer: the buffer pointer
848 * @count: the data size to read
849 *
850 * Reads the data from the internal buffer.
851 *
852 * Returns the size of read data, or a negative error code on failure.
853 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100854int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
855 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 unsigned long flags;
858 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100859 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (runtime->buffer == NULL) {
862 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
863 return -EINVAL;
864 }
865 spin_lock_irqsave(&runtime->lock, flags);
866 if (count == 1) { /* special case, faster code */
867 substream->bytes++;
868 if (runtime->avail < runtime->buffer_size) {
869 runtime->buffer[runtime->hw_ptr++] = buffer[0];
870 runtime->hw_ptr %= runtime->buffer_size;
871 runtime->avail++;
872 result++;
873 } else {
874 runtime->xruns++;
875 }
876 } else {
877 substream->bytes += count;
878 count1 = runtime->buffer_size - runtime->hw_ptr;
879 if (count1 > count)
880 count1 = count;
881 if (count1 > (int)(runtime->buffer_size - runtime->avail))
882 count1 = runtime->buffer_size - runtime->avail;
883 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
884 runtime->hw_ptr += count1;
885 runtime->hw_ptr %= runtime->buffer_size;
886 runtime->avail += count1;
887 count -= count1;
888 result += count1;
889 if (count > 0) {
890 buffer += count1;
891 count1 = count;
892 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
893 count1 = runtime->buffer_size - runtime->avail;
894 runtime->xruns += count - count1;
895 }
896 if (count1 > 0) {
897 memcpy(runtime->buffer, buffer, count1);
898 runtime->hw_ptr = count1;
899 runtime->avail += count1;
900 result += count1;
901 }
902 }
903 }
904 if (result > 0) {
905 if (runtime->event)
906 tasklet_hi_schedule(&runtime->tasklet);
907 else if (snd_rawmidi_ready(substream))
908 wake_up(&runtime->sleep);
909 }
910 spin_unlock_irqrestore(&runtime->lock, flags);
911 return result;
912}
913
Takashi Iwai48c9d412005-11-17 13:56:51 +0100914static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 unsigned char *buf, long count, int kernel)
916{
917 unsigned long flags;
918 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100919 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 while (count > 0 && runtime->avail) {
922 count1 = runtime->buffer_size - runtime->appl_ptr;
923 if (count1 > count)
924 count1 = count;
925 spin_lock_irqsave(&runtime->lock, flags);
926 if (count1 > (int)runtime->avail)
927 count1 = runtime->avail;
928 if (kernel) {
929 memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
930 } else {
931 spin_unlock_irqrestore(&runtime->lock, flags);
932 if (copy_to_user((char __user *)buf + result,
933 runtime->buffer + runtime->appl_ptr, count1)) {
934 return result > 0 ? result : -EFAULT;
935 }
936 spin_lock_irqsave(&runtime->lock, flags);
937 }
938 runtime->appl_ptr += count1;
939 runtime->appl_ptr %= runtime->buffer_size;
940 runtime->avail -= count1;
941 spin_unlock_irqrestore(&runtime->lock, flags);
942 result += count1;
943 count -= count1;
944 }
945 return result;
946}
947
Takashi Iwai48c9d412005-11-17 13:56:51 +0100948long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
949 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 snd_rawmidi_input_trigger(substream, 1);
952 return snd_rawmidi_kernel_read1(substream, buf, count, 1);
953}
954
Takashi Iwai48c9d412005-11-17 13:56:51 +0100955static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
956 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 long result;
959 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100960 struct snd_rawmidi_file *rfile;
961 struct snd_rawmidi_substream *substream;
962 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 rfile = file->private_data;
965 substream = rfile->input;
966 if (substream == NULL)
967 return -EIO;
968 runtime = substream->runtime;
969 snd_rawmidi_input_trigger(substream, 1);
970 result = 0;
971 while (count > 0) {
972 spin_lock_irq(&runtime->lock);
973 while (!snd_rawmidi_ready(substream)) {
974 wait_queue_t wait;
975 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
976 spin_unlock_irq(&runtime->lock);
977 return result > 0 ? result : -EAGAIN;
978 }
979 init_waitqueue_entry(&wait, current);
980 add_wait_queue(&runtime->sleep, &wait);
981 set_current_state(TASK_INTERRUPTIBLE);
982 spin_unlock_irq(&runtime->lock);
983 schedule();
984 remove_wait_queue(&runtime->sleep, &wait);
985 if (signal_pending(current))
986 return result > 0 ? result : -ERESTARTSYS;
987 if (!runtime->avail)
988 return result > 0 ? result : -EIO;
989 spin_lock_irq(&runtime->lock);
990 }
991 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +0200992 count1 = snd_rawmidi_kernel_read1(substream,
993 (unsigned char __force *)buf,
994 count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (count1 < 0)
996 return result > 0 ? result : count1;
997 result += count1;
998 buf += count1;
999 count -= count1;
1000 }
1001 return result;
1002}
1003
1004/**
1005 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1006 * @substream: the rawmidi substream
1007 *
1008 * Returns 1 if the internal output buffer is empty, 0 if not.
1009 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001010int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001012 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int result;
1014 unsigned long flags;
1015
1016 if (runtime->buffer == NULL) {
1017 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1018 return 1;
1019 }
1020 spin_lock_irqsave(&runtime->lock, flags);
1021 result = runtime->avail >= runtime->buffer_size;
1022 spin_unlock_irqrestore(&runtime->lock, flags);
1023 return result;
1024}
1025
1026/**
1027 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1028 * @substream: the rawmidi substream
1029 * @buffer: the buffer pointer
1030 * @count: data size to transfer
1031 *
1032 * Copies data from the internal output buffer to the given buffer.
1033 *
1034 * Call this in the interrupt handler when the midi output is ready,
1035 * and call snd_rawmidi_transmit_ack() after the transmission is
1036 * finished.
1037 *
1038 * Returns the size of copied data, or a negative error code on failure.
1039 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001040int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1041 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 unsigned long flags;
1044 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001045 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (runtime->buffer == NULL) {
1048 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1049 return -EINVAL;
1050 }
1051 result = 0;
1052 spin_lock_irqsave(&runtime->lock, flags);
1053 if (runtime->avail >= runtime->buffer_size) {
1054 /* warning: lowlevel layer MUST trigger down the hardware */
1055 goto __skip;
1056 }
1057 if (count == 1) { /* special case, faster code */
1058 *buffer = runtime->buffer[runtime->hw_ptr];
1059 result++;
1060 } else {
1061 count1 = runtime->buffer_size - runtime->hw_ptr;
1062 if (count1 > count)
1063 count1 = count;
1064 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1065 count1 = runtime->buffer_size - runtime->avail;
1066 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1067 count -= count1;
1068 result += count1;
1069 if (count > 0) {
1070 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1071 count = runtime->buffer_size - runtime->avail - count1;
1072 memcpy(buffer + count1, runtime->buffer, count);
1073 result += count;
1074 }
1075 }
1076 __skip:
1077 spin_unlock_irqrestore(&runtime->lock, flags);
1078 return result;
1079}
1080
1081/**
1082 * snd_rawmidi_transmit_ack - acknowledge the transmission
1083 * @substream: the rawmidi substream
1084 * @count: the tranferred count
1085 *
1086 * Advances the hardware pointer for the internal output buffer with
1087 * the given size and updates the condition.
1088 * Call after the transmission is finished.
1089 *
1090 * Returns the advanced size if successful, or a negative error code on failure.
1091 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001092int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001095 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 if (runtime->buffer == NULL) {
1098 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1099 return -EINVAL;
1100 }
1101 spin_lock_irqsave(&runtime->lock, flags);
1102 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1103 runtime->hw_ptr += count;
1104 runtime->hw_ptr %= runtime->buffer_size;
1105 runtime->avail += count;
1106 substream->bytes += count;
1107 if (count > 0) {
1108 if (runtime->drain || snd_rawmidi_ready(substream))
1109 wake_up(&runtime->sleep);
1110 }
1111 spin_unlock_irqrestore(&runtime->lock, flags);
1112 return count;
1113}
1114
1115/**
1116 * snd_rawmidi_transmit - copy from the buffer to the device
1117 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001118 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 * @count: the data size to transfer
1120 *
1121 * Copies data from the buffer to the device and advances the pointer.
1122 *
1123 * Returns the copied size if successful, or a negative error code on failure.
1124 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001125int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1126 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1129 if (count < 0)
1130 return count;
1131 return snd_rawmidi_transmit_ack(substream, count);
1132}
1133
Takashi Iwai48c9d412005-11-17 13:56:51 +01001134static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1135 const unsigned char *buf, long count, int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 unsigned long flags;
1138 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001139 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 snd_assert(buf != NULL, return -EINVAL);
1142 snd_assert(runtime->buffer != NULL, return -EINVAL);
1143
1144 result = 0;
1145 spin_lock_irqsave(&runtime->lock, flags);
1146 if (substream->append) {
1147 if ((long)runtime->avail < count) {
1148 spin_unlock_irqrestore(&runtime->lock, flags);
1149 return -EAGAIN;
1150 }
1151 }
1152 while (count > 0 && runtime->avail > 0) {
1153 count1 = runtime->buffer_size - runtime->appl_ptr;
1154 if (count1 > count)
1155 count1 = count;
1156 if (count1 > (long)runtime->avail)
1157 count1 = runtime->avail;
1158 if (kernel) {
1159 memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1160 } else {
1161 spin_unlock_irqrestore(&runtime->lock, flags);
1162 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1163 (char __user *)buf, count1)) {
1164 spin_lock_irqsave(&runtime->lock, flags);
1165 result = result > 0 ? result : -EFAULT;
1166 goto __end;
1167 }
1168 spin_lock_irqsave(&runtime->lock, flags);
1169 }
1170 runtime->appl_ptr += count1;
1171 runtime->appl_ptr %= runtime->buffer_size;
1172 runtime->avail -= count1;
1173 result += count1;
1174 buf += count1;
1175 count -= count1;
1176 }
1177 __end:
1178 count1 = runtime->avail < runtime->buffer_size;
1179 spin_unlock_irqrestore(&runtime->lock, flags);
1180 if (count1)
1181 snd_rawmidi_output_trigger(substream, 1);
1182 return result;
1183}
1184
Takashi Iwai48c9d412005-11-17 13:56:51 +01001185long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1186 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1189}
1190
Takashi Iwai48c9d412005-11-17 13:56:51 +01001191static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1192 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 long result, timeout;
1195 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001196 struct snd_rawmidi_file *rfile;
1197 struct snd_rawmidi_runtime *runtime;
1198 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 rfile = file->private_data;
1201 substream = rfile->output;
1202 runtime = substream->runtime;
1203 /* we cannot put an atomic message to our buffer */
1204 if (substream->append && count > runtime->buffer_size)
1205 return -EIO;
1206 result = 0;
1207 while (count > 0) {
1208 spin_lock_irq(&runtime->lock);
1209 while (!snd_rawmidi_ready_append(substream, count)) {
1210 wait_queue_t wait;
1211 if (file->f_flags & O_NONBLOCK) {
1212 spin_unlock_irq(&runtime->lock);
1213 return result > 0 ? result : -EAGAIN;
1214 }
1215 init_waitqueue_entry(&wait, current);
1216 add_wait_queue(&runtime->sleep, &wait);
1217 set_current_state(TASK_INTERRUPTIBLE);
1218 spin_unlock_irq(&runtime->lock);
1219 timeout = schedule_timeout(30 * HZ);
1220 remove_wait_queue(&runtime->sleep, &wait);
1221 if (signal_pending(current))
1222 return result > 0 ? result : -ERESTARTSYS;
1223 if (!runtime->avail && !timeout)
1224 return result > 0 ? result : -EIO;
1225 spin_lock_irq(&runtime->lock);
1226 }
1227 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001228 count1 = snd_rawmidi_kernel_write1(substream,
1229 (unsigned char __force *)buf,
1230 count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (count1 < 0)
1232 return result > 0 ? result : count1;
1233 result += count1;
1234 buf += count1;
1235 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1236 break;
1237 count -= count1;
1238 }
1239 if (file->f_flags & O_SYNC) {
1240 spin_lock_irq(&runtime->lock);
1241 while (runtime->avail != runtime->buffer_size) {
1242 wait_queue_t wait;
1243 unsigned int last_avail = runtime->avail;
1244 init_waitqueue_entry(&wait, current);
1245 add_wait_queue(&runtime->sleep, &wait);
1246 set_current_state(TASK_INTERRUPTIBLE);
1247 spin_unlock_irq(&runtime->lock);
1248 timeout = schedule_timeout(30 * HZ);
1249 remove_wait_queue(&runtime->sleep, &wait);
1250 if (signal_pending(current))
1251 return result > 0 ? result : -ERESTARTSYS;
1252 if (runtime->avail == last_avail && !timeout)
1253 return result > 0 ? result : -EIO;
1254 spin_lock_irq(&runtime->lock);
1255 }
1256 spin_unlock_irq(&runtime->lock);
1257 }
1258 return result;
1259}
1260
1261static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1262{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001263 struct snd_rawmidi_file *rfile;
1264 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 unsigned int mask;
1266
1267 rfile = file->private_data;
1268 if (rfile->input != NULL) {
1269 runtime = rfile->input->runtime;
1270 snd_rawmidi_input_trigger(rfile->input, 1);
1271 poll_wait(file, &runtime->sleep, wait);
1272 }
1273 if (rfile->output != NULL) {
1274 runtime = rfile->output->runtime;
1275 poll_wait(file, &runtime->sleep, wait);
1276 }
1277 mask = 0;
1278 if (rfile->input != NULL) {
1279 if (snd_rawmidi_ready(rfile->input))
1280 mask |= POLLIN | POLLRDNORM;
1281 }
1282 if (rfile->output != NULL) {
1283 if (snd_rawmidi_ready(rfile->output))
1284 mask |= POLLOUT | POLLWRNORM;
1285 }
1286 return mask;
1287}
1288
1289/*
1290 */
1291#ifdef CONFIG_COMPAT
1292#include "rawmidi_compat.c"
1293#else
1294#define snd_rawmidi_ioctl_compat NULL
1295#endif
1296
1297/*
1298
1299 */
1300
Takashi Iwai48c9d412005-11-17 13:56:51 +01001301static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1302 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001304 struct snd_rawmidi *rmidi;
1305 struct snd_rawmidi_substream *substream;
1306 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 rmidi = entry->private_data;
1309 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001310 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001312 list_for_each_entry(substream,
1313 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1314 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 snd_iprintf(buffer,
1316 "Output %d\n"
1317 " Tx bytes : %lu\n",
1318 substream->number,
1319 (unsigned long) substream->bytes);
1320 if (substream->opened) {
1321 runtime = substream->runtime;
1322 snd_iprintf(buffer,
1323 " Mode : %s\n"
1324 " Buffer size : %lu\n"
1325 " Avail : %lu\n",
1326 runtime->oss ? "OSS compatible" : "native",
1327 (unsigned long) runtime->buffer_size,
1328 (unsigned long) runtime->avail);
1329 }
1330 }
1331 }
1332 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001333 list_for_each_entry(substream,
1334 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1335 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 snd_iprintf(buffer,
1337 "Input %d\n"
1338 " Rx bytes : %lu\n",
1339 substream->number,
1340 (unsigned long) substream->bytes);
1341 if (substream->opened) {
1342 runtime = substream->runtime;
1343 snd_iprintf(buffer,
1344 " Buffer size : %lu\n"
1345 " Avail : %lu\n"
1346 " Overruns : %lu\n",
1347 (unsigned long) runtime->buffer_size,
1348 (unsigned long) runtime->avail,
1349 (unsigned long) runtime->xruns);
1350 }
1351 }
1352 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001353 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356/*
1357 * Register functions
1358 */
1359
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001360static const struct file_operations snd_rawmidi_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 .owner = THIS_MODULE,
1363 .read = snd_rawmidi_read,
1364 .write = snd_rawmidi_write,
1365 .open = snd_rawmidi_open,
1366 .release = snd_rawmidi_release,
1367 .poll = snd_rawmidi_poll,
1368 .unlocked_ioctl = snd_rawmidi_ioctl,
1369 .compat_ioctl = snd_rawmidi_ioctl_compat,
1370};
1371
Takashi Iwai48c9d412005-11-17 13:56:51 +01001372static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1373 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 int direction,
1375 int count)
1376{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001377 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 int idx;
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001381 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001382 if (substream == NULL) {
1383 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 substream->stream = direction;
1387 substream->number = idx;
1388 substream->rmidi = rmidi;
1389 substream->pstr = stream;
1390 list_add_tail(&substream->list, &stream->substreams);
1391 stream->substream_count++;
1392 }
1393 return 0;
1394}
1395
1396/**
1397 * snd_rawmidi_new - create a rawmidi instance
1398 * @card: the card instance
1399 * @id: the id string
1400 * @device: the device index
1401 * @output_count: the number of output streams
1402 * @input_count: the number of input streams
1403 * @rrawmidi: the pointer to store the new rawmidi instance
1404 *
1405 * Creates a new rawmidi instance.
1406 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1407 *
1408 * Returns zero if successful, or a negative error code on failure.
1409 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001410int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 int output_count, int input_count,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001412 struct snd_rawmidi ** rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001414 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001416 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 .dev_free = snd_rawmidi_dev_free,
1418 .dev_register = snd_rawmidi_dev_register,
1419 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 };
1421
1422 snd_assert(rrawmidi != NULL, return -EINVAL);
1423 *rrawmidi = NULL;
1424 snd_assert(card != NULL, return -ENXIO);
Takashi Iwaica2c0962005-09-09 14:20:23 +02001425 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001426 if (rmidi == NULL) {
1427 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 rmidi->card = card;
1431 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001432 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001434 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1435 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (id != NULL)
1438 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001439 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1440 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1441 SNDRV_RAWMIDI_STREAM_INPUT,
1442 input_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 snd_rawmidi_free(rmidi);
1444 return err;
1445 }
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001446 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1447 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1448 SNDRV_RAWMIDI_STREAM_OUTPUT,
1449 output_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 snd_rawmidi_free(rmidi);
1451 return err;
1452 }
1453 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1454 snd_rawmidi_free(rmidi);
1455 return err;
1456 }
1457 *rrawmidi = rmidi;
1458 return 0;
1459}
1460
Takashi Iwai48c9d412005-11-17 13:56:51 +01001461static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001463 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001466 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 list_del(&substream->list);
1468 kfree(substream);
1469 }
1470}
1471
Takashi Iwai48c9d412005-11-17 13:56:51 +01001472static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 snd_assert(rmidi != NULL, return -ENXIO);
Takashi Iwaic4614822006-06-23 14:38:23 +02001475
1476 snd_info_free_entry(rmidi->proc_entry);
1477 rmidi->proc_entry = NULL;
1478 mutex_lock(&register_mutex);
1479 if (rmidi->ops && rmidi->ops->dev_unregister)
1480 rmidi->ops->dev_unregister(rmidi);
1481 mutex_unlock(&register_mutex);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1484 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1485 if (rmidi->private_free)
1486 rmidi->private_free(rmidi);
1487 kfree(rmidi);
1488 return 0;
1489}
1490
Takashi Iwai48c9d412005-11-17 13:56:51 +01001491static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001493 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return snd_rawmidi_free(rmidi);
1495}
1496
1497#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
Takashi Iwai48c9d412005-11-17 13:56:51 +01001498static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001500 struct snd_rawmidi *rmidi = device->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 rmidi->seq_dev = NULL;
1502}
1503#endif
1504
Takashi Iwai48c9d412005-11-17 13:56:51 +01001505static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001507 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001508 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001510 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1513 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001514 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001515 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001516 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return -EBUSY;
1518 }
Clemens Ladischf87135f2005-11-20 14:06:59 +01001519 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1521 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1522 rmidi->card, rmidi->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001523 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001525 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001526 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return err;
1528 }
1529 if (rmidi->ops && rmidi->ops->dev_register &&
1530 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1531 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001532 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001533 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return err;
1535 }
1536#ifdef CONFIG_SND_OSSEMUL
1537 rmidi->ossreg = 0;
1538 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1539 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001540 rmidi->card, 0, &snd_rawmidi_f_ops,
1541 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1543 } else {
1544 rmidi->ossreg++;
1545#ifdef SNDRV_OSS_INFO_DEV_MIDI
1546 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1547#endif
1548 }
1549 }
1550 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1551 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001552 rmidi->card, 1, &snd_rawmidi_f_ops,
1553 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1555 } else {
1556 rmidi->ossreg++;
1557 }
1558 }
1559#endif /* CONFIG_SND_OSSEMUL */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001560 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 sprintf(name, "midi%d", rmidi->device);
1562 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1563 if (entry) {
1564 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 entry->c.text.read = snd_rawmidi_proc_info_read;
1566 if (snd_info_register(entry) < 0) {
1567 snd_info_free_entry(entry);
1568 entry = NULL;
1569 }
1570 }
1571 rmidi->proc_entry = entry;
1572#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1573 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1574 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1575 rmidi->seq_dev->private_data = rmidi;
1576 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1577 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1578 snd_device_register(rmidi->card, rmidi->seq_dev);
1579 }
1580 }
1581#endif
1582 return 0;
1583}
1584
Takashi Iwai48c9d412005-11-17 13:56:51 +01001585static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001587 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001589 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001590 list_del_init(&rmidi->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591#ifdef CONFIG_SND_OSSEMUL
1592 if (rmidi->ossreg) {
1593 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1594 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1595#ifdef SNDRV_OSS_INFO_DEV_MIDI
1596 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1597#endif
1598 }
1599 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1600 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1601 rmidi->ossreg = 0;
1602 }
1603#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001605 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
1609/**
1610 * snd_rawmidi_set_ops - set the rawmidi operators
1611 * @rmidi: the rawmidi instance
1612 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1613 * @ops: the operator table
1614 *
1615 * Sets the rawmidi operators for the given stream direction.
1616 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001617void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1618 struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001620 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Johannes Berg9244b2c2006-10-05 16:02:22 +02001622 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
1626/*
1627 * ENTRY functions
1628 */
1629
1630static int __init alsa_rawmidi_init(void)
1631{
1632
1633 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1634 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1635#ifdef CONFIG_SND_OSSEMUL
1636 { int i;
1637 /* check device map table */
1638 for (i = 0; i < SNDRV_CARDS; i++) {
1639 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1640 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1641 midi_map[i] = 0;
1642 }
1643 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1644 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1645 amidi_map[i] = 1;
1646 }
1647 }
1648 }
1649#endif /* CONFIG_SND_OSSEMUL */
1650 return 0;
1651}
1652
1653static void __exit alsa_rawmidi_exit(void)
1654{
1655 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1656 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1657}
1658
1659module_init(alsa_rawmidi_init)
1660module_exit(alsa_rawmidi_exit)
1661
1662EXPORT_SYMBOL(snd_rawmidi_output_params);
1663EXPORT_SYMBOL(snd_rawmidi_input_params);
1664EXPORT_SYMBOL(snd_rawmidi_drop_output);
1665EXPORT_SYMBOL(snd_rawmidi_drain_output);
1666EXPORT_SYMBOL(snd_rawmidi_drain_input);
1667EXPORT_SYMBOL(snd_rawmidi_receive);
1668EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1669EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1670EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1671EXPORT_SYMBOL(snd_rawmidi_transmit);
1672EXPORT_SYMBOL(snd_rawmidi_new);
1673EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674EXPORT_SYMBOL(snd_rawmidi_info_select);
1675EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1676EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1677EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1678EXPORT_SYMBOL(snd_rawmidi_kernel_write);