blob: c4995c9f57303e0590ce5706676c2dc5918bf000 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Abstract layer for MIDI v1.0 stream
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <sound/core.h>
23#include <linux/major.h>
24#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/sched.h>
26#include <linux/slab.h>
27#include <linux/time.h>
28#include <linux/wait.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010029#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/moduleparam.h>
31#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/rawmidi.h>
33#include <sound/info.h>
34#include <sound/control.h>
35#include <sound/minors.h>
36#include <sound/initval.h>
37
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020038MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
40MODULE_LICENSE("GPL");
41
42#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai6581f4e2006-05-17 17:14:51 +020043static int midi_map[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
45module_param_array(midi_map, int, NULL, 0444);
46MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
47module_param_array(amidi_map, int, NULL, 0444);
48MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
49#endif /* CONFIG_SND_OSSEMUL */
50
Takashi Iwai48c9d412005-11-17 13:56:51 +010051static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
52static int snd_rawmidi_dev_free(struct snd_device *device);
53static int snd_rawmidi_dev_register(struct snd_device *device);
54static int snd_rawmidi_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Clemens Ladischf87135f2005-11-20 14:06:59 +010056static LIST_HEAD(snd_rawmidi_devices);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010057static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Clemens Ladischf87135f2005-11-20 14:06:59 +010059static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
60{
Clemens Ladischf87135f2005-11-20 14:06:59 +010061 struct snd_rawmidi *rawmidi;
62
Johannes Berg9244b2c2006-10-05 16:02:22 +020063 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
Clemens Ladischf87135f2005-11-20 14:06:59 +010064 if (rawmidi->card == card && rawmidi->device == device)
65 return rawmidi;
Clemens Ladischf87135f2005-11-20 14:06:59 +010066 return NULL;
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static inline unsigned short snd_rawmidi_file_flags(struct file *file)
70{
71 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
72 case FMODE_WRITE:
73 return SNDRV_RAWMIDI_LFLG_OUTPUT;
74 case FMODE_READ:
75 return SNDRV_RAWMIDI_LFLG_INPUT;
76 default:
77 return SNDRV_RAWMIDI_LFLG_OPEN;
78 }
79}
80
Takashi Iwai48c9d412005-11-17 13:56:51 +010081static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Takashi Iwai48c9d412005-11-17 13:56:51 +010083 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return runtime->avail >= runtime->avail_min;
85}
86
Takashi Iwai48c9d412005-11-17 13:56:51 +010087static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
88 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Takashi Iwai48c9d412005-11-17 13:56:51 +010090 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return runtime->avail >= runtime->avail_min &&
92 (!substream->append || runtime->avail >= count);
93}
94
95static void snd_rawmidi_input_event_tasklet(unsigned long data)
96{
Takashi Iwai48c9d412005-11-17 13:56:51 +010097 struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 substream->runtime->event(substream);
99}
100
101static void snd_rawmidi_output_trigger_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->ops->trigger(substream, 1);
105}
106
Takashi Iwai48c9d412005-11-17 13:56:51 +0100107static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100109 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Takashi Iwaica2c0962005-09-09 14:20:23 +0200111 if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -ENOMEM;
113 spin_lock_init(&runtime->lock);
114 init_waitqueue_head(&runtime->sleep);
115 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
116 tasklet_init(&runtime->tasklet,
117 snd_rawmidi_input_event_tasklet,
118 (unsigned long)substream);
119 else
120 tasklet_init(&runtime->tasklet,
121 snd_rawmidi_output_trigger_tasklet,
122 (unsigned long)substream);
123 runtime->event = NULL;
124 runtime->buffer_size = PAGE_SIZE;
125 runtime->avail_min = 1;
126 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
127 runtime->avail = 0;
128 else
129 runtime->avail = runtime->buffer_size;
130 if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
131 kfree(runtime);
132 return -ENOMEM;
133 }
134 runtime->appl_ptr = runtime->hw_ptr = 0;
135 substream->runtime = runtime;
136 return 0;
137}
138
Takashi Iwai48c9d412005-11-17 13:56:51 +0100139static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100141 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 kfree(runtime->buffer);
144 kfree(runtime);
145 substream->runtime = NULL;
146 return 0;
147}
148
Takashi Iwai48c9d412005-11-17 13:56:51 +0100149static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 if (up) {
152 tasklet_hi_schedule(&substream->runtime->tasklet);
153 } else {
154 tasklet_kill(&substream->runtime->tasklet);
155 substream->ops->trigger(substream, 0);
156 }
157}
158
Takashi Iwai48c9d412005-11-17 13:56:51 +0100159static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 substream->ops->trigger(substream, up);
162 if (!up && substream->runtime->event)
163 tasklet_kill(&substream->runtime->tasklet);
164}
165
Takashi Iwai48c9d412005-11-17 13:56:51 +0100166int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100169 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 snd_rawmidi_output_trigger(substream, 0);
172 runtime->drain = 0;
173 spin_lock_irqsave(&runtime->lock, flags);
174 runtime->appl_ptr = runtime->hw_ptr = 0;
175 runtime->avail = runtime->buffer_size;
176 spin_unlock_irqrestore(&runtime->lock, flags);
177 return 0;
178}
179
Takashi Iwai48c9d412005-11-17 13:56:51 +0100180int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 int err;
183 long timeout;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100184 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 err = 0;
187 runtime->drain = 1;
188 timeout = wait_event_interruptible_timeout(runtime->sleep,
189 (runtime->avail >= runtime->buffer_size),
190 10*HZ);
191 if (signal_pending(current))
192 err = -ERESTARTSYS;
193 if (runtime->avail < runtime->buffer_size && !timeout) {
194 snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
195 err = -EIO;
196 }
197 runtime->drain = 0;
198 if (err != -ERESTARTSYS) {
199 /* we need wait a while to make sure that Tx FIFOs are empty */
200 if (substream->ops->drain)
201 substream->ops->drain(substream);
202 else
203 msleep(50);
204 snd_rawmidi_drop_output(substream);
205 }
206 return err;
207}
208
Takashi Iwai48c9d412005-11-17 13:56:51 +0100209int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100212 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 snd_rawmidi_input_trigger(substream, 0);
215 runtime->drain = 0;
216 spin_lock_irqsave(&runtime->lock, flags);
217 runtime->appl_ptr = runtime->hw_ptr = 0;
218 runtime->avail = 0;
219 spin_unlock_irqrestore(&runtime->lock, flags);
220 return 0;
221}
222
Clemens Ladischf87135f2005-11-20 14:06:59 +0100223int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
Takashi Iwai48c9d412005-11-17 13:56:51 +0100224 int mode, struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100226 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct list_head *list1, *list2;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100228 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
229 struct snd_rawmidi_runtime *input = NULL, *output = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 int err;
231
232 if (rfile)
233 rfile->input = rfile->output = NULL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100234 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100235 rmidi = snd_rawmidi_search(card, device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100236 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (rmidi == NULL) {
238 err = -ENODEV;
239 goto __error1;
240 }
241 if (!try_module_get(rmidi->card->module)) {
242 err = -EFAULT;
243 goto __error1;
244 }
245 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100246 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
248 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
249 err = -ENXIO;
250 goto __error;
251 }
252 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
253 err = -ENODEV;
254 goto __error;
255 }
256 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >=
257 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) {
258 err = -EAGAIN;
259 goto __error;
260 }
261 }
262 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
263 if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) {
264 err = -ENXIO;
265 goto __error;
266 }
267 if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
268 err = -ENODEV;
269 goto __error;
270 }
271 if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >=
272 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) {
273 err = -EAGAIN;
274 goto __error;
275 }
276 }
277 list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next;
278 while (1) {
279 if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
280 sinput = NULL;
281 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
282 err = -EAGAIN;
283 goto __error;
284 }
285 break;
286 }
Takashi Iwai48c9d412005-11-17 13:56:51 +0100287 sinput = list_entry(list1, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
289 goto __nexti;
290 if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
291 break;
292 __nexti:
293 list1 = list1->next;
294 }
295 list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next;
296 while (1) {
297 if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
298 soutput = NULL;
299 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
300 err = -EAGAIN;
301 goto __error;
302 }
303 break;
304 }
Takashi Iwai48c9d412005-11-17 13:56:51 +0100305 soutput = list_entry(list2, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
307 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
308 if (soutput->opened && !soutput->append)
309 goto __nexto;
310 } else {
311 if (soutput->opened)
312 goto __nexto;
313 }
314 }
315 if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number))
316 break;
317 __nexto:
318 list2 = list2->next;
319 }
320 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
321 if ((err = snd_rawmidi_runtime_create(sinput)) < 0)
322 goto __error;
323 input = sinput->runtime;
324 if ((err = sinput->ops->open(sinput)) < 0)
325 goto __error;
326 sinput->opened = 1;
327 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
328 } else {
329 sinput = NULL;
330 }
331 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
332 if (soutput->opened)
333 goto __skip_output;
334 if ((err = snd_rawmidi_runtime_create(soutput)) < 0) {
335 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
336 sinput->ops->close(sinput);
337 goto __error;
338 }
339 output = soutput->runtime;
340 if ((err = soutput->ops->open(soutput)) < 0) {
341 if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
342 sinput->ops->close(sinput);
343 goto __error;
344 }
345 __skip_output:
346 soutput->opened = 1;
347 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
348 soutput->append = 1;
349 if (soutput->use_count++ == 0)
350 soutput->active_sensing = 1;
351 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++;
352 } else {
353 soutput = NULL;
354 }
355 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100356 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (rfile) {
358 rfile->rmidi = rmidi;
359 rfile->input = sinput;
360 rfile->output = soutput;
361 }
362 return 0;
363
364 __error:
365 if (input != NULL)
366 snd_rawmidi_runtime_free(sinput);
367 if (output != NULL)
368 snd_rawmidi_runtime_free(soutput);
369 module_put(rmidi->card->module);
370 if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100371 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 __error1:
373 return err;
374}
375
376static int snd_rawmidi_open(struct inode *inode, struct file *file)
377{
378 int maj = imajor(inode);
Takashi Iwai48c9d412005-11-17 13:56:51 +0100379 struct snd_card *card;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100380 int subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 unsigned short fflags;
382 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100383 struct snd_rawmidi *rmidi;
384 struct snd_rawmidi_file *rawmidi_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 wait_queue_t wait;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100386 struct snd_ctl_file *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Clemens Ladischf1902862005-10-24 17:05:03 +0200388 if (maj == snd_major) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100389 rmidi = snd_lookup_minor_data(iminor(inode),
390 SNDRV_DEVICE_TYPE_RAWMIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391#ifdef CONFIG_SND_OSSEMUL
Clemens Ladischf1902862005-10-24 17:05:03 +0200392 } else if (maj == SOUND_MAJOR) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100393 rmidi = snd_lookup_oss_minor_data(iminor(inode),
394 SNDRV_OSS_DEVICE_TYPE_MIDI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#endif
Clemens Ladischf1902862005-10-24 17:05:03 +0200396 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (rmidi == NULL)
400 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
402 return -EINVAL; /* invalid combination */
403 card = rmidi->card;
404 err = snd_card_file_add(card, file);
405 if (err < 0)
406 return -ENODEV;
407 fflags = snd_rawmidi_file_flags(file);
Clemens Ladischf1902862005-10-24 17:05:03 +0200408 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
410 fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
411 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
412 if (rawmidi_file == NULL) {
413 snd_card_file_remove(card, file);
414 return -ENOMEM;
415 }
416 init_waitqueue_entry(&wait, current);
417 add_wait_queue(&rmidi->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100418 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 while (1) {
420 subdevice = -1;
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200421 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200422 list_for_each_entry(kctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (kctl->pid == current->pid) {
424 subdevice = kctl->prefer_rawmidi_subdevice;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200425 if (subdevice != -1)
426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 }
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200429 read_unlock(&card->ctl_files_rwlock);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100430 err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
431 subdevice, fflags, rawmidi_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (err >= 0)
433 break;
434 if (err == -EAGAIN) {
435 if (file->f_flags & O_NONBLOCK) {
436 err = -EBUSY;
437 break;
438 }
439 } else
440 break;
441 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100442 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100444 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (signal_pending(current)) {
446 err = -ERESTARTSYS;
447 break;
448 }
449 }
450#ifdef CONFIG_SND_OSSEMUL
451 if (rawmidi_file->input && rawmidi_file->input->runtime)
452 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
453 if (rawmidi_file->output && rawmidi_file->output->runtime)
454 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
455#endif
456 remove_wait_queue(&rmidi->open_wait, &wait);
457 if (err >= 0) {
458 file->private_data = rawmidi_file;
459 } else {
460 snd_card_file_remove(card, file);
461 kfree(rawmidi_file);
462 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100463 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return err;
465}
466
Takashi Iwai48c9d412005-11-17 13:56:51 +0100467int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100469 struct snd_rawmidi *rmidi;
470 struct snd_rawmidi_substream *substream;
471 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200473 if (snd_BUG_ON(!rfile))
474 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 rmidi = rfile->rmidi;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100476 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (rfile->input != NULL) {
478 substream = rfile->input;
479 rfile->input = NULL;
480 runtime = substream->runtime;
481 snd_rawmidi_input_trigger(substream, 0);
482 substream->ops->close(substream);
483 if (runtime->private_free != NULL)
484 runtime->private_free(substream);
485 snd_rawmidi_runtime_free(substream);
486 substream->opened = 0;
487 rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
488 }
489 if (rfile->output != NULL) {
490 substream = rfile->output;
491 rfile->output = NULL;
492 if (--substream->use_count == 0) {
493 runtime = substream->runtime;
494 if (substream->active_sensing) {
495 unsigned char buf = 0xfe;
496 /* sending single active sensing message to shut the device up */
497 snd_rawmidi_kernel_write(substream, &buf, 1);
498 }
499 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
500 snd_rawmidi_output_trigger(substream, 0);
501 substream->ops->close(substream);
502 if (runtime->private_free != NULL)
503 runtime->private_free(substream);
504 snd_rawmidi_runtime_free(substream);
505 substream->opened = 0;
506 substream->append = 0;
507 }
508 rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
509 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100510 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 module_put(rmidi->card->module);
512 return 0;
513}
514
515static int snd_rawmidi_release(struct inode *inode, struct file *file)
516{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100517 struct snd_rawmidi_file *rfile;
518 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int err;
520
521 rfile = file->private_data;
522 err = snd_rawmidi_kernel_release(rfile);
523 rmidi = rfile->rmidi;
524 wake_up(&rmidi->open_wait);
525 kfree(rfile);
526 snd_card_file_remove(rmidi->card, file);
527 return err;
528}
529
Adrian Bunk18612042005-11-23 13:14:50 +0100530static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
531 struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100533 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (substream == NULL)
536 return -ENODEV;
537 rmidi = substream->rmidi;
538 memset(info, 0, sizeof(*info));
539 info->card = rmidi->card->number;
540 info->device = rmidi->device;
541 info->subdevice = substream->number;
542 info->stream = substream->stream;
543 info->flags = rmidi->info_flags;
544 strcpy(info->id, rmidi->id);
545 strcpy(info->name, rmidi->name);
546 strcpy(info->subname, substream->name);
547 info->subdevices_count = substream->pstr->substream_count;
548 info->subdevices_avail = (substream->pstr->substream_count -
549 substream->pstr->substream_opened);
550 return 0;
551}
552
Takashi Iwai48c9d412005-11-17 13:56:51 +0100553static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
554 struct snd_rawmidi_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100556 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int err;
558 if ((err = snd_rawmidi_info(substream, &info)) < 0)
559 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100560 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -EFAULT;
562 return 0;
563}
564
Takashi Iwai48c9d412005-11-17 13:56:51 +0100565int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100567 struct snd_rawmidi *rmidi;
568 struct snd_rawmidi_str *pstr;
569 struct snd_rawmidi_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100570
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100571 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100572 rmidi = snd_rawmidi_search(card, info->device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100573 mutex_unlock(&register_mutex);
Clemens Ladischa106cd32005-11-20 13:59:56 +0100574 if (!rmidi)
575 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (info->stream < 0 || info->stream > 1)
577 return -EINVAL;
578 pstr = &rmidi->streams[info->stream];
579 if (pstr->substream_count == 0)
580 return -ENOENT;
581 if (info->subdevice >= pstr->substream_count)
582 return -ENXIO;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200583 list_for_each_entry(substream, &pstr->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if ((unsigned int)substream->number == info->subdevice)
585 return snd_rawmidi_info(substream, info);
586 }
587 return -ENXIO;
588}
589
Takashi Iwai48c9d412005-11-17 13:56:51 +0100590static int snd_rawmidi_info_select_user(struct snd_card *card,
591 struct snd_rawmidi_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100594 struct snd_rawmidi_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (get_user(info.device, &_info->device))
596 return -EFAULT;
597 if (get_user(info.stream, &_info->stream))
598 return -EFAULT;
599 if (get_user(info.subdevice, &_info->subdevice))
600 return -EFAULT;
601 if ((err = snd_rawmidi_info_select(card, &info)) < 0)
602 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100603 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return -EFAULT;
605 return 0;
606}
607
Takashi Iwai48c9d412005-11-17 13:56:51 +0100608int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
609 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100612 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if (substream->append && substream->use_count > 1)
615 return -EBUSY;
616 snd_rawmidi_drain_output(substream);
617 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
618 return -EINVAL;
619 }
620 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
621 return -EINVAL;
622 }
623 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800624 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
625 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return -ENOMEM;
627 kfree(runtime->buffer);
628 runtime->buffer = newbuf;
629 runtime->buffer_size = params->buffer_size;
Clemens Ladisch1b98ea42005-11-21 07:31:31 +0100630 runtime->avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632 runtime->avail_min = params->avail_min;
633 substream->active_sensing = !params->no_active_sensing;
634 return 0;
635}
636
Takashi Iwai48c9d412005-11-17 13:56:51 +0100637int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
638 struct snd_rawmidi_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 char *newbuf;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100641 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 snd_rawmidi_drain_input(substream);
644 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
645 return -EINVAL;
646 }
647 if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
648 return -EINVAL;
649 }
650 if (params->buffer_size != runtime->buffer_size) {
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800651 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
652 if (!newbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -ENOMEM;
654 kfree(runtime->buffer);
655 runtime->buffer = newbuf;
656 runtime->buffer_size = params->buffer_size;
657 }
658 runtime->avail_min = params->avail_min;
659 return 0;
660}
661
Takashi Iwai48c9d412005-11-17 13:56:51 +0100662static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
663 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100665 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 memset(status, 0, sizeof(*status));
668 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
669 spin_lock_irq(&runtime->lock);
670 status->avail = runtime->avail;
671 spin_unlock_irq(&runtime->lock);
672 return 0;
673}
674
Takashi Iwai48c9d412005-11-17 13:56:51 +0100675static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
676 struct snd_rawmidi_status * status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100678 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 memset(status, 0, sizeof(*status));
681 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
682 spin_lock_irq(&runtime->lock);
683 status->avail = runtime->avail;
684 status->xruns = runtime->xruns;
685 runtime->xruns = 0;
686 spin_unlock_irq(&runtime->lock);
687 return 0;
688}
689
690static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
691{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100692 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 void __user *argp = (void __user *)arg;
694
695 rfile = file->private_data;
696 if (((cmd >> 8) & 0xff) != 'W')
697 return -ENOTTY;
698 switch (cmd) {
699 case SNDRV_RAWMIDI_IOCTL_PVERSION:
700 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
701 case SNDRV_RAWMIDI_IOCTL_INFO:
702 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100703 int stream;
704 struct snd_rawmidi_info __user *info = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (get_user(stream, &info->stream))
706 return -EFAULT;
707 switch (stream) {
708 case SNDRV_RAWMIDI_STREAM_INPUT:
709 return snd_rawmidi_info_user(rfile->input, info);
710 case SNDRV_RAWMIDI_STREAM_OUTPUT:
711 return snd_rawmidi_info_user(rfile->output, info);
712 default:
713 return -EINVAL;
714 }
715 }
716 case SNDRV_RAWMIDI_IOCTL_PARAMS:
717 {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100718 struct snd_rawmidi_params params;
719 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return -EFAULT;
721 switch (params.stream) {
722 case SNDRV_RAWMIDI_STREAM_OUTPUT:
723 if (rfile->output == NULL)
724 return -EINVAL;
725 return snd_rawmidi_output_params(rfile->output, &params);
726 case SNDRV_RAWMIDI_STREAM_INPUT:
727 if (rfile->input == NULL)
728 return -EINVAL;
729 return snd_rawmidi_input_params(rfile->input, &params);
730 default:
731 return -EINVAL;
732 }
733 }
734 case SNDRV_RAWMIDI_IOCTL_STATUS:
735 {
736 int err = 0;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100737 struct snd_rawmidi_status status;
738 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -EFAULT;
740 switch (status.stream) {
741 case SNDRV_RAWMIDI_STREAM_OUTPUT:
742 if (rfile->output == NULL)
743 return -EINVAL;
744 err = snd_rawmidi_output_status(rfile->output, &status);
745 break;
746 case SNDRV_RAWMIDI_STREAM_INPUT:
747 if (rfile->input == NULL)
748 return -EINVAL;
749 err = snd_rawmidi_input_status(rfile->input, &status);
750 break;
751 default:
752 return -EINVAL;
753 }
754 if (err < 0)
755 return err;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100756 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return -EFAULT;
758 return 0;
759 }
760 case SNDRV_RAWMIDI_IOCTL_DROP:
761 {
762 int val;
763 if (get_user(val, (int __user *) argp))
764 return -EFAULT;
765 switch (val) {
766 case SNDRV_RAWMIDI_STREAM_OUTPUT:
767 if (rfile->output == NULL)
768 return -EINVAL;
769 return snd_rawmidi_drop_output(rfile->output);
770 default:
771 return -EINVAL;
772 }
773 }
774 case SNDRV_RAWMIDI_IOCTL_DRAIN:
775 {
776 int val;
777 if (get_user(val, (int __user *) argp))
778 return -EFAULT;
779 switch (val) {
780 case SNDRV_RAWMIDI_STREAM_OUTPUT:
781 if (rfile->output == NULL)
782 return -EINVAL;
783 return snd_rawmidi_drain_output(rfile->output);
784 case SNDRV_RAWMIDI_STREAM_INPUT:
785 if (rfile->input == NULL)
786 return -EINVAL;
787 return snd_rawmidi_drain_input(rfile->input);
788 default:
789 return -EINVAL;
790 }
791 }
792#ifdef CONFIG_SND_DEBUG
793 default:
794 snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
795#endif
796 }
797 return -ENOTTY;
798}
799
Takashi Iwai48c9d412005-11-17 13:56:51 +0100800static int snd_rawmidi_control_ioctl(struct snd_card *card,
801 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 unsigned int cmd,
803 unsigned long arg)
804{
805 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 switch (cmd) {
808 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
809 {
810 int device;
811
812 if (get_user(device, (int __user *)argp))
813 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100814 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 device = device < 0 ? 0 : device + 1;
816 while (device < SNDRV_RAWMIDI_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100817 if (snd_rawmidi_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 break;
819 device++;
820 }
821 if (device == SNDRV_RAWMIDI_DEVICES)
822 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100823 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (put_user(device, (int __user *)argp))
825 return -EFAULT;
826 return 0;
827 }
828 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
829 {
830 int val;
831
832 if (get_user(val, (int __user *)argp))
833 return -EFAULT;
834 control->prefer_rawmidi_subdevice = val;
835 return 0;
836 }
837 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
838 return snd_rawmidi_info_select_user(card, argp);
839 }
840 return -ENOIOCTLCMD;
841}
842
843/**
844 * snd_rawmidi_receive - receive the input data from the device
845 * @substream: the rawmidi substream
846 * @buffer: the buffer pointer
847 * @count: the data size to read
848 *
849 * Reads the data from the internal buffer.
850 *
851 * Returns the size of read data, or a negative error code on failure.
852 */
Takashi Iwai48c9d412005-11-17 13:56:51 +0100853int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
854 const unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 unsigned long flags;
857 int result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100858 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 if (runtime->buffer == NULL) {
861 snd_printd("snd_rawmidi_receive: input is not active!!!\n");
862 return -EINVAL;
863 }
864 spin_lock_irqsave(&runtime->lock, flags);
865 if (count == 1) { /* special case, faster code */
866 substream->bytes++;
867 if (runtime->avail < runtime->buffer_size) {
868 runtime->buffer[runtime->hw_ptr++] = buffer[0];
869 runtime->hw_ptr %= runtime->buffer_size;
870 runtime->avail++;
871 result++;
872 } else {
873 runtime->xruns++;
874 }
875 } else {
876 substream->bytes += count;
877 count1 = runtime->buffer_size - runtime->hw_ptr;
878 if (count1 > count)
879 count1 = count;
880 if (count1 > (int)(runtime->buffer_size - runtime->avail))
881 count1 = runtime->buffer_size - runtime->avail;
882 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
883 runtime->hw_ptr += count1;
884 runtime->hw_ptr %= runtime->buffer_size;
885 runtime->avail += count1;
886 count -= count1;
887 result += count1;
888 if (count > 0) {
889 buffer += count1;
890 count1 = count;
891 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
892 count1 = runtime->buffer_size - runtime->avail;
893 runtime->xruns += count - count1;
894 }
895 if (count1 > 0) {
896 memcpy(runtime->buffer, buffer, count1);
897 runtime->hw_ptr = count1;
898 runtime->avail += count1;
899 result += count1;
900 }
901 }
902 }
903 if (result > 0) {
904 if (runtime->event)
905 tasklet_hi_schedule(&runtime->tasklet);
906 else if (snd_rawmidi_ready(substream))
907 wake_up(&runtime->sleep);
908 }
909 spin_unlock_irqrestore(&runtime->lock, flags);
910 return result;
911}
912
Takashi Iwai48c9d412005-11-17 13:56:51 +0100913static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100914 unsigned char __user *userbuf,
915 unsigned char *kernelbuf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 unsigned long flags;
918 long result = 0, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100919 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 while (count > 0 && runtime->avail) {
922 count1 = runtime->buffer_size - runtime->appl_ptr;
923 if (count1 > count)
924 count1 = count;
925 spin_lock_irqsave(&runtime->lock, flags);
926 if (count1 > (int)runtime->avail)
927 count1 = runtime->avail;
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100928 if (kernelbuf)
929 memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
930 if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 spin_unlock_irqrestore(&runtime->lock, flags);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100932 if (copy_to_user(userbuf + result,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 runtime->buffer + runtime->appl_ptr, count1)) {
934 return result > 0 ? result : -EFAULT;
935 }
936 spin_lock_irqsave(&runtime->lock, flags);
937 }
938 runtime->appl_ptr += count1;
939 runtime->appl_ptr %= runtime->buffer_size;
940 runtime->avail -= count1;
941 spin_unlock_irqrestore(&runtime->lock, flags);
942 result += count1;
943 count -= count1;
944 }
945 return result;
946}
947
Takashi Iwai48c9d412005-11-17 13:56:51 +0100948long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
949 unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 snd_rawmidi_input_trigger(substream, 1);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100952 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Takashi Iwai48c9d412005-11-17 13:56:51 +0100955static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
956 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 long result;
959 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +0100960 struct snd_rawmidi_file *rfile;
961 struct snd_rawmidi_substream *substream;
962 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 rfile = file->private_data;
965 substream = rfile->input;
966 if (substream == NULL)
967 return -EIO;
968 runtime = substream->runtime;
969 snd_rawmidi_input_trigger(substream, 1);
970 result = 0;
971 while (count > 0) {
972 spin_lock_irq(&runtime->lock);
973 while (!snd_rawmidi_ready(substream)) {
974 wait_queue_t wait;
975 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
976 spin_unlock_irq(&runtime->lock);
977 return result > 0 ? result : -EAGAIN;
978 }
979 init_waitqueue_entry(&wait, current);
980 add_wait_queue(&runtime->sleep, &wait);
981 set_current_state(TASK_INTERRUPTIBLE);
982 spin_unlock_irq(&runtime->lock);
983 schedule();
984 remove_wait_queue(&runtime->sleep, &wait);
985 if (signal_pending(current))
986 return result > 0 ? result : -ERESTARTSYS;
987 if (!runtime->avail)
988 return result > 0 ? result : -EIO;
989 spin_lock_irq(&runtime->lock);
990 }
991 spin_unlock_irq(&runtime->lock);
Clemens Ladisch4d233592005-09-05 10:35:20 +0200992 count1 = snd_rawmidi_kernel_read1(substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +0100993 (unsigned char __user *)buf,
994 NULL/*kernelbuf*/,
995 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (count1 < 0)
997 return result > 0 ? result : count1;
998 result += count1;
999 buf += count1;
1000 count -= count1;
1001 }
1002 return result;
1003}
1004
1005/**
1006 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1007 * @substream: the rawmidi substream
1008 *
1009 * Returns 1 if the internal output buffer is empty, 0 if not.
1010 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001011int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001013 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int result;
1015 unsigned long flags;
1016
1017 if (runtime->buffer == NULL) {
1018 snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
1019 return 1;
1020 }
1021 spin_lock_irqsave(&runtime->lock, flags);
1022 result = runtime->avail >= runtime->buffer_size;
1023 spin_unlock_irqrestore(&runtime->lock, flags);
1024 return result;
1025}
1026
1027/**
1028 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1029 * @substream: the rawmidi substream
1030 * @buffer: the buffer pointer
1031 * @count: data size to transfer
1032 *
1033 * Copies data from the internal output buffer to the given buffer.
1034 *
1035 * Call this in the interrupt handler when the midi output is ready,
1036 * and call snd_rawmidi_transmit_ack() after the transmission is
1037 * finished.
1038 *
1039 * Returns the size of copied data, or a negative error code on failure.
1040 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001041int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1042 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 unsigned long flags;
1045 int result, count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001046 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 if (runtime->buffer == NULL) {
1049 snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
1050 return -EINVAL;
1051 }
1052 result = 0;
1053 spin_lock_irqsave(&runtime->lock, flags);
1054 if (runtime->avail >= runtime->buffer_size) {
1055 /* warning: lowlevel layer MUST trigger down the hardware */
1056 goto __skip;
1057 }
1058 if (count == 1) { /* special case, faster code */
1059 *buffer = runtime->buffer[runtime->hw_ptr];
1060 result++;
1061 } else {
1062 count1 = runtime->buffer_size - runtime->hw_ptr;
1063 if (count1 > count)
1064 count1 = count;
1065 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1066 count1 = runtime->buffer_size - runtime->avail;
1067 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1068 count -= count1;
1069 result += count1;
1070 if (count > 0) {
1071 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1072 count = runtime->buffer_size - runtime->avail - count1;
1073 memcpy(buffer + count1, runtime->buffer, count);
1074 result += count;
1075 }
1076 }
1077 __skip:
1078 spin_unlock_irqrestore(&runtime->lock, flags);
1079 return result;
1080}
1081
1082/**
1083 * snd_rawmidi_transmit_ack - acknowledge the transmission
1084 * @substream: the rawmidi substream
1085 * @count: the tranferred count
1086 *
1087 * Advances the hardware pointer for the internal output buffer with
1088 * the given size and updates the condition.
1089 * Call after the transmission is finished.
1090 *
1091 * Returns the advanced size if successful, or a negative error code on failure.
1092 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001093int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 unsigned long flags;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001096 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 if (runtime->buffer == NULL) {
1099 snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
1100 return -EINVAL;
1101 }
1102 spin_lock_irqsave(&runtime->lock, flags);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001103 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 runtime->hw_ptr += count;
1105 runtime->hw_ptr %= runtime->buffer_size;
1106 runtime->avail += count;
1107 substream->bytes += count;
1108 if (count > 0) {
1109 if (runtime->drain || snd_rawmidi_ready(substream))
1110 wake_up(&runtime->sleep);
1111 }
1112 spin_unlock_irqrestore(&runtime->lock, flags);
1113 return count;
1114}
1115
1116/**
1117 * snd_rawmidi_transmit - copy from the buffer to the device
1118 * @substream: the rawmidi substream
Takashi Iwaidf8db932005-09-07 13:38:19 +02001119 * @buffer: the buffer pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 * @count: the data size to transfer
1121 *
1122 * Copies data from the buffer to the device and advances the pointer.
1123 *
1124 * Returns the copied size if successful, or a negative error code on failure.
1125 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001126int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1127 unsigned char *buffer, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 count = snd_rawmidi_transmit_peek(substream, buffer, count);
1130 if (count < 0)
1131 return count;
1132 return snd_rawmidi_transmit_ack(substream, count);
1133}
1134
Takashi Iwai48c9d412005-11-17 13:56:51 +01001135static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001136 const unsigned char __user *userbuf,
1137 const unsigned char *kernelbuf,
1138 long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 unsigned long flags;
1141 long count1, result;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001142 struct snd_rawmidi_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001144 if (snd_BUG_ON(!kernelbuf && !userbuf))
1145 return -EINVAL;
1146 if (snd_BUG_ON(!runtime->buffer))
1147 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 result = 0;
1150 spin_lock_irqsave(&runtime->lock, flags);
1151 if (substream->append) {
1152 if ((long)runtime->avail < count) {
1153 spin_unlock_irqrestore(&runtime->lock, flags);
1154 return -EAGAIN;
1155 }
1156 }
1157 while (count > 0 && runtime->avail > 0) {
1158 count1 = runtime->buffer_size - runtime->appl_ptr;
1159 if (count1 > count)
1160 count1 = count;
1161 if (count1 > (long)runtime->avail)
1162 count1 = runtime->avail;
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001163 if (kernelbuf)
1164 memcpy(runtime->buffer + runtime->appl_ptr,
1165 kernelbuf + result, count1);
1166 else if (userbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 spin_unlock_irqrestore(&runtime->lock, flags);
1168 if (copy_from_user(runtime->buffer + runtime->appl_ptr,
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001169 userbuf + result, count1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 spin_lock_irqsave(&runtime->lock, flags);
1171 result = result > 0 ? result : -EFAULT;
1172 goto __end;
1173 }
1174 spin_lock_irqsave(&runtime->lock, flags);
1175 }
1176 runtime->appl_ptr += count1;
1177 runtime->appl_ptr %= runtime->buffer_size;
1178 runtime->avail -= count1;
1179 result += count1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 count -= count1;
1181 }
1182 __end:
1183 count1 = runtime->avail < runtime->buffer_size;
1184 spin_unlock_irqrestore(&runtime->lock, flags);
1185 if (count1)
1186 snd_rawmidi_output_trigger(substream, 1);
1187 return result;
1188}
1189
Takashi Iwai48c9d412005-11-17 13:56:51 +01001190long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1191 const unsigned char *buf, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001193 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Takashi Iwai48c9d412005-11-17 13:56:51 +01001196static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1197 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 long result, timeout;
1200 int count1;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001201 struct snd_rawmidi_file *rfile;
1202 struct snd_rawmidi_runtime *runtime;
1203 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 rfile = file->private_data;
1206 substream = rfile->output;
1207 runtime = substream->runtime;
1208 /* we cannot put an atomic message to our buffer */
1209 if (substream->append && count > runtime->buffer_size)
1210 return -EIO;
1211 result = 0;
1212 while (count > 0) {
1213 spin_lock_irq(&runtime->lock);
1214 while (!snd_rawmidi_ready_append(substream, count)) {
1215 wait_queue_t wait;
1216 if (file->f_flags & O_NONBLOCK) {
1217 spin_unlock_irq(&runtime->lock);
1218 return result > 0 ? result : -EAGAIN;
1219 }
1220 init_waitqueue_entry(&wait, current);
1221 add_wait_queue(&runtime->sleep, &wait);
1222 set_current_state(TASK_INTERRUPTIBLE);
1223 spin_unlock_irq(&runtime->lock);
1224 timeout = schedule_timeout(30 * HZ);
1225 remove_wait_queue(&runtime->sleep, &wait);
1226 if (signal_pending(current))
1227 return result > 0 ? result : -ERESTARTSYS;
1228 if (!runtime->avail && !timeout)
1229 return result > 0 ? result : -EIO;
1230 spin_lock_irq(&runtime->lock);
1231 }
1232 spin_unlock_irq(&runtime->lock);
Marcin Åšlusarz17596a82008-01-09 17:56:07 +01001233 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (count1 < 0)
1235 return result > 0 ? result : count1;
1236 result += count1;
1237 buf += count1;
1238 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1239 break;
1240 count -= count1;
1241 }
1242 if (file->f_flags & O_SYNC) {
1243 spin_lock_irq(&runtime->lock);
1244 while (runtime->avail != runtime->buffer_size) {
1245 wait_queue_t wait;
1246 unsigned int last_avail = runtime->avail;
1247 init_waitqueue_entry(&wait, current);
1248 add_wait_queue(&runtime->sleep, &wait);
1249 set_current_state(TASK_INTERRUPTIBLE);
1250 spin_unlock_irq(&runtime->lock);
1251 timeout = schedule_timeout(30 * HZ);
1252 remove_wait_queue(&runtime->sleep, &wait);
1253 if (signal_pending(current))
1254 return result > 0 ? result : -ERESTARTSYS;
1255 if (runtime->avail == last_avail && !timeout)
1256 return result > 0 ? result : -EIO;
1257 spin_lock_irq(&runtime->lock);
1258 }
1259 spin_unlock_irq(&runtime->lock);
1260 }
1261 return result;
1262}
1263
1264static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1265{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001266 struct snd_rawmidi_file *rfile;
1267 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 unsigned int mask;
1269
1270 rfile = file->private_data;
1271 if (rfile->input != NULL) {
1272 runtime = rfile->input->runtime;
1273 snd_rawmidi_input_trigger(rfile->input, 1);
1274 poll_wait(file, &runtime->sleep, wait);
1275 }
1276 if (rfile->output != NULL) {
1277 runtime = rfile->output->runtime;
1278 poll_wait(file, &runtime->sleep, wait);
1279 }
1280 mask = 0;
1281 if (rfile->input != NULL) {
1282 if (snd_rawmidi_ready(rfile->input))
1283 mask |= POLLIN | POLLRDNORM;
1284 }
1285 if (rfile->output != NULL) {
1286 if (snd_rawmidi_ready(rfile->output))
1287 mask |= POLLOUT | POLLWRNORM;
1288 }
1289 return mask;
1290}
1291
1292/*
1293 */
1294#ifdef CONFIG_COMPAT
1295#include "rawmidi_compat.c"
1296#else
1297#define snd_rawmidi_ioctl_compat NULL
1298#endif
1299
1300/*
1301
1302 */
1303
Takashi Iwai48c9d412005-11-17 13:56:51 +01001304static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1305 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001307 struct snd_rawmidi *rmidi;
1308 struct snd_rawmidi_substream *substream;
1309 struct snd_rawmidi_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 rmidi = entry->private_data;
1312 snd_iprintf(buffer, "%s\n\n", rmidi->name);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001313 mutex_lock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001315 list_for_each_entry(substream,
1316 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1317 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 snd_iprintf(buffer,
1319 "Output %d\n"
1320 " Tx bytes : %lu\n",
1321 substream->number,
1322 (unsigned long) substream->bytes);
1323 if (substream->opened) {
1324 runtime = substream->runtime;
1325 snd_iprintf(buffer,
1326 " Mode : %s\n"
1327 " Buffer size : %lu\n"
1328 " Avail : %lu\n",
1329 runtime->oss ? "OSS compatible" : "native",
1330 (unsigned long) runtime->buffer_size,
1331 (unsigned long) runtime->avail);
1332 }
1333 }
1334 }
1335 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
Johannes Berg9244b2c2006-10-05 16:02:22 +02001336 list_for_each_entry(substream,
1337 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1338 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 snd_iprintf(buffer,
1340 "Input %d\n"
1341 " Rx bytes : %lu\n",
1342 substream->number,
1343 (unsigned long) substream->bytes);
1344 if (substream->opened) {
1345 runtime = substream->runtime;
1346 snd_iprintf(buffer,
1347 " Buffer size : %lu\n"
1348 " Avail : %lu\n"
1349 " Overruns : %lu\n",
1350 (unsigned long) runtime->buffer_size,
1351 (unsigned long) runtime->avail,
1352 (unsigned long) runtime->xruns);
1353 }
1354 }
1355 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001356 mutex_unlock(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359/*
1360 * Register functions
1361 */
1362
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001363static const struct file_operations snd_rawmidi_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 .owner = THIS_MODULE,
1366 .read = snd_rawmidi_read,
1367 .write = snd_rawmidi_write,
1368 .open = snd_rawmidi_open,
1369 .release = snd_rawmidi_release,
1370 .poll = snd_rawmidi_poll,
1371 .unlocked_ioctl = snd_rawmidi_ioctl,
1372 .compat_ioctl = snd_rawmidi_ioctl_compat,
1373};
1374
Takashi Iwai48c9d412005-11-17 13:56:51 +01001375static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1376 struct snd_rawmidi_str *stream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 int direction,
1378 int count)
1379{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001380 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 int idx;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 for (idx = 0; idx < count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +02001384 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001385 if (substream == NULL) {
1386 snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 substream->stream = direction;
1390 substream->number = idx;
1391 substream->rmidi = rmidi;
1392 substream->pstr = stream;
1393 list_add_tail(&substream->list, &stream->substreams);
1394 stream->substream_count++;
1395 }
1396 return 0;
1397}
1398
1399/**
1400 * snd_rawmidi_new - create a rawmidi instance
1401 * @card: the card instance
1402 * @id: the id string
1403 * @device: the device index
1404 * @output_count: the number of output streams
1405 * @input_count: the number of input streams
1406 * @rrawmidi: the pointer to store the new rawmidi instance
1407 *
1408 * Creates a new rawmidi instance.
1409 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1410 *
1411 * Returns zero if successful, or a negative error code on failure.
1412 */
Takashi Iwai48c9d412005-11-17 13:56:51 +01001413int snd_rawmidi_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 int output_count, int input_count,
Takashi Iwai48c9d412005-11-17 13:56:51 +01001415 struct snd_rawmidi ** rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001417 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +01001419 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 .dev_free = snd_rawmidi_dev_free,
1421 .dev_register = snd_rawmidi_dev_register,
1422 .dev_disconnect = snd_rawmidi_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 };
1424
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001425 if (snd_BUG_ON(!card))
1426 return -ENXIO;
1427 if (rrawmidi)
1428 *rrawmidi = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001429 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001430 if (rmidi == NULL) {
1431 snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 rmidi->card = card;
1435 rmidi->device = device;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001436 mutex_init(&rmidi->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 init_waitqueue_head(&rmidi->open_wait);
Akinobu Mitac13893d2006-11-23 12:02:33 +01001438 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1439 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (id != NULL)
1442 strlcpy(rmidi->id, id, sizeof(rmidi->id));
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001443 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1444 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1445 SNDRV_RAWMIDI_STREAM_INPUT,
1446 input_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 snd_rawmidi_free(rmidi);
1448 return err;
1449 }
Takashi Iwai73e77ba2005-11-17 17:44:01 +01001450 if ((err = snd_rawmidi_alloc_substreams(rmidi,
1451 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1452 SNDRV_RAWMIDI_STREAM_OUTPUT,
1453 output_count)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 snd_rawmidi_free(rmidi);
1455 return err;
1456 }
1457 if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1458 snd_rawmidi_free(rmidi);
1459 return err;
1460 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001461 if (rrawmidi)
1462 *rrawmidi = rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 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{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001479 if (!rmidi)
1480 return 0;
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{
Takashi Iwai48c9d412005-11-17 13:56:51 +01001626 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Johannes Berg9244b2c2006-10-05 16:02:22 +02001628 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 substream->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
1632/*
1633 * ENTRY functions
1634 */
1635
1636static int __init alsa_rawmidi_init(void)
1637{
1638
1639 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1640 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1641#ifdef CONFIG_SND_OSSEMUL
1642 { int i;
1643 /* check device map table */
1644 for (i = 0; i < SNDRV_CARDS; i++) {
1645 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1646 snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
1647 midi_map[i] = 0;
1648 }
1649 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1650 snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
1651 amidi_map[i] = 1;
1652 }
1653 }
1654 }
1655#endif /* CONFIG_SND_OSSEMUL */
1656 return 0;
1657}
1658
1659static void __exit alsa_rawmidi_exit(void)
1660{
1661 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1662 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1663}
1664
1665module_init(alsa_rawmidi_init)
1666module_exit(alsa_rawmidi_exit)
1667
1668EXPORT_SYMBOL(snd_rawmidi_output_params);
1669EXPORT_SYMBOL(snd_rawmidi_input_params);
1670EXPORT_SYMBOL(snd_rawmidi_drop_output);
1671EXPORT_SYMBOL(snd_rawmidi_drain_output);
1672EXPORT_SYMBOL(snd_rawmidi_drain_input);
1673EXPORT_SYMBOL(snd_rawmidi_receive);
1674EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1675EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1676EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1677EXPORT_SYMBOL(snd_rawmidi_transmit);
1678EXPORT_SYMBOL(snd_rawmidi_new);
1679EXPORT_SYMBOL(snd_rawmidi_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680EXPORT_SYMBOL(snd_rawmidi_info_select);
1681EXPORT_SYMBOL(snd_rawmidi_kernel_open);
1682EXPORT_SYMBOL(snd_rawmidi_kernel_release);
1683EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1684EXPORT_SYMBOL(snd_rawmidi_kernel_write);