Commit | Line | Data |
---|---|---|
523f1dce | 1 | /* |
8d048841 | 2 | * Copyright (c) 2006-2008 Daniel Mack, Karsten Wiese |
523f1dce DM |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | */ | |
18 | ||
e431cf45 | 19 | #include <linux/spinlock.h> |
5a0e3ad6 | 20 | #include <linux/slab.h> |
523f1dce | 21 | #include <linux/init.h> |
523f1dce | 22 | #include <linux/usb.h> |
523f1dce | 23 | #include <sound/core.h> |
523f1dce | 24 | #include <sound/pcm.h> |
523f1dce | 25 | |
936e7d03 DM |
26 | #include "device.h" |
27 | #include "audio.h" | |
523f1dce DM |
28 | |
29 | #define N_URBS 32 | |
30 | #define CLOCK_DRIFT_TOLERANCE 5 | |
31 | #define FRAMES_PER_URB 8 | |
32 | #define BYTES_PER_FRAME 512 | |
33 | #define CHANNELS_PER_STREAM 2 | |
34 | #define BYTES_PER_SAMPLE 3 | |
35 | #define BYTES_PER_SAMPLE_USB 4 | |
36 | #define MAX_BUFFER_SIZE (128*1024) | |
6e9fc6bd DM |
37 | #define MAX_ENDPOINT_SIZE 512 |
38 | ||
523f1dce DM |
39 | #define ENDPOINT_CAPTURE 2 |
40 | #define ENDPOINT_PLAYBACK 6 | |
41 | ||
42 | #define MAKE_CHECKBYTE(dev,stream,i) \ | |
43 | (stream << 1) | (~(i / (dev->n_streams * BYTES_PER_SAMPLE_USB)) & 1) | |
44 | ||
45 | static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = { | |
9318dce5 | 46 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | |
523f1dce DM |
47 | SNDRV_PCM_INFO_BLOCK_TRANSFER), |
48 | .formats = SNDRV_PCM_FMTBIT_S24_3BE, | |
9318dce5 | 49 | .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | |
523f1dce DM |
50 | SNDRV_PCM_RATE_96000), |
51 | .rate_min = 44100, | |
52 | .rate_max = 0, /* will overwrite later */ | |
53 | .channels_min = CHANNELS_PER_STREAM, | |
54 | .channels_max = CHANNELS_PER_STREAM, | |
55 | .buffer_bytes_max = MAX_BUFFER_SIZE, | |
09189ac7 | 56 | .period_bytes_min = 128, |
523f1dce DM |
57 | .period_bytes_max = MAX_BUFFER_SIZE, |
58 | .periods_min = 1, | |
59 | .periods_max = 1024, | |
60 | }; | |
61 | ||
62 | static void | |
63 | activate_substream(struct snd_usb_caiaqdev *dev, | |
64 | struct snd_pcm_substream *sub) | |
65 | { | |
ac9dd9d3 MH |
66 | spin_lock(&dev->spinlock); |
67 | ||
523f1dce DM |
68 | if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) |
69 | dev->sub_playback[sub->number] = sub; | |
70 | else | |
71 | dev->sub_capture[sub->number] = sub; | |
ac9dd9d3 MH |
72 | |
73 | spin_unlock(&dev->spinlock); | |
523f1dce DM |
74 | } |
75 | ||
9318dce5 | 76 | static void |
523f1dce DM |
77 | deactivate_substream(struct snd_usb_caiaqdev *dev, |
78 | struct snd_pcm_substream *sub) | |
79 | { | |
8d048841 DM |
80 | unsigned long flags; |
81 | spin_lock_irqsave(&dev->spinlock, flags); | |
82 | ||
523f1dce DM |
83 | if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) |
84 | dev->sub_playback[sub->number] = NULL; | |
85 | else | |
86 | dev->sub_capture[sub->number] = NULL; | |
8d048841 DM |
87 | |
88 | spin_unlock_irqrestore(&dev->spinlock, flags); | |
523f1dce DM |
89 | } |
90 | ||
91 | static int | |
92 | all_substreams_zero(struct snd_pcm_substream **subs) | |
93 | { | |
94 | int i; | |
95 | for (i = 0; i < MAX_STREAMS; i++) | |
96 | if (subs[i] != NULL) | |
97 | return 0; | |
98 | return 1; | |
99 | } | |
100 | ||
101 | static int stream_start(struct snd_usb_caiaqdev *dev) | |
102 | { | |
103 | int i, ret; | |
104 | ||
8d048841 DM |
105 | debug("%s(%p)\n", __func__, dev); |
106 | ||
107 | if (dev->streaming) | |
523f1dce | 108 | return -EINVAL; |
523f1dce | 109 | |
8d048841 DM |
110 | memset(dev->sub_playback, 0, sizeof(dev->sub_playback)); |
111 | memset(dev->sub_capture, 0, sizeof(dev->sub_capture)); | |
523f1dce DM |
112 | dev->input_panic = 0; |
113 | dev->output_panic = 0; | |
15c5ab60 | 114 | dev->first_packet = 4; |
523f1dce | 115 | dev->streaming = 1; |
1313e704 | 116 | dev->warned = 0; |
523f1dce DM |
117 | |
118 | for (i = 0; i < N_URBS; i++) { | |
119 | ret = usb_submit_urb(dev->data_urbs_in[i], GFP_ATOMIC); | |
120 | if (ret) { | |
8d048841 | 121 | log("unable to trigger read #%d! (ret %d)\n", i, ret); |
523f1dce | 122 | dev->streaming = 0; |
523f1dce DM |
123 | return -EPIPE; |
124 | } | |
125 | } | |
9318dce5 | 126 | |
523f1dce DM |
127 | return 0; |
128 | } | |
129 | ||
130 | static void stream_stop(struct snd_usb_caiaqdev *dev) | |
131 | { | |
132 | int i; | |
8d048841 DM |
133 | |
134 | debug("%s(%p)\n", __func__, dev); | |
523f1dce DM |
135 | if (!dev->streaming) |
136 | return; | |
9318dce5 | 137 | |
523f1dce | 138 | dev->streaming = 0; |
8d048841 | 139 | |
523f1dce | 140 | for (i = 0; i < N_URBS; i++) { |
8d048841 | 141 | usb_kill_urb(dev->data_urbs_in[i]); |
da6094ea DM |
142 | |
143 | if (test_bit(i, &dev->outurb_active_mask)) | |
144 | usb_kill_urb(dev->data_urbs_out[i]); | |
523f1dce | 145 | } |
da6094ea DM |
146 | |
147 | dev->outurb_active_mask = 0; | |
523f1dce DM |
148 | } |
149 | ||
150 | static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream) | |
151 | { | |
152 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream); | |
8d048841 | 153 | debug("%s(%p)\n", __func__, substream); |
523f1dce DM |
154 | substream->runtime->hw = dev->pcm_info; |
155 | snd_pcm_limit_hw_rates(substream->runtime); | |
156 | return 0; | |
157 | } | |
158 | ||
159 | static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream) | |
160 | { | |
161 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream); | |
162 | ||
8d048841 | 163 | debug("%s(%p)\n", __func__, substream); |
523f1dce DM |
164 | if (all_substreams_zero(dev->sub_playback) && |
165 | all_substreams_zero(dev->sub_capture)) { | |
9318dce5 | 166 | /* when the last client has stopped streaming, |
523f1dce DM |
167 | * all sample rates are allowed again */ |
168 | stream_stop(dev); | |
169 | dev->pcm_info.rates = dev->samplerates; | |
170 | } | |
8d048841 | 171 | |
523f1dce DM |
172 | return 0; |
173 | } | |
174 | ||
175 | static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub, | |
15c5ab60 | 176 | struct snd_pcm_hw_params *hw_params) |
523f1dce | 177 | { |
8d048841 | 178 | debug("%s(%p)\n", __func__, sub); |
523f1dce DM |
179 | return snd_pcm_lib_malloc_pages(sub, params_buffer_bytes(hw_params)); |
180 | } | |
181 | ||
182 | static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub) | |
183 | { | |
184 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub); | |
8d048841 | 185 | debug("%s(%p)\n", __func__, sub); |
523f1dce | 186 | deactivate_substream(dev, sub); |
523f1dce DM |
187 | return snd_pcm_lib_free_pages(sub); |
188 | } | |
189 | ||
190 | /* this should probably go upstream */ | |
191 | #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 | |
192 | #error "Change this table" | |
193 | #endif | |
194 | ||
195 | static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100, | |
15c5ab60 | 196 | 48000, 64000, 88200, 96000, 176400, 192000 }; |
523f1dce DM |
197 | |
198 | static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream) | |
199 | { | |
200 | int bytes_per_sample, bpp, ret, i; | |
201 | int index = substream->number; | |
202 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream); | |
203 | struct snd_pcm_runtime *runtime = substream->runtime; | |
204 | ||
8d048841 | 205 | debug("%s(%p)\n", __func__, substream); |
9318dce5 | 206 | |
a9b487fa | 207 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
15c5ab60 DM |
208 | int out_pos; |
209 | ||
210 | switch (dev->spec.data_alignment) { | |
211 | case 0: | |
212 | case 2: | |
213 | out_pos = BYTES_PER_SAMPLE + 1; | |
214 | break; | |
215 | case 3: | |
216 | default: | |
217 | out_pos = 0; | |
218 | break; | |
219 | } | |
220 | ||
221 | dev->period_out_count[index] = out_pos; | |
222 | dev->audio_out_buf_pos[index] = out_pos; | |
a9b487fa | 223 | } else { |
15c5ab60 DM |
224 | int in_pos; |
225 | ||
226 | switch (dev->spec.data_alignment) { | |
227 | case 0: | |
228 | in_pos = BYTES_PER_SAMPLE + 2; | |
229 | break; | |
230 | case 2: | |
231 | in_pos = BYTES_PER_SAMPLE; | |
232 | break; | |
233 | case 3: | |
234 | default: | |
235 | in_pos = 0; | |
236 | break; | |
237 | } | |
238 | ||
239 | dev->period_in_count[index] = in_pos; | |
240 | dev->audio_in_buf_pos[index] = in_pos; | |
a9b487fa DM |
241 | } |
242 | ||
523f1dce DM |
243 | if (dev->streaming) |
244 | return 0; | |
9318dce5 | 245 | |
523f1dce DM |
246 | /* the first client that opens a stream defines the sample rate |
247 | * setting for all subsequent calls, until the last client closed. */ | |
248 | for (i=0; i < ARRAY_SIZE(rates); i++) | |
249 | if (runtime->rate == rates[i]) | |
250 | dev->pcm_info.rates = 1 << i; | |
9318dce5 | 251 | |
523f1dce DM |
252 | snd_pcm_limit_hw_rates(runtime); |
253 | ||
254 | bytes_per_sample = BYTES_PER_SAMPLE; | |
15c5ab60 | 255 | if (dev->spec.data_alignment >= 2) |
523f1dce | 256 | bytes_per_sample++; |
9318dce5 | 257 | |
523f1dce DM |
258 | bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE) |
259 | * bytes_per_sample * CHANNELS_PER_STREAM * dev->n_streams; | |
6e9fc6bd DM |
260 | |
261 | if (bpp > MAX_ENDPOINT_SIZE) | |
262 | bpp = MAX_ENDPOINT_SIZE; | |
263 | ||
523f1dce DM |
264 | ret = snd_usb_caiaq_set_audio_params(dev, runtime->rate, |
265 | runtime->sample_bits, bpp); | |
266 | if (ret) | |
267 | return ret; | |
268 | ||
269 | ret = stream_start(dev); | |
270 | if (ret) | |
271 | return ret; | |
9318dce5 | 272 | |
523f1dce DM |
273 | dev->output_running = 0; |
274 | wait_event_timeout(dev->prepare_wait_queue, dev->output_running, HZ); | |
275 | if (!dev->output_running) { | |
276 | stream_stop(dev); | |
277 | return -EPIPE; | |
278 | } | |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
283 | static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd) | |
284 | { | |
285 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub); | |
286 | ||
15c5ab60 DM |
287 | debug("%s(%p) cmd %d\n", __func__, sub, cmd); |
288 | ||
523f1dce DM |
289 | switch (cmd) { |
290 | case SNDRV_PCM_TRIGGER_START: | |
291 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
523f1dce | 292 | activate_substream(dev, sub); |
523f1dce DM |
293 | break; |
294 | case SNDRV_PCM_TRIGGER_STOP: | |
295 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
523f1dce | 296 | deactivate_substream(dev, sub); |
523f1dce DM |
297 | break; |
298 | default: | |
299 | return -EINVAL; | |
300 | } | |
301 | ||
302 | return 0; | |
303 | } | |
304 | ||
305 | static snd_pcm_uframes_t | |
306 | snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub) | |
307 | { | |
308 | int index = sub->number; | |
309 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub); | |
3702b082 MH |
310 | snd_pcm_uframes_t ptr; |
311 | ||
312 | spin_lock(&dev->spinlock); | |
523f1dce DM |
313 | |
314 | if (dev->input_panic || dev->output_panic) | |
3702b082 | 315 | ptr = SNDRV_PCM_POS_XRUN; |
523f1dce DM |
316 | |
317 | if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
3702b082 | 318 | ptr = bytes_to_frames(sub->runtime, |
523f1dce DM |
319 | dev->audio_out_buf_pos[index]); |
320 | else | |
3702b082 | 321 | ptr = bytes_to_frames(sub->runtime, |
523f1dce | 322 | dev->audio_in_buf_pos[index]); |
3702b082 MH |
323 | |
324 | spin_unlock(&dev->spinlock); | |
325 | return ptr; | |
523f1dce DM |
326 | } |
327 | ||
328 | /* operators for both playback and capture */ | |
329 | static struct snd_pcm_ops snd_usb_caiaq_ops = { | |
330 | .open = snd_usb_caiaq_substream_open, | |
331 | .close = snd_usb_caiaq_substream_close, | |
332 | .ioctl = snd_pcm_lib_ioctl, | |
333 | .hw_params = snd_usb_caiaq_pcm_hw_params, | |
334 | .hw_free = snd_usb_caiaq_pcm_hw_free, | |
335 | .prepare = snd_usb_caiaq_pcm_prepare, | |
336 | .trigger = snd_usb_caiaq_pcm_trigger, | |
337 | .pointer = snd_usb_caiaq_pcm_pointer | |
338 | }; | |
9318dce5 | 339 | |
523f1dce DM |
340 | static void check_for_elapsed_periods(struct snd_usb_caiaqdev *dev, |
341 | struct snd_pcm_substream **subs) | |
342 | { | |
343 | int stream, pb, *cnt; | |
344 | struct snd_pcm_substream *sub; | |
345 | ||
346 | for (stream = 0; stream < dev->n_streams; stream++) { | |
347 | sub = subs[stream]; | |
348 | if (!sub) | |
349 | continue; | |
350 | ||
a9b487fa | 351 | pb = snd_pcm_lib_period_bytes(sub); |
523f1dce DM |
352 | cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ? |
353 | &dev->period_out_count[stream] : | |
354 | &dev->period_in_count[stream]; | |
355 | ||
356 | if (*cnt >= pb) { | |
357 | snd_pcm_period_elapsed(sub); | |
358 | *cnt %= pb; | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
363 | static void read_in_urb_mode0(struct snd_usb_caiaqdev *dev, | |
364 | const struct urb *urb, | |
365 | const struct usb_iso_packet_descriptor *iso) | |
366 | { | |
367 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; | |
368 | struct snd_pcm_substream *sub; | |
369 | int stream, i; | |
370 | ||
371 | if (all_substreams_zero(dev->sub_capture)) | |
372 | return; | |
373 | ||
523f1dce DM |
374 | for (i = 0; i < iso->actual_length;) { |
375 | for (stream = 0; stream < dev->n_streams; stream++, i++) { | |
376 | sub = dev->sub_capture[stream]; | |
377 | if (sub) { | |
378 | struct snd_pcm_runtime *rt = sub->runtime; | |
379 | char *audio_buf = rt->dma_area; | |
380 | int sz = frames_to_bytes(rt, rt->buffer_size); | |
9318dce5 | 381 | audio_buf[dev->audio_in_buf_pos[stream]++] |
523f1dce DM |
382 | = usb_buf[i]; |
383 | dev->period_in_count[stream]++; | |
384 | if (dev->audio_in_buf_pos[stream] == sz) | |
385 | dev->audio_in_buf_pos[stream] = 0; | |
386 | } | |
387 | } | |
388 | } | |
523f1dce DM |
389 | } |
390 | ||
391 | static void read_in_urb_mode2(struct snd_usb_caiaqdev *dev, | |
392 | const struct urb *urb, | |
393 | const struct usb_iso_packet_descriptor *iso) | |
394 | { | |
395 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; | |
396 | unsigned char check_byte; | |
397 | struct snd_pcm_substream *sub; | |
398 | int stream, i; | |
399 | ||
523f1dce DM |
400 | for (i = 0; i < iso->actual_length;) { |
401 | if (i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == 0) { | |
9318dce5 DM |
402 | for (stream = 0; |
403 | stream < dev->n_streams; | |
523f1dce DM |
404 | stream++, i++) { |
405 | if (dev->first_packet) | |
406 | continue; | |
407 | ||
408 | check_byte = MAKE_CHECKBYTE(dev, stream, i); | |
9318dce5 | 409 | |
523f1dce DM |
410 | if ((usb_buf[i] & 0x3f) != check_byte) |
411 | dev->input_panic = 1; | |
412 | ||
413 | if (usb_buf[i] & 0x80) | |
414 | dev->output_panic = 1; | |
415 | } | |
416 | } | |
417 | dev->first_packet = 0; | |
418 | ||
419 | for (stream = 0; stream < dev->n_streams; stream++, i++) { | |
420 | sub = dev->sub_capture[stream]; | |
9311c9b4 DM |
421 | if (dev->input_panic) |
422 | usb_buf[i] = 0; | |
423 | ||
523f1dce DM |
424 | if (sub) { |
425 | struct snd_pcm_runtime *rt = sub->runtime; | |
426 | char *audio_buf = rt->dma_area; | |
427 | int sz = frames_to_bytes(rt, rt->buffer_size); | |
a971c3d4 KW |
428 | audio_buf[dev->audio_in_buf_pos[stream]++] = |
429 | usb_buf[i]; | |
523f1dce DM |
430 | dev->period_in_count[stream]++; |
431 | if (dev->audio_in_buf_pos[stream] == sz) | |
432 | dev->audio_in_buf_pos[stream] = 0; | |
433 | } | |
434 | } | |
435 | } | |
523f1dce DM |
436 | } |
437 | ||
15c5ab60 DM |
438 | static void read_in_urb_mode3(struct snd_usb_caiaqdev *dev, |
439 | const struct urb *urb, | |
440 | const struct usb_iso_packet_descriptor *iso) | |
441 | { | |
442 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; | |
443 | int stream, i; | |
444 | ||
445 | /* paranoia check */ | |
446 | if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM)) | |
447 | return; | |
448 | ||
449 | for (i = 0; i < iso->actual_length;) { | |
450 | for (stream = 0; stream < dev->n_streams; stream++) { | |
451 | struct snd_pcm_substream *sub = dev->sub_capture[stream]; | |
452 | char *audio_buf = NULL; | |
453 | int c, n, sz = 0; | |
454 | ||
455 | if (sub && !dev->input_panic) { | |
456 | struct snd_pcm_runtime *rt = sub->runtime; | |
457 | audio_buf = rt->dma_area; | |
458 | sz = frames_to_bytes(rt, rt->buffer_size); | |
459 | } | |
460 | ||
461 | for (c = 0; c < CHANNELS_PER_STREAM; c++) { | |
462 | /* 3 audio data bytes, followed by 1 check byte */ | |
463 | if (audio_buf) { | |
464 | for (n = 0; n < BYTES_PER_SAMPLE; n++) { | |
465 | audio_buf[dev->audio_in_buf_pos[stream]++] = usb_buf[i+n]; | |
466 | ||
467 | if (dev->audio_in_buf_pos[stream] == sz) | |
468 | dev->audio_in_buf_pos[stream] = 0; | |
469 | } | |
470 | ||
471 | dev->period_in_count[stream] += BYTES_PER_SAMPLE; | |
472 | } | |
473 | ||
474 | i += BYTES_PER_SAMPLE; | |
475 | ||
476 | if (usb_buf[i] != ((stream << 1) | c) && | |
477 | !dev->first_packet) { | |
478 | if (!dev->input_panic) | |
479 | printk(" EXPECTED: %02x got %02x, c %d, stream %d, i %d\n", | |
480 | ((stream << 1) | c), usb_buf[i], c, stream, i); | |
481 | dev->input_panic = 1; | |
482 | } | |
483 | ||
484 | i++; | |
485 | } | |
486 | } | |
487 | } | |
488 | ||
489 | if (dev->first_packet > 0) | |
490 | dev->first_packet--; | |
491 | } | |
492 | ||
523f1dce DM |
493 | static void read_in_urb(struct snd_usb_caiaqdev *dev, |
494 | const struct urb *urb, | |
495 | const struct usb_iso_packet_descriptor *iso) | |
496 | { | |
497 | if (!dev->streaming) | |
498 | return; | |
499 | ||
9311c9b4 DM |
500 | if (iso->actual_length < dev->bpp) |
501 | return; | |
502 | ||
523f1dce DM |
503 | switch (dev->spec.data_alignment) { |
504 | case 0: | |
505 | read_in_urb_mode0(dev, urb, iso); | |
506 | break; | |
507 | case 2: | |
508 | read_in_urb_mode2(dev, urb, iso); | |
509 | break; | |
15c5ab60 DM |
510 | case 3: |
511 | read_in_urb_mode3(dev, urb, iso); | |
512 | break; | |
523f1dce DM |
513 | } |
514 | ||
1313e704 | 515 | if ((dev->input_panic || dev->output_panic) && !dev->warned) { |
9318dce5 | 516 | debug("streaming error detected %s %s\n", |
523f1dce DM |
517 | dev->input_panic ? "(input)" : "", |
518 | dev->output_panic ? "(output)" : ""); | |
1313e704 | 519 | dev->warned = 1; |
523f1dce | 520 | } |
523f1dce DM |
521 | } |
522 | ||
15c5ab60 DM |
523 | static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *dev, |
524 | struct urb *urb, | |
525 | const struct usb_iso_packet_descriptor *iso) | |
523f1dce DM |
526 | { |
527 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; | |
528 | struct snd_pcm_substream *sub; | |
529 | int stream, i; | |
9318dce5 | 530 | |
523f1dce | 531 | for (i = 0; i < iso->length;) { |
a971c3d4 | 532 | for (stream = 0; stream < dev->n_streams; stream++, i++) { |
523f1dce DM |
533 | sub = dev->sub_playback[stream]; |
534 | if (sub) { | |
535 | struct snd_pcm_runtime *rt = sub->runtime; | |
536 | char *audio_buf = rt->dma_area; | |
537 | int sz = frames_to_bytes(rt, rt->buffer_size); | |
a971c3d4 KW |
538 | usb_buf[i] = |
539 | audio_buf[dev->audio_out_buf_pos[stream]]; | |
540 | dev->period_out_count[stream]++; | |
523f1dce DM |
541 | dev->audio_out_buf_pos[stream]++; |
542 | if (dev->audio_out_buf_pos[stream] == sz) | |
543 | dev->audio_out_buf_pos[stream] = 0; | |
544 | } else | |
a971c3d4 KW |
545 | usb_buf[i] = 0; |
546 | } | |
523f1dce DM |
547 | |
548 | /* fill in the check bytes */ | |
549 | if (dev->spec.data_alignment == 2 && | |
9318dce5 | 550 | i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == |
15c5ab60 DM |
551 | (dev->n_streams * CHANNELS_PER_STREAM)) |
552 | for (stream = 0; stream < dev->n_streams; stream++, i++) | |
553 | usb_buf[i] = MAKE_CHECKBYTE(dev, stream, i); | |
554 | } | |
555 | } | |
556 | ||
557 | static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *dev, | |
558 | struct urb *urb, | |
559 | const struct usb_iso_packet_descriptor *iso) | |
560 | { | |
561 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; | |
562 | int stream, i; | |
563 | ||
564 | for (i = 0; i < iso->length;) { | |
565 | for (stream = 0; stream < dev->n_streams; stream++) { | |
566 | struct snd_pcm_substream *sub = dev->sub_playback[stream]; | |
567 | char *audio_buf = NULL; | |
568 | int c, n, sz = 0; | |
569 | ||
570 | if (sub) { | |
571 | struct snd_pcm_runtime *rt = sub->runtime; | |
572 | audio_buf = rt->dma_area; | |
573 | sz = frames_to_bytes(rt, rt->buffer_size); | |
574 | } | |
575 | ||
576 | for (c = 0; c < CHANNELS_PER_STREAM; c++) { | |
577 | for (n = 0; n < BYTES_PER_SAMPLE; n++) { | |
578 | if (audio_buf) { | |
579 | usb_buf[i+n] = audio_buf[dev->audio_out_buf_pos[stream]++]; | |
580 | ||
581 | if (dev->audio_out_buf_pos[stream] == sz) | |
582 | dev->audio_out_buf_pos[stream] = 0; | |
583 | } else { | |
584 | usb_buf[i+n] = 0; | |
585 | } | |
586 | } | |
587 | ||
588 | if (audio_buf) | |
589 | dev->period_out_count[stream] += BYTES_PER_SAMPLE; | |
590 | ||
591 | i += BYTES_PER_SAMPLE; | |
592 | ||
593 | /* fill in the check byte pattern */ | |
594 | usb_buf[i++] = (stream << 1) | c; | |
595 | } | |
596 | } | |
597 | } | |
598 | } | |
599 | ||
600 | static inline void fill_out_urb(struct snd_usb_caiaqdev *dev, | |
601 | struct urb *urb, | |
602 | const struct usb_iso_packet_descriptor *iso) | |
603 | { | |
604 | switch (dev->spec.data_alignment) { | |
605 | case 0: | |
606 | case 2: | |
607 | fill_out_urb_mode_0(dev, urb, iso); | |
608 | break; | |
609 | case 3: | |
610 | fill_out_urb_mode_3(dev, urb, iso); | |
611 | break; | |
523f1dce | 612 | } |
523f1dce DM |
613 | } |
614 | ||
615 | static void read_completed(struct urb *urb) | |
616 | { | |
9318dce5 | 617 | struct snd_usb_caiaq_cb_info *info = urb->context; |
523f1dce | 618 | struct snd_usb_caiaqdev *dev; |
da6094ea DM |
619 | struct urb *out = NULL; |
620 | int i, frame, len, send_it = 0, outframe = 0; | |
15439bde | 621 | size_t offset = 0; |
523f1dce DM |
622 | |
623 | if (urb->status || !info) | |
624 | return; | |
625 | ||
626 | dev = info->dev; | |
8d048841 | 627 | |
523f1dce DM |
628 | if (!dev->streaming) |
629 | return; | |
630 | ||
da6094ea DM |
631 | /* find an unused output urb that is unused */ |
632 | for (i = 0; i < N_URBS; i++) | |
633 | if (test_and_set_bit(i, &dev->outurb_active_mask) == 0) { | |
634 | out = dev->data_urbs_out[i]; | |
635 | break; | |
636 | } | |
637 | ||
638 | if (!out) { | |
639 | log("Unable to find an output urb to use\n"); | |
640 | goto requeue; | |
641 | } | |
523f1dce DM |
642 | |
643 | /* read the recently received packet and send back one which has | |
644 | * the same layout */ | |
645 | for (frame = 0; frame < FRAMES_PER_URB; frame++) { | |
646 | if (urb->iso_frame_desc[frame].status) | |
647 | continue; | |
648 | ||
649 | len = urb->iso_frame_desc[outframe].actual_length; | |
650 | out->iso_frame_desc[outframe].length = len; | |
651 | out->iso_frame_desc[outframe].actual_length = 0; | |
15439bde DM |
652 | out->iso_frame_desc[outframe].offset = offset; |
653 | offset += len; | |
9318dce5 | 654 | |
523f1dce | 655 | if (len > 0) { |
8d048841 | 656 | spin_lock(&dev->spinlock); |
523f1dce DM |
657 | fill_out_urb(dev, out, &out->iso_frame_desc[outframe]); |
658 | read_in_urb(dev, urb, &urb->iso_frame_desc[frame]); | |
8d048841 DM |
659 | spin_unlock(&dev->spinlock); |
660 | check_for_elapsed_periods(dev, dev->sub_playback); | |
661 | check_for_elapsed_periods(dev, dev->sub_capture); | |
523f1dce DM |
662 | send_it = 1; |
663 | } | |
664 | ||
665 | outframe++; | |
666 | } | |
667 | ||
668 | if (send_it) { | |
15439bde | 669 | out->number_of_packets = outframe; |
523f1dce DM |
670 | out->transfer_flags = URB_ISO_ASAP; |
671 | usb_submit_urb(out, GFP_ATOMIC); | |
da6094ea DM |
672 | } else { |
673 | struct snd_usb_caiaq_cb_info *oinfo = out->context; | |
674 | clear_bit(oinfo->index, &dev->outurb_active_mask); | |
523f1dce | 675 | } |
9318dce5 | 676 | |
da6094ea | 677 | requeue: |
523f1dce DM |
678 | /* re-submit inbound urb */ |
679 | for (frame = 0; frame < FRAMES_PER_URB; frame++) { | |
680 | urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame; | |
681 | urb->iso_frame_desc[frame].length = BYTES_PER_FRAME; | |
682 | urb->iso_frame_desc[frame].actual_length = 0; | |
683 | } | |
9318dce5 | 684 | |
523f1dce DM |
685 | urb->number_of_packets = FRAMES_PER_URB; |
686 | urb->transfer_flags = URB_ISO_ASAP; | |
687 | usb_submit_urb(urb, GFP_ATOMIC); | |
688 | } | |
689 | ||
690 | static void write_completed(struct urb *urb) | |
691 | { | |
692 | struct snd_usb_caiaq_cb_info *info = urb->context; | |
693 | struct snd_usb_caiaqdev *dev = info->dev; | |
694 | ||
695 | if (!dev->output_running) { | |
696 | dev->output_running = 1; | |
697 | wake_up(&dev->prepare_wait_queue); | |
698 | } | |
da6094ea DM |
699 | |
700 | clear_bit(info->index, &dev->outurb_active_mask); | |
523f1dce DM |
701 | } |
702 | ||
703 | static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret) | |
704 | { | |
705 | int i, frame; | |
706 | struct urb **urbs; | |
707 | struct usb_device *usb_dev = dev->chip.dev; | |
708 | unsigned int pipe; | |
709 | ||
9318dce5 | 710 | pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ? |
523f1dce DM |
711 | usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) : |
712 | usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE); | |
713 | ||
714 | urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL); | |
715 | if (!urbs) { | |
716 | log("unable to kmalloc() urbs, OOM!?\n"); | |
717 | *ret = -ENOMEM; | |
718 | return NULL; | |
719 | } | |
720 | ||
721 | for (i = 0; i < N_URBS; i++) { | |
722 | urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL); | |
723 | if (!urbs[i]) { | |
724 | log("unable to usb_alloc_urb(), OOM!?\n"); | |
725 | *ret = -ENOMEM; | |
726 | return urbs; | |
727 | } | |
728 | ||
9318dce5 | 729 | urbs[i]->transfer_buffer = |
523f1dce DM |
730 | kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL); |
731 | if (!urbs[i]->transfer_buffer) { | |
732 | log("unable to kmalloc() transfer buffer, OOM!?\n"); | |
733 | *ret = -ENOMEM; | |
734 | return urbs; | |
735 | } | |
9318dce5 | 736 | |
523f1dce | 737 | for (frame = 0; frame < FRAMES_PER_URB; frame++) { |
9318dce5 | 738 | struct usb_iso_packet_descriptor *iso = |
523f1dce | 739 | &urbs[i]->iso_frame_desc[frame]; |
9318dce5 | 740 | |
523f1dce DM |
741 | iso->offset = BYTES_PER_FRAME * frame; |
742 | iso->length = BYTES_PER_FRAME; | |
743 | } | |
9318dce5 | 744 | |
523f1dce DM |
745 | urbs[i]->dev = usb_dev; |
746 | urbs[i]->pipe = pipe; | |
9318dce5 | 747 | urbs[i]->transfer_buffer_length = FRAMES_PER_URB |
523f1dce DM |
748 | * BYTES_PER_FRAME; |
749 | urbs[i]->context = &dev->data_cb_info[i]; | |
750 | urbs[i]->interval = 1; | |
751 | urbs[i]->transfer_flags = URB_ISO_ASAP; | |
752 | urbs[i]->number_of_packets = FRAMES_PER_URB; | |
753 | urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ? | |
754 | read_completed : write_completed; | |
755 | } | |
756 | ||
757 | *ret = 0; | |
758 | return urbs; | |
759 | } | |
760 | ||
761 | static void free_urbs(struct urb **urbs) | |
762 | { | |
763 | int i; | |
764 | ||
765 | if (!urbs) | |
766 | return; | |
767 | ||
768 | for (i = 0; i < N_URBS; i++) { | |
769 | if (!urbs[i]) | |
770 | continue; | |
9318dce5 | 771 | |
523f1dce DM |
772 | usb_kill_urb(urbs[i]); |
773 | kfree(urbs[i]->transfer_buffer); | |
774 | usb_free_urb(urbs[i]); | |
775 | } | |
776 | ||
777 | kfree(urbs); | |
778 | } | |
779 | ||
599c3e76 | 780 | int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev) |
523f1dce DM |
781 | { |
782 | int i, ret; | |
783 | ||
9318dce5 DM |
784 | dev->n_audio_in = max(dev->spec.num_analog_audio_in, |
785 | dev->spec.num_digital_audio_in) / | |
523f1dce DM |
786 | CHANNELS_PER_STREAM; |
787 | dev->n_audio_out = max(dev->spec.num_analog_audio_out, | |
9318dce5 | 788 | dev->spec.num_digital_audio_out) / |
523f1dce DM |
789 | CHANNELS_PER_STREAM; |
790 | dev->n_streams = max(dev->n_audio_in, dev->n_audio_out); | |
791 | ||
792 | debug("dev->n_audio_in = %d\n", dev->n_audio_in); | |
793 | debug("dev->n_audio_out = %d\n", dev->n_audio_out); | |
794 | debug("dev->n_streams = %d\n", dev->n_streams); | |
795 | ||
796 | if (dev->n_streams > MAX_STREAMS) { | |
797 | log("unable to initialize device, too many streams.\n"); | |
798 | return -EINVAL; | |
799 | } | |
800 | ||
9318dce5 | 801 | ret = snd_pcm_new(dev->chip.card, dev->product_name, 0, |
523f1dce DM |
802 | dev->n_audio_out, dev->n_audio_in, &dev->pcm); |
803 | ||
804 | if (ret < 0) { | |
805 | log("snd_pcm_new() returned %d\n", ret); | |
806 | return ret; | |
807 | } | |
808 | ||
809 | dev->pcm->private_data = dev; | |
eaae55da | 810 | strlcpy(dev->pcm->name, dev->product_name, sizeof(dev->pcm->name)); |
523f1dce DM |
811 | |
812 | memset(dev->sub_playback, 0, sizeof(dev->sub_playback)); | |
813 | memset(dev->sub_capture, 0, sizeof(dev->sub_capture)); | |
9318dce5 | 814 | |
523f1dce DM |
815 | memcpy(&dev->pcm_info, &snd_usb_caiaq_pcm_hardware, |
816 | sizeof(snd_usb_caiaq_pcm_hardware)); | |
817 | ||
818 | /* setup samplerates */ | |
819 | dev->samplerates = dev->pcm_info.rates; | |
820 | switch (dev->chip.usb_id) { | |
821 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1): | |
ad1e34b5 | 822 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3): |
f3e9d5d1 | 823 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO): |
2165592b | 824 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE): |
523f1dce | 825 | dev->samplerates |= SNDRV_PCM_RATE_192000; |
2165592b | 826 | /* fall thru */ |
b30c4947 | 827 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ): |
2165592b | 828 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ): |
523f1dce | 829 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ): |
df8d81a3 | 830 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2): |
523f1dce DM |
831 | dev->samplerates |= SNDRV_PCM_RATE_88200; |
832 | break; | |
833 | } | |
834 | ||
9318dce5 | 835 | snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, |
523f1dce | 836 | &snd_usb_caiaq_ops); |
9318dce5 | 837 | snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, |
523f1dce DM |
838 | &snd_usb_caiaq_ops); |
839 | ||
840 | snd_pcm_lib_preallocate_pages_for_all(dev->pcm, | |
841 | SNDRV_DMA_TYPE_CONTINUOUS, | |
842 | snd_dma_continuous_data(GFP_KERNEL), | |
843 | MAX_BUFFER_SIZE, MAX_BUFFER_SIZE); | |
844 | ||
845 | dev->data_cb_info = | |
9318dce5 | 846 | kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS, |
523f1dce DM |
847 | GFP_KERNEL); |
848 | ||
849 | if (!dev->data_cb_info) | |
850 | return -ENOMEM; | |
851 | ||
da6094ea DM |
852 | dev->outurb_active_mask = 0; |
853 | BUILD_BUG_ON(N_URBS > (sizeof(dev->outurb_active_mask) * 8)); | |
854 | ||
523f1dce DM |
855 | for (i = 0; i < N_URBS; i++) { |
856 | dev->data_cb_info[i].dev = dev; | |
857 | dev->data_cb_info[i].index = i; | |
858 | } | |
9318dce5 | 859 | |
523f1dce DM |
860 | dev->data_urbs_in = alloc_urbs(dev, SNDRV_PCM_STREAM_CAPTURE, &ret); |
861 | if (ret < 0) { | |
862 | kfree(dev->data_cb_info); | |
863 | free_urbs(dev->data_urbs_in); | |
864 | return ret; | |
865 | } | |
9318dce5 | 866 | |
523f1dce DM |
867 | dev->data_urbs_out = alloc_urbs(dev, SNDRV_PCM_STREAM_PLAYBACK, &ret); |
868 | if (ret < 0) { | |
869 | kfree(dev->data_cb_info); | |
870 | free_urbs(dev->data_urbs_in); | |
871 | free_urbs(dev->data_urbs_out); | |
872 | return ret; | |
873 | } | |
874 | ||
875 | return 0; | |
876 | } | |
877 | ||
878 | void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *dev) | |
879 | { | |
8d048841 | 880 | debug("%s(%p)\n", __func__, dev); |
523f1dce DM |
881 | stream_stop(dev); |
882 | free_urbs(dev->data_urbs_in); | |
883 | free_urbs(dev->data_urbs_out); | |
884 | kfree(dev->data_cb_info); | |
885 | } | |
886 |