1From b4c068e8e4077b2d4082e3f7e58e63ecd980db8b Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Wed, 29 Apr 2020 11:48:28 +0800
4Subject: [PATCH 15/20] adbd: usb_linux_client: Compat with old kernel
5
6Port related logic from upstream(without os desc):
7git log --oneline adb/daemon/usb_ffs.cpp
89026a44bb Revert "Reland "adb: daemon: Assign valid fd to usb_handle ep0 file descriptor""
90871824de Move adbd's legacy USB implementation to fastboot.
10bfe3dac36 Reland "adb: daemon: Assign valid fd to usb_handle ep0 file descriptor"
11b310da608 adbd: Update DeviceInterfaceGUID for WinUSB
12134c98814 Revert "adb: daemon: Assign valid fd to usb_handle ep0 file descriptor" am: ba4684c2b2
13
14Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
15---
16 core/adbd/usb_linux_client.c | 252 ++++++++++++++++++++---------------
17 1 file changed, 144 insertions(+), 108 deletions(-)
18
19diff --git a/core/adbd/usb_linux_client.c b/core/adbd/usb_linux_client.c
20index a437ad1..e17e07d 100644
21--- a/core/adbd/usb_linux_client.c
22+++ b/core/adbd/usb_linux_client.c
23@@ -63,119 +63,130 @@ struct usb_handle
24     int bulk_in;  /* "in" from the host's perspective => sink for adbd */
25 };
26
27-static const struct {
28+// clang-format off
29+struct func_desc {
30+    struct usb_interface_descriptor intf;
31+    struct usb_endpoint_descriptor_no_audio source;
32+    struct usb_endpoint_descriptor_no_audio sink;
33+} __attribute__((packed));
34+
35+struct ss_func_desc {
36+    struct usb_interface_descriptor intf;
37+    struct usb_endpoint_descriptor_no_audio source;
38+    struct usb_ss_ep_comp_descriptor source_comp;
39+    struct usb_endpoint_descriptor_no_audio sink;
40+    struct usb_ss_ep_comp_descriptor sink_comp;
41+} __attribute__((packed));
42+
43+struct desc_v1 {
44+    struct usb_functionfs_descs_head_v1 {
45+        __le32 magic;
46+        __le32 length;
47+        __le32 fs_count;
48+        __le32 hs_count;
49+    } __attribute__((packed)) header;
50+    struct func_desc fs_descs, hs_descs;
51+} __attribute__((packed));
52+
53+struct desc_v2 {
54     __le32 magic;
55     __le32 length;
56     __le32 flags;
57     __le32 fs_count;
58     __le32 hs_count;
59     __le32 ss_count;
60-    struct {
61-        struct usb_interface_descriptor intf;
62-        struct usb_endpoint_descriptor_no_audio source;
63-        struct usb_endpoint_descriptor_no_audio sink;
64-    } __attribute__((packed)) fs_descs, hs_descs;
65-    struct {
66-        struct usb_interface_descriptor intf;
67-	struct usb_endpoint_descriptor_no_audio source;
68-	struct usb_ss_ep_comp_descriptor source_comp;
69-	struct usb_endpoint_descriptor_no_audio sink;
70-	struct usb_ss_ep_comp_descriptor sink_comp;
71-    } __attribute__((packed)) ss_descs;
72-} __attribute__((packed)) descriptors = {
73-    .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
74-    .length = cpu_to_le32(sizeof(descriptors)),
75-    .flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC |
76-                         FUNCTIONFS_HAS_HS_DESC |
77-                         FUNCTIONFS_HAS_SS_DESC),
78-    .fs_count = 3,
79-    .hs_count = 3,
80-    .ss_count = 5,
81-    .fs_descs = {
82-        .intf = {
83-            .bLength = sizeof(descriptors.fs_descs.intf),
84-            .bDescriptorType = USB_DT_INTERFACE,
85-            .bInterfaceNumber = 0,
86-            .bNumEndpoints = 2,
87-            .bInterfaceClass = ADB_CLASS,
88-            .bInterfaceSubClass = ADB_SUBCLASS,
89-            .bInterfaceProtocol = ADB_PROTOCOL,
90-            .iInterface = 1, /* first string from the provided table */
91-        },
92-        .source = {
93-            .bLength = sizeof(descriptors.fs_descs.source),
94-            .bDescriptorType = USB_DT_ENDPOINT,
95-            .bEndpointAddress = 1 | USB_DIR_OUT,
96-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
97-            .wMaxPacketSize = MAX_PACKET_SIZE_FS,
98-        },
99-        .sink = {
100-            .bLength = sizeof(descriptors.fs_descs.sink),
101-            .bDescriptorType = USB_DT_ENDPOINT,
102-            .bEndpointAddress = 2 | USB_DIR_IN,
103-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
104-            .wMaxPacketSize = MAX_PACKET_SIZE_FS,
105-        },
106+    struct func_desc fs_descs, hs_descs;
107+    struct ss_func_desc ss_descs;
108+} __attribute__((packed));
109+
110+static struct func_desc fs_descriptors = {
111+    .intf = {
112+        .bLength = sizeof(fs_descriptors.intf),
113+        .bDescriptorType = USB_DT_INTERFACE,
114+        .bInterfaceNumber = 0,
115+        .bNumEndpoints = 2,
116+        .bInterfaceClass = ADB_CLASS,
117+        .bInterfaceSubClass = ADB_SUBCLASS,
118+        .bInterfaceProtocol = ADB_PROTOCOL,
119+        .iInterface = 1, /* first string from the provided table */
120+    },
121+    .source = {
122+        .bLength = sizeof(fs_descriptors.source),
123+        .bDescriptorType = USB_DT_ENDPOINT,
124+        .bEndpointAddress = 1 | USB_DIR_OUT,
125+        .bmAttributes = USB_ENDPOINT_XFER_BULK,
126+        .wMaxPacketSize = MAX_PACKET_SIZE_FS,
127+    },
128+    .sink = {
129+        .bLength = sizeof(fs_descriptors.sink),
130+        .bDescriptorType = USB_DT_ENDPOINT,
131+        .bEndpointAddress = 2 | USB_DIR_IN,
132+        .bmAttributes = USB_ENDPOINT_XFER_BULK,
133+        .wMaxPacketSize = MAX_PACKET_SIZE_FS,
134     },
135-    .hs_descs = {
136-        .intf = {
137-            .bLength = sizeof(descriptors.hs_descs.intf),
138-            .bDescriptorType = USB_DT_INTERFACE,
139-            .bInterfaceNumber = 0,
140-            .bNumEndpoints = 2,
141-            .bInterfaceClass = ADB_CLASS,
142-            .bInterfaceSubClass = ADB_SUBCLASS,
143-            .bInterfaceProtocol = ADB_PROTOCOL,
144-            .iInterface = 1, /* first string from the provided table */
145-        },
146-        .source = {
147-            .bLength = sizeof(descriptors.hs_descs.source),
148-            .bDescriptorType = USB_DT_ENDPOINT,
149-            .bEndpointAddress = 1 | USB_DIR_OUT,
150-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
151-            .wMaxPacketSize = MAX_PACKET_SIZE_HS,
152-        },
153-        .sink = {
154-            .bLength = sizeof(descriptors.hs_descs.sink),
155-            .bDescriptorType = USB_DT_ENDPOINT,
156-            .bEndpointAddress = 2 | USB_DIR_IN,
157-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
158-            .wMaxPacketSize = MAX_PACKET_SIZE_HS,
159-        },
160+};
161+
162+static struct func_desc hs_descriptors = {
163+    .intf = {
164+        .bLength = sizeof(hs_descriptors.intf),
165+        .bDescriptorType = USB_DT_INTERFACE,
166+        .bInterfaceNumber = 0,
167+        .bNumEndpoints = 2,
168+        .bInterfaceClass = ADB_CLASS,
169+        .bInterfaceSubClass = ADB_SUBCLASS,
170+        .bInterfaceProtocol = ADB_PROTOCOL,
171+        .iInterface = 1, /* first string from the provided table */
172+    },
173+    .source = {
174+        .bLength = sizeof(hs_descriptors.source),
175+        .bDescriptorType = USB_DT_ENDPOINT,
176+        .bEndpointAddress = 1 | USB_DIR_OUT,
177+        .bmAttributes = USB_ENDPOINT_XFER_BULK,
178+        .wMaxPacketSize = MAX_PACKET_SIZE_HS,
179     },
180-    .ss_descs = {
181-        .intf = {
182-            .bLength = sizeof(descriptors.ss_descs.intf),
183-            .bDescriptorType = USB_DT_INTERFACE,
184-            .bInterfaceNumber = 0,
185-            .bNumEndpoints = 2,
186-            .bInterfaceClass = ADB_CLASS,
187-            .bInterfaceSubClass = ADB_SUBCLASS,
188-            .bInterfaceProtocol = ADB_PROTOCOL,
189-            .iInterface = 1, /* first string from the provided table */
190-        },
191-        .source = {
192-            .bLength = sizeof(descriptors.ss_descs.source),
193-            .bDescriptorType = USB_DT_ENDPOINT,
194-            .bEndpointAddress = 1 | USB_DIR_OUT,
195-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
196-            .wMaxPacketSize = MAX_PACKET_SIZE_SS,
197-        },
198-        .source_comp = {
199-            .bLength = sizeof(descriptors.ss_descs.source_comp),
200-            .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
201-        },
202-        .sink = {
203-            .bLength = sizeof(descriptors.ss_descs.sink),
204-            .bDescriptorType = USB_DT_ENDPOINT,
205-            .bEndpointAddress = 2 | USB_DIR_IN,
206-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
207-            .wMaxPacketSize = MAX_PACKET_SIZE_SS,
208-        },
209-        .sink_comp = {
210-            .bLength = sizeof(descriptors.ss_descs.sink_comp),
211-            .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
212-        },
213+    .sink = {
214+        .bLength = sizeof(hs_descriptors.sink),
215+        .bDescriptorType = USB_DT_ENDPOINT,
216+        .bEndpointAddress = 2 | USB_DIR_IN,
217+        .bmAttributes = USB_ENDPOINT_XFER_BULK,
218+        .wMaxPacketSize = MAX_PACKET_SIZE_HS,
219+    },
220+};
221+
222+static struct ss_func_desc ss_descriptors = {
223+    .intf = {
224+        .bLength = sizeof(ss_descriptors.intf),
225+        .bDescriptorType = USB_DT_INTERFACE,
226+        .bInterfaceNumber = 0,
227+        .bNumEndpoints = 2,
228+        .bInterfaceClass = ADB_CLASS,
229+        .bInterfaceSubClass = ADB_SUBCLASS,
230+        .bInterfaceProtocol = ADB_PROTOCOL,
231+        .iInterface = 1, /* first string from the provided table */
232+    },
233+    .source = {
234+        .bLength = sizeof(ss_descriptors.source),
235+        .bDescriptorType = USB_DT_ENDPOINT,
236+        .bEndpointAddress = 1 | USB_DIR_OUT,
237+        .bmAttributes = USB_ENDPOINT_XFER_BULK,
238+        .wMaxPacketSize = MAX_PACKET_SIZE_SS,
239+    },
240+    .source_comp = {
241+        .bLength = sizeof(ss_descriptors.source_comp),
242+        .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
243+        .bMaxBurst = 4,
244+    },
245+    .sink = {
246+        .bLength = sizeof(ss_descriptors.sink),
247+        .bDescriptorType = USB_DT_ENDPOINT,
248+        .bEndpointAddress = 2 | USB_DIR_IN,
249+        .bmAttributes = USB_ENDPOINT_XFER_BULK,
250+        .wMaxPacketSize = MAX_PACKET_SIZE_SS,
251+    },
252+    .sink_comp = {
253+        .bLength = sizeof(ss_descriptors.sink_comp),
254+        .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
255+        .bMaxBurst = 4,
256     },
257 };
258
259@@ -318,8 +329,21 @@ static void usb_adb_init()
260
261 static void init_functionfs(struct usb_handle *h)
262 {
263+    struct desc_v1 v1_descriptor = {};
264+    struct desc_v2 v2_descriptor = {};
265     ssize_t ret;
266
267+    v2_descriptor.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
268+    v2_descriptor.length = cpu_to_le32(sizeof(v2_descriptor));
269+    v2_descriptor.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
270+                          FUNCTIONFS_HAS_SS_DESC;
271+    v2_descriptor.fs_count = 3;
272+    v2_descriptor.hs_count = 3;
273+    v2_descriptor.ss_count = 5;
274+    v2_descriptor.fs_descs = fs_descriptors;
275+    v2_descriptor.hs_descs = hs_descriptors;
276+    v2_descriptor.ss_descs = ss_descriptors;
277+
278     D("OPENING %s\n", USB_FFS_ADB_EP0);
279     h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
280     if (h->control < 0) {
281@@ -327,10 +351,22 @@ static void init_functionfs(struct usb_handle *h)
282         goto err;
283     }
284
285-    ret = adb_write(h->control, &descriptors, sizeof(descriptors));
286+    ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
287     if (ret < 0) {
288-        D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
289-        goto err;
290+        D("[ %s: Switching to V1_descriptor format errno=%s ]", USB_FFS_ADB_EP0,
291+          strerror(errno));
292+        v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
293+        v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
294+        v1_descriptor.header.fs_count = 3;
295+        v1_descriptor.header.hs_count = 3;
296+        v1_descriptor.fs_descs = fs_descriptors;
297+        v1_descriptor.hs_descs = hs_descriptors;
298+        ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
299+        if (ret < 0) {
300+            D("[ %s: write descriptors failed: errno=%s ]\n", USB_FFS_ADB_EP0,
301+              strerror(errno));
302+            goto err;
303+        }
304     }
305
306     ret = adb_write(h->control, &strings, sizeof(strings));
307--
3082.20.1
309
310