Commit | Line | Data |
---|---|---|
705ececd | 1 | /* |
c078a4aa | 2 | * Line 6 Linux USB driver |
705ececd | 3 | * |
1027f476 | 4 | * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at) |
705ececd MG |
5 | * |
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. | |
9 | * | |
10 | */ | |
11 | ||
140e28b8 | 12 | #include <linux/slab.h> |
705ececd MG |
13 | #include <sound/core.h> |
14 | #include <sound/pcm.h> | |
15 | #include <sound/pcm_params.h> | |
16 | ||
1027f476 MG |
17 | #include "capture.h" |
18 | #include "driver.h" | |
705ececd | 19 | #include "pcm.h" |
1027f476 | 20 | |
705ececd MG |
21 | /* |
22 | Find a free URB and submit it. | |
3d3ae445 | 23 | must be called in line6pcm->in.lock context |
705ececd | 24 | */ |
1027f476 | 25 | static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm) |
705ececd | 26 | { |
1027f476 | 27 | int index; |
705ececd | 28 | int i, urb_size; |
e1a164d7 | 29 | int ret; |
705ececd MG |
30 | struct urb *urb_in; |
31 | ||
e0645d63 | 32 | index = |
ad0119ab | 33 | find_first_zero_bit(&line6pcm->in.active_urbs, LINE6_ISO_BUFFERS); |
705ececd | 34 | |
1027f476 | 35 | if (index < 0 || index >= LINE6_ISO_BUFFERS) { |
1027f476 | 36 | dev_err(line6pcm->line6->ifcdev, "no free URB found\n"); |
705ececd MG |
37 | return -EINVAL; |
38 | } | |
39 | ||
ad0119ab | 40 | urb_in = line6pcm->in.urbs[index]; |
705ececd MG |
41 | urb_size = 0; |
42 | ||
6efc5667 | 43 | for (i = 0; i < LINE6_ISO_PACKETS; ++i) { |
e0645d63 SB |
44 | struct usb_iso_packet_descriptor *fin = |
45 | &urb_in->iso_frame_desc[i]; | |
705ececd MG |
46 | fin->offset = urb_size; |
47 | fin->length = line6pcm->max_packet_size; | |
48 | urb_size += line6pcm->max_packet_size; | |
49 | } | |
50 | ||
e0645d63 | 51 | urb_in->transfer_buffer = |
ad0119ab | 52 | line6pcm->in.buffer + |
e0645d63 | 53 | index * LINE6_ISO_PACKETS * line6pcm->max_packet_size; |
705ececd | 54 | urb_in->transfer_buffer_length = urb_size; |
1027f476 | 55 | urb_in->context = line6pcm; |
705ececd | 56 | |
e1a164d7 MG |
57 | ret = usb_submit_urb(urb_in, GFP_ATOMIC); |
58 | ||
59 | if (ret == 0) | |
ad0119ab | 60 | set_bit(index, &line6pcm->in.active_urbs); |
705ececd | 61 | else |
1027f476 | 62 | dev_err(line6pcm->line6->ifcdev, |
e1a164d7 | 63 | "URB in #%d submission failed (%d)\n", index, ret); |
705ececd | 64 | |
705ececd MG |
65 | return 0; |
66 | } | |
67 | ||
68 | /* | |
69 | Submit all currently available capture URBs. | |
63e20df1 | 70 | must be called in line6pcm->in.lock context |
705ececd | 71 | */ |
1027f476 | 72 | int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm) |
705ececd | 73 | { |
3d3ae445 | 74 | int ret = 0, i; |
705ececd | 75 | |
6efc5667 | 76 | for (i = 0; i < LINE6_ISO_BUFFERS; ++i) { |
1027f476 | 77 | ret = submit_audio_in_urb(line6pcm); |
6efc5667 | 78 | if (ret < 0) |
3d3ae445 | 79 | break; |
6efc5667 | 80 | } |
705ececd | 81 | |
3d3ae445 | 82 | return ret; |
705ececd MG |
83 | } |
84 | ||
705ececd | 85 | /* |
1027f476 MG |
86 | Copy data into ALSA capture buffer. |
87 | */ | |
88 | void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize) | |
89 | { | |
90 | struct snd_pcm_substream *substream = | |
91 | get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE); | |
92 | struct snd_pcm_runtime *runtime = substream->runtime; | |
93 | const int bytes_per_frame = line6pcm->properties->bytes_per_frame; | |
94 | int frames = fsize / bytes_per_frame; | |
95 | ||
597a1e7c | 96 | if (runtime == NULL) |
c7fcf255 MG |
97 | return; |
98 | ||
ad0119ab | 99 | if (line6pcm->in.pos_done + frames > runtime->buffer_size) { |
1027f476 | 100 | /* |
e1a164d7 MG |
101 | The transferred area goes over buffer boundary, |
102 | copy two separate chunks. | |
103 | */ | |
1027f476 | 104 | int len; |
b5f87cf9 | 105 | |
ad0119ab | 106 | len = runtime->buffer_size - line6pcm->in.pos_done; |
1027f476 MG |
107 | |
108 | if (len > 0) { | |
109 | memcpy(runtime->dma_area + | |
ad0119ab | 110 | line6pcm->in.pos_done * bytes_per_frame, fbuf, |
1027f476 MG |
111 | len * bytes_per_frame); |
112 | memcpy(runtime->dma_area, fbuf + len * bytes_per_frame, | |
113 | (frames - len) * bytes_per_frame); | |
027360c5 GKH |
114 | } else { |
115 | /* this is somewhat paranoid */ | |
116 | dev_err(line6pcm->line6->ifcdev, | |
117 | "driver bug: len = %d\n", len); | |
118 | } | |
1027f476 MG |
119 | } else { |
120 | /* copy single chunk */ | |
121 | memcpy(runtime->dma_area + | |
ad0119ab | 122 | line6pcm->in.pos_done * bytes_per_frame, fbuf, fsize); |
1027f476 MG |
123 | } |
124 | ||
ad0119ab TI |
125 | line6pcm->in.pos_done += frames; |
126 | if (line6pcm->in.pos_done >= runtime->buffer_size) | |
127 | line6pcm->in.pos_done -= runtime->buffer_size; | |
1027f476 MG |
128 | } |
129 | ||
130 | void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length) | |
131 | { | |
132 | struct snd_pcm_substream *substream = | |
133 | get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE); | |
134 | ||
ad0119ab TI |
135 | line6pcm->in.bytes += length; |
136 | if (line6pcm->in.bytes >= line6pcm->in.period) { | |
137 | line6pcm->in.bytes %= line6pcm->in.period; | |
3d3ae445 | 138 | spin_unlock(&line6pcm->in.lock); |
1027f476 | 139 | snd_pcm_period_elapsed(substream); |
3d3ae445 | 140 | spin_lock(&line6pcm->in.lock); |
1027f476 MG |
141 | } |
142 | } | |
143 | ||
144 | /* | |
027360c5 GKH |
145 | * Callback for completed capture URB. |
146 | */ | |
0c7ab158 | 147 | static void audio_in_callback(struct urb *urb) |
705ececd MG |
148 | { |
149 | int i, index, length = 0, shutdown = 0; | |
705ececd MG |
150 | unsigned long flags; |
151 | ||
1027f476 MG |
152 | struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context; |
153 | ||
ad0119ab | 154 | line6pcm->in.last_frame = urb->start_frame; |
705ececd MG |
155 | |
156 | /* find index of URB */ | |
6efc5667 | 157 | for (index = 0; index < LINE6_ISO_BUFFERS; ++index) |
ad0119ab | 158 | if (urb == line6pcm->in.urbs[index]) |
705ececd MG |
159 | break; |
160 | ||
ad0119ab | 161 | spin_lock_irqsave(&line6pcm->in.lock, flags); |
705ececd | 162 | |
6efc5667 | 163 | for (i = 0; i < LINE6_ISO_PACKETS; ++i) { |
705ececd MG |
164 | char *fbuf; |
165 | int fsize; | |
166 | struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i]; | |
167 | ||
e1a164d7 | 168 | if (fin->status == -EXDEV) { |
705ececd MG |
169 | shutdown = 1; |
170 | break; | |
171 | } | |
172 | ||
173 | fbuf = urb->transfer_buffer + fin->offset; | |
174 | fsize = fin->actual_length; | |
1027f476 MG |
175 | |
176 | if (fsize > line6pcm->max_packet_size) { | |
177 | dev_err(line6pcm->line6->ifcdev, | |
178 | "driver and/or device bug: packet too large (%d > %d)\n", | |
179 | fsize, line6pcm->max_packet_size); | |
180 | } | |
181 | ||
705ececd MG |
182 | length += fsize; |
183 | ||
1027f476 | 184 | /* the following assumes LINE6_ISO_PACKETS == 1: */ |
1027f476 | 185 | line6pcm->prev_fbuf = fbuf; |
1027f476 | 186 | line6pcm->prev_fsize = fsize; |
705ececd | 187 | |
63e20df1 TI |
188 | if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) && |
189 | test_bit(LINE6_STREAM_PCM, &line6pcm->in.running) && | |
190 | fsize > 0) | |
191 | line6_capture_copy(line6pcm, fbuf, fsize); | |
705ececd MG |
192 | } |
193 | ||
ad0119ab | 194 | clear_bit(index, &line6pcm->in.active_urbs); |
705ececd | 195 | |
ad0119ab | 196 | if (test_and_clear_bit(index, &line6pcm->in.unlink_urbs)) |
705ececd MG |
197 | shutdown = 1; |
198 | ||
6efc5667 | 199 | if (!shutdown) { |
1027f476 | 200 | submit_audio_in_urb(line6pcm); |
705ececd | 201 | |
63e20df1 TI |
202 | if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) && |
203 | test_bit(LINE6_STREAM_PCM, &line6pcm->in.running)) | |
204 | line6_capture_check_period(line6pcm, length); | |
705ececd | 205 | } |
3d3ae445 TI |
206 | |
207 | spin_unlock_irqrestore(&line6pcm->in.lock, flags); | |
705ececd MG |
208 | } |
209 | ||
210 | /* open capture callback */ | |
211 | static int snd_line6_capture_open(struct snd_pcm_substream *substream) | |
212 | { | |
213 | int err; | |
214 | struct snd_pcm_runtime *runtime = substream->runtime; | |
215 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); | |
216 | ||
6efc5667 GKH |
217 | err = snd_pcm_hw_constraint_ratdens(runtime, 0, |
218 | SNDRV_PCM_HW_PARAM_RATE, | |
1263f611 | 219 | &line6pcm->properties->rates); |
6efc5667 | 220 | if (err < 0) |
705ececd MG |
221 | return err; |
222 | ||
1263f611 | 223 | runtime->hw = line6pcm->properties->capture_hw; |
705ececd MG |
224 | return 0; |
225 | } | |
226 | ||
227 | /* close capture callback */ | |
228 | static int snd_line6_capture_close(struct snd_pcm_substream *substream) | |
229 | { | |
230 | return 0; | |
231 | } | |
232 | ||
705ececd MG |
233 | /* capture operators */ |
234 | struct snd_pcm_ops snd_line6_capture_ops = { | |
e1a164d7 MG |
235 | .open = snd_line6_capture_open, |
236 | .close = snd_line6_capture_close, | |
237 | .ioctl = snd_pcm_lib_ioctl, | |
63e20df1 TI |
238 | .hw_params = snd_line6_hw_params, |
239 | .hw_free = snd_line6_hw_free, | |
e1a164d7 MG |
240 | .prepare = snd_line6_prepare, |
241 | .trigger = snd_line6_trigger, | |
2954f914 | 242 | .pointer = snd_line6_pointer, |
705ececd MG |
243 | }; |
244 | ||
1027f476 | 245 | int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm) |
705ececd | 246 | { |
16d603d3 | 247 | struct usb_line6 *line6 = line6pcm->line6; |
705ececd MG |
248 | int i; |
249 | ||
250 | /* create audio URBs and fill in constant values: */ | |
6efc5667 | 251 | for (i = 0; i < LINE6_ISO_BUFFERS; ++i) { |
705ececd MG |
252 | struct urb *urb; |
253 | ||
254 | /* URB for audio in: */ | |
ad0119ab | 255 | urb = line6pcm->in.urbs[i] = |
e0645d63 | 256 | usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL); |
705ececd | 257 | |
a019f5e8 | 258 | if (urb == NULL) |
705ececd | 259 | return -ENOMEM; |
705ececd | 260 | |
16d603d3 | 261 | urb->dev = line6->usbdev; |
e0645d63 | 262 | urb->pipe = |
16d603d3 CR |
263 | usb_rcvisocpipe(line6->usbdev, |
264 | line6->properties->ep_audio_r & | |
e1a164d7 | 265 | USB_ENDPOINT_NUMBER_MASK); |
705ececd MG |
266 | urb->transfer_flags = URB_ISO_ASAP; |
267 | urb->start_frame = -1; | |
268 | urb->number_of_packets = LINE6_ISO_PACKETS; | |
269 | urb->interval = LINE6_ISO_INTERVAL; | |
270 | urb->error_count = 0; | |
271 | urb->complete = audio_in_callback; | |
272 | } | |
273 | ||
274 | return 0; | |
275 | } |