include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / line6 / capture.c
CommitLineData
705ececd
MG
1/*
2 * Line6 Linux USB driver - 0.8.0
3 *
4 * Copyright (C) 2004-2009 Markus Grabner (grabner@icg.tugraz.at)
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
12#include "driver.h"
13
5a0e3ad6
TH
14#include <linux/slab.h>
15
705ececd
MG
16#include <sound/core.h>
17#include <sound/pcm.h>
18#include <sound/pcm_params.h>
19
20#include "audio.h"
21#include "pcm.h"
22#include "pod.h"
b702ed25 23#include "capture.h"
705ececd 24
705ececd
MG
25/*
26 Find a free URB and submit it.
27*/
28static int submit_audio_in_urb(struct snd_pcm_substream *substream)
29{
c2f5e9d2 30 unsigned int index;
705ececd
MG
31 unsigned long flags;
32 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
33 int i, urb_size;
34 struct urb *urb_in;
35
36 spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
e0645d63
SB
37 index =
38 find_first_zero_bit(&line6pcm->active_urb_in, LINE6_ISO_BUFFERS);
705ececd 39
c2f5e9d2 40 if (index >= LINE6_ISO_BUFFERS) {
705ececd
MG
41 spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
42 dev_err(s2m(substream), "no free URB found\n");
43 return -EINVAL;
44 }
45
46 urb_in = line6pcm->urb_audio_in[index];
47 urb_size = 0;
48
6efc5667 49 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
e0645d63
SB
50 struct usb_iso_packet_descriptor *fin =
51 &urb_in->iso_frame_desc[i];
705ececd
MG
52 fin->offset = urb_size;
53 fin->length = line6pcm->max_packet_size;
54 urb_size += line6pcm->max_packet_size;
55 }
56
e0645d63
SB
57 urb_in->transfer_buffer =
58 line6pcm->buffer_in +
59 index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
705ececd
MG
60 urb_in->transfer_buffer_length = urb_size;
61 urb_in->context = substream;
62
6efc5667 63 if (usb_submit_urb(urb_in, GFP_ATOMIC) == 0)
705ececd
MG
64 set_bit(index, &line6pcm->active_urb_in);
65 else
e0645d63
SB
66 dev_err(s2m(substream), "URB in #%d submission failed\n",
67 index);
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*/
76static int submit_audio_in_all_urbs(struct snd_pcm_substream *substream)
77{
78 int ret, i;
79
6efc5667
GKH
80 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
81 ret = submit_audio_in_urb(substream);
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*/
92static void unlink_audio_in_urbs(struct snd_line6_pcm *line6pcm)
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
MG
109*/
110static void wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
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);
129
130 line6pcm->active_urb_in = 0;
131 line6pcm->unlink_urb_in = 0;
132}
133
134/*
135 Unlink all currently active capture URBs, and wait for finishing.
136*/
137void unlink_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
138{
139 unlink_audio_in_urbs(line6pcm);
140 wait_clear_audio_in_urbs(line6pcm);
141}
142
143/*
144 Callback for completed capture URB.
145*/
0c7ab158 146static void audio_in_callback(struct urb *urb)
705ececd
MG
147{
148 int i, index, length = 0, shutdown = 0;
149 int frames;
150 unsigned long flags;
151
e0645d63
SB
152 struct snd_pcm_substream *substream =
153 (struct snd_pcm_substream *)urb->context;
705ececd
MG
154 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
155 const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
156 struct snd_pcm_runtime *runtime = substream->runtime;
157
158 /* find index of URB */
6efc5667
GKH
159 for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
160 if (urb == line6pcm->urb_audio_in[index])
705ececd
MG
161 break;
162
163#if DO_DUMP_PCM_RECEIVE
6efc5667 164 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
e0645d63
SB
165 struct usb_iso_packet_descriptor *fout =
166 &urb->iso_frame_desc[i];
167 line6_write_hexdump(line6pcm->line6, 'C',
168 urb->transfer_buffer + fout->offset,
169 fout->length);
705ececd
MG
170 }
171#endif
172
173 spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
174
6efc5667 175 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
705ececd
MG
176 char *fbuf;
177 int fsize;
178 struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
179
6efc5667 180 if (fin->status == -18) {
705ececd
MG
181 shutdown = 1;
182 break;
183 }
184
185 fbuf = urb->transfer_buffer + fin->offset;
186 fsize = fin->actual_length;
187 length += fsize;
188
6efc5667 189 if (fsize > 0) {
705ececd
MG
190 frames = fsize / bytes_per_frame;
191
e0645d63
SB
192 if (line6pcm->pos_in_done + frames >
193 runtime->buffer_size) {
705ececd 194 /*
e0645d63
SB
195 The transferred area goes over buffer
196 boundary, copy two separate chunks.
197 */
705ececd 198 int len;
e0645d63
SB
199 len =
200 runtime->buffer_size -
201 line6pcm->pos_in_done;
705ececd 202
6efc5667 203 if (len > 0) {
e0645d63
SB
204 memcpy(runtime->dma_area +
205 line6pcm->pos_in_done *
206 bytes_per_frame, fbuf,
207 len * bytes_per_frame);
208 memcpy(runtime->dma_area,
209 fbuf + len * bytes_per_frame,
210 (frames -
211 len) * bytes_per_frame);
212 } else {
213 /* this is somewhat paranoid */
214 dev_err(s2m(substream),
215 "driver bug: len = %d\n", len);
216 }
6efc5667 217 } else {
705ececd 218 /* copy single chunk */
e0645d63
SB
219 memcpy(runtime->dma_area +
220 line6pcm->pos_in_done * bytes_per_frame,
221 fbuf, fsize * bytes_per_frame);
705ececd
MG
222 }
223
df30c0f0
SB
224 line6pcm->pos_in_done += frames;
225 if (line6pcm->pos_in_done >= runtime->buffer_size)
705ececd
MG
226 line6pcm->pos_in_done -= runtime->buffer_size;
227 }
228 }
229
230 clear_bit(index, &line6pcm->active_urb_in);
231
6efc5667 232 if (test_bit(index, &line6pcm->unlink_urb_in))
705ececd
MG
233 shutdown = 1;
234
235 spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
236
6efc5667 237 if (!shutdown) {
705ececd
MG
238 submit_audio_in_urb(substream);
239
df30c0f0
SB
240 line6pcm->bytes_in += length;
241 if (line6pcm->bytes_in >= line6pcm->period_in) {
705ececd
MG
242 line6pcm->bytes_in -= line6pcm->period_in;
243 snd_pcm_period_elapsed(substream);
244 }
245 }
246}
247
248/* open capture callback */
249static int snd_line6_capture_open(struct snd_pcm_substream *substream)
250{
251 int err;
252 struct snd_pcm_runtime *runtime = substream->runtime;
253 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
254
6efc5667
GKH
255 err = snd_pcm_hw_constraint_ratdens(runtime, 0,
256 SNDRV_PCM_HW_PARAM_RATE,
e0645d63
SB
257 (&line6pcm->properties->
258 snd_line6_rates));
6efc5667 259 if (err < 0)
705ececd
MG
260 return err;
261
262 runtime->hw = line6pcm->properties->snd_line6_capture_hw;
263 return 0;
264}
265
266/* close capture callback */
267static int snd_line6_capture_close(struct snd_pcm_substream *substream)
268{
269 return 0;
270}
271
272/* hw_params capture callback */
6efc5667
GKH
273static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream,
274 struct snd_pcm_hw_params *hw_params)
705ececd
MG
275{
276 int ret;
277 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
278
279 /* -- Florian Demski [FD] */
280 /* don't ask me why, but this fixes the bug on my machine */
6efc5667
GKH
281 if (line6pcm == NULL) {
282 if (substream->pcm == NULL)
705ececd 283 return -ENOMEM;
6efc5667 284 if (substream->pcm->private_data == NULL)
705ececd
MG
285 return -ENOMEM;
286 substream->private_data = substream->pcm->private_data;
6efc5667 287 line6pcm = snd_pcm_substream_chip(substream);
705ececd
MG
288 }
289 /* -- [FD] end */
290
6efc5667
GKH
291 ret = snd_pcm_lib_malloc_pages(substream,
292 params_buffer_bytes(hw_params));
293 if (ret < 0)
705ececd
MG
294 return ret;
295
296 line6pcm->period_in = params_period_bytes(hw_params);
e0645d63
SB
297 line6pcm->buffer_in =
298 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
299 LINE6_ISO_PACKET_SIZE_MAX, GFP_KERNEL);
705ececd 300
6efc5667 301 if (!line6pcm->buffer_in) {
705ececd
MG
302 dev_err(s2m(substream), "cannot malloc buffer_in\n");
303 return -ENOMEM;
304 }
305
306 return 0;
307}
308
309/* hw_free capture callback */
310static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
311{
312 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
313 unlink_wait_clear_audio_in_urbs(line6pcm);
314
6efc5667
GKH
315 kfree(line6pcm->buffer_in);
316 line6pcm->buffer_in = NULL;
705ececd
MG
317
318 return snd_pcm_lib_free_pages(substream);
319}
320
321/* trigger callback */
322int snd_line6_capture_trigger(struct snd_pcm_substream *substream, int cmd)
323{
324 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
325 int err;
326 line6pcm->count_in = 0;
327
6efc5667 328 switch (cmd) {
705ececd 329 case SNDRV_PCM_TRIGGER_START:
6efc5667 330 if (!test_and_set_bit(BIT_RUNNING_CAPTURE, &line6pcm->flags)) {
705ececd
MG
331 err = submit_audio_in_all_urbs(substream);
332
6efc5667 333 if (err < 0) {
e0645d63
SB
334 clear_bit(BIT_RUNNING_CAPTURE,
335 &line6pcm->flags);
705ececd
MG
336 return err;
337 }
338 }
339
340 break;
341
342 case SNDRV_PCM_TRIGGER_STOP:
6efc5667 343 if (test_and_clear_bit(BIT_RUNNING_CAPTURE, &line6pcm->flags))
705ececd
MG
344 unlink_audio_in_urbs(line6pcm);
345
346 break;
347
348 default:
349 return -EINVAL;
350 }
351
352 return 0;
353}
354
355/* capture pointer callback */
356static snd_pcm_uframes_t
357snd_line6_capture_pointer(struct snd_pcm_substream *substream)
358{
359 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
360 return line6pcm->pos_in_done;
361}
362
363/* capture operators */
364struct snd_pcm_ops snd_line6_capture_ops = {
e0645d63
SB
365 .open = snd_line6_capture_open,
366 .close = snd_line6_capture_close,
367 .ioctl = snd_pcm_lib_ioctl,
368 .hw_params = snd_line6_capture_hw_params,
369 .hw_free = snd_line6_capture_hw_free,
370 .prepare = snd_line6_prepare,
371 .trigger = snd_line6_trigger,
372 .pointer = snd_line6_capture_pointer,
705ececd
MG
373};
374
375int create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
376{
377 int i;
378
379 /* create audio URBs and fill in constant values: */
6efc5667 380 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
705ececd
MG
381 struct urb *urb;
382
383 /* URB for audio in: */
e0645d63
SB
384 urb = line6pcm->urb_audio_in[i] =
385 usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
705ececd 386
6efc5667 387 if (urb == NULL) {
705ececd
MG
388 dev_err(line6pcm->line6->ifcdev, "Out of memory\n");
389 return -ENOMEM;
390 }
391
392 urb->dev = line6pcm->line6->usbdev;
e0645d63
SB
393 urb->pipe =
394 usb_rcvisocpipe(line6pcm->line6->usbdev,
395 line6pcm->
396 ep_audio_read & USB_ENDPOINT_NUMBER_MASK);
705ececd
MG
397 urb->transfer_flags = URB_ISO_ASAP;
398 urb->start_frame = -1;
399 urb->number_of_packets = LINE6_ISO_PACKETS;
400 urb->interval = LINE6_ISO_INTERVAL;
401 urb->error_count = 0;
402 urb->complete = audio_in_callback;
403 }
404
405 return 0;
406}