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