add hlist_bl_lock/unlock helpers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / line6 / capture.c
blob9647154a4923157827210aa37b8f90ae8b2a14eb
1 /*
2 * Line6 Linux USB driver - 0.9.1beta
4 * Copyright (C) 2004-2010 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 <sound/core.h>
13 #include <sound/pcm.h>
14 #include <sound/pcm_params.h>
16 #include "audio.h"
17 #include "capture.h"
18 #include "driver.h"
19 #include "pcm.h"
20 #include "pod.h"
23 Find a free URB and submit it.
25 static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm)
27 int index;
28 unsigned long flags;
29 int i, urb_size;
30 int ret;
31 struct urb *urb_in;
33 spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
34 index =
35 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(line6pcm->line6->ifcdev, "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 =
48 &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;
54 urb_in->transfer_buffer =
55 line6pcm->buffer_in +
56 index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
57 urb_in->transfer_buffer_length = urb_size;
58 urb_in->context = line6pcm;
60 ret = usb_submit_urb(urb_in, GFP_ATOMIC);
62 if (ret == 0)
63 set_bit(index, &line6pcm->active_urb_in);
64 else
65 dev_err(line6pcm->line6->ifcdev,
66 "URB in #%d submission failed (%d)\n", index, ret);
68 spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
69 return 0;
73 Submit all currently available capture URBs.
75 int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm)
77 int ret, i;
79 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
80 ret = submit_audio_in_urb(line6pcm);
81 if (ret < 0)
82 return ret;
85 return 0;
89 Unlink all currently active capture URBs.
91 void line6_unlink_audio_in_urbs(struct snd_line6_pcm *line6pcm)
93 unsigned int i;
95 for (i = LINE6_ISO_BUFFERS; i--;) {
96 if (test_bit(i, &line6pcm->active_urb_in)) {
97 if (!test_and_set_bit(i, &line6pcm->unlink_urb_in)) {
98 struct urb *u = line6pcm->urb_audio_in[i];
99 usb_unlink_urb(u);
106 Wait until unlinking of all currently active capture URBs has been
107 finished.
109 static void wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
111 int timeout = HZ;
112 unsigned int i;
113 int alive;
115 do {
116 alive = 0;
117 for (i = LINE6_ISO_BUFFERS; i--;) {
118 if (test_bit(i, &line6pcm->active_urb_in))
119 alive++;
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);
131 Unlink all currently active capture URBs, and wait for finishing.
133 void line6_unlink_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
135 line6_unlink_audio_in_urbs(line6pcm);
136 wait_clear_audio_in_urbs(line6pcm);
140 Copy data into ALSA capture buffer.
142 void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize)
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;
150 if (runtime == NULL)
151 return;
153 if (line6pcm->pos_in_done + frames > runtime->buffer_size) {
155 The transferred area goes over buffer boundary,
156 copy two separate chunks.
158 int len;
159 len = runtime->buffer_size - line6pcm->pos_in_done;
161 if (len > 0) {
162 memcpy(runtime->dma_area +
163 line6pcm->pos_in_done * bytes_per_frame, fbuf,
164 len * bytes_per_frame);
165 memcpy(runtime->dma_area, fbuf + len * bytes_per_frame,
166 (frames - len) * bytes_per_frame);
167 } else {
168 /* this is somewhat paranoid */
169 dev_err(line6pcm->line6->ifcdev,
170 "driver bug: len = %d\n", len);
172 } else {
173 /* copy single chunk */
174 memcpy(runtime->dma_area +
175 line6pcm->pos_in_done * bytes_per_frame, fbuf, fsize);
178 line6pcm->pos_in_done += frames;
179 if (line6pcm->pos_in_done >= runtime->buffer_size)
180 line6pcm->pos_in_done -= runtime->buffer_size;
183 void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length)
185 struct snd_pcm_substream *substream =
186 get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
188 line6pcm->bytes_in += length;
189 if (line6pcm->bytes_in >= line6pcm->period_in) {
190 line6pcm->bytes_in %= line6pcm->period_in;
191 snd_pcm_period_elapsed(substream);
196 * Callback for completed capture URB.
198 static void audio_in_callback(struct urb *urb)
200 int i, index, length = 0, shutdown = 0;
201 unsigned long flags;
203 struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
205 line6pcm->last_frame_in = urb->start_frame;
207 /* find index of URB */
208 for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
209 if (urb == line6pcm->urb_audio_in[index])
210 break;
212 #ifdef CONFIG_LINE6_USB_DUMP_PCM
213 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
214 struct usb_iso_packet_descriptor *fout =
215 &urb->iso_frame_desc[i];
216 line6_write_hexdump(line6pcm->line6, 'C',
217 urb->transfer_buffer + fout->offset,
218 fout->length);
220 #endif
222 spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
224 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
225 char *fbuf;
226 int fsize;
227 struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
229 if (fin->status == -EXDEV) {
230 shutdown = 1;
231 break;
234 fbuf = urb->transfer_buffer + fin->offset;
235 fsize = fin->actual_length;
237 if (fsize > line6pcm->max_packet_size) {
238 dev_err(line6pcm->line6->ifcdev,
239 "driver and/or device bug: packet too large (%d > %d)\n",
240 fsize, line6pcm->max_packet_size);
243 length += fsize;
245 /* the following assumes LINE6_ISO_PACKETS == 1: */
246 #if LINE6_BACKUP_MONITOR_SIGNAL
247 memcpy(line6pcm->prev_fbuf, fbuf, fsize);
248 #else
249 line6pcm->prev_fbuf = fbuf;
250 #endif
251 line6pcm->prev_fsize = fsize;
253 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
254 if (!(line6pcm->flags & MASK_PCM_IMPULSE))
255 #endif
256 if (test_bit(BIT_PCM_ALSA_CAPTURE, &line6pcm->flags)
257 && (fsize > 0))
258 line6_capture_copy(line6pcm, fbuf, fsize);
261 clear_bit(index, &line6pcm->active_urb_in);
263 if (test_and_clear_bit(index, &line6pcm->unlink_urb_in))
264 shutdown = 1;
266 spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
268 if (!shutdown) {
269 submit_audio_in_urb(line6pcm);
271 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
272 if (!(line6pcm->flags & MASK_PCM_IMPULSE))
273 #endif
274 if (test_bit(BIT_PCM_ALSA_CAPTURE, &line6pcm->flags))
275 line6_capture_check_period(line6pcm, length);
279 /* open capture callback */
280 static int snd_line6_capture_open(struct snd_pcm_substream *substream)
282 int err;
283 struct snd_pcm_runtime *runtime = substream->runtime;
284 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
286 err = snd_pcm_hw_constraint_ratdens(runtime, 0,
287 SNDRV_PCM_HW_PARAM_RATE,
288 (&line6pcm->
289 properties->snd_line6_rates));
290 if (err < 0)
291 return err;
293 runtime->hw = line6pcm->properties->snd_line6_capture_hw;
294 return 0;
297 /* close capture callback */
298 static int snd_line6_capture_close(struct snd_pcm_substream *substream)
300 return 0;
303 /* hw_params capture callback */
304 static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream,
305 struct snd_pcm_hw_params *hw_params)
307 int ret;
308 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
310 /* -- Florian Demski [FD] */
311 /* don't ask me why, but this fixes the bug on my machine */
312 if (line6pcm == NULL) {
313 if (substream->pcm == NULL)
314 return -ENOMEM;
315 if (substream->pcm->private_data == NULL)
316 return -ENOMEM;
317 substream->private_data = substream->pcm->private_data;
318 line6pcm = snd_pcm_substream_chip(substream);
320 /* -- [FD] end */
322 ret = snd_pcm_lib_malloc_pages(substream,
323 params_buffer_bytes(hw_params));
324 if (ret < 0)
325 return ret;
327 line6pcm->period_in = params_period_bytes(hw_params);
328 return 0;
331 /* hw_free capture callback */
332 static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
334 return snd_pcm_lib_free_pages(substream);
337 /* trigger callback */
338 int snd_line6_capture_trigger(struct snd_line6_pcm *line6pcm, int cmd)
340 int err;
342 switch (cmd) {
343 case SNDRV_PCM_TRIGGER_START:
344 #ifdef CONFIG_PM
345 case SNDRV_PCM_TRIGGER_RESUME:
346 #endif
347 err = line6_pcm_start(line6pcm, MASK_PCM_ALSA_CAPTURE);
349 if (err < 0)
350 return err;
352 break;
354 case SNDRV_PCM_TRIGGER_STOP:
355 #ifdef CONFIG_PM
356 case SNDRV_PCM_TRIGGER_SUSPEND:
357 #endif
358 err = line6_pcm_stop(line6pcm, MASK_PCM_ALSA_CAPTURE);
360 if (err < 0)
361 return err;
363 break;
365 default:
366 return -EINVAL;
369 return 0;
372 /* capture pointer callback */
373 static snd_pcm_uframes_t
374 snd_line6_capture_pointer(struct snd_pcm_substream *substream)
376 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
377 return line6pcm->pos_in_done;
380 /* capture operators */
381 struct snd_pcm_ops snd_line6_capture_ops = {
382 .open = snd_line6_capture_open,
383 .close = snd_line6_capture_close,
384 .ioctl = snd_pcm_lib_ioctl,
385 .hw_params = snd_line6_capture_hw_params,
386 .hw_free = snd_line6_capture_hw_free,
387 .prepare = snd_line6_prepare,
388 .trigger = snd_line6_trigger,
389 .pointer = snd_line6_capture_pointer,
392 int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
394 int i;
396 /* create audio URBs and fill in constant values: */
397 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
398 struct urb *urb;
400 /* URB for audio in: */
401 urb = line6pcm->urb_audio_in[i] =
402 usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
404 if (urb == NULL) {
405 dev_err(line6pcm->line6->ifcdev, "Out of memory\n");
406 return -ENOMEM;
409 urb->dev = line6pcm->line6->usbdev;
410 urb->pipe =
411 usb_rcvisocpipe(line6pcm->line6->usbdev,
412 line6pcm->ep_audio_read &
413 USB_ENDPOINT_NUMBER_MASK);
414 urb->transfer_flags = URB_ISO_ASAP;
415 urb->start_frame = -1;
416 urb->number_of_packets = LINE6_ISO_PACKETS;
417 urb->interval = LINE6_ISO_INTERVAL;
418 urb->error_count = 0;
419 urb->complete = audio_in_callback;
422 return 0;