Staging: line6: remove KERNEL_VERSION checks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / line6 / capture.c
blob6660c0b4cc5c094e966cac3bed29232e1447ba1d
1 /*
2 * Line6 Linux USB driver - 0.8.0
4 * Copyright (C) 2004-2009 Markus Grabner (grabner@icg.tugraz.at)
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.
12 #include "driver.h"
14 #include <sound/core.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
18 #include "audio.h"
19 #include "pcm.h"
20 #include "pod.h"
24 Find a free URB and submit it.
26 static int submit_audio_in_urb(struct snd_pcm_substream *substream)
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;
34 spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
35 index = find_first_zero_bit(&line6pcm->active_urb_in, LINE6_ISO_BUFFERS);
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;
43 urb_in = line6pcm->urb_audio_in[index];
44 urb_size = 0;
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;
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;
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);
62 spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
63 return 0;
67 Submit all currently available capture URBs.
69 static int submit_audio_in_all_urbs(struct snd_pcm_substream *substream)
71 int ret, i;
73 for(i = 0; i < LINE6_ISO_BUFFERS; ++i)
74 if((ret = submit_audio_in_urb(substream)) < 0)
75 return ret;
77 return 0;
81 Unlink all currently active capture URBs.
83 static void unlink_audio_in_urbs(struct snd_line6_pcm *line6pcm)
85 unsigned int i;
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 usb_unlink_urb(u);
98 Wait until unlinking of all currently active capture URBs has been finished.
100 static void wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
102 int timeout = HZ;
103 unsigned int i;
104 int alive;
106 do {
107 alive = 0;
108 for (i = LINE6_ISO_BUFFERS; i--;) {
109 if (test_bit(i, &line6pcm->active_urb_in))
110 alive++;
112 if (! alive)
113 break;
114 set_current_state(TASK_UNINTERRUPTIBLE);
115 schedule_timeout(1);
116 } while (--timeout > 0);
117 if (alive)
118 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
120 line6pcm->active_urb_in = 0;
121 line6pcm->unlink_urb_in = 0;
125 Unlink all currently active capture URBs, and wait for finishing.
127 void unlink_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
129 unlink_audio_in_urbs(line6pcm);
130 wait_clear_audio_in_urbs(line6pcm);
134 Callback for completed capture URB.
136 static void audio_in_callback(struct urb *urb)
138 int i, index, length = 0, shutdown = 0;
139 int frames;
140 unsigned long flags;
142 struct snd_pcm_substream *substream = (struct snd_pcm_substream *)urb->context;
143 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
144 const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
145 struct snd_pcm_runtime *runtime = substream->runtime;
147 /* find index of URB */
148 for(index = 0; index < LINE6_ISO_BUFFERS; ++index)
149 if(urb == line6pcm->urb_audio_in[index])
150 break;
152 #if DO_DUMP_PCM_RECEIVE
153 for(i = 0; i < LINE6_ISO_PACKETS; ++i) {
154 struct usb_iso_packet_descriptor *fout = &urb->iso_frame_desc[i];
155 line6_write_hexdump(line6pcm->line6, 'C', urb->transfer_buffer + fout->offset, fout->length);
157 #endif
159 spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
161 for(i = 0; i < LINE6_ISO_PACKETS; ++i) {
162 char *fbuf;
163 int fsize;
164 struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
166 if(fin->status == -18) {
167 shutdown = 1;
168 break;
171 fbuf = urb->transfer_buffer + fin->offset;
172 fsize = fin->actual_length;
173 length += fsize;
175 if(fsize > 0) {
176 frames = fsize / bytes_per_frame;
178 if(line6pcm->pos_in_done + frames > runtime->buffer_size) {
180 The transferred area goes over buffer boundary,
181 copy two separate chunks.
183 int len;
184 len = runtime->buffer_size - line6pcm->pos_in_done;
186 if(len > 0) {
187 memcpy(runtime->dma_area + line6pcm->pos_in_done * bytes_per_frame, fbuf, len * bytes_per_frame);
188 memcpy(runtime->dma_area, fbuf + len * bytes_per_frame, (frames - len) * bytes_per_frame);
190 else
191 dev_err(s2m(substream), "driver bug: len = %d\n", len); /* this is somewhat paranoid */
193 else {
194 /* copy single chunk */
195 memcpy(runtime->dma_area + line6pcm->pos_in_done * bytes_per_frame, fbuf, fsize * bytes_per_frame);
198 if((line6pcm->pos_in_done += frames) >= runtime->buffer_size)
199 line6pcm->pos_in_done -= runtime->buffer_size;
203 clear_bit(index, &line6pcm->active_urb_in);
205 if(test_bit(index, &line6pcm->unlink_urb_in))
206 shutdown = 1;
208 spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
210 if(!shutdown) {
211 submit_audio_in_urb(substream);
213 if((line6pcm->bytes_in += length) >= line6pcm->period_in) {
214 line6pcm->bytes_in -= line6pcm->period_in;
215 snd_pcm_period_elapsed(substream);
220 /* open capture callback */
221 static int snd_line6_capture_open(struct snd_pcm_substream *substream)
223 int err;
224 struct snd_pcm_runtime *runtime = substream->runtime;
225 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
227 if((err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
228 (&line6pcm->properties->snd_line6_rates))) < 0)
229 return err;
231 runtime->hw = line6pcm->properties->snd_line6_capture_hw;
232 return 0;
235 /* close capture callback */
236 static int snd_line6_capture_close(struct snd_pcm_substream *substream)
238 return 0;
241 /* hw_params capture callback */
242 static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params)
244 int ret;
245 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
247 /* -- Florian Demski [FD] */
248 /* don't ask me why, but this fixes the bug on my machine */
249 if ( line6pcm == NULL ) {
250 if ( substream->pcm == NULL )
251 return -ENOMEM;
252 if ( substream->pcm->private_data == NULL )
253 return -ENOMEM;
254 substream->private_data = substream->pcm->private_data;
255 line6pcm = snd_pcm_substream_chip( substream );
257 /* -- [FD] end */
259 if((ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
260 return ret;
262 line6pcm->period_in = params_period_bytes(hw_params);
263 line6pcm->buffer_in = kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS * LINE6_ISO_PACKET_SIZE_MAX, GFP_KERNEL);
265 if(!line6pcm->buffer_in) {
266 dev_err(s2m(substream), "cannot malloc buffer_in\n");
267 return -ENOMEM;
270 return 0;
273 /* hw_free capture callback */
274 static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
276 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
277 unlink_wait_clear_audio_in_urbs(line6pcm);
279 if(line6pcm->buffer_in) {
280 kfree(line6pcm->buffer_in);
281 line6pcm->buffer_in = 0;
284 return snd_pcm_lib_free_pages(substream);
287 /* trigger callback */
288 int snd_line6_capture_trigger(struct snd_pcm_substream *substream, int cmd)
290 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
291 int err;
292 line6pcm->count_in = 0;
294 switch(cmd) {
295 case SNDRV_PCM_TRIGGER_START:
296 if(!test_and_set_bit(BIT_RUNNING_CAPTURE, &line6pcm->flags)) {
297 err = submit_audio_in_all_urbs(substream);
299 if(err < 0) {
300 clear_bit(BIT_RUNNING_CAPTURE, &line6pcm->flags);
301 return err;
305 break;
307 case SNDRV_PCM_TRIGGER_STOP:
308 if(test_and_clear_bit(BIT_RUNNING_CAPTURE, &line6pcm->flags))
309 unlink_audio_in_urbs(line6pcm);
311 break;
313 default:
314 return -EINVAL;
317 return 0;
320 /* capture pointer callback */
321 static snd_pcm_uframes_t
322 snd_line6_capture_pointer(struct snd_pcm_substream *substream)
324 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
325 return line6pcm->pos_in_done;
328 /* capture operators */
329 struct snd_pcm_ops snd_line6_capture_ops = {
330 .open = snd_line6_capture_open,
331 .close = snd_line6_capture_close,
332 .ioctl = snd_pcm_lib_ioctl,
333 .hw_params = snd_line6_capture_hw_params,
334 .hw_free = snd_line6_capture_hw_free,
335 .prepare = snd_line6_prepare,
336 .trigger = snd_line6_trigger,
337 .pointer = snd_line6_capture_pointer,
340 int create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
342 int i;
344 /* create audio URBs and fill in constant values: */
345 for(i = 0; i < LINE6_ISO_BUFFERS; ++i) {
346 struct urb *urb;
348 /* URB for audio in: */
349 urb = line6pcm->urb_audio_in[i] = usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
351 if(urb == NULL) {
352 dev_err(line6pcm->line6->ifcdev, "Out of memory\n");
353 return -ENOMEM;
356 urb->dev = line6pcm->line6->usbdev;
357 urb->pipe = usb_rcvisocpipe(line6pcm->line6->usbdev, line6pcm->ep_audio_read & USB_ENDPOINT_NUMBER_MASK);
358 urb->transfer_flags = URB_ISO_ASAP;
359 urb->start_frame = -1;
360 urb->number_of_packets = LINE6_ISO_PACKETS;
361 urb->interval = LINE6_ISO_INTERVAL;
362 urb->error_count = 0;
363 urb->complete = audio_in_callback;
366 return 0;