aboutsummaryrefslogtreecommitdiffstats
path: root/sound/usb
diff options
context:
space:
mode:
Diffstat (limited to 'sound/usb')
-rw-r--r--sound/usb/6fire/firmware.c25
-rw-r--r--sound/usb/Kconfig2
-rw-r--r--sound/usb/Makefile12
-rw-r--r--sound/usb/caiaq/audio.c37
-rw-r--r--sound/usb/caiaq/device.c8
-rw-r--r--sound/usb/caiaq/device.h2
-rw-r--r--sound/usb/caiaq/input.c157
-rw-r--r--sound/usb/card.c11
-rw-r--r--sound/usb/card.h2
-rw-r--r--sound/usb/clock.c12
-rw-r--r--sound/usb/endpoint.c1199
-rw-r--r--sound/usb/endpoint.h20
-rw-r--r--sound/usb/format.c4
-rw-r--r--sound/usb/helper.c4
-rw-r--r--sound/usb/helper.h2
-rw-r--r--sound/usb/midi.c27
-rw-r--r--sound/usb/mixer.c49
-rw-r--r--sound/usb/mixer.h1
-rw-r--r--sound/usb/mixer_quirks.c10
-rw-r--r--sound/usb/pcm.c34
-rw-r--r--sound/usb/pcm.h3
-rw-r--r--sound/usb/quirks-table.h65
-rw-r--r--sound/usb/quirks.c18
-rw-r--r--sound/usb/stream.c452
-rw-r--r--sound/usb/stream.h12
-rw-r--r--sound/usb/urb.c941
-rw-r--r--sound/usb/urb.h21
-rw-r--r--sound/usb/usbaudio.h1
28 files changed, 1743 insertions, 1388 deletions
diff --git a/sound/usb/6fire/firmware.c b/sound/usb/6fire/firmware.c
index 1e3ae3327dd3..07bcfe4d18a7 100644
--- a/sound/usb/6fire/firmware.c
+++ b/sound/usb/6fire/firmware.c
@@ -16,6 +16,7 @@
16 16
17#include <linux/firmware.h> 17#include <linux/firmware.h>
18#include <linux/bitrev.h> 18#include <linux/bitrev.h>
19#include <linux/kernel.h>
19 20
20#include "firmware.h" 21#include "firmware.h"
21#include "chip.h" 22#include "chip.h"
@@ -59,21 +60,19 @@ struct ihex_record {
59 unsigned int txt_offset; /* current position in txt_data */ 60 unsigned int txt_offset; /* current position in txt_data */
60}; 61};
61 62
62static u8 usb6fire_fw_ihex_nibble(const u8 n)
63{
64 if (n >= '0' && n <= '9')
65 return n - '0';
66 else if (n >= 'A' && n <= 'F')
67 return n - ('A' - 10);
68 else if (n >= 'a' && n <= 'f')
69 return n - ('a' - 10);
70 return 0;
71}
72
73static u8 usb6fire_fw_ihex_hex(const u8 *data, u8 *crc) 63static u8 usb6fire_fw_ihex_hex(const u8 *data, u8 *crc)
74{ 64{
75 u8 val = (usb6fire_fw_ihex_nibble(data[0]) << 4) | 65 u8 val = 0;
76 usb6fire_fw_ihex_nibble(data[1]); 66 int hval;
67
68 hval = hex_to_bin(data[0]);
69 if (hval >= 0)
70 val |= (hval << 4);
71
72 hval = hex_to_bin(data[1]);
73 if (hval >= 0)
74 val |= hval;
75
77 *crc += val; 76 *crc += val;
78 return val; 77 return val;
79} 78}
diff --git a/sound/usb/Kconfig b/sound/usb/Kconfig
index 8beb77563da2..3efc21c3d67c 100644
--- a/sound/usb/Kconfig
+++ b/sound/usb/Kconfig
@@ -67,6 +67,7 @@ config SND_USB_CAIAQ
67 * Native Instruments Guitar Rig mobile 67 * Native Instruments Guitar Rig mobile
68 * Native Instruments Traktor Kontrol X1 68 * Native Instruments Traktor Kontrol X1
69 * Native Instruments Traktor Kontrol S4 69 * Native Instruments Traktor Kontrol S4
70 * Native Instruments Maschine Controller
70 71
71 To compile this driver as a module, choose M here: the module 72 To compile this driver as a module, choose M here: the module
72 will be called snd-usb-caiaq. 73 will be called snd-usb-caiaq.
@@ -85,6 +86,7 @@ config SND_USB_CAIAQ_INPUT
85 * Native Instruments Kore Controller 2 86 * Native Instruments Kore Controller 2
86 * Native Instruments Audio Kontrol 1 87 * Native Instruments Audio Kontrol 1
87 * Native Instruments Traktor Kontrol S4 88 * Native Instruments Traktor Kontrol S4
89 * Native Instruments Maschine Controller
88 90
89config SND_USB_US122L 91config SND_USB_US122L
90 tristate "Tascam US-122L USB driver" 92 tristate "Tascam US-122L USB driver"
diff --git a/sound/usb/Makefile b/sound/usb/Makefile
index cf9ed66445fa..ac256dc4c6be 100644
--- a/sound/usb/Makefile
+++ b/sound/usb/Makefile
@@ -3,16 +3,16 @@
3# 3#
4 4
5snd-usb-audio-objs := card.o \ 5snd-usb-audio-objs := card.o \
6 clock.o \
7 endpoint.o \
8 format.o \
9 helper.o \
6 mixer.o \ 10 mixer.o \
7 mixer_quirks.o \ 11 mixer_quirks.o \
12 pcm.o \
8 proc.o \ 13 proc.o \
9 quirks.o \ 14 quirks.o \
10 format.o \ 15 stream.o
11 endpoint.o \
12 urb.o \
13 pcm.o \
14 helper.o \
15 clock.o
16 16
17snd-usbmidi-lib-objs := midi.o 17snd-usbmidi-lib-objs := midi.o
18 18
diff --git a/sound/usb/caiaq/audio.c b/sound/usb/caiaq/audio.c
index d0d493ca28ae..2cf87f5afed4 100644
--- a/sound/usb/caiaq/audio.c
+++ b/sound/usb/caiaq/audio.c
@@ -139,8 +139,12 @@ static void stream_stop(struct snd_usb_caiaqdev *dev)
139 139
140 for (i = 0; i < N_URBS; i++) { 140 for (i = 0; i < N_URBS; i++) {
141 usb_kill_urb(dev->data_urbs_in[i]); 141 usb_kill_urb(dev->data_urbs_in[i]);
142 usb_kill_urb(dev->data_urbs_out[i]); 142
143 if (test_bit(i, &dev->outurb_active_mask))
144 usb_kill_urb(dev->data_urbs_out[i]);
143 } 145 }
146
147 dev->outurb_active_mask = 0;
144} 148}
145 149
146static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream) 150static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
@@ -612,8 +616,9 @@ static void read_completed(struct urb *urb)
612{ 616{
613 struct snd_usb_caiaq_cb_info *info = urb->context; 617 struct snd_usb_caiaq_cb_info *info = urb->context;
614 struct snd_usb_caiaqdev *dev; 618 struct snd_usb_caiaqdev *dev;
615 struct urb *out; 619 struct urb *out = NULL;
616 int frame, len, send_it = 0, outframe = 0; 620 int i, frame, len, send_it = 0, outframe = 0;
621 size_t offset = 0;
617 622
618 if (urb->status || !info) 623 if (urb->status || !info)
619 return; 624 return;
@@ -623,7 +628,17 @@ static void read_completed(struct urb *urb)
623 if (!dev->streaming) 628 if (!dev->streaming)
624 return; 629 return;
625 630
626 out = dev->data_urbs_out[info->index]; 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 }
627 642
628 /* read the recently received packet and send back one which has 643 /* read the recently received packet and send back one which has
629 * the same layout */ 644 * the same layout */
@@ -634,7 +649,8 @@ static void read_completed(struct urb *urb)
634 len = urb->iso_frame_desc[outframe].actual_length; 649 len = urb->iso_frame_desc[outframe].actual_length;
635 out->iso_frame_desc[outframe].length = len; 650 out->iso_frame_desc[outframe].length = len;
636 out->iso_frame_desc[outframe].actual_length = 0; 651 out->iso_frame_desc[outframe].actual_length = 0;
637 out->iso_frame_desc[outframe].offset = BYTES_PER_FRAME * frame; 652 out->iso_frame_desc[outframe].offset = offset;
653 offset += len;
638 654
639 if (len > 0) { 655 if (len > 0) {
640 spin_lock(&dev->spinlock); 656 spin_lock(&dev->spinlock);
@@ -650,11 +666,15 @@ static void read_completed(struct urb *urb)
650 } 666 }
651 667
652 if (send_it) { 668 if (send_it) {
653 out->number_of_packets = FRAMES_PER_URB; 669 out->number_of_packets = outframe;
654 out->transfer_flags = URB_ISO_ASAP; 670 out->transfer_flags = URB_ISO_ASAP;
655 usb_submit_urb(out, GFP_ATOMIC); 671 usb_submit_urb(out, GFP_ATOMIC);
672 } else {
673 struct snd_usb_caiaq_cb_info *oinfo = out->context;
674 clear_bit(oinfo->index, &dev->outurb_active_mask);
656 } 675 }
657 676
677requeue:
658 /* re-submit inbound urb */ 678 /* re-submit inbound urb */
659 for (frame = 0; frame < FRAMES_PER_URB; frame++) { 679 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
660 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame; 680 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
@@ -676,6 +696,8 @@ static void write_completed(struct urb *urb)
676 dev->output_running = 1; 696 dev->output_running = 1;
677 wake_up(&dev->prepare_wait_queue); 697 wake_up(&dev->prepare_wait_queue);
678 } 698 }
699
700 clear_bit(info->index, &dev->outurb_active_mask);
679} 701}
680 702
681static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret) 703static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret)
@@ -827,6 +849,9 @@ int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev)
827 if (!dev->data_cb_info) 849 if (!dev->data_cb_info)
828 return -ENOMEM; 850 return -ENOMEM;
829 851
852 dev->outurb_active_mask = 0;
853 BUILD_BUG_ON(N_URBS > (sizeof(dev->outurb_active_mask) * 8));
854
830 for (i = 0; i < N_URBS; i++) { 855 for (i = 0; i < N_URBS; i++) {
831 dev->data_cb_info[i].dev = dev; 856 dev->data_cb_info[i].dev = dev;
832 dev->data_cb_info[i].index = i; 857 dev->data_cb_info[i].index = i;
diff --git a/sound/usb/caiaq/device.c b/sound/usb/caiaq/device.c
index 45bc4a2dc6f0..3eb605bd9503 100644
--- a/sound/usb/caiaq/device.c
+++ b/sound/usb/caiaq/device.c
@@ -50,7 +50,8 @@ MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2},"
50 "{Native Instruments, Session I/O}," 50 "{Native Instruments, Session I/O},"
51 "{Native Instruments, GuitarRig mobile}" 51 "{Native Instruments, GuitarRig mobile}"
52 "{Native Instruments, Traktor Kontrol X1}" 52 "{Native Instruments, Traktor Kontrol X1}"
53 "{Native Instruments, Traktor Kontrol S4}"); 53 "{Native Instruments, Traktor Kontrol S4}"
54 "{Native Instruments, Maschine Controller}");
54 55
55static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */ 56static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
56static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */ 57static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
@@ -146,6 +147,11 @@ static struct usb_device_id snd_usb_id_table[] = {
146 .idVendor = USB_VID_NATIVEINSTRUMENTS, 147 .idVendor = USB_VID_NATIVEINSTRUMENTS,
147 .idProduct = USB_PID_TRAKTORAUDIO2 148 .idProduct = USB_PID_TRAKTORAUDIO2
148 }, 149 },
150 {
151 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
152 .idVendor = USB_VID_NATIVEINSTRUMENTS,
153 .idProduct = USB_PID_MASCHINECONTROLLER
154 },
149 { /* terminator */ } 155 { /* terminator */ }
150}; 156};
151 157
diff --git a/sound/usb/caiaq/device.h b/sound/usb/caiaq/device.h
index b2b310194ffa..562b0bff9c41 100644
--- a/sound/usb/caiaq/device.h
+++ b/sound/usb/caiaq/device.h
@@ -18,6 +18,7 @@
18#define USB_PID_TRAKTORKONTROLX1 0x2305 18#define USB_PID_TRAKTORKONTROLX1 0x2305
19#define USB_PID_TRAKTORKONTROLS4 0xbaff 19#define USB_PID_TRAKTORKONTROLS4 0xbaff
20#define USB_PID_TRAKTORAUDIO2 0x041d 20#define USB_PID_TRAKTORAUDIO2 0x041d
21#define USB_PID_MASCHINECONTROLLER 0x0808
21 22
22#define EP1_BUFSIZE 64 23#define EP1_BUFSIZE 64
23#define EP4_BUFSIZE 512 24#define EP4_BUFSIZE 512
@@ -96,6 +97,7 @@ struct snd_usb_caiaqdev {
96 int input_panic, output_panic, warned; 97 int input_panic, output_panic, warned;
97 char *audio_in_buf, *audio_out_buf; 98 char *audio_in_buf, *audio_out_buf;
98 unsigned int samplerates, bpp; 99 unsigned int samplerates, bpp;
100 unsigned long outurb_active_mask;
99 101
100 struct snd_pcm_substream *sub_playback[MAX_STREAMS]; 102 struct snd_pcm_substream *sub_playback[MAX_STREAMS];
101 struct snd_pcm_substream *sub_capture[MAX_STREAMS]; 103 struct snd_pcm_substream *sub_capture[MAX_STREAMS];
diff --git a/sound/usb/caiaq/input.c b/sound/usb/caiaq/input.c
index 4432ef7a70a9..26a121b42c3c 100644
--- a/sound/usb/caiaq/input.c
+++ b/sound/usb/caiaq/input.c
@@ -30,7 +30,7 @@ static unsigned short keycode_ak1[] = { KEY_C, KEY_B, KEY_A };
30static unsigned short keycode_rk2[] = { KEY_1, KEY_2, KEY_3, KEY_4, 30static unsigned short keycode_rk2[] = { KEY_1, KEY_2, KEY_3, KEY_4,
31 KEY_5, KEY_6, KEY_7 }; 31 KEY_5, KEY_6, KEY_7 };
32static unsigned short keycode_rk3[] = { KEY_1, KEY_2, KEY_3, KEY_4, 32static unsigned short keycode_rk3[] = { KEY_1, KEY_2, KEY_3, KEY_4,
33 KEY_5, KEY_6, KEY_7, KEY_5, KEY_6 }; 33 KEY_5, KEY_6, KEY_7, KEY_8, KEY_9 };
34 34
35static unsigned short keycode_kore[] = { 35static unsigned short keycode_kore[] = {
36 KEY_FN_F1, /* "menu" */ 36 KEY_FN_F1, /* "menu" */
@@ -67,6 +67,61 @@ static unsigned short keycode_kore[] = {
67 KEY_BRL_DOT5 67 KEY_BRL_DOT5
68}; 68};
69 69
70#define MASCHINE_BUTTONS (42)
71#define MASCHINE_BUTTON(X) ((X) + BTN_MISC)
72#define MASCHINE_PADS (16)
73#define MASCHINE_PAD(X) ((X) + ABS_PRESSURE)
74
75static unsigned short keycode_maschine[] = {
76 MASCHINE_BUTTON(40), /* mute */
77 MASCHINE_BUTTON(39), /* solo */
78 MASCHINE_BUTTON(38), /* select */
79 MASCHINE_BUTTON(37), /* duplicate */
80 MASCHINE_BUTTON(36), /* navigate */
81 MASCHINE_BUTTON(35), /* pad mode */
82 MASCHINE_BUTTON(34), /* pattern */
83 MASCHINE_BUTTON(33), /* scene */
84 KEY_RESERVED, /* spacer */
85
86 MASCHINE_BUTTON(30), /* rec */
87 MASCHINE_BUTTON(31), /* erase */
88 MASCHINE_BUTTON(32), /* shift */
89 MASCHINE_BUTTON(28), /* grid */
90 MASCHINE_BUTTON(27), /* > */
91 MASCHINE_BUTTON(26), /* < */
92 MASCHINE_BUTTON(25), /* restart */
93
94 MASCHINE_BUTTON(21), /* E */
95 MASCHINE_BUTTON(22), /* F */
96 MASCHINE_BUTTON(23), /* G */
97 MASCHINE_BUTTON(24), /* H */
98 MASCHINE_BUTTON(20), /* D */
99 MASCHINE_BUTTON(19), /* C */
100 MASCHINE_BUTTON(18), /* B */
101 MASCHINE_BUTTON(17), /* A */
102
103 MASCHINE_BUTTON(0), /* control */
104 MASCHINE_BUTTON(2), /* browse */
105 MASCHINE_BUTTON(4), /* < */
106 MASCHINE_BUTTON(6), /* snap */
107 MASCHINE_BUTTON(7), /* autowrite */
108 MASCHINE_BUTTON(5), /* > */
109 MASCHINE_BUTTON(3), /* sampling */
110 MASCHINE_BUTTON(1), /* step */
111
112 MASCHINE_BUTTON(15), /* 8 softkeys */
113 MASCHINE_BUTTON(14),
114 MASCHINE_BUTTON(13),
115 MASCHINE_BUTTON(12),
116 MASCHINE_BUTTON(11),
117 MASCHINE_BUTTON(10),
118 MASCHINE_BUTTON(9),
119 MASCHINE_BUTTON(8),
120
121 MASCHINE_BUTTON(16), /* note repeat */
122 MASCHINE_BUTTON(29) /* play */
123};
124
70#define KONTROLX1_INPUTS (40) 125#define KONTROLX1_INPUTS (40)
71#define KONTROLS4_BUTTONS (12 * 8) 126#define KONTROLS4_BUTTONS (12 * 8)
72#define KONTROLS4_AXIS (46) 127#define KONTROLS4_AXIS (46)
@@ -218,6 +273,29 @@ static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
218 input_report_abs(input_dev, ABS_HAT3Y, i); 273 input_report_abs(input_dev, ABS_HAT3Y, i);
219 input_sync(input_dev); 274 input_sync(input_dev);
220 break; 275 break;
276
277 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_MASCHINECONTROLLER):
278 /* 4 under the left screen */
279 input_report_abs(input_dev, ABS_HAT0X, decode_erp(buf[21], buf[20]));
280 input_report_abs(input_dev, ABS_HAT0Y, decode_erp(buf[15], buf[14]));
281 input_report_abs(input_dev, ABS_HAT1X, decode_erp(buf[9], buf[8]));
282 input_report_abs(input_dev, ABS_HAT1Y, decode_erp(buf[3], buf[2]));
283
284 /* 4 under the right screen */
285 input_report_abs(input_dev, ABS_HAT2X, decode_erp(buf[19], buf[18]));
286 input_report_abs(input_dev, ABS_HAT2Y, decode_erp(buf[13], buf[12]));
287 input_report_abs(input_dev, ABS_HAT3X, decode_erp(buf[7], buf[6]));
288 input_report_abs(input_dev, ABS_HAT3Y, decode_erp(buf[1], buf[0]));
289
290 /* volume */
291 input_report_abs(input_dev, ABS_RX, decode_erp(buf[17], buf[16]));
292 /* tempo */
293 input_report_abs(input_dev, ABS_RY, decode_erp(buf[11], buf[10]));
294 /* swing */
295 input_report_abs(input_dev, ABS_RZ, decode_erp(buf[5], buf[4]));
296
297 input_sync(input_dev);
298 break;
221 } 299 }
222} 300}
223 301
@@ -400,6 +478,25 @@ static void snd_usb_caiaq_tks4_dispatch(struct snd_usb_caiaqdev *dev,
400 input_sync(dev->input_dev); 478 input_sync(dev->input_dev);
401} 479}
402 480
481#define MASCHINE_MSGBLOCK_SIZE 2
482
483static void snd_usb_caiaq_maschine_dispatch(struct snd_usb_caiaqdev *dev,
484 const unsigned char *buf,
485 unsigned int len)
486{
487 unsigned int i, pad_id;
488 uint16_t pressure;
489
490 for (i = 0; i < MASCHINE_PADS; i++) {
491 pressure = be16_to_cpu(buf[i * 2] << 8 | buf[(i * 2) + 1]);
492 pad_id = pressure >> 12;
493
494 input_report_abs(dev->input_dev, MASCHINE_PAD(pad_id), pressure & 0xfff);
495 }
496
497 input_sync(dev->input_dev);
498}
499
403static void snd_usb_caiaq_ep4_reply_dispatch(struct urb *urb) 500static void snd_usb_caiaq_ep4_reply_dispatch(struct urb *urb)
404{ 501{
405 struct snd_usb_caiaqdev *dev = urb->context; 502 struct snd_usb_caiaqdev *dev = urb->context;
@@ -425,6 +522,13 @@ static void snd_usb_caiaq_ep4_reply_dispatch(struct urb *urb)
425 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4): 522 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4):
426 snd_usb_caiaq_tks4_dispatch(dev, buf, urb->actual_length); 523 snd_usb_caiaq_tks4_dispatch(dev, buf, urb->actual_length);
427 break; 524 break;
525
526 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_MASCHINECONTROLLER):
527 if (urb->actual_length < (MASCHINE_PADS * MASCHINE_MSGBLOCK_SIZE))
528 goto requeue;
529
530 snd_usb_caiaq_maschine_dispatch(dev, buf, urb->actual_length);
531 break;
428 } 532 }
429 533
430requeue: 534requeue:
@@ -444,6 +548,7 @@ static int snd_usb_caiaq_input_open(struct input_dev *idev)
444 switch (dev->chip.usb_id) { 548 switch (dev->chip.usb_id) {
445 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1): 549 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
446 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4): 550 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4):
551 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_MASCHINECONTROLLER):
447 if (usb_submit_urb(dev->ep4_in_urb, GFP_KERNEL) != 0) 552 if (usb_submit_urb(dev->ep4_in_urb, GFP_KERNEL) != 0)
448 return -EIO; 553 return -EIO;
449 break; 554 break;
@@ -462,6 +567,7 @@ static void snd_usb_caiaq_input_close(struct input_dev *idev)
462 switch (dev->chip.usb_id) { 567 switch (dev->chip.usb_id) {
463 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1): 568 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
464 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4): 569 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4):
570 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_MASCHINECONTROLLER):
465 usb_kill_urb(dev->ep4_in_urb); 571 usb_kill_urb(dev->ep4_in_urb);
466 break; 572 break;
467 } 573 }
@@ -652,6 +758,50 @@ int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev)
652 758
653 break; 759 break;
654 760
761 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_MASCHINECONTROLLER):
762 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
763 input->absbit[0] = BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
764 BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y) |
765 BIT_MASK(ABS_HAT2X) | BIT_MASK(ABS_HAT2Y) |
766 BIT_MASK(ABS_HAT3X) | BIT_MASK(ABS_HAT3Y) |
767 BIT_MASK(ABS_RX) | BIT_MASK(ABS_RY) |
768 BIT_MASK(ABS_RZ);
769
770 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_maschine));
771 memcpy(dev->keycode, keycode_maschine, sizeof(keycode_maschine));
772 input->keycodemax = ARRAY_SIZE(keycode_maschine);
773
774 for (i = 0; i < MASCHINE_PADS; i++) {
775 input->absbit[0] |= MASCHINE_PAD(i);
776 input_set_abs_params(input, MASCHINE_PAD(i), 0, 0xfff, 5, 10);
777 }
778
779 input_set_abs_params(input, ABS_HAT0X, 0, 999, 0, 10);
780 input_set_abs_params(input, ABS_HAT0Y, 0, 999, 0, 10);
781 input_set_abs_params(input, ABS_HAT1X, 0, 999, 0, 10);
782 input_set_abs_params(input, ABS_HAT1Y, 0, 999, 0, 10);
783 input_set_abs_params(input, ABS_HAT2X, 0, 999, 0, 10);
784 input_set_abs_params(input, ABS_HAT2Y, 0, 999, 0, 10);
785 input_set_abs_params(input, ABS_HAT3X, 0, 999, 0, 10);
786 input_set_abs_params(input, ABS_HAT3Y, 0, 999, 0, 10);
787 input_set_abs_params(input, ABS_RX, 0, 999, 0, 10);
788 input_set_abs_params(input, ABS_RY, 0, 999, 0, 10);
789 input_set_abs_params(input, ABS_RZ, 0, 999, 0, 10);
790
791 dev->ep4_in_urb = usb_alloc_urb(0, GFP_KERNEL);
792 if (!dev->ep4_in_urb) {
793 ret = -ENOMEM;
794 goto exit_free_idev;
795 }
796
797 usb_fill_bulk_urb(dev->ep4_in_urb, usb_dev,
798 usb_rcvbulkpipe(usb_dev, 0x4),
799 dev->ep4_in_buf, EP4_BUFSIZE,
800 snd_usb_caiaq_ep4_reply_dispatch, dev);
801
802 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 5);
803 break;
804
655 default: 805 default:
656 /* no input methods supported on this device */ 806 /* no input methods supported on this device */
657 goto exit_free_idev; 807 goto exit_free_idev;
@@ -664,15 +814,17 @@ int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev)
664 for (i = 0; i < input->keycodemax; i++) 814 for (i = 0; i < input->keycodemax; i++)
665 __set_bit(dev->keycode[i], input->keybit); 815 __set_bit(dev->keycode[i], input->keybit);
666 816
817 dev->input_dev = input;
818
667 ret = input_register_device(input); 819 ret = input_register_device(input);
668 if (ret < 0) 820 if (ret < 0)
669 goto exit_free_idev; 821 goto exit_free_idev;
670 822
671 dev->input_dev = input;
672 return 0; 823 return 0;
673 824
674exit_free_idev: 825exit_free_idev:
675 input_free_device(input); 826 input_free_device(input);
827 dev->input_dev = NULL;
676 return ret; 828 return ret;
677} 829}
678 830
@@ -688,4 +840,3 @@ void snd_usb_caiaq_input_free(struct snd_usb_caiaqdev *dev)
688 input_unregister_device(dev->input_dev); 840 input_unregister_device(dev->input_dev);
689 dev->input_dev = NULL; 841 dev->input_dev = NULL;
690} 842}
691
diff --git a/sound/usb/card.c b/sound/usb/card.c
index 781d9e61adfb..c1575eafff12 100644
--- a/sound/usb/card.c
+++ b/sound/usb/card.c
@@ -65,9 +65,9 @@
65#include "helper.h" 65#include "helper.h"
66#include "debug.h" 66#include "debug.h"
67#include "pcm.h" 67#include "pcm.h"
68#include "urb.h"
69#include "format.h" 68#include "format.h"
70#include "power.h" 69#include "power.h"
70#include "stream.h"
71 71
72MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); 72MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
73MODULE_DESCRIPTION("USB Audio"); 73MODULE_DESCRIPTION("USB Audio");
@@ -185,7 +185,7 @@ static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int int
185 return -EINVAL; 185 return -EINVAL;
186 } 186 }
187 187
188 if (! snd_usb_parse_audio_endpoints(chip, interface)) { 188 if (! snd_usb_parse_audio_interface(chip, interface)) {
189 usb_set_interface(dev, interface, 0); /* reset the current interface */ 189 usb_set_interface(dev, interface, 0); /* reset the current interface */
190 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); 190 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
191 return -EINVAL; 191 return -EINVAL;
@@ -530,8 +530,11 @@ snd_usb_audio_probe(struct usb_device *dev,
530 return chip; 530 return chip;
531 531
532 __error: 532 __error:
533 if (chip && !chip->num_interfaces) 533 if (chip) {
534 snd_card_free(chip->card); 534 if (!chip->num_interfaces)
535 snd_card_free(chip->card);
536 chip->probing = 0;
537 }
535 mutex_unlock(&register_mutex); 538 mutex_unlock(&register_mutex);
536 __err_val: 539 __err_val:
537 return NULL; 540 return NULL;
diff --git a/sound/usb/card.h b/sound/usb/card.h
index ae4251d5abf7..a39edcc32a93 100644
--- a/sound/usb/card.h
+++ b/sound/usb/card.h
@@ -94,6 +94,8 @@ struct snd_usb_substream {
94 spinlock_t lock; 94 spinlock_t lock;
95 95
96 struct snd_urb_ops ops; /* callbacks (must be filled at init) */ 96 struct snd_urb_ops ops; /* callbacks (must be filled at init) */
97 int last_frame_number; /* stored frame number */
98 int last_delay; /* stored delay */
97}; 99};
98 100
99struct snd_usb_stream { 101struct snd_usb_stream {
diff --git a/sound/usb/clock.c b/sound/usb/clock.c
index 075195e8661a..379baad3d5ad 100644
--- a/sound/usb/clock.c
+++ b/sound/usb/clock.c
@@ -91,7 +91,7 @@ static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_i
91 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 91 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
92 UAC2_CX_CLOCK_SELECTOR << 8, 92 UAC2_CX_CLOCK_SELECTOR << 8,
93 snd_usb_ctrl_intf(chip) | (selector_id << 8), 93 snd_usb_ctrl_intf(chip) | (selector_id << 8),
94 &buf, sizeof(buf), 1000); 94 &buf, sizeof(buf));
95 95
96 if (ret < 0) 96 if (ret < 0)
97 return ret; 97 return ret;
@@ -118,7 +118,7 @@ static bool uac_clock_source_is_valid(struct snd_usb_audio *chip, int source_id)
118 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 118 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
119 UAC2_CS_CONTROL_CLOCK_VALID << 8, 119 UAC2_CS_CONTROL_CLOCK_VALID << 8,
120 snd_usb_ctrl_intf(chip) | (source_id << 8), 120 snd_usb_ctrl_intf(chip) | (source_id << 8),
121 &data, sizeof(data), 1000); 121 &data, sizeof(data));
122 122
123 if (err < 0) { 123 if (err < 0) {
124 snd_printk(KERN_WARNING "%s(): cannot get clock validity for id %d\n", 124 snd_printk(KERN_WARNING "%s(): cannot get clock validity for id %d\n",
@@ -222,7 +222,7 @@ static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
222 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, 222 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
223 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT, 223 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
224 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, 224 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
225 data, sizeof(data), 1000)) < 0) { 225 data, sizeof(data))) < 0) {
226 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n", 226 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
227 dev->devnum, iface, fmt->altsetting, rate, ep); 227 dev->devnum, iface, fmt->altsetting, rate, ep);
228 return err; 228 return err;
@@ -231,7 +231,7 @@ static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
231 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR, 231 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
232 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN, 232 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
233 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, 233 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
234 data, sizeof(data), 1000)) < 0) { 234 data, sizeof(data))) < 0) {
235 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n", 235 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
236 dev->devnum, iface, fmt->altsetting, ep); 236 dev->devnum, iface, fmt->altsetting, ep);
237 return 0; /* some devices don't support reading */ 237 return 0; /* some devices don't support reading */
@@ -273,7 +273,7 @@ static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
273 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, 273 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
274 UAC2_CS_CONTROL_SAM_FREQ << 8, 274 UAC2_CS_CONTROL_SAM_FREQ << 8,
275 snd_usb_ctrl_intf(chip) | (clock << 8), 275 snd_usb_ctrl_intf(chip) | (clock << 8),
276 data, sizeof(data), 1000)) < 0) { 276 data, sizeof(data))) < 0) {
277 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2)\n", 277 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2)\n",
278 dev->devnum, iface, fmt->altsetting, rate); 278 dev->devnum, iface, fmt->altsetting, rate);
279 return err; 279 return err;
@@ -283,7 +283,7 @@ static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
283 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 283 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
284 UAC2_CS_CONTROL_SAM_FREQ << 8, 284 UAC2_CS_CONTROL_SAM_FREQ << 8,
285 snd_usb_ctrl_intf(chip) | (clock << 8), 285 snd_usb_ctrl_intf(chip) | (clock << 8),
286 data, sizeof(data), 1000)) < 0) { 286 data, sizeof(data))) < 0) {
287 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2)\n", 287 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2)\n",
288 dev->devnum, iface, fmt->altsetting); 288 dev->devnum, iface, fmt->altsetting);
289 return err; 289 return err;
diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c
index 7c0d21ecd821..81c6edecd862 100644
--- a/sound/usb/endpoint.c
+++ b/sound/usb/endpoint.c
@@ -15,436 +15,951 @@
15 * 15 *
16 */ 16 */
17 17
18#include <linux/gfp.h>
18#include <linux/init.h> 19#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/usb.h> 20#include <linux/usb.h>
21#include <linux/usb/audio.h> 21#include <linux/usb/audio.h>
22#include <linux/usb/audio-v2.h>
23 22
24#include <sound/core.h> 23#include <sound/core.h>
25#include <sound/pcm.h> 24#include <sound/pcm.h>
26 25
27#include "usbaudio.h" 26#include "usbaudio.h"
27#include "helper.h"
28#include "card.h" 28#include "card.h"
29#include "proc.h"
30#include "quirks.h"
31#include "endpoint.h" 29#include "endpoint.h"
32#include "urb.h"
33#include "pcm.h" 30#include "pcm.h"
34#include "helper.h"
35#include "format.h"
36#include "clock.h"
37 31
38/* 32/*
39 * free a substream 33 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
34 * this will overflow at approx 524 kHz
40 */ 35 */
41static void free_substream(struct snd_usb_substream *subs) 36static inline unsigned get_usb_full_speed_rate(unsigned int rate)
42{ 37{
43 struct list_head *p, *n; 38 return ((rate << 13) + 62) / 125;
44
45 if (!subs->num_formats)
46 return; /* not initialized */
47 list_for_each_safe(p, n, &subs->fmt_list) {
48 struct audioformat *fp = list_entry(p, struct audioformat, list);
49 kfree(fp->rate_table);
50 kfree(fp);
51 }
52 kfree(subs->rate_list.list);
53} 39}
54 40
41/*
42 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
43 * this will overflow at approx 4 MHz
44 */
45static inline unsigned get_usb_high_speed_rate(unsigned int rate)
46{
47 return ((rate << 10) + 62) / 125;
48}
55 49
56/* 50/*
57 * free a usb stream instance 51 * unlink active urbs.
58 */ 52 */
59static void snd_usb_audio_stream_free(struct snd_usb_stream *stream) 53static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
60{ 54{
61 free_substream(&stream->substream[0]); 55 struct snd_usb_audio *chip = subs->stream->chip;
62 free_substream(&stream->substream[1]); 56 unsigned int i;
63 list_del(&stream->list); 57 int async;
64 kfree(stream); 58
59 subs->running = 0;
60
61 if (!force && subs->stream->chip->shutdown) /* to be sure... */
62 return -EBADFD;
63
64 async = !can_sleep && chip->async_unlink;
65
66 if (!async && in_interrupt())
67 return 0;
68
69 for (i = 0; i < subs->nurbs; i++) {
70 if (test_bit(i, &subs->active_mask)) {
71 if (!test_and_set_bit(i, &subs->unlink_mask)) {
72 struct urb *u = subs->dataurb[i].urb;
73 if (async)
74 usb_unlink_urb(u);
75 else
76 usb_kill_urb(u);
77 }
78 }
79 }
80 if (subs->syncpipe) {
81 for (i = 0; i < SYNC_URBS; i++) {
82 if (test_bit(i+16, &subs->active_mask)) {
83 if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
84 struct urb *u = subs->syncurb[i].urb;
85 if (async)
86 usb_unlink_urb(u);
87 else
88 usb_kill_urb(u);
89 }
90 }
91 }
92 }
93 return 0;
65} 94}
66 95
67static void snd_usb_audio_pcm_free(struct snd_pcm *pcm) 96
97/*
98 * release a urb data
99 */
100static void release_urb_ctx(struct snd_urb_ctx *u)
68{ 101{
69 struct snd_usb_stream *stream = pcm->private_data; 102 if (u->urb) {
70 if (stream) { 103 if (u->buffer_size)
71 stream->pcm = NULL; 104 usb_free_coherent(u->subs->dev, u->buffer_size,
72 snd_usb_audio_stream_free(stream); 105 u->urb->transfer_buffer,
106 u->urb->transfer_dma);
107 usb_free_urb(u->urb);
108 u->urb = NULL;
73 } 109 }
74} 110}
75 111
112/*
113 * wait until all urbs are processed.
114 */
115static int wait_clear_urbs(struct snd_usb_substream *subs)
116{
117 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
118 unsigned int i;
119 int alive;
120
121 do {
122 alive = 0;
123 for (i = 0; i < subs->nurbs; i++) {
124 if (test_bit(i, &subs->active_mask))
125 alive++;
126 }
127 if (subs->syncpipe) {
128 for (i = 0; i < SYNC_URBS; i++) {
129 if (test_bit(i + 16, &subs->active_mask))
130 alive++;
131 }
132 }
133 if (! alive)
134 break;
135 schedule_timeout_uninterruptible(1);
136 } while (time_before(jiffies, end_time));
137 if (alive)
138 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
139 return 0;
140}
76 141
77/* 142/*
78 * add this endpoint to the chip instance. 143 * release a substream
79 * if a stream with the same endpoint already exists, append to it.
80 * if not, create a new pcm stream.
81 */ 144 */
82int snd_usb_add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp) 145void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force)
83{ 146{
84 struct list_head *p; 147 int i;
85 struct snd_usb_stream *as; 148
86 struct snd_usb_substream *subs; 149 /* stop urbs (to be sure) */
87 struct snd_pcm *pcm; 150 deactivate_urbs(subs, force, 1);
88 int err; 151 wait_clear_urbs(subs);
152
153 for (i = 0; i < MAX_URBS; i++)
154 release_urb_ctx(&subs->dataurb[i]);
155 for (i = 0; i < SYNC_URBS; i++)
156 release_urb_ctx(&subs->syncurb[i]);
157 usb_free_coherent(subs->dev, SYNC_URBS * 4,
158 subs->syncbuf, subs->sync_dma);
159 subs->syncbuf = NULL;
160 subs->nurbs = 0;
161}
89 162
90 list_for_each(p, &chip->pcm_list) { 163/*
91 as = list_entry(p, struct snd_usb_stream, list); 164 * complete callback from data urb
92 if (as->fmt_type != fp->fmt_type) 165 */
93 continue; 166static void snd_complete_urb(struct urb *urb)
94 subs = &as->substream[stream]; 167{
95 if (!subs->endpoint) 168 struct snd_urb_ctx *ctx = urb->context;
96 continue; 169 struct snd_usb_substream *subs = ctx->subs;
97 if (subs->endpoint == fp->endpoint) { 170 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
98 list_add_tail(&fp->list, &subs->fmt_list); 171 int err = 0;
99 subs->num_formats++; 172
100 subs->formats |= fp->formats; 173 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
101 return 0; 174 !subs->running || /* can be stopped during retire callback */
175 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
176 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
177 clear_bit(ctx->index, &subs->active_mask);
178 if (err < 0) {
179 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
180 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
102 } 181 }
103 } 182 }
104 /* look for an empty stream */ 183}
105 list_for_each(p, &chip->pcm_list) { 184
106 as = list_entry(p, struct snd_usb_stream, list); 185
107 if (as->fmt_type != fp->fmt_type) 186/*
108 continue; 187 * complete callback from sync urb
109 subs = &as->substream[stream]; 188 */
110 if (subs->endpoint) 189static void snd_complete_sync_urb(struct urb *urb)
111 continue; 190{
112 err = snd_pcm_new_stream(as->pcm, stream, 1); 191 struct snd_urb_ctx *ctx = urb->context;
113 if (err < 0) 192 struct snd_usb_substream *subs = ctx->subs;
114 return err; 193 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
115 snd_usb_init_substream(as, stream, fp); 194 int err = 0;
116 return 0; 195
196 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
197 !subs->running || /* can be stopped during retire callback */
198 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
199 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
200 clear_bit(ctx->index + 16, &subs->active_mask);
201 if (err < 0) {
202 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
203 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
204 }
117 } 205 }
206}
207
118 208
119 /* create a new pcm */ 209/*
120 as = kzalloc(sizeof(*as), GFP_KERNEL); 210 * initialize a substream for plaback/capture
121 if (!as) 211 */
122 return -ENOMEM; 212int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
123 as->pcm_index = chip->pcm_devs; 213 unsigned int period_bytes,
124 as->chip = chip; 214 unsigned int rate,
125 as->fmt_type = fp->fmt_type; 215 unsigned int frame_bits)
126 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs, 216{
127 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0, 217 unsigned int maxsize, i;
128 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1, 218 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
129 &pcm); 219 unsigned int urb_packs, total_packs, packs_per_ms;
130 if (err < 0) { 220 struct snd_usb_audio *chip = subs->stream->chip;
131 kfree(as); 221
132 return err; 222 /* calculate the frequency in 16.16 format */
223 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
224 subs->freqn = get_usb_full_speed_rate(rate);
225 else
226 subs->freqn = get_usb_high_speed_rate(rate);
227 subs->freqm = subs->freqn;
228 subs->freqshift = INT_MIN;
229 /* calculate max. frequency */
230 if (subs->maxpacksize) {
231 /* whatever fits into a max. size packet */
232 maxsize = subs->maxpacksize;
233 subs->freqmax = (maxsize / (frame_bits >> 3))
234 << (16 - subs->datainterval);
235 } else {
236 /* no max. packet size: just take 25% higher than nominal */
237 subs->freqmax = subs->freqn + (subs->freqn >> 2);
238 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
239 >> (16 - subs->datainterval);
133 } 240 }
134 as->pcm = pcm; 241 subs->phase = 0;
135 pcm->private_data = as; 242
136 pcm->private_free = snd_usb_audio_pcm_free; 243 if (subs->fill_max)
137 pcm->info_flags = 0; 244 subs->curpacksize = subs->maxpacksize;
138 if (chip->pcm_devs > 0)
139 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
140 else 245 else
141 strcpy(pcm->name, "USB Audio"); 246 subs->curpacksize = maxsize;
142 247
143 snd_usb_init_substream(as, stream, fp); 248 if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL)
249 packs_per_ms = 8 >> subs->datainterval;
250 else
251 packs_per_ms = 1;
252
253 if (is_playback) {
254 urb_packs = max(chip->nrpacks, 1);
255 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
256 } else
257 urb_packs = 1;
258 urb_packs *= packs_per_ms;
259 if (subs->syncpipe)
260 urb_packs = min(urb_packs, 1U << subs->syncinterval);
261
262 /* decide how many packets to be used */
263 if (is_playback) {
264 unsigned int minsize, maxpacks;
265 /* determine how small a packet can be */
266 minsize = (subs->freqn >> (16 - subs->datainterval))
267 * (frame_bits >> 3);
268 /* with sync from device, assume it can be 12% lower */
269 if (subs->syncpipe)
270 minsize -= minsize >> 3;
271 minsize = max(minsize, 1u);
272 total_packs = (period_bytes + minsize - 1) / minsize;
273 /* we need at least two URBs for queueing */
274 if (total_packs < 2) {
275 total_packs = 2;
276 } else {
277 /* and we don't want too long a queue either */
278 maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
279 total_packs = min(total_packs, maxpacks);
280 }
281 } else {
282 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
283 urb_packs >>= 1;
284 total_packs = MAX_URBS * urb_packs;
285 }
286 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
287 if (subs->nurbs > MAX_URBS) {
288 /* too much... */
289 subs->nurbs = MAX_URBS;
290 total_packs = MAX_URBS * urb_packs;
291 } else if (subs->nurbs < 2) {
292 /* too little - we need at least two packets
293 * to ensure contiguous playback/capture
294 */
295 subs->nurbs = 2;
296 }
144 297
145 list_add(&as->list, &chip->pcm_list); 298 /* allocate and initialize data urbs */
146 chip->pcm_devs++; 299 for (i = 0; i < subs->nurbs; i++) {
300 struct snd_urb_ctx *u = &subs->dataurb[i];
301 u->index = i;
302 u->subs = subs;
303 u->packets = (i + 1) * total_packs / subs->nurbs
304 - i * total_packs / subs->nurbs;
305 u->buffer_size = maxsize * u->packets;
306 if (subs->fmt_type == UAC_FORMAT_TYPE_II)
307 u->packets++; /* for transfer delimiter */
308 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
309 if (!u->urb)
310 goto out_of_memory;
311 u->urb->transfer_buffer =
312 usb_alloc_coherent(subs->dev, u->buffer_size,
313 GFP_KERNEL, &u->urb->transfer_dma);
314 if (!u->urb->transfer_buffer)
315 goto out_of_memory;
316 u->urb->pipe = subs->datapipe;
317 u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
318 u->urb->interval = 1 << subs->datainterval;
319 u->urb->context = u;
320 u->urb->complete = snd_complete_urb;
321 }
322
323 if (subs->syncpipe) {
324 /* allocate and initialize sync urbs */
325 subs->syncbuf = usb_alloc_coherent(subs->dev, SYNC_URBS * 4,
326 GFP_KERNEL, &subs->sync_dma);
327 if (!subs->syncbuf)
328 goto out_of_memory;
329 for (i = 0; i < SYNC_URBS; i++) {
330 struct snd_urb_ctx *u = &subs->syncurb[i];
331 u->index = i;
332 u->subs = subs;
333 u->packets = 1;
334 u->urb = usb_alloc_urb(1, GFP_KERNEL);
335 if (!u->urb)
336 goto out_of_memory;
337 u->urb->transfer_buffer = subs->syncbuf + i * 4;
338 u->urb->transfer_dma = subs->sync_dma + i * 4;
339 u->urb->transfer_buffer_length = 4;
340 u->urb->pipe = subs->syncpipe;
341 u->urb->transfer_flags = URB_ISO_ASAP |
342 URB_NO_TRANSFER_DMA_MAP;
343 u->urb->number_of_packets = 1;
344 u->urb->interval = 1 << subs->syncinterval;
345 u->urb->context = u;
346 u->urb->complete = snd_complete_sync_urb;
347 }
348 }
349 return 0;
147 350
148 snd_usb_proc_pcm_format_add(as); 351out_of_memory:
352 snd_usb_release_substream_urbs(subs, 0);
353 return -ENOMEM;
354}
149 355
356/*
357 * prepare urb for full speed capture sync pipe
358 *
359 * fill the length and offset of each urb descriptor.
360 * the fixed 10.14 frequency is passed through the pipe.
361 */
362static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
363 struct snd_pcm_runtime *runtime,
364 struct urb *urb)
365{
366 unsigned char *cp = urb->transfer_buffer;
367 struct snd_urb_ctx *ctx = urb->context;
368
369 urb->dev = ctx->subs->dev; /* we need to set this at each time */
370 urb->iso_frame_desc[0].length = 3;
371 urb->iso_frame_desc[0].offset = 0;
372 cp[0] = subs->freqn >> 2;
373 cp[1] = subs->freqn >> 10;
374 cp[2] = subs->freqn >> 18;
150 return 0; 375 return 0;
151} 376}
152 377
153static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip, 378/*
154 struct usb_host_interface *alts, 379 * prepare urb for high speed capture sync pipe
155 int protocol, int iface_no) 380 *
381 * fill the length and offset of each urb descriptor.
382 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
383 */
384static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
385 struct snd_pcm_runtime *runtime,
386 struct urb *urb)
156{ 387{
157 /* parsed with a v1 header here. that's ok as we only look at the 388 unsigned char *cp = urb->transfer_buffer;
158 * header first which is the same for both versions */ 389 struct snd_urb_ctx *ctx = urb->context;
159 struct uac_iso_endpoint_descriptor *csep; 390
160 struct usb_interface_descriptor *altsd = get_iface_desc(alts); 391 urb->dev = ctx->subs->dev; /* we need to set this at each time */
161 int attributes = 0; 392 urb->iso_frame_desc[0].length = 4;
162 393 urb->iso_frame_desc[0].offset = 0;
163 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT); 394 cp[0] = subs->freqn;
164 395 cp[1] = subs->freqn >> 8;
165 /* Creamware Noah has this descriptor after the 2nd endpoint */ 396 cp[2] = subs->freqn >> 16;
166 if (!csep && altsd->bNumEndpoints >= 2) 397 cp[3] = subs->freqn >> 24;
167 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT); 398 return 0;
168 399}
169 if (!csep || csep->bLength < 7 ||
170 csep->bDescriptorSubtype != UAC_EP_GENERAL) {
171 snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
172 " class specific endpoint descriptor\n",
173 chip->dev->devnum, iface_no,
174 altsd->bAlternateSetting);
175 return 0;
176 }
177 400
178 if (protocol == UAC_VERSION_1) { 401/*
179 attributes = csep->bmAttributes; 402 * process after capture sync complete
180 } else { 403 * - nothing to do
181 struct uac2_iso_endpoint_descriptor *csep2 = 404 */
182 (struct uac2_iso_endpoint_descriptor *) csep; 405static int retire_capture_sync_urb(struct snd_usb_substream *subs,
406 struct snd_pcm_runtime *runtime,
407 struct urb *urb)
408{
409 return 0;
410}
183 411
184 attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX; 412/*
413 * prepare urb for capture data pipe
414 *
415 * fill the offset and length of each descriptor.
416 *
417 * we use a temporary buffer to write the captured data.
418 * since the length of written data is determined by host, we cannot
419 * write onto the pcm buffer directly... the data is thus copied
420 * later at complete callback to the global buffer.
421 */
422static int prepare_capture_urb(struct snd_usb_substream *subs,
423 struct snd_pcm_runtime *runtime,
424 struct urb *urb)
425{
426 int i, offs;
427 struct snd_urb_ctx *ctx = urb->context;
428
429 offs = 0;
430 urb->dev = ctx->subs->dev; /* we need to set this at each time */
431 for (i = 0; i < ctx->packets; i++) {
432 urb->iso_frame_desc[i].offset = offs;
433 urb->iso_frame_desc[i].length = subs->curpacksize;
434 offs += subs->curpacksize;
435 }
436 urb->transfer_buffer_length = offs;
437 urb->number_of_packets = ctx->packets;
438 return 0;
439}
185 440
186 /* emulate the endpoint attributes of a v1 device */ 441/*
187 if (csep2->bmControls & UAC2_CONTROL_PITCH) 442 * process after capture complete
188 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL; 443 *
444 * copy the data from each desctiptor to the pcm buffer, and
445 * update the current position.
446 */
447static int retire_capture_urb(struct snd_usb_substream *subs,
448 struct snd_pcm_runtime *runtime,
449 struct urb *urb)
450{
451 unsigned long flags;
452 unsigned char *cp;
453 int i;
454 unsigned int stride, frames, bytes, oldptr;
455 int period_elapsed = 0;
456
457 stride = runtime->frame_bits >> 3;
458
459 for (i = 0; i < urb->number_of_packets; i++) {
460 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
461 if (urb->iso_frame_desc[i].status) {
462 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
463 // continue;
464 }
465 bytes = urb->iso_frame_desc[i].actual_length;
466 frames = bytes / stride;
467 if (!subs->txfr_quirk)
468 bytes = frames * stride;
469 if (bytes % (runtime->sample_bits >> 3) != 0) {
470#ifdef CONFIG_SND_DEBUG_VERBOSE
471 int oldbytes = bytes;
472#endif
473 bytes = frames * stride;
474 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
475 oldbytes, bytes);
476 }
477 /* update the current pointer */
478 spin_lock_irqsave(&subs->lock, flags);
479 oldptr = subs->hwptr_done;
480 subs->hwptr_done += bytes;
481 if (subs->hwptr_done >= runtime->buffer_size * stride)
482 subs->hwptr_done -= runtime->buffer_size * stride;
483 frames = (bytes + (oldptr % stride)) / stride;
484 subs->transfer_done += frames;
485 if (subs->transfer_done >= runtime->period_size) {
486 subs->transfer_done -= runtime->period_size;
487 period_elapsed = 1;
488 }
489 spin_unlock_irqrestore(&subs->lock, flags);
490 /* copy a data chunk */
491 if (oldptr + bytes > runtime->buffer_size * stride) {
492 unsigned int bytes1 =
493 runtime->buffer_size * stride - oldptr;
494 memcpy(runtime->dma_area + oldptr, cp, bytes1);
495 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
496 } else {
497 memcpy(runtime->dma_area + oldptr, cp, bytes);
498 }
189 } 499 }
500 if (period_elapsed)
501 snd_pcm_period_elapsed(subs->pcm_substream);
502 return 0;
503}
190 504
191 return attributes; 505/*
506 * Process after capture complete when paused. Nothing to do.
507 */
508static int retire_paused_capture_urb(struct snd_usb_substream *subs,
509 struct snd_pcm_runtime *runtime,
510 struct urb *urb)
511{
512 return 0;
192} 513}
193 514
194static struct uac2_input_terminal_descriptor * 515
195 snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface, 516/*
196 int terminal_id) 517 * prepare urb for playback sync pipe
518 *
519 * set up the offset and length to receive the current frequency.
520 */
521static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
522 struct snd_pcm_runtime *runtime,
523 struct urb *urb)
197{ 524{
198 struct uac2_input_terminal_descriptor *term = NULL; 525 struct snd_urb_ctx *ctx = urb->context;
526
527 urb->dev = ctx->subs->dev; /* we need to set this at each time */
528 urb->iso_frame_desc[0].length = min(4u, ctx->subs->syncmaxsize);
529 urb->iso_frame_desc[0].offset = 0;
530 return 0;
531}
199 532
200 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra, 533/*
201 ctrl_iface->extralen, 534 * process after playback sync complete
202 term, UAC_INPUT_TERMINAL))) { 535 *
203 if (term->bTerminalID == terminal_id) 536 * Full speed devices report feedback values in 10.14 format as samples per
204 return term; 537 * frame, high speed devices in 16.16 format as samples per microframe.
538 * Because the Audio Class 1 spec was written before USB 2.0, many high speed
539 * devices use a wrong interpretation, some others use an entirely different
540 * format. Therefore, we cannot predict what format any particular device uses
541 * and must detect it automatically.
542 */
543static int retire_playback_sync_urb(struct snd_usb_substream *subs,
544 struct snd_pcm_runtime *runtime,
545 struct urb *urb)
546{
547 unsigned int f;
548 int shift;
549 unsigned long flags;
550
551 if (urb->iso_frame_desc[0].status != 0 ||
552 urb->iso_frame_desc[0].actual_length < 3)
553 return 0;
554
555 f = le32_to_cpup(urb->transfer_buffer);
556 if (urb->iso_frame_desc[0].actual_length == 3)
557 f &= 0x00ffffff;
558 else
559 f &= 0x0fffffff;
560 if (f == 0)
561 return 0;
562
563 if (unlikely(subs->freqshift == INT_MIN)) {
564 /*
565 * The first time we see a feedback value, determine its format
566 * by shifting it left or right until it matches the nominal
567 * frequency value. This assumes that the feedback does not
568 * differ from the nominal value more than +50% or -25%.
569 */
570 shift = 0;
571 while (f < subs->freqn - subs->freqn / 4) {
572 f <<= 1;
573 shift++;
574 }
575 while (f > subs->freqn + subs->freqn / 2) {
576 f >>= 1;
577 shift--;
578 }
579 subs->freqshift = shift;
580 }
581 else if (subs->freqshift >= 0)
582 f <<= subs->freqshift;
583 else
584 f >>= -subs->freqshift;
585
586 if (likely(f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax)) {
587 /*
588 * If the frequency looks valid, set it.
589 * This value is referred to in prepare_playback_urb().
590 */
591 spin_lock_irqsave(&subs->lock, flags);
592 subs->freqm = f;
593 spin_unlock_irqrestore(&subs->lock, flags);
594 } else {
595 /*
596 * Out of range; maybe the shift value is wrong.
597 * Reset it so that we autodetect again the next time.
598 */
599 subs->freqshift = INT_MIN;
205 } 600 }
206 601
207 return NULL; 602 return 0;
208} 603}
209 604
210static struct uac2_output_terminal_descriptor * 605/* determine the number of frames in the next packet */
211 snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface, 606static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
212 int terminal_id)
213{ 607{
214 struct uac2_output_terminal_descriptor *term = NULL; 608 if (subs->fill_max)
215 609 return subs->maxframesize;
216 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra, 610 else {
217 ctrl_iface->extralen, 611 subs->phase = (subs->phase & 0xffff)
218 term, UAC_OUTPUT_TERMINAL))) { 612 + (subs->freqm << subs->datainterval);
219 if (term->bTerminalID == terminal_id) 613 return min(subs->phase >> 16, subs->maxframesize);
220 return term;
221 } 614 }
615}
222 616
223 return NULL; 617/*
618 * Prepare urb for streaming before playback starts or when paused.
619 *
620 * We don't have any data, so we send silence.
621 */
622static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
623 struct snd_pcm_runtime *runtime,
624 struct urb *urb)
625{
626 unsigned int i, offs, counts;
627 struct snd_urb_ctx *ctx = urb->context;
628 int stride = runtime->frame_bits >> 3;
629
630 offs = 0;
631 urb->dev = ctx->subs->dev;
632 for (i = 0; i < ctx->packets; ++i) {
633 counts = snd_usb_audio_next_packet_size(subs);
634 urb->iso_frame_desc[i].offset = offs * stride;
635 urb->iso_frame_desc[i].length = counts * stride;
636 offs += counts;
637 }
638 urb->number_of_packets = ctx->packets;
639 urb->transfer_buffer_length = offs * stride;
640 memset(urb->transfer_buffer,
641 runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
642 offs * stride);
643 return 0;
224} 644}
225 645
226int snd_usb_parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no) 646/*
647 * prepare urb for playback data pipe
648 *
649 * Since a URB can handle only a single linear buffer, we must use double
650 * buffering when the data to be transferred overflows the buffer boundary.
651 * To avoid inconsistencies when updating hwptr_done, we use double buffering
652 * for all URBs.
653 */
654static int prepare_playback_urb(struct snd_usb_substream *subs,
655 struct snd_pcm_runtime *runtime,
656 struct urb *urb)
227{ 657{
228 struct usb_device *dev; 658 int i, stride;
229 struct usb_interface *iface; 659 unsigned int counts, frames, bytes;
230 struct usb_host_interface *alts; 660 unsigned long flags;
231 struct usb_interface_descriptor *altsd; 661 int period_elapsed = 0;
232 int i, altno, err, stream; 662 struct snd_urb_ctx *ctx = urb->context;
233 int format = 0, num_channels = 0; 663
234 struct audioformat *fp = NULL; 664 stride = runtime->frame_bits >> 3;
235 int num, protocol, clock = 0; 665
236 struct uac_format_type_i_continuous_descriptor *fmt; 666 frames = 0;
667 urb->dev = ctx->subs->dev; /* we need to set this at each time */
668 urb->number_of_packets = 0;
669 spin_lock_irqsave(&subs->lock, flags);
670 for (i = 0; i < ctx->packets; i++) {
671 counts = snd_usb_audio_next_packet_size(subs);
672 /* set up descriptor */
673 urb->iso_frame_desc[i].offset = frames * stride;
674 urb->iso_frame_desc[i].length = counts * stride;
675 frames += counts;
676 urb->number_of_packets++;
677 subs->transfer_done += counts;
678 if (subs->transfer_done >= runtime->period_size) {
679 subs->transfer_done -= runtime->period_size;
680 period_elapsed = 1;
681 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
682 if (subs->transfer_done > 0) {
683 /* FIXME: fill-max mode is not
684 * supported yet */
685 frames -= subs->transfer_done;
686 counts -= subs->transfer_done;
687 urb->iso_frame_desc[i].length =
688 counts * stride;
689 subs->transfer_done = 0;
690 }
691 i++;
692 if (i < ctx->packets) {
693 /* add a transfer delimiter */
694 urb->iso_frame_desc[i].offset =
695 frames * stride;
696 urb->iso_frame_desc[i].length = 0;
697 urb->number_of_packets++;
698 }
699 break;
700 }
701 }
702 if (period_elapsed) /* finish at the period boundary */
703 break;
704 }
705 bytes = frames * stride;
706 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
707 /* err, the transferred area goes over buffer boundary. */
708 unsigned int bytes1 =
709 runtime->buffer_size * stride - subs->hwptr_done;
710 memcpy(urb->transfer_buffer,
711 runtime->dma_area + subs->hwptr_done, bytes1);
712 memcpy(urb->transfer_buffer + bytes1,
713 runtime->dma_area, bytes - bytes1);
714 } else {
715 memcpy(urb->transfer_buffer,
716 runtime->dma_area + subs->hwptr_done, bytes);
717 }
718 subs->hwptr_done += bytes;
719 if (subs->hwptr_done >= runtime->buffer_size * stride)
720 subs->hwptr_done -= runtime->buffer_size * stride;
721
722 /* update delay with exact number of samples queued */
723 runtime->delay = subs->last_delay;
724 runtime->delay += frames;
725 subs->last_delay = runtime->delay;
726
727 /* realign last_frame_number */
728 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
729 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
730
731 spin_unlock_irqrestore(&subs->lock, flags);
732 urb->transfer_buffer_length = bytes;
733 if (period_elapsed)
734 snd_pcm_period_elapsed(subs->pcm_substream);
735 return 0;
736}
237 737
238 dev = chip->dev; 738/*
739 * process after playback data complete
740 * - decrease the delay count again
741 */
742static int retire_playback_urb(struct snd_usb_substream *subs,
743 struct snd_pcm_runtime *runtime,
744 struct urb *urb)
745{
746 unsigned long flags;
747 int stride = runtime->frame_bits >> 3;
748 int processed = urb->transfer_buffer_length / stride;
749 int est_delay;
239 750
240 /* parse the interface's altsettings */ 751 spin_lock_irqsave(&subs->lock, flags);
241 iface = usb_ifnum_to_if(dev, iface_no);
242 752
243 num = iface->num_altsetting; 753 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
754 /* update delay with exact number of samples played */
755 if (processed > subs->last_delay)
756 subs->last_delay = 0;
757 else
758 subs->last_delay -= processed;
759 runtime->delay = subs->last_delay;
244 760
245 /* 761 /*
246 * Dallas DS4201 workaround: It presents 5 altsettings, but the last 762 * Report when delay estimate is off by more than 2ms.
247 * one misses syncpipe, and does not produce any sound. 763 * The error should be lower than 2ms since the estimate relies
764 * on two reads of a counter updated every ms.
248 */ 765 */
249 if (chip->usb_id == USB_ID(0x04fa, 0x4201)) 766 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
250 num = 4; 767 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
251 768 est_delay, subs->last_delay);
252 for (i = 0; i < num; i++) {
253 alts = &iface->altsetting[i];
254 altsd = get_iface_desc(alts);
255 protocol = altsd->bInterfaceProtocol;
256 /* skip invalid one */
257 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
258 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
259 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
260 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
261 altsd->bNumEndpoints < 1 ||
262 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
263 continue;
264 /* must be isochronous */
265 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
266 USB_ENDPOINT_XFER_ISOC)
267 continue;
268 /* check direction */
269 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
270 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
271 altno = altsd->bAlternateSetting;
272
273 if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
274 continue;
275
276 /* get audio formats */
277 switch (protocol) {
278 default:
279 snd_printdd(KERN_WARNING "%d:%u:%d: unknown interface protocol %#02x, assuming v1\n",
280 dev->devnum, iface_no, altno, protocol);
281 protocol = UAC_VERSION_1;
282 /* fall through */
283
284 case UAC_VERSION_1: {
285 struct uac1_as_header_descriptor *as =
286 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
287
288 if (!as) {
289 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
290 dev->devnum, iface_no, altno);
291 continue;
292 }
293 769
294 if (as->bLength < sizeof(*as)) { 770 spin_unlock_irqrestore(&subs->lock, flags);
295 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n", 771 return 0;
296 dev->devnum, iface_no, altno); 772}
297 continue;
298 }
299 773
300 format = le16_to_cpu(as->wFormatTag); /* remember the format value */ 774static const char *usb_error_string(int err)
301 break; 775{
302 } 776 switch (err) {
777 case -ENODEV:
778 return "no device";
779 case -ENOENT:
780 return "endpoint not enabled";
781 case -EPIPE:
782 return "endpoint stalled";
783 case -ENOSPC:
784 return "not enough bandwidth";
785 case -ESHUTDOWN:
786 return "device disabled";
787 case -EHOSTUNREACH:
788 return "device suspended";
789 case -EINVAL:
790 case -EAGAIN:
791 case -EFBIG:
792 case -EMSGSIZE:
793 return "internal error";
794 default:
795 return "unknown error";
796 }
797}
303 798
304 case UAC_VERSION_2: { 799/*
305 struct uac2_input_terminal_descriptor *input_term; 800 * set up and start data/sync urbs
306 struct uac2_output_terminal_descriptor *output_term; 801 */
307 struct uac2_as_header_descriptor *as = 802static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
308 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL); 803{
804 unsigned int i;
805 int err;
309 806
310 if (!as) { 807 if (subs->stream->chip->shutdown)
311 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n", 808 return -EBADFD;
312 dev->devnum, iface_no, altno); 809
313 continue; 810 for (i = 0; i < subs->nurbs; i++) {
811 if (snd_BUG_ON(!subs->dataurb[i].urb))
812 return -EINVAL;
813 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
814 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
815 goto __error;
816 }
817 }
818 if (subs->syncpipe) {
819 for (i = 0; i < SYNC_URBS; i++) {
820 if (snd_BUG_ON(!subs->syncurb[i].urb))
821 return -EINVAL;
822 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
823 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
824 goto __error;
314 } 825 }
826 }
827 }
315 828
316 if (as->bLength < sizeof(*as)) { 829 subs->active_mask = 0;
317 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n", 830 subs->unlink_mask = 0;
318 dev->devnum, iface_no, altno); 831 subs->running = 1;
319 continue; 832 for (i = 0; i < subs->nurbs; i++) {
833 err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
834 if (err < 0) {
835 snd_printk(KERN_ERR "cannot submit datapipe "
836 "for urb %d, error %d: %s\n",
837 i, err, usb_error_string(err));
838 goto __error;
839 }
840 set_bit(i, &subs->active_mask);
841 }
842 if (subs->syncpipe) {
843 for (i = 0; i < SYNC_URBS; i++) {
844 err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
845 if (err < 0) {
846 snd_printk(KERN_ERR "cannot submit syncpipe "
847 "for urb %d, error %d: %s\n",
848 i, err, usb_error_string(err));
849 goto __error;
320 } 850 }
851 set_bit(i + 16, &subs->active_mask);
852 }
853 }
854 return 0;
321 855
322 num_channels = as->bNrChannels; 856 __error:
323 format = le32_to_cpu(as->bmFormats); 857 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
858 deactivate_urbs(subs, 0, 0);
859 return -EPIPE;
860}
324 861
325 /* lookup the terminal associated to this interface
326 * to extract the clock */
327 input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
328 as->bTerminalLink);
329 if (input_term) {
330 clock = input_term->bCSourceID;
331 break;
332 }
333 862
334 output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf, 863/*
335 as->bTerminalLink); 864 */
336 if (output_term) { 865static struct snd_urb_ops audio_urb_ops[2] = {
337 clock = output_term->bCSourceID; 866 {
338 break; 867 .prepare = prepare_nodata_playback_urb,
339 } 868 .retire = retire_playback_urb,
869 .prepare_sync = prepare_playback_sync_urb,
870 .retire_sync = retire_playback_sync_urb,
871 },
872 {
873 .prepare = prepare_capture_urb,
874 .retire = retire_capture_urb,
875 .prepare_sync = prepare_capture_sync_urb,
876 .retire_sync = retire_capture_sync_urb,
877 },
878};
340 879
341 snd_printk(KERN_ERR "%d:%u:%d : bogus bTerminalLink %d\n", 880/*
342 dev->devnum, iface_no, altno, as->bTerminalLink); 881 * initialize the substream instance.
343 continue; 882 */
344 }
345 }
346 883
347 /* get format type */ 884void snd_usb_init_substream(struct snd_usb_stream *as,
348 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE); 885 int stream, struct audioformat *fp)
349 if (!fmt) { 886{
350 snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n", 887 struct snd_usb_substream *subs = &as->substream[stream];
351 dev->devnum, iface_no, altno); 888
352 continue; 889 INIT_LIST_HEAD(&subs->fmt_list);
353 } 890 spin_lock_init(&subs->lock);
354 if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8)) || 891
355 ((protocol == UAC_VERSION_2) && (fmt->bLength != 6))) { 892 subs->stream = as;
356 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n", 893 subs->direction = stream;
357 dev->devnum, iface_no, altno); 894 subs->dev = as->chip->dev;
358 continue; 895 subs->txfr_quirk = as->chip->txfr_quirk;
359 } 896 subs->ops = audio_urb_ops[stream];
897 if (snd_usb_get_speed(subs->dev) >= USB_SPEED_HIGH)
898 subs->ops.prepare_sync = prepare_capture_sync_urb_hs;
899
900 snd_usb_set_pcm_ops(as->pcm, stream);
901
902 list_add_tail(&fp->list, &subs->fmt_list);
903 subs->formats |= fp->formats;
904 subs->endpoint = fp->endpoint;
905 subs->num_formats++;
906 subs->fmt_type = fp->fmt_type;
907}
360 908
361 /* 909int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd)
362 * Blue Microphones workaround: The last altsetting is identical 910{
363 * with the previous one, except for a larger packet size, but 911 struct snd_usb_substream *subs = substream->runtime->private_data;
364 * is actually a mislabeled two-channel setting; ignore it.
365 */
366 if (fmt->bNrChannels == 1 &&
367 fmt->bSubframeSize == 2 &&
368 altno == 2 && num == 3 &&
369 fp && fp->altsetting == 1 && fp->channels == 1 &&
370 fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
371 protocol == UAC_VERSION_1 &&
372 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
373 fp->maxpacksize * 2)
374 continue;
375
376 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
377 if (! fp) {
378 snd_printk(KERN_ERR "cannot malloc\n");
379 return -ENOMEM;
380 }
381 912
382 fp->iface = iface_no; 913 switch (cmd) {
383 fp->altsetting = altno; 914 case SNDRV_PCM_TRIGGER_START:
384 fp->altset_idx = i; 915 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
385 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; 916 subs->ops.prepare = prepare_playback_urb;
386 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; 917 return 0;
387 fp->datainterval = snd_usb_parse_datainterval(chip, alts); 918 case SNDRV_PCM_TRIGGER_STOP:
388 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); 919 return deactivate_urbs(subs, 0, 0);
389 /* num_channels is only set for v2 interfaces */ 920 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
390 fp->channels = num_channels; 921 subs->ops.prepare = prepare_nodata_playback_urb;
391 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH) 922 return 0;
392 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1) 923 }
393 * (fp->maxpacksize & 0x7ff);
394 fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no);
395 fp->clock = clock;
396
397 /* some quirks for attributes here */
398
399 switch (chip->usb_id) {
400 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
401 /* Optoplay sets the sample rate attribute although
402 * it seems not supporting it in fact.
403 */
404 fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
405 break;
406 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
407 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
408 /* doesn't set the sample rate attribute, but supports it */
409 fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
410 break;
411 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro USB */
412 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
413 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
414 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
415 an older model 77d:223) */
416 /*
417 * plantronics headset and Griffin iMic have set adaptive-in
418 * although it's really not...
419 */
420 fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
421 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
422 fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
423 else
424 fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
425 break;
426 }
427 924
428 /* ok, let's parse further... */ 925 return -EINVAL;
429 if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) { 926}
430 kfree(fp->rate_table);
431 kfree(fp);
432 fp = NULL;
433 continue;
434 }
435 927
436 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint); 928int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)
437 err = snd_usb_add_audio_endpoint(chip, stream, fp); 929{
438 if (err < 0) { 930 struct snd_usb_substream *subs = substream->runtime->private_data;
439 kfree(fp->rate_table); 931
440 kfree(fp); 932 switch (cmd) {
441 return err; 933 case SNDRV_PCM_TRIGGER_START:
442 } 934 subs->ops.retire = retire_capture_urb;
443 /* try to set the interface... */ 935 return start_urbs(subs, substream->runtime);
444 usb_set_interface(chip->dev, iface_no, altno); 936 case SNDRV_PCM_TRIGGER_STOP:
445 snd_usb_init_pitch(chip, iface_no, alts, fp); 937 return deactivate_urbs(subs, 0, 0);
446 snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max); 938 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
939 subs->ops.retire = retire_paused_capture_urb;
940 return 0;
941 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
942 subs->ops.retire = retire_capture_urb;
943 return 0;
447 } 944 }
945
946 return -EINVAL;
947}
948
949int snd_usb_substream_prepare(struct snd_usb_substream *subs,
950 struct snd_pcm_runtime *runtime)
951{
952 /* clear urbs (to be sure) */
953 deactivate_urbs(subs, 0, 1);
954 wait_clear_urbs(subs);
955
956 /* for playback, submit the URBs now; otherwise, the first hwptr_done
957 * updates for all URBs would happen at the same time when starting */
958 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
959 subs->ops.prepare = prepare_nodata_playback_urb;
960 return start_urbs(subs, runtime);
961 }
962
448 return 0; 963 return 0;
449} 964}
450 965
diff --git a/sound/usb/endpoint.h b/sound/usb/endpoint.h
index 64dd0db023b2..88eb63a636eb 100644
--- a/sound/usb/endpoint.h
+++ b/sound/usb/endpoint.h
@@ -1,11 +1,21 @@
1#ifndef __USBAUDIO_ENDPOINT_H 1#ifndef __USBAUDIO_ENDPOINT_H
2#define __USBAUDIO_ENDPOINT_H 2#define __USBAUDIO_ENDPOINT_H
3 3
4int snd_usb_parse_audio_endpoints(struct snd_usb_audio *chip, 4void snd_usb_init_substream(struct snd_usb_stream *as,
5 int iface_no); 5 int stream,
6 struct audioformat *fp);
6 7
7int snd_usb_add_audio_endpoint(struct snd_usb_audio *chip, 8int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
8 int stream, 9 unsigned int period_bytes,
9 struct audioformat *fp); 10 unsigned int rate,
11 unsigned int frame_bits);
12
13void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force);
14
15int snd_usb_substream_prepare(struct snd_usb_substream *subs,
16 struct snd_pcm_runtime *runtime);
17
18int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd);
19int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd);
10 20
11#endif /* __USBAUDIO_ENDPOINT_H */ 21#endif /* __USBAUDIO_ENDPOINT_H */
diff --git a/sound/usb/format.c b/sound/usb/format.c
index 8d042dce0d16..89421d176570 100644
--- a/sound/usb/format.c
+++ b/sound/usb/format.c
@@ -286,7 +286,7 @@ static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,
286 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 286 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
287 UAC2_CS_CONTROL_SAM_FREQ << 8, 287 UAC2_CS_CONTROL_SAM_FREQ << 8,
288 snd_usb_ctrl_intf(chip) | (clock << 8), 288 snd_usb_ctrl_intf(chip) | (clock << 8),
289 tmp, sizeof(tmp), 1000); 289 tmp, sizeof(tmp));
290 290
291 if (ret < 0) { 291 if (ret < 0) {
292 snd_printk(KERN_ERR "%s(): unable to retrieve number of sample rates (clock %d)\n", 292 snd_printk(KERN_ERR "%s(): unable to retrieve number of sample rates (clock %d)\n",
@@ -307,7 +307,7 @@ static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,
307 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 307 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
308 UAC2_CS_CONTROL_SAM_FREQ << 8, 308 UAC2_CS_CONTROL_SAM_FREQ << 8,
309 snd_usb_ctrl_intf(chip) | (clock << 8), 309 snd_usb_ctrl_intf(chip) | (clock << 8),
310 data, data_size, 1000); 310 data, data_size);
311 311
312 if (ret < 0) { 312 if (ret < 0) {
313 snd_printk(KERN_ERR "%s(): unable to retrieve sample rate range (clock %d)\n", 313 snd_printk(KERN_ERR "%s(): unable to retrieve sample rate range (clock %d)\n",
diff --git a/sound/usb/helper.c b/sound/usb/helper.c
index f280c1903c25..9eed8f40b179 100644
--- a/sound/usb/helper.c
+++ b/sound/usb/helper.c
@@ -81,7 +81,7 @@ void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype
81 */ 81 */
82int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request, 82int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
83 __u8 requesttype, __u16 value, __u16 index, void *data, 83 __u8 requesttype, __u16 value, __u16 index, void *data,
84 __u16 size, int timeout) 84 __u16 size)
85{ 85{
86 int err; 86 int err;
87 void *buf = NULL; 87 void *buf = NULL;
@@ -92,7 +92,7 @@ int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
92 return -ENOMEM; 92 return -ENOMEM;
93 } 93 }
94 err = usb_control_msg(dev, pipe, request, requesttype, 94 err = usb_control_msg(dev, pipe, request, requesttype,
95 value, index, buf, size, timeout); 95 value, index, buf, size, 1000);
96 if (size > 0) { 96 if (size > 0) {
97 memcpy(data, buf, size); 97 memcpy(data, buf, size);
98 kfree(buf); 98 kfree(buf);
diff --git a/sound/usb/helper.h b/sound/usb/helper.h
index 09bd943c43bf..805c300dd004 100644
--- a/sound/usb/helper.h
+++ b/sound/usb/helper.h
@@ -8,7 +8,7 @@ void *snd_usb_find_csint_desc(void *descstart, int desclen, void *after, u8 dsub
8 8
9int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, 9int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe,
10 __u8 request, __u8 requesttype, __u16 value, __u16 index, 10 __u8 request, __u8 requesttype, __u16 value, __u16 index,
11 void *data, __u16 size, int timeout); 11 void *data, __u16 size);
12 12
13unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip, 13unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
14 struct usb_host_interface *alts); 14 struct usb_host_interface *alts);
diff --git a/sound/usb/midi.c b/sound/usb/midi.c
index f9289102886a..e21f026d9577 100644
--- a/sound/usb/midi.c
+++ b/sound/usb/midi.c
@@ -816,6 +816,22 @@ static struct usb_protocol_ops snd_usbmidi_raw_ops = {
816 .output = snd_usbmidi_raw_output, 816 .output = snd_usbmidi_raw_output,
817}; 817};
818 818
819/*
820 * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes.
821 */
822
823static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint* ep,
824 uint8_t* buffer, int buffer_length)
825{
826 if (buffer_length > 2)
827 snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2);
828}
829
830static struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
831 .input = snd_usbmidi_ftdi_input,
832 .output = snd_usbmidi_raw_output,
833};
834
819static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep, 835static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
820 uint8_t *buffer, int buffer_length) 836 uint8_t *buffer, int buffer_length)
821{ 837{
@@ -2163,6 +2179,17 @@ int snd_usbmidi_create(struct snd_card *card,
2163 /* endpoint 1 is input-only */ 2179 /* endpoint 1 is input-only */
2164 endpoints[1].out_cables = 0; 2180 endpoints[1].out_cables = 0;
2165 break; 2181 break;
2182 case QUIRK_MIDI_FTDI:
2183 umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;
2184
2185 /* set baud rate to 31250 (48 MHz / 16 / 96) */
2186 err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
2187 3, 0x40, 0x60, 0, NULL, 0, 1000);
2188 if (err < 0)
2189 break;
2190
2191 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2192 break;
2166 default: 2193 default:
2167 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type); 2194 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2168 err = -ENXIO; 2195 err = -ENXIO;
diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c
index c22fa76e363a..60f65ace7474 100644
--- a/sound/usb/mixer.c
+++ b/sound/usb/mixer.c
@@ -152,6 +152,7 @@ static inline void check_mapped_dB(const struct usbmix_name_map *p,
152 if (p && p->dB) { 152 if (p && p->dB) {
153 cval->dBmin = p->dB->min; 153 cval->dBmin = p->dB->min;
154 cval->dBmax = p->dB->max; 154 cval->dBmax = p->dB->max;
155 cval->initialized = 1;
155 } 156 }
156} 157}
157 158
@@ -295,7 +296,7 @@ static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request, int v
295 if (snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request, 296 if (snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
296 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 297 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
297 validx, snd_usb_ctrl_intf(chip) | (cval->id << 8), 298 validx, snd_usb_ctrl_intf(chip) | (cval->id << 8),
298 buf, val_len, 100) >= val_len) { 299 buf, val_len) >= val_len) {
299 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len)); 300 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
300 snd_usb_autosuspend(cval->mixer->chip); 301 snd_usb_autosuspend(cval->mixer->chip);
301 return 0; 302 return 0;
@@ -332,7 +333,7 @@ static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request, int v
332 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest, 333 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
333 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 334 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
334 validx, snd_usb_ctrl_intf(chip) | (cval->id << 8), 335 validx, snd_usb_ctrl_intf(chip) | (cval->id << 8),
335 buf, size, 1000); 336 buf, size);
336 snd_usb_autosuspend(chip); 337 snd_usb_autosuspend(chip);
337 338
338 if (ret < 0) { 339 if (ret < 0) {
@@ -444,7 +445,7 @@ int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
444 usb_sndctrlpipe(chip->dev, 0), request, 445 usb_sndctrlpipe(chip->dev, 0), request,
445 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, 446 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
446 validx, snd_usb_ctrl_intf(chip) | (cval->id << 8), 447 validx, snd_usb_ctrl_intf(chip) | (cval->id << 8),
447 buf, val_len, 100) >= 0) { 448 buf, val_len) >= 0) {
448 snd_usb_autosuspend(chip); 449 snd_usb_autosuspend(chip);
449 return 0; 450 return 0;
450 } 451 }
@@ -880,8 +881,17 @@ static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_
880 uinfo->value.integer.min = 0; 881 uinfo->value.integer.min = 0;
881 uinfo->value.integer.max = 1; 882 uinfo->value.integer.max = 1;
882 } else { 883 } else {
883 if (! cval->initialized) 884 if (!cval->initialized) {
884 get_min_max(cval, 0); 885 get_min_max(cval, 0);
886 if (cval->initialized && cval->dBmin >= cval->dBmax) {
887 kcontrol->vd[0].access &=
888 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
889 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
890 snd_ctl_notify(cval->mixer->chip->card,
891 SNDRV_CTL_EVENT_MASK_INFO,
892 &kcontrol->id);
893 }
894 }
885 uinfo->value.integer.min = 0; 895 uinfo->value.integer.min = 0;
886 uinfo->value.integer.max = 896 uinfo->value.integer.max =
887 (cval->max - cval->min + cval->res - 1) / cval->res; 897 (cval->max - cval->min + cval->res - 1) / cval->res;
@@ -1092,7 +1102,7 @@ static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1092 " Switch" : " Volume"); 1102 " Switch" : " Volume");
1093 if (control == UAC_FU_VOLUME) { 1103 if (control == UAC_FU_VOLUME) {
1094 check_mapped_dB(map, cval); 1104 check_mapped_dB(map, cval);
1095 if (cval->dBmin < cval->dBmax) { 1105 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1096 kctl->tlv.c = mixer_vol_tlv; 1106 kctl->tlv.c = mixer_vol_tlv;
1097 kctl->vd[0].access |= 1107 kctl->vd[0].access |=
1098 SNDRV_CTL_ELEM_ACCESS_TLV_READ | 1108 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
@@ -1191,6 +1201,11 @@ static int parse_audio_feature_unit(struct mixer_build *state, int unitid, void
1191 1201
1192 if (state->mixer->protocol == UAC_VERSION_1) { 1202 if (state->mixer->protocol == UAC_VERSION_1) {
1193 csize = hdr->bControlSize; 1203 csize = hdr->bControlSize;
1204 if (!csize) {
1205 snd_printdd(KERN_ERR "usbaudio: unit %u: "
1206 "invalid bControlSize == 0\n", unitid);
1207 return -EINVAL;
1208 }
1194 channels = (hdr->bLength - 7) / csize - 1; 1209 channels = (hdr->bLength - 7) / csize - 1;
1195 bmaControls = hdr->bmaControls; 1210 bmaControls = hdr->bmaControls;
1196 } else { 1211 } else {
@@ -1244,7 +1259,7 @@ static int parse_audio_feature_unit(struct mixer_build *state, int unitid, void
1244 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, 0); 1259 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, 0);
1245 } 1260 }
1246 } else { /* UAC_VERSION_2 */ 1261 } else { /* UAC_VERSION_2 */
1247 for (i = 0; i < 30/2; i++) { 1262 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
1248 unsigned int ch_bits = 0; 1263 unsigned int ch_bits = 0;
1249 unsigned int ch_read_only = 0; 1264 unsigned int ch_read_only = 0;
1250 1265
@@ -1934,15 +1949,13 @@ static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1934 struct mixer_build state; 1949 struct mixer_build state;
1935 int err; 1950 int err;
1936 const struct usbmix_ctl_map *map; 1951 const struct usbmix_ctl_map *map;
1937 struct usb_host_interface *hostif;
1938 void *p; 1952 void *p;
1939 1953
1940 hostif = mixer->chip->ctrl_intf;
1941 memset(&state, 0, sizeof(state)); 1954 memset(&state, 0, sizeof(state));
1942 state.chip = mixer->chip; 1955 state.chip = mixer->chip;
1943 state.mixer = mixer; 1956 state.mixer = mixer;
1944 state.buffer = hostif->extra; 1957 state.buffer = mixer->hostif->extra;
1945 state.buflen = hostif->extralen; 1958 state.buflen = mixer->hostif->extralen;
1946 1959
1947 /* check the mapping table */ 1960 /* check the mapping table */
1948 for (map = usbmix_ctl_maps; map->id; map++) { 1961 for (map = usbmix_ctl_maps; map->id; map++) {
@@ -1955,7 +1968,8 @@ static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1955 } 1968 }
1956 1969
1957 p = NULL; 1970 p = NULL;
1958 while ((p = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, p, UAC_OUTPUT_TERMINAL)) != NULL) { 1971 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra, mixer->hostif->extralen,
1972 p, UAC_OUTPUT_TERMINAL)) != NULL) {
1959 if (mixer->protocol == UAC_VERSION_1) { 1973 if (mixer->protocol == UAC_VERSION_1) {
1960 struct uac1_output_terminal_descriptor *desc = p; 1974 struct uac1_output_terminal_descriptor *desc = p;
1961 1975
@@ -2162,17 +2176,15 @@ int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
2162/* create the handler for the optional status interrupt endpoint */ 2176/* create the handler for the optional status interrupt endpoint */
2163static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer) 2177static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
2164{ 2178{
2165 struct usb_host_interface *hostif;
2166 struct usb_endpoint_descriptor *ep; 2179 struct usb_endpoint_descriptor *ep;
2167 void *transfer_buffer; 2180 void *transfer_buffer;
2168 int buffer_length; 2181 int buffer_length;
2169 unsigned int epnum; 2182 unsigned int epnum;
2170 2183
2171 hostif = mixer->chip->ctrl_intf;
2172 /* we need one interrupt input endpoint */ 2184 /* we need one interrupt input endpoint */
2173 if (get_iface_desc(hostif)->bNumEndpoints < 1) 2185 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
2174 return 0; 2186 return 0;
2175 ep = get_endpoint(hostif, 0); 2187 ep = get_endpoint(mixer->hostif, 0);
2176 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep)) 2188 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
2177 return 0; 2189 return 0;
2178 2190
@@ -2202,7 +2214,6 @@ int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2202 }; 2214 };
2203 struct usb_mixer_interface *mixer; 2215 struct usb_mixer_interface *mixer;
2204 struct snd_info_entry *entry; 2216 struct snd_info_entry *entry;
2205 struct usb_host_interface *host_iface;
2206 int err; 2217 int err;
2207 2218
2208 strcpy(chip->card->mixername, "USB Mixer"); 2219 strcpy(chip->card->mixername, "USB Mixer");
@@ -2219,8 +2230,8 @@ int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2219 return -ENOMEM; 2230 return -ENOMEM;
2220 } 2231 }
2221 2232
2222 host_iface = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0]; 2233 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
2223 switch (get_iface_desc(host_iface)->bInterfaceProtocol) { 2234 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
2224 case UAC_VERSION_1: 2235 case UAC_VERSION_1:
2225 default: 2236 default:
2226 mixer->protocol = UAC_VERSION_1; 2237 mixer->protocol = UAC_VERSION_1;
diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h
index ae1a14dcfe82..81b2d8a32fb0 100644
--- a/sound/usb/mixer.h
+++ b/sound/usb/mixer.h
@@ -3,6 +3,7 @@
3 3
4struct usb_mixer_interface { 4struct usb_mixer_interface {
5 struct snd_usb_audio *chip; 5 struct snd_usb_audio *chip;
6 struct usb_host_interface *hostif;
6 struct list_head list; 7 struct list_head list;
7 unsigned int ignore_ctl_error; 8 unsigned int ignore_ctl_error;
8 struct urb *urb; 9 struct urb *urb;
diff --git a/sound/usb/mixer_quirks.c b/sound/usb/mixer_quirks.c
index 3d0f4873112b..ab125ee0b0f0 100644
--- a/sound/usb/mixer_quirks.c
+++ b/sound/usb/mixer_quirks.c
@@ -190,18 +190,18 @@ static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_e
190 err = snd_usb_ctl_msg(mixer->chip->dev, 190 err = snd_usb_ctl_msg(mixer->chip->dev,
191 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24, 191 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
192 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER, 192 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
193 !value, 0, NULL, 0, 100); 193 !value, 0, NULL, 0);
194 /* USB X-Fi S51 Pro */ 194 /* USB X-Fi S51 Pro */
195 if (mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) 195 if (mixer->chip->usb_id == USB_ID(0x041e, 0x30df))
196 err = snd_usb_ctl_msg(mixer->chip->dev, 196 err = snd_usb_ctl_msg(mixer->chip->dev,
197 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24, 197 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
198 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER, 198 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
199 !value, 0, NULL, 0, 100); 199 !value, 0, NULL, 0);
200 else 200 else
201 err = snd_usb_ctl_msg(mixer->chip->dev, 201 err = snd_usb_ctl_msg(mixer->chip->dev,
202 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24, 202 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
203 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER, 203 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
204 value, index + 2, NULL, 0, 100); 204 value, index + 2, NULL, 0);
205 if (err < 0) 205 if (err < 0)
206 return err; 206 return err;
207 mixer->audigy2nx_leds[index] = value; 207 mixer->audigy2nx_leds[index] = value;
@@ -299,7 +299,7 @@ static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
299 usb_rcvctrlpipe(mixer->chip->dev, 0), 299 usb_rcvctrlpipe(mixer->chip->dev, 0),
300 UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS | 300 UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
301 USB_RECIP_INTERFACE, 0, 301 USB_RECIP_INTERFACE, 0,
302 jacks[i].unitid << 8, buf, 3, 100); 302 jacks[i].unitid << 8, buf, 3);
303 if (err == 3 && (buf[0] == 3 || buf[0] == 6)) 303 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
304 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]); 304 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
305 else 305 else
@@ -332,7 +332,7 @@ static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
332 err = snd_usb_ctl_msg(mixer->chip->dev, 332 err = snd_usb_ctl_msg(mixer->chip->dev,
333 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08, 333 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
334 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER, 334 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
335 50, 0, &new_status, 1, 100); 335 50, 0, &new_status, 1);
336 if (err < 0) 336 if (err < 0)
337 return err; 337 return err;
338 mixer->xonar_u1_status = new_status; 338 mixer->xonar_u1_status = new_status;
diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
index b8dcbf407bbb..0220b0f335b9 100644
--- a/sound/usb/pcm.c
+++ b/sound/usb/pcm.c
@@ -28,12 +28,36 @@
28#include "card.h" 28#include "card.h"
29#include "quirks.h" 29#include "quirks.h"
30#include "debug.h" 30#include "debug.h"
31#include "urb.h" 31#include "endpoint.h"
32#include "helper.h" 32#include "helper.h"
33#include "pcm.h" 33#include "pcm.h"
34#include "clock.h" 34#include "clock.h"
35#include "power.h" 35#include "power.h"
36 36
37/* return the estimated delay based on USB frame counters */
38snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
39 unsigned int rate)
40{
41 int current_frame_number;
42 int frame_diff;
43 int est_delay;
44
45 current_frame_number = usb_get_current_frame_number(subs->dev);
46 /*
47 * HCD implementations use different widths, use lower 8 bits.
48 * The delay will be managed up to 256ms, which is more than
49 * enough
50 */
51 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
52
53 /* Approximation based on number of samples per USB frame (ms),
54 some truncation for 44.1 but the estimate is good enough */
55 est_delay = subs->last_delay - (frame_diff * rate / 1000);
56 if (est_delay < 0)
57 est_delay = 0;
58 return est_delay;
59}
60
37/* 61/*
38 * return the current pcm pointer. just based on the hwptr_done value. 62 * return the current pcm pointer. just based on the hwptr_done value.
39 */ 63 */
@@ -45,6 +69,8 @@ static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream
45 subs = (struct snd_usb_substream *)substream->runtime->private_data; 69 subs = (struct snd_usb_substream *)substream->runtime->private_data;
46 spin_lock(&subs->lock); 70 spin_lock(&subs->lock);
47 hwptr_done = subs->hwptr_done; 71 hwptr_done = subs->hwptr_done;
72 substream->runtime->delay = snd_usb_pcm_delay(subs,
73 substream->runtime->rate);
48 spin_unlock(&subs->lock); 74 spin_unlock(&subs->lock);
49 return hwptr_done / (substream->runtime->frame_bits >> 3); 75 return hwptr_done / (substream->runtime->frame_bits >> 3);
50} 76}
@@ -126,7 +152,7 @@ static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
126 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, 152 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
127 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, 153 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
128 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, 154 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
129 data, sizeof(data), 1000)) < 0) { 155 data, sizeof(data))) < 0) {
130 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n", 156 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
131 dev->devnum, iface, ep); 157 dev->devnum, iface, ep);
132 return err; 158 return err;
@@ -150,7 +176,7 @@ static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
150 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR, 176 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
151 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT, 177 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
152 UAC2_EP_CS_PITCH << 8, 0, 178 UAC2_EP_CS_PITCH << 8, 0,
153 data, sizeof(data), 1000)) < 0) { 179 data, sizeof(data))) < 0) {
154 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n", 180 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
155 dev->devnum, iface, fmt->altsetting); 181 dev->devnum, iface, fmt->altsetting);
156 return err; 182 return err;
@@ -417,6 +443,8 @@ static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
417 subs->hwptr_done = 0; 443 subs->hwptr_done = 0;
418 subs->transfer_done = 0; 444 subs->transfer_done = 0;
419 subs->phase = 0; 445 subs->phase = 0;
446 subs->last_delay = 0;
447 subs->last_frame_number = 0;
420 runtime->delay = 0; 448 runtime->delay = 0;
421 449
422 return snd_usb_substream_prepare(subs, runtime); 450 return snd_usb_substream_prepare(subs, runtime);
diff --git a/sound/usb/pcm.h b/sound/usb/pcm.h
index ed3e283f618d..df7a003682ad 100644
--- a/sound/usb/pcm.h
+++ b/sound/usb/pcm.h
@@ -1,6 +1,9 @@
1#ifndef __USBAUDIO_PCM_H 1#ifndef __USBAUDIO_PCM_H
2#define __USBAUDIO_PCM_H 2#define __USBAUDIO_PCM_H
3 3
4snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
5 unsigned int rate);
6
4void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream); 7void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream);
5 8
6int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface, 9int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
diff --git a/sound/usb/quirks-table.h b/sound/usb/quirks-table.h
index dba0b7f11c54..b61945f3af9e 100644
--- a/sound/usb/quirks-table.h
+++ b/sound/usb/quirks-table.h
@@ -39,6 +39,17 @@
39 .idProduct = prod, \ 39 .idProduct = prod, \
40 .bInterfaceClass = USB_CLASS_VENDOR_SPEC 40 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
41 41
42/* FTDI devices */
43{
44 USB_DEVICE(0x0403, 0xb8d8),
45 .driver_info = (unsigned long) & (const struct snd_usb_audio_quirk) {
46 /* .vendor_name = "STARR LABS", */
47 /* .product_name = "Starr Labs MIDI USB device", */
48 .ifnum = 0,
49 .type = QUIRK_MIDI_FTDI
50 }
51},
52
42/* Creative/Toshiba Multimedia Center SB-0500 */ 53/* Creative/Toshiba Multimedia Center SB-0500 */
43{ 54{
44 USB_DEVICE(0x041e, 0x3048), 55 USB_DEVICE(0x041e, 0x3048),
@@ -1678,6 +1689,20 @@ YAMAHA_DEVICE(0x7010, "UB99"),
1678 } 1689 }
1679}, 1690},
1680{ 1691{
1692 /* Added support for Roland UM-ONE which differs from UM-1 */
1693 USB_DEVICE(0x0582, 0x012a),
1694 .driver_info = (unsigned long) & (const struct snd_usb_audio_quirk) {
1695 /* .vendor_name = "ROLAND", */
1696 /* .product_name = "UM-ONE", */
1697 .ifnum = 0,
1698 .type = QUIRK_MIDI_FIXED_ENDPOINT,
1699 .data = & (const struct snd_usb_midi_endpoint_info) {
1700 .out_cables = 0x0001,
1701 .in_cables = 0x0003
1702 }
1703 }
1704},
1705{
1681 USB_DEVICE(0x0582, 0x011e), 1706 USB_DEVICE(0x0582, 0x011e),
1682 .driver_info = (unsigned long) & (const struct snd_usb_audio_quirk) { 1707 .driver_info = (unsigned long) & (const struct snd_usb_audio_quirk) {
1683 /* .vendor_name = "BOSS", */ 1708 /* .vendor_name = "BOSS", */
@@ -1707,6 +1732,40 @@ YAMAHA_DEVICE(0x7010, "UB99"),
1707 } 1732 }
1708 } 1733 }
1709}, 1734},
1735{
1736 USB_DEVICE(0x0582, 0x0130),
1737 .driver_info = (unsigned long) & (const struct snd_usb_audio_quirk) {
1738 /* .vendor_name = "BOSS", */
1739 /* .product_name = "MICRO BR-80", */
1740 .ifnum = QUIRK_ANY_INTERFACE,
1741 .type = QUIRK_COMPOSITE,
1742 .data = (const struct snd_usb_audio_quirk[]) {
1743 {
1744 .ifnum = 0,
1745 .type = QUIRK_IGNORE_INTERFACE
1746 },
1747 {
1748 .ifnum = 1,
1749 .type = QUIRK_AUDIO_STANDARD_INTERFACE
1750 },
1751 {
1752 .ifnum = 2,
1753 .type = QUIRK_AUDIO_STANDARD_INTERFACE
1754 },
1755 {
1756 .ifnum = 3,
1757 .type = QUIRK_MIDI_FIXED_ENDPOINT,
1758 .data = & (const struct snd_usb_midi_endpoint_info) {
1759 .out_cables = 0x0001,
1760 .in_cables = 0x0001
1761 }
1762 },
1763 {
1764 .ifnum = -1
1765 }
1766 }
1767 }
1768},
1710 1769
1711/* Guillemot devices */ 1770/* Guillemot devices */
1712{ 1771{
@@ -2417,6 +2476,12 @@ YAMAHA_DEVICE(0x7010, "UB99"),
2417 .idProduct = 0x1020, 2476 .idProduct = 0x1020,
2418}, 2477},
2419 2478
2479/* KeithMcMillen Stringport */
2480{
2481 USB_DEVICE(0x1f38, 0x0001),
2482 .bInterfaceClass = USB_CLASS_AUDIO,
2483},
2484
2420/* Miditech devices */ 2485/* Miditech devices */
2421{ 2486{
2422 USB_DEVICE(0x4752, 0x0011), 2487 USB_DEVICE(0x4752, 0x0011),
diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c
index 77762c99afbe..2e5bc7344026 100644
--- a/sound/usb/quirks.c
+++ b/sound/usb/quirks.c
@@ -34,6 +34,7 @@
34#include "endpoint.h" 34#include "endpoint.h"
35#include "pcm.h" 35#include "pcm.h"
36#include "clock.h" 36#include "clock.h"
37#include "stream.h"
37 38
38/* 39/*
39 * handle the quirks for the contained interfaces 40 * handle the quirks for the contained interfaces
@@ -106,7 +107,7 @@ static int create_standard_audio_quirk(struct snd_usb_audio *chip,
106 107
107 alts = &iface->altsetting[0]; 108 alts = &iface->altsetting[0];
108 altsd = get_iface_desc(alts); 109 altsd = get_iface_desc(alts);
109 err = snd_usb_parse_audio_endpoints(chip, altsd->bInterfaceNumber); 110 err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
110 if (err < 0) { 111 if (err < 0) {
111 snd_printk(KERN_ERR "cannot setup if %d: error %d\n", 112 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
112 altsd->bInterfaceNumber, err); 113 altsd->bInterfaceNumber, err);
@@ -147,7 +148,7 @@ static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
147 148
148 stream = (fp->endpoint & USB_DIR_IN) 149 stream = (fp->endpoint & USB_DIR_IN)
149 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; 150 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
150 err = snd_usb_add_audio_endpoint(chip, stream, fp); 151 err = snd_usb_add_audio_stream(chip, stream, fp);
151 if (err < 0) { 152 if (err < 0) {
152 kfree(fp); 153 kfree(fp);
153 kfree(rate_table); 154 kfree(rate_table);
@@ -254,7 +255,7 @@ static int create_uaxx_quirk(struct snd_usb_audio *chip,
254 255
255 stream = (fp->endpoint & USB_DIR_IN) 256 stream = (fp->endpoint & USB_DIR_IN)
256 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; 257 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
257 err = snd_usb_add_audio_endpoint(chip, stream, fp); 258 err = snd_usb_add_audio_stream(chip, stream, fp);
258 if (err < 0) { 259 if (err < 0) {
259 kfree(fp); 260 kfree(fp);
260 return err; 261 return err;
@@ -306,6 +307,7 @@ int snd_usb_create_quirk(struct snd_usb_audio *chip,
306 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk, 307 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
307 [QUIRK_MIDI_CME] = create_any_midi_quirk, 308 [QUIRK_MIDI_CME] = create_any_midi_quirk,
308 [QUIRK_MIDI_AKAI] = create_any_midi_quirk, 309 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
310 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
309 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk, 311 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
310 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk, 312 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
311 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk, 313 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
@@ -338,7 +340,7 @@ static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interfac
338 snd_printdd("sending Extigy boot sequence...\n"); 340 snd_printdd("sending Extigy boot sequence...\n");
339 /* Send message to force it to reconnect with full interface. */ 341 /* Send message to force it to reconnect with full interface. */
340 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0), 342 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
341 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000); 343 0x10, 0x43, 0x0001, 0x000a, NULL, 0);
342 if (err < 0) snd_printdd("error sending boot message: %d\n", err); 344 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
343 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, 345 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
344 &dev->descriptor, sizeof(dev->descriptor)); 346 &dev->descriptor, sizeof(dev->descriptor));
@@ -359,11 +361,11 @@ static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
359 361
360 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a, 362 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
361 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER, 363 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
362 0, 0, &buf, 1, 1000); 364 0, 0, &buf, 1);
363 if (buf == 0) { 365 if (buf == 0) {
364 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29, 366 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
365 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER, 367 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
366 1, 2000, NULL, 0, 1000); 368 1, 2000, NULL, 0);
367 return -ENODEV; 369 return -ENODEV;
368 } 370 }
369 return 0; 371 return 0;
@@ -406,7 +408,7 @@ static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 valu
406 buf[3] = reg; 408 buf[3] = reg;
407 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION, 409 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
408 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, 410 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
409 0, 0, &buf, 4, 1000); 411 0, 0, &buf, 4);
410} 412}
411 413
412static int snd_usb_cm106_boot_quirk(struct usb_device *dev) 414static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
@@ -426,7 +428,7 @@ static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
426 */ 428 */
427static int snd_usb_cm6206_boot_quirk(struct usb_device *dev) 429static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
428{ 430{
429 int err, reg; 431 int err = 0, reg;
430 int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000}; 432 int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
431 433
432 for (reg = 0; reg < ARRAY_SIZE(val); reg++) { 434 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
diff --git a/sound/usb/stream.c b/sound/usb/stream.c
new file mode 100644
index 000000000000..5ff8010b2d6f
--- /dev/null
+++ b/sound/usb/stream.c
@@ -0,0 +1,452 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/usb.h>
21#include <linux/usb/audio.h>
22#include <linux/usb/audio-v2.h>
23
24#include <sound/core.h>
25#include <sound/pcm.h>
26
27#include "usbaudio.h"
28#include "card.h"
29#include "proc.h"
30#include "quirks.h"
31#include "endpoint.h"
32#include "pcm.h"
33#include "helper.h"
34#include "format.h"
35#include "clock.h"
36#include "stream.h"
37
38/*
39 * free a substream
40 */
41static void free_substream(struct snd_usb_substream *subs)
42{
43 struct list_head *p, *n;
44
45 if (!subs->num_formats)
46 return; /* not initialized */
47 list_for_each_safe(p, n, &subs->fmt_list) {
48 struct audioformat *fp = list_entry(p, struct audioformat, list);
49 kfree(fp->rate_table);
50 kfree(fp);
51 }
52 kfree(subs->rate_list.list);
53}
54
55
56/*
57 * free a usb stream instance
58 */
59static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
60{
61 free_substream(&stream->substream[0]);
62 free_substream(&stream->substream[1]);
63 list_del(&stream->list);
64 kfree(stream);
65}
66
67static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
68{
69 struct snd_usb_stream *stream = pcm->private_data;
70 if (stream) {
71 stream->pcm = NULL;
72 snd_usb_audio_stream_free(stream);
73 }
74}
75
76
77/*
78 * add this endpoint to the chip instance.
79 * if a stream with the same endpoint already exists, append to it.
80 * if not, create a new pcm stream.
81 */
82int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
83 int stream,
84 struct audioformat *fp)
85{
86 struct list_head *p;
87 struct snd_usb_stream *as;
88 struct snd_usb_substream *subs;
89 struct snd_pcm *pcm;
90 int err;
91
92 list_for_each(p, &chip->pcm_list) {
93 as = list_entry(p, struct snd_usb_stream, list);
94 if (as->fmt_type != fp->fmt_type)
95 continue;
96 subs = &as->substream[stream];
97 if (!subs->endpoint)
98 continue;
99 if (subs->endpoint == fp->endpoint) {
100 list_add_tail(&fp->list, &subs->fmt_list);
101 subs->num_formats++;
102 subs->formats |= fp->formats;
103 return 0;
104 }
105 }
106 /* look for an empty stream */
107 list_for_each(p, &chip->pcm_list) {
108 as = list_entry(p, struct snd_usb_stream, list);
109 if (as->fmt_type != fp->fmt_type)
110 continue;
111 subs = &as->substream[stream];
112 if (subs->endpoint)
113 continue;
114 err = snd_pcm_new_stream(as->pcm, stream, 1);
115 if (err < 0)
116 return err;
117 snd_usb_init_substream(as, stream, fp);
118 return 0;
119 }
120
121 /* create a new pcm */
122 as = kzalloc(sizeof(*as), GFP_KERNEL);
123 if (!as)
124 return -ENOMEM;
125 as->pcm_index = chip->pcm_devs;
126 as->chip = chip;
127 as->fmt_type = fp->fmt_type;
128 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
129 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
130 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
131 &pcm);
132 if (err < 0) {
133 kfree(as);
134 return err;
135 }
136 as->pcm = pcm;
137 pcm->private_data = as;
138 pcm->private_free = snd_usb_audio_pcm_free;
139 pcm->info_flags = 0;
140 if (chip->pcm_devs > 0)
141 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
142 else
143 strcpy(pcm->name, "USB Audio");
144
145 snd_usb_init_substream(as, stream, fp);
146
147 list_add(&as->list, &chip->pcm_list);
148 chip->pcm_devs++;
149
150 snd_usb_proc_pcm_format_add(as);
151
152 return 0;
153}
154
155static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
156 struct usb_host_interface *alts,
157 int protocol, int iface_no)
158{
159 /* parsed with a v1 header here. that's ok as we only look at the
160 * header first which is the same for both versions */
161 struct uac_iso_endpoint_descriptor *csep;
162 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
163 int attributes = 0;
164
165 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
166
167 /* Creamware Noah has this descriptor after the 2nd endpoint */
168 if (!csep && altsd->bNumEndpoints >= 2)
169 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
170
171 if (!csep || csep->bLength < 7 ||
172 csep->bDescriptorSubtype != UAC_EP_GENERAL) {
173 snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
174 " class specific endpoint descriptor\n",
175 chip->dev->devnum, iface_no,
176 altsd->bAlternateSetting);
177 return 0;
178 }
179
180 if (protocol == UAC_VERSION_1) {
181 attributes = csep->bmAttributes;
182 } else {
183 struct uac2_iso_endpoint_descriptor *csep2 =
184 (struct uac2_iso_endpoint_descriptor *) csep;
185
186 attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
187
188 /* emulate the endpoint attributes of a v1 device */
189 if (csep2->bmControls & UAC2_CONTROL_PITCH)
190 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
191 }
192
193 return attributes;
194}
195
196static struct uac2_input_terminal_descriptor *
197 snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
198 int terminal_id)
199{
200 struct uac2_input_terminal_descriptor *term = NULL;
201
202 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
203 ctrl_iface->extralen,
204 term, UAC_INPUT_TERMINAL))) {
205 if (term->bTerminalID == terminal_id)
206 return term;
207 }
208
209 return NULL;
210}
211
212static struct uac2_output_terminal_descriptor *
213 snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
214 int terminal_id)
215{
216 struct uac2_output_terminal_descriptor *term = NULL;
217
218 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
219 ctrl_iface->extralen,
220 term, UAC_OUTPUT_TERMINAL))) {
221 if (term->bTerminalID == terminal_id)
222 return term;
223 }
224
225 return NULL;
226}
227
228int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
229{
230 struct usb_device *dev;
231 struct usb_interface *iface;
232 struct usb_host_interface *alts;
233 struct usb_interface_descriptor *altsd;
234 int i, altno, err, stream;
235 int format = 0, num_channels = 0;
236 struct audioformat *fp = NULL;
237 int num, protocol, clock = 0;
238 struct uac_format_type_i_continuous_descriptor *fmt;
239
240 dev = chip->dev;
241
242 /* parse the interface's altsettings */
243 iface = usb_ifnum_to_if(dev, iface_no);
244
245 num = iface->num_altsetting;
246
247 /*
248 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
249 * one misses syncpipe, and does not produce any sound.
250 */
251 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
252 num = 4;
253
254 for (i = 0; i < num; i++) {
255 alts = &iface->altsetting[i];
256 altsd = get_iface_desc(alts);
257 protocol = altsd->bInterfaceProtocol;
258 /* skip invalid one */
259 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
260 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
261 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
262 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
263 altsd->bNumEndpoints < 1 ||
264 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
265 continue;
266 /* must be isochronous */
267 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
268 USB_ENDPOINT_XFER_ISOC)
269 continue;
270 /* check direction */
271 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
272 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
273 altno = altsd->bAlternateSetting;
274
275 if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
276 continue;
277
278 /* get audio formats */
279 switch (protocol) {
280 default:
281 snd_printdd(KERN_WARNING "%d:%u:%d: unknown interface protocol %#02x, assuming v1\n",
282 dev->devnum, iface_no, altno, protocol);
283 protocol = UAC_VERSION_1;
284 /* fall through */
285
286 case UAC_VERSION_1: {
287 struct uac1_as_header_descriptor *as =
288 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
289
290 if (!as) {
291 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
292 dev->devnum, iface_no, altno);
293 continue;
294 }
295
296 if (as->bLength < sizeof(*as)) {
297 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
298 dev->devnum, iface_no, altno);
299 continue;
300 }
301
302 format = le16_to_cpu(as->wFormatTag); /* remember the format value */
303 break;
304 }
305
306 case UAC_VERSION_2: {
307 struct uac2_input_terminal_descriptor *input_term;
308 struct uac2_output_terminal_descriptor *output_term;
309 struct uac2_as_header_descriptor *as =
310 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
311
312 if (!as) {
313 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
314 dev->devnum, iface_no, altno);
315 continue;
316 }
317
318 if (as->bLength < sizeof(*as)) {
319 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
320 dev->devnum, iface_no, altno);
321 continue;
322 }
323
324 num_channels = as->bNrChannels;
325 format = le32_to_cpu(as->bmFormats);
326
327 /* lookup the terminal associated to this interface
328 * to extract the clock */
329 input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
330 as->bTerminalLink);
331 if (input_term) {
332 clock = input_term->bCSourceID;
333 break;
334 }
335
336 output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
337 as->bTerminalLink);
338 if (output_term) {
339 clock = output_term->bCSourceID;
340 break;
341 }
342
343 snd_printk(KERN_ERR "%d:%u:%d : bogus bTerminalLink %d\n",
344 dev->devnum, iface_no, altno, as->bTerminalLink);
345 continue;
346 }
347 }
348
349 /* get format type */
350 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
351 if (!fmt) {
352 snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
353 dev->devnum, iface_no, altno);
354 continue;
355 }
356 if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8)) ||
357 ((protocol == UAC_VERSION_2) && (fmt->bLength < 6))) {
358 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
359 dev->devnum, iface_no, altno);
360 continue;
361 }
362
363 /*
364 * Blue Microphones workaround: The last altsetting is identical
365 * with the previous one, except for a larger packet size, but
366 * is actually a mislabeled two-channel setting; ignore it.
367 */
368 if (fmt->bNrChannels == 1 &&
369 fmt->bSubframeSize == 2 &&
370 altno == 2 && num == 3 &&
371 fp && fp->altsetting == 1 && fp->channels == 1 &&
372 fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
373 protocol == UAC_VERSION_1 &&
374 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
375 fp->maxpacksize * 2)
376 continue;
377
378 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
379 if (! fp) {
380 snd_printk(KERN_ERR "cannot malloc\n");
381 return -ENOMEM;
382 }
383
384 fp->iface = iface_no;
385 fp->altsetting = altno;
386 fp->altset_idx = i;
387 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
388 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
389 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
390 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
391 /* num_channels is only set for v2 interfaces */
392 fp->channels = num_channels;
393 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
394 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
395 * (fp->maxpacksize & 0x7ff);
396 fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no);
397 fp->clock = clock;
398
399 /* some quirks for attributes here */
400
401 switch (chip->usb_id) {
402 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
403 /* Optoplay sets the sample rate attribute although
404 * it seems not supporting it in fact.
405 */
406 fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
407 break;
408 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
409 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
410 /* doesn't set the sample rate attribute, but supports it */
411 fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
412 break;
413 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro USB */
414 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
415 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
416 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
417 an older model 77d:223) */
418 /*
419 * plantronics headset and Griffin iMic have set adaptive-in
420 * although it's really not...
421 */
422 fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
423 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
424 fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
425 else
426 fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
427 break;
428 }
429
430 /* ok, let's parse further... */
431 if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {
432 kfree(fp->rate_table);
433 kfree(fp);
434 fp = NULL;
435 continue;
436 }
437
438 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
439 err = snd_usb_add_audio_stream(chip, stream, fp);
440 if (err < 0) {
441 kfree(fp->rate_table);
442 kfree(fp);
443 return err;
444 }
445 /* try to set the interface... */
446 usb_set_interface(chip->dev, iface_no, altno);
447 snd_usb_init_pitch(chip, iface_no, alts, fp);
448 snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max);
449 }
450 return 0;
451}
452
diff --git a/sound/usb/stream.h b/sound/usb/stream.h
new file mode 100644
index 000000000000..c97f679fc84f
--- /dev/null
+++ b/sound/usb/stream.h
@@ -0,0 +1,12 @@
1#ifndef __USBAUDIO_STREAM_H
2#define __USBAUDIO_STREAM_H
3
4int snd_usb_parse_audio_interface(struct snd_usb_audio *chip,
5 int iface_no);
6
7int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
8 int stream,
9 struct audioformat *fp);
10
11#endif /* __USBAUDIO_STREAM_H */
12
diff --git a/sound/usb/urb.c b/sound/usb/urb.c
deleted file mode 100644
index e184349aee83..000000000000
--- a/sound/usb/urb.c
+++ /dev/null
@@ -1,941 +0,0 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 */
17
18#include <linux/gfp.h>
19#include <linux/init.h>
20#include <linux/usb.h>
21#include <linux/usb/audio.h>
22
23#include <sound/core.h>
24#include <sound/pcm.h>
25
26#include "usbaudio.h"
27#include "helper.h"
28#include "card.h"
29#include "urb.h"
30#include "pcm.h"
31
32/*
33 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
34 * this will overflow at approx 524 kHz
35 */
36static inline unsigned get_usb_full_speed_rate(unsigned int rate)
37{
38 return ((rate << 13) + 62) / 125;
39}
40
41/*
42 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
43 * this will overflow at approx 4 MHz
44 */
45static inline unsigned get_usb_high_speed_rate(unsigned int rate)
46{
47 return ((rate << 10) + 62) / 125;
48}
49
50/*
51 * unlink active urbs.
52 */
53static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
54{
55 struct snd_usb_audio *chip = subs->stream->chip;
56 unsigned int i;
57 int async;
58
59 subs->running = 0;
60
61 if (!force && subs->stream->chip->shutdown) /* to be sure... */
62 return -EBADFD;
63
64 async = !can_sleep && chip->async_unlink;
65
66 if (!async && in_interrupt())
67 return 0;
68
69 for (i = 0; i < subs->nurbs; i++) {
70 if (test_bit(i, &subs->active_mask)) {
71 if (!test_and_set_bit(i, &subs->unlink_mask)) {
72 struct urb *u = subs->dataurb[i].urb;
73 if (async)
74 usb_unlink_urb(u);
75 else
76 usb_kill_urb(u);
77 }
78 }
79 }
80 if (subs->syncpipe) {
81 for (i = 0; i < SYNC_URBS; i++) {
82 if (test_bit(i+16, &subs->active_mask)) {
83 if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
84 struct urb *u = subs->syncurb[i].urb;
85 if (async)
86 usb_unlink_urb(u);
87 else
88 usb_kill_urb(u);
89 }
90 }
91 }
92 }
93 return 0;
94}
95
96
97/*
98 * release a urb data
99 */
100static void release_urb_ctx(struct snd_urb_ctx *u)
101{
102 if (u->urb) {
103 if (u->buffer_size)
104 usb_free_coherent(u->subs->dev, u->buffer_size,
105 u->urb->transfer_buffer,
106 u->urb->transfer_dma);
107 usb_free_urb(u->urb);
108 u->urb = NULL;
109 }
110}
111
112/*
113 * wait until all urbs are processed.
114 */
115static int wait_clear_urbs(struct snd_usb_substream *subs)
116{
117 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
118 unsigned int i;
119 int alive;
120
121 do {
122 alive = 0;
123 for (i = 0; i < subs->nurbs; i++) {
124 if (test_bit(i, &subs->active_mask))
125 alive++;
126 }
127 if (subs->syncpipe) {
128 for (i = 0; i < SYNC_URBS; i++) {
129 if (test_bit(i + 16, &subs->active_mask))
130 alive++;
131 }
132 }
133 if (! alive)
134 break;
135 schedule_timeout_uninterruptible(1);
136 } while (time_before(jiffies, end_time));
137 if (alive)
138 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
139 return 0;
140}
141
142/*
143 * release a substream
144 */
145void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force)
146{
147 int i;
148
149 /* stop urbs (to be sure) */
150 deactivate_urbs(subs, force, 1);
151 wait_clear_urbs(subs);
152
153 for (i = 0; i < MAX_URBS; i++)
154 release_urb_ctx(&subs->dataurb[i]);
155 for (i = 0; i < SYNC_URBS; i++)
156 release_urb_ctx(&subs->syncurb[i]);
157 usb_free_coherent(subs->dev, SYNC_URBS * 4,
158 subs->syncbuf, subs->sync_dma);
159 subs->syncbuf = NULL;
160 subs->nurbs = 0;
161}
162
163/*
164 * complete callback from data urb
165 */
166static void snd_complete_urb(struct urb *urb)
167{
168 struct snd_urb_ctx *ctx = urb->context;
169 struct snd_usb_substream *subs = ctx->subs;
170 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
171 int err = 0;
172
173 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
174 !subs->running || /* can be stopped during retire callback */
175 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
176 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
177 clear_bit(ctx->index, &subs->active_mask);
178 if (err < 0) {
179 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
180 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
181 }
182 }
183}
184
185
186/*
187 * complete callback from sync urb
188 */
189static void snd_complete_sync_urb(struct urb *urb)
190{
191 struct snd_urb_ctx *ctx = urb->context;
192 struct snd_usb_substream *subs = ctx->subs;
193 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
194 int err = 0;
195
196 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
197 !subs->running || /* can be stopped during retire callback */
198 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
199 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
200 clear_bit(ctx->index + 16, &subs->active_mask);
201 if (err < 0) {
202 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
203 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
204 }
205 }
206}
207
208
209/*
210 * initialize a substream for plaback/capture
211 */
212int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
213 unsigned int period_bytes,
214 unsigned int rate,
215 unsigned int frame_bits)
216{
217 unsigned int maxsize, i;
218 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
219 unsigned int urb_packs, total_packs, packs_per_ms;
220 struct snd_usb_audio *chip = subs->stream->chip;
221
222 /* calculate the frequency in 16.16 format */
223 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
224 subs->freqn = get_usb_full_speed_rate(rate);
225 else
226 subs->freqn = get_usb_high_speed_rate(rate);
227 subs->freqm = subs->freqn;
228 subs->freqshift = INT_MIN;
229 /* calculate max. frequency */
230 if (subs->maxpacksize) {
231 /* whatever fits into a max. size packet */
232 maxsize = subs->maxpacksize;
233 subs->freqmax = (maxsize / (frame_bits >> 3))
234 << (16 - subs->datainterval);
235 } else {
236 /* no max. packet size: just take 25% higher than nominal */
237 subs->freqmax = subs->freqn + (subs->freqn >> 2);
238 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
239 >> (16 - subs->datainterval);
240 }
241 subs->phase = 0;
242
243 if (subs->fill_max)
244 subs->curpacksize = subs->maxpacksize;
245 else
246 subs->curpacksize = maxsize;
247
248 if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL)
249 packs_per_ms = 8 >> subs->datainterval;
250 else
251 packs_per_ms = 1;
252
253 if (is_playback) {
254 urb_packs = max(chip->nrpacks, 1);
255 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
256 } else
257 urb_packs = 1;
258 urb_packs *= packs_per_ms;
259 if (subs->syncpipe)
260 urb_packs = min(urb_packs, 1U << subs->syncinterval);
261
262 /* decide how many packets to be used */
263 if (is_playback) {
264 unsigned int minsize, maxpacks;
265 /* determine how small a packet can be */
266 minsize = (subs->freqn >> (16 - subs->datainterval))
267 * (frame_bits >> 3);
268 /* with sync from device, assume it can be 12% lower */
269 if (subs->syncpipe)
270 minsize -= minsize >> 3;
271 minsize = max(minsize, 1u);
272 total_packs = (period_bytes + minsize - 1) / minsize;
273 /* we need at least two URBs for queueing */
274 if (total_packs < 2) {
275 total_packs = 2;
276 } else {
277 /* and we don't want too long a queue either */
278 maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
279 total_packs = min(total_packs, maxpacks);
280 }
281 } else {
282 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
283 urb_packs >>= 1;
284 total_packs = MAX_URBS * urb_packs;
285 }
286 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
287 if (subs->nurbs > MAX_URBS) {
288 /* too much... */
289 subs->nurbs = MAX_URBS;
290 total_packs = MAX_URBS * urb_packs;
291 } else if (subs->nurbs < 2) {
292 /* too little - we need at least two packets
293 * to ensure contiguous playback/capture
294 */
295 subs->nurbs = 2;
296 }
297
298 /* allocate and initialize data urbs */
299 for (i = 0; i < subs->nurbs; i++) {
300 struct snd_urb_ctx *u = &subs->dataurb[i];
301 u->index = i;
302 u->subs = subs;
303 u->packets = (i + 1) * total_packs / subs->nurbs
304 - i * total_packs / subs->nurbs;
305 u->buffer_size = maxsize * u->packets;
306 if (subs->fmt_type == UAC_FORMAT_TYPE_II)
307 u->packets++; /* for transfer delimiter */
308 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
309 if (!u->urb)
310 goto out_of_memory;
311 u->urb->transfer_buffer =
312 usb_alloc_coherent(subs->dev, u->buffer_size,
313 GFP_KERNEL, &u->urb->transfer_dma);
314 if (!u->urb->transfer_buffer)
315 goto out_of_memory;
316 u->urb->pipe = subs->datapipe;
317 u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
318 u->urb->interval = 1 << subs->datainterval;
319 u->urb->context = u;
320 u->urb->complete = snd_complete_urb;
321 }
322
323 if (subs->syncpipe) {
324 /* allocate and initialize sync urbs */
325 subs->syncbuf = usb_alloc_coherent(subs->dev, SYNC_URBS * 4,
326 GFP_KERNEL, &subs->sync_dma);
327 if (!subs->syncbuf)
328 goto out_of_memory;
329 for (i = 0; i < SYNC_URBS; i++) {
330 struct snd_urb_ctx *u = &subs->syncurb[i];
331 u->index = i;
332 u->subs = subs;
333 u->packets = 1;
334 u->urb = usb_alloc_urb(1, GFP_KERNEL);
335 if (!u->urb)
336 goto out_of_memory;
337 u->urb->transfer_buffer = subs->syncbuf + i * 4;
338 u->urb->transfer_dma = subs->sync_dma + i * 4;
339 u->urb->transfer_buffer_length = 4;
340 u->urb->pipe = subs->syncpipe;
341 u->urb->transfer_flags = URB_ISO_ASAP |
342 URB_NO_TRANSFER_DMA_MAP;
343 u->urb->number_of_packets = 1;
344 u->urb->interval = 1 << subs->syncinterval;
345 u->urb->context = u;
346 u->urb->complete = snd_complete_sync_urb;
347 }
348 }
349 return 0;
350
351out_of_memory:
352 snd_usb_release_substream_urbs(subs, 0);
353 return -ENOMEM;
354}
355
356/*
357 * prepare urb for full speed capture sync pipe
358 *
359 * fill the length and offset of each urb descriptor.
360 * the fixed 10.14 frequency is passed through the pipe.
361 */
362static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
363 struct snd_pcm_runtime *runtime,
364 struct urb *urb)
365{
366 unsigned char *cp = urb->transfer_buffer;
367 struct snd_urb_ctx *ctx = urb->context;
368
369 urb->dev = ctx->subs->dev; /* we need to set this at each time */
370 urb->iso_frame_desc[0].length = 3;
371 urb->iso_frame_desc[0].offset = 0;
372 cp[0] = subs->freqn >> 2;
373 cp[1] = subs->freqn >> 10;
374 cp[2] = subs->freqn >> 18;
375 return 0;
376}
377
378/*
379 * prepare urb for high speed capture sync pipe
380 *
381 * fill the length and offset of each urb descriptor.
382 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
383 */
384static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
385 struct snd_pcm_runtime *runtime,
386 struct urb *urb)
387{
388 unsigned char *cp = urb->transfer_buffer;
389 struct snd_urb_ctx *ctx = urb->context;
390
391 urb->dev = ctx->subs->dev; /* we need to set this at each time */
392 urb->iso_frame_desc[0].length = 4;
393 urb->iso_frame_desc[0].offset = 0;
394 cp[0] = subs->freqn;
395 cp[1] = subs->freqn >> 8;
396 cp[2] = subs->freqn >> 16;
397 cp[3] = subs->freqn >> 24;
398 return 0;
399}
400
401/*
402 * process after capture sync complete
403 * - nothing to do
404 */
405static int retire_capture_sync_urb(struct snd_usb_substream *subs,
406 struct snd_pcm_runtime *runtime,
407 struct urb *urb)
408{
409 return 0;
410}
411
412/*
413 * prepare urb for capture data pipe
414 *
415 * fill the offset and length of each descriptor.
416 *
417 * we use a temporary buffer to write the captured data.
418 * since the length of written data is determined by host, we cannot
419 * write onto the pcm buffer directly... the data is thus copied
420 * later at complete callback to the global buffer.
421 */
422static int prepare_capture_urb(struct snd_usb_substream *subs,
423 struct snd_pcm_runtime *runtime,
424 struct urb *urb)
425{
426 int i, offs;
427 struct snd_urb_ctx *ctx = urb->context;
428
429 offs = 0;
430 urb->dev = ctx->subs->dev; /* we need to set this at each time */
431 for (i = 0; i < ctx->packets; i++) {
432 urb->iso_frame_desc[i].offset = offs;
433 urb->iso_frame_desc[i].length = subs->curpacksize;
434 offs += subs->curpacksize;
435 }
436 urb->transfer_buffer_length = offs;
437 urb->number_of_packets = ctx->packets;
438 return 0;
439}
440
441/*
442 * process after capture complete
443 *
444 * copy the data from each desctiptor to the pcm buffer, and
445 * update the current position.
446 */
447static int retire_capture_urb(struct snd_usb_substream *subs,
448 struct snd_pcm_runtime *runtime,
449 struct urb *urb)
450{
451 unsigned long flags;
452 unsigned char *cp;
453 int i;
454 unsigned int stride, frames, bytes, oldptr;
455 int period_elapsed = 0;
456
457 stride = runtime->frame_bits >> 3;
458
459 for (i = 0; i < urb->number_of_packets; i++) {
460 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
461 if (urb->iso_frame_desc[i].status) {
462 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
463 // continue;
464 }
465 bytes = urb->iso_frame_desc[i].actual_length;
466 frames = bytes / stride;
467 if (!subs->txfr_quirk)
468 bytes = frames * stride;
469 if (bytes % (runtime->sample_bits >> 3) != 0) {
470#ifdef CONFIG_SND_DEBUG_VERBOSE
471 int oldbytes = bytes;
472#endif
473 bytes = frames * stride;
474 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
475 oldbytes, bytes);
476 }
477 /* update the current pointer */
478 spin_lock_irqsave(&subs->lock, flags);
479 oldptr = subs->hwptr_done;
480 subs->hwptr_done += bytes;
481 if (subs->hwptr_done >= runtime->buffer_size * stride)
482 subs->hwptr_done -= runtime->buffer_size * stride;
483 frames = (bytes + (oldptr % stride)) / stride;
484 subs->transfer_done += frames;
485 if (subs->transfer_done >= runtime->period_size) {
486 subs->transfer_done -= runtime->period_size;
487 period_elapsed = 1;
488 }
489 spin_unlock_irqrestore(&subs->lock, flags);
490 /* copy a data chunk */
491 if (oldptr + bytes > runtime->buffer_size * stride) {
492 unsigned int bytes1 =
493 runtime->buffer_size * stride - oldptr;
494 memcpy(runtime->dma_area + oldptr, cp, bytes1);
495 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
496 } else {
497 memcpy(runtime->dma_area + oldptr, cp, bytes);
498 }
499 }
500 if (period_elapsed)
501 snd_pcm_period_elapsed(subs->pcm_substream);
502 return 0;
503}
504
505/*
506 * Process after capture complete when paused. Nothing to do.
507 */
508static int retire_paused_capture_urb(struct snd_usb_substream *subs,
509 struct snd_pcm_runtime *runtime,
510 struct urb *urb)
511{
512 return 0;
513}
514
515
516/*
517 * prepare urb for playback sync pipe
518 *
519 * set up the offset and length to receive the current frequency.
520 */
521static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
522 struct snd_pcm_runtime *runtime,
523 struct urb *urb)
524{
525 struct snd_urb_ctx *ctx = urb->context;
526
527 urb->dev = ctx->subs->dev; /* we need to set this at each time */
528 urb->iso_frame_desc[0].length = min(4u, ctx->subs->syncmaxsize);
529 urb->iso_frame_desc[0].offset = 0;
530 return 0;
531}
532
533/*
534 * process after playback sync complete
535 *
536 * Full speed devices report feedback values in 10.14 format as samples per
537 * frame, high speed devices in 16.16 format as samples per microframe.
538 * Because the Audio Class 1 spec was written before USB 2.0, many high speed
539 * devices use a wrong interpretation, some others use an entirely different
540 * format. Therefore, we cannot predict what format any particular device uses
541 * and must detect it automatically.
542 */
543static int retire_playback_sync_urb(struct snd_usb_substream *subs,
544 struct snd_pcm_runtime *runtime,
545 struct urb *urb)
546{
547 unsigned int f;
548 int shift;
549 unsigned long flags;
550
551 if (urb->iso_frame_desc[0].status != 0 ||
552 urb->iso_frame_desc[0].actual_length < 3)
553 return 0;
554
555 f = le32_to_cpup(urb->transfer_buffer);
556 if (urb->iso_frame_desc[0].actual_length == 3)
557 f &= 0x00ffffff;
558 else
559 f &= 0x0fffffff;
560 if (f == 0)
561 return 0;
562
563 if (unlikely(subs->freqshift == INT_MIN)) {
564 /*
565 * The first time we see a feedback value, determine its format
566 * by shifting it left or right until it matches the nominal
567 * frequency value. This assumes that the feedback does not
568 * differ from the nominal value more than +50% or -25%.
569 */
570 shift = 0;
571 while (f < subs->freqn - subs->freqn / 4) {
572 f <<= 1;
573 shift++;
574 }
575 while (f > subs->freqn + subs->freqn / 2) {
576 f >>= 1;
577 shift--;
578 }
579 subs->freqshift = shift;
580 }
581 else if (subs->freqshift >= 0)
582 f <<= subs->freqshift;
583 else
584 f >>= -subs->freqshift;
585
586 if (likely(f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax)) {
587 /*
588 * If the frequency looks valid, set it.
589 * This value is referred to in prepare_playback_urb().
590 */
591 spin_lock_irqsave(&subs->lock, flags);
592 subs->freqm = f;
593 spin_unlock_irqrestore(&subs->lock, flags);
594 } else {
595 /*
596 * Out of range; maybe the shift value is wrong.
597 * Reset it so that we autodetect again the next time.
598 */
599 subs->freqshift = INT_MIN;
600 }
601
602 return 0;
603}
604
605/* determine the number of frames in the next packet */
606static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
607{
608 if (subs->fill_max)
609 return subs->maxframesize;
610 else {
611 subs->phase = (subs->phase & 0xffff)
612 + (subs->freqm << subs->datainterval);
613 return min(subs->phase >> 16, subs->maxframesize);
614 }
615}
616
617/*
618 * Prepare urb for streaming before playback starts or when paused.
619 *
620 * We don't have any data, so we send silence.
621 */
622static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
623 struct snd_pcm_runtime *runtime,
624 struct urb *urb)
625{
626 unsigned int i, offs, counts;
627 struct snd_urb_ctx *ctx = urb->context;
628 int stride = runtime->frame_bits >> 3;
629
630 offs = 0;
631 urb->dev = ctx->subs->dev;
632 for (i = 0; i < ctx->packets; ++i) {
633 counts = snd_usb_audio_next_packet_size(subs);
634 urb->iso_frame_desc[i].offset = offs * stride;
635 urb->iso_frame_desc[i].length = counts * stride;
636 offs += counts;
637 }
638 urb->number_of_packets = ctx->packets;
639 urb->transfer_buffer_length = offs * stride;
640 memset(urb->transfer_buffer,
641 runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
642 offs * stride);
643 return 0;
644}
645
646/*
647 * prepare urb for playback data pipe
648 *
649 * Since a URB can handle only a single linear buffer, we must use double
650 * buffering when the data to be transferred overflows the buffer boundary.
651 * To avoid inconsistencies when updating hwptr_done, we use double buffering
652 * for all URBs.
653 */
654static int prepare_playback_urb(struct snd_usb_substream *subs,
655 struct snd_pcm_runtime *runtime,
656 struct urb *urb)
657{
658 int i, stride;
659 unsigned int counts, frames, bytes;
660 unsigned long flags;
661 int period_elapsed = 0;
662 struct snd_urb_ctx *ctx = urb->context;
663
664 stride = runtime->frame_bits >> 3;
665
666 frames = 0;
667 urb->dev = ctx->subs->dev; /* we need to set this at each time */
668 urb->number_of_packets = 0;
669 spin_lock_irqsave(&subs->lock, flags);
670 for (i = 0; i < ctx->packets; i++) {
671 counts = snd_usb_audio_next_packet_size(subs);
672 /* set up descriptor */
673 urb->iso_frame_desc[i].offset = frames * stride;
674 urb->iso_frame_desc[i].length = counts * stride;
675 frames += counts;
676 urb->number_of_packets++;
677 subs->transfer_done += counts;
678 if (subs->transfer_done >= runtime->period_size) {
679 subs->transfer_done -= runtime->period_size;
680 period_elapsed = 1;
681 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
682 if (subs->transfer_done > 0) {
683 /* FIXME: fill-max mode is not
684 * supported yet */
685 frames -= subs->transfer_done;
686 counts -= subs->transfer_done;
687 urb->iso_frame_desc[i].length =
688 counts * stride;
689 subs->transfer_done = 0;
690 }
691 i++;
692 if (i < ctx->packets) {
693 /* add a transfer delimiter */
694 urb->iso_frame_desc[i].offset =
695 frames * stride;
696 urb->iso_frame_desc[i].length = 0;
697 urb->number_of_packets++;
698 }
699 break;
700 }
701 }
702 if (period_elapsed) /* finish at the period boundary */
703 break;
704 }
705 bytes = frames * stride;
706 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
707 /* err, the transferred area goes over buffer boundary. */
708 unsigned int bytes1 =
709 runtime->buffer_size * stride - subs->hwptr_done;
710 memcpy(urb->transfer_buffer,
711 runtime->dma_area + subs->hwptr_done, bytes1);
712 memcpy(urb->transfer_buffer + bytes1,
713 runtime->dma_area, bytes - bytes1);
714 } else {
715 memcpy(urb->transfer_buffer,
716 runtime->dma_area + subs->hwptr_done, bytes);
717 }
718 subs->hwptr_done += bytes;
719 if (subs->hwptr_done >= runtime->buffer_size * stride)
720 subs->hwptr_done -= runtime->buffer_size * stride;
721 runtime->delay += frames;
722 spin_unlock_irqrestore(&subs->lock, flags);
723 urb->transfer_buffer_length = bytes;
724 if (period_elapsed)
725 snd_pcm_period_elapsed(subs->pcm_substream);
726 return 0;
727}
728
729/*
730 * process after playback data complete
731 * - decrease the delay count again
732 */
733static int retire_playback_urb(struct snd_usb_substream *subs,
734 struct snd_pcm_runtime *runtime,
735 struct urb *urb)
736{
737 unsigned long flags;
738 int stride = runtime->frame_bits >> 3;
739 int processed = urb->transfer_buffer_length / stride;
740
741 spin_lock_irqsave(&subs->lock, flags);
742 if (processed > runtime->delay)
743 runtime->delay = 0;
744 else
745 runtime->delay -= processed;
746 spin_unlock_irqrestore(&subs->lock, flags);
747 return 0;
748}
749
750static const char *usb_error_string(int err)
751{
752 switch (err) {
753 case -ENODEV:
754 return "no device";
755 case -ENOENT:
756 return "endpoint not enabled";
757 case -EPIPE:
758 return "endpoint stalled";
759 case -ENOSPC:
760 return "not enough bandwidth";
761 case -ESHUTDOWN:
762 return "device disabled";
763 case -EHOSTUNREACH:
764 return "device suspended";
765 case -EINVAL:
766 case -EAGAIN:
767 case -EFBIG:
768 case -EMSGSIZE:
769 return "internal error";
770 default:
771 return "unknown error";
772 }
773}
774
775/*
776 * set up and start data/sync urbs
777 */
778static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
779{
780 unsigned int i;
781 int err;
782
783 if (subs->stream->chip->shutdown)
784 return -EBADFD;
785
786 for (i = 0; i < subs->nurbs; i++) {
787 if (snd_BUG_ON(!subs->dataurb[i].urb))
788 return -EINVAL;
789 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
790 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
791 goto __error;
792 }
793 }
794 if (subs->syncpipe) {
795 for (i = 0; i < SYNC_URBS; i++) {
796 if (snd_BUG_ON(!subs->syncurb[i].urb))
797 return -EINVAL;
798 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
799 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
800 goto __error;
801 }
802 }
803 }
804
805 subs->active_mask = 0;
806 subs->unlink_mask = 0;
807 subs->running = 1;
808 for (i = 0; i < subs->nurbs; i++) {
809 err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
810 if (err < 0) {
811 snd_printk(KERN_ERR "cannot submit datapipe "
812 "for urb %d, error %d: %s\n",
813 i, err, usb_error_string(err));
814 goto __error;
815 }
816 set_bit(i, &subs->active_mask);
817 }
818 if (subs->syncpipe) {
819 for (i = 0; i < SYNC_URBS; i++) {
820 err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
821 if (err < 0) {
822 snd_printk(KERN_ERR "cannot submit syncpipe "
823 "for urb %d, error %d: %s\n",
824 i, err, usb_error_string(err));
825 goto __error;
826 }
827 set_bit(i + 16, &subs->active_mask);
828 }
829 }
830 return 0;
831
832 __error:
833 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
834 deactivate_urbs(subs, 0, 0);
835 return -EPIPE;
836}
837
838
839/*
840 */
841static struct snd_urb_ops audio_urb_ops[2] = {
842 {
843 .prepare = prepare_nodata_playback_urb,
844 .retire = retire_playback_urb,
845 .prepare_sync = prepare_playback_sync_urb,
846 .retire_sync = retire_playback_sync_urb,
847 },
848 {
849 .prepare = prepare_capture_urb,
850 .retire = retire_capture_urb,
851 .prepare_sync = prepare_capture_sync_urb,
852 .retire_sync = retire_capture_sync_urb,
853 },
854};
855
856/*
857 * initialize the substream instance.
858 */
859
860void snd_usb_init_substream(struct snd_usb_stream *as,
861 int stream, struct audioformat *fp)
862{
863 struct snd_usb_substream *subs = &as->substream[stream];
864
865 INIT_LIST_HEAD(&subs->fmt_list);
866 spin_lock_init(&subs->lock);
867
868 subs->stream = as;
869 subs->direction = stream;
870 subs->dev = as->chip->dev;
871 subs->txfr_quirk = as->chip->txfr_quirk;
872 subs->ops = audio_urb_ops[stream];
873 if (snd_usb_get_speed(subs->dev) >= USB_SPEED_HIGH)
874 subs->ops.prepare_sync = prepare_capture_sync_urb_hs;
875
876 snd_usb_set_pcm_ops(as->pcm, stream);
877
878 list_add_tail(&fp->list, &subs->fmt_list);
879 subs->formats |= fp->formats;
880 subs->endpoint = fp->endpoint;
881 subs->num_formats++;
882 subs->fmt_type = fp->fmt_type;
883}
884
885int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd)
886{
887 struct snd_usb_substream *subs = substream->runtime->private_data;
888
889 switch (cmd) {
890 case SNDRV_PCM_TRIGGER_START:
891 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
892 subs->ops.prepare = prepare_playback_urb;
893 return 0;
894 case SNDRV_PCM_TRIGGER_STOP:
895 return deactivate_urbs(subs, 0, 0);
896 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
897 subs->ops.prepare = prepare_nodata_playback_urb;
898 return 0;
899 }
900
901 return -EINVAL;
902}
903
904int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)
905{
906 struct snd_usb_substream *subs = substream->runtime->private_data;
907
908 switch (cmd) {
909 case SNDRV_PCM_TRIGGER_START:
910 subs->ops.retire = retire_capture_urb;
911 return start_urbs(subs, substream->runtime);
912 case SNDRV_PCM_TRIGGER_STOP:
913 return deactivate_urbs(subs, 0, 0);
914 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
915 subs->ops.retire = retire_paused_capture_urb;
916 return 0;
917 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
918 subs->ops.retire = retire_capture_urb;
919 return 0;
920 }
921
922 return -EINVAL;
923}
924
925int snd_usb_substream_prepare(struct snd_usb_substream *subs,
926 struct snd_pcm_runtime *runtime)
927{
928 /* clear urbs (to be sure) */
929 deactivate_urbs(subs, 0, 1);
930 wait_clear_urbs(subs);
931
932 /* for playback, submit the URBs now; otherwise, the first hwptr_done
933 * updates for all URBs would happen at the same time when starting */
934 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
935 subs->ops.prepare = prepare_nodata_playback_urb;
936 return start_urbs(subs, runtime);
937 }
938
939 return 0;
940}
941
diff --git a/sound/usb/urb.h b/sound/usb/urb.h
deleted file mode 100644
index 888da38079cf..000000000000
--- a/sound/usb/urb.h
+++ /dev/null
@@ -1,21 +0,0 @@
1#ifndef __USBAUDIO_URB_H
2#define __USBAUDIO_URB_H
3
4void snd_usb_init_substream(struct snd_usb_stream *as,
5 int stream,
6 struct audioformat *fp);
7
8int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
9 unsigned int period_bytes,
10 unsigned int rate,
11 unsigned int frame_bits);
12
13void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force);
14
15int snd_usb_substream_prepare(struct snd_usb_substream *subs,
16 struct snd_pcm_runtime *runtime);
17
18int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd);
19int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd);
20
21#endif /* __USBAUDIO_URB_H */
diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h
index 1e79986b5777..3e2b03577936 100644
--- a/sound/usb/usbaudio.h
+++ b/sound/usb/usbaudio.h
@@ -80,6 +80,7 @@ enum quirk_type {
80 QUIRK_MIDI_CME, 80 QUIRK_MIDI_CME,
81 QUIRK_MIDI_AKAI, 81 QUIRK_MIDI_AKAI,
82 QUIRK_MIDI_US122L, 82 QUIRK_MIDI_US122L,
83 QUIRK_MIDI_FTDI,
83 QUIRK_AUDIO_STANDARD_INTERFACE, 84 QUIRK_AUDIO_STANDARD_INTERFACE,
84 QUIRK_AUDIO_FIXED_ENDPOINT, 85 QUIRK_AUDIO_FIXED_ENDPOINT,
85 QUIRK_AUDIO_EDIROL_UAXX, 86 QUIRK_AUDIO_EDIROL_UAXX,