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