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