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 <linux/slab.h>
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
24 Find a free URB and submit it.
26 static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm)
34 spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
36 find_first_zero_bit(&line6pcm->active_urb_in, LINE6_ISO_BUFFERS);
38 if (index < 0 || index >= LINE6_ISO_BUFFERS) {
39 spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
40 dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
44 urb_in = line6pcm->urb_audio_in[index];
47 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
48 struct usb_iso_packet_descriptor *fin =
49 &urb_in->iso_frame_desc[i];
50 fin->offset = urb_size;
51 fin->length = line6pcm->max_packet_size;
52 urb_size += line6pcm->max_packet_size;
55 urb_in->transfer_buffer =
57 index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
58 urb_in->transfer_buffer_length = urb_size;
59 urb_in->context = line6pcm;
61 ret = usb_submit_urb(urb_in, GFP_ATOMIC);
64 set_bit(index, &line6pcm->active_urb_in);
66 dev_err(line6pcm->line6->ifcdev,
67 "URB in #%d submission failed (%d)\n", index, ret);
69 spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
74 Submit all currently available capture URBs.
76 int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm)
80 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
81 ret = submit_audio_in_urb(line6pcm);
90 Unlink all currently active capture URBs.
92 void line6_unlink_audio_in_urbs(struct snd_line6_pcm *line6pcm)
96 for (i = LINE6_ISO_BUFFERS; i--;) {
97 if (test_bit(i, &line6pcm->active_urb_in)) {
98 if (!test_and_set_bit(i, &line6pcm->unlink_urb_in)) {
99 struct urb *u = line6pcm->urb_audio_in[i];
107 Wait until unlinking of all currently active capture URBs has been
110 void line6_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
118 for (i = LINE6_ISO_BUFFERS; i--;) {
119 if (test_bit(i, &line6pcm->active_urb_in))
124 set_current_state(TASK_UNINTERRUPTIBLE);
126 } while (--timeout > 0);
128 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
132 Unlink all currently active capture URBs, and wait for finishing.
134 void line6_unlink_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
136 line6_unlink_audio_in_urbs(line6pcm);
137 line6_wait_clear_audio_in_urbs(line6pcm);
141 Copy data into ALSA capture buffer.
143 void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize)
145 struct snd_pcm_substream *substream =
146 get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
147 struct snd_pcm_runtime *runtime = substream->runtime;
148 const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
149 int frames = fsize / bytes_per_frame;
154 if (line6pcm->pos_in_done + frames > runtime->buffer_size) {
156 The transferred area goes over buffer boundary,
157 copy two separate chunks.
161 len = runtime->buffer_size - line6pcm->pos_in_done;
164 memcpy(runtime->dma_area +
165 line6pcm->pos_in_done * bytes_per_frame, fbuf,
166 len * bytes_per_frame);
167 memcpy(runtime->dma_area, fbuf + len * bytes_per_frame,
168 (frames - len) * bytes_per_frame);
170 /* this is somewhat paranoid */
171 dev_err(line6pcm->line6->ifcdev,
172 "driver bug: len = %d\n", len);
175 /* copy single chunk */
176 memcpy(runtime->dma_area +
177 line6pcm->pos_in_done * bytes_per_frame, fbuf, fsize);
180 line6pcm->pos_in_done += frames;
181 if (line6pcm->pos_in_done >= runtime->buffer_size)
182 line6pcm->pos_in_done -= runtime->buffer_size;
185 void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length)
187 struct snd_pcm_substream *substream =
188 get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
190 line6pcm->bytes_in += length;
191 if (line6pcm->bytes_in >= line6pcm->period_in) {
192 line6pcm->bytes_in %= line6pcm->period_in;
193 snd_pcm_period_elapsed(substream);
197 void line6_free_capture_buffer(struct snd_line6_pcm *line6pcm)
199 kfree(line6pcm->buffer_in);
200 line6pcm->buffer_in = NULL;
204 * Callback for completed capture URB.
206 static void audio_in_callback(struct urb *urb)
208 int i, index, length = 0, shutdown = 0;
211 struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
213 line6pcm->last_frame_in = urb->start_frame;
215 /* find index of URB */
216 for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
217 if (urb == line6pcm->urb_audio_in[index])
220 spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
222 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
225 struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
227 if (fin->status == -EXDEV) {
232 fbuf = urb->transfer_buffer + fin->offset;
233 fsize = fin->actual_length;
235 if (fsize > line6pcm->max_packet_size) {
236 dev_err(line6pcm->line6->ifcdev,
237 "driver and/or device bug: packet too large (%d > %d)\n",
238 fsize, line6pcm->max_packet_size);
243 /* the following assumes LINE6_ISO_PACKETS == 1: */
244 line6pcm->prev_fbuf = fbuf;
245 line6pcm->prev_fsize = fsize;
247 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
248 if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
250 if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
251 &line6pcm->flags) && (fsize > 0))
252 line6_capture_copy(line6pcm, fbuf, fsize);
255 clear_bit(index, &line6pcm->active_urb_in);
257 if (test_and_clear_bit(index, &line6pcm->unlink_urb_in))
260 spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
263 submit_audio_in_urb(line6pcm);
265 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
266 if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
268 if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
270 line6_capture_check_period(line6pcm, length);
274 /* open capture callback */
275 static int snd_line6_capture_open(struct snd_pcm_substream *substream)
278 struct snd_pcm_runtime *runtime = substream->runtime;
279 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
281 err = snd_pcm_hw_constraint_ratdens(runtime, 0,
282 SNDRV_PCM_HW_PARAM_RATE,
284 properties->snd_line6_rates));
288 runtime->hw = line6pcm->properties->snd_line6_capture_hw;
292 /* close capture callback */
293 static int snd_line6_capture_close(struct snd_pcm_substream *substream)
298 /* hw_params capture callback */
299 static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream,
300 struct snd_pcm_hw_params *hw_params)
303 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
305 /* -- Florian Demski [FD] */
306 /* don't ask me why, but this fixes the bug on my machine */
307 if (line6pcm == NULL) {
308 if (substream->pcm == NULL)
310 if (substream->pcm->private_data == NULL)
312 substream->private_data = substream->pcm->private_data;
313 line6pcm = snd_pcm_substream_chip(substream);
317 ret = line6_pcm_acquire(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
322 ret = snd_pcm_lib_malloc_pages(substream,
323 params_buffer_bytes(hw_params));
325 line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
329 line6pcm->period_in = params_period_bytes(hw_params);
333 /* hw_free capture callback */
334 static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
336 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
337 line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
338 return snd_pcm_lib_free_pages(substream);
341 /* trigger callback */
342 int snd_line6_capture_trigger(struct snd_line6_pcm *line6pcm, int cmd)
347 case SNDRV_PCM_TRIGGER_START:
349 case SNDRV_PCM_TRIGGER_RESUME:
351 err = line6_pcm_acquire(line6pcm,
352 LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
359 case SNDRV_PCM_TRIGGER_STOP:
361 case SNDRV_PCM_TRIGGER_SUSPEND:
363 err = line6_pcm_release(line6pcm,
364 LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
378 /* capture pointer callback */
379 static snd_pcm_uframes_t
380 snd_line6_capture_pointer(struct snd_pcm_substream *substream)
382 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
383 return line6pcm->pos_in_done;
386 /* capture operators */
387 struct snd_pcm_ops snd_line6_capture_ops = {
388 .open = snd_line6_capture_open,
389 .close = snd_line6_capture_close,
390 .ioctl = snd_pcm_lib_ioctl,
391 .hw_params = snd_line6_capture_hw_params,
392 .hw_free = snd_line6_capture_hw_free,
393 .prepare = snd_line6_prepare,
394 .trigger = snd_line6_trigger,
395 .pointer = snd_line6_capture_pointer,
398 int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
402 /* create audio URBs and fill in constant values: */
403 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
406 /* URB for audio in: */
407 urb = line6pcm->urb_audio_in[i] =
408 usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
411 dev_err(line6pcm->line6->ifcdev, "Out of memory\n");
415 urb->dev = line6pcm->line6->usbdev;
417 usb_rcvisocpipe(line6pcm->line6->usbdev,
418 line6pcm->ep_audio_read &
419 USB_ENDPOINT_NUMBER_MASK);
420 urb->transfer_flags = URB_ISO_ASAP;
421 urb->start_frame = -1;
422 urb->number_of_packets = LINE6_ISO_PACKETS;
423 urb->interval = LINE6_ISO_INTERVAL;
424 urb->error_count = 0;
425 urb->complete = audio_in_callback;