OSDN Git Service

ALSA: line6: Fix racy loopback handling
[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         must be called in line6pcm->in.lock context
24 */
25 static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm)
26 {
27         int index;
28         int i, urb_size;
29         int ret;
30         struct urb *urb_in;
31
32         index =
33             find_first_zero_bit(&line6pcm->in.active_urbs, LINE6_ISO_BUFFERS);
34
35         if (index < 0 || index >= LINE6_ISO_BUFFERS) {
36                 dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
37                 return -EINVAL;
38         }
39
40         urb_in = line6pcm->in.urbs[index];
41         urb_size = 0;
42
43         for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
44                 struct usb_iso_packet_descriptor *fin =
45                     &urb_in->iso_frame_desc[i];
46                 fin->offset = urb_size;
47                 fin->length = line6pcm->max_packet_size;
48                 urb_size += line6pcm->max_packet_size;
49         }
50
51         urb_in->transfer_buffer =
52             line6pcm->in.buffer +
53             index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
54         urb_in->transfer_buffer_length = urb_size;
55         urb_in->context = line6pcm;
56
57         ret = usb_submit_urb(urb_in, GFP_ATOMIC);
58
59         if (ret == 0)
60                 set_bit(index, &line6pcm->in.active_urbs);
61         else
62                 dev_err(line6pcm->line6->ifcdev,
63                         "URB in #%d submission failed (%d)\n", index, ret);
64
65         return 0;
66 }
67
68 /*
69         Submit all currently available capture URBs.
70 */
71 int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm)
72 {
73         unsigned long flags;
74         int ret = 0, i;
75
76         spin_lock_irqsave(&line6pcm->in.lock, flags);
77         for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
78                 ret = submit_audio_in_urb(line6pcm);
79                 if (ret < 0)
80                         break;
81         }
82
83         spin_unlock_irqrestore(&line6pcm->in.lock, flags);
84         return ret;
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                 spin_unlock(&line6pcm->in.lock);
141                 snd_pcm_period_elapsed(substream);
142                 spin_lock(&line6pcm->in.lock);
143         }
144 }
145
146 /*
147  * Callback for completed capture URB.
148  */
149 static void audio_in_callback(struct urb *urb)
150 {
151         int i, index, length = 0, shutdown = 0;
152         unsigned long flags;
153
154         struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
155
156         line6pcm->in.last_frame = urb->start_frame;
157
158         /* find index of URB */
159         for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
160                 if (urb == line6pcm->in.urbs[index])
161                         break;
162
163         spin_lock_irqsave(&line6pcm->in.lock, flags);
164
165         for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
166                 char *fbuf;
167                 int fsize;
168                 struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
169
170                 if (fin->status == -EXDEV) {
171                         shutdown = 1;
172                         break;
173                 }
174
175                 fbuf = urb->transfer_buffer + fin->offset;
176                 fsize = fin->actual_length;
177
178                 if (fsize > line6pcm->max_packet_size) {
179                         dev_err(line6pcm->line6->ifcdev,
180                                 "driver and/or device bug: packet too large (%d > %d)\n",
181                                 fsize, line6pcm->max_packet_size);
182                 }
183
184                 length += fsize;
185
186                 /* the following assumes LINE6_ISO_PACKETS == 1: */
187                 line6pcm->prev_fbuf = fbuf;
188                 line6pcm->prev_fsize = fsize;
189
190                 if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
191                         if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
192                                      &line6pcm->flags) && (fsize > 0))
193                                 line6_capture_copy(line6pcm, fbuf, fsize);
194         }
195
196         clear_bit(index, &line6pcm->in.active_urbs);
197
198         if (test_and_clear_bit(index, &line6pcm->in.unlink_urbs))
199                 shutdown = 1;
200
201         if (!shutdown) {
202                 submit_audio_in_urb(line6pcm);
203
204                 if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
205                         if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
206                                      &line6pcm->flags))
207                                 line6_capture_check_period(line6pcm, length);
208         }
209
210         spin_unlock_irqrestore(&line6pcm->in.lock, flags);
211 }
212
213 /* open capture callback */
214 static int snd_line6_capture_open(struct snd_pcm_substream *substream)
215 {
216         int err;
217         struct snd_pcm_runtime *runtime = substream->runtime;
218         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
219
220         err = snd_pcm_hw_constraint_ratdens(runtime, 0,
221                                             SNDRV_PCM_HW_PARAM_RATE,
222                                             (&line6pcm->
223                                              properties->snd_line6_rates));
224         if (err < 0)
225                 return err;
226
227         runtime->hw = line6pcm->properties->snd_line6_capture_hw;
228         return 0;
229 }
230
231 /* close capture callback */
232 static int snd_line6_capture_close(struct snd_pcm_substream *substream)
233 {
234         return 0;
235 }
236
237 /* hw_params capture callback */
238 static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream,
239                                        struct snd_pcm_hw_params *hw_params)
240 {
241         int ret;
242         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
243
244         ret = line6_pcm_acquire(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
245
246         if (ret < 0)
247                 return ret;
248
249         ret = snd_pcm_lib_malloc_pages(substream,
250                                        params_buffer_bytes(hw_params));
251         if (ret < 0) {
252                 line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
253                 return ret;
254         }
255
256         line6pcm->in.period = params_period_bytes(hw_params);
257         return 0;
258 }
259
260 /* hw_free capture callback */
261 static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
262 {
263         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
264
265         line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
266         return snd_pcm_lib_free_pages(substream);
267 }
268
269 /* trigger callback */
270 int snd_line6_capture_trigger(struct snd_line6_pcm *line6pcm, int cmd)
271 {
272         int err;
273
274         switch (cmd) {
275         case SNDRV_PCM_TRIGGER_START:
276         case SNDRV_PCM_TRIGGER_RESUME:
277                 err = line6_pcm_acquire(line6pcm,
278                                         LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
279
280                 if (err < 0)
281                         return err;
282
283                 break;
284
285         case SNDRV_PCM_TRIGGER_STOP:
286         case SNDRV_PCM_TRIGGER_SUSPEND:
287                 err = line6_pcm_release(line6pcm,
288                                         LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
289
290                 if (err < 0)
291                         return err;
292
293                 break;
294
295         default:
296                 return -EINVAL;
297         }
298
299         return 0;
300 }
301
302 /* capture pointer callback */
303 static snd_pcm_uframes_t
304 snd_line6_capture_pointer(struct snd_pcm_substream *substream)
305 {
306         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
307
308         return line6pcm->in.pos_done;
309 }
310
311 /* capture operators */
312 struct snd_pcm_ops snd_line6_capture_ops = {
313         .open = snd_line6_capture_open,
314         .close = snd_line6_capture_close,
315         .ioctl = snd_pcm_lib_ioctl,
316         .hw_params = snd_line6_capture_hw_params,
317         .hw_free = snd_line6_capture_hw_free,
318         .prepare = snd_line6_prepare,
319         .trigger = snd_line6_trigger,
320         .pointer = snd_line6_capture_pointer,
321 };
322
323 int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
324 {
325         struct usb_line6 *line6 = line6pcm->line6;
326         int i;
327
328         /* create audio URBs and fill in constant values: */
329         for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
330                 struct urb *urb;
331
332                 /* URB for audio in: */
333                 urb = line6pcm->in.urbs[i] =
334                     usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
335
336                 if (urb == NULL)
337                         return -ENOMEM;
338
339                 urb->dev = line6->usbdev;
340                 urb->pipe =
341                     usb_rcvisocpipe(line6->usbdev,
342                                     line6->properties->ep_audio_r &
343                                     USB_ENDPOINT_NUMBER_MASK);
344                 urb->transfer_flags = URB_ISO_ASAP;
345                 urb->start_frame = -1;
346                 urb->number_of_packets = LINE6_ISO_PACKETS;
347                 urb->interval = LINE6_ISO_INTERVAL;
348                 urb->error_count = 0;
349                 urb->complete = audio_in_callback;
350         }
351
352         return 0;
353 }