Commit | Line | Data |
---|---|---|
705ececd | 1 | /* |
e1a164d7 | 2 | * Line6 Linux USB driver - 0.9.1beta |
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 | ||
140e28b8 | 12 | #include <linux/slab.h> |
705ececd MG |
13 | #include <sound/core.h> |
14 | #include <sound/pcm.h> | |
15 | #include <sound/pcm_params.h> | |
16 | ||
17 | #include "audio.h" | |
1027f476 MG |
18 | #include "capture.h" |
19 | #include "driver.h" | |
705ececd MG |
20 | #include "pcm.h" |
21 | #include "pod.h" | |
1027f476 | 22 | |
705ececd MG |
23 | /* |
24 | Find a free URB and submit it. | |
25 | */ | |
1027f476 | 26 | static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm) |
705ececd | 27 | { |
1027f476 | 28 | int index; |
705ececd | 29 | unsigned long flags; |
705ececd | 30 | int i, urb_size; |
e1a164d7 | 31 | int ret; |
705ececd MG |
32 | struct urb *urb_in; |
33 | ||
34 | spin_lock_irqsave(&line6pcm->lock_audio_in, flags); | |
e0645d63 SB |
35 | index = |
36 | find_first_zero_bit(&line6pcm->active_urb_in, LINE6_ISO_BUFFERS); | |
705ececd | 37 | |
1027f476 | 38 | if (index < 0 || index >= LINE6_ISO_BUFFERS) { |
705ececd | 39 | spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags); |
1027f476 | 40 | dev_err(line6pcm->line6->ifcdev, "no free URB found\n"); |
705ececd MG |
41 | return -EINVAL; |
42 | } | |
43 | ||
44 | urb_in = line6pcm->urb_audio_in[index]; | |
45 | urb_size = 0; | |
46 | ||
6efc5667 | 47 | for (i = 0; i < LINE6_ISO_PACKETS; ++i) { |
e0645d63 SB |
48 | struct usb_iso_packet_descriptor *fin = |
49 | &urb_in->iso_frame_desc[i]; | |
705ececd MG |
50 | fin->offset = urb_size; |
51 | fin->length = line6pcm->max_packet_size; | |
52 | urb_size += line6pcm->max_packet_size; | |
53 | } | |
54 | ||
e0645d63 SB |
55 | urb_in->transfer_buffer = |
56 | line6pcm->buffer_in + | |
57 | index * LINE6_ISO_PACKETS * line6pcm->max_packet_size; | |
705ececd | 58 | urb_in->transfer_buffer_length = urb_size; |
1027f476 | 59 | urb_in->context = line6pcm; |
705ececd | 60 | |
e1a164d7 MG |
61 | ret = usb_submit_urb(urb_in, GFP_ATOMIC); |
62 | ||
63 | if (ret == 0) | |
705ececd MG |
64 | set_bit(index, &line6pcm->active_urb_in); |
65 | else | |
1027f476 | 66 | dev_err(line6pcm->line6->ifcdev, |
e1a164d7 | 67 | "URB in #%d submission failed (%d)\n", index, ret); |
705ececd MG |
68 | |
69 | spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags); | |
70 | return 0; | |
71 | } | |
72 | ||
73 | /* | |
74 | Submit all currently available capture URBs. | |
75 | */ | |
1027f476 | 76 | int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm) |
705ececd MG |
77 | { |
78 | int ret, i; | |
79 | ||
6efc5667 | 80 | for (i = 0; i < LINE6_ISO_BUFFERS; ++i) { |
1027f476 | 81 | ret = submit_audio_in_urb(line6pcm); |
6efc5667 | 82 | if (ret < 0) |
705ececd | 83 | return ret; |
6efc5667 | 84 | } |
705ececd MG |
85 | |
86 | return 0; | |
87 | } | |
88 | ||
89 | /* | |
90 | Unlink all currently active capture URBs. | |
91 | */ | |
1027f476 | 92 | void line6_unlink_audio_in_urbs(struct snd_line6_pcm *line6pcm) |
705ececd MG |
93 | { |
94 | unsigned int i; | |
95 | ||
6efc5667 GKH |
96 | for (i = LINE6_ISO_BUFFERS; i--;) { |
97 | if (test_bit(i, &line6pcm->active_urb_in)) { | |
98 | if (!test_and_set_bit(i, &line6pcm->unlink_urb_in)) { | |
705ececd | 99 | struct urb *u = line6pcm->urb_audio_in[i]; |
705ececd MG |
100 | usb_unlink_urb(u); |
101 | } | |
102 | } | |
103 | } | |
104 | } | |
105 | ||
106 | /* | |
6efc5667 GKH |
107 | Wait until unlinking of all currently active capture URBs has been |
108 | finished. | |
705ececd | 109 | */ |
0ca54888 | 110 | void line6_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm) |
705ececd MG |
111 | { |
112 | int timeout = HZ; | |
113 | unsigned int i; | |
114 | int alive; | |
115 | ||
116 | do { | |
117 | alive = 0; | |
118 | for (i = LINE6_ISO_BUFFERS; i--;) { | |
119 | if (test_bit(i, &line6pcm->active_urb_in)) | |
120 | alive++; | |
121 | } | |
6efc5667 | 122 | if (!alive) |
705ececd MG |
123 | break; |
124 | set_current_state(TASK_UNINTERRUPTIBLE); | |
125 | schedule_timeout(1); | |
126 | } while (--timeout > 0); | |
127 | if (alive) | |
128 | snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive); | |
705ececd MG |
129 | } |
130 | ||
131 | /* | |
132 | Unlink all currently active capture URBs, and wait for finishing. | |
133 | */ | |
1027f476 | 134 | void line6_unlink_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm) |
705ececd | 135 | { |
1027f476 | 136 | line6_unlink_audio_in_urbs(line6pcm); |
0ca54888 | 137 | line6_wait_clear_audio_in_urbs(line6pcm); |
705ececd MG |
138 | } |
139 | ||
140 | /* | |
1027f476 MG |
141 | Copy data into ALSA capture buffer. |
142 | */ | |
143 | void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize) | |
144 | { | |
145 | struct snd_pcm_substream *substream = | |
146 | get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE); | |
147 | struct snd_pcm_runtime *runtime = substream->runtime; | |
148 | const int bytes_per_frame = line6pcm->properties->bytes_per_frame; | |
149 | int frames = fsize / bytes_per_frame; | |
150 | ||
597a1e7c | 151 | if (runtime == NULL) |
c7fcf255 MG |
152 | return; |
153 | ||
1027f476 MG |
154 | if (line6pcm->pos_in_done + frames > runtime->buffer_size) { |
155 | /* | |
e1a164d7 MG |
156 | The transferred area goes over buffer boundary, |
157 | copy two separate chunks. | |
158 | */ | |
1027f476 MG |
159 | int len; |
160 | len = runtime->buffer_size - line6pcm->pos_in_done; | |
161 | ||
162 | if (len > 0) { | |
163 | memcpy(runtime->dma_area + | |
164 | line6pcm->pos_in_done * bytes_per_frame, fbuf, | |
165 | len * bytes_per_frame); | |
166 | memcpy(runtime->dma_area, fbuf + len * bytes_per_frame, | |
167 | (frames - len) * bytes_per_frame); | |
027360c5 GKH |
168 | } else { |
169 | /* this is somewhat paranoid */ | |
170 | dev_err(line6pcm->line6->ifcdev, | |
171 | "driver bug: len = %d\n", len); | |
172 | } | |
1027f476 MG |
173 | } else { |
174 | /* copy single chunk */ | |
175 | memcpy(runtime->dma_area + | |
176 | line6pcm->pos_in_done * bytes_per_frame, fbuf, fsize); | |
177 | } | |
178 | ||
027360c5 GKH |
179 | line6pcm->pos_in_done += frames; |
180 | if (line6pcm->pos_in_done >= runtime->buffer_size) | |
1027f476 MG |
181 | line6pcm->pos_in_done -= runtime->buffer_size; |
182 | } | |
183 | ||
184 | void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length) | |
185 | { | |
186 | struct snd_pcm_substream *substream = | |
187 | get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE); | |
188 | ||
027360c5 GKH |
189 | line6pcm->bytes_in += length; |
190 | if (line6pcm->bytes_in >= line6pcm->period_in) { | |
1027f476 MG |
191 | line6pcm->bytes_in %= line6pcm->period_in; |
192 | snd_pcm_period_elapsed(substream); | |
193 | } | |
194 | } | |
195 | ||
6b02a17e MG |
196 | void line6_free_capture_buffer(struct snd_line6_pcm *line6pcm) |
197 | { | |
198 | kfree(line6pcm->buffer_in); | |
199 | line6pcm->buffer_in = NULL; | |
200 | } | |
201 | ||
1027f476 | 202 | /* |
027360c5 GKH |
203 | * Callback for completed capture URB. |
204 | */ | |
0c7ab158 | 205 | static void audio_in_callback(struct urb *urb) |
705ececd MG |
206 | { |
207 | int i, index, length = 0, shutdown = 0; | |
705ececd MG |
208 | unsigned long flags; |
209 | ||
1027f476 MG |
210 | struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context; |
211 | ||
212 | line6pcm->last_frame_in = urb->start_frame; | |
705ececd MG |
213 | |
214 | /* find index of URB */ | |
6efc5667 GKH |
215 | for (index = 0; index < LINE6_ISO_BUFFERS; ++index) |
216 | if (urb == line6pcm->urb_audio_in[index]) | |
705ececd MG |
217 | break; |
218 | ||
705ececd MG |
219 | spin_lock_irqsave(&line6pcm->lock_audio_in, flags); |
220 | ||
6efc5667 | 221 | for (i = 0; i < LINE6_ISO_PACKETS; ++i) { |
705ececd MG |
222 | char *fbuf; |
223 | int fsize; | |
224 | struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i]; | |
225 | ||
e1a164d7 | 226 | if (fin->status == -EXDEV) { |
705ececd MG |
227 | shutdown = 1; |
228 | break; | |
229 | } | |
230 | ||
231 | fbuf = urb->transfer_buffer + fin->offset; | |
232 | fsize = fin->actual_length; | |
1027f476 MG |
233 | |
234 | if (fsize > line6pcm->max_packet_size) { | |
235 | dev_err(line6pcm->line6->ifcdev, | |
236 | "driver and/or device bug: packet too large (%d > %d)\n", | |
237 | fsize, line6pcm->max_packet_size); | |
238 | } | |
239 | ||
705ececd MG |
240 | length += fsize; |
241 | ||
1027f476 | 242 | /* the following assumes LINE6_ISO_PACKETS == 1: */ |
1027f476 | 243 | line6pcm->prev_fbuf = fbuf; |
1027f476 | 244 | line6pcm->prev_fsize = fsize; |
705ececd | 245 | |
1027f476 | 246 | #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE |
0ca54888 | 247 | if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE)) |
1027f476 | 248 | #endif |
a71cac2a SH |
249 | if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM, |
250 | &line6pcm->flags) && (fsize > 0)) | |
1027f476 | 251 | line6_capture_copy(line6pcm, fbuf, fsize); |
705ececd MG |
252 | } |
253 | ||
254 | clear_bit(index, &line6pcm->active_urb_in); | |
255 | ||
1027f476 | 256 | if (test_and_clear_bit(index, &line6pcm->unlink_urb_in)) |
705ececd MG |
257 | shutdown = 1; |
258 | ||
259 | spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags); | |
260 | ||
6efc5667 | 261 | if (!shutdown) { |
1027f476 | 262 | submit_audio_in_urb(line6pcm); |
705ececd | 263 | |
e1a164d7 | 264 | #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE |
0ca54888 | 265 | if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE)) |
e1a164d7 | 266 | #endif |
a71cac2a SH |
267 | if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM, |
268 | &line6pcm->flags)) | |
e1a164d7 | 269 | line6_capture_check_period(line6pcm, length); |
705ececd MG |
270 | } |
271 | } | |
272 | ||
273 | /* open capture callback */ | |
274 | static int snd_line6_capture_open(struct snd_pcm_substream *substream) | |
275 | { | |
276 | int err; | |
277 | struct snd_pcm_runtime *runtime = substream->runtime; | |
278 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); | |
279 | ||
6efc5667 GKH |
280 | err = snd_pcm_hw_constraint_ratdens(runtime, 0, |
281 | SNDRV_PCM_HW_PARAM_RATE, | |
e1a164d7 MG |
282 | (&line6pcm-> |
283 | properties->snd_line6_rates)); | |
6efc5667 | 284 | if (err < 0) |
705ececd MG |
285 | return err; |
286 | ||
287 | runtime->hw = line6pcm->properties->snd_line6_capture_hw; | |
288 | return 0; | |
289 | } | |
290 | ||
291 | /* close capture callback */ | |
292 | static int snd_line6_capture_close(struct snd_pcm_substream *substream) | |
293 | { | |
294 | return 0; | |
295 | } | |
296 | ||
297 | /* hw_params capture callback */ | |
6efc5667 GKH |
298 | static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream, |
299 | struct snd_pcm_hw_params *hw_params) | |
705ececd MG |
300 | { |
301 | int ret; | |
302 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); | |
303 | ||
304 | /* -- Florian Demski [FD] */ | |
305 | /* don't ask me why, but this fixes the bug on my machine */ | |
6efc5667 GKH |
306 | if (line6pcm == NULL) { |
307 | if (substream->pcm == NULL) | |
705ececd | 308 | return -ENOMEM; |
6efc5667 | 309 | if (substream->pcm->private_data == NULL) |
705ececd MG |
310 | return -ENOMEM; |
311 | substream->private_data = substream->pcm->private_data; | |
6efc5667 | 312 | line6pcm = snd_pcm_substream_chip(substream); |
705ececd MG |
313 | } |
314 | /* -- [FD] end */ | |
315 | ||
0ca54888 | 316 | ret = line6_pcm_acquire(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER); |
140e28b8 | 317 | |
0ca54888 MG |
318 | if (ret < 0) |
319 | return ret; | |
140e28b8 | 320 | |
6efc5667 GKH |
321 | ret = snd_pcm_lib_malloc_pages(substream, |
322 | params_buffer_bytes(hw_params)); | |
0ca54888 MG |
323 | if (ret < 0) { |
324 | line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER); | |
705ececd | 325 | return ret; |
0ca54888 | 326 | } |
705ececd MG |
327 | |
328 | line6pcm->period_in = params_period_bytes(hw_params); | |
705ececd MG |
329 | return 0; |
330 | } | |
331 | ||
332 | /* hw_free capture callback */ | |
333 | static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream) | |
334 | { | |
140e28b8 | 335 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
0ca54888 | 336 | line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER); |
705ececd MG |
337 | return snd_pcm_lib_free_pages(substream); |
338 | } | |
339 | ||
340 | /* trigger callback */ | |
1027f476 | 341 | int snd_line6_capture_trigger(struct snd_line6_pcm *line6pcm, int cmd) |
705ececd | 342 | { |
705ececd | 343 | int err; |
705ececd | 344 | |
6efc5667 | 345 | switch (cmd) { |
705ececd | 346 | case SNDRV_PCM_TRIGGER_START: |
1027f476 MG |
347 | #ifdef CONFIG_PM |
348 | case SNDRV_PCM_TRIGGER_RESUME: | |
349 | #endif | |
a71cac2a SH |
350 | err = line6_pcm_acquire(line6pcm, |
351 | LINE6_BIT_PCM_ALSA_CAPTURE_STREAM); | |
705ececd | 352 | |
1027f476 MG |
353 | if (err < 0) |
354 | return err; | |
705ececd MG |
355 | |
356 | break; | |
357 | ||
358 | case SNDRV_PCM_TRIGGER_STOP: | |
1027f476 MG |
359 | #ifdef CONFIG_PM |
360 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
361 | #endif | |
a71cac2a SH |
362 | err = line6_pcm_release(line6pcm, |
363 | LINE6_BIT_PCM_ALSA_CAPTURE_STREAM); | |
1027f476 MG |
364 | |
365 | if (err < 0) | |
366 | return err; | |
705ececd MG |
367 | |
368 | break; | |
369 | ||
370 | default: | |
371 | return -EINVAL; | |
372 | } | |
373 | ||
374 | return 0; | |
375 | } | |
376 | ||
377 | /* capture pointer callback */ | |
378 | static snd_pcm_uframes_t | |
379 | snd_line6_capture_pointer(struct snd_pcm_substream *substream) | |
380 | { | |
381 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); | |
382 | return line6pcm->pos_in_done; | |
383 | } | |
384 | ||
385 | /* capture operators */ | |
386 | struct snd_pcm_ops snd_line6_capture_ops = { | |
e1a164d7 MG |
387 | .open = snd_line6_capture_open, |
388 | .close = snd_line6_capture_close, | |
389 | .ioctl = snd_pcm_lib_ioctl, | |
390 | .hw_params = snd_line6_capture_hw_params, | |
391 | .hw_free = snd_line6_capture_hw_free, | |
392 | .prepare = snd_line6_prepare, | |
393 | .trigger = snd_line6_trigger, | |
394 | .pointer = snd_line6_capture_pointer, | |
705ececd MG |
395 | }; |
396 | ||
1027f476 | 397 | int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm) |
705ececd MG |
398 | { |
399 | int i; | |
400 | ||
401 | /* create audio URBs and fill in constant values: */ | |
6efc5667 | 402 | for (i = 0; i < LINE6_ISO_BUFFERS; ++i) { |
705ececd MG |
403 | struct urb *urb; |
404 | ||
405 | /* URB for audio in: */ | |
e0645d63 SB |
406 | urb = line6pcm->urb_audio_in[i] = |
407 | usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL); | |
705ececd | 408 | |
6efc5667 | 409 | if (urb == NULL) { |
705ececd MG |
410 | dev_err(line6pcm->line6->ifcdev, "Out of memory\n"); |
411 | return -ENOMEM; | |
412 | } | |
413 | ||
414 | urb->dev = line6pcm->line6->usbdev; | |
e0645d63 SB |
415 | urb->pipe = |
416 | usb_rcvisocpipe(line6pcm->line6->usbdev, | |
e1a164d7 MG |
417 | line6pcm->ep_audio_read & |
418 | USB_ENDPOINT_NUMBER_MASK); | |
705ececd MG |
419 | urb->transfer_flags = URB_ISO_ASAP; |
420 | urb->start_frame = -1; | |
421 | urb->number_of_packets = LINE6_ISO_PACKETS; | |
422 | urb->interval = LINE6_ISO_INTERVAL; | |
423 | urb->error_count = 0; | |
424 | urb->complete = audio_in_callback; | |
425 | } | |
426 | ||
427 | return 0; | |
428 | } |