blob: 8a2bdfae63e39968fde086198f0e84243e2aaf62 [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{
64 struct list_head *p;
65 struct snd_rawmidi *rawmidi;
66
67 list_for_each(p, &snd_rawmidi_devices) {
68 rawmidi = list_entry(p, struct snd_rawmidi, list);
69 if (rawmidi->card == card && rawmidi->device == device)
70 return rawmidi;
71 }
72 return NULL;
73}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static inline unsigned short snd_rawmidi_file_flags(struct file *file)
76{
77 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
78 case FMODE_WRITE:
79 return SNDRV_RAWMIDI_LFLG_OUTPUT;
80 case FMODE_READ:
81 return SNDRV_RAWMIDI_LFLG_INPUT;
82 default:
83 return SNDRV_RAWMIDI_LFLG_OPEN;
84 }
85}
86
Takashi Iwai48c9d412005-11-17 13:56:51 +010087static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Takashi Iwai48c9d412005-11-17 13:56:51 +010089 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return runtime->avail >= runtime->avail_min;
91}
92
Takashi Iwai48c9d412005-11-17 13:56:51 +010093static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
94 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Takashi Iwai48c9d412005-11-17 13:56:51 +010096 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return runtime->avail >= runtime->avail_min &&
98 (!substream->append || runtime->avail >= count);
99}
100
101static void snd_rawmidi_input_event_tasklet(unsigned long data)
102{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100103 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 substream->runtime->event(substream);
105}
106
107static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
108{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100109 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 substream->ops->trigger(substream, 1);
111}
112
Takashi Iwai48c9d412005-11-17 13:56:51 +0100113static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100115 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Takashi Iwaica2c0962005-09-09 14:20:23 +0200117 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return -ENOMEM;
119 spin_lock_init(&runtime->lock);
120 init_waitqueue_head(&runtime->sleep);
121 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
122 tasklet_init(&runtime->tasklet,
123 snd_rawmidi_input_event_tasklet,
124 (unsigned long)substream);
125 else
126 tasklet_init(&runtime->tasklet,
127 snd_rawmidi_output_trigger_tasklet,
128 (unsigned long)substream);
129 runtime->event = NULL;
130 runtime->buffer_size = PAGE_SIZE;
131 runtime->avail_min = 1;
132 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
133 runtime->avail = 0;
134 else
135 runtime->avail = runtime->buffer_size;
136 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
137 kfree(runtime);
138 return -ENOMEM;
139 }
140 runtime->appl_ptr = runtime->hw_ptr = 0;
141 substream->runtime = runtime;
142 return 0;
143}
144
Takashi Iwai48c9d412005-11-17 13:56:51 +0100145static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100147 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 kfree(runtime->buffer);
150 kfree(runtime);
151 substream->runtime = NULL;
152 return 0;
153}
154
Takashi Iwai48c9d412005-11-17 13:56:51 +0100155static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 if (up) {
158 tasklet_hi_schedule(&substream->runtime->tasklet);
159 } else {
160 tasklet_kill(&substream->runtime->tasklet);
161 substream->ops->trigger(substream, 0);
162 }
163}
164
Takashi Iwai48c9d412005-11-17 13:56:51 +0100165static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 substream->ops->trigger(substream, up);
168 if (!up && substream->runtime->event)
169 tasklet_kill(&substream->runtime->tasklet);
170}
171
Takashi Iwai48c9d412005-11-17 13:56:51 +0100172int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100175 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 snd_rawmidi_output_trigger(substream, 0);
178 runtime->drain = 0;
179 spin_lock_irqsave(&runtime->lock, flags);
180 runtime->appl_ptr = runtime->hw_ptr = 0;
181 runtime->avail = runtime->buffer_size;
182 spin_unlock_irqrestore(&runtime->lock, flags);
183 return 0;
184}
185
Takashi Iwai48c9d412005-11-17 13:56:51 +0100186int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int err;
189 long timeout;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100190 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 err = 0;
193 runtime->drain = 1;
194 timeout = wait_event_interruptible_timeout(runtime->sleep,
195 (runtime->avail >= runtime->buffer_size),
196 10*HZ);
197 if (signal_pending(current))
198 err = -ERESTARTSYS;
199 if (runtime->avail < runtime->buffer_size && !timeout) {
200 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
201 err = -EIO;
202 }
203 runtime->drain = 0;
204 if (err != -ERESTARTSYS) {
205 /* we need wait a while to make sure that Tx FIFOs are empty */
206 if (substream->ops->drain)
207 substream->ops->drain(substream);
208 else
209 msleep(50);
210 snd_rawmidi_drop_output(substream);
211 }
212 return err;
213}
214
Takashi Iwai48c9d412005-11-17 13:56:51 +0100215int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100218 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 snd_rawmidi_input_trigger(substream, 0);
221 runtime->drain = 0;
222 spin_lock_irqsave(&runtime->lock, flags);
223 runtime->appl_ptr = runtime->hw_ptr = 0;
224 runtime->avail = 0;
225 spin_unlock_irqrestore(&runtime->lock, flags);
226 return 0;
227}
228
Clemens Ladischf87135f2005-11-20 14:06:59 +0100229int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai48c9d412005-11-17 13:56:51 +0100230 int mode, struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100232 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct list_head *list1, *list2;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100234 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
235 struct snd_rawmidi_runtime *input = NULL, *output = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 int err;
237
238 if (rfile)
239 rfile->input = rfile->output = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100240 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100241 rmidi = snd_rawmidi_search(card, device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100242 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (rmidi == NULL) {
244 err = -ENODEV;
245 goto __error1;
246 }
247 if (!try_module_get(rmidi->card->module)) {
248 err = -EFAULT;
249 goto __error1;
250 }
251 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100252 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
254 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
255 err = -ENXIO;
256 goto __error;
257 }
258 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
259 err = -ENODEV;
260 goto __error;
261 }
262 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
263 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
264 err = -EAGAIN;
265 goto __error;
266 }
267 }
268 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
269 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
270 err = -ENXIO;
271 goto __error;
272 }
273 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
274 err = -ENODEV;
275 goto __error;
276 }
277 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
278 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
279 err = -EAGAIN;
280 goto __error;
281 }
282 }
283 list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
284 while (1) {
285 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
286 sinput = NULL;
287 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
288 err = -EAGAIN;
289 goto __error;
290 }
291 break;
292 }
Takashi Iwai48c9d412005-11-17 13:56:51 +0100293 sinput = list_entry(list1, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
295 goto __nexti;
296 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
297 break;
298 __nexti:
299 list1 = list1->next;
300 }
301 list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
302 while (1) {
303 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
304 soutput = NULL;
305 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
306 err = -EAGAIN;
307 goto __error;
308 }
309 break;
310 }
Takashi Iwai48c9d412005-11-17 13:56:51 +0100311 soutput = list_entry(list2, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
313 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
314 if (soutput->opened && !soutput->append)
315 goto __nexto;
316 } else {
317 if (soutput->opened)
318 goto __nexto;
319 }
320 }
321 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
322 break;
323 __nexto:
324 list2 = list2->next;
325 }
326 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
327 if ((err = snd_rawmidi_runtime_create(sinput)) < 0)
328 goto __error;
329 input = sinput->runtime;
330 if ((err = sinput->ops->open(sinput)) < 0)
331 goto __error;
332 sinput->opened = 1;
333 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
334 } else {
335 sinput = NULL;
336 }
337 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
338 if (soutput->opened)
339 goto __skip_output;
340 if ((err = snd_rawmidi_runtime_create(soutput)) < 0) {
341 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
342 sinput->ops->close(sinput);
343 goto __error;
344 }
345 output = soutput->runtime;
346 if ((err = soutput->ops->open(soutput)) < 0) {
347 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
348 sinput->ops->close(sinput);
349 goto __error;
350 }
351 __skip_output:
352 soutput->opened = 1;
353 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
354 soutput->append = 1;
355 if (soutput->use_count++ == 0)
356 soutput->active_sensing = 1;
357 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
358 } else {
359 soutput = NULL;
360 }
361 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100362 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (rfile) {
364 rfile->rmidi = rmidi;
365 rfile->input = sinput;
366 rfile->output = soutput;
367 }
368 return 0;
369
370 __error:
371 if (input != NULL)
372 snd_rawmidi_runtime_free(sinput);
373 if (output != NULL)
374 snd_rawmidi_runtime_free(soutput);
375 module_put(rmidi->card->module);
376 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100377 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 __error1:
379 return err;
380}
381
382static int snd_rawmidi_open(struct inode *inode, struct file *file)
383{
384 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100385 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100386 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 unsigned short fflags;
388 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100389 struct snd_rawmidi *rmidi;
390 struct snd_rawmidi_file *rawmidi_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 wait_queue_t wait;
392 struct list_head *list;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100393 struct snd_ctl_file *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Clemens Ladischf1902862005-10-24 17:05:03 +0200395 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100396 rmidi = snd_lookup_minor_data(iminor(inode),
397 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200399 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100400 rmidi = snd_lookup_oss_minor_data(iminor(inode),
401 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200403 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (rmidi == NULL)
407 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
409 return -EINVAL; /* invalid combination */
410 card = rmidi->card;
411 err = snd_card_file_add(card, file);
412 if (err < 0)
413 return -ENODEV;
414 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200415 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
417 fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
418 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
419 if (rawmidi_file == NULL) {
420 snd_card_file_remove(card, file);
421 return -ENOMEM;
422 }
423 init_waitqueue_entry(&wait, current);
424 add_wait_queue(&rmidi->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100425 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 while (1) {
427 subdevice = -1;
428 down_read(&card->controls_rwsem);
429 list_for_each(list, &card->ctl_files) {
430 kctl = snd_ctl_file(list);
431 if (kctl->pid == current->pid) {
432 subdevice = kctl->prefer_rawmidi_subdevice;
433 break;
434 }
435 }
436 up_read(&card->controls_rwsem);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100437 err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
438 subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (err >= 0)
440 break;
441 if (err == -EAGAIN) {
442 if (file->f_flags & O_NONBLOCK) {
443 err = -EBUSY;
444 break;
445 }
446 } else
447 break;
448 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100449 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100451 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (signal_pending(current)) {
453 err = -ERESTARTSYS;
454 break;
455 }
456 }
457#ifdef CONFIG_SND_OSSEMUL
458 if (rawmidi_file->input && rawmidi_file->input->runtime)
459 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
460 if (rawmidi_file->output && rawmidi_file->output->runtime)
461 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
462#endif
463 remove_wait_queue(&rmidi->open_wait, &wait);
464 if (err >= 0) {
465 file->private_data = rawmidi_file;
466 } else {
467 snd_card_file_remove(card, file);
468 kfree(rawmidi_file);
469 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100470 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return err;
472}
473
Takashi Iwai48c9d412005-11-17 13:56:51 +0100474int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100476 struct snd_rawmidi *rmidi;
477 struct snd_rawmidi_substream *substream;
478 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 snd_assert(rfile != NULL, return -ENXIO);
481 snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
482 rmidi = rfile->rmidi;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100483 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (rfile->input != NULL) {
485 substream = rfile->input;
486 rfile->input = NULL;
487 runtime = substream->runtime;
488 snd_rawmidi_input_trigger(substream, 0);
489 substream->ops->close(substream);
490 if (runtime->private_free != NULL)
491 runtime->private_free(substream);
492 snd_rawmidi_runtime_free(substream);
493 substream->opened = 0;
494 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
495 }
496 if (rfile->output != NULL) {
497 substream = rfile->output;
498 rfile->output = NULL;
499 if (--substream->use_count == 0) {
500 runtime = substream->runtime;
501 if (substream->active_sensing) {
502 unsigned char buf = 0xfe;
503 /* sending single active sensing message to shut the device up */
504 snd_rawmidi_kernel_write(substream, &buf, 1);
505 }
506 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
507 snd_rawmidi_output_trigger(substream, 0);
508 substream->ops->close(substream);
509 if (runtime->private_free != NULL)
510 runtime->private_free(substream);
511 snd_rawmidi_runtime_free(substream);
512 substream->opened = 0;
513 substream->append = 0;
514 }
515 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
516 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100517 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 module_put(rmidi->card->module);
519 return 0;
520}
521
522static int snd_rawmidi_release(struct inode *inode, struct file *file)
523{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100524 struct snd_rawmidi_file *rfile;
525 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int err;
527
528 rfile = file->private_data;
529 err = snd_rawmidi_kernel_release(rfile);
530 rmidi = rfile->rmidi;
531 wake_up(&rmidi->open_wait);
532 kfree(rfile);
533 snd_card_file_remove(rmidi->card, file);
534 return err;
535}
536
Adrian Bunk18612042005-11-23 13:14:50 +0100537static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
538 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100540 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 if (substream == NULL)
543 return -ENODEV;
544 rmidi = substream->rmidi;
545 memset(info, 0, sizeof(*info));
546 info->card = rmidi->card->number;
547 info->device = rmidi->device;
548 info->subdevice = substream->number;
549 info->stream = substream->stream;
550 info->flags = rmidi->info_flags;
551 strcpy(info->id, rmidi->id);
552 strcpy(info->name, rmidi->name);
553 strcpy(info->subname, substream->name);
554 info->subdevices_count = substream->pstr->substream_count;
555 info->subdevices_avail = (substream->pstr->substream_count -
556 substream->pstr->substream_opened);
557 return 0;
558}
559
Takashi Iwai48c9d412005-11-17 13:56:51 +0100560static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
561 struct snd_rawmidi_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100563 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int err;
565 if ((err = snd_rawmidi_info(substream, &info)) < 0)
566 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100567 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return -EFAULT;
569 return 0;
570}
571
Takashi Iwai48c9d412005-11-17 13:56:51 +0100572int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100574 struct snd_rawmidi *rmidi;
575 struct snd_rawmidi_str *pstr;
576 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct list_head *list;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100578
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100579 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100580 rmidi = snd_rawmidi_search(card, info->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100581 mutex_unlock(&register_mutex);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100582 if (!rmidi)
583 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (info->stream < 0 || info->stream > 1)
585 return -EINVAL;
586 pstr = &rmidi->streams[info->stream];
587 if (pstr->substream_count == 0)
588 return -ENOENT;
589 if (info->subdevice >= pstr->substream_count)
590 return -ENXIO;
591 list_for_each(list, &pstr->substreams) {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100592 substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if ((unsigned int)substream->number == info->subdevice)
594 return snd_rawmidi_info(substream, info);
595 }
596 return -ENXIO;
597}
598
Takashi Iwai48c9d412005-11-17 13:56:51 +0100599static int snd_rawmidi_info_select_user(struct snd_card *card,
600 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100603 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (get_user(info.device, &_info->device))
605 return -EFAULT;
606 if (get_user(info.stream, &_info->stream))
607 return -EFAULT;
608 if (get_user(info.subdevice, &_info->subdevice))
609 return -EFAULT;
610 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
611 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100612 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return -EFAULT;
614 return 0;
615}
616
Takashi Iwai48c9d412005-11-17 13:56:51 +0100617int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
618 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100621 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (substream->append && substream->use_count > 1)
624 return -EBUSY;
625 snd_rawmidi_drain_output(substream);
626 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
627 return -EINVAL;
628 }
629 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
630 return -EINVAL;
631 }
632 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800633 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
634 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -ENOMEM;
636 kfree(runtime->buffer);
637 runtime->buffer = newbuf;
638 runtime->buffer_size = params->buffer_size;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100639 runtime->avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 runtime->avail_min = params->avail_min;
642 substream->active_sensing = !params->no_active_sensing;
643 return 0;
644}
645
Takashi Iwai48c9d412005-11-17 13:56:51 +0100646int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
647 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100650 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 snd_rawmidi_drain_input(substream);
653 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
654 return -EINVAL;
655 }
656 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
657 return -EINVAL;
658 }
659 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800660 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
661 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return -ENOMEM;
663 kfree(runtime->buffer);
664 runtime->buffer = newbuf;
665 runtime->buffer_size = params->buffer_size;
666 }
667 runtime->avail_min = params->avail_min;
668 return 0;
669}
670
Takashi Iwai48c9d412005-11-17 13:56:51 +0100671static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
672 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100674 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 memset(status, 0, sizeof(*status));
677 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
678 spin_lock_irq(&runtime->lock);
679 status->avail = runtime->avail;
680 spin_unlock_irq(&runtime->lock);
681 return 0;
682}
683
Takashi Iwai48c9d412005-11-17 13:56:51 +0100684static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
685 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100687 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 memset(status, 0, sizeof(*status));
690 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
691 spin_lock_irq(&runtime->lock);
692 status->avail = runtime->avail;
693 status->xruns = runtime->xruns;
694 runtime->xruns = 0;
695 spin_unlock_irq(&runtime->lock);
696 return 0;
697}
698
699static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
700{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100701 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 void __user *argp = (void __user *)arg;
703
704 rfile = file->private_data;
705 if (((cmd >> 8) & 0xff) != 'W')
706 return -ENOTTY;
707 switch (cmd) {
708 case SNDRV_RAWMIDI_IOCTL_PVERSION:
709 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
710 case SNDRV_RAWMIDI_IOCTL_INFO:
711 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100712 int stream;
713 struct snd_rawmidi_info __user *info = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (get_user(stream, &info->stream))
715 return -EFAULT;
716 switch (stream) {
717 case SNDRV_RAWMIDI_STREAM_INPUT:
718 return snd_rawmidi_info_user(rfile->input, info);
719 case SNDRV_RAWMIDI_STREAM_OUTPUT:
720 return snd_rawmidi_info_user(rfile->output, info);
721 default:
722 return -EINVAL;
723 }
724 }
725 case SNDRV_RAWMIDI_IOCTL_PARAMS:
726 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100727 struct snd_rawmidi_params params;
728 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return -EFAULT;
730 switch (params.stream) {
731 case SNDRV_RAWMIDI_STREAM_OUTPUT:
732 if (rfile->output == NULL)
733 return -EINVAL;
734 return snd_rawmidi_output_params(rfile->output, &params);
735 case SNDRV_RAWMIDI_STREAM_INPUT:
736 if (rfile->input == NULL)
737 return -EINVAL;
738 return snd_rawmidi_input_params(rfile->input, &params);
739 default:
740 return -EINVAL;
741 }
742 }
743 case SNDRV_RAWMIDI_IOCTL_STATUS:
744 {
745 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100746 struct snd_rawmidi_status status;
747 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return -EFAULT;
749 switch (status.stream) {
750 case SNDRV_RAWMIDI_STREAM_OUTPUT:
751 if (rfile->output == NULL)
752 return -EINVAL;
753 err = snd_rawmidi_output_status(rfile->output, &status);
754 break;
755 case SNDRV_RAWMIDI_STREAM_INPUT:
756 if (rfile->input == NULL)
757 return -EINVAL;
758 err = snd_rawmidi_input_status(rfile->input, &status);
759 break;
760 default:
761 return -EINVAL;
762 }
763 if (err < 0)
764 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100765 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return -EFAULT;
767 return 0;
768 }
769 case SNDRV_RAWMIDI_IOCTL_DROP:
770 {
771 int val;
772 if (get_user(val, (int __user *) argp))
773 return -EFAULT;
774 switch (val) {
775 case SNDRV_RAWMIDI_STREAM_OUTPUT:
776 if (rfile->output == NULL)
777 return -EINVAL;
778 return snd_rawmidi_drop_output(rfile->output);
779 default:
780 return -EINVAL;
781 }
782 }
783 case SNDRV_RAWMIDI_IOCTL_DRAIN:
784 {
785 int val;
786 if (get_user(val, (int __user *) argp))
787 return -EFAULT;
788 switch (val) {
789 case SNDRV_RAWMIDI_STREAM_OUTPUT:
790 if (rfile->output == NULL)
791 return -EINVAL;
792 return snd_rawmidi_drain_output(rfile->output);
793 case SNDRV_RAWMIDI_STREAM_INPUT:
794 if (rfile->input == NULL)
795 return -EINVAL;
796 return snd_rawmidi_drain_input(rfile->input);
797 default:
798 return -EINVAL;
799 }
800 }
801#ifdef CONFIG_SND_DEBUG
802 default:
803 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
804#endif
805 }
806 return -ENOTTY;
807}
808
Takashi Iwai48c9d412005-11-17 13:56:51 +0100809static int snd_rawmidi_control_ioctl(struct snd_card *card,
810 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 unsigned int cmd,
812 unsigned long arg)
813{
814 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 switch (cmd) {
817 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
818 {
819 int device;
820
821 if (get_user(device, (int __user *)argp))
822 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100823 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 device = device < 0 ? 0 : device + 1;
825 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100826 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 break;
828 device++;
829 }
830 if (device == SNDRV_RAWMIDI_DEVICES)
831 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100832 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (put_user(device, (int __user *)argp))
834 return -EFAULT;
835 return 0;
836 }
837 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
838 {
839 int val;
840
841 if (get_user(val, (int __user *)argp))
842 return -EFAULT;
843 control->prefer_rawmidi_subdevice = val;
844 return 0;
845 }
846 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
847 return snd_rawmidi_info_select_user(card, argp);
848 }
849 return -ENOIOCTLCMD;
850}
851
852/**
853 * snd_rawmidi_receive - receive the input data from the device
854 * @substream: the rawmidi substream
855 * @buffer: the buffer pointer
856 * @count: the data size to read
857 *
858 * Reads the data from the internal buffer.
859 *
860 * Returns the size of read data, or a negative error code on failure.
861 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100862int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
863 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 unsigned long flags;
866 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100867 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 if (runtime->buffer == NULL) {
870 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
871 return -EINVAL;
872 }
873 spin_lock_irqsave(&runtime->lock, flags);
874 if (count == 1) { /* special case, faster code */
875 substream->bytes++;
876 if (runtime->avail < runtime->buffer_size) {
877 runtime->buffer[runtime->hw_ptr++] = buffer[0];
878 runtime->hw_ptr %= runtime->buffer_size;
879 runtime->avail++;
880 result++;
881 } else {
882 runtime->xruns++;
883 }
884 } else {
885 substream->bytes += count;
886 count1 = runtime->buffer_size - runtime->hw_ptr;
887 if (count1 > count)
888 count1 = count;
889 if (count1 > (int)(runtime->buffer_size - runtime->avail))
890 count1 = runtime->buffer_size - runtime->avail;
891 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
892 runtime->hw_ptr += count1;
893 runtime->hw_ptr %= runtime->buffer_size;
894 runtime->avail += count1;
895 count -= count1;
896 result += count1;
897 if (count > 0) {
898 buffer += count1;
899 count1 = count;
900 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
901 count1 = runtime->buffer_size - runtime->avail;
902 runtime->xruns += count - count1;
903 }
904 if (count1 > 0) {
905 memcpy(runtime->buffer, buffer, count1);
906 runtime->hw_ptr = count1;
907 runtime->avail += count1;
908 result += count1;
909 }
910 }
911 }
912 if (result > 0) {
913 if (runtime->event)
914 tasklet_hi_schedule(&runtime->tasklet);
915 else if (snd_rawmidi_ready(substream))
916 wake_up(&runtime->sleep);
917 }
918 spin_unlock_irqrestore(&runtime->lock, flags);
919 return result;
920}
921
Takashi Iwai48c9d412005-11-17 13:56:51 +0100922static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 unsigned char *buf, long count, int kernel)
924{
925 unsigned long flags;
926 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100927 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 while (count > 0 && runtime->avail) {
930 count1 = runtime->buffer_size - runtime->appl_ptr;
931 if (count1 > count)
932 count1 = count;
933 spin_lock_irqsave(&runtime->lock, flags);
934 if (count1 > (int)runtime->avail)
935 count1 = runtime->avail;
936 if (kernel) {
937 memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
938 } else {
939 spin_unlock_irqrestore(&runtime->lock, flags);
940 if (copy_to_user((char __user *)buf + result,
941 runtime->buffer + runtime->appl_ptr, count1)) {
942 return result > 0 ? result : -EFAULT;
943 }
944 spin_lock_irqsave(&runtime->lock, flags);
945 }
946 runtime->appl_ptr += count1;
947 runtime->appl_ptr %= runtime->buffer_size;
948 runtime->avail -= count1;
949 spin_unlock_irqrestore(&runtime->lock, flags);
950 result += count1;
951 count -= count1;
952 }
953 return result;
954}
955
Takashi Iwai48c9d412005-11-17 13:56:51 +0100956long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
957 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 snd_rawmidi_input_trigger(substream, 1);
960 return snd_rawmidi_kernel_read1(substream, buf, count, 1);
961}
962
Takashi Iwai48c9d412005-11-17 13:56:51 +0100963static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
964 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 long result;
967 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100968 struct snd_rawmidi_file *rfile;
969 struct snd_rawmidi_substream *substream;
970 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 rfile = file->private_data;
973 substream = rfile->input;
974 if (substream == NULL)
975 return -EIO;
976 runtime = substream->runtime;
977 snd_rawmidi_input_trigger(substream, 1);
978 result = 0;
979 while (count > 0) {
980 spin_lock_irq(&runtime->lock);
981 while (!snd_rawmidi_ready(substream)) {
982 wait_queue_t wait;
983 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
984 spin_unlock_irq(&runtime->lock);
985 return result > 0 ? result : -EAGAIN;
986 }
987 init_waitqueue_entry(&wait, current);
988 add_wait_queue(&runtime->sleep, &wait);
989 set_current_state(TASK_INTERRUPTIBLE);
990 spin_unlock_irq(&runtime->lock);
991 schedule();
992 remove_wait_queue(&runtime->sleep, &wait);
993 if (signal_pending(current))
994 return result > 0 ? result : -ERESTARTSYS;
995 if (!runtime->avail)
996 return result > 0 ? result : -EIO;
997 spin_lock_irq(&runtime->lock);
998 }
999 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001000 count1 = snd_rawmidi_kernel_read1(substream,
1001 (unsigned char __force *)buf,
1002 count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (count1 < 0)
1004 return result > 0 ? result : count1;
1005 result += count1;
1006 buf += count1;
1007 count -= count1;
1008 }
1009 return result;
1010}
1011
1012/**
1013 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1014 * @substream: the rawmidi substream
1015 *
1016 * Returns 1 if the internal output buffer is empty, 0 if not.
1017 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001018int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001020 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 int result;
1022 unsigned long flags;
1023
1024 if (runtime->buffer == NULL) {
1025 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1026 return 1;
1027 }
1028 spin_lock_irqsave(&runtime->lock, flags);
1029 result = runtime->avail >= runtime->buffer_size;
1030 spin_unlock_irqrestore(&runtime->lock, flags);
1031 return result;
1032}
1033
1034/**
1035 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1036 * @substream: the rawmidi substream
1037 * @buffer: the buffer pointer
1038 * @count: data size to transfer
1039 *
1040 * Copies data from the internal output buffer to the given buffer.
1041 *
1042 * Call this in the interrupt handler when the midi output is ready,
1043 * and call snd_rawmidi_transmit_ack() after the transmission is
1044 * finished.
1045 *
1046 * Returns the size of copied data, or a negative error code on failure.
1047 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001048int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1049 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
1051 unsigned long flags;
1052 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001053 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 if (runtime->buffer == NULL) {
1056 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1057 return -EINVAL;
1058 }
1059 result = 0;
1060 spin_lock_irqsave(&runtime->lock, flags);
1061 if (runtime->avail >= runtime->buffer_size) {
1062 /* warning: lowlevel layer MUST trigger down the hardware */
1063 goto __skip;
1064 }
1065 if (count == 1) { /* special case, faster code */
1066 *buffer = runtime->buffer[runtime->hw_ptr];
1067 result++;
1068 } else {
1069 count1 = runtime->buffer_size - runtime->hw_ptr;
1070 if (count1 > count)
1071 count1 = count;
1072 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1073 count1 = runtime->buffer_size - runtime->avail;
1074 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1075 count -= count1;
1076 result += count1;
1077 if (count > 0) {
1078 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1079 count = runtime->buffer_size - runtime->avail - count1;
1080 memcpy(buffer + count1, runtime->buffer, count);
1081 result += count;
1082 }
1083 }
1084 __skip:
1085 spin_unlock_irqrestore(&runtime->lock, flags);
1086 return result;
1087}
1088
1089/**
1090 * snd_rawmidi_transmit_ack - acknowledge the transmission
1091 * @substream: the rawmidi substream
1092 * @count: the tranferred count
1093 *
1094 * Advances the hardware pointer for the internal output buffer with
1095 * the given size and updates the condition.
1096 * Call after the transmission is finished.
1097 *
1098 * Returns the advanced size if successful, or a negative error code on failure.
1099 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001100int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001103 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 if (runtime->buffer == NULL) {
1106 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1107 return -EINVAL;
1108 }
1109 spin_lock_irqsave(&runtime->lock, flags);
1110 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1111 runtime->hw_ptr += count;
1112 runtime->hw_ptr %= runtime->buffer_size;
1113 runtime->avail += count;
1114 substream->bytes += count;
1115 if (count > 0) {
1116 if (runtime->drain || snd_rawmidi_ready(substream))
1117 wake_up(&runtime->sleep);
1118 }
1119 spin_unlock_irqrestore(&runtime->lock, flags);
1120 return count;
1121}
1122
1123/**
1124 * snd_rawmidi_transmit - copy from the buffer to the device
1125 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001126 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 * @count: the data size to transfer
1128 *
1129 * Copies data from the buffer to the device and advances the pointer.
1130 *
1131 * Returns the copied size if successful, or a negative error code on failure.
1132 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001133int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1134 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1137 if (count < 0)
1138 return count;
1139 return snd_rawmidi_transmit_ack(substream, count);
1140}
1141
Takashi Iwai48c9d412005-11-17 13:56:51 +01001142static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1143 const unsigned char *buf, long count, int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 unsigned long flags;
1146 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001147 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 snd_assert(buf != NULL, return -EINVAL);
1150 snd_assert(runtime->buffer != NULL, return -EINVAL);
1151
1152 result = 0;
1153 spin_lock_irqsave(&runtime->lock, flags);
1154 if (substream->append) {
1155 if ((long)runtime->avail < count) {
1156 spin_unlock_irqrestore(&runtime->lock, flags);
1157 return -EAGAIN;
1158 }
1159 }
1160 while (count > 0 && runtime->avail > 0) {
1161 count1 = runtime->buffer_size - runtime->appl_ptr;
1162 if (count1 > count)
1163 count1 = count;
1164 if (count1 > (long)runtime->avail)
1165 count1 = runtime->avail;
1166 if (kernel) {
1167 memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1168 } else {
1169 spin_unlock_irqrestore(&runtime->lock, flags);
1170 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1171 (char __user *)buf, count1)) {
1172 spin_lock_irqsave(&runtime->lock, flags);
1173 result = result > 0 ? result : -EFAULT;
1174 goto __end;
1175 }
1176 spin_lock_irqsave(&runtime->lock, flags);
1177 }
1178 runtime->appl_ptr += count1;
1179 runtime->appl_ptr %= runtime->buffer_size;
1180 runtime->avail -= count1;
1181 result += count1;
1182 buf += count1;
1183 count -= count1;
1184 }
1185 __end:
1186 count1 = runtime->avail < runtime->buffer_size;
1187 spin_unlock_irqrestore(&runtime->lock, flags);
1188 if (count1)
1189 snd_rawmidi_output_trigger(substream, 1);
1190 return result;
1191}
1192
Takashi Iwai48c9d412005-11-17 13:56:51 +01001193long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1194 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1197}
1198
Takashi Iwai48c9d412005-11-17 13:56:51 +01001199static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1200 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 long result, timeout;
1203 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001204 struct snd_rawmidi_file *rfile;
1205 struct snd_rawmidi_runtime *runtime;
1206 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 rfile = file->private_data;
1209 substream = rfile->output;
1210 runtime = substream->runtime;
1211 /* we cannot put an atomic message to our buffer */
1212 if (substream->append && count > runtime->buffer_size)
1213 return -EIO;
1214 result = 0;
1215 while (count > 0) {
1216 spin_lock_irq(&runtime->lock);
1217 while (!snd_rawmidi_ready_append(substream, count)) {
1218 wait_queue_t wait;
1219 if (file->f_flags & O_NONBLOCK) {
1220 spin_unlock_irq(&runtime->lock);
1221 return result > 0 ? result : -EAGAIN;
1222 }
1223 init_waitqueue_entry(&wait, current);
1224 add_wait_queue(&runtime->sleep, &wait);
1225 set_current_state(TASK_INTERRUPTIBLE);
1226 spin_unlock_irq(&runtime->lock);
1227 timeout = schedule_timeout(30 * HZ);
1228 remove_wait_queue(&runtime->sleep, &wait);
1229 if (signal_pending(current))
1230 return result > 0 ? result : -ERESTARTSYS;
1231 if (!runtime->avail && !timeout)
1232 return result > 0 ? result : -EIO;
1233 spin_lock_irq(&runtime->lock);
1234 }
1235 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001236 count1 = snd_rawmidi_kernel_write1(substream,
1237 (unsigned char __force *)buf,
1238 count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (count1 < 0)
1240 return result > 0 ? result : count1;
1241 result += count1;
1242 buf += count1;
1243 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1244 break;
1245 count -= count1;
1246 }
1247 if (file->f_flags & O_SYNC) {
1248 spin_lock_irq(&runtime->lock);
1249 while (runtime->avail != runtime->buffer_size) {
1250 wait_queue_t wait;
1251 unsigned int last_avail = runtime->avail;
1252 init_waitqueue_entry(&wait, current);
1253 add_wait_queue(&runtime->sleep, &wait);
1254 set_current_state(TASK_INTERRUPTIBLE);
1255 spin_unlock_irq(&runtime->lock);
1256 timeout = schedule_timeout(30 * HZ);
1257 remove_wait_queue(&runtime->sleep, &wait);
1258 if (signal_pending(current))
1259 return result > 0 ? result : -ERESTARTSYS;
1260 if (runtime->avail == last_avail && !timeout)
1261 return result > 0 ? result : -EIO;
1262 spin_lock_irq(&runtime->lock);
1263 }
1264 spin_unlock_irq(&runtime->lock);
1265 }
1266 return result;
1267}
1268
1269static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1270{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001271 struct snd_rawmidi_file *rfile;
1272 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 unsigned int mask;
1274
1275 rfile = file->private_data;
1276 if (rfile->input != NULL) {
1277 runtime = rfile->input->runtime;
1278 snd_rawmidi_input_trigger(rfile->input, 1);
1279 poll_wait(file, &runtime->sleep, wait);
1280 }
1281 if (rfile->output != NULL) {
1282 runtime = rfile->output->runtime;
1283 poll_wait(file, &runtime->sleep, wait);
1284 }
1285 mask = 0;
1286 if (rfile->input != NULL) {
1287 if (snd_rawmidi_ready(rfile->input))
1288 mask |= POLLIN | POLLRDNORM;
1289 }
1290 if (rfile->output != NULL) {
1291 if (snd_rawmidi_ready(rfile->output))
1292 mask |= POLLOUT | POLLWRNORM;
1293 }
1294 return mask;
1295}
1296
1297/*
1298 */
1299#ifdef CONFIG_COMPAT
1300#include "rawmidi_compat.c"
1301#else
1302#define snd_rawmidi_ioctl_compat NULL
1303#endif
1304
1305/*
1306
1307 */
1308
Takashi Iwai48c9d412005-11-17 13:56:51 +01001309static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1310 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001312 struct snd_rawmidi *rmidi;
1313 struct snd_rawmidi_substream *substream;
1314 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 struct list_head *list;
1316
1317 rmidi = entry->private_data;
1318 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001319 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1321 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001322 substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 snd_iprintf(buffer,
1324 "Output %d\n"
1325 " Tx bytes : %lu\n",
1326 substream->number,
1327 (unsigned long) substream->bytes);
1328 if (substream->opened) {
1329 runtime = substream->runtime;
1330 snd_iprintf(buffer,
1331 " Mode : %s\n"
1332 " Buffer size : %lu\n"
1333 " Avail : %lu\n",
1334 runtime->oss ? "OSS compatible" : "native",
1335 (unsigned long) runtime->buffer_size,
1336 (unsigned long) runtime->avail);
1337 }
1338 }
1339 }
1340 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1341 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001342 substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 snd_iprintf(buffer,
1344 "Input %d\n"
1345 " Rx bytes : %lu\n",
1346 substream->number,
1347 (unsigned long) substream->bytes);
1348 if (substream->opened) {
1349 runtime = substream->runtime;
1350 snd_iprintf(buffer,
1351 " Buffer size : %lu\n"
1352 " Avail : %lu\n"
1353 " Overruns : %lu\n",
1354 (unsigned long) runtime->buffer_size,
1355 (unsigned long) runtime->avail,
1356 (unsigned long) runtime->xruns);
1357 }
1358 }
1359 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001360 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363/*
1364 * Register functions
1365 */
1366
1367static struct file_operations snd_rawmidi_f_ops =
1368{
1369 .owner = THIS_MODULE,
1370 .read = snd_rawmidi_read,
1371 .write = snd_rawmidi_write,
1372 .open = snd_rawmidi_open,
1373 .release = snd_rawmidi_release,
1374 .poll = snd_rawmidi_poll,
1375 .unlocked_ioctl = snd_rawmidi_ioctl,
1376 .compat_ioctl = snd_rawmidi_ioctl_compat,
1377};
1378
Takashi Iwai48c9d412005-11-17 13:56:51 +01001379static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1380 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 int direction,
1382 int count)
1383{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001384 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 int idx;
1386
1387 INIT_LIST_HEAD(&stream->substreams);
1388 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001389 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001390 if (substream == NULL) {
1391 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 substream->stream = direction;
1395 substream->number = idx;
1396 substream->rmidi = rmidi;
1397 substream->pstr = stream;
1398 list_add_tail(&substream->list, &stream->substreams);
1399 stream->substream_count++;
1400 }
1401 return 0;
1402}
1403
1404/**
1405 * snd_rawmidi_new - create a rawmidi instance
1406 * @card: the card instance
1407 * @id: the id string
1408 * @device: the device index
1409 * @output_count: the number of output streams
1410 * @input_count: the number of input streams
1411 * @rrawmidi: the pointer to store the new rawmidi instance
1412 *
1413 * Creates a new rawmidi instance.
1414 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1415 *
1416 * Returns zero if successful, or a negative error code on failure.
1417 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001418int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 int output_count, int input_count,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001420 struct snd_rawmidi ** rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001422 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001424 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 .dev_free = snd_rawmidi_dev_free,
1426 .dev_register = snd_rawmidi_dev_register,
1427 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 };
1429
1430 snd_assert(rrawmidi != NULL, return -EINVAL);
1431 *rrawmidi = NULL;
1432 snd_assert(card != NULL, return -ENXIO);
Takashi Iwaica2c0962005-09-09 14:20:23 +02001433 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001434 if (rmidi == NULL) {
1435 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 rmidi->card = card;
1439 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001440 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 init_waitqueue_head(&rmidi->open_wait);
1442 if (id != NULL)
1443 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001444 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1445 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1446 SNDRV_RAWMIDI_STREAM_INPUT,
1447 input_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 snd_rawmidi_free(rmidi);
1449 return err;
1450 }
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001451 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1452 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1453 SNDRV_RAWMIDI_STREAM_OUTPUT,
1454 output_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 snd_rawmidi_free(rmidi);
1456 return err;
1457 }
1458 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1459 snd_rawmidi_free(rmidi);
1460 return err;
1461 }
1462 *rrawmidi = rmidi;
1463 return 0;
1464}
1465
Takashi Iwai48c9d412005-11-17 13:56:51 +01001466static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001468 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001471 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 list_del(&substream->list);
1473 kfree(substream);
1474 }
1475}
1476
Takashi Iwai48c9d412005-11-17 13:56:51 +01001477static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 snd_assert(rmidi != NULL, return -ENXIO);
Takashi Iwaic4614822006-06-23 14:38:23 +02001480
1481 snd_info_free_entry(rmidi->proc_entry);
1482 rmidi->proc_entry = NULL;
1483 mutex_lock(&register_mutex);
1484 if (rmidi->ops && rmidi->ops->dev_unregister)
1485 rmidi->ops->dev_unregister(rmidi);
1486 mutex_unlock(&register_mutex);
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1489 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1490 if (rmidi->private_free)
1491 rmidi->private_free(rmidi);
1492 kfree(rmidi);
1493 return 0;
1494}
1495
Takashi Iwai48c9d412005-11-17 13:56:51 +01001496static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001498 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return snd_rawmidi_free(rmidi);
1500}
1501
1502#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
Takashi Iwai48c9d412005-11-17 13:56:51 +01001503static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001505 struct snd_rawmidi *rmidi = device->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 rmidi->seq_dev = NULL;
1507}
1508#endif
1509
Takashi Iwai48c9d412005-11-17 13:56:51 +01001510static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001512 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001513 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001515 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1518 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001519 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001520 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001521 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return -EBUSY;
1523 }
Clemens Ladischf87135f2005-11-20 14:06:59 +01001524 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1526 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1527 rmidi->card, rmidi->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001528 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001530 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001531 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return err;
1533 }
1534 if (rmidi->ops && rmidi->ops->dev_register &&
1535 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1536 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001537 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001538 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return err;
1540 }
1541#ifdef CONFIG_SND_OSSEMUL
1542 rmidi->ossreg = 0;
1543 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1544 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001545 rmidi->card, 0, &snd_rawmidi_f_ops,
1546 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1548 } else {
1549 rmidi->ossreg++;
1550#ifdef SNDRV_OSS_INFO_DEV_MIDI
1551 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1552#endif
1553 }
1554 }
1555 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1556 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001557 rmidi->card, 1, &snd_rawmidi_f_ops,
1558 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1560 } else {
1561 rmidi->ossreg++;
1562 }
1563 }
1564#endif /* CONFIG_SND_OSSEMUL */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001565 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 sprintf(name, "midi%d", rmidi->device);
1567 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1568 if (entry) {
1569 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 entry->c.text.read = snd_rawmidi_proc_info_read;
1571 if (snd_info_register(entry) < 0) {
1572 snd_info_free_entry(entry);
1573 entry = NULL;
1574 }
1575 }
1576 rmidi->proc_entry = entry;
1577#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1578 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1579 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1580 rmidi->seq_dev->private_data = rmidi;
1581 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1582 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1583 snd_device_register(rmidi->card, rmidi->seq_dev);
1584 }
1585 }
1586#endif
1587 return 0;
1588}
1589
Takashi Iwai48c9d412005-11-17 13:56:51 +01001590static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001592 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001594 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001595 list_del_init(&rmidi->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596#ifdef CONFIG_SND_OSSEMUL
1597 if (rmidi->ossreg) {
1598 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1599 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1600#ifdef SNDRV_OSS_INFO_DEV_MIDI
1601 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1602#endif
1603 }
1604 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1605 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1606 rmidi->ossreg = 0;
1607 }
1608#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001610 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
1614/**
1615 * snd_rawmidi_set_ops - set the rawmidi operators
1616 * @rmidi: the rawmidi instance
1617 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1618 * @ops: the operator table
1619 *
1620 * Sets the rawmidi operators for the given stream direction.
1621 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001622void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1623 struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 struct list_head *list;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001626 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 list_for_each(list, &rmidi->streams[stream].substreams) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001629 substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 substream->ops = ops;
1631 }
1632}
1633
1634/*
1635 * ENTRY functions
1636 */
1637
1638static int __init alsa_rawmidi_init(void)
1639{
1640
1641 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1642 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1643#ifdef CONFIG_SND_OSSEMUL
1644 { int i;
1645 /* check device map table */
1646 for (i = 0; i < SNDRV_CARDS; i++) {
1647 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1648 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1649 midi_map[i] = 0;
1650 }
1651 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1652 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1653 amidi_map[i] = 1;
1654 }
1655 }
1656 }
1657#endif /* CONFIG_SND_OSSEMUL */
1658 return 0;
1659}
1660
1661static void __exit alsa_rawmidi_exit(void)
1662{
1663 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1664 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1665}
1666
1667module_init(alsa_rawmidi_init)
1668module_exit(alsa_rawmidi_exit)
1669
1670EXPORT_SYMBOL(snd_rawmidi_output_params);
1671EXPORT_SYMBOL(snd_rawmidi_input_params);
1672EXPORT_SYMBOL(snd_rawmidi_drop_output);
1673EXPORT_SYMBOL(snd_rawmidi_drain_output);
1674EXPORT_SYMBOL(snd_rawmidi_drain_input);
1675EXPORT_SYMBOL(snd_rawmidi_receive);
1676EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1677EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1678EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1679EXPORT_SYMBOL(snd_rawmidi_transmit);
1680EXPORT_SYMBOL(snd_rawmidi_new);
1681EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682EXPORT_SYMBOL(snd_rawmidi_info_select);
1683EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1684EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1685EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1686EXPORT_SYMBOL(snd_rawmidi_kernel_write);