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