20 #include <linux/slab.h>
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)
37 #define MAX_ENDPOINT_SIZE 512
39 #define ENDPOINT_CAPTURE 2
40 #define ENDPOINT_PLAYBACK 6
42 #define MAKE_CHECKBYTE(dev,stream,i) \
43 (stream << 1) | (~(i / (dev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
56 .period_bytes_min = 128,
88 spin_unlock_irqrestore(&dev->
spinlock, flags);
105 debug(
"%s(%p)\n", __func__, dev);
118 for (i = 0; i <
N_URBS; i++) {
121 log(
"unable to trigger read #%d! (ret %d)\n", i, ret);
134 debug(
"%s(%p)\n", __func__, dev);
140 for (i = 0; i <
N_URBS; i++) {
153 debug(
"%s(%p)\n", __func__, substream);
163 debug(
"%s(%p)\n", __func__, substream);
178 debug(
"%s(%p)\n", __func__, sub);
185 debug(
"%s(%p)\n", __func__, sub);
186 deactivate_substream(dev, sub);
191 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
192 #error "Change this table"
195 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
196 48000, 64000, 88200, 96000, 176400, 192000 };
205 debug(
"%s(%p)\n", __func__, substream);
210 switch (dev->
spec.data_alignment) {
226 switch (dev->
spec.data_alignment) {
249 if (runtime->
rate == rates[i])
255 if (dev->
spec.data_alignment >= 2)
269 ret = stream_start(dev);
287 debug(
"%s(%p) cmd %d\n", __func__, sub, cmd);
292 activate_substream(dev, sub);
296 deactivate_substream(dev, sub);
320 ptr = bytes_to_frames(sub->
runtime,
323 ptr = bytes_to_frames(sub->
runtime,
333 .open = snd_usb_caiaq_substream_open,
334 .close = snd_usb_caiaq_substream_close,
336 .hw_params = snd_usb_caiaq_pcm_hw_params,
337 .hw_free = snd_usb_caiaq_pcm_hw_free,
338 .prepare = snd_usb_caiaq_pcm_prepare,
339 .trigger = snd_usb_caiaq_pcm_trigger,
340 .pointer = snd_usb_caiaq_pcm_pointer
349 for (stream = 0; stream < dev->
n_streams; stream++) {
354 pb = snd_pcm_lib_period_bytes(sub);
368 const struct usb_iso_packet_descriptor *iso)
370 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
377 for (i = 0; i < iso->actual_length;) {
378 for (stream = 0; stream < dev->
n_streams; stream++, i++) {
395 const struct urb *urb,
396 const struct usb_iso_packet_descriptor *iso)
398 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
399 unsigned char check_byte;
403 for (i = 0; i < iso->actual_length;) {
413 if ((usb_buf[i] & 0x3f) != check_byte)
416 if (usb_buf[i] & 0x80)
422 for (stream = 0; stream < dev->
n_streams; stream++, i++) {
442 const struct urb *urb,
443 const struct usb_iso_packet_descriptor *iso)
445 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
452 for (i = 0; i < iso->actual_length;) {
453 for (stream = 0; stream < dev->
n_streams; stream++) {
455 char *audio_buf =
NULL;
479 if (usb_buf[i] != ((stream << 1) | c) &&
482 printk(
" EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
483 ((stream << 1) | c), usb_buf[i], c, stream, i);
497 const struct urb *urb,
498 const struct usb_iso_packet_descriptor *iso)
503 if (iso->actual_length < dev->
bpp)
506 switch (dev->
spec.data_alignment) {
508 read_in_urb_mode0(dev, urb, iso);
511 read_in_urb_mode2(dev, urb, iso);
514 read_in_urb_mode3(dev, urb, iso);
519 debug(
"streaming error detected %s %s\n",
528 const struct usb_iso_packet_descriptor *iso)
530 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
534 for (i = 0; i < iso->length;) {
535 for (stream = 0; stream < dev->
n_streams; stream++, i++) {
552 if (dev->
spec.data_alignment == 2 &&
555 for (stream = 0; stream < dev->
n_streams; stream++, i++)
562 const struct usb_iso_packet_descriptor *iso)
564 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
567 for (i = 0; i < iso->length;) {
568 for (stream = 0; stream < dev->
n_streams; stream++) {
570 char *audio_buf =
NULL;
597 usb_buf[i++] = (stream << 1) | c;
605 const struct usb_iso_packet_descriptor *iso)
607 switch (dev->
spec.data_alignment) {
610 fill_out_urb_mode_0(dev, urb, iso);
613 fill_out_urb_mode_3(dev, urb, iso);
618 static void read_completed(
struct urb *urb)
623 int i,
frame,
len, send_it = 0, outframe = 0;
626 if (urb->status || !info)
635 for (i = 0; i <
N_URBS; i++)
642 log(
"Unable to find an output urb to use\n");
649 if (urb->iso_frame_desc[frame].status)
652 len = urb->iso_frame_desc[outframe].actual_length;
653 out->iso_frame_desc[outframe].length = len;
654 out->iso_frame_desc[outframe].actual_length = 0;
655 out->iso_frame_desc[outframe].offset =
offset;
660 fill_out_urb(dev, out, &out->iso_frame_desc[outframe]);
661 read_in_urb(dev, urb, &urb->iso_frame_desc[frame]);
672 out->number_of_packets = outframe;
673 out->transfer_flags = URB_ISO_ASAP;
685 urb->iso_frame_desc[
frame].actual_length = 0;
689 urb->transfer_flags = URB_ISO_ASAP;
693 static void write_completed(
struct urb *urb)
706 static struct urb **alloc_urbs(
struct snd_usb_caiaqdev *dev,
int dir,
int *ret)
710 struct usb_device *usb_dev = dev->
chip.dev;
719 log(
"unable to kmalloc() urbs, OOM!?\n");
724 for (i = 0; i <
N_URBS; i++) {
727 log(
"unable to usb_alloc_urb(), OOM!?\n");
732 urbs[
i]->transfer_buffer =
735 log(
"unable to kmalloc() transfer buffer, OOM!?\n");
741 struct usb_iso_packet_descriptor *iso =
742 &urbs[
i]->iso_frame_desc[
frame];
748 urbs[
i]->dev = usb_dev;
749 urbs[
i]->pipe =
pipe;
750 urbs[
i]->transfer_buffer_length = FRAMES_PER_URB
753 urbs[
i]->interval = 1;
754 urbs[
i]->transfer_flags = URB_ISO_ASAP;
757 read_completed : write_completed;
764 static void free_urbs(
struct urb **urbs)
771 for (i = 0; i <
N_URBS; i++) {
788 dev->
spec.num_digital_audio_in) /
791 dev->
spec.num_digital_audio_out) /
800 log(
"unable to initialize device, too many streams.\n");
808 log(
"snd_pcm_new() returned %d\n", ret);
812 dev->
pcm->private_data =
dev;
819 sizeof(snd_usb_caiaq_pcm_hardware));
823 switch (dev->
chip.usb_id) {
858 for (i = 0; i <
N_URBS; i++) {
883 debug(
"%s(%p)\n", __func__, dev);