]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
31 | #include <linux/moduleparam.h> | |
32 | #include <linux/delay.h> | |
33 | #include <linux/wait.h> | |
34 | #include <sound/rawmidi.h> | |
35 | #include <sound/info.h> | |
36 | #include <sound/control.h> | |
37 | #include <sound/minors.h> | |
38 | #include <sound/initval.h> | |
39 | ||
40 | MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>"); | |
41 | MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA."); | |
42 | MODULE_LICENSE("GPL"); | |
43 | ||
44 | #ifdef CONFIG_SND_OSSEMUL | |
45 | static int midi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0}; | |
46 | static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1}; | |
47 | module_param_array(midi_map, int, NULL, 0444); | |
48 | MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device."); | |
49 | module_param_array(amidi_map, int, NULL, 0444); | |
50 | MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device."); | |
51 | #endif /* CONFIG_SND_OSSEMUL */ | |
52 | ||
48c9d417 TI |
53 | static int snd_rawmidi_free(struct snd_rawmidi *rawmidi); |
54 | static int snd_rawmidi_dev_free(struct snd_device *device); | |
55 | static int snd_rawmidi_dev_register(struct snd_device *device); | |
56 | static int snd_rawmidi_dev_disconnect(struct snd_device *device); | |
57 | static int snd_rawmidi_dev_unregister(struct snd_device *device); | |
1da177e4 | 58 | |
48c9d417 | 59 | static struct snd_rawmidi *snd_rawmidi_devices[SNDRV_CARDS * SNDRV_RAWMIDI_DEVICES]; |
1da177e4 LT |
60 | |
61 | static DECLARE_MUTEX(register_mutex); | |
62 | ||
63 | static inline unsigned short snd_rawmidi_file_flags(struct file *file) | |
64 | { | |
65 | switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) { | |
66 | case FMODE_WRITE: | |
67 | return SNDRV_RAWMIDI_LFLG_OUTPUT; | |
68 | case FMODE_READ: | |
69 | return SNDRV_RAWMIDI_LFLG_INPUT; | |
70 | default: | |
71 | return SNDRV_RAWMIDI_LFLG_OPEN; | |
72 | } | |
73 | } | |
74 | ||
48c9d417 | 75 | static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream) |
1da177e4 | 76 | { |
48c9d417 | 77 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
78 | return runtime->avail >= runtime->avail_min; |
79 | } | |
80 | ||
48c9d417 TI |
81 | static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream, |
82 | size_t count) | |
1da177e4 | 83 | { |
48c9d417 | 84 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
85 | return runtime->avail >= runtime->avail_min && |
86 | (!substream->append || runtime->avail >= count); | |
87 | } | |
88 | ||
89 | static void snd_rawmidi_input_event_tasklet(unsigned long data) | |
90 | { | |
48c9d417 | 91 | struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data; |
1da177e4 LT |
92 | substream->runtime->event(substream); |
93 | } | |
94 | ||
95 | static void snd_rawmidi_output_trigger_tasklet(unsigned long data) | |
96 | { | |
48c9d417 | 97 | struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data; |
1da177e4 LT |
98 | substream->ops->trigger(substream, 1); |
99 | } | |
100 | ||
48c9d417 | 101 | static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream) |
1da177e4 | 102 | { |
48c9d417 | 103 | struct snd_rawmidi_runtime *runtime; |
1da177e4 | 104 | |
ca2c0966 | 105 | if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL) |
1da177e4 LT |
106 | return -ENOMEM; |
107 | spin_lock_init(&runtime->lock); | |
108 | init_waitqueue_head(&runtime->sleep); | |
109 | if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT) | |
110 | tasklet_init(&runtime->tasklet, | |
111 | snd_rawmidi_input_event_tasklet, | |
112 | (unsigned long)substream); | |
113 | else | |
114 | tasklet_init(&runtime->tasklet, | |
115 | snd_rawmidi_output_trigger_tasklet, | |
116 | (unsigned long)substream); | |
117 | runtime->event = NULL; | |
118 | runtime->buffer_size = PAGE_SIZE; | |
119 | runtime->avail_min = 1; | |
120 | if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT) | |
121 | runtime->avail = 0; | |
122 | else | |
123 | runtime->avail = runtime->buffer_size; | |
124 | if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) { | |
125 | kfree(runtime); | |
126 | return -ENOMEM; | |
127 | } | |
128 | runtime->appl_ptr = runtime->hw_ptr = 0; | |
129 | substream->runtime = runtime; | |
130 | return 0; | |
131 | } | |
132 | ||
48c9d417 | 133 | static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream) |
1da177e4 | 134 | { |
48c9d417 | 135 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
136 | |
137 | kfree(runtime->buffer); | |
138 | kfree(runtime); | |
139 | substream->runtime = NULL; | |
140 | return 0; | |
141 | } | |
142 | ||
48c9d417 | 143 | static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up) |
1da177e4 LT |
144 | { |
145 | if (up) { | |
146 | tasklet_hi_schedule(&substream->runtime->tasklet); | |
147 | } else { | |
148 | tasklet_kill(&substream->runtime->tasklet); | |
149 | substream->ops->trigger(substream, 0); | |
150 | } | |
151 | } | |
152 | ||
48c9d417 | 153 | static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up) |
1da177e4 LT |
154 | { |
155 | substream->ops->trigger(substream, up); | |
156 | if (!up && substream->runtime->event) | |
157 | tasklet_kill(&substream->runtime->tasklet); | |
158 | } | |
159 | ||
48c9d417 | 160 | int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream) |
1da177e4 LT |
161 | { |
162 | unsigned long flags; | |
48c9d417 | 163 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
164 | |
165 | snd_rawmidi_output_trigger(substream, 0); | |
166 | runtime->drain = 0; | |
167 | spin_lock_irqsave(&runtime->lock, flags); | |
168 | runtime->appl_ptr = runtime->hw_ptr = 0; | |
169 | runtime->avail = runtime->buffer_size; | |
170 | spin_unlock_irqrestore(&runtime->lock, flags); | |
171 | return 0; | |
172 | } | |
173 | ||
48c9d417 | 174 | int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream) |
1da177e4 LT |
175 | { |
176 | int err; | |
177 | long timeout; | |
48c9d417 | 178 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
179 | |
180 | err = 0; | |
181 | runtime->drain = 1; | |
182 | timeout = wait_event_interruptible_timeout(runtime->sleep, | |
183 | (runtime->avail >= runtime->buffer_size), | |
184 | 10*HZ); | |
185 | if (signal_pending(current)) | |
186 | err = -ERESTARTSYS; | |
187 | if (runtime->avail < runtime->buffer_size && !timeout) { | |
188 | snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size); | |
189 | err = -EIO; | |
190 | } | |
191 | runtime->drain = 0; | |
192 | if (err != -ERESTARTSYS) { | |
193 | /* we need wait a while to make sure that Tx FIFOs are empty */ | |
194 | if (substream->ops->drain) | |
195 | substream->ops->drain(substream); | |
196 | else | |
197 | msleep(50); | |
198 | snd_rawmidi_drop_output(substream); | |
199 | } | |
200 | return err; | |
201 | } | |
202 | ||
48c9d417 | 203 | int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream) |
1da177e4 LT |
204 | { |
205 | unsigned long flags; | |
48c9d417 | 206 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
207 | |
208 | snd_rawmidi_input_trigger(substream, 0); | |
209 | runtime->drain = 0; | |
210 | spin_lock_irqsave(&runtime->lock, flags); | |
211 | runtime->appl_ptr = runtime->hw_ptr = 0; | |
212 | runtime->avail = 0; | |
213 | spin_unlock_irqrestore(&runtime->lock, flags); | |
214 | return 0; | |
215 | } | |
216 | ||
217 | int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice, | |
48c9d417 | 218 | int mode, struct snd_rawmidi_file * rfile) |
1da177e4 | 219 | { |
48c9d417 | 220 | struct snd_rawmidi *rmidi; |
1da177e4 | 221 | struct list_head *list1, *list2; |
48c9d417 TI |
222 | struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL; |
223 | struct snd_rawmidi_runtime *input = NULL, *output = NULL; | |
1da177e4 LT |
224 | int err; |
225 | ||
226 | if (rfile) | |
227 | rfile->input = rfile->output = NULL; | |
228 | rmidi = snd_rawmidi_devices[(cardnum * SNDRV_RAWMIDI_DEVICES) + device]; | |
229 | if (rmidi == NULL) { | |
230 | err = -ENODEV; | |
231 | goto __error1; | |
232 | } | |
233 | if (!try_module_get(rmidi->card->module)) { | |
234 | err = -EFAULT; | |
235 | goto __error1; | |
236 | } | |
237 | if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK)) | |
238 | down(&rmidi->open_mutex); | |
239 | if (mode & SNDRV_RAWMIDI_LFLG_INPUT) { | |
240 | if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) { | |
241 | err = -ENXIO; | |
242 | goto __error; | |
243 | } | |
244 | if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) { | |
245 | err = -ENODEV; | |
246 | goto __error; | |
247 | } | |
248 | if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened >= | |
249 | rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_count) { | |
250 | err = -EAGAIN; | |
251 | goto __error; | |
252 | } | |
253 | } | |
254 | if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) { | |
255 | if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT)) { | |
256 | err = -ENXIO; | |
257 | goto __error; | |
258 | } | |
259 | if (subdevice >= 0 && (unsigned int)subdevice >= rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) { | |
260 | err = -ENODEV; | |
261 | goto __error; | |
262 | } | |
263 | if (rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened >= | |
264 | rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_count) { | |
265 | err = -EAGAIN; | |
266 | goto __error; | |
267 | } | |
268 | } | |
269 | list1 = rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams.next; | |
270 | while (1) { | |
271 | if (list1 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) { | |
272 | sinput = NULL; | |
273 | if (mode & SNDRV_RAWMIDI_LFLG_INPUT) { | |
274 | err = -EAGAIN; | |
275 | goto __error; | |
276 | } | |
277 | break; | |
278 | } | |
48c9d417 | 279 | sinput = list_entry(list1, struct snd_rawmidi_substream, list); |
1da177e4 LT |
280 | if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened) |
281 | goto __nexti; | |
282 | if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number)) | |
283 | break; | |
284 | __nexti: | |
285 | list1 = list1->next; | |
286 | } | |
287 | list2 = rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams.next; | |
288 | while (1) { | |
289 | if (list2 == &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) { | |
290 | soutput = NULL; | |
291 | if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) { | |
292 | err = -EAGAIN; | |
293 | goto __error; | |
294 | } | |
295 | break; | |
296 | } | |
48c9d417 | 297 | soutput = list_entry(list2, struct snd_rawmidi_substream, list); |
1da177e4 LT |
298 | if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) { |
299 | if (mode & SNDRV_RAWMIDI_LFLG_APPEND) { | |
300 | if (soutput->opened && !soutput->append) | |
301 | goto __nexto; | |
302 | } else { | |
303 | if (soutput->opened) | |
304 | goto __nexto; | |
305 | } | |
306 | } | |
307 | if (subdevice < 0 || (subdevice >= 0 && subdevice == soutput->number)) | |
308 | break; | |
309 | __nexto: | |
310 | list2 = list2->next; | |
311 | } | |
312 | if (mode & SNDRV_RAWMIDI_LFLG_INPUT) { | |
313 | if ((err = snd_rawmidi_runtime_create(sinput)) < 0) | |
314 | goto __error; | |
315 | input = sinput->runtime; | |
316 | if ((err = sinput->ops->open(sinput)) < 0) | |
317 | goto __error; | |
318 | sinput->opened = 1; | |
319 | rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++; | |
320 | } else { | |
321 | sinput = NULL; | |
322 | } | |
323 | if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) { | |
324 | if (soutput->opened) | |
325 | goto __skip_output; | |
326 | if ((err = snd_rawmidi_runtime_create(soutput)) < 0) { | |
327 | if (mode & SNDRV_RAWMIDI_LFLG_INPUT) | |
328 | sinput->ops->close(sinput); | |
329 | goto __error; | |
330 | } | |
331 | output = soutput->runtime; | |
332 | if ((err = soutput->ops->open(soutput)) < 0) { | |
333 | if (mode & SNDRV_RAWMIDI_LFLG_INPUT) | |
334 | sinput->ops->close(sinput); | |
335 | goto __error; | |
336 | } | |
337 | __skip_output: | |
338 | soutput->opened = 1; | |
339 | if (mode & SNDRV_RAWMIDI_LFLG_APPEND) | |
340 | soutput->append = 1; | |
341 | if (soutput->use_count++ == 0) | |
342 | soutput->active_sensing = 1; | |
343 | rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened++; | |
344 | } else { | |
345 | soutput = NULL; | |
346 | } | |
347 | if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK)) | |
348 | up(&rmidi->open_mutex); | |
349 | if (rfile) { | |
350 | rfile->rmidi = rmidi; | |
351 | rfile->input = sinput; | |
352 | rfile->output = soutput; | |
353 | } | |
354 | return 0; | |
355 | ||
356 | __error: | |
357 | if (input != NULL) | |
358 | snd_rawmidi_runtime_free(sinput); | |
359 | if (output != NULL) | |
360 | snd_rawmidi_runtime_free(soutput); | |
361 | module_put(rmidi->card->module); | |
362 | if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK)) | |
363 | up(&rmidi->open_mutex); | |
364 | __error1: | |
365 | return err; | |
366 | } | |
367 | ||
368 | static int snd_rawmidi_open(struct inode *inode, struct file *file) | |
369 | { | |
370 | int maj = imajor(inode); | |
371 | int cardnum; | |
48c9d417 | 372 | struct snd_card *card; |
1da177e4 LT |
373 | int device, subdevice; |
374 | unsigned short fflags; | |
375 | int err; | |
48c9d417 TI |
376 | struct snd_rawmidi *rmidi; |
377 | struct snd_rawmidi_file *rawmidi_file; | |
1da177e4 LT |
378 | wait_queue_t wait; |
379 | struct list_head *list; | |
48c9d417 | 380 | struct snd_ctl_file *kctl; |
1da177e4 | 381 | |
f1902860 | 382 | if (maj == snd_major) { |
1da177e4 LT |
383 | cardnum = SNDRV_MINOR_CARD(iminor(inode)); |
384 | cardnum %= SNDRV_CARDS; | |
385 | device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_RAWMIDI; | |
386 | device %= SNDRV_MINOR_RAWMIDIS; | |
1da177e4 | 387 | #ifdef CONFIG_SND_OSSEMUL |
f1902860 | 388 | } else if (maj == SOUND_MAJOR) { |
1da177e4 LT |
389 | cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode)); |
390 | cardnum %= SNDRV_CARDS; | |
391 | device = SNDRV_MINOR_OSS_DEVICE(iminor(inode)) == SNDRV_MINOR_OSS_MIDI ? | |
392 | midi_map[cardnum] : amidi_map[cardnum]; | |
1da177e4 | 393 | #endif |
f1902860 | 394 | } else |
1da177e4 | 395 | return -ENXIO; |
1da177e4 LT |
396 | |
397 | rmidi = snd_rawmidi_devices[(cardnum * SNDRV_RAWMIDI_DEVICES) + device]; | |
398 | if (rmidi == NULL) | |
399 | return -ENODEV; | |
400 | #ifdef CONFIG_SND_OSSEMUL | |
401 | if (maj == SOUND_MAJOR && !rmidi->ossreg) | |
402 | return -ENXIO; | |
403 | #endif | |
404 | if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK)) | |
405 | return -EINVAL; /* invalid combination */ | |
406 | card = rmidi->card; | |
407 | err = snd_card_file_add(card, file); | |
408 | if (err < 0) | |
409 | return -ENODEV; | |
410 | fflags = snd_rawmidi_file_flags(file); | |
f1902860 | 411 | if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */ |
1da177e4 LT |
412 | fflags |= SNDRV_RAWMIDI_LFLG_APPEND; |
413 | fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK; | |
414 | rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL); | |
415 | if (rawmidi_file == NULL) { | |
416 | snd_card_file_remove(card, file); | |
417 | return -ENOMEM; | |
418 | } | |
419 | init_waitqueue_entry(&wait, current); | |
420 | add_wait_queue(&rmidi->open_wait, &wait); | |
421 | down(&rmidi->open_mutex); | |
422 | while (1) { | |
423 | subdevice = -1; | |
424 | down_read(&card->controls_rwsem); | |
425 | list_for_each(list, &card->ctl_files) { | |
426 | kctl = snd_ctl_file(list); | |
427 | if (kctl->pid == current->pid) { | |
428 | subdevice = kctl->prefer_rawmidi_subdevice; | |
429 | break; | |
430 | } | |
431 | } | |
432 | up_read(&card->controls_rwsem); | |
433 | err = snd_rawmidi_kernel_open(cardnum, device, subdevice, fflags, rawmidi_file); | |
434 | if (err >= 0) | |
435 | break; | |
436 | if (err == -EAGAIN) { | |
437 | if (file->f_flags & O_NONBLOCK) { | |
438 | err = -EBUSY; | |
439 | break; | |
440 | } | |
441 | } else | |
442 | break; | |
443 | set_current_state(TASK_INTERRUPTIBLE); | |
444 | up(&rmidi->open_mutex); | |
445 | schedule(); | |
446 | down(&rmidi->open_mutex); | |
447 | if (signal_pending(current)) { | |
448 | err = -ERESTARTSYS; | |
449 | break; | |
450 | } | |
451 | } | |
452 | #ifdef CONFIG_SND_OSSEMUL | |
453 | if (rawmidi_file->input && rawmidi_file->input->runtime) | |
454 | rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR); | |
455 | if (rawmidi_file->output && rawmidi_file->output->runtime) | |
456 | rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR); | |
457 | #endif | |
458 | remove_wait_queue(&rmidi->open_wait, &wait); | |
459 | if (err >= 0) { | |
460 | file->private_data = rawmidi_file; | |
461 | } else { | |
462 | snd_card_file_remove(card, file); | |
463 | kfree(rawmidi_file); | |
464 | } | |
465 | up(&rmidi->open_mutex); | |
466 | return err; | |
467 | } | |
468 | ||
48c9d417 | 469 | int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile) |
1da177e4 | 470 | { |
48c9d417 TI |
471 | struct snd_rawmidi *rmidi; |
472 | struct snd_rawmidi_substream *substream; | |
473 | struct snd_rawmidi_runtime *runtime; | |
1da177e4 LT |
474 | |
475 | snd_assert(rfile != NULL, return -ENXIO); | |
476 | snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO); | |
477 | rmidi = rfile->rmidi; | |
478 | down(&rmidi->open_mutex); | |
479 | if (rfile->input != NULL) { | |
480 | substream = rfile->input; | |
481 | rfile->input = NULL; | |
482 | runtime = substream->runtime; | |
483 | snd_rawmidi_input_trigger(substream, 0); | |
484 | substream->ops->close(substream); | |
485 | if (runtime->private_free != NULL) | |
486 | runtime->private_free(substream); | |
487 | snd_rawmidi_runtime_free(substream); | |
488 | substream->opened = 0; | |
489 | rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--; | |
490 | } | |
491 | if (rfile->output != NULL) { | |
492 | substream = rfile->output; | |
493 | rfile->output = NULL; | |
494 | if (--substream->use_count == 0) { | |
495 | runtime = substream->runtime; | |
496 | if (substream->active_sensing) { | |
497 | unsigned char buf = 0xfe; | |
498 | /* sending single active sensing message to shut the device up */ | |
499 | snd_rawmidi_kernel_write(substream, &buf, 1); | |
500 | } | |
501 | if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS) | |
502 | snd_rawmidi_output_trigger(substream, 0); | |
503 | substream->ops->close(substream); | |
504 | if (runtime->private_free != NULL) | |
505 | runtime->private_free(substream); | |
506 | snd_rawmidi_runtime_free(substream); | |
507 | substream->opened = 0; | |
508 | substream->append = 0; | |
509 | } | |
510 | rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--; | |
511 | } | |
512 | up(&rmidi->open_mutex); | |
513 | module_put(rmidi->card->module); | |
514 | return 0; | |
515 | } | |
516 | ||
517 | static int snd_rawmidi_release(struct inode *inode, struct file *file) | |
518 | { | |
48c9d417 TI |
519 | struct snd_rawmidi_file *rfile; |
520 | struct snd_rawmidi *rmidi; | |
1da177e4 LT |
521 | int err; |
522 | ||
523 | rfile = file->private_data; | |
524 | err = snd_rawmidi_kernel_release(rfile); | |
525 | rmidi = rfile->rmidi; | |
526 | wake_up(&rmidi->open_wait); | |
527 | kfree(rfile); | |
528 | snd_card_file_remove(rmidi->card, file); | |
529 | return err; | |
530 | } | |
531 | ||
48c9d417 TI |
532 | int snd_rawmidi_info(struct snd_rawmidi_substream *substream, |
533 | struct snd_rawmidi_info *info) | |
1da177e4 | 534 | { |
48c9d417 | 535 | struct snd_rawmidi *rmidi; |
1da177e4 LT |
536 | |
537 | if (substream == NULL) | |
538 | return -ENODEV; | |
539 | rmidi = substream->rmidi; | |
540 | memset(info, 0, sizeof(*info)); | |
541 | info->card = rmidi->card->number; | |
542 | info->device = rmidi->device; | |
543 | info->subdevice = substream->number; | |
544 | info->stream = substream->stream; | |
545 | info->flags = rmidi->info_flags; | |
546 | strcpy(info->id, rmidi->id); | |
547 | strcpy(info->name, rmidi->name); | |
548 | strcpy(info->subname, substream->name); | |
549 | info->subdevices_count = substream->pstr->substream_count; | |
550 | info->subdevices_avail = (substream->pstr->substream_count - | |
551 | substream->pstr->substream_opened); | |
552 | return 0; | |
553 | } | |
554 | ||
48c9d417 TI |
555 | static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream, |
556 | struct snd_rawmidi_info __user * _info) | |
1da177e4 | 557 | { |
48c9d417 | 558 | struct snd_rawmidi_info info; |
1da177e4 LT |
559 | int err; |
560 | if ((err = snd_rawmidi_info(substream, &info)) < 0) | |
561 | return err; | |
48c9d417 | 562 | if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info))) |
1da177e4 LT |
563 | return -EFAULT; |
564 | return 0; | |
565 | } | |
566 | ||
48c9d417 | 567 | int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info) |
1da177e4 | 568 | { |
48c9d417 TI |
569 | struct snd_rawmidi *rmidi; |
570 | struct snd_rawmidi_str *pstr; | |
571 | struct snd_rawmidi_substream *substream; | |
1da177e4 LT |
572 | struct list_head *list; |
573 | if (info->device >= SNDRV_RAWMIDI_DEVICES) | |
574 | return -ENXIO; | |
575 | rmidi = snd_rawmidi_devices[card->number * SNDRV_RAWMIDI_DEVICES + info->device]; | |
576 | 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; | |
583 | list_for_each(list, &pstr->substreams) { | |
48c9d417 | 584 | substream = list_entry(list, struct snd_rawmidi_substream, list); |
1da177e4 LT |
585 | if ((unsigned int)substream->number == info->subdevice) |
586 | return snd_rawmidi_info(substream, info); | |
587 | } | |
588 | return -ENXIO; | |
589 | } | |
590 | ||
48c9d417 TI |
591 | static int snd_rawmidi_info_select_user(struct snd_card *card, |
592 | struct snd_rawmidi_info __user *_info) | |
1da177e4 LT |
593 | { |
594 | int err; | |
48c9d417 | 595 | struct snd_rawmidi_info info; |
1da177e4 LT |
596 | if (get_user(info.device, &_info->device)) |
597 | return -EFAULT; | |
598 | if (get_user(info.stream, &_info->stream)) | |
599 | return -EFAULT; | |
600 | if (get_user(info.subdevice, &_info->subdevice)) | |
601 | return -EFAULT; | |
602 | if ((err = snd_rawmidi_info_select(card, &info)) < 0) | |
603 | return err; | |
48c9d417 | 604 | if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info))) |
1da177e4 LT |
605 | return -EFAULT; |
606 | return 0; | |
607 | } | |
608 | ||
48c9d417 TI |
609 | int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream, |
610 | struct snd_rawmidi_params * params) | |
1da177e4 LT |
611 | { |
612 | char *newbuf; | |
48c9d417 | 613 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
614 | |
615 | if (substream->append && substream->use_count > 1) | |
616 | return -EBUSY; | |
617 | snd_rawmidi_drain_output(substream); | |
618 | if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) { | |
619 | return -EINVAL; | |
620 | } | |
621 | if (params->avail_min < 1 || params->avail_min > params->buffer_size) { | |
622 | return -EINVAL; | |
623 | } | |
624 | if (params->buffer_size != runtime->buffer_size) { | |
625 | if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL) | |
626 | return -ENOMEM; | |
627 | kfree(runtime->buffer); | |
628 | runtime->buffer = newbuf; | |
629 | runtime->buffer_size = params->buffer_size; | |
630 | } | |
631 | runtime->avail_min = params->avail_min; | |
632 | substream->active_sensing = !params->no_active_sensing; | |
633 | return 0; | |
634 | } | |
635 | ||
48c9d417 TI |
636 | int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream, |
637 | struct snd_rawmidi_params * params) | |
1da177e4 LT |
638 | { |
639 | char *newbuf; | |
48c9d417 | 640 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
641 | |
642 | snd_rawmidi_drain_input(substream); | |
643 | if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) { | |
644 | return -EINVAL; | |
645 | } | |
646 | if (params->avail_min < 1 || params->avail_min > params->buffer_size) { | |
647 | return -EINVAL; | |
648 | } | |
649 | if (params->buffer_size != runtime->buffer_size) { | |
650 | if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL) | |
651 | return -ENOMEM; | |
652 | kfree(runtime->buffer); | |
653 | runtime->buffer = newbuf; | |
654 | runtime->buffer_size = params->buffer_size; | |
655 | } | |
656 | runtime->avail_min = params->avail_min; | |
657 | return 0; | |
658 | } | |
659 | ||
48c9d417 TI |
660 | static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream, |
661 | struct snd_rawmidi_status * status) | |
1da177e4 | 662 | { |
48c9d417 | 663 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
664 | |
665 | memset(status, 0, sizeof(*status)); | |
666 | status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT; | |
667 | spin_lock_irq(&runtime->lock); | |
668 | status->avail = runtime->avail; | |
669 | spin_unlock_irq(&runtime->lock); | |
670 | return 0; | |
671 | } | |
672 | ||
48c9d417 TI |
673 | static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream, |
674 | struct snd_rawmidi_status * status) | |
1da177e4 | 675 | { |
48c9d417 | 676 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
677 | |
678 | memset(status, 0, sizeof(*status)); | |
679 | status->stream = SNDRV_RAWMIDI_STREAM_INPUT; | |
680 | spin_lock_irq(&runtime->lock); | |
681 | status->avail = runtime->avail; | |
682 | status->xruns = runtime->xruns; | |
683 | runtime->xruns = 0; | |
684 | spin_unlock_irq(&runtime->lock); | |
685 | return 0; | |
686 | } | |
687 | ||
688 | static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
689 | { | |
48c9d417 | 690 | struct snd_rawmidi_file *rfile; |
1da177e4 LT |
691 | void __user *argp = (void __user *)arg; |
692 | ||
693 | rfile = file->private_data; | |
694 | if (((cmd >> 8) & 0xff) != 'W') | |
695 | return -ENOTTY; | |
696 | switch (cmd) { | |
697 | case SNDRV_RAWMIDI_IOCTL_PVERSION: | |
698 | return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0; | |
699 | case SNDRV_RAWMIDI_IOCTL_INFO: | |
700 | { | |
48c9d417 TI |
701 | int stream; |
702 | struct snd_rawmidi_info __user *info = argp; | |
1da177e4 LT |
703 | if (get_user(stream, &info->stream)) |
704 | return -EFAULT; | |
705 | switch (stream) { | |
706 | case SNDRV_RAWMIDI_STREAM_INPUT: | |
707 | return snd_rawmidi_info_user(rfile->input, info); | |
708 | case SNDRV_RAWMIDI_STREAM_OUTPUT: | |
709 | return snd_rawmidi_info_user(rfile->output, info); | |
710 | default: | |
711 | return -EINVAL; | |
712 | } | |
713 | } | |
714 | case SNDRV_RAWMIDI_IOCTL_PARAMS: | |
715 | { | |
48c9d417 TI |
716 | struct snd_rawmidi_params params; |
717 | if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params))) | |
1da177e4 LT |
718 | return -EFAULT; |
719 | switch (params.stream) { | |
720 | case SNDRV_RAWMIDI_STREAM_OUTPUT: | |
721 | if (rfile->output == NULL) | |
722 | return -EINVAL; | |
723 | return snd_rawmidi_output_params(rfile->output, ¶ms); | |
724 | case SNDRV_RAWMIDI_STREAM_INPUT: | |
725 | if (rfile->input == NULL) | |
726 | return -EINVAL; | |
727 | return snd_rawmidi_input_params(rfile->input, ¶ms); | |
728 | default: | |
729 | return -EINVAL; | |
730 | } | |
731 | } | |
732 | case SNDRV_RAWMIDI_IOCTL_STATUS: | |
733 | { | |
734 | int err = 0; | |
48c9d417 TI |
735 | struct snd_rawmidi_status status; |
736 | if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status))) | |
1da177e4 LT |
737 | return -EFAULT; |
738 | switch (status.stream) { | |
739 | case SNDRV_RAWMIDI_STREAM_OUTPUT: | |
740 | if (rfile->output == NULL) | |
741 | return -EINVAL; | |
742 | err = snd_rawmidi_output_status(rfile->output, &status); | |
743 | break; | |
744 | case SNDRV_RAWMIDI_STREAM_INPUT: | |
745 | if (rfile->input == NULL) | |
746 | return -EINVAL; | |
747 | err = snd_rawmidi_input_status(rfile->input, &status); | |
748 | break; | |
749 | default: | |
750 | return -EINVAL; | |
751 | } | |
752 | if (err < 0) | |
753 | return err; | |
48c9d417 | 754 | if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status))) |
1da177e4 LT |
755 | return -EFAULT; |
756 | return 0; | |
757 | } | |
758 | case SNDRV_RAWMIDI_IOCTL_DROP: | |
759 | { | |
760 | int val; | |
761 | if (get_user(val, (int __user *) argp)) | |
762 | return -EFAULT; | |
763 | switch (val) { | |
764 | case SNDRV_RAWMIDI_STREAM_OUTPUT: | |
765 | if (rfile->output == NULL) | |
766 | return -EINVAL; | |
767 | return snd_rawmidi_drop_output(rfile->output); | |
768 | default: | |
769 | return -EINVAL; | |
770 | } | |
771 | } | |
772 | case SNDRV_RAWMIDI_IOCTL_DRAIN: | |
773 | { | |
774 | int val; | |
775 | if (get_user(val, (int __user *) argp)) | |
776 | return -EFAULT; | |
777 | switch (val) { | |
778 | case SNDRV_RAWMIDI_STREAM_OUTPUT: | |
779 | if (rfile->output == NULL) | |
780 | return -EINVAL; | |
781 | return snd_rawmidi_drain_output(rfile->output); | |
782 | case SNDRV_RAWMIDI_STREAM_INPUT: | |
783 | if (rfile->input == NULL) | |
784 | return -EINVAL; | |
785 | return snd_rawmidi_drain_input(rfile->input); | |
786 | default: | |
787 | return -EINVAL; | |
788 | } | |
789 | } | |
790 | #ifdef CONFIG_SND_DEBUG | |
791 | default: | |
792 | snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd); | |
793 | #endif | |
794 | } | |
795 | return -ENOTTY; | |
796 | } | |
797 | ||
48c9d417 TI |
798 | static int snd_rawmidi_control_ioctl(struct snd_card *card, |
799 | struct snd_ctl_file *control, | |
1da177e4 LT |
800 | unsigned int cmd, |
801 | unsigned long arg) | |
802 | { | |
803 | void __user *argp = (void __user *)arg; | |
804 | unsigned int tmp; | |
805 | ||
806 | tmp = card->number * SNDRV_RAWMIDI_DEVICES; | |
807 | 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; | |
814 | device = device < 0 ? 0 : device + 1; | |
815 | while (device < SNDRV_RAWMIDI_DEVICES) { | |
816 | if (snd_rawmidi_devices[tmp + device]) | |
817 | break; | |
818 | device++; | |
819 | } | |
820 | if (device == SNDRV_RAWMIDI_DEVICES) | |
821 | device = -1; | |
822 | if (put_user(device, (int __user *)argp)) | |
823 | return -EFAULT; | |
824 | return 0; | |
825 | } | |
826 | case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE: | |
827 | { | |
828 | int val; | |
829 | ||
830 | if (get_user(val, (int __user *)argp)) | |
831 | return -EFAULT; | |
832 | control->prefer_rawmidi_subdevice = val; | |
833 | return 0; | |
834 | } | |
835 | case SNDRV_CTL_IOCTL_RAWMIDI_INFO: | |
836 | return snd_rawmidi_info_select_user(card, argp); | |
837 | } | |
838 | return -ENOIOCTLCMD; | |
839 | } | |
840 | ||
841 | /** | |
842 | * snd_rawmidi_receive - receive the input data from the device | |
843 | * @substream: the rawmidi substream | |
844 | * @buffer: the buffer pointer | |
845 | * @count: the data size to read | |
846 | * | |
847 | * Reads the data from the internal buffer. | |
848 | * | |
849 | * Returns the size of read data, or a negative error code on failure. | |
850 | */ | |
48c9d417 TI |
851 | int snd_rawmidi_receive(struct snd_rawmidi_substream *substream, |
852 | const unsigned char *buffer, int count) | |
1da177e4 LT |
853 | { |
854 | unsigned long flags; | |
855 | int result = 0, count1; | |
48c9d417 | 856 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
857 | |
858 | if (runtime->buffer == NULL) { | |
859 | snd_printd("snd_rawmidi_receive: input is not active!!!\n"); | |
860 | return -EINVAL; | |
861 | } | |
862 | spin_lock_irqsave(&runtime->lock, flags); | |
863 | if (count == 1) { /* special case, faster code */ | |
864 | substream->bytes++; | |
865 | if (runtime->avail < runtime->buffer_size) { | |
866 | runtime->buffer[runtime->hw_ptr++] = buffer[0]; | |
867 | runtime->hw_ptr %= runtime->buffer_size; | |
868 | runtime->avail++; | |
869 | result++; | |
870 | } else { | |
871 | runtime->xruns++; | |
872 | } | |
873 | } else { | |
874 | substream->bytes += count; | |
875 | count1 = runtime->buffer_size - runtime->hw_ptr; | |
876 | if (count1 > count) | |
877 | count1 = count; | |
878 | if (count1 > (int)(runtime->buffer_size - runtime->avail)) | |
879 | count1 = runtime->buffer_size - runtime->avail; | |
880 | memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1); | |
881 | runtime->hw_ptr += count1; | |
882 | runtime->hw_ptr %= runtime->buffer_size; | |
883 | runtime->avail += count1; | |
884 | count -= count1; | |
885 | result += count1; | |
886 | if (count > 0) { | |
887 | buffer += count1; | |
888 | count1 = count; | |
889 | if (count1 > (int)(runtime->buffer_size - runtime->avail)) { | |
890 | count1 = runtime->buffer_size - runtime->avail; | |
891 | runtime->xruns += count - count1; | |
892 | } | |
893 | if (count1 > 0) { | |
894 | memcpy(runtime->buffer, buffer, count1); | |
895 | runtime->hw_ptr = count1; | |
896 | runtime->avail += count1; | |
897 | result += count1; | |
898 | } | |
899 | } | |
900 | } | |
901 | if (result > 0) { | |
902 | if (runtime->event) | |
903 | tasklet_hi_schedule(&runtime->tasklet); | |
904 | else if (snd_rawmidi_ready(substream)) | |
905 | wake_up(&runtime->sleep); | |
906 | } | |
907 | spin_unlock_irqrestore(&runtime->lock, flags); | |
908 | return result; | |
909 | } | |
910 | ||
48c9d417 | 911 | static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream, |
1da177e4 LT |
912 | unsigned char *buf, long count, int kernel) |
913 | { | |
914 | unsigned long flags; | |
915 | long result = 0, count1; | |
48c9d417 | 916 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
917 | |
918 | while (count > 0 && runtime->avail) { | |
919 | count1 = runtime->buffer_size - runtime->appl_ptr; | |
920 | if (count1 > count) | |
921 | count1 = count; | |
922 | spin_lock_irqsave(&runtime->lock, flags); | |
923 | if (count1 > (int)runtime->avail) | |
924 | count1 = runtime->avail; | |
925 | if (kernel) { | |
926 | memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1); | |
927 | } else { | |
928 | spin_unlock_irqrestore(&runtime->lock, flags); | |
929 | if (copy_to_user((char __user *)buf + result, | |
930 | runtime->buffer + runtime->appl_ptr, count1)) { | |
931 | return result > 0 ? result : -EFAULT; | |
932 | } | |
933 | spin_lock_irqsave(&runtime->lock, flags); | |
934 | } | |
935 | runtime->appl_ptr += count1; | |
936 | runtime->appl_ptr %= runtime->buffer_size; | |
937 | runtime->avail -= count1; | |
938 | spin_unlock_irqrestore(&runtime->lock, flags); | |
939 | result += count1; | |
940 | count -= count1; | |
941 | } | |
942 | return result; | |
943 | } | |
944 | ||
48c9d417 TI |
945 | long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream, |
946 | unsigned char *buf, long count) | |
1da177e4 LT |
947 | { |
948 | snd_rawmidi_input_trigger(substream, 1); | |
949 | return snd_rawmidi_kernel_read1(substream, buf, count, 1); | |
950 | } | |
951 | ||
48c9d417 TI |
952 | static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count, |
953 | loff_t *offset) | |
1da177e4 LT |
954 | { |
955 | long result; | |
956 | int count1; | |
48c9d417 TI |
957 | struct snd_rawmidi_file *rfile; |
958 | struct snd_rawmidi_substream *substream; | |
959 | struct snd_rawmidi_runtime *runtime; | |
1da177e4 LT |
960 | |
961 | rfile = file->private_data; | |
962 | substream = rfile->input; | |
963 | if (substream == NULL) | |
964 | return -EIO; | |
965 | runtime = substream->runtime; | |
966 | snd_rawmidi_input_trigger(substream, 1); | |
967 | result = 0; | |
968 | while (count > 0) { | |
969 | spin_lock_irq(&runtime->lock); | |
970 | while (!snd_rawmidi_ready(substream)) { | |
971 | wait_queue_t wait; | |
972 | if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) { | |
973 | spin_unlock_irq(&runtime->lock); | |
974 | return result > 0 ? result : -EAGAIN; | |
975 | } | |
976 | init_waitqueue_entry(&wait, current); | |
977 | add_wait_queue(&runtime->sleep, &wait); | |
978 | set_current_state(TASK_INTERRUPTIBLE); | |
979 | spin_unlock_irq(&runtime->lock); | |
980 | schedule(); | |
981 | remove_wait_queue(&runtime->sleep, &wait); | |
982 | if (signal_pending(current)) | |
983 | return result > 0 ? result : -ERESTARTSYS; | |
984 | if (!runtime->avail) | |
985 | return result > 0 ? result : -EIO; | |
986 | spin_lock_irq(&runtime->lock); | |
987 | } | |
988 | spin_unlock_irq(&runtime->lock); | |
4d23359b CL |
989 | count1 = snd_rawmidi_kernel_read1(substream, |
990 | (unsigned char __force *)buf, | |
991 | count, 0); | |
1da177e4 LT |
992 | if (count1 < 0) |
993 | return result > 0 ? result : count1; | |
994 | result += count1; | |
995 | buf += count1; | |
996 | count -= count1; | |
997 | } | |
998 | return result; | |
999 | } | |
1000 | ||
1001 | /** | |
1002 | * snd_rawmidi_transmit_empty - check whether the output buffer is empty | |
1003 | * @substream: the rawmidi substream | |
1004 | * | |
1005 | * Returns 1 if the internal output buffer is empty, 0 if not. | |
1006 | */ | |
48c9d417 | 1007 | int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream) |
1da177e4 | 1008 | { |
48c9d417 | 1009 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
1010 | int result; |
1011 | unsigned long flags; | |
1012 | ||
1013 | if (runtime->buffer == NULL) { | |
1014 | snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n"); | |
1015 | return 1; | |
1016 | } | |
1017 | spin_lock_irqsave(&runtime->lock, flags); | |
1018 | result = runtime->avail >= runtime->buffer_size; | |
1019 | spin_unlock_irqrestore(&runtime->lock, flags); | |
1020 | return result; | |
1021 | } | |
1022 | ||
1023 | /** | |
1024 | * snd_rawmidi_transmit_peek - copy data from the internal buffer | |
1025 | * @substream: the rawmidi substream | |
1026 | * @buffer: the buffer pointer | |
1027 | * @count: data size to transfer | |
1028 | * | |
1029 | * Copies data from the internal output buffer to the given buffer. | |
1030 | * | |
1031 | * Call this in the interrupt handler when the midi output is ready, | |
1032 | * and call snd_rawmidi_transmit_ack() after the transmission is | |
1033 | * finished. | |
1034 | * | |
1035 | * Returns the size of copied data, or a negative error code on failure. | |
1036 | */ | |
48c9d417 TI |
1037 | int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream, |
1038 | unsigned char *buffer, int count) | |
1da177e4 LT |
1039 | { |
1040 | unsigned long flags; | |
1041 | int result, count1; | |
48c9d417 | 1042 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
1043 | |
1044 | if (runtime->buffer == NULL) { | |
1045 | snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n"); | |
1046 | return -EINVAL; | |
1047 | } | |
1048 | result = 0; | |
1049 | spin_lock_irqsave(&runtime->lock, flags); | |
1050 | if (runtime->avail >= runtime->buffer_size) { | |
1051 | /* warning: lowlevel layer MUST trigger down the hardware */ | |
1052 | goto __skip; | |
1053 | } | |
1054 | if (count == 1) { /* special case, faster code */ | |
1055 | *buffer = runtime->buffer[runtime->hw_ptr]; | |
1056 | result++; | |
1057 | } else { | |
1058 | count1 = runtime->buffer_size - runtime->hw_ptr; | |
1059 | if (count1 > count) | |
1060 | count1 = count; | |
1061 | if (count1 > (int)(runtime->buffer_size - runtime->avail)) | |
1062 | count1 = runtime->buffer_size - runtime->avail; | |
1063 | memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1); | |
1064 | count -= count1; | |
1065 | result += count1; | |
1066 | if (count > 0) { | |
1067 | if (count > (int)(runtime->buffer_size - runtime->avail - count1)) | |
1068 | count = runtime->buffer_size - runtime->avail - count1; | |
1069 | memcpy(buffer + count1, runtime->buffer, count); | |
1070 | result += count; | |
1071 | } | |
1072 | } | |
1073 | __skip: | |
1074 | spin_unlock_irqrestore(&runtime->lock, flags); | |
1075 | return result; | |
1076 | } | |
1077 | ||
1078 | /** | |
1079 | * snd_rawmidi_transmit_ack - acknowledge the transmission | |
1080 | * @substream: the rawmidi substream | |
1081 | * @count: the tranferred count | |
1082 | * | |
1083 | * Advances the hardware pointer for the internal output buffer with | |
1084 | * the given size and updates the condition. | |
1085 | * Call after the transmission is finished. | |
1086 | * | |
1087 | * Returns the advanced size if successful, or a negative error code on failure. | |
1088 | */ | |
48c9d417 | 1089 | int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count) |
1da177e4 LT |
1090 | { |
1091 | unsigned long flags; | |
48c9d417 | 1092 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
1093 | |
1094 | if (runtime->buffer == NULL) { | |
1095 | snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n"); | |
1096 | return -EINVAL; | |
1097 | } | |
1098 | spin_lock_irqsave(&runtime->lock, flags); | |
1099 | snd_assert(runtime->avail + count <= runtime->buffer_size, ); | |
1100 | runtime->hw_ptr += count; | |
1101 | runtime->hw_ptr %= runtime->buffer_size; | |
1102 | runtime->avail += count; | |
1103 | substream->bytes += count; | |
1104 | if (count > 0) { | |
1105 | if (runtime->drain || snd_rawmidi_ready(substream)) | |
1106 | wake_up(&runtime->sleep); | |
1107 | } | |
1108 | spin_unlock_irqrestore(&runtime->lock, flags); | |
1109 | return count; | |
1110 | } | |
1111 | ||
1112 | /** | |
1113 | * snd_rawmidi_transmit - copy from the buffer to the device | |
1114 | * @substream: the rawmidi substream | |
df8db936 | 1115 | * @buffer: the buffer pointer |
1da177e4 LT |
1116 | * @count: the data size to transfer |
1117 | * | |
1118 | * Copies data from the buffer to the device and advances the pointer. | |
1119 | * | |
1120 | * Returns the copied size if successful, or a negative error code on failure. | |
1121 | */ | |
48c9d417 TI |
1122 | int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream, |
1123 | unsigned char *buffer, int count) | |
1da177e4 LT |
1124 | { |
1125 | count = snd_rawmidi_transmit_peek(substream, buffer, count); | |
1126 | if (count < 0) | |
1127 | return count; | |
1128 | return snd_rawmidi_transmit_ack(substream, count); | |
1129 | } | |
1130 | ||
48c9d417 TI |
1131 | static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream, |
1132 | const unsigned char *buf, long count, int kernel) | |
1da177e4 LT |
1133 | { |
1134 | unsigned long flags; | |
1135 | long count1, result; | |
48c9d417 | 1136 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
1da177e4 LT |
1137 | |
1138 | snd_assert(buf != NULL, return -EINVAL); | |
1139 | snd_assert(runtime->buffer != NULL, return -EINVAL); | |
1140 | ||
1141 | result = 0; | |
1142 | spin_lock_irqsave(&runtime->lock, flags); | |
1143 | if (substream->append) { | |
1144 | if ((long)runtime->avail < count) { | |
1145 | spin_unlock_irqrestore(&runtime->lock, flags); | |
1146 | return -EAGAIN; | |
1147 | } | |
1148 | } | |
1149 | while (count > 0 && runtime->avail > 0) { | |
1150 | count1 = runtime->buffer_size - runtime->appl_ptr; | |
1151 | if (count1 > count) | |
1152 | count1 = count; | |
1153 | if (count1 > (long)runtime->avail) | |
1154 | count1 = runtime->avail; | |
1155 | if (kernel) { | |
1156 | memcpy(runtime->buffer + runtime->appl_ptr, buf, count1); | |
1157 | } else { | |
1158 | spin_unlock_irqrestore(&runtime->lock, flags); | |
1159 | if (copy_from_user(runtime->buffer + runtime->appl_ptr, | |
1160 | (char __user *)buf, count1)) { | |
1161 | spin_lock_irqsave(&runtime->lock, flags); | |
1162 | result = result > 0 ? result : -EFAULT; | |
1163 | goto __end; | |
1164 | } | |
1165 | spin_lock_irqsave(&runtime->lock, flags); | |
1166 | } | |
1167 | runtime->appl_ptr += count1; | |
1168 | runtime->appl_ptr %= runtime->buffer_size; | |
1169 | runtime->avail -= count1; | |
1170 | result += count1; | |
1171 | buf += count1; | |
1172 | count -= count1; | |
1173 | } | |
1174 | __end: | |
1175 | count1 = runtime->avail < runtime->buffer_size; | |
1176 | spin_unlock_irqrestore(&runtime->lock, flags); | |
1177 | if (count1) | |
1178 | snd_rawmidi_output_trigger(substream, 1); | |
1179 | return result; | |
1180 | } | |
1181 | ||
48c9d417 TI |
1182 | long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream, |
1183 | const unsigned char *buf, long count) | |
1da177e4 LT |
1184 | { |
1185 | return snd_rawmidi_kernel_write1(substream, buf, count, 1); | |
1186 | } | |
1187 | ||
48c9d417 TI |
1188 | static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf, |
1189 | size_t count, loff_t *offset) | |
1da177e4 LT |
1190 | { |
1191 | long result, timeout; | |
1192 | int count1; | |
48c9d417 TI |
1193 | struct snd_rawmidi_file *rfile; |
1194 | struct snd_rawmidi_runtime *runtime; | |
1195 | struct snd_rawmidi_substream *substream; | |
1da177e4 LT |
1196 | |
1197 | rfile = file->private_data; | |
1198 | substream = rfile->output; | |
1199 | runtime = substream->runtime; | |
1200 | /* we cannot put an atomic message to our buffer */ | |
1201 | if (substream->append && count > runtime->buffer_size) | |
1202 | return -EIO; | |
1203 | result = 0; | |
1204 | while (count > 0) { | |
1205 | spin_lock_irq(&runtime->lock); | |
1206 | while (!snd_rawmidi_ready_append(substream, count)) { | |
1207 | wait_queue_t wait; | |
1208 | if (file->f_flags & O_NONBLOCK) { | |
1209 | spin_unlock_irq(&runtime->lock); | |
1210 | return result > 0 ? result : -EAGAIN; | |
1211 | } | |
1212 | init_waitqueue_entry(&wait, current); | |
1213 | add_wait_queue(&runtime->sleep, &wait); | |
1214 | set_current_state(TASK_INTERRUPTIBLE); | |
1215 | spin_unlock_irq(&runtime->lock); | |
1216 | timeout = schedule_timeout(30 * HZ); | |
1217 | remove_wait_queue(&runtime->sleep, &wait); | |
1218 | if (signal_pending(current)) | |
1219 | return result > 0 ? result : -ERESTARTSYS; | |
1220 | if (!runtime->avail && !timeout) | |
1221 | return result > 0 ? result : -EIO; | |
1222 | spin_lock_irq(&runtime->lock); | |
1223 | } | |
1224 | spin_unlock_irq(&runtime->lock); | |
4d23359b CL |
1225 | count1 = snd_rawmidi_kernel_write1(substream, |
1226 | (unsigned char __force *)buf, | |
1227 | count, 0); | |
1da177e4 LT |
1228 | if (count1 < 0) |
1229 | return result > 0 ? result : count1; | |
1230 | result += count1; | |
1231 | buf += count1; | |
1232 | if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK)) | |
1233 | break; | |
1234 | count -= count1; | |
1235 | } | |
1236 | if (file->f_flags & O_SYNC) { | |
1237 | spin_lock_irq(&runtime->lock); | |
1238 | while (runtime->avail != runtime->buffer_size) { | |
1239 | wait_queue_t wait; | |
1240 | unsigned int last_avail = runtime->avail; | |
1241 | init_waitqueue_entry(&wait, current); | |
1242 | add_wait_queue(&runtime->sleep, &wait); | |
1243 | set_current_state(TASK_INTERRUPTIBLE); | |
1244 | spin_unlock_irq(&runtime->lock); | |
1245 | timeout = schedule_timeout(30 * HZ); | |
1246 | remove_wait_queue(&runtime->sleep, &wait); | |
1247 | if (signal_pending(current)) | |
1248 | return result > 0 ? result : -ERESTARTSYS; | |
1249 | if (runtime->avail == last_avail && !timeout) | |
1250 | return result > 0 ? result : -EIO; | |
1251 | spin_lock_irq(&runtime->lock); | |
1252 | } | |
1253 | spin_unlock_irq(&runtime->lock); | |
1254 | } | |
1255 | return result; | |
1256 | } | |
1257 | ||
1258 | static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait) | |
1259 | { | |
48c9d417 TI |
1260 | struct snd_rawmidi_file *rfile; |
1261 | struct snd_rawmidi_runtime *runtime; | |
1da177e4 LT |
1262 | unsigned int mask; |
1263 | ||
1264 | rfile = file->private_data; | |
1265 | if (rfile->input != NULL) { | |
1266 | runtime = rfile->input->runtime; | |
1267 | snd_rawmidi_input_trigger(rfile->input, 1); | |
1268 | poll_wait(file, &runtime->sleep, wait); | |
1269 | } | |
1270 | if (rfile->output != NULL) { | |
1271 | runtime = rfile->output->runtime; | |
1272 | poll_wait(file, &runtime->sleep, wait); | |
1273 | } | |
1274 | mask = 0; | |
1275 | if (rfile->input != NULL) { | |
1276 | if (snd_rawmidi_ready(rfile->input)) | |
1277 | mask |= POLLIN | POLLRDNORM; | |
1278 | } | |
1279 | if (rfile->output != NULL) { | |
1280 | if (snd_rawmidi_ready(rfile->output)) | |
1281 | mask |= POLLOUT | POLLWRNORM; | |
1282 | } | |
1283 | return mask; | |
1284 | } | |
1285 | ||
1286 | /* | |
1287 | */ | |
1288 | #ifdef CONFIG_COMPAT | |
1289 | #include "rawmidi_compat.c" | |
1290 | #else | |
1291 | #define snd_rawmidi_ioctl_compat NULL | |
1292 | #endif | |
1293 | ||
1294 | /* | |
1295 | ||
1296 | */ | |
1297 | ||
48c9d417 TI |
1298 | static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry, |
1299 | struct snd_info_buffer *buffer) | |
1da177e4 | 1300 | { |
48c9d417 TI |
1301 | struct snd_rawmidi *rmidi; |
1302 | struct snd_rawmidi_substream *substream; | |
1303 | struct snd_rawmidi_runtime *runtime; | |
1da177e4 LT |
1304 | struct list_head *list; |
1305 | ||
1306 | rmidi = entry->private_data; | |
1307 | snd_iprintf(buffer, "%s\n\n", rmidi->name); | |
1308 | down(&rmidi->open_mutex); | |
1309 | if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) { | |
1310 | list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) { | |
48c9d417 | 1311 | substream = list_entry(list, struct snd_rawmidi_substream, list); |
1da177e4 LT |
1312 | snd_iprintf(buffer, |
1313 | "Output %d\n" | |
1314 | " Tx bytes : %lu\n", | |
1315 | substream->number, | |
1316 | (unsigned long) substream->bytes); | |
1317 | if (substream->opened) { | |
1318 | runtime = substream->runtime; | |
1319 | snd_iprintf(buffer, | |
1320 | " Mode : %s\n" | |
1321 | " Buffer size : %lu\n" | |
1322 | " Avail : %lu\n", | |
1323 | runtime->oss ? "OSS compatible" : "native", | |
1324 | (unsigned long) runtime->buffer_size, | |
1325 | (unsigned long) runtime->avail); | |
1326 | } | |
1327 | } | |
1328 | } | |
1329 | if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) { | |
1330 | list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) { | |
48c9d417 | 1331 | substream = list_entry(list, struct snd_rawmidi_substream, list); |
1da177e4 LT |
1332 | snd_iprintf(buffer, |
1333 | "Input %d\n" | |
1334 | " Rx bytes : %lu\n", | |
1335 | substream->number, | |
1336 | (unsigned long) substream->bytes); | |
1337 | if (substream->opened) { | |
1338 | runtime = substream->runtime; | |
1339 | snd_iprintf(buffer, | |
1340 | " Buffer size : %lu\n" | |
1341 | " Avail : %lu\n" | |
1342 | " Overruns : %lu\n", | |
1343 | (unsigned long) runtime->buffer_size, | |
1344 | (unsigned long) runtime->avail, | |
1345 | (unsigned long) runtime->xruns); | |
1346 | } | |
1347 | } | |
1348 | } | |
1349 | up(&rmidi->open_mutex); | |
1350 | } | |
1351 | ||
1352 | /* | |
1353 | * Register functions | |
1354 | */ | |
1355 | ||
1356 | static struct file_operations snd_rawmidi_f_ops = | |
1357 | { | |
1358 | .owner = THIS_MODULE, | |
1359 | .read = snd_rawmidi_read, | |
1360 | .write = snd_rawmidi_write, | |
1361 | .open = snd_rawmidi_open, | |
1362 | .release = snd_rawmidi_release, | |
1363 | .poll = snd_rawmidi_poll, | |
1364 | .unlocked_ioctl = snd_rawmidi_ioctl, | |
1365 | .compat_ioctl = snd_rawmidi_ioctl_compat, | |
1366 | }; | |
1367 | ||
48c9d417 | 1368 | static struct snd_minor snd_rawmidi_reg = |
1da177e4 LT |
1369 | { |
1370 | .comment = "raw midi", | |
1371 | .f_ops = &snd_rawmidi_f_ops, | |
1372 | }; | |
1373 | ||
48c9d417 TI |
1374 | static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi, |
1375 | struct snd_rawmidi_str *stream, | |
1da177e4 LT |
1376 | int direction, |
1377 | int count) | |
1378 | { | |
48c9d417 | 1379 | struct snd_rawmidi_substream *substream; |
1da177e4 LT |
1380 | int idx; |
1381 | ||
1382 | INIT_LIST_HEAD(&stream->substreams); | |
1383 | for (idx = 0; idx < count; idx++) { | |
ca2c0966 | 1384 | substream = kzalloc(sizeof(*substream), GFP_KERNEL); |
1da177e4 LT |
1385 | if (substream == NULL) |
1386 | return -ENOMEM; | |
1387 | substream->stream = direction; | |
1388 | substream->number = idx; | |
1389 | substream->rmidi = rmidi; | |
1390 | substream->pstr = stream; | |
1391 | list_add_tail(&substream->list, &stream->substreams); | |
1392 | stream->substream_count++; | |
1393 | } | |
1394 | return 0; | |
1395 | } | |
1396 | ||
1397 | /** | |
1398 | * snd_rawmidi_new - create a rawmidi instance | |
1399 | * @card: the card instance | |
1400 | * @id: the id string | |
1401 | * @device: the device index | |
1402 | * @output_count: the number of output streams | |
1403 | * @input_count: the number of input streams | |
1404 | * @rrawmidi: the pointer to store the new rawmidi instance | |
1405 | * | |
1406 | * Creates a new rawmidi instance. | |
1407 | * Use snd_rawmidi_set_ops() to set the operators to the new instance. | |
1408 | * | |
1409 | * Returns zero if successful, or a negative error code on failure. | |
1410 | */ | |
48c9d417 | 1411 | int snd_rawmidi_new(struct snd_card *card, char *id, int device, |
1da177e4 | 1412 | int output_count, int input_count, |
48c9d417 | 1413 | struct snd_rawmidi ** rrawmidi) |
1da177e4 | 1414 | { |
48c9d417 | 1415 | struct snd_rawmidi *rmidi; |
1da177e4 | 1416 | int err; |
48c9d417 | 1417 | static struct snd_device_ops ops = { |
1da177e4 LT |
1418 | .dev_free = snd_rawmidi_dev_free, |
1419 | .dev_register = snd_rawmidi_dev_register, | |
1420 | .dev_disconnect = snd_rawmidi_dev_disconnect, | |
1421 | .dev_unregister = snd_rawmidi_dev_unregister | |
1422 | }; | |
1423 | ||
1424 | snd_assert(rrawmidi != NULL, return -EINVAL); | |
1425 | *rrawmidi = NULL; | |
1426 | snd_assert(card != NULL, return -ENXIO); | |
ca2c0966 | 1427 | rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL); |
1da177e4 LT |
1428 | if (rmidi == NULL) |
1429 | return -ENOMEM; | |
1430 | rmidi->card = card; | |
1431 | rmidi->device = device; | |
1432 | init_MUTEX(&rmidi->open_mutex); | |
1433 | init_waitqueue_head(&rmidi->open_wait); | |
1434 | if (id != NULL) | |
1435 | strlcpy(rmidi->id, id, sizeof(rmidi->id)); | |
1436 | if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT], SNDRV_RAWMIDI_STREAM_INPUT, input_count)) < 0) { | |
1437 | snd_rawmidi_free(rmidi); | |
1438 | return err; | |
1439 | } | |
1440 | if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT], SNDRV_RAWMIDI_STREAM_OUTPUT, output_count)) < 0) { | |
1441 | snd_rawmidi_free(rmidi); | |
1442 | return err; | |
1443 | } | |
1444 | if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) { | |
1445 | snd_rawmidi_free(rmidi); | |
1446 | return err; | |
1447 | } | |
1448 | *rrawmidi = rmidi; | |
1449 | return 0; | |
1450 | } | |
1451 | ||
48c9d417 | 1452 | static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream) |
1da177e4 | 1453 | { |
48c9d417 | 1454 | struct snd_rawmidi_substream *substream; |
1da177e4 LT |
1455 | |
1456 | while (!list_empty(&stream->substreams)) { | |
48c9d417 | 1457 | substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list); |
1da177e4 LT |
1458 | list_del(&substream->list); |
1459 | kfree(substream); | |
1460 | } | |
1461 | } | |
1462 | ||
48c9d417 | 1463 | static int snd_rawmidi_free(struct snd_rawmidi *rmidi) |
1da177e4 LT |
1464 | { |
1465 | snd_assert(rmidi != NULL, return -ENXIO); | |
1466 | snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]); | |
1467 | snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]); | |
1468 | if (rmidi->private_free) | |
1469 | rmidi->private_free(rmidi); | |
1470 | kfree(rmidi); | |
1471 | return 0; | |
1472 | } | |
1473 | ||
48c9d417 | 1474 | static int snd_rawmidi_dev_free(struct snd_device *device) |
1da177e4 | 1475 | { |
48c9d417 | 1476 | struct snd_rawmidi *rmidi = device->device_data; |
1da177e4 LT |
1477 | return snd_rawmidi_free(rmidi); |
1478 | } | |
1479 | ||
1480 | #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE)) | |
48c9d417 | 1481 | static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device) |
1da177e4 | 1482 | { |
48c9d417 | 1483 | struct snd_rawmidi *rmidi = device->private_data; |
1da177e4 LT |
1484 | rmidi->seq_dev = NULL; |
1485 | } | |
1486 | #endif | |
1487 | ||
48c9d417 | 1488 | static int snd_rawmidi_dev_register(struct snd_device *device) |
1da177e4 LT |
1489 | { |
1490 | int idx, err; | |
48c9d417 | 1491 | struct snd_info_entry *entry; |
1da177e4 | 1492 | char name[16]; |
48c9d417 | 1493 | struct snd_rawmidi *rmidi = device->device_data; |
1da177e4 LT |
1494 | |
1495 | if (rmidi->device >= SNDRV_RAWMIDI_DEVICES) | |
1496 | return -ENOMEM; | |
1497 | down(®ister_mutex); | |
1498 | idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device; | |
1499 | if (snd_rawmidi_devices[idx] != NULL) { | |
1500 | up(®ister_mutex); | |
1501 | return -EBUSY; | |
1502 | } | |
1503 | snd_rawmidi_devices[idx] = rmidi; | |
1504 | sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device); | |
1505 | if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI, | |
1506 | rmidi->card, rmidi->device, | |
1507 | &snd_rawmidi_reg, name)) < 0) { | |
1508 | snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device); | |
1509 | snd_rawmidi_devices[idx] = NULL; | |
1510 | up(®ister_mutex); | |
1511 | return err; | |
1512 | } | |
1513 | if (rmidi->ops && rmidi->ops->dev_register && | |
1514 | (err = rmidi->ops->dev_register(rmidi)) < 0) { | |
1515 | snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device); | |
1516 | snd_rawmidi_devices[idx] = NULL; | |
1517 | up(®ister_mutex); | |
1518 | return err; | |
1519 | } | |
1520 | #ifdef CONFIG_SND_OSSEMUL | |
1521 | rmidi->ossreg = 0; | |
1522 | if ((int)rmidi->device == midi_map[rmidi->card->number]) { | |
1523 | if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, | |
1524 | rmidi->card, 0, &snd_rawmidi_reg, name) < 0) { | |
1525 | snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0); | |
1526 | } else { | |
1527 | rmidi->ossreg++; | |
1528 | #ifdef SNDRV_OSS_INFO_DEV_MIDI | |
1529 | snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name); | |
1530 | #endif | |
1531 | } | |
1532 | } | |
1533 | if ((int)rmidi->device == amidi_map[rmidi->card->number]) { | |
1534 | if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, | |
1535 | rmidi->card, 1, &snd_rawmidi_reg, name) < 0) { | |
1536 | snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1); | |
1537 | } else { | |
1538 | rmidi->ossreg++; | |
1539 | } | |
1540 | } | |
1541 | #endif /* CONFIG_SND_OSSEMUL */ | |
1542 | up(®ister_mutex); | |
1543 | sprintf(name, "midi%d", rmidi->device); | |
1544 | entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root); | |
1545 | if (entry) { | |
1546 | entry->private_data = rmidi; | |
1547 | entry->c.text.read_size = 1024; | |
1548 | entry->c.text.read = snd_rawmidi_proc_info_read; | |
1549 | if (snd_info_register(entry) < 0) { | |
1550 | snd_info_free_entry(entry); | |
1551 | entry = NULL; | |
1552 | } | |
1553 | } | |
1554 | rmidi->proc_entry = entry; | |
1555 | #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE)) | |
1556 | if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */ | |
1557 | if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) { | |
1558 | rmidi->seq_dev->private_data = rmidi; | |
1559 | rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free; | |
1560 | sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device); | |
1561 | snd_device_register(rmidi->card, rmidi->seq_dev); | |
1562 | } | |
1563 | } | |
1564 | #endif | |
1565 | return 0; | |
1566 | } | |
1567 | ||
48c9d417 | 1568 | static int snd_rawmidi_dev_disconnect(struct snd_device *device) |
1da177e4 | 1569 | { |
48c9d417 | 1570 | struct snd_rawmidi *rmidi = device->device_data; |
1da177e4 LT |
1571 | int idx; |
1572 | ||
1573 | down(®ister_mutex); | |
1574 | idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device; | |
1575 | snd_rawmidi_devices[idx] = NULL; | |
1576 | up(®ister_mutex); | |
1577 | return 0; | |
1578 | } | |
1579 | ||
48c9d417 | 1580 | static int snd_rawmidi_dev_unregister(struct snd_device *device) |
1da177e4 LT |
1581 | { |
1582 | int idx; | |
48c9d417 | 1583 | struct snd_rawmidi *rmidi = device->device_data; |
1da177e4 LT |
1584 | |
1585 | snd_assert(rmidi != NULL, return -ENXIO); | |
1586 | down(®ister_mutex); | |
1587 | idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device; | |
1588 | snd_rawmidi_devices[idx] = NULL; | |
1589 | if (rmidi->proc_entry) { | |
1590 | snd_info_unregister(rmidi->proc_entry); | |
1591 | rmidi->proc_entry = NULL; | |
1592 | } | |
1593 | #ifdef CONFIG_SND_OSSEMUL | |
1594 | if (rmidi->ossreg) { | |
1595 | if ((int)rmidi->device == midi_map[rmidi->card->number]) { | |
1596 | snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0); | |
1597 | #ifdef SNDRV_OSS_INFO_DEV_MIDI | |
1598 | snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number); | |
1599 | #endif | |
1600 | } | |
1601 | if ((int)rmidi->device == amidi_map[rmidi->card->number]) | |
1602 | snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1); | |
1603 | rmidi->ossreg = 0; | |
1604 | } | |
1605 | #endif /* CONFIG_SND_OSSEMUL */ | |
1606 | if (rmidi->ops && rmidi->ops->dev_unregister) | |
1607 | rmidi->ops->dev_unregister(rmidi); | |
1608 | snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device); | |
1609 | up(®ister_mutex); | |
1610 | #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE)) | |
1611 | if (rmidi->seq_dev) { | |
1612 | snd_device_free(rmidi->card, rmidi->seq_dev); | |
1613 | rmidi->seq_dev = NULL; | |
1614 | } | |
1615 | #endif | |
1616 | return snd_rawmidi_free(rmidi); | |
1617 | } | |
1618 | ||
1619 | /** | |
1620 | * snd_rawmidi_set_ops - set the rawmidi operators | |
1621 | * @rmidi: the rawmidi instance | |
1622 | * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX | |
1623 | * @ops: the operator table | |
1624 | * | |
1625 | * Sets the rawmidi operators for the given stream direction. | |
1626 | */ | |
48c9d417 TI |
1627 | void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream, |
1628 | struct snd_rawmidi_ops *ops) | |
1da177e4 LT |
1629 | { |
1630 | struct list_head *list; | |
48c9d417 | 1631 | struct snd_rawmidi_substream *substream; |
1da177e4 LT |
1632 | |
1633 | list_for_each(list, &rmidi->streams[stream].substreams) { | |
48c9d417 | 1634 | substream = list_entry(list, struct snd_rawmidi_substream, list); |
1da177e4 LT |
1635 | substream->ops = ops; |
1636 | } | |
1637 | } | |
1638 | ||
1639 | /* | |
1640 | * ENTRY functions | |
1641 | */ | |
1642 | ||
1643 | static int __init alsa_rawmidi_init(void) | |
1644 | { | |
1645 | ||
1646 | snd_ctl_register_ioctl(snd_rawmidi_control_ioctl); | |
1647 | snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl); | |
1648 | #ifdef CONFIG_SND_OSSEMUL | |
1649 | { int i; | |
1650 | /* check device map table */ | |
1651 | for (i = 0; i < SNDRV_CARDS; i++) { | |
1652 | if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) { | |
1653 | snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]); | |
1654 | midi_map[i] = 0; | |
1655 | } | |
1656 | if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) { | |
1657 | snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]); | |
1658 | amidi_map[i] = 1; | |
1659 | } | |
1660 | } | |
1661 | } | |
1662 | #endif /* CONFIG_SND_OSSEMUL */ | |
1663 | return 0; | |
1664 | } | |
1665 | ||
1666 | static void __exit alsa_rawmidi_exit(void) | |
1667 | { | |
1668 | snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl); | |
1669 | snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl); | |
1670 | } | |
1671 | ||
1672 | module_init(alsa_rawmidi_init) | |
1673 | module_exit(alsa_rawmidi_exit) | |
1674 | ||
1675 | EXPORT_SYMBOL(snd_rawmidi_output_params); | |
1676 | EXPORT_SYMBOL(snd_rawmidi_input_params); | |
1677 | EXPORT_SYMBOL(snd_rawmidi_drop_output); | |
1678 | EXPORT_SYMBOL(snd_rawmidi_drain_output); | |
1679 | EXPORT_SYMBOL(snd_rawmidi_drain_input); | |
1680 | EXPORT_SYMBOL(snd_rawmidi_receive); | |
1681 | EXPORT_SYMBOL(snd_rawmidi_transmit_empty); | |
1682 | EXPORT_SYMBOL(snd_rawmidi_transmit_peek); | |
1683 | EXPORT_SYMBOL(snd_rawmidi_transmit_ack); | |
1684 | EXPORT_SYMBOL(snd_rawmidi_transmit); | |
1685 | EXPORT_SYMBOL(snd_rawmidi_new); | |
1686 | EXPORT_SYMBOL(snd_rawmidi_set_ops); | |
1687 | EXPORT_SYMBOL(snd_rawmidi_info); | |
1688 | EXPORT_SYMBOL(snd_rawmidi_info_select); | |
1689 | EXPORT_SYMBOL(snd_rawmidi_kernel_open); | |
1690 | EXPORT_SYMBOL(snd_rawmidi_kernel_release); | |
1691 | EXPORT_SYMBOL(snd_rawmidi_kernel_read); | |
1692 | EXPORT_SYMBOL(snd_rawmidi_kernel_write); |