1 // Copyright 2017 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <assert.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <threads.h>
11
12 #include <ddk/binding.h>
13 #include <ddk/debug.h>
14 #include <ddk/device.h>
15 #include <ddk/driver.h>
16 #include <ddk/metadata.h>
17 #include <ddk/protocol/usb/dci.h>
18 #include <ddk/protocol/usb/function.h>
19 #include <ddk/protocol/usb/modeswitch.h>
20 #include <zircon/listnode.h>
21 #include <zircon/device/usb-peripheral.h>
22 #include <zircon/usb/peripheral/c/fidl.h>
23 #include <zircon/hw/usb/cdc.h>
24 #include <zircon/hw/usb.h>
25
26 /*
27 THEORY OF OPERATION
28
29 This driver is responsible for USB in the peripheral role, that is,
30 acting as a USB device to a USB host.
31 It serves as the central point of coordination for the peripheral role.
32 It is configured via ioctls in the ZX_PROTOCOL_USB_DEVICE protocol
33 (which is used by the usbctl command line program).
34 Based on this configuration, it creates one or more DDK devices with protocol
35 ZX_PROTOCOL_USB_FUNCTION. These devices are bind points for USB function drivers,
36 which implement USB interfaces for particular functions (like USB ethernet or mass storage).
37 This driver also binds to a device with protocol ZX_PROTOCOL_USB_DCI
38 (Device Controller Interface) which is implemented by a driver for the actual
39 USB controller hardware for the peripheral role.
40
41 There are several steps needed to initialize and start USB in the peripheral role.
42 The first step is setting up the USB configuration via ioctls.
43 ioctl_usb_peripheral_set_device_desc() sets the USB device descriptor to be presented
44 to the host during enumeration.
45 Next, ioctl_usb_peripheral_add_function() can be called one or more times to add
46 descriptors for the USB functions to be included in the USB configuration.
47 Finally after all the functions have been added, ioctl_usb_peripheral_bind_functions()
48 tells this driver that configuration is complete and it is now possible to build
49 the configuration descriptor. Once we get to this point, usb_device_t.functions_bound
50 is set to true.
51
52 Independent of this configuration process, ioctl_usb_peripheral_set_mode() can be used
53 to configure the role of the USB controller. If the role is set to USB_MODE_PERIPHERAL
54 and "functions_bound" is true, then we are ready to start USB in peripheral role.
55 At this point, we create DDK devices for our list of functions.
56 When the function drivers bind to these functions, they register an interface of type
57 usb_function_interface_t with this driver via the usb_function_register() API.
58 Once all of the function drivers have registered themselves this way,
59 usb_device_t.functions_registered is set to true.
60
61 if the usb mode is set to USB_MODE_PERIPHERAL and "functions_registered" is true,
62 we are now finally ready to operate in the peripheral role.
63 At this point we can inform the DCI driver to start running in peripheral role
64 by calling usb_mode_switch_set_mode(USB_MODE_PERIPHERAL) on its ZX_PROTOCOL_USB_MODE_SWITCH
65 interface. Now the USB controller hardware is up and running as a USB peripheral.
66
67 Teardown of the peripheral role one of two ways:
68 First, ioctl_usb_peripheral_clear_functions() will reset this device's list of USB functions.
69 Second, the USB mode can be set to something other than USB_MODE_PERIPHERAL.
70 In this second case, we will remove the DDK devices for the USB functions
71 so the function drivers will unbind, but the USB configuration remains ready to go
72 for when the USB mode is switched back to USB_MODE_PERIPHERAL.
73 */
74
75 #define MAX_INTERFACES 32
76
77 typedef zircon_usb_peripheral_FunctionDescriptor usb_function_descriptor_t;
78
79 typedef struct {
80 zx_device_t* zxdev;
81 zx_device_t* dci_dev;
82 struct usb_device* dev;
83 list_node_t node;
84 usb_function_interface_t interface;
85 usb_function_descriptor_t desc;
86 usb_descriptor_header_t* descriptors;
87 size_t descriptors_length;
88 unsigned num_interfaces;
89 } usb_function_t;
90
91 typedef struct usb_device {
92 // the device we publish
93 zx_device_t* zxdev;
94 // our parent device
95 zx_device_t* dci_dev;
96 // our parent's DCI protocol
97 usb_dci_protocol_t usb_dci;
98 // our parent's USB switch protocol
99 usb_mode_switch_protocol_t usb_mode_switch;
100 // true if our parent implements usb_mode_switch_protocol_t
101 bool has_usb_mode_switch;
102 // USB device descriptor set via ioctl_usb_peripheral_set_device_desc()
103 usb_device_descriptor_t device_desc;
104 // USB configuration descriptor, synthesized from our functions' descriptors
105 usb_configuration_descriptor_t* config_desc;
106 // map from interface number to function
107 usb_function_t* interface_map[MAX_INTERFACES];
108 // map from endpoint index to function
109 usb_function_t* endpoint_map[USB_MAX_EPS];
110 // strings for USB string descriptors
111 char* strings[256];
112 // list of usb_function_t
113 list_node_t functions;
114 // mutex for protecting our state
115 mtx_t lock;
116 // current USB mode set via ioctl_usb_peripheral_set_mode()
117 usb_mode_t usb_mode;
118 // our parent's USB mode
119 usb_mode_t dci_usb_mode;
120 // set if ioctl_usb_peripheral_bind_functions() has been called
121 // and we have a complete list of our function.
122 bool functions_bound;
123 // set if all our functions have registered their usb_function_interface_t
124 bool functions_registered;
125 // true if we have added child devices for our functions
126 bool function_devs_added;
127 // true if we are connected to a host
128 bool connected;
129 // current configuration number selected via USB_REQ_SET_CONFIGURATION
130 // (will be 0 or 1 since we currently do not support multiple configurations)
131 uint8_t configuration;
132 // USB connection speed
133 usb_speed_t speed;
134 size_t parent_request_size;
135 } usb_device_t;
136
137 // for mapping bEndpointAddress value to/from index in range 0 - 31
138 // OUT endpoints are in range 1 - 15, IN endpoints are in range 17 - 31
139 #define ep_address_to_index(addr) (((addr) & 0xF) | (((addr) & 0x80) >> 3))
140 #define ep_index_to_address(index) (((index) & 0xF) | (((index) & 0x10) << 3))
141 #define OUT_EP_START 1
142 #define OUT_EP_END 15
143 #define IN_EP_START 17
144 #define IN_EP_END 31
145
146 static zx_status_t usb_dev_state_changed_locked(usb_device_t* dev);
147
usb_device_alloc_string_desc(usb_device_t * dev,const char * string,uint8_t * out_index)148 static zx_status_t usb_device_alloc_string_desc(usb_device_t* dev, const char* string,
149 uint8_t* out_index) {
150
151 mtx_lock(&dev->lock);
152
153 unsigned i;
154 for (i = 1; i < countof(dev->strings); i++) {
155 if (!dev->strings[i]) {
156 break;
157 }
158 }
159 if (i == countof(dev->strings)) {
160 mtx_unlock(&dev->lock);
161 return ZX_ERR_NO_RESOURCES;
162 }
163
164 dev->strings[i] = strdup(string);
165 if (!dev->strings[i]) {
166 mtx_unlock(&dev->lock);
167 return ZX_ERR_NO_MEMORY;
168 }
169
170 mtx_unlock(&dev->lock);
171
172 *out_index = i;
173 return ZX_OK;
174 }
175
176 static zx_protocol_device_t function_proto = {
177 .version = DEVICE_OPS_VERSION,
178 // Note that we purposely do not have a release callback for USB functions.
179 // The functions are kept on a list when not active so they can be re-added
180 // when reentering device mode.
181 };
182
usb_device_function_registered(usb_device_t * dev)183 static zx_status_t usb_device_function_registered(usb_device_t* dev) {
184 mtx_lock(&dev->lock);
185
186 if (dev->config_desc) {
187 zxlogf(ERROR, "usb_device_function_registered: already have configuration descriptor!\n");
188 mtx_unlock(&dev->lock);
189 return ZX_ERR_BAD_STATE;
190 }
191
192 // check to see if we have all our functions registered
193 // if so, we can build our configuration descriptor and tell the DCI driver we are ready
194 usb_function_t* function;
195 size_t length = sizeof(usb_configuration_descriptor_t);
196 list_for_every_entry(&dev->functions, function, usb_function_t, node) {
197 if (function->descriptors) {
198 length += function->descriptors_length;
199 } else {
200 // need to wait for more functions to register
201 mtx_unlock(&dev->lock);
202 return ZX_OK;
203 }
204 }
205
206 // build our configuration descriptor
207 usb_configuration_descriptor_t* config_desc = malloc(length);
208 if (!config_desc) {
209 mtx_unlock(&dev->lock);
210 return ZX_ERR_NO_MEMORY;
211 }
212
213 config_desc->bLength = sizeof(*config_desc);
214 config_desc->bDescriptorType = USB_DT_CONFIG;
215 config_desc->wTotalLength = htole16(length);
216 config_desc->bNumInterfaces = 0;
217 config_desc->bConfigurationValue = 1;
218 config_desc->iConfiguration = 0;
219 // TODO(voydanoff) add a way to configure bmAttributes and bMaxPower
220 config_desc->bmAttributes = USB_CONFIGURATION_SELF_POWERED | USB_CONFIGURATION_RESERVED_7;
221 config_desc->bMaxPower = 0;
222
223 void* dest = config_desc + 1;
224 list_for_every_entry(&dev->functions, function, usb_function_t, node) {
225 memcpy(dest, function->descriptors, function->descriptors_length);
226 dest += function->descriptors_length;
227 config_desc->bNumInterfaces += function->num_interfaces;
228 }
229 dev->config_desc = config_desc;
230
231 zxlogf(TRACE, "usb_device_function_registered functions_registered = true\n");
232 dev->functions_registered = true;
233
234 zx_status_t status = usb_dev_state_changed_locked(dev);
235 mtx_unlock(&dev->lock);
236 return status;
237 }
238
usb_func_set_interface(void * ctx,const usb_function_interface_t * interface)239 static zx_status_t usb_func_set_interface(void* ctx, const usb_function_interface_t* interface) {
240 usb_function_t* function = ctx;
241 usb_device_t* dev = function->dev;
242 usb_function_t** endpoint_map = dev->endpoint_map;
243
244 size_t length = usb_function_interface_get_descriptors_size(interface);
245 void* descriptors = malloc(length);
246 if (!descriptors) {
247 return ZX_ERR_NO_MEMORY;
248 }
249 size_t actual;
250 usb_function_interface_get_descriptors(interface, descriptors, length, &actual);
251 if (actual != length) {
252 zxlogf(ERROR, "usb_function_interface_get_descriptors failed\n");
253 return ZX_ERR_INTERNAL;
254 }
255
256 usb_interface_descriptor_t* intf_desc = (usb_interface_descriptor_t *)descriptors;
257 if (intf_desc->bDescriptorType != USB_DT_INTERFACE ||
258 intf_desc->bLength != sizeof(usb_interface_descriptor_t)) {
259 zxlogf(ERROR, "usb_func_set_interface: first descriptor not an interface descriptor\n");
260 return ZX_ERR_INVALID_ARGS;
261 }
262
263 const usb_descriptor_header_t* end = (void *)descriptors + length;
264 const usb_descriptor_header_t* header = descriptors;
265
266 while (header < end) {
267 if (header->bDescriptorType == USB_DT_INTERFACE) {
268 usb_interface_descriptor_t* desc = (usb_interface_descriptor_t *)header;
269 if (desc->bInterfaceNumber >= countof(dev->interface_map) ||
270 dev->interface_map[desc->bInterfaceNumber] != function) {
271 zxlogf(ERROR, "usb_func_set_interface: bInterfaceNumber %u\n",
272 desc->bInterfaceNumber);
273 return ZX_ERR_INVALID_ARGS;
274 }
275 if (desc->bAlternateSetting == 0) {
276 function->num_interfaces++;
277 }
278 } else if (header->bDescriptorType == USB_DT_ENDPOINT) {
279 usb_endpoint_descriptor_t* desc = (usb_endpoint_descriptor_t *)header;
280 unsigned index = ep_address_to_index(desc->bEndpointAddress);
281 if (index == 0 || index >= countof(dev->endpoint_map) ||
282 endpoint_map[index] != function) {
283 zxlogf(ERROR, "usb_func_set_interface: bad endpoint address 0x%X\n",
284 desc->bEndpointAddress);
285 return ZX_ERR_INVALID_ARGS;
286 }
287 }
288
289 if (header->bLength == 0) {
290 zxlogf(ERROR, "usb_func_set_interface: zero length descriptor\n");
291 return ZX_ERR_INVALID_ARGS;
292 }
293 header = (void *)header + header->bLength;
294 }
295
296 function->descriptors = malloc(length);
297 if (!function->descriptors) {
298 return ZX_ERR_NO_MEMORY;
299 }
300 memcpy(function->descriptors, descriptors, length);
301 function->descriptors_length = length;
302 memcpy(&function->interface, interface, sizeof(function->interface));
303
304 return usb_device_function_registered(function->dev);
305 }
306
usb_func_alloc_interface(void * ctx,uint8_t * out_intf_num)307 static zx_status_t usb_func_alloc_interface(void* ctx, uint8_t* out_intf_num) {
308 usb_function_t* function = ctx;
309 usb_device_t* dev = function->dev;
310
311 mtx_lock(&dev->lock);
312
313 for (unsigned i = 0; i < countof(dev->interface_map); i++) {
314 if (dev->interface_map[i] == NULL) {
315 dev->interface_map[i] = function;
316 mtx_unlock(&dev->lock);
317 *out_intf_num = i;
318 return ZX_OK;
319 }
320 }
321
322 mtx_unlock(&dev->lock);
323 return ZX_ERR_NO_RESOURCES;
324 }
325
usb_func_alloc_ep(void * ctx,uint8_t direction,uint8_t * out_address)326 static zx_status_t usb_func_alloc_ep(void* ctx, uint8_t direction, uint8_t* out_address) {
327 unsigned start, end;
328
329 if (direction == USB_DIR_OUT) {
330 start = OUT_EP_START;
331 end = OUT_EP_END;
332 } else if (direction == USB_DIR_IN) {
333 start = IN_EP_START;
334 end = IN_EP_END;
335 } else {
336 return ZX_ERR_INVALID_ARGS;
337 }
338
339 usb_function_t* function = ctx;
340 usb_device_t* dev = function->dev;
341 usb_function_t** endpoint_map = dev->endpoint_map;
342
343 mtx_lock(&dev->lock);
344 for (unsigned index = start; index <= end; index++) {
345 if (endpoint_map[index] == NULL) {
346 endpoint_map[index] = function;
347 mtx_unlock(&dev->lock);
348 *out_address = ep_index_to_address(index);
349 return ZX_OK;
350 }
351 }
352
353 mtx_unlock(&dev->lock);
354 return ZX_ERR_NO_RESOURCES;
355 }
356
usb_func_config_ep(void * ctx,const usb_endpoint_descriptor_t * ep_desc,const usb_ss_ep_comp_descriptor_t * ss_comp_desc)357 static zx_status_t usb_func_config_ep(void* ctx, const usb_endpoint_descriptor_t* ep_desc,
358 const usb_ss_ep_comp_descriptor_t* ss_comp_desc) {
359 usb_function_t* function = ctx;
360 return usb_dci_config_ep(&function->dev->usb_dci, ep_desc, ss_comp_desc);
361 }
362
usb_func_disable_ep(void * ctx,uint8_t ep_addr)363 static zx_status_t usb_func_disable_ep(void* ctx, uint8_t ep_addr) {
364 zxlogf(TRACE, "usb_func_disable_ep\n");
365 usb_function_t* function = ctx;
366 return usb_dci_disable_ep(&function->dev->usb_dci, ep_addr);
367 }
368
usb_func_alloc_string_desc(void * ctx,const char * string,uint8_t * out_index)369 static zx_status_t usb_func_alloc_string_desc(void* ctx, const char* string, uint8_t* out_index) {
370 usb_function_t* function = ctx;
371 return usb_device_alloc_string_desc(function->dev, string, out_index);
372 }
373
usb_func_request_queue(void * ctx,usb_request_t * req,const usb_request_complete_t * cb)374 static void usb_func_request_queue(void* ctx, usb_request_t* req, const usb_request_complete_t* cb) {
375 usb_function_t* function = ctx;
376 usb_dci_request_queue(&function->dev->usb_dci, req, cb);
377 }
378
usb_func_ep_set_stall(void * ctx,uint8_t ep_address)379 static zx_status_t usb_func_ep_set_stall(void* ctx, uint8_t ep_address) {
380 usb_function_t* function = ctx;
381 return usb_dci_ep_set_stall(&function->dev->usb_dci, ep_address);
382 }
383
usb_func_ep_clear_stall(void * ctx,uint8_t ep_address)384 static zx_status_t usb_func_ep_clear_stall(void* ctx, uint8_t ep_address) {
385 usb_function_t* function = ctx;
386 return usb_dci_ep_clear_stall(&function->dev->usb_dci, ep_address);
387 }
388
usb_func_get_request_size(void * ctx)389 static size_t usb_func_get_request_size(void* ctx) {
390 usb_function_t* function = ctx;
391 return function->dev->parent_request_size;
392 }
393
394 usb_function_protocol_ops_t usb_function_proto = {
395 .set_interface = usb_func_set_interface,
396 .alloc_interface = usb_func_alloc_interface,
397 .alloc_ep = usb_func_alloc_ep,
398 .config_ep = usb_func_config_ep,
399 .disable_ep = usb_func_disable_ep,
400 .alloc_string_desc = usb_func_alloc_string_desc,
401 .request_queue = usb_func_request_queue,
402 .ep_set_stall = usb_func_ep_set_stall,
403 .ep_clear_stall = usb_func_ep_clear_stall,
404 .get_request_size = usb_func_get_request_size,
405 };
406
usb_dev_get_descriptor(usb_device_t * dev,uint8_t request_type,uint16_t value,uint16_t index,void * buffer,size_t length,size_t * out_actual)407 static zx_status_t usb_dev_get_descriptor(usb_device_t* dev, uint8_t request_type,
408 uint16_t value, uint16_t index, void* buffer,
409 size_t length, size_t* out_actual) {
410 uint8_t type = request_type & USB_TYPE_MASK;
411
412 if (type == USB_TYPE_STANDARD) {
413 uint8_t desc_type = value >> 8;
414 if (desc_type == USB_DT_DEVICE && index == 0) {
415 const usb_device_descriptor_t* desc = &dev->device_desc;
416 if (desc->bLength == 0) {
417 zxlogf(ERROR, "usb_dev_get_descriptor: device descriptor not set\n");
418 return ZX_ERR_INTERNAL;
419 }
420 if (length > sizeof(*desc)) length = sizeof(*desc);
421 memcpy(buffer, desc, length);
422 *out_actual = length;
423 return ZX_OK;
424 } else if (desc_type == USB_DT_CONFIG && index == 0) {
425 const usb_configuration_descriptor_t* desc = dev->config_desc;
426 if (!desc) {
427 zxlogf(ERROR, "usb_dev_get_descriptor: configuration descriptor not set\n");
428 return ZX_ERR_INTERNAL;
429 }
430 uint16_t desc_length = letoh16(desc->wTotalLength);
431 if (length > desc_length) length =desc_length;
432 memcpy(buffer, desc, length);
433 *out_actual = length;
434 return ZX_OK;
435 }
436 else if (value >> 8 == USB_DT_STRING) {
437 uint8_t desc[255];
438 usb_descriptor_header_t* header = (usb_descriptor_header_t *)desc;
439 header->bDescriptorType = USB_DT_STRING;
440
441 uint8_t string_index = value & 0xFF;
442 if (string_index == 0) {
443 // special case - return language list
444 header->bLength = 4;
445 desc[2] = 0x09; // language ID
446 desc[3] = 0x04;
447 } else {
448 char* string = dev->strings[string_index];
449 if (!string) {
450 return ZX_ERR_INVALID_ARGS;
451 }
452 unsigned index = 2;
453
454 // convert ASCII to Unicode
455 if (string) {
456 while (*string && index < sizeof(desc) - 2) {
457 desc[index++] = *string++;
458 desc[index++] = 0;
459 }
460 }
461 header->bLength = index;
462 }
463
464 if (header->bLength < length) length = header->bLength;
465 memcpy(buffer, desc, length);
466 *out_actual = length;
467 return ZX_OK;
468 }
469 }
470
471 zxlogf(ERROR, "usb_device_get_descriptor unsupported value: %d index: %d\n", value, index);
472 return ZX_ERR_NOT_SUPPORTED;
473 }
474
usb_dev_set_configuration(usb_device_t * dev,uint8_t configuration)475 static zx_status_t usb_dev_set_configuration(usb_device_t* dev, uint8_t configuration) {
476 zx_status_t status = ZX_OK;
477 bool configured = configuration > 0;
478
479 mtx_lock(&dev->lock);
480
481 usb_function_t* function;
482 list_for_every_entry(&dev->functions, function, usb_function_t, node) {
483 if (function->interface.ops) {
484 status = usb_function_interface_set_configured(&function->interface, configured,
485 dev->speed);
486 if (status != ZX_OK && configured) {
487 goto fail;
488 }
489 }
490 }
491
492 dev->configuration = configuration;
493
494 fail:
495 mtx_unlock(&dev->lock);
496 return status;
497 }
498
usb_dev_set_interface(usb_device_t * dev,unsigned interface,unsigned alt_setting)499 static zx_status_t usb_dev_set_interface(usb_device_t* dev, unsigned interface,
500 unsigned alt_setting) {
501 usb_function_t* function = dev->interface_map[interface];
502 if (function && function->interface.ops) {
503 return usb_function_interface_set_interface(&function->interface, interface, alt_setting);
504 }
505 return ZX_ERR_NOT_SUPPORTED;
506 }
507
usb_dev_control(void * ctx,const usb_setup_t * setup,const void * write_buffer,size_t write_size,void * read_buffer,size_t read_size,size_t * out_read_actual)508 static zx_status_t usb_dev_control(void* ctx, const usb_setup_t* setup, const void* write_buffer,
509 size_t write_size, void* read_buffer, size_t read_size,
510 size_t* out_read_actual) {
511 usb_device_t* dev = ctx;
512 uint8_t request_type = setup->bmRequestType;
513 uint8_t direction = request_type & USB_DIR_MASK;
514 uint8_t request = setup->bRequest;
515 uint16_t value = le16toh(setup->wValue);
516 uint16_t index = le16toh(setup->wIndex);
517 uint16_t length = le16toh(setup->wLength);
518
519 if (direction == USB_DIR_IN && length > read_size) {
520 return ZX_ERR_BUFFER_TOO_SMALL;
521 } else if (direction == USB_DIR_OUT && length > write_size) {
522 return ZX_ERR_BUFFER_TOO_SMALL;
523 }
524 if ((write_size > 0 && write_buffer == NULL) || (read_size > 0 && read_buffer == NULL)) {
525 return ZX_ERR_INVALID_ARGS;
526 }
527
528 zxlogf(TRACE, "usb_dev_control type: 0x%02X req: %d value: %d index: %d length: %d\n",
529 request_type, request, value, index, length);
530
531 switch (request_type & USB_RECIP_MASK) {
532 case USB_RECIP_DEVICE:
533 // handle standard device requests
534 if ((request_type & (USB_DIR_MASK | USB_TYPE_MASK)) == (USB_DIR_IN | USB_TYPE_STANDARD) &&
535 request == USB_REQ_GET_DESCRIPTOR) {
536 return usb_dev_get_descriptor(dev, request_type, value, index, read_buffer, length,
537 out_read_actual);
538 } else if (request_type == (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE) &&
539 request == USB_REQ_SET_CONFIGURATION && length == 0) {
540 return usb_dev_set_configuration(dev, value);
541 } else if (request_type == (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE) &&
542 request == USB_REQ_GET_CONFIGURATION && length > 0) {
543 *((uint8_t *)read_buffer) = dev->configuration;
544 *out_read_actual = sizeof(uint8_t);
545 return ZX_OK;
546 }
547 break;
548 case USB_RECIP_INTERFACE: {
549 if (request_type == (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) &&
550 request == USB_REQ_SET_INTERFACE && length == 0) {
551 return usb_dev_set_interface(dev, index, value);
552 } else {
553 // delegate to the function driver for the interface
554 usb_function_t* function = dev->interface_map[index];
555 if (function && function->interface.ops) {
556 return usb_function_interface_control(&function->interface, setup, write_buffer,
557 write_size, read_buffer, read_size,
558 out_read_actual);
559 }
560 }
561 break;
562 }
563 case USB_RECIP_ENDPOINT: {
564 // delegate to the function driver for the endpoint
565 index = ep_address_to_index(index);
566 if (index == 0 || index >= USB_MAX_EPS) {
567 return ZX_ERR_INVALID_ARGS;
568 }
569 usb_function_t* function = dev->endpoint_map[index];
570 if (function && function->interface.ops) {
571 return usb_function_interface_control(&function->interface, setup, write_buffer,
572 write_size, read_buffer, read_size,
573 out_read_actual);
574 }
575 break;
576 }
577 case USB_RECIP_OTHER:
578 // TODO(voydanoff) - how to handle this?
579 default:
580 break;
581 }
582
583 return ZX_ERR_NOT_SUPPORTED;
584 }
585
usb_dev_set_connected(void * ctx,bool connected)586 static void usb_dev_set_connected(void* ctx, bool connected) {
587 usb_device_t* dev = ctx;
588 if (dev->connected != connected) {
589 if (!connected) {
590 usb_function_t* function;
591 list_for_every_entry(&dev->functions, function, usb_function_t, node) {
592 if (function->interface.ops) {
593 usb_function_interface_set_configured(&function->interface, false,
594 USB_SPEED_UNDEFINED);
595 }
596 }
597 }
598
599 dev->connected = connected;
600 }
601 }
602
usb_dev_set_speed(void * ctx,usb_speed_t speed)603 static void usb_dev_set_speed(void* ctx, usb_speed_t speed) {
604 usb_device_t* dev = ctx;
605 dev->speed = speed;
606 }
607
608 usb_dci_interface_ops_t dci_ops = {
609 .control = usb_dev_control,
610 .set_connected = usb_dev_set_connected,
611 .set_speed = usb_dev_set_speed,
612 };
613
usb_dev_set_device_desc(void * ctx,const zircon_usb_peripheral_DeviceDescriptor * desc,fidl_txn_t * txn)614 static zx_status_t usb_dev_set_device_desc(void* ctx,
615 const zircon_usb_peripheral_DeviceDescriptor* desc,
616 fidl_txn_t* txn) {
617 usb_device_t* dev = ctx;
618
619 zx_status_t status;
620 if (desc->bNumConfigurations != 1) {
621 zxlogf(ERROR, "usb_device_ioctl: bNumConfigurations: %u, only 1 supported\n",
622 desc->bNumConfigurations);
623 status = ZX_ERR_INVALID_ARGS;
624 } else {
625 dev->device_desc.bLength = sizeof(usb_device_descriptor_t);
626 dev->device_desc.bDescriptorType = USB_DT_DEVICE;
627 dev->device_desc.bcdUSB = desc->bcdUSB;
628 dev->device_desc.bDeviceClass = desc->bDeviceClass;
629 dev->device_desc.bDeviceSubClass = desc->bDeviceSubClass;
630 dev->device_desc.bDeviceProtocol = desc->bDeviceProtocol;
631 dev->device_desc.bMaxPacketSize0 = desc->bMaxPacketSize0;
632 dev->device_desc.idVendor = desc->idVendor;
633 dev->device_desc.idProduct = desc->idProduct;
634 dev->device_desc.bcdDevice = desc->bcdDevice;
635 dev->device_desc.iManufacturer = desc->iManufacturer;
636 dev->device_desc.iProduct = desc->iProduct;
637 dev->device_desc.iSerialNumber = desc->iSerialNumber;
638 dev->device_desc.bNumConfigurations = desc->bNumConfigurations;
639 status = ZX_OK;
640 }
641
642 return zircon_usb_peripheral_DeviceSetDeviceDescriptor_reply(txn, status);
643 }
644
usb_dev_alloc_string_desc(void * ctx,const char * name_data,size_t name_size,fidl_txn_t * txn)645 static zx_status_t usb_dev_alloc_string_desc(void* ctx, const char* name_data, size_t name_size,
646 fidl_txn_t* txn) {
647 usb_device_t* dev = ctx;
648
649 uint8_t index = 0;
650 zx_status_t status = usb_device_alloc_string_desc(dev, name_data, &index);
651 return zircon_usb_peripheral_DeviceAllocStringDesc_reply(txn, status, index);
652 }
653
usb_dev_do_add_function(usb_device_t * dev,const usb_function_descriptor_t * desc)654 static zx_status_t usb_dev_do_add_function(usb_device_t* dev,
655 const usb_function_descriptor_t* desc) {
656 if (dev->functions_bound) {
657 return ZX_ERR_BAD_STATE;
658 }
659
660 usb_function_t* function = calloc(1, sizeof(usb_function_t));
661 if (!function) {
662 return ZX_ERR_NO_MEMORY;
663 }
664 function->dci_dev = dev->dci_dev;
665 function->dev = dev;
666 memcpy(&function->desc, desc, sizeof(function->desc));
667 list_add_tail(&dev->functions, &function->node);
668
669 return ZX_OK;
670 }
671
usb_dev_add_function(void * ctx,const usb_function_descriptor_t * desc,fidl_txn_t * txn)672 static zx_status_t usb_dev_add_function(void* ctx, const usb_function_descriptor_t* desc,
673 fidl_txn_t* txn) {
674 usb_device_t* dev = ctx;
675
676 zx_status_t status = usb_dev_do_add_function(dev, desc);
677 return zircon_usb_peripheral_DeviceAddFunction_reply(txn, status);
678 }
679
usb_dev_remove_function_devices_locked(usb_device_t * dev)680 static void usb_dev_remove_function_devices_locked(usb_device_t* dev) {
681 zxlogf(TRACE, "usb_dev_remove_function_devices_locked\n");
682
683 usb_function_t* function;
684 list_for_every_entry(&dev->functions, function, usb_function_t, node) {
685 if (function->zxdev) {
686 // here we remove the function from the DDK device tree,
687 // but the storage for the function remains on our function list.
688 device_remove(function->zxdev);
689 function->zxdev = NULL;
690 }
691 }
692
693 free(dev->config_desc);
694 dev->config_desc = NULL;
695 dev->functions_registered = false;
696 dev->function_devs_added = false;
697 }
698
usb_dev_add_function_devices_locked(usb_device_t * dev)699 static zx_status_t usb_dev_add_function_devices_locked(usb_device_t* dev) {
700 zxlogf(TRACE, "usb_dev_add_function_devices_locked\n");
701 if (dev->function_devs_added) {
702 return ZX_OK;
703 }
704
705 usb_device_descriptor_t* device_desc = &dev->device_desc;
706 int index = 0;
707 usb_function_t* function;
708 list_for_every_entry(&dev->functions, function, usb_function_t, node) {
709 char name[16];
710 snprintf(name, sizeof(name), "function-%03d", index);
711
712 usb_function_descriptor_t* desc = &function->desc;
713
714 zx_device_prop_t props[] = {
715 { BIND_PROTOCOL, 0, ZX_PROTOCOL_USB_FUNCTION },
716 { BIND_USB_CLASS, 0, desc->interface_class },
717 { BIND_USB_SUBCLASS, 0, desc->interface_subclass },
718 { BIND_USB_PROTOCOL, 0, desc->interface_protocol },
719 { BIND_USB_VID, 0, device_desc->idVendor },
720 { BIND_USB_PID, 0, device_desc->idProduct },
721 };
722
723 device_add_args_t args = {
724 .version = DEVICE_ADD_ARGS_VERSION,
725 .name = name,
726 .ctx = function,
727 .ops = &function_proto,
728 .proto_id = ZX_PROTOCOL_USB_FUNCTION,
729 .proto_ops = &usb_function_proto,
730 .props = props,
731 .prop_count = countof(props),
732 };
733
734 zx_status_t status = device_add(dev->zxdev, &args, &function->zxdev);
735 if (status != ZX_OK) {
736 zxlogf(ERROR, "usb_dev_bind_functions add_device failed %d\n", status);
737 return status;
738 }
739
740 index++;
741 }
742
743 dev->function_devs_added = true;
744 return ZX_OK;
745 }
746
usb_dev_state_changed_locked(usb_device_t * dev)747 static zx_status_t usb_dev_state_changed_locked(usb_device_t* dev) {
748 zxlogf(TRACE, "usb_dev_state_changed_locked usb_mode: %d dci_usb_mode: %d\n", dev->usb_mode,
749 dev->dci_usb_mode);
750
751 usb_mode_t new_dci_usb_mode = dev->dci_usb_mode;
752 bool add_function_devs = (dev->usb_mode == USB_MODE_PERIPHERAL && dev->functions_bound);
753 zx_status_t status = ZX_OK;
754
755 if (dev->usb_mode == USB_MODE_PERIPHERAL) {
756 if (dev->functions_registered) {
757 // switch DCI to device mode
758 new_dci_usb_mode = USB_MODE_PERIPHERAL;
759 } else {
760 new_dci_usb_mode = USB_MODE_NONE;
761 }
762 } else {
763 new_dci_usb_mode = dev->usb_mode;
764 }
765
766 if (add_function_devs) {
767 // publish child devices if necessary
768 if (!dev->function_devs_added) {
769 status = usb_dev_add_function_devices_locked(dev);
770 if (status != ZX_OK) {
771 return status;
772 }
773 }
774 }
775
776 if (dev->dci_usb_mode != new_dci_usb_mode) {
777 zxlogf(TRACE, "usb_dev_state_changed_locked set DCI mode %d\n", new_dci_usb_mode);
778 if (dev->has_usb_mode_switch) {
779 status = usb_mode_switch_set_mode(&dev->usb_mode_switch, new_dci_usb_mode);
780 if (status != ZX_OK) {
781 usb_mode_switch_set_mode(&dev->usb_mode_switch, USB_MODE_NONE);
782 new_dci_usb_mode = USB_MODE_NONE;
783 }
784 }
785 dev->dci_usb_mode = new_dci_usb_mode;
786 }
787
788 if (!add_function_devs && dev->function_devs_added) {
789 usb_dev_remove_function_devices_locked(dev);
790 }
791
792 return status;
793 }
794
usb_dev_do_bind_functions(usb_device_t * dev)795 static zx_status_t usb_dev_do_bind_functions(usb_device_t* dev) {
796 mtx_lock(&dev->lock);
797
798 if (dev->functions_bound) {
799 zxlogf(ERROR, "usb_dev_bind_functions: already bound!\n");
800 mtx_unlock(&dev->lock);
801 return ZX_ERR_BAD_STATE;
802 }
803
804 usb_device_descriptor_t* device_desc = &dev->device_desc;
805 if (device_desc->bLength == 0) {
806 zxlogf(ERROR, "usb_dev_bind_functions: device descriptor not set\n");
807 mtx_unlock(&dev->lock);
808 return ZX_ERR_BAD_STATE;
809 }
810 if (list_is_empty(&dev->functions)) {
811 zxlogf(ERROR, "usb_dev_bind_functions: no functions to bind\n");
812 mtx_unlock(&dev->lock);
813 return ZX_ERR_BAD_STATE;
814 }
815
816 zxlogf(TRACE, "usb_dev_bind_functions functions_bound = true\n");
817 dev->functions_bound = true;
818 zx_status_t status = usb_dev_state_changed_locked(dev);
819 mtx_unlock(&dev->lock);
820
821 return status;
822 }
823
usb_dev_bind_functions(void * ctx,fidl_txn_t * txn)824 static zx_status_t usb_dev_bind_functions(void* ctx, fidl_txn_t* txn) {
825 usb_device_t* dev = ctx;
826
827 zx_status_t status = usb_dev_do_bind_functions(dev);
828 return zircon_usb_peripheral_DeviceBindFunctions_reply(txn, status);
829 }
830
831
usb_dev_do_clear_functions(usb_device_t * dev)832 static zx_status_t usb_dev_do_clear_functions(usb_device_t* dev) {
833 mtx_lock(&dev->lock);
834
835 usb_function_t* function;
836 while ((function = list_remove_head_type(&dev->functions, usb_function_t, node)) != NULL) {
837 if (function->zxdev) {
838 device_remove(function->zxdev);
839 // device_remove will not actually free the function, so we free it here
840 free(function->descriptors);
841 free(function);
842 }
843 }
844 free(dev->config_desc);
845 dev->config_desc = NULL;
846 dev->functions_bound = false;
847 dev->functions_registered = false;
848
849 memset(dev->interface_map, 0, sizeof(dev->interface_map));
850 memset(dev->endpoint_map, 0, sizeof(dev->endpoint_map));
851 for (unsigned i = 0; i < countof(dev->strings); i++) {
852 free(dev->strings[i]);
853 dev->strings[i] = NULL;
854 }
855
856 zx_status_t status = usb_dev_state_changed_locked(dev);
857 mtx_unlock(&dev->lock);
858
859 return status;
860 }
861
usb_dev_clear_functions(void * ctx,fidl_txn_t * txn)862 static zx_status_t usb_dev_clear_functions(void* ctx, fidl_txn_t* txn) {
863 usb_device_t* dev = ctx;
864
865 zxlogf(TRACE, "usb_dev_clear_functions\n");
866
867 zx_status_t status = usb_dev_do_clear_functions(dev);
868 return zircon_usb_peripheral_DeviceClearFunctions_reply(txn, status);
869 }
870
usb_dev_get_mode(void * ctx,fidl_txn_t * txn)871 static zx_status_t usb_dev_get_mode(void* ctx, fidl_txn_t* txn) {
872 usb_device_t* dev = ctx;
873
874 mtx_lock(&dev->lock);
875 uint32_t mode = dev->usb_mode;
876 mtx_unlock(&dev->lock);
877
878 return zircon_usb_peripheral_DeviceGetMode_reply(txn, ZX_OK, mode);
879 }
880
usb_dev_set_mode(void * ctx,uint32_t mode,fidl_txn_t * txn)881 static zx_status_t usb_dev_set_mode(void* ctx, uint32_t mode, fidl_txn_t* txn) {
882 usb_device_t* dev = ctx;
883
884 mtx_lock(&dev->lock);
885 dev->usb_mode = mode;
886 zx_status_t status = usb_dev_state_changed_locked(dev);
887 mtx_unlock(&dev->lock);
888
889 return zircon_usb_peripheral_DeviceSetMode_reply(txn, status);
890 }
891
892 static zircon_usb_peripheral_Device_ops_t fidl_ops = {
893 .SetDeviceDescriptor = usb_dev_set_device_desc,
894 .AllocStringDesc = usb_dev_alloc_string_desc,
895 .AddFunction = usb_dev_add_function,
896 .BindFunctions = usb_dev_bind_functions,
897 .ClearFunctions = usb_dev_clear_functions,
898 .GetMode = usb_dev_get_mode,
899 .SetMode = usb_dev_set_mode,
900 };
901
usb_dev_message(void * ctx,fidl_msg_t * msg,fidl_txn_t * txn)902 zx_status_t usb_dev_message(void* ctx, fidl_msg_t* msg, fidl_txn_t* txn) {
903 return zircon_usb_peripheral_Device_dispatch(ctx, txn, msg, &fidl_ops);
904 }
905
usb_dev_unbind(void * ctx)906 static void usb_dev_unbind(void* ctx) {
907 zxlogf(TRACE, "usb_dev_unbind\n");
908 usb_device_t* dev = ctx;
909 usb_dev_do_clear_functions(dev);
910 device_remove(dev->zxdev);
911 }
912
usb_dev_release(void * ctx)913 static void usb_dev_release(void* ctx) {
914 zxlogf(TRACE, "usb_dev_release\n");
915 usb_device_t* dev = ctx;
916 free(dev->config_desc);
917 for (unsigned i = 0; i < countof(dev->strings); i++) {
918 free(dev->strings[i]);
919 }
920 free(dev);
921 }
922
923 static zx_protocol_device_t device_proto = {
924 .version = DEVICE_OPS_VERSION,
925 .message = usb_dev_message,
926 .unbind = usb_dev_unbind,
927 .release = usb_dev_release,
928 };
929
930 #if defined(USB_DEVICE_VID) && defined(USB_DEVICE_PID) && defined(USB_DEVICE_FUNCTIONS)
usb_dev_set_default_config(usb_device_t * dev)931 static zx_status_t usb_dev_set_default_config(usb_device_t* dev) {
932 usb_device_descriptor_t device_desc = {
933 .bLength = sizeof(usb_device_descriptor_t),
934 .bDescriptorType = USB_DT_DEVICE,
935 .bcdUSB = htole16(0x0200),
936 .bDeviceClass = 0,
937 .bDeviceSubClass = 0,
938 .bDeviceProtocol = 0,
939 .bMaxPacketSize0 = 64,
940 .idVendor = htole16(USB_DEVICE_VID),
941 .idProduct = htole16(USB_DEVICE_PID),
942 .bcdDevice = htole16(0x0100),
943 .bNumConfigurations = 1,
944 };
945
946 zx_status_t status = ZX_OK;
947
948 #ifdef USB_DEVICE_MANUFACTURER
949 status = usb_device_alloc_string_desc(dev, USB_DEVICE_MANUFACTURER, &device_desc.iManufacturer);
950 if (status != ZX_OK) return status;
951 #endif
952 #ifdef USB_DEVICE_PRODUCT
953 usb_device_alloc_string_desc(dev, USB_DEVICE_PRODUCT, &device_desc.iProduct);
954 if (status != ZX_OK) return status;
955 #endif
956 #ifdef USB_DEVICE_SERIAL
957 usb_device_alloc_string_desc(dev, USB_DEVICE_SERIAL, &device_desc.iSerialNumber);
958 if (status != ZX_OK) return status;
959 #endif
960
961 memcpy(&dev->device_desc, &device_desc, sizeof(dev->device_desc));
962
963 usb_function_descriptor_t function_desc;
964 if (strcasecmp(USB_DEVICE_FUNCTIONS, "cdc") == 0) {
965 function_desc.interface_class = USB_CLASS_COMM;
966 function_desc.interface_subclass = USB_CDC_SUBCLASS_ETHERNET;
967 function_desc.interface_protocol = 0;
968 } else if (strcasecmp(USB_DEVICE_FUNCTIONS, "ums") == 0) {
969 function_desc.interface_class = USB_CLASS_MSC;
970 function_desc.interface_subclass = USB_SUBCLASS_MSC_SCSI;
971 function_desc.interface_protocol = USB_PROTOCOL_MSC_BULK_ONLY;
972 } else if (strcasecmp(USB_DEVICE_FUNCTIONS, "test") == 0) {
973 function_desc.interface_class = USB_CLASS_VENDOR;
974 function_desc.interface_subclass = 0;
975 function_desc.interface_protocol = 0;
976 } else {
977 zxlogf(ERROR, "usb_dev_set_default_config: unknown function %s\n", USB_DEVICE_FUNCTIONS);
978 return ZX_ERR_INVALID_ARGS;
979 }
980
981 status = usb_dev_do_add_function(dev, &function_desc);
982 if (status != ZX_OK) return status;
983
984 return usb_dev_do_bind_functions(dev);
985 }
986 #endif // defined(USB_DEVICE_VID) && defined(USB_DEVICE_PID) && defined(USB_DEVICE_FUNCTIONS)
987
usb_dev_bind(void * ctx,zx_device_t * parent)988 zx_status_t usb_dev_bind(void* ctx, zx_device_t* parent) {
989 zxlogf(INFO, "usb_dev_bind\n");
990
991 usb_device_t* dev = calloc(1, sizeof(usb_device_t));
992 if (!dev) {
993 return ZX_ERR_NO_MEMORY;
994 }
995 list_initialize(&dev->functions);
996 mtx_init(&dev->lock, mtx_plain);
997 dev->dci_dev = parent;
998
999 if (device_get_protocol(parent, ZX_PROTOCOL_USB_DCI, &dev->usb_dci)) {
1000 free(dev);
1001 return ZX_ERR_NOT_SUPPORTED;
1002 }
1003
1004 if (device_get_protocol(parent, ZX_PROTOCOL_USB_MODE_SWITCH, &dev->usb_mode_switch) == ZX_OK) {
1005 dev->has_usb_mode_switch = true;
1006 }
1007
1008 // Starting USB mode is determined from device metadata.
1009 // We read initial value and store it in dev->usb_mode, but do not actually
1010 // enable it until after all of our functions have bound.
1011 size_t actual;
1012 zx_status_t status = device_get_metadata(parent, DEVICE_METADATA_USB_MODE,
1013 &dev->usb_mode, sizeof(dev->usb_mode), &actual);
1014 if (status == ZX_ERR_NOT_FOUND) {
1015 // Assume peripheral mode by default.
1016 dev->usb_mode = USB_MODE_PERIPHERAL;
1017 } else if (status != ZX_OK || actual != sizeof(dev->usb_mode)) {
1018 zxlogf(ERROR, "usb_dev_bind: DEVICE_METADATA_USB_MODE not found\n");
1019 free(dev);
1020 return status;
1021 }
1022 // Set DCI mode to USB_MODE_NONE until we are ready
1023 if (dev->has_usb_mode_switch) {
1024 usb_mode_switch_set_mode(&dev->usb_mode_switch, USB_MODE_NONE);
1025 }
1026 dev->dci_usb_mode = USB_MODE_NONE;
1027 dev->parent_request_size = usb_dci_get_request_size(&dev->usb_dci);
1028
1029 device_add_args_t args = {
1030 .version = DEVICE_ADD_ARGS_VERSION,
1031 .name = "usb-peripheral",
1032 .ctx = dev,
1033 .ops = &device_proto,
1034 .proto_id = ZX_PROTOCOL_USB_PERIPHERAL,
1035 .flags = DEVICE_ADD_NON_BINDABLE,
1036 };
1037
1038 status = device_add(parent, &args, &dev->zxdev);
1039 if (status != ZX_OK) {
1040 zxlogf(ERROR, "usb_device_bind add_device failed %d\n", status);
1041 free(dev);
1042 return status;
1043 }
1044
1045 usb_dci_interface_t intf = {
1046 .ops = &dci_ops,
1047 .ctx = dev,
1048 };
1049 usb_dci_set_interface(&dev->usb_dci, &intf);
1050
1051 #if defined(USB_DEVICE_VID) && defined(USB_DEVICE_PID) && defined(USB_DEVICE_FUNCTIONS)
1052 // set compile time configuration, if we have one
1053 usb_dev_set_default_config(dev);
1054 #endif
1055
1056 return ZX_OK;
1057 }
1058
1059 static zx_driver_ops_t usb_device_ops = {
1060 .version = DRIVER_OPS_VERSION,
1061 .bind = usb_dev_bind,
1062 };
1063
1064 // clang-format off
1065 ZIRCON_DRIVER_BEGIN(usb_device, usb_device_ops, "zircon", "0.1", 1)
1066 BI_MATCH_IF(EQ, BIND_PROTOCOL, ZX_PROTOCOL_USB_DCI),
1067 ZIRCON_DRIVER_END(usb_device)
1068