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