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