OSDN Git Service

ALSA: line6: Consolidate URB unlink and sync helpers
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / sound / usb / line6 / capture.c
1 /*
2  * Line 6 Linux USB driver
3  *
4  * Copyright (C) 2004-2010 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 <linux/slab.h>
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
16
17 #include "capture.h"
18 #include "driver.h"
19 #include "pcm.h"
20
21 /*
22         Find a free URB and submit it.
23 */
24 static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm)
25 {
26         int index;
27         unsigned long flags;
28         int i, urb_size;
29         int ret;
30         struct urb *urb_in;
31
32         spin_lock_irqsave(&line6pcm->in.lock, flags);
33         index =
34             find_first_zero_bit(&line6pcm->in.active_urbs, LINE6_ISO_BUFFERS);
35
36         if (index < 0 || index >= LINE6_ISO_BUFFERS) {
37                 spin_unlock_irqrestore(&line6pcm->in.lock, flags);
38                 dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
39                 return -EINVAL;
40         }
41
42         urb_in = line6pcm->in.urbs[index];
43         urb_size = 0;
44
45         for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
46                 struct usb_iso_packet_descriptor *fin =
47                     &urb_in->iso_frame_desc[i];
48                 fin->offset = urb_size;
49                 fin->length = line6pcm->max_packet_size;
50                 urb_size += line6pcm->max_packet_size;
51         }
52
53         urb_in->transfer_buffer =
54             line6pcm->in.buffer +
55             index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
56         urb_in->transfer_buffer_length = urb_size;
57         urb_in->context = line6pcm;
58
59         ret = usb_submit_urb(urb_in, GFP_ATOMIC);
60
61         if (ret == 0)
62                 set_bit(index, &line6pcm->in.active_urbs);
63         else
64                 dev_err(line6pcm->line6->ifcdev,
65                         "URB in #%d submission failed (%d)\n", index, ret);
66
67         spin_unlock_irqrestore(&line6pcm->in.lock, flags);
68         return 0;
69 }
70
71 /*
72         Submit all currently available capture URBs.
73 */
74 int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm)
75 {
76         int ret, i;
77
78         for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
79                 ret = submit_audio_in_urb(line6pcm);
80                 if (ret < 0)
81                         return ret;
82         }
83
84         return 0;
85 }
86
87 /*
88         Copy data into ALSA capture buffer.
89 */
90 void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize)
91 {
92         struct snd_pcm_substream *substream =
93             get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
94         struct snd_pcm_runtime *runtime = substream->runtime;
95         const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
96         int frames = fsize / bytes_per_frame;
97
98         if (runtime == NULL)
99                 return;
100
101         if (line6pcm->in.pos_done + frames > runtime->buffer_size) {
102                 /*
103                    The transferred area goes over buffer boundary,
104                    copy two separate chunks.
105                  */
106                 int len;
107
108                 len = runtime->buffer_size - line6pcm->in.pos_done;
109
110                 if (len > 0) {
111                         memcpy(runtime->dma_area +
112                                line6pcm->in.pos_done * bytes_per_frame, fbuf,
113                                len * bytes_per_frame);
114                         memcpy(runtime->dma_area, fbuf + len * bytes_per_frame,
115                                (frames - len) * bytes_per_frame);
116                 } else {
117                         /* this is somewhat paranoid */
118                         dev_err(line6pcm->line6->ifcdev,
119                                 "driver bug: len = %d\n", len);
120                 }
121         } else {
122                 /* copy single chunk */
123                 memcpy(runtime->dma_area +
124                        line6pcm->in.pos_done * bytes_per_frame, fbuf, fsize);
125         }
126
127         line6pcm->in.pos_done += frames;
128         if (line6pcm->in.pos_done >= runtime->buffer_size)
129                 line6pcm->in.pos_done -= runtime->buffer_size;
130 }
131
132 void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length)
133 {
134         struct snd_pcm_substream *substream =
135             get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
136
137         line6pcm->in.bytes += length;
138         if (line6pcm->in.bytes >= line6pcm->in.period) {
139                 line6pcm->in.bytes %= line6pcm->in.period;
140                 snd_pcm_period_elapsed(substream);
141         }
142 }
143
144 void line6_free_capture_buffer(struct snd_line6_pcm *line6pcm)
145 {
146         kfree(line6pcm->in.buffer);
147         line6pcm->in.buffer = NULL;
148 }
149
150 /*
151  * Callback for completed capture URB.
152  */
153 static void audio_in_callback(struct urb *urb)
154 {
155         int i, index, length = 0, shutdown = 0;
156         unsigned long flags;
157
158         struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
159
160         line6pcm->in.last_frame = urb->start_frame;
161
162         /* find index of URB */
163         for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
164                 if (urb == line6pcm->in.urbs[index])
165                         break;
166
167         spin_lock_irqsave(&line6pcm->in.lock, flags);
168
169         for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
170                 char *fbuf;
171                 int fsize;
172                 struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
173
174                 if (fin->status == -EXDEV) {
175                         shutdown = 1;
176                         break;
177                 }
178
179                 fbuf = urb->transfer_buffer + fin->offset;
180                 fsize = fin->actual_length;
181
182                 if (fsize > line6pcm->max_packet_size) {
183                         dev_err(line6pcm->line6->ifcdev,
184                                 "driver and/or device bug: packet too large (%d > %d)\n",
185                                 fsize, line6pcm->max_packet_size);
186                 }
187
188                 length += fsize;
189
190                 /* the following assumes LINE6_ISO_PACKETS == 1: */
191                 line6pcm->prev_fbuf = fbuf;
192                 line6pcm->prev_fsize = fsize;
193
194                 if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
195                         if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
196                                      &line6pcm->flags) && (fsize > 0))
197                                 line6_capture_copy(line6pcm, fbuf, fsize);
198         }
199
200         clear_bit(index, &line6pcm->in.active_urbs);
201
202         if (test_and_clear_bit(index, &line6pcm->in.unlink_urbs))
203                 shutdown = 1;
204
205         spin_unlock_irqrestore(&line6pcm->in.lock, flags);
206
207         if (!shutdown) {
208                 submit_audio_in_urb(line6pcm);
209
210                 if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
211                         if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
212                                      &line6pcm->flags))
213                                 line6_capture_check_period(line6pcm, length);
214         }
215 }
216
217 /* open capture callback */
218 static int snd_line6_capture_open(struct snd_pcm_substream *substream)
219 {
220         int err;
221         struct snd_pcm_runtime *runtime = substream->runtime;
222         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
223
224         err = snd_pcm_hw_constraint_ratdens(runtime, 0,
225                                             SNDRV_PCM_HW_PARAM_RATE,
226                                             (&line6pcm->
227                                              properties->snd_line6_rates));
228         if (err < 0)
229                 return err;
230
231         runtime->hw = line6pcm->properties->snd_line6_capture_hw;
232         return 0;
233 }
234
235 /* close capture callback */
236 static int snd_line6_capture_close(struct snd_pcm_substream *substream)
237 {
238         return 0;
239 }
240
241 /* hw_params capture callback */
242 static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream,
243                                        struct snd_pcm_hw_params *hw_params)
244 {
245         int ret;
246         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
247
248         ret = line6_pcm_acquire(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
249
250         if (ret < 0)
251                 return ret;
252
253         ret = snd_pcm_lib_malloc_pages(substream,
254                                        params_buffer_bytes(hw_params));
255         if (ret < 0) {
256                 line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
257                 return ret;
258         }
259
260         line6pcm->in.period = params_period_bytes(hw_params);
261         return 0;
262 }
263
264 /* hw_free capture callback */
265 static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
266 {
267         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
268
269         line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
270         return snd_pcm_lib_free_pages(substream);
271 }
272
273 /* trigger callback */
274 int snd_line6_capture_trigger(struct snd_line6_pcm *line6pcm, int cmd)
275 {
276         int err;
277
278         switch (cmd) {
279         case SNDRV_PCM_TRIGGER_START:
280         case SNDRV_PCM_TRIGGER_RESUME:
281                 err = line6_pcm_acquire(line6pcm,
282                                         LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
283
284                 if (err < 0)
285                         return err;
286
287                 break;
288
289         case SNDRV_PCM_TRIGGER_STOP:
290         case SNDRV_PCM_TRIGGER_SUSPEND:
291                 err = line6_pcm_release(line6pcm,
292                                         LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
293
294                 if (err < 0)
295                         return err;
296
297                 break;
298
299         default:
300                 return -EINVAL;
301         }
302
303         return 0;
304 }
305
306 /* capture pointer callback */
307 static snd_pcm_uframes_t
308 snd_line6_capture_pointer(struct snd_pcm_substream *substream)
309 {
310         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
311
312         return line6pcm->in.pos_done;
313 }
314
315 /* capture operators */
316 struct snd_pcm_ops snd_line6_capture_ops = {
317         .open = snd_line6_capture_open,
318         .close = snd_line6_capture_close,
319         .ioctl = snd_pcm_lib_ioctl,
320         .hw_params = snd_line6_capture_hw_params,
321         .hw_free = snd_line6_capture_hw_free,
322         .prepare = snd_line6_prepare,
323         .trigger = snd_line6_trigger,
324         .pointer = snd_line6_capture_pointer,
325 };
326
327 int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
328 {
329         struct usb_line6 *line6 = line6pcm->line6;
330         int i;
331
332         /* create audio URBs and fill in constant values: */
333         for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
334                 struct urb *urb;
335
336                 /* URB for audio in: */
337                 urb = line6pcm->in.urbs[i] =
338                     usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
339
340                 if (urb == NULL)
341                         return -ENOMEM;
342
343                 urb->dev = line6->usbdev;
344                 urb->pipe =
345                     usb_rcvisocpipe(line6->usbdev,
346                                     line6->properties->ep_audio_r &
347                                     USB_ENDPOINT_NUMBER_MASK);
348                 urb->transfer_flags = URB_ISO_ASAP;
349                 urb->start_frame = -1;
350                 urb->number_of_packets = LINE6_ISO_PACKETS;
351                 urb->interval = LINE6_ISO_INTERVAL;
352                 urb->error_count = 0;
353                 urb->complete = audio_in_callback;
354         }
355
356         return 0;
357 }