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.
24 static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm)
32 spin_lock_irqsave(&line6pcm->in.lock, flags);
34 find_first_zero_bit(&line6pcm->in.active_urbs, LINE6_ISO_BUFFERS);
36 if (index < 0 || index >= LINE6_ISO_BUFFERS) {
37 spin_unlock_irqrestore(&line6pcm->in.lock, flags);
38 dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
42 urb_in = line6pcm->in.urbs[index];
45 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
46 struct usb_iso_packet_descriptor *fin =
47 &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 =
55 index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
56 urb_in->transfer_buffer_length = urb_size;
57 urb_in->context = line6pcm;
59 ret = usb_submit_urb(urb_in, GFP_ATOMIC);
62 set_bit(index, &line6pcm->in.active_urbs);
64 dev_err(line6pcm->line6->ifcdev,
65 "URB in #%d submission failed (%d)\n", index, ret);
67 spin_unlock_irqrestore(&line6pcm->in.lock, flags);
72 Submit all currently available capture URBs.
74 int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm)
78 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
79 ret = submit_audio_in_urb(line6pcm);
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 snd_pcm_period_elapsed(substream);
144 void line6_free_capture_buffer(struct snd_line6_pcm *line6pcm)
146 kfree(line6pcm->in.buffer);
147 line6pcm->in.buffer = NULL;
151 * Callback for completed capture URB.
153 static void audio_in_callback(struct urb *urb)
155 int i, index, length = 0, shutdown = 0;
158 struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
160 line6pcm->in.last_frame = urb->start_frame;
162 /* find index of URB */
163 for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
164 if (urb == line6pcm->in.urbs[index])
167 spin_lock_irqsave(&line6pcm->in.lock, flags);
169 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
172 struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
174 if (fin->status == -EXDEV) {
179 fbuf = urb->transfer_buffer + fin->offset;
180 fsize = fin->actual_length;
182 if (fsize > line6pcm->max_packet_size) {
183 dev_err(line6pcm->line6->ifcdev,
184 "driver and/or device bug: packet too large (%d > %d)\n",
185 fsize, line6pcm->max_packet_size);
190 /* the following assumes LINE6_ISO_PACKETS == 1: */
191 line6pcm->prev_fbuf = fbuf;
192 line6pcm->prev_fsize = fsize;
194 if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
195 if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
196 &line6pcm->flags) && (fsize > 0))
197 line6_capture_copy(line6pcm, fbuf, fsize);
200 clear_bit(index, &line6pcm->in.active_urbs);
202 if (test_and_clear_bit(index, &line6pcm->in.unlink_urbs))
205 spin_unlock_irqrestore(&line6pcm->in.lock, flags);
208 submit_audio_in_urb(line6pcm);
210 if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
211 if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
213 line6_capture_check_period(line6pcm, length);
217 /* open capture callback */
218 static int snd_line6_capture_open(struct snd_pcm_substream *substream)
221 struct snd_pcm_runtime *runtime = substream->runtime;
222 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
224 err = snd_pcm_hw_constraint_ratdens(runtime, 0,
225 SNDRV_PCM_HW_PARAM_RATE,
227 properties->snd_line6_rates));
231 runtime->hw = line6pcm->properties->snd_line6_capture_hw;
235 /* close capture callback */
236 static int snd_line6_capture_close(struct snd_pcm_substream *substream)
241 /* hw_params capture callback */
242 static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream,
243 struct snd_pcm_hw_params *hw_params)
246 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
248 ret = line6_pcm_acquire(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
253 ret = snd_pcm_lib_malloc_pages(substream,
254 params_buffer_bytes(hw_params));
256 line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
260 line6pcm->in.period = params_period_bytes(hw_params);
264 /* hw_free capture callback */
265 static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
267 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
269 line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
270 return snd_pcm_lib_free_pages(substream);
273 /* trigger callback */
274 int snd_line6_capture_trigger(struct snd_line6_pcm *line6pcm, int cmd)
279 case SNDRV_PCM_TRIGGER_START:
280 case SNDRV_PCM_TRIGGER_RESUME:
281 err = line6_pcm_acquire(line6pcm,
282 LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
289 case SNDRV_PCM_TRIGGER_STOP:
290 case SNDRV_PCM_TRIGGER_SUSPEND:
291 err = line6_pcm_release(line6pcm,
292 LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
306 /* capture pointer callback */
307 static snd_pcm_uframes_t
308 snd_line6_capture_pointer(struct snd_pcm_substream *substream)
310 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
312 return line6pcm->in.pos_done;
315 /* capture operators */
316 struct snd_pcm_ops snd_line6_capture_ops = {
317 .open = snd_line6_capture_open,
318 .close = snd_line6_capture_close,
319 .ioctl = snd_pcm_lib_ioctl,
320 .hw_params = snd_line6_capture_hw_params,
321 .hw_free = snd_line6_capture_hw_free,
322 .prepare = snd_line6_prepare,
323 .trigger = snd_line6_trigger,
324 .pointer = snd_line6_capture_pointer,
327 int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
329 struct usb_line6 *line6 = line6pcm->line6;
332 /* create audio URBs and fill in constant values: */
333 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
336 /* URB for audio in: */
337 urb = line6pcm->in.urbs[i] =
338 usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
343 urb->dev = line6->usbdev;
345 usb_rcvisocpipe(line6->usbdev,
346 line6->properties->ep_audio_r &
347 USB_ENDPOINT_NUMBER_MASK);
348 urb->transfer_flags = URB_ISO_ASAP;
349 urb->start_frame = -1;
350 urb->number_of_packets = LINE6_ISO_PACKETS;
351 urb->interval = LINE6_ISO_INTERVAL;
352 urb->error_count = 0;
353 urb->complete = audio_in_callback;