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