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