OSDN Git Service

439f1941eb56d98783205ef3a309eec9b335d06f
[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         Unlink all currently active capture URBs.
89 */
90 void line6_unlink_audio_in_urbs(struct snd_line6_pcm *line6pcm)
91 {
92         unsigned int i;
93
94         for (i = 0; i < LINE6_ISO_BUFFERS; i++) {
95                 if (test_bit(i, &line6pcm->in.active_urbs)) {
96                         if (!test_and_set_bit(i, &line6pcm->in.unlink_urbs)) {
97                                 struct urb *u = line6pcm->in.urbs[i];
98
99                                 usb_unlink_urb(u);
100                         }
101                 }
102         }
103 }
104
105 /*
106         Wait until unlinking of all currently active capture URBs has been
107         finished.
108 */
109 void line6_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 = 0; i < LINE6_ISO_BUFFERS; i++) {
118                         if (test_bit(i, &line6pcm->in.active_urbs))
119                                 alive++;
120                 }
121                 if (!alive)
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);
128 }
129
130 /*
131         Unlink all currently active capture URBs, and wait for finishing.
132 */
133 void line6_unlink_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
134 {
135         line6_unlink_audio_in_urbs(line6pcm);
136         line6_wait_clear_audio_in_urbs(line6pcm);
137 }
138
139 /*
140         Copy data into ALSA capture buffer.
141 */
142 void 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 (runtime == NULL)
151                 return;
152
153         if (line6pcm->in.pos_done + frames > runtime->buffer_size) {
154                 /*
155                    The transferred area goes over buffer boundary,
156                    copy two separate chunks.
157                  */
158                 int len;
159
160                 len = runtime->buffer_size - line6pcm->in.pos_done;
161
162                 if (len > 0) {
163                         memcpy(runtime->dma_area +
164                                line6pcm->in.pos_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);
168                 } else {
169                         /* this is somewhat paranoid */
170                         dev_err(line6pcm->line6->ifcdev,
171                                 "driver bug: len = %d\n", len);
172                 }
173         } else {
174                 /* copy single chunk */
175                 memcpy(runtime->dma_area +
176                        line6pcm->in.pos_done * bytes_per_frame, fbuf, fsize);
177         }
178
179         line6pcm->in.pos_done += frames;
180         if (line6pcm->in.pos_done >= runtime->buffer_size)
181                 line6pcm->in.pos_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
189         line6pcm->in.bytes += length;
190         if (line6pcm->in.bytes >= line6pcm->in.period) {
191                 line6pcm->in.bytes %= line6pcm->in.period;
192                 snd_pcm_period_elapsed(substream);
193         }
194 }
195
196 void line6_free_capture_buffer(struct snd_line6_pcm *line6pcm)
197 {
198         kfree(line6pcm->in.buffer);
199         line6pcm->in.buffer = NULL;
200 }
201
202 /*
203  * Callback for completed capture URB.
204  */
205 static void audio_in_callback(struct urb *urb)
206 {
207         int i, index, length = 0, shutdown = 0;
208         unsigned long flags;
209
210         struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
211
212         line6pcm->in.last_frame = urb->start_frame;
213
214         /* find index of URB */
215         for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
216                 if (urb == line6pcm->in.urbs[index])
217                         break;
218
219         spin_lock_irqsave(&line6pcm->in.lock, flags);
220
221         for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
222                 char *fbuf;
223                 int fsize;
224                 struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
225
226                 if (fin->status == -EXDEV) {
227                         shutdown = 1;
228                         break;
229                 }
230
231                 fbuf = urb->transfer_buffer + fin->offset;
232                 fsize = fin->actual_length;
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
240                 length += fsize;
241
242                 /* the following assumes LINE6_ISO_PACKETS == 1: */
243                 line6pcm->prev_fbuf = fbuf;
244                 line6pcm->prev_fsize = fsize;
245
246                 if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
247                         if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
248                                      &line6pcm->flags) && (fsize > 0))
249                                 line6_capture_copy(line6pcm, fbuf, fsize);
250         }
251
252         clear_bit(index, &line6pcm->in.active_urbs);
253
254         if (test_and_clear_bit(index, &line6pcm->in.unlink_urbs))
255                 shutdown = 1;
256
257         spin_unlock_irqrestore(&line6pcm->in.lock, flags);
258
259         if (!shutdown) {
260                 submit_audio_in_urb(line6pcm);
261
262                 if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
263                         if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
264                                      &line6pcm->flags))
265                                 line6_capture_check_period(line6pcm, length);
266         }
267 }
268
269 /* open capture callback */
270 static int snd_line6_capture_open(struct snd_pcm_substream *substream)
271 {
272         int err;
273         struct snd_pcm_runtime *runtime = substream->runtime;
274         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
275
276         err = snd_pcm_hw_constraint_ratdens(runtime, 0,
277                                             SNDRV_PCM_HW_PARAM_RATE,
278                                             (&line6pcm->
279                                              properties->snd_line6_rates));
280         if (err < 0)
281                 return err;
282
283         runtime->hw = line6pcm->properties->snd_line6_capture_hw;
284         return 0;
285 }
286
287 /* close capture callback */
288 static int snd_line6_capture_close(struct snd_pcm_substream *substream)
289 {
290         return 0;
291 }
292
293 /* hw_params capture callback */
294 static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream,
295                                        struct snd_pcm_hw_params *hw_params)
296 {
297         int ret;
298         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
299
300         ret = line6_pcm_acquire(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
301
302         if (ret < 0)
303                 return ret;
304
305         ret = snd_pcm_lib_malloc_pages(substream,
306                                        params_buffer_bytes(hw_params));
307         if (ret < 0) {
308                 line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
309                 return ret;
310         }
311
312         line6pcm->in.period = params_period_bytes(hw_params);
313         return 0;
314 }
315
316 /* hw_free capture callback */
317 static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
318 {
319         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
320
321         line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
322         return snd_pcm_lib_free_pages(substream);
323 }
324
325 /* trigger callback */
326 int snd_line6_capture_trigger(struct snd_line6_pcm *line6pcm, int cmd)
327 {
328         int err;
329
330         switch (cmd) {
331         case SNDRV_PCM_TRIGGER_START:
332         case SNDRV_PCM_TRIGGER_RESUME:
333                 err = line6_pcm_acquire(line6pcm,
334                                         LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
335
336                 if (err < 0)
337                         return err;
338
339                 break;
340
341         case SNDRV_PCM_TRIGGER_STOP:
342         case SNDRV_PCM_TRIGGER_SUSPEND:
343                 err = line6_pcm_release(line6pcm,
344                                         LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
345
346                 if (err < 0)
347                         return err;
348
349                 break;
350
351         default:
352                 return -EINVAL;
353         }
354
355         return 0;
356 }
357
358 /* capture pointer callback */
359 static snd_pcm_uframes_t
360 snd_line6_capture_pointer(struct snd_pcm_substream *substream)
361 {
362         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
363
364         return line6pcm->in.pos_done;
365 }
366
367 /* capture operators */
368 struct snd_pcm_ops snd_line6_capture_ops = {
369         .open = snd_line6_capture_open,
370         .close = snd_line6_capture_close,
371         .ioctl = snd_pcm_lib_ioctl,
372         .hw_params = snd_line6_capture_hw_params,
373         .hw_free = snd_line6_capture_hw_free,
374         .prepare = snd_line6_prepare,
375         .trigger = snd_line6_trigger,
376         .pointer = snd_line6_capture_pointer,
377 };
378
379 int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
380 {
381         struct usb_line6 *line6 = line6pcm->line6;
382         int i;
383
384         /* create audio URBs and fill in constant values: */
385         for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
386                 struct urb *urb;
387
388                 /* URB for audio in: */
389                 urb = line6pcm->in.urbs[i] =
390                     usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
391
392                 if (urb == NULL)
393                         return -ENOMEM;
394
395                 urb->dev = line6->usbdev;
396                 urb->pipe =
397                     usb_rcvisocpipe(line6->usbdev,
398                                     line6->properties->ep_audio_r &
399                                     USB_ENDPOINT_NUMBER_MASK);
400                 urb->transfer_flags = URB_ISO_ASAP;
401                 urb->start_frame = -1;
402                 urb->number_of_packets = LINE6_ISO_PACKETS;
403                 urb->interval = LINE6_ISO_INTERVAL;
404                 urb->error_count = 0;
405                 urb->complete = audio_in_callback;
406         }
407
408         return 0;
409 }