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