• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define TRACE_TAG USB
18 
19 #include "sysdeps.h"
20 
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/functionfs.h>
23 
24 #include <android-base/logging.h>
25 #include <android-base/properties.h>
26 #include <android-base/unique_fd.h>
27 
28 #include "adb.h"
29 #include "adbd/usb.h"
30 
31 #define MAX_PACKET_SIZE_FS 64
32 #define MAX_PACKET_SIZE_HS 512
33 #define MAX_PACKET_SIZE_SS 1024
34 
35 #define USB_FFS_BULK_SIZE 16384
36 
37 // Number of buffers needed to fit MAX_PAYLOAD, with an extra for ZLPs.
38 #define USB_FFS_NUM_BUFS ((4 * MAX_PAYLOAD / USB_FFS_BULK_SIZE) + 1)
39 
40 #define cpu_to_le16(x) htole16(x)
41 #define cpu_to_le32(x) htole32(x)
42 
43 struct func_desc {
44     struct usb_interface_descriptor intf;
45     struct usb_endpoint_descriptor_no_audio source;
46     struct usb_endpoint_descriptor_no_audio sink;
47 } __attribute__((packed));
48 
49 struct ss_func_desc {
50     struct usb_interface_descriptor intf;
51     struct usb_endpoint_descriptor_no_audio source;
52     struct usb_ss_ep_comp_descriptor source_comp;
53     struct usb_endpoint_descriptor_no_audio sink;
54     struct usb_ss_ep_comp_descriptor sink_comp;
55 } __attribute__((packed));
56 
57 struct desc_v1 {
58     struct usb_functionfs_descs_head_v1 {
59         __le32 magic;
60         __le32 length;
61         __le32 fs_count;
62         __le32 hs_count;
63     } __attribute__((packed)) header;
64     struct func_desc fs_descs, hs_descs;
65 } __attribute__((packed));
66 
67 struct desc_v2 {
68     struct usb_functionfs_descs_head_v2 header;
69     // The rest of the structure depends on the flags in the header.
70     __le32 fs_count;
71     __le32 hs_count;
72     __le32 ss_count;
73     __le32 os_count;
74     struct func_desc fs_descs, hs_descs;
75     struct ss_func_desc ss_descs;
76     struct usb_os_desc_header os_header;
77     struct usb_ext_compat_desc os_desc;
78 } __attribute__((packed));
79 
80 // clang-format off
81 static struct func_desc fs_descriptors = {
82     .intf = {
83         .bLength = sizeof(fs_descriptors.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(fs_descriptors.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(fs_descriptors.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 };
107 
108 static struct func_desc hs_descriptors = {
109     .intf = {
110         .bLength = sizeof(hs_descriptors.intf),
111         .bDescriptorType = USB_DT_INTERFACE,
112         .bInterfaceNumber = 0,
113         .bNumEndpoints = 2,
114         .bInterfaceClass = ADB_CLASS,
115         .bInterfaceSubClass = ADB_SUBCLASS,
116         .bInterfaceProtocol = ADB_PROTOCOL,
117         .iInterface = 1, /* first string from the provided table */
118     },
119     .source = {
120         .bLength = sizeof(hs_descriptors.source),
121         .bDescriptorType = USB_DT_ENDPOINT,
122         .bEndpointAddress = 1 | USB_DIR_OUT,
123         .bmAttributes = USB_ENDPOINT_XFER_BULK,
124         .wMaxPacketSize = MAX_PACKET_SIZE_HS,
125     },
126     .sink = {
127         .bLength = sizeof(hs_descriptors.sink),
128         .bDescriptorType = USB_DT_ENDPOINT,
129         .bEndpointAddress = 2 | USB_DIR_IN,
130         .bmAttributes = USB_ENDPOINT_XFER_BULK,
131         .wMaxPacketSize = MAX_PACKET_SIZE_HS,
132     },
133 };
134 
135 static struct ss_func_desc ss_descriptors = {
136     .intf = {
137         .bLength = sizeof(ss_descriptors.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(ss_descriptors.source),
148         .bDescriptorType = USB_DT_ENDPOINT,
149         .bEndpointAddress = 1 | USB_DIR_OUT,
150         .bmAttributes = USB_ENDPOINT_XFER_BULK,
151         .wMaxPacketSize = MAX_PACKET_SIZE_SS,
152     },
153     .source_comp = {
154         .bLength = sizeof(ss_descriptors.source_comp),
155         .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
156         .bMaxBurst = 4,
157     },
158     .sink = {
159         .bLength = sizeof(ss_descriptors.sink),
160         .bDescriptorType = USB_DT_ENDPOINT,
161         .bEndpointAddress = 2 | USB_DIR_IN,
162         .bmAttributes = USB_ENDPOINT_XFER_BULK,
163         .wMaxPacketSize = MAX_PACKET_SIZE_SS,
164     },
165     .sink_comp = {
166         .bLength = sizeof(ss_descriptors.sink_comp),
167         .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
168         .bMaxBurst = 4,
169     },
170 };
171 
172 struct usb_ext_compat_desc os_desc_compat = {
173     .bFirstInterfaceNumber = 0,
174     .Reserved1 = cpu_to_le32(1),
175     .CompatibleID = {0},
176     .SubCompatibleID = {0},
177     .Reserved2 = {0},
178 };
179 
180 static struct usb_os_desc_header os_desc_header = {
181     .interface = cpu_to_le32(1),
182     .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)),
183     .bcdVersion = cpu_to_le32(1),
184     .wIndex = cpu_to_le32(4),
185     .bCount = cpu_to_le32(1),
186     .Reserved = cpu_to_le32(0),
187 };
188 
189 #define STR_INTERFACE_ "ADB Interface"
190 
191 static const struct {
192     struct usb_functionfs_strings_head header;
193     struct {
194         __le16 code;
195         const char str1[sizeof(STR_INTERFACE_)];
196     } __attribute__((packed)) lang0;
197 } __attribute__((packed)) strings = {
198     .header = {
199         .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
200         .length = cpu_to_le32(sizeof(strings)),
201         .str_count = cpu_to_le32(1),
202         .lang_count = cpu_to_le32(1),
203     },
204     .lang0 = {
205         cpu_to_le16(0x0409), /* en-us */
206         STR_INTERFACE_,
207     },
208 };
209 // clang-format on
210 
open_functionfs(android::base::unique_fd * out_control,android::base::unique_fd * out_bulk_out,android::base::unique_fd * out_bulk_in)211 bool open_functionfs(android::base::unique_fd* out_control, android::base::unique_fd* out_bulk_out,
212                      android::base::unique_fd* out_bulk_in) {
213     unique_fd control, bulk_out, bulk_in;
214     struct desc_v1 v1_descriptor = {};
215     struct desc_v2 v2_descriptor = {};
216 
217     v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
218     v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
219     v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
220                                  FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC;
221     v2_descriptor.fs_count = 3;
222     v2_descriptor.hs_count = 3;
223     v2_descriptor.ss_count = 5;
224     v2_descriptor.os_count = 1;
225     v2_descriptor.fs_descs = fs_descriptors;
226     v2_descriptor.hs_descs = hs_descriptors;
227     v2_descriptor.ss_descs = ss_descriptors;
228     v2_descriptor.os_header = os_desc_header;
229     v2_descriptor.os_desc = os_desc_compat;
230 
231     if (out_control->get() < 0) {  // might have already done this before
232         LOG(INFO) << "opening control endpoint " << USB_FFS_ADB_EP0;
233         control.reset(adb_open(USB_FFS_ADB_EP0, O_RDWR));
234         if (control < 0) {
235             PLOG(ERROR) << "cannot open control endpoint " << USB_FFS_ADB_EP0;
236             return false;
237         }
238 
239         if (adb_write(control.get(), &v2_descriptor, sizeof(v2_descriptor)) < 0) {
240             D("[ %s: Switching to V1_descriptor format errno=%s ]", USB_FFS_ADB_EP0,
241               strerror(errno));
242             v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
243             v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
244             v1_descriptor.header.fs_count = 3;
245             v1_descriptor.header.hs_count = 3;
246             v1_descriptor.fs_descs = fs_descriptors;
247             v1_descriptor.hs_descs = hs_descriptors;
248             if (adb_write(control.get(), &v1_descriptor, sizeof(v1_descriptor)) < 0) {
249                 PLOG(ERROR) << "failed to write USB descriptors";
250                 return false;
251             }
252         }
253 
254         if (adb_write(control.get(), &strings, sizeof(strings)) < 0) {
255             PLOG(ERROR) << "failed to write USB strings";
256             return false;
257         }
258         // Signal only when writing the descriptors to ffs
259         android::base::SetProperty("sys.usb.ffs.ready", "1");
260     }
261 
262     bulk_out.reset(adb_open(USB_FFS_ADB_OUT, O_RDONLY));
263     if (bulk_out < 0) {
264         PLOG(ERROR) << "cannot open bulk-out endpoint " << USB_FFS_ADB_OUT;
265         return false;
266     }
267 
268     bulk_in.reset(adb_open(USB_FFS_ADB_IN, O_WRONLY));
269     if (bulk_in < 0) {
270         PLOG(ERROR) << "cannot open bulk-in endpoint " << USB_FFS_ADB_IN;
271         return false;
272     }
273 
274     *out_control = std::move(control);
275     *out_bulk_in = std::move(bulk_in);
276     *out_bulk_out = std::move(bulk_out);
277     return true;
278 }
279