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 | ||
b2233d97 AK |
32 | index = find_first_zero_bit(&line6pcm->in.active_urbs, |
33 | line6pcm->line6->iso_buffers); | |
705ececd | 34 | |
b2233d97 | 35 | if (index < 0 || index >= line6pcm->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 | 46 | fin->offset = urb_size; |
7a0f55ae AK |
47 | fin->length = line6pcm->max_packet_size_in; |
48 | urb_size += line6pcm->max_packet_size_in; | |
705ececd MG |
49 | } |
50 | ||
e0645d63 | 51 | urb_in->transfer_buffer = |
ad0119ab | 52 | line6pcm->in.buffer + |
7a0f55ae | 53 | index * LINE6_ISO_PACKETS * line6pcm->max_packet_size_in; |
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 | |
b2233d97 | 76 | for (i = 0; i < line6pcm->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; | |
97d78acf AK |
93 | const int bytes_per_frame = |
94 | line6pcm->properties->bytes_per_channel * | |
95 | line6pcm->properties->capture_hw.channels_max; | |
1027f476 MG |
96 | int frames = fsize / bytes_per_frame; |
97 | ||
597a1e7c | 98 | if (runtime == NULL) |
c7fcf255 MG |
99 | return; |
100 | ||
ad0119ab | 101 | if (line6pcm->in.pos_done + frames > runtime->buffer_size) { |
1027f476 | 102 | /* |
e1a164d7 MG |
103 | The transferred area goes over buffer boundary, |
104 | copy two separate chunks. | |
105 | */ | |
1027f476 | 106 | int len; |
b5f87cf9 | 107 | |
ad0119ab | 108 | len = runtime->buffer_size - line6pcm->in.pos_done; |
1027f476 MG |
109 | |
110 | if (len > 0) { | |
111 | memcpy(runtime->dma_area + | |
ad0119ab | 112 | line6pcm->in.pos_done * bytes_per_frame, fbuf, |
1027f476 MG |
113 | len * bytes_per_frame); |
114 | memcpy(runtime->dma_area, fbuf + len * bytes_per_frame, | |
115 | (frames - len) * bytes_per_frame); | |
027360c5 GKH |
116 | } else { |
117 | /* this is somewhat paranoid */ | |
118 | dev_err(line6pcm->line6->ifcdev, | |
119 | "driver bug: len = %d\n", len); | |
120 | } | |
1027f476 MG |
121 | } else { |
122 | /* copy single chunk */ | |
123 | memcpy(runtime->dma_area + | |
ad0119ab | 124 | line6pcm->in.pos_done * bytes_per_frame, fbuf, fsize); |
1027f476 MG |
125 | } |
126 | ||
ad0119ab TI |
127 | line6pcm->in.pos_done += frames; |
128 | if (line6pcm->in.pos_done >= runtime->buffer_size) | |
129 | line6pcm->in.pos_done -= runtime->buffer_size; | |
1027f476 MG |
130 | } |
131 | ||
132 | void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length) | |
133 | { | |
134 | struct snd_pcm_substream *substream = | |
135 | get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE); | |
136 | ||
ad0119ab TI |
137 | line6pcm->in.bytes += length; |
138 | if (line6pcm->in.bytes >= line6pcm->in.period) { | |
139 | line6pcm->in.bytes %= line6pcm->in.period; | |
3d3ae445 | 140 | spin_unlock(&line6pcm->in.lock); |
1027f476 | 141 | snd_pcm_period_elapsed(substream); |
3d3ae445 | 142 | spin_lock(&line6pcm->in.lock); |
1027f476 MG |
143 | } |
144 | } | |
145 | ||
146 | /* | |
027360c5 GKH |
147 | * Callback for completed capture URB. |
148 | */ | |
0c7ab158 | 149 | static void audio_in_callback(struct urb *urb) |
705ececd MG |
150 | { |
151 | int i, index, length = 0, shutdown = 0; | |
705ececd MG |
152 | unsigned long flags; |
153 | ||
1027f476 MG |
154 | struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context; |
155 | ||
ad0119ab | 156 | line6pcm->in.last_frame = urb->start_frame; |
705ececd MG |
157 | |
158 | /* find index of URB */ | |
b2233d97 | 159 | for (index = 0; index < line6pcm->line6->iso_buffers; ++index) |
ad0119ab | 160 | if (urb == line6pcm->in.urbs[index]) |
705ececd MG |
161 | break; |
162 | ||
ad0119ab | 163 | spin_lock_irqsave(&line6pcm->in.lock, flags); |
705ececd | 164 | |
6efc5667 | 165 | for (i = 0; i < LINE6_ISO_PACKETS; ++i) { |
705ececd MG |
166 | char *fbuf; |
167 | int fsize; | |
168 | struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i]; | |
169 | ||
e1a164d7 | 170 | if (fin->status == -EXDEV) { |
705ececd MG |
171 | shutdown = 1; |
172 | break; | |
173 | } | |
174 | ||
175 | fbuf = urb->transfer_buffer + fin->offset; | |
176 | fsize = fin->actual_length; | |
1027f476 | 177 | |
7a0f55ae | 178 | if (fsize > line6pcm->max_packet_size_in) { |
1027f476 MG |
179 | dev_err(line6pcm->line6->ifcdev, |
180 | "driver and/or device bug: packet too large (%d > %d)\n", | |
7a0f55ae | 181 | fsize, line6pcm->max_packet_size_in); |
1027f476 MG |
182 | } |
183 | ||
705ececd MG |
184 | length += fsize; |
185 | ||
79faa2b0 AK |
186 | BUILD_BUG_ON_MSG(LINE6_ISO_PACKETS != 1, |
187 | "The following code assumes LINE6_ISO_PACKETS == 1"); | |
188 | /* TODO: | |
189 | * Also, if iso_buffers != 2, the prev frame is almost random at | |
190 | * playback side. | |
191 | * This needs to be redesigned. It should be "stable", but we may | |
192 | * experience sync problems on such high-speed configs. | |
193 | */ | |
194 | ||
1027f476 | 195 | line6pcm->prev_fbuf = fbuf; |
97d78acf AK |
196 | line6pcm->prev_fsize = fsize / |
197 | (line6pcm->properties->bytes_per_channel * | |
198 | line6pcm->properties->capture_hw.channels_max); | |
705ececd | 199 | |
63e20df1 TI |
200 | if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) && |
201 | test_bit(LINE6_STREAM_PCM, &line6pcm->in.running) && | |
202 | fsize > 0) | |
203 | line6_capture_copy(line6pcm, fbuf, fsize); | |
705ececd MG |
204 | } |
205 | ||
ad0119ab | 206 | clear_bit(index, &line6pcm->in.active_urbs); |
705ececd | 207 | |
ad0119ab | 208 | if (test_and_clear_bit(index, &line6pcm->in.unlink_urbs)) |
705ececd MG |
209 | shutdown = 1; |
210 | ||
6efc5667 | 211 | if (!shutdown) { |
1027f476 | 212 | submit_audio_in_urb(line6pcm); |
705ececd | 213 | |
63e20df1 TI |
214 | if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) && |
215 | test_bit(LINE6_STREAM_PCM, &line6pcm->in.running)) | |
216 | line6_capture_check_period(line6pcm, length); | |
705ececd | 217 | } |
3d3ae445 TI |
218 | |
219 | spin_unlock_irqrestore(&line6pcm->in.lock, flags); | |
705ececd MG |
220 | } |
221 | ||
222 | /* open capture callback */ | |
223 | static int snd_line6_capture_open(struct snd_pcm_substream *substream) | |
224 | { | |
225 | int err; | |
226 | struct snd_pcm_runtime *runtime = substream->runtime; | |
227 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); | |
228 | ||
6efc5667 GKH |
229 | err = snd_pcm_hw_constraint_ratdens(runtime, 0, |
230 | SNDRV_PCM_HW_PARAM_RATE, | |
1263f611 | 231 | &line6pcm->properties->rates); |
6efc5667 | 232 | if (err < 0) |
705ececd MG |
233 | return err; |
234 | ||
f56742cc AK |
235 | line6_pcm_acquire(line6pcm, LINE6_STREAM_CAPTURE_HELPER, false); |
236 | ||
1263f611 | 237 | runtime->hw = line6pcm->properties->capture_hw; |
705ececd MG |
238 | return 0; |
239 | } | |
240 | ||
241 | /* close capture callback */ | |
242 | static int snd_line6_capture_close(struct snd_pcm_substream *substream) | |
243 | { | |
f56742cc AK |
244 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
245 | ||
246 | line6_pcm_release(line6pcm, LINE6_STREAM_CAPTURE_HELPER); | |
705ececd MG |
247 | return 0; |
248 | } | |
249 | ||
705ececd MG |
250 | /* capture operators */ |
251 | struct snd_pcm_ops snd_line6_capture_ops = { | |
e1a164d7 MG |
252 | .open = snd_line6_capture_open, |
253 | .close = snd_line6_capture_close, | |
254 | .ioctl = snd_pcm_lib_ioctl, | |
63e20df1 TI |
255 | .hw_params = snd_line6_hw_params, |
256 | .hw_free = snd_line6_hw_free, | |
e1a164d7 MG |
257 | .prepare = snd_line6_prepare, |
258 | .trigger = snd_line6_trigger, | |
2954f914 | 259 | .pointer = snd_line6_pointer, |
705ececd MG |
260 | }; |
261 | ||
1027f476 | 262 | int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm) |
705ececd | 263 | { |
16d603d3 | 264 | struct usb_line6 *line6 = line6pcm->line6; |
705ececd MG |
265 | int i; |
266 | ||
b2233d97 AK |
267 | line6pcm->in.urbs = kzalloc( |
268 | sizeof(struct urb *) * line6->iso_buffers, GFP_KERNEL); | |
269 | if (line6pcm->in.urbs == NULL) | |
270 | return -ENOMEM; | |
271 | ||
705ececd | 272 | /* create audio URBs and fill in constant values: */ |
b2233d97 | 273 | for (i = 0; i < line6->iso_buffers; ++i) { |
705ececd MG |
274 | struct urb *urb; |
275 | ||
276 | /* URB for audio in: */ | |
ad0119ab | 277 | urb = line6pcm->in.urbs[i] = |
e0645d63 | 278 | usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL); |
705ececd | 279 | |
a019f5e8 | 280 | if (urb == NULL) |
705ececd | 281 | return -ENOMEM; |
705ececd | 282 | |
16d603d3 | 283 | urb->dev = line6->usbdev; |
e0645d63 | 284 | urb->pipe = |
16d603d3 CR |
285 | usb_rcvisocpipe(line6->usbdev, |
286 | line6->properties->ep_audio_r & | |
e1a164d7 | 287 | USB_ENDPOINT_NUMBER_MASK); |
705ececd MG |
288 | urb->transfer_flags = URB_ISO_ASAP; |
289 | urb->start_frame = -1; | |
290 | urb->number_of_packets = LINE6_ISO_PACKETS; | |
291 | urb->interval = LINE6_ISO_INTERVAL; | |
292 | urb->error_count = 0; | |
293 | urb->complete = audio_in_callback; | |
294 | } | |
295 | ||
296 | return 0; | |
297 | } |