blob: 269c467ca9bb2ba76f3c001f1d824414ccb5bd07 [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;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200433 if (subdevice != -1)
434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436 }
437 up_read(&card->controls_rwsem);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100438 err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
439 subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (err >= 0)
441 break;
442 if (err == -EAGAIN) {
443 if (file->f_flags & O_NONBLOCK) {
444 err = -EBUSY;
445 break;
446 }
447 } else
448 break;
449 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100450 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100452 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (signal_pending(current)) {
454 err = -ERESTARTSYS;
455 break;
456 }
457 }
458#ifdef CONFIG_SND_OSSEMUL
459 if (rawmidi_file->input && rawmidi_file->input->runtime)
460 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
461 if (rawmidi_file->output && rawmidi_file->output->runtime)
462 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
463#endif
464 remove_wait_queue(&rmidi->open_wait, &wait);
465 if (err >= 0) {
466 file->private_data = rawmidi_file;
467 } else {
468 snd_card_file_remove(card, file);
469 kfree(rawmidi_file);
470 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100471 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return err;
473}
474
Takashi Iwai48c9d412005-11-17 13:56:51 +0100475int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100477 struct snd_rawmidi *rmidi;
478 struct snd_rawmidi_substream *substream;
479 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 snd_assert(rfile != NULL, return -ENXIO);
482 snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
483 rmidi = rfile->rmidi;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100484 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (rfile->input != NULL) {
486 substream = rfile->input;
487 rfile->input = NULL;
488 runtime = substream->runtime;
489 snd_rawmidi_input_trigger(substream, 0);
490 substream->ops->close(substream);
491 if (runtime->private_free != NULL)
492 runtime->private_free(substream);
493 snd_rawmidi_runtime_free(substream);
494 substream->opened = 0;
495 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
496 }
497 if (rfile->output != NULL) {
498 substream = rfile->output;
499 rfile->output = NULL;
500 if (--substream->use_count == 0) {
501 runtime = substream->runtime;
502 if (substream->active_sensing) {
503 unsigned char buf = 0xfe;
504 /* sending single active sensing message to shut the device up */
505 snd_rawmidi_kernel_write(substream, &buf, 1);
506 }
507 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
508 snd_rawmidi_output_trigger(substream, 0);
509 substream->ops->close(substream);
510 if (runtime->private_free != NULL)
511 runtime->private_free(substream);
512 snd_rawmidi_runtime_free(substream);
513 substream->opened = 0;
514 substream->append = 0;
515 }
516 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
517 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100518 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 module_put(rmidi->card->module);
520 return 0;
521}
522
523static int snd_rawmidi_release(struct inode *inode, struct file *file)
524{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100525 struct snd_rawmidi_file *rfile;
526 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int err;
528
529 rfile = file->private_data;
530 err = snd_rawmidi_kernel_release(rfile);
531 rmidi = rfile->rmidi;
532 wake_up(&rmidi->open_wait);
533 kfree(rfile);
534 snd_card_file_remove(rmidi->card, file);
535 return err;
536}
537
Adrian Bunk18612042005-11-23 13:14:50 +0100538static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
539 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100541 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if (substream == NULL)
544 return -ENODEV;
545 rmidi = substream->rmidi;
546 memset(info, 0, sizeof(*info));
547 info->card = rmidi->card->number;
548 info->device = rmidi->device;
549 info->subdevice = substream->number;
550 info->stream = substream->stream;
551 info->flags = rmidi->info_flags;
552 strcpy(info->id, rmidi->id);
553 strcpy(info->name, rmidi->name);
554 strcpy(info->subname, substream->name);
555 info->subdevices_count = substream->pstr->substream_count;
556 info->subdevices_avail = (substream->pstr->substream_count -
557 substream->pstr->substream_opened);
558 return 0;
559}
560
Takashi Iwai48c9d412005-11-17 13:56:51 +0100561static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
562 struct snd_rawmidi_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100564 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int err;
566 if ((err = snd_rawmidi_info(substream, &info)) < 0)
567 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100568 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return -EFAULT;
570 return 0;
571}
572
Takashi Iwai48c9d412005-11-17 13:56:51 +0100573int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100575 struct snd_rawmidi *rmidi;
576 struct snd_rawmidi_str *pstr;
577 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct list_head *list;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100579
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100580 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100581 rmidi = snd_rawmidi_search(card, info->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100582 mutex_unlock(&register_mutex);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100583 if (!rmidi)
584 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (info->stream < 0 || info->stream > 1)
586 return -EINVAL;
587 pstr = &rmidi->streams[info->stream];
588 if (pstr->substream_count == 0)
589 return -ENOENT;
590 if (info->subdevice >= pstr->substream_count)
591 return -ENXIO;
592 list_for_each(list, &pstr->substreams) {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100593 substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if ((unsigned int)substream->number == info->subdevice)
595 return snd_rawmidi_info(substream, info);
596 }
597 return -ENXIO;
598}
599
Takashi Iwai48c9d412005-11-17 13:56:51 +0100600static int snd_rawmidi_info_select_user(struct snd_card *card,
601 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100604 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (get_user(info.device, &_info->device))
606 return -EFAULT;
607 if (get_user(info.stream, &_info->stream))
608 return -EFAULT;
609 if (get_user(info.subdevice, &_info->subdevice))
610 return -EFAULT;
611 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
612 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100613 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return -EFAULT;
615 return 0;
616}
617
Takashi Iwai48c9d412005-11-17 13:56:51 +0100618int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
619 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100622 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 if (substream->append && substream->use_count > 1)
625 return -EBUSY;
626 snd_rawmidi_drain_output(substream);
627 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
628 return -EINVAL;
629 }
630 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
631 return -EINVAL;
632 }
633 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800634 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
635 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -ENOMEM;
637 kfree(runtime->buffer);
638 runtime->buffer = newbuf;
639 runtime->buffer_size = params->buffer_size;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100640 runtime->avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642 runtime->avail_min = params->avail_min;
643 substream->active_sensing = !params->no_active_sensing;
644 return 0;
645}
646
Takashi Iwai48c9d412005-11-17 13:56:51 +0100647int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
648 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100651 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 snd_rawmidi_drain_input(substream);
654 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
655 return -EINVAL;
656 }
657 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
658 return -EINVAL;
659 }
660 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800661 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
662 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -ENOMEM;
664 kfree(runtime->buffer);
665 runtime->buffer = newbuf;
666 runtime->buffer_size = params->buffer_size;
667 }
668 runtime->avail_min = params->avail_min;
669 return 0;
670}
671
Takashi Iwai48c9d412005-11-17 13:56:51 +0100672static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
673 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100675 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 memset(status, 0, sizeof(*status));
678 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
679 spin_lock_irq(&runtime->lock);
680 status->avail = runtime->avail;
681 spin_unlock_irq(&runtime->lock);
682 return 0;
683}
684
Takashi Iwai48c9d412005-11-17 13:56:51 +0100685static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
686 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100688 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 memset(status, 0, sizeof(*status));
691 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
692 spin_lock_irq(&runtime->lock);
693 status->avail = runtime->avail;
694 status->xruns = runtime->xruns;
695 runtime->xruns = 0;
696 spin_unlock_irq(&runtime->lock);
697 return 0;
698}
699
700static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
701{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100702 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 void __user *argp = (void __user *)arg;
704
705 rfile = file->private_data;
706 if (((cmd >> 8) & 0xff) != 'W')
707 return -ENOTTY;
708 switch (cmd) {
709 case SNDRV_RAWMIDI_IOCTL_PVERSION:
710 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
711 case SNDRV_RAWMIDI_IOCTL_INFO:
712 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100713 int stream;
714 struct snd_rawmidi_info __user *info = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (get_user(stream, &info->stream))
716 return -EFAULT;
717 switch (stream) {
718 case SNDRV_RAWMIDI_STREAM_INPUT:
719 return snd_rawmidi_info_user(rfile->input, info);
720 case SNDRV_RAWMIDI_STREAM_OUTPUT:
721 return snd_rawmidi_info_user(rfile->output, info);
722 default:
723 return -EINVAL;
724 }
725 }
726 case SNDRV_RAWMIDI_IOCTL_PARAMS:
727 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100728 struct snd_rawmidi_params params;
729 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -EFAULT;
731 switch (params.stream) {
732 case SNDRV_RAWMIDI_STREAM_OUTPUT:
733 if (rfile->output == NULL)
734 return -EINVAL;
735 return snd_rawmidi_output_params(rfile->output, &params);
736 case SNDRV_RAWMIDI_STREAM_INPUT:
737 if (rfile->input == NULL)
738 return -EINVAL;
739 return snd_rawmidi_input_params(rfile->input, &params);
740 default:
741 return -EINVAL;
742 }
743 }
744 case SNDRV_RAWMIDI_IOCTL_STATUS:
745 {
746 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100747 struct snd_rawmidi_status status;
748 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return -EFAULT;
750 switch (status.stream) {
751 case SNDRV_RAWMIDI_STREAM_OUTPUT:
752 if (rfile->output == NULL)
753 return -EINVAL;
754 err = snd_rawmidi_output_status(rfile->output, &status);
755 break;
756 case SNDRV_RAWMIDI_STREAM_INPUT:
757 if (rfile->input == NULL)
758 return -EINVAL;
759 err = snd_rawmidi_input_status(rfile->input, &status);
760 break;
761 default:
762 return -EINVAL;
763 }
764 if (err < 0)
765 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100766 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return -EFAULT;
768 return 0;
769 }
770 case SNDRV_RAWMIDI_IOCTL_DROP:
771 {
772 int val;
773 if (get_user(val, (int __user *) argp))
774 return -EFAULT;
775 switch (val) {
776 case SNDRV_RAWMIDI_STREAM_OUTPUT:
777 if (rfile->output == NULL)
778 return -EINVAL;
779 return snd_rawmidi_drop_output(rfile->output);
780 default:
781 return -EINVAL;
782 }
783 }
784 case SNDRV_RAWMIDI_IOCTL_DRAIN:
785 {
786 int val;
787 if (get_user(val, (int __user *) argp))
788 return -EFAULT;
789 switch (val) {
790 case SNDRV_RAWMIDI_STREAM_OUTPUT:
791 if (rfile->output == NULL)
792 return -EINVAL;
793 return snd_rawmidi_drain_output(rfile->output);
794 case SNDRV_RAWMIDI_STREAM_INPUT:
795 if (rfile->input == NULL)
796 return -EINVAL;
797 return snd_rawmidi_drain_input(rfile->input);
798 default:
799 return -EINVAL;
800 }
801 }
802#ifdef CONFIG_SND_DEBUG
803 default:
804 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
805#endif
806 }
807 return -ENOTTY;
808}
809
Takashi Iwai48c9d412005-11-17 13:56:51 +0100810static int snd_rawmidi_control_ioctl(struct snd_card *card,
811 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned int cmd,
813 unsigned long arg)
814{
815 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 switch (cmd) {
818 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
819 {
820 int device;
821
822 if (get_user(device, (int __user *)argp))
823 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100824 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 device = device < 0 ? 0 : device + 1;
826 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100827 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 break;
829 device++;
830 }
831 if (device == SNDRV_RAWMIDI_DEVICES)
832 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100833 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (put_user(device, (int __user *)argp))
835 return -EFAULT;
836 return 0;
837 }
838 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
839 {
840 int val;
841
842 if (get_user(val, (int __user *)argp))
843 return -EFAULT;
844 control->prefer_rawmidi_subdevice = val;
845 return 0;
846 }
847 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
848 return snd_rawmidi_info_select_user(card, argp);
849 }
850 return -ENOIOCTLCMD;
851}
852
853/**
854 * snd_rawmidi_receive - receive the input data from the device
855 * @substream: the rawmidi substream
856 * @buffer: the buffer pointer
857 * @count: the data size to read
858 *
859 * Reads the data from the internal buffer.
860 *
861 * Returns the size of read data, or a negative error code on failure.
862 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100863int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
864 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 unsigned long flags;
867 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100868 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 if (runtime->buffer == NULL) {
871 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
872 return -EINVAL;
873 }
874 spin_lock_irqsave(&runtime->lock, flags);
875 if (count == 1) { /* special case, faster code */
876 substream->bytes++;
877 if (runtime->avail < runtime->buffer_size) {
878 runtime->buffer[runtime->hw_ptr++] = buffer[0];
879 runtime->hw_ptr %= runtime->buffer_size;
880 runtime->avail++;
881 result++;
882 } else {
883 runtime->xruns++;
884 }
885 } else {
886 substream->bytes += count;
887 count1 = runtime->buffer_size - runtime->hw_ptr;
888 if (count1 > count)
889 count1 = count;
890 if (count1 > (int)(runtime->buffer_size - runtime->avail))
891 count1 = runtime->buffer_size - runtime->avail;
892 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
893 runtime->hw_ptr += count1;
894 runtime->hw_ptr %= runtime->buffer_size;
895 runtime->avail += count1;
896 count -= count1;
897 result += count1;
898 if (count > 0) {
899 buffer += count1;
900 count1 = count;
901 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
902 count1 = runtime->buffer_size - runtime->avail;
903 runtime->xruns += count - count1;
904 }
905 if (count1 > 0) {
906 memcpy(runtime->buffer, buffer, count1);
907 runtime->hw_ptr = count1;
908 runtime->avail += count1;
909 result += count1;
910 }
911 }
912 }
913 if (result > 0) {
914 if (runtime->event)
915 tasklet_hi_schedule(&runtime->tasklet);
916 else if (snd_rawmidi_ready(substream))
917 wake_up(&runtime->sleep);
918 }
919 spin_unlock_irqrestore(&runtime->lock, flags);
920 return result;
921}
922
Takashi Iwai48c9d412005-11-17 13:56:51 +0100923static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 unsigned char *buf, long count, int kernel)
925{
926 unsigned long flags;
927 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100928 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 while (count > 0 && runtime->avail) {
931 count1 = runtime->buffer_size - runtime->appl_ptr;
932 if (count1 > count)
933 count1 = count;
934 spin_lock_irqsave(&runtime->lock, flags);
935 if (count1 > (int)runtime->avail)
936 count1 = runtime->avail;
937 if (kernel) {
938 memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
939 } else {
940 spin_unlock_irqrestore(&runtime->lock, flags);
941 if (copy_to_user((char __user *)buf + result,
942 runtime->buffer + runtime->appl_ptr, count1)) {
943 return result > 0 ? result : -EFAULT;
944 }
945 spin_lock_irqsave(&runtime->lock, flags);
946 }
947 runtime->appl_ptr += count1;
948 runtime->appl_ptr %= runtime->buffer_size;
949 runtime->avail -= count1;
950 spin_unlock_irqrestore(&runtime->lock, flags);
951 result += count1;
952 count -= count1;
953 }
954 return result;
955}
956
Takashi Iwai48c9d412005-11-17 13:56:51 +0100957long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
958 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 snd_rawmidi_input_trigger(substream, 1);
961 return snd_rawmidi_kernel_read1(substream, buf, count, 1);
962}
963
Takashi Iwai48c9d412005-11-17 13:56:51 +0100964static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
965 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 long result;
968 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100969 struct snd_rawmidi_file *rfile;
970 struct snd_rawmidi_substream *substream;
971 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 rfile = file->private_data;
974 substream = rfile->input;
975 if (substream == NULL)
976 return -EIO;
977 runtime = substream->runtime;
978 snd_rawmidi_input_trigger(substream, 1);
979 result = 0;
980 while (count > 0) {
981 spin_lock_irq(&runtime->lock);
982 while (!snd_rawmidi_ready(substream)) {
983 wait_queue_t wait;
984 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
985 spin_unlock_irq(&runtime->lock);
986 return result > 0 ? result : -EAGAIN;
987 }
988 init_waitqueue_entry(&wait, current);
989 add_wait_queue(&runtime->sleep, &wait);
990 set_current_state(TASK_INTERRUPTIBLE);
991 spin_unlock_irq(&runtime->lock);
992 schedule();
993 remove_wait_queue(&runtime->sleep, &wait);
994 if (signal_pending(current))
995 return result > 0 ? result : -ERESTARTSYS;
996 if (!runtime->avail)
997 return result > 0 ? result : -EIO;
998 spin_lock_irq(&runtime->lock);
999 }
1000 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001001 count1 = snd_rawmidi_kernel_read1(substream,
1002 (unsigned char __force *)buf,
1003 count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (count1 < 0)
1005 return result > 0 ? result : count1;
1006 result += count1;
1007 buf += count1;
1008 count -= count1;
1009 }
1010 return result;
1011}
1012
1013/**
1014 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1015 * @substream: the rawmidi substream
1016 *
1017 * Returns 1 if the internal output buffer is empty, 0 if not.
1018 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001019int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001021 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 int result;
1023 unsigned long flags;
1024
1025 if (runtime->buffer == NULL) {
1026 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1027 return 1;
1028 }
1029 spin_lock_irqsave(&runtime->lock, flags);
1030 result = runtime->avail >= runtime->buffer_size;
1031 spin_unlock_irqrestore(&runtime->lock, flags);
1032 return result;
1033}
1034
1035/**
1036 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1037 * @substream: the rawmidi substream
1038 * @buffer: the buffer pointer
1039 * @count: data size to transfer
1040 *
1041 * Copies data from the internal output buffer to the given buffer.
1042 *
1043 * Call this in the interrupt handler when the midi output is ready,
1044 * and call snd_rawmidi_transmit_ack() after the transmission is
1045 * finished.
1046 *
1047 * Returns the size of copied data, or a negative error code on failure.
1048 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001049int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1050 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 unsigned long flags;
1053 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001054 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (runtime->buffer == NULL) {
1057 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1058 return -EINVAL;
1059 }
1060 result = 0;
1061 spin_lock_irqsave(&runtime->lock, flags);
1062 if (runtime->avail >= runtime->buffer_size) {
1063 /* warning: lowlevel layer MUST trigger down the hardware */
1064 goto __skip;
1065 }
1066 if (count == 1) { /* special case, faster code */
1067 *buffer = runtime->buffer[runtime->hw_ptr];
1068 result++;
1069 } else {
1070 count1 = runtime->buffer_size - runtime->hw_ptr;
1071 if (count1 > count)
1072 count1 = count;
1073 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1074 count1 = runtime->buffer_size - runtime->avail;
1075 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1076 count -= count1;
1077 result += count1;
1078 if (count > 0) {
1079 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1080 count = runtime->buffer_size - runtime->avail - count1;
1081 memcpy(buffer + count1, runtime->buffer, count);
1082 result += count;
1083 }
1084 }
1085 __skip:
1086 spin_unlock_irqrestore(&runtime->lock, flags);
1087 return result;
1088}
1089
1090/**
1091 * snd_rawmidi_transmit_ack - acknowledge the transmission
1092 * @substream: the rawmidi substream
1093 * @count: the tranferred count
1094 *
1095 * Advances the hardware pointer for the internal output buffer with
1096 * the given size and updates the condition.
1097 * Call after the transmission is finished.
1098 *
1099 * Returns the advanced size if successful, or a negative error code on failure.
1100 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001101int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001104 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 if (runtime->buffer == NULL) {
1107 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1108 return -EINVAL;
1109 }
1110 spin_lock_irqsave(&runtime->lock, flags);
1111 snd_assert(runtime->avail + count <= runtime->buffer_size, );
1112 runtime->hw_ptr += count;
1113 runtime->hw_ptr %= runtime->buffer_size;
1114 runtime->avail += count;
1115 substream->bytes += count;
1116 if (count > 0) {
1117 if (runtime->drain || snd_rawmidi_ready(substream))
1118 wake_up(&runtime->sleep);
1119 }
1120 spin_unlock_irqrestore(&runtime->lock, flags);
1121 return count;
1122}
1123
1124/**
1125 * snd_rawmidi_transmit - copy from the buffer to the device
1126 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001127 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 * @count: the data size to transfer
1129 *
1130 * Copies data from the buffer to the device and advances the pointer.
1131 *
1132 * Returns the copied size if successful, or a negative error code on failure.
1133 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001134int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1135 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1138 if (count < 0)
1139 return count;
1140 return snd_rawmidi_transmit_ack(substream, count);
1141}
1142
Takashi Iwai48c9d412005-11-17 13:56:51 +01001143static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1144 const unsigned char *buf, long count, int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 unsigned long flags;
1147 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001148 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 snd_assert(buf != NULL, return -EINVAL);
1151 snd_assert(runtime->buffer != NULL, return -EINVAL);
1152
1153 result = 0;
1154 spin_lock_irqsave(&runtime->lock, flags);
1155 if (substream->append) {
1156 if ((long)runtime->avail < count) {
1157 spin_unlock_irqrestore(&runtime->lock, flags);
1158 return -EAGAIN;
1159 }
1160 }
1161 while (count > 0 && runtime->avail > 0) {
1162 count1 = runtime->buffer_size - runtime->appl_ptr;
1163 if (count1 > count)
1164 count1 = count;
1165 if (count1 > (long)runtime->avail)
1166 count1 = runtime->avail;
1167 if (kernel) {
1168 memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
1169 } else {
1170 spin_unlock_irqrestore(&runtime->lock, flags);
1171 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
1172 (char __user *)buf, count1)) {
1173 spin_lock_irqsave(&runtime->lock, flags);
1174 result = result > 0 ? result : -EFAULT;
1175 goto __end;
1176 }
1177 spin_lock_irqsave(&runtime->lock, flags);
1178 }
1179 runtime->appl_ptr += count1;
1180 runtime->appl_ptr %= runtime->buffer_size;
1181 runtime->avail -= count1;
1182 result += count1;
1183 buf += count1;
1184 count -= count1;
1185 }
1186 __end:
1187 count1 = runtime->avail < runtime->buffer_size;
1188 spin_unlock_irqrestore(&runtime->lock, flags);
1189 if (count1)
1190 snd_rawmidi_output_trigger(substream, 1);
1191 return result;
1192}
1193
Takashi Iwai48c9d412005-11-17 13:56:51 +01001194long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1195 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 return snd_rawmidi_kernel_write1(substream, buf, count, 1);
1198}
1199
Takashi Iwai48c9d412005-11-17 13:56:51 +01001200static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1201 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 long result, timeout;
1204 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001205 struct snd_rawmidi_file *rfile;
1206 struct snd_rawmidi_runtime *runtime;
1207 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 rfile = file->private_data;
1210 substream = rfile->output;
1211 runtime = substream->runtime;
1212 /* we cannot put an atomic message to our buffer */
1213 if (substream->append && count > runtime->buffer_size)
1214 return -EIO;
1215 result = 0;
1216 while (count > 0) {
1217 spin_lock_irq(&runtime->lock);
1218 while (!snd_rawmidi_ready_append(substream, count)) {
1219 wait_queue_t wait;
1220 if (file->f_flags & O_NONBLOCK) {
1221 spin_unlock_irq(&runtime->lock);
1222 return result > 0 ? result : -EAGAIN;
1223 }
1224 init_waitqueue_entry(&wait, current);
1225 add_wait_queue(&runtime->sleep, &wait);
1226 set_current_state(TASK_INTERRUPTIBLE);
1227 spin_unlock_irq(&runtime->lock);
1228 timeout = schedule_timeout(30 * HZ);
1229 remove_wait_queue(&runtime->sleep, &wait);
1230 if (signal_pending(current))
1231 return result > 0 ? result : -ERESTARTSYS;
1232 if (!runtime->avail && !timeout)
1233 return result > 0 ? result : -EIO;
1234 spin_lock_irq(&runtime->lock);
1235 }
1236 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +02001237 count1 = snd_rawmidi_kernel_write1(substream,
1238 (unsigned char __force *)buf,
1239 count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (count1 < 0)
1241 return result > 0 ? result : count1;
1242 result += count1;
1243 buf += count1;
1244 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1245 break;
1246 count -= count1;
1247 }
1248 if (file->f_flags & O_SYNC) {
1249 spin_lock_irq(&runtime->lock);
1250 while (runtime->avail != runtime->buffer_size) {
1251 wait_queue_t wait;
1252 unsigned int last_avail = runtime->avail;
1253 init_waitqueue_entry(&wait, current);
1254 add_wait_queue(&runtime->sleep, &wait);
1255 set_current_state(TASK_INTERRUPTIBLE);
1256 spin_unlock_irq(&runtime->lock);
1257 timeout = schedule_timeout(30 * HZ);
1258 remove_wait_queue(&runtime->sleep, &wait);
1259 if (signal_pending(current))
1260 return result > 0 ? result : -ERESTARTSYS;
1261 if (runtime->avail == last_avail && !timeout)
1262 return result > 0 ? result : -EIO;
1263 spin_lock_irq(&runtime->lock);
1264 }
1265 spin_unlock_irq(&runtime->lock);
1266 }
1267 return result;
1268}
1269
1270static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1271{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001272 struct snd_rawmidi_file *rfile;
1273 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 unsigned int mask;
1275
1276 rfile = file->private_data;
1277 if (rfile->input != NULL) {
1278 runtime = rfile->input->runtime;
1279 snd_rawmidi_input_trigger(rfile->input, 1);
1280 poll_wait(file, &runtime->sleep, wait);
1281 }
1282 if (rfile->output != NULL) {
1283 runtime = rfile->output->runtime;
1284 poll_wait(file, &runtime->sleep, wait);
1285 }
1286 mask = 0;
1287 if (rfile->input != NULL) {
1288 if (snd_rawmidi_ready(rfile->input))
1289 mask |= POLLIN | POLLRDNORM;
1290 }
1291 if (rfile->output != NULL) {
1292 if (snd_rawmidi_ready(rfile->output))
1293 mask |= POLLOUT | POLLWRNORM;
1294 }
1295 return mask;
1296}
1297
1298/*
1299 */
1300#ifdef CONFIG_COMPAT
1301#include "rawmidi_compat.c"
1302#else
1303#define snd_rawmidi_ioctl_compat NULL
1304#endif
1305
1306/*
1307
1308 */
1309
Takashi Iwai48c9d412005-11-17 13:56:51 +01001310static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1311 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001313 struct snd_rawmidi *rmidi;
1314 struct snd_rawmidi_substream *substream;
1315 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 struct list_head *list;
1317
1318 rmidi = entry->private_data;
1319 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001320 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1322 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001323 substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 snd_iprintf(buffer,
1325 "Output %d\n"
1326 " Tx bytes : %lu\n",
1327 substream->number,
1328 (unsigned long) substream->bytes);
1329 if (substream->opened) {
1330 runtime = substream->runtime;
1331 snd_iprintf(buffer,
1332 " Mode : %s\n"
1333 " Buffer size : %lu\n"
1334 " Avail : %lu\n",
1335 runtime->oss ? "OSS compatible" : "native",
1336 (unsigned long) runtime->buffer_size,
1337 (unsigned long) runtime->avail);
1338 }
1339 }
1340 }
1341 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1342 list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001343 substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 snd_iprintf(buffer,
1345 "Input %d\n"
1346 " Rx bytes : %lu\n",
1347 substream->number,
1348 (unsigned long) substream->bytes);
1349 if (substream->opened) {
1350 runtime = substream->runtime;
1351 snd_iprintf(buffer,
1352 " Buffer size : %lu\n"
1353 " Avail : %lu\n"
1354 " Overruns : %lu\n",
1355 (unsigned long) runtime->buffer_size,
1356 (unsigned long) runtime->avail,
1357 (unsigned long) runtime->xruns);
1358 }
1359 }
1360 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001361 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
1364/*
1365 * Register functions
1366 */
1367
1368static struct file_operations snd_rawmidi_f_ops =
1369{
1370 .owner = THIS_MODULE,
1371 .read = snd_rawmidi_read,
1372 .write = snd_rawmidi_write,
1373 .open = snd_rawmidi_open,
1374 .release = snd_rawmidi_release,
1375 .poll = snd_rawmidi_poll,
1376 .unlocked_ioctl = snd_rawmidi_ioctl,
1377 .compat_ioctl = snd_rawmidi_ioctl_compat,
1378};
1379
Takashi Iwai48c9d412005-11-17 13:56:51 +01001380static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1381 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 int direction,
1383 int count)
1384{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001385 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 int idx;
1387
1388 INIT_LIST_HEAD(&stream->substreams);
1389 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001390 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001391 if (substream == NULL) {
1392 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 substream->stream = direction;
1396 substream->number = idx;
1397 substream->rmidi = rmidi;
1398 substream->pstr = stream;
1399 list_add_tail(&substream->list, &stream->substreams);
1400 stream->substream_count++;
1401 }
1402 return 0;
1403}
1404
1405/**
1406 * snd_rawmidi_new - create a rawmidi instance
1407 * @card: the card instance
1408 * @id: the id string
1409 * @device: the device index
1410 * @output_count: the number of output streams
1411 * @input_count: the number of input streams
1412 * @rrawmidi: the pointer to store the new rawmidi instance
1413 *
1414 * Creates a new rawmidi instance.
1415 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1416 *
1417 * Returns zero if successful, or a negative error code on failure.
1418 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001419int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 int output_count, int input_count,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001421 struct snd_rawmidi ** rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001423 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001425 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 .dev_free = snd_rawmidi_dev_free,
1427 .dev_register = snd_rawmidi_dev_register,
1428 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 };
1430
1431 snd_assert(rrawmidi != NULL, return -EINVAL);
1432 *rrawmidi = NULL;
1433 snd_assert(card != NULL, return -ENXIO);
Takashi Iwaica2c0962005-09-09 14:20:23 +02001434 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001435 if (rmidi == NULL) {
1436 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 rmidi->card = card;
1440 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001441 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 init_waitqueue_head(&rmidi->open_wait);
1443 if (id != NULL)
1444 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001445 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1446 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1447 SNDRV_RAWMIDI_STREAM_INPUT,
1448 input_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 snd_rawmidi_free(rmidi);
1450 return err;
1451 }
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001452 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1453 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1454 SNDRV_RAWMIDI_STREAM_OUTPUT,
1455 output_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 snd_rawmidi_free(rmidi);
1457 return err;
1458 }
1459 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1460 snd_rawmidi_free(rmidi);
1461 return err;
1462 }
1463 *rrawmidi = rmidi;
1464 return 0;
1465}
1466
Takashi Iwai48c9d412005-11-17 13:56:51 +01001467static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001469 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 while (!list_empty(&stream->substreams)) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001472 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 list_del(&substream->list);
1474 kfree(substream);
1475 }
1476}
1477
Takashi Iwai48c9d412005-11-17 13:56:51 +01001478static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 snd_assert(rmidi != NULL, return -ENXIO);
Takashi Iwaic4614822006-06-23 14:38:23 +02001481
1482 snd_info_free_entry(rmidi->proc_entry);
1483 rmidi->proc_entry = NULL;
1484 mutex_lock(&register_mutex);
1485 if (rmidi->ops && rmidi->ops->dev_unregister)
1486 rmidi->ops->dev_unregister(rmidi);
1487 mutex_unlock(&register_mutex);
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1490 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1491 if (rmidi->private_free)
1492 rmidi->private_free(rmidi);
1493 kfree(rmidi);
1494 return 0;
1495}
1496
Takashi Iwai48c9d412005-11-17 13:56:51 +01001497static int snd_rawmidi_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001499 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return snd_rawmidi_free(rmidi);
1501}
1502
1503#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
Takashi Iwai48c9d412005-11-17 13:56:51 +01001504static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001506 struct snd_rawmidi *rmidi = device->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 rmidi->seq_dev = NULL;
1508}
1509#endif
1510
Takashi Iwai48c9d412005-11-17 13:56:51 +01001511static int snd_rawmidi_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001513 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001514 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 char name[16];
Takashi Iwai48c9d412005-11-17 13:56:51 +01001516 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1519 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001520 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001521 if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001522 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return -EBUSY;
1524 }
Clemens Ladischf87135f2005-11-20 14:06:59 +01001525 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
1527 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1528 rmidi->card, rmidi->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001529 &snd_rawmidi_f_ops, rmidi, name)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001531 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001532 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return err;
1534 }
1535 if (rmidi->ops && rmidi->ops->dev_register &&
1536 (err = rmidi->ops->dev_register(rmidi)) < 0) {
1537 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001538 list_del(&rmidi->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001539 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return err;
1541 }
1542#ifdef CONFIG_SND_OSSEMUL
1543 rmidi->ossreg = 0;
1544 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1545 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001546 rmidi->card, 0, &snd_rawmidi_f_ops,
1547 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
1549 } else {
1550 rmidi->ossreg++;
1551#ifdef SNDRV_OSS_INFO_DEV_MIDI
1552 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1553#endif
1554 }
1555 }
1556 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1557 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
Clemens Ladischf87135f2005-11-20 14:06:59 +01001558 rmidi->card, 1, &snd_rawmidi_f_ops,
1559 rmidi, name) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
1561 } else {
1562 rmidi->ossreg++;
1563 }
1564 }
1565#endif /* CONFIG_SND_OSSEMUL */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001566 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 sprintf(name, "midi%d", rmidi->device);
1568 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1569 if (entry) {
1570 entry->private_data = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 entry->c.text.read = snd_rawmidi_proc_info_read;
1572 if (snd_info_register(entry) < 0) {
1573 snd_info_free_entry(entry);
1574 entry = NULL;
1575 }
1576 }
1577 rmidi->proc_entry = entry;
1578#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1579 if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1580 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1581 rmidi->seq_dev->private_data = rmidi;
1582 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1583 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1584 snd_device_register(rmidi->card, rmidi->seq_dev);
1585 }
1586 }
1587#endif
1588 return 0;
1589}
1590
Takashi Iwai48c9d412005-11-17 13:56:51 +01001591static int snd_rawmidi_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001593 struct snd_rawmidi *rmidi = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001595 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001596 list_del_init(&rmidi->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597#ifdef CONFIG_SND_OSSEMUL
1598 if (rmidi->ossreg) {
1599 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1600 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1601#ifdef SNDRV_OSS_INFO_DEV_MIDI
1602 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1603#endif
1604 }
1605 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1606 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1607 rmidi->ossreg = 0;
1608 }
1609#endif /* CONFIG_SND_OSSEMUL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001611 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
1615/**
1616 * snd_rawmidi_set_ops - set the rawmidi operators
1617 * @rmidi: the rawmidi instance
1618 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1619 * @ops: the operator table
1620 *
1621 * Sets the rawmidi operators for the given stream direction.
1622 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001623void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1624 struct snd_rawmidi_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 struct list_head *list;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001627 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 list_for_each(list, &rmidi->streams[stream].substreams) {
Takashi Iwai48c9d412005-11-17 13:56:51 +01001630 substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 substream->ops = ops;
1632 }
1633}
1634
1635/*
1636 * ENTRY functions
1637 */
1638
1639static int __init alsa_rawmidi_init(void)
1640{
1641
1642 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1643 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1644#ifdef CONFIG_SND_OSSEMUL
1645 { int i;
1646 /* check device map table */
1647 for (i = 0; i < SNDRV_CARDS; i++) {
1648 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1649 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1650 midi_map[i] = 0;
1651 }
1652 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1653 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1654 amidi_map[i] = 1;
1655 }
1656 }
1657 }
1658#endif /* CONFIG_SND_OSSEMUL */
1659 return 0;
1660}
1661
1662static void __exit alsa_rawmidi_exit(void)
1663{
1664 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1665 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1666}
1667
1668module_init(alsa_rawmidi_init)
1669module_exit(alsa_rawmidi_exit)
1670
1671EXPORT_SYMBOL(snd_rawmidi_output_params);
1672EXPORT_SYMBOL(snd_rawmidi_input_params);
1673EXPORT_SYMBOL(snd_rawmidi_drop_output);
1674EXPORT_SYMBOL(snd_rawmidi_drain_output);
1675EXPORT_SYMBOL(snd_rawmidi_drain_input);
1676EXPORT_SYMBOL(snd_rawmidi_receive);
1677EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1678EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1679EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1680EXPORT_SYMBOL(snd_rawmidi_transmit);
1681EXPORT_SYMBOL(snd_rawmidi_new);
1682EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683EXPORT_SYMBOL(snd_rawmidi_info_select);
1684EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1685EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1686EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1687EXPORT_SYMBOL(snd_rawmidi_kernel_write);