Commit | Line | Data |
---|---|---|
705ececd | 1 | /* |
c078a4aa | 2 | * Line 6 Linux USB driver |
705ececd | 3 | * |
1027f476 | 4 | * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at) |
705ececd MG |
5 | * |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License as | |
8 | * published by the Free Software Foundation, version 2. | |
9 | * | |
10 | */ | |
11 | ||
5a0e3ad6 | 12 | #include <linux/slab.h> |
ccddbe4a | 13 | #include <linux/export.h> |
705ececd MG |
14 | #include <sound/core.h> |
15 | #include <sound/control.h> | |
16 | #include <sound/pcm.h> | |
17 | #include <sound/pcm_params.h> | |
18 | ||
705ececd | 19 | #include "capture.h" |
1027f476 | 20 | #include "driver.h" |
705ececd | 21 | #include "playback.h" |
705ececd | 22 | |
075587b7 TI |
23 | /* impulse response volume controls */ |
24 | static int snd_line6_impulse_volume_info(struct snd_kcontrol *kcontrol, | |
25 | struct snd_ctl_elem_info *uinfo) | |
1027f476 | 26 | { |
075587b7 TI |
27 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
28 | uinfo->count = 1; | |
29 | uinfo->value.integer.min = 0; | |
30 | uinfo->value.integer.max = 255; | |
31 | return 0; | |
1027f476 MG |
32 | } |
33 | ||
075587b7 TI |
34 | static int snd_line6_impulse_volume_get(struct snd_kcontrol *kcontrol, |
35 | struct snd_ctl_elem_value *ucontrol) | |
1027f476 | 36 | { |
075587b7 TI |
37 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); |
38 | ||
39 | ucontrol->value.integer.value[0] = line6pcm->impulse_volume; | |
40 | return 0; | |
1027f476 MG |
41 | } |
42 | ||
075587b7 TI |
43 | static int snd_line6_impulse_volume_put(struct snd_kcontrol *kcontrol, |
44 | struct snd_ctl_elem_value *ucontrol) | |
1027f476 | 45 | { |
075587b7 TI |
46 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); |
47 | int value = ucontrol->value.integer.value[0]; | |
247d95ee | 48 | int err; |
cdf5e551 | 49 | |
075587b7 TI |
50 | if (line6pcm->impulse_volume == value) |
51 | return 0; | |
cdf5e551 | 52 | |
1027f476 | 53 | line6pcm->impulse_volume = value; |
247d95ee TI |
54 | if (value > 0) { |
55 | err = line6_pcm_acquire(line6pcm, LINE6_STREAM_IMPULSE); | |
56 | if (err < 0) { | |
57 | line6pcm->impulse_volume = 0; | |
58 | line6_pcm_release(line6pcm, LINE6_STREAM_IMPULSE); | |
59 | return err; | |
60 | } | |
61 | } else { | |
63e20df1 | 62 | line6_pcm_release(line6pcm, LINE6_STREAM_IMPULSE); |
247d95ee | 63 | } |
075587b7 TI |
64 | return 1; |
65 | } | |
1027f476 | 66 | |
075587b7 TI |
67 | /* impulse response period controls */ |
68 | static int snd_line6_impulse_period_info(struct snd_kcontrol *kcontrol, | |
69 | struct snd_ctl_elem_info *uinfo) | |
70 | { | |
71 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
72 | uinfo->count = 1; | |
73 | uinfo->value.integer.min = 0; | |
74 | uinfo->value.integer.max = 2000; | |
75 | return 0; | |
1027f476 MG |
76 | } |
77 | ||
075587b7 TI |
78 | static int snd_line6_impulse_period_get(struct snd_kcontrol *kcontrol, |
79 | struct snd_ctl_elem_value *ucontrol) | |
1027f476 | 80 | { |
075587b7 TI |
81 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); |
82 | ||
83 | ucontrol->value.integer.value[0] = line6pcm->impulse_period; | |
84 | return 0; | |
1027f476 MG |
85 | } |
86 | ||
075587b7 TI |
87 | static int snd_line6_impulse_period_put(struct snd_kcontrol *kcontrol, |
88 | struct snd_ctl_elem_value *ucontrol) | |
1027f476 | 89 | { |
075587b7 TI |
90 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); |
91 | int value = ucontrol->value.integer.value[0]; | |
a3762902 | 92 | |
075587b7 TI |
93 | if (line6pcm->impulse_period == value) |
94 | return 0; | |
a3762902 | 95 | |
075587b7 TI |
96 | line6pcm->impulse_period = value; |
97 | return 1; | |
1027f476 | 98 | } |
1027f476 | 99 | |
d8131e67 TI |
100 | /* |
101 | Unlink all currently active URBs. | |
102 | */ | |
103 | static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm, | |
104 | struct line6_pcm_stream *pcms) | |
105 | { | |
106 | int i; | |
107 | ||
108 | for (i = 0; i < LINE6_ISO_BUFFERS; i++) { | |
109 | if (test_bit(i, &pcms->active_urbs)) { | |
110 | if (!test_and_set_bit(i, &pcms->unlink_urbs)) | |
111 | usb_unlink_urb(pcms->urbs[i]); | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | /* | |
117 | Wait until unlinking of all currently active URBs has been finished. | |
118 | */ | |
119 | static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm, | |
120 | struct line6_pcm_stream *pcms) | |
121 | { | |
122 | int timeout = HZ; | |
123 | int i; | |
124 | int alive; | |
125 | ||
126 | do { | |
127 | alive = 0; | |
128 | for (i = 0; i < LINE6_ISO_BUFFERS; i++) { | |
129 | if (test_bit(i, &pcms->active_urbs)) | |
130 | alive++; | |
131 | } | |
132 | if (!alive) | |
133 | break; | |
134 | set_current_state(TASK_UNINTERRUPTIBLE); | |
135 | schedule_timeout(1); | |
136 | } while (--timeout > 0); | |
137 | if (alive) | |
ccaac9ed TI |
138 | dev_err(line6pcm->line6->ifcdev, |
139 | "timeout: still %d active urbs..\n", alive); | |
d8131e67 TI |
140 | } |
141 | ||
63e20df1 TI |
142 | static inline struct line6_pcm_stream * |
143 | get_stream(struct snd_line6_pcm *line6pcm, int direction) | |
e90576c5 | 144 | { |
63e20df1 TI |
145 | return (direction == SNDRV_PCM_STREAM_PLAYBACK) ? |
146 | &line6pcm->out : &line6pcm->in; | |
e90576c5 TI |
147 | } |
148 | ||
63e20df1 TI |
149 | /* allocate a buffer if not opened yet; |
150 | * call this in line6pcm.state_change mutex | |
151 | */ | |
152 | static int line6_buffer_acquire(struct snd_line6_pcm *line6pcm, | |
153 | struct line6_pcm_stream *pstr, int type) | |
e90576c5 | 154 | { |
63e20df1 TI |
155 | /* Invoked multiple times in a row so allocate once only */ |
156 | if (!test_and_set_bit(type, &pstr->opened) && !pstr->buffer) { | |
157 | pstr->buffer = kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS * | |
158 | line6pcm->max_packet_size, GFP_KERNEL); | |
159 | if (!pstr->buffer) | |
160 | return -ENOMEM; | |
161 | } | |
162 | return 0; | |
e90576c5 TI |
163 | } |
164 | ||
63e20df1 TI |
165 | /* free a buffer if all streams are closed; |
166 | * call this in line6pcm.state_change mutex | |
167 | */ | |
168 | static void line6_buffer_release(struct snd_line6_pcm *line6pcm, | |
169 | struct line6_pcm_stream *pstr, int type) | |
6b02a17e | 170 | { |
63e20df1 TI |
171 | |
172 | clear_bit(type, &pstr->opened); | |
173 | if (!pstr->opened) { | |
174 | line6_wait_clear_audio_urbs(line6pcm, pstr); | |
175 | kfree(pstr->buffer); | |
176 | pstr->buffer = NULL; | |
177 | } | |
6b02a17e MG |
178 | } |
179 | ||
63e20df1 TI |
180 | /* start a PCM stream */ |
181 | static int line6_stream_start(struct snd_line6_pcm *line6pcm, int direction, | |
182 | int type) | |
1027f476 | 183 | { |
63e20df1 TI |
184 | unsigned long flags; |
185 | struct line6_pcm_stream *pstr = get_stream(line6pcm, direction); | |
186 | int ret = 0; | |
187 | ||
188 | spin_lock_irqsave(&pstr->lock, flags); | |
4d0e6775 TI |
189 | if (!test_and_set_bit(type, &pstr->running) && |
190 | !(pstr->active_urbs || pstr->unlink_urbs)) { | |
63e20df1 TI |
191 | pstr->count = 0; |
192 | /* Submit all currently available URBs */ | |
193 | if (direction == SNDRV_PCM_STREAM_PLAYBACK) | |
194 | ret = line6_submit_audio_out_all_urbs(line6pcm); | |
195 | else | |
196 | ret = line6_submit_audio_in_all_urbs(line6pcm); | |
0ca54888 | 197 | } |
63e20df1 TI |
198 | if (ret < 0) |
199 | clear_bit(type, &pstr->running); | |
200 | spin_unlock_irqrestore(&pstr->lock, flags); | |
201 | return ret; | |
202 | } | |
0ca54888 | 203 | |
63e20df1 TI |
204 | /* stop a PCM stream; this doesn't sync with the unlinked URBs */ |
205 | static void line6_stream_stop(struct snd_line6_pcm *line6pcm, int direction, | |
206 | int type) | |
207 | { | |
208 | unsigned long flags; | |
209 | struct line6_pcm_stream *pstr = get_stream(line6pcm, direction); | |
210 | ||
211 | spin_lock_irqsave(&pstr->lock, flags); | |
212 | clear_bit(type, &pstr->running); | |
213 | if (!pstr->running) { | |
214 | line6_unlink_audio_urbs(line6pcm, pstr); | |
215 | if (direction == SNDRV_PCM_STREAM_CAPTURE) { | |
216 | line6pcm->prev_fbuf = NULL; | |
217 | line6pcm->prev_fsize = 0; | |
6b02a17e | 218 | } |
63e20df1 TI |
219 | } |
220 | spin_unlock_irqrestore(&pstr->lock, flags); | |
221 | } | |
6b02a17e | 222 | |
63e20df1 TI |
223 | /* common PCM trigger callback */ |
224 | int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd) | |
225 | { | |
226 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); | |
227 | struct snd_pcm_substream *s; | |
228 | int err; | |
e1a164d7 | 229 | |
63e20df1 | 230 | clear_bit(LINE6_FLAG_PREPARED, &line6pcm->flags); |
0ca54888 | 231 | |
63e20df1 TI |
232 | snd_pcm_group_for_each_entry(s, substream) { |
233 | if (s->pcm->card != substream->pcm->card) | |
234 | continue; | |
e1a164d7 | 235 | |
63e20df1 TI |
236 | switch (cmd) { |
237 | case SNDRV_PCM_TRIGGER_START: | |
238 | case SNDRV_PCM_TRIGGER_RESUME: | |
239 | err = line6_stream_start(line6pcm, s->stream, | |
240 | LINE6_STREAM_PCM); | |
241 | if (err < 0) | |
242 | return err; | |
243 | break; | |
6b02a17e | 244 | |
63e20df1 TI |
245 | case SNDRV_PCM_TRIGGER_STOP: |
246 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
247 | line6_stream_stop(line6pcm, s->stream, | |
248 | LINE6_STREAM_PCM); | |
249 | break; | |
6b02a17e | 250 | |
63e20df1 TI |
251 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
252 | if (s->stream != SNDRV_PCM_STREAM_PLAYBACK) | |
253 | return -EINVAL; | |
254 | set_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags); | |
255 | break; | |
e1a164d7 | 256 | |
63e20df1 TI |
257 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
258 | if (s->stream != SNDRV_PCM_STREAM_PLAYBACK) | |
259 | return -EINVAL; | |
260 | clear_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags); | |
261 | break; | |
0ca54888 | 262 | |
63e20df1 TI |
263 | default: |
264 | return -EINVAL; | |
265 | } | |
1027f476 | 266 | } |
e1a164d7 | 267 | |
1027f476 MG |
268 | return 0; |
269 | } | |
270 | ||
2954f914 TI |
271 | /* common PCM pointer callback */ |
272 | snd_pcm_uframes_t snd_line6_pointer(struct snd_pcm_substream *substream) | |
273 | { | |
274 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); | |
275 | struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream); | |
276 | ||
277 | return pstr->pos_done; | |
278 | } | |
279 | ||
63e20df1 TI |
280 | /* Acquire and start duplex streams: |
281 | * type is either LINE6_STREAM_IMPULSE or LINE6_STREAM_MONITOR | |
282 | */ | |
283 | int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type) | |
1027f476 | 284 | { |
63e20df1 TI |
285 | struct line6_pcm_stream *pstr; |
286 | int ret = 0, dir; | |
287 | ||
288 | mutex_lock(&line6pcm->state_mutex); | |
289 | for (dir = 0; dir < 2; dir++) { | |
290 | pstr = get_stream(line6pcm, dir); | |
291 | ret = line6_buffer_acquire(line6pcm, pstr, type); | |
292 | if (ret < 0) | |
293 | goto error; | |
294 | if (!pstr->running) | |
295 | line6_wait_clear_audio_urbs(line6pcm, pstr); | |
f2bb614b | 296 | } |
63e20df1 TI |
297 | for (dir = 0; dir < 2; dir++) { |
298 | ret = line6_stream_start(line6pcm, dir, type); | |
299 | if (ret < 0) | |
300 | goto error; | |
1027f476 | 301 | } |
63e20df1 TI |
302 | error: |
303 | mutex_unlock(&line6pcm->state_mutex); | |
304 | if (ret < 0) | |
305 | line6_pcm_release(line6pcm, type); | |
306 | return ret; | |
307 | } | |
308 | EXPORT_SYMBOL_GPL(line6_pcm_acquire); | |
1027f476 | 309 | |
63e20df1 TI |
310 | /* Stop and release duplex streams */ |
311 | void line6_pcm_release(struct snd_line6_pcm *line6pcm, int type) | |
312 | { | |
313 | struct line6_pcm_stream *pstr; | |
314 | int dir; | |
315 | ||
316 | mutex_lock(&line6pcm->state_mutex); | |
317 | for (dir = 0; dir < 2; dir++) | |
318 | line6_stream_stop(line6pcm, dir, type); | |
319 | for (dir = 0; dir < 2; dir++) { | |
320 | pstr = get_stream(line6pcm, dir); | |
321 | line6_buffer_release(line6pcm, pstr, type); | |
1027f476 | 322 | } |
63e20df1 | 323 | mutex_unlock(&line6pcm->state_mutex); |
1027f476 | 324 | } |
ccddbe4a | 325 | EXPORT_SYMBOL_GPL(line6_pcm_release); |
1027f476 | 326 | |
63e20df1 TI |
327 | /* common PCM hw_params callback */ |
328 | int snd_line6_hw_params(struct snd_pcm_substream *substream, | |
329 | struct snd_pcm_hw_params *hw_params) | |
705ececd | 330 | { |
63e20df1 | 331 | int ret; |
705ececd | 332 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
63e20df1 TI |
333 | struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream); |
334 | ||
335 | mutex_lock(&line6pcm->state_mutex); | |
336 | ret = line6_buffer_acquire(line6pcm, pstr, LINE6_STREAM_PCM); | |
337 | if (ret < 0) | |
338 | goto error; | |
339 | ||
340 | ret = snd_pcm_lib_malloc_pages(substream, | |
341 | params_buffer_bytes(hw_params)); | |
342 | if (ret < 0) { | |
343 | line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM); | |
344 | goto error; | |
345 | } | |
705ececd | 346 | |
63e20df1 TI |
347 | pstr->period = params_period_bytes(hw_params); |
348 | error: | |
349 | mutex_unlock(&line6pcm->state_mutex); | |
350 | return ret; | |
351 | } | |
705ececd | 352 | |
63e20df1 TI |
353 | /* common PCM hw_free callback */ |
354 | int snd_line6_hw_free(struct snd_pcm_substream *substream) | |
355 | { | |
356 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); | |
357 | struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream); | |
705ececd | 358 | |
63e20df1 TI |
359 | mutex_lock(&line6pcm->state_mutex); |
360 | line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM); | |
361 | mutex_unlock(&line6pcm->state_mutex); | |
362 | return snd_pcm_lib_free_pages(substream); | |
705ececd MG |
363 | } |
364 | ||
63e20df1 | 365 | |
705ececd | 366 | /* control info callback */ |
1027f476 MG |
367 | static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol, |
368 | struct snd_ctl_elem_info *uinfo) | |
68dc3dde | 369 | { |
705ececd MG |
370 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
371 | uinfo->count = 2; | |
372 | uinfo->value.integer.min = 0; | |
373 | uinfo->value.integer.max = 256; | |
374 | return 0; | |
375 | } | |
376 | ||
377 | /* control get callback */ | |
1027f476 MG |
378 | static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol, |
379 | struct snd_ctl_elem_value *ucontrol) | |
68dc3dde | 380 | { |
705ececd MG |
381 | int i; |
382 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); | |
383 | ||
9fb754b7 | 384 | for (i = 0; i < 2; i++) |
1027f476 | 385 | ucontrol->value.integer.value[i] = line6pcm->volume_playback[i]; |
705ececd MG |
386 | |
387 | return 0; | |
388 | } | |
389 | ||
390 | /* control put callback */ | |
1027f476 MG |
391 | static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol, |
392 | struct snd_ctl_elem_value *ucontrol) | |
68dc3dde | 393 | { |
705ececd MG |
394 | int i, changed = 0; |
395 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); | |
396 | ||
9fb754b7 | 397 | for (i = 0; i < 2; i++) |
e1a164d7 MG |
398 | if (line6pcm->volume_playback[i] != |
399 | ucontrol->value.integer.value[i]) { | |
400 | line6pcm->volume_playback[i] = | |
401 | ucontrol->value.integer.value[i]; | |
705ececd MG |
402 | changed = 1; |
403 | } | |
404 | ||
405 | return changed; | |
406 | } | |
407 | ||
408 | /* control definition */ | |
075587b7 TI |
409 | static struct snd_kcontrol_new line6_controls[] = { |
410 | { | |
411 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
412 | .name = "PCM Playback Volume", | |
413 | .info = snd_line6_control_playback_info, | |
414 | .get = snd_line6_control_playback_get, | |
415 | .put = snd_line6_control_playback_put | |
416 | }, | |
417 | { | |
418 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
419 | .name = "Impulse Response Volume", | |
420 | .info = snd_line6_impulse_volume_info, | |
421 | .get = snd_line6_impulse_volume_get, | |
422 | .put = snd_line6_impulse_volume_put | |
423 | }, | |
424 | { | |
425 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
426 | .name = "Impulse Response Period", | |
427 | .info = snd_line6_impulse_period_info, | |
428 | .get = snd_line6_impulse_period_get, | |
429 | .put = snd_line6_impulse_period_put | |
430 | }, | |
705ececd MG |
431 | }; |
432 | ||
433 | /* | |
434 | Cleanup the PCM device. | |
435 | */ | |
d8131e67 | 436 | static void cleanup_urbs(struct line6_pcm_stream *pcms) |
705ececd MG |
437 | { |
438 | int i; | |
705ececd | 439 | |
9fb754b7 | 440 | for (i = 0; i < LINE6_ISO_BUFFERS; i++) { |
d8131e67 TI |
441 | if (pcms->urbs[i]) { |
442 | usb_kill_urb(pcms->urbs[i]); | |
443 | usb_free_urb(pcms->urbs[i]); | |
705ececd MG |
444 | } |
445 | } | |
d8131e67 TI |
446 | } |
447 | ||
448 | static void line6_cleanup_pcm(struct snd_pcm *pcm) | |
449 | { | |
450 | struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm); | |
451 | ||
452 | cleanup_urbs(&line6pcm->out); | |
453 | cleanup_urbs(&line6pcm->in); | |
b45a7c56 | 454 | kfree(line6pcm); |
705ececd MG |
455 | } |
456 | ||
457 | /* create a PCM device */ | |
b45a7c56 | 458 | static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret) |
705ececd MG |
459 | { |
460 | struct snd_pcm *pcm; | |
461 | int err; | |
462 | ||
b45a7c56 TI |
463 | err = snd_pcm_new(line6->card, (char *)line6->properties->name, |
464 | 0, 1, 1, pcm_ret); | |
68dc3dde | 465 | if (err < 0) |
705ececd | 466 | return err; |
b45a7c56 TI |
467 | pcm = *pcm_ret; |
468 | strcpy(pcm->name, line6->properties->name); | |
705ececd MG |
469 | |
470 | /* set operators */ | |
afb9091d SB |
471 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, |
472 | &snd_line6_playback_ops); | |
e1a164d7 | 473 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops); |
705ececd MG |
474 | |
475 | /* pre-allocation of buffers */ | |
68dc3dde | 476 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, |
e1a164d7 MG |
477 | snd_dma_continuous_data |
478 | (GFP_KERNEL), 64 * 1024, | |
479 | 128 * 1024); | |
705ececd MG |
480 | return 0; |
481 | } | |
482 | ||
1027f476 | 483 | /* |
5a475311 | 484 | Sync with PCM stream stops. |
1027f476 MG |
485 | */ |
486 | void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm) | |
487 | { | |
d8131e67 TI |
488 | line6_unlink_audio_urbs(line6pcm, &line6pcm->out); |
489 | line6_unlink_audio_urbs(line6pcm, &line6pcm->in); | |
490 | line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out); | |
491 | line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in); | |
1027f476 MG |
492 | } |
493 | ||
705ececd MG |
494 | /* |
495 | Create and register the PCM device and mixer entries. | |
496 | Create URBs for playback and capture. | |
497 | */ | |
68dc3dde GKH |
498 | int line6_init_pcm(struct usb_line6 *line6, |
499 | struct line6_pcm_properties *properties) | |
705ececd | 500 | { |
075587b7 | 501 | int i, err; |
16d603d3 CR |
502 | unsigned ep_read = line6->properties->ep_audio_r; |
503 | unsigned ep_write = line6->properties->ep_audio_w; | |
b45a7c56 | 504 | struct snd_pcm *pcm; |
705ececd MG |
505 | struct snd_line6_pcm *line6pcm; |
506 | ||
4cb1a4ae | 507 | if (!(line6->properties->capabilities & LINE6_CAP_PCM)) |
e1a164d7 | 508 | return 0; /* skip PCM initialization and report success */ |
705ececd | 509 | |
b45a7c56 TI |
510 | err = snd_line6_new_pcm(line6, &pcm); |
511 | if (err < 0) | |
512 | return err; | |
705ececd | 513 | |
b45a7c56 TI |
514 | line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL); |
515 | if (!line6pcm) | |
705ececd MG |
516 | return -ENOMEM; |
517 | ||
63e20df1 | 518 | mutex_init(&line6pcm->state_mutex); |
b45a7c56 TI |
519 | line6pcm->pcm = pcm; |
520 | line6pcm->properties = properties; | |
1027f476 MG |
521 | line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255; |
522 | line6pcm->volume_monitor = 255; | |
705ececd | 523 | line6pcm->line6 = line6; |
3b08db37 SH |
524 | |
525 | /* Read and write buffers are sized identically, so choose minimum */ | |
526 | line6pcm->max_packet_size = min( | |
527 | usb_maxpacket(line6->usbdev, | |
528 | usb_rcvisocpipe(line6->usbdev, ep_read), 0), | |
529 | usb_maxpacket(line6->usbdev, | |
530 | usb_sndisocpipe(line6->usbdev, ep_write), 1)); | |
531 | ||
ad0119ab TI |
532 | spin_lock_init(&line6pcm->out.lock); |
533 | spin_lock_init(&line6pcm->in.lock); | |
075587b7 | 534 | line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD; |
705ececd | 535 | |
b45a7c56 TI |
536 | line6->line6pcm = line6pcm; |
537 | ||
538 | pcm->private_data = line6pcm; | |
539 | pcm->private_free = line6_cleanup_pcm; | |
540 | ||
1027f476 | 541 | err = line6_create_audio_out_urbs(line6pcm); |
68dc3dde | 542 | if (err < 0) |
705ececd MG |
543 | return err; |
544 | ||
1027f476 | 545 | err = line6_create_audio_in_urbs(line6pcm); |
68dc3dde | 546 | if (err < 0) |
705ececd MG |
547 | return err; |
548 | ||
549 | /* mixer: */ | |
075587b7 TI |
550 | for (i = 0; i < ARRAY_SIZE(line6_controls); i++) { |
551 | err = snd_ctl_add(line6->card, | |
552 | snd_ctl_new1(&line6_controls[i], line6pcm)); | |
553 | if (err < 0) | |
554 | return err; | |
555 | } | |
1027f476 | 556 | |
705ececd MG |
557 | return 0; |
558 | } | |
ccddbe4a | 559 | EXPORT_SYMBOL_GPL(line6_init_pcm); |
705ececd MG |
560 | |
561 | /* prepare pcm callback */ | |
562 | int snd_line6_prepare(struct snd_pcm_substream *substream) | |
563 | { | |
564 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); | |
63e20df1 | 565 | struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream); |
705ececd | 566 | |
63e20df1 TI |
567 | mutex_lock(&line6pcm->state_mutex); |
568 | if (!pstr->running) | |
569 | line6_wait_clear_audio_urbs(line6pcm, pstr); | |
665f3f50 | 570 | |
63e20df1 | 571 | if (!test_and_set_bit(LINE6_FLAG_PREPARED, &line6pcm->flags)) { |
ad0119ab TI |
572 | line6pcm->out.count = 0; |
573 | line6pcm->out.pos = 0; | |
574 | line6pcm->out.pos_done = 0; | |
575 | line6pcm->out.bytes = 0; | |
576 | line6pcm->in.count = 0; | |
577 | line6pcm->in.pos_done = 0; | |
578 | line6pcm->in.bytes = 0; | |
705ececd MG |
579 | } |
580 | ||
63e20df1 | 581 | mutex_unlock(&line6pcm->state_mutex); |
705ececd MG |
582 | return 0; |
583 | } |