2 * Line 6 Linux USB driver
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>
22 Find a free URB and submit it.
23 must be called in line6pcm->in.lock context
25 static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm)
33 find_first_zero_bit(&line6pcm->in.active_urbs, LINE6_ISO_BUFFERS);
35 if (index < 0 || index >= LINE6_ISO_BUFFERS) {
36 dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
40 urb_in = line6pcm->in.urbs[index];
43 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
44 struct usb_iso_packet_descriptor *fin =
45 &urb_in->iso_frame_desc[i];
46 fin->offset = urb_size;
47 fin->length = line6pcm->max_packet_size;
48 urb_size += line6pcm->max_packet_size;
51 urb_in->transfer_buffer =
53 index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
54 urb_in->transfer_buffer_length = urb_size;
55 urb_in->context = line6pcm;
57 ret = usb_submit_urb(urb_in, GFP_ATOMIC);
60 set_bit(index, &line6pcm->in.active_urbs);
62 dev_err(line6pcm->line6->ifcdev,
63 "URB in #%d submission failed (%d)\n", index, ret);
69 Submit all currently available capture URBs.
71 int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm)
76 spin_lock_irqsave(&line6pcm->in.lock, flags);
77 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
78 ret = submit_audio_in_urb(line6pcm);
83 spin_unlock_irqrestore(&line6pcm->in.lock, flags);
88 Copy data into ALSA capture buffer.
90 void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize)
92 struct snd_pcm_substream *substream =
93 get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
94 struct snd_pcm_runtime *runtime = substream->runtime;
95 const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
96 int frames = fsize / bytes_per_frame;
101 if (line6pcm->in.pos_done + frames > runtime->buffer_size) {
103 The transferred area goes over buffer boundary,
104 copy two separate chunks.
108 len = runtime->buffer_size - line6pcm->in.pos_done;
111 memcpy(runtime->dma_area +
112 line6pcm->in.pos_done * bytes_per_frame, fbuf,
113 len * bytes_per_frame);
114 memcpy(runtime->dma_area, fbuf + len * bytes_per_frame,
115 (frames - len) * bytes_per_frame);
117 /* this is somewhat paranoid */
118 dev_err(line6pcm->line6->ifcdev,
119 "driver bug: len = %d\n", len);
122 /* copy single chunk */
123 memcpy(runtime->dma_area +
124 line6pcm->in.pos_done * bytes_per_frame, fbuf, fsize);
127 line6pcm->in.pos_done += frames;
128 if (line6pcm->in.pos_done >= runtime->buffer_size)
129 line6pcm->in.pos_done -= runtime->buffer_size;
132 void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length)
134 struct snd_pcm_substream *substream =
135 get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
137 line6pcm->in.bytes += length;
138 if (line6pcm->in.bytes >= line6pcm->in.period) {
139 line6pcm->in.bytes %= line6pcm->in.period;
140 spin_unlock(&line6pcm->in.lock);
141 snd_pcm_period_elapsed(substream);
142 spin_lock(&line6pcm->in.lock);
147 * Callback for completed capture URB.
149 static void audio_in_callback(struct urb *urb)
151 int i, index, length = 0, shutdown = 0;
154 struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
156 line6pcm->in.last_frame = urb->start_frame;
158 /* find index of URB */
159 for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
160 if (urb == line6pcm->in.urbs[index])
163 spin_lock_irqsave(&line6pcm->in.lock, flags);
165 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
168 struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
170 if (fin->status == -EXDEV) {
175 fbuf = urb->transfer_buffer + fin->offset;
176 fsize = fin->actual_length;
178 if (fsize > line6pcm->max_packet_size) {
179 dev_err(line6pcm->line6->ifcdev,
180 "driver and/or device bug: packet too large (%d > %d)\n",
181 fsize, line6pcm->max_packet_size);
186 /* the following assumes LINE6_ISO_PACKETS == 1: */
187 line6pcm->prev_fbuf = fbuf;
188 line6pcm->prev_fsize = fsize;
190 if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
191 if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
192 &line6pcm->flags) && (fsize > 0))
193 line6_capture_copy(line6pcm, fbuf, fsize);
196 clear_bit(index, &line6pcm->in.active_urbs);
198 if (test_and_clear_bit(index, &line6pcm->in.unlink_urbs))
202 submit_audio_in_urb(line6pcm);
204 if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
205 if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
207 line6_capture_check_period(line6pcm, length);
210 spin_unlock_irqrestore(&line6pcm->in.lock, flags);
213 /* open capture callback */
214 static int snd_line6_capture_open(struct snd_pcm_substream *substream)
217 struct snd_pcm_runtime *runtime = substream->runtime;
218 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
220 err = snd_pcm_hw_constraint_ratdens(runtime, 0,
221 SNDRV_PCM_HW_PARAM_RATE,
223 properties->snd_line6_rates));
227 runtime->hw = line6pcm->properties->snd_line6_capture_hw;
231 /* close capture callback */
232 static int snd_line6_capture_close(struct snd_pcm_substream *substream)
237 /* hw_params capture callback */
238 static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream,
239 struct snd_pcm_hw_params *hw_params)
242 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
244 ret = line6_pcm_acquire(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
249 ret = snd_pcm_lib_malloc_pages(substream,
250 params_buffer_bytes(hw_params));
252 line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
256 line6pcm->in.period = params_period_bytes(hw_params);
260 /* hw_free capture callback */
261 static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
263 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
265 line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
266 return snd_pcm_lib_free_pages(substream);
269 /* trigger callback */
270 int snd_line6_capture_trigger(struct snd_line6_pcm *line6pcm, int cmd)
275 case SNDRV_PCM_TRIGGER_START:
276 case SNDRV_PCM_TRIGGER_RESUME:
277 err = line6_pcm_acquire(line6pcm,
278 LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
285 case SNDRV_PCM_TRIGGER_STOP:
286 case SNDRV_PCM_TRIGGER_SUSPEND:
287 err = line6_pcm_release(line6pcm,
288 LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
302 /* capture pointer callback */
303 static snd_pcm_uframes_t
304 snd_line6_capture_pointer(struct snd_pcm_substream *substream)
306 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
308 return line6pcm->in.pos_done;
311 /* capture operators */
312 struct snd_pcm_ops snd_line6_capture_ops = {
313 .open = snd_line6_capture_open,
314 .close = snd_line6_capture_close,
315 .ioctl = snd_pcm_lib_ioctl,
316 .hw_params = snd_line6_capture_hw_params,
317 .hw_free = snd_line6_capture_hw_free,
318 .prepare = snd_line6_prepare,
319 .trigger = snd_line6_trigger,
320 .pointer = snd_line6_capture_pointer,
323 int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
325 struct usb_line6 *line6 = line6pcm->line6;
328 /* create audio URBs and fill in constant values: */
329 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
332 /* URB for audio in: */
333 urb = line6pcm->in.urbs[i] =
334 usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
339 urb->dev = line6->usbdev;
341 usb_rcvisocpipe(line6->usbdev,
342 line6->properties->ep_audio_r &
343 USB_ENDPOINT_NUMBER_MASK);
344 urb->transfer_flags = URB_ISO_ASAP;
345 urb->start_frame = -1;
346 urb->number_of_packets = LINE6_ISO_PACKETS;
347 urb->interval = LINE6_ISO_INTERVAL;
348 urb->error_count = 0;
349 urb->complete = audio_in_callback;