1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com> 5 * Copyright (c) 2018 The FreeBSD Foundation 6 * All rights reserved. 7 * 8 * This software was developed by SRI International and the University of 9 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 10 * ("CTSRD"), as part of the DARPA CRASH research programme. 11 * 12 * Portions of this software were developed by Edward Tomasz Napierala 13 * under sponsorship from the FreeBSD Foundation. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 /* 37 * USB template for CDC ACM (serial), CDC ECM (network), and CDC MSC (storage). 38 */ 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 #ifdef USB_GLOBAL_INCLUDE_FILE 44 #include USB_GLOBAL_INCLUDE_FILE 45 #else 46 #include <sys/stdint.h> 47 #include <sys/stddef.h> 48 #include <sys/param.h> 49 #include <sys/queue.h> 50 #include <sys/types.h> 51 #include <sys/systm.h> 52 #include <sys/kernel.h> 53 #include <sys/bus.h> 54 #include <sys/module.h> 55 #include <sys/lock.h> 56 #include <sys/mutex.h> 57 #include <sys/condvar.h> 58 #include <sys/sysctl.h> 59 #include <sys/sx.h> 60 #include <sys/unistd.h> 61 #include <sys/callout.h> 62 #include <sys/malloc.h> 63 #include <sys/priv.h> 64 65 #include <dev/usb/usb.h> 66 #include <dev/usb/usbdi.h> 67 #include <dev/usb/usb_core.h> 68 #include <dev/usb/usb_cdc.h> 69 #include <dev/usb/usb_ioctl.h> 70 #include <dev/usb/usb_util.h> 71 72 #include <dev/usb/template/usb_template.h> 73 #endif /* USB_GLOBAL_INCLUDE_FILE */ 74 75 #define MODEM_IFACE_0 0 76 #define MODEM_IFACE_1 1 77 78 enum { 79 MULTI_LANG_INDEX, 80 MULTI_MODEM_INDEX, 81 MULTI_ETH_MAC_INDEX, 82 MULTI_ETH_CONTROL_INDEX, 83 MULTI_ETH_DATA_INDEX, 84 MULTI_STORAGE_INDEX, 85 MULTI_CONFIGURATION_INDEX, 86 MULTI_MANUFACTURER_INDEX, 87 MULTI_PRODUCT_INDEX, 88 MULTI_SERIAL_NUMBER_INDEX, 89 MULTI_MAX_INDEX, 90 }; 91 92 #define MULTI_DEFAULT_MODEM "Virtual serial console" 93 #define MULTI_DEFAULT_ETH_MAC "2A02030405060789AB" 94 #define MULTI_DEFAULT_ETH_CONTROL "Ethernet Comm Interface" 95 #define MULTI_DEFAULT_ETH_DATA "Ethernet Data Interface" 96 #define MULTI_DEFAULT_STORAGE "Mass Storage Interface" 97 #define MULTI_DEFAULT_CONFIGURATION "Default configuration" 98 #define MULTI_DEFAULT_MANUFACTURER "The FreeBSD Project" 99 #define MULTI_DEFAULT_PRODUCT "Multifunction Device" 100 #define MULTI_DEFAULT_SERIAL_NUMBER "May 2018" 101 102 static struct usb_string_descriptor multi_modem; 103 static struct usb_string_descriptor multi_eth_mac; 104 static struct usb_string_descriptor multi_eth_control; 105 static struct usb_string_descriptor multi_eth_data; 106 static struct usb_string_descriptor multi_storage; 107 static struct usb_string_descriptor multi_configuration; 108 static struct usb_string_descriptor multi_manufacturer; 109 static struct usb_string_descriptor multi_product; 110 static struct usb_string_descriptor multi_serial_number; 111 112 static struct sysctl_ctx_list multi_ctx_list; 113 114 /* prototypes */ 115 116 static usb_temp_get_string_desc_t multi_get_string_desc; 117 118 static const struct usb_cdc_union_descriptor eth_union_desc = { 119 .bLength = sizeof(eth_union_desc), 120 .bDescriptorType = UDESC_CS_INTERFACE, 121 .bDescriptorSubtype = UDESCSUB_CDC_UNION, 122 .bMasterInterface = 0, /* this is automatically updated */ 123 .bSlaveInterface[0] = 1, /* this is automatically updated */ 124 }; 125 126 static const struct usb_cdc_header_descriptor eth_header_desc = { 127 .bLength = sizeof(eth_header_desc), 128 .bDescriptorType = UDESC_CS_INTERFACE, 129 .bDescriptorSubtype = UDESCSUB_CDC_HEADER, 130 .bcdCDC[0] = 0x10, 131 .bcdCDC[1] = 0x01, 132 }; 133 134 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = { 135 .bLength = sizeof(eth_enf_desc), 136 .bDescriptorType = UDESC_CS_INTERFACE, 137 .bDescriptorSubtype = UDESCSUB_CDC_ENF, 138 .iMacAddress = MULTI_ETH_MAC_INDEX, 139 .bmEthernetStatistics = {0, 0, 0, 0}, 140 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */ 141 .wNumberMCFilters = {0, 0}, 142 .bNumberPowerFilters = 0, 143 }; 144 145 static const void *eth_control_if_desc[] = { 146 ð_union_desc, 147 ð_header_desc, 148 ð_enf_desc, 149 NULL, 150 }; 151 152 static const struct usb_temp_packet_size bulk_mps = { 153 .mps[USB_SPEED_FULL] = 64, 154 .mps[USB_SPEED_HIGH] = 512, 155 }; 156 157 static const struct usb_temp_packet_size intr_mps = { 158 .mps[USB_SPEED_FULL] = 8, 159 .mps[USB_SPEED_HIGH] = 8, 160 }; 161 162 static const struct usb_temp_endpoint_desc bulk_in_ep = { 163 .pPacketSize = &bulk_mps, 164 #ifdef USB_HIP_IN_EP_0 165 .bEndpointAddress = USB_HIP_IN_EP_0, 166 #else 167 .bEndpointAddress = UE_DIR_IN, 168 #endif 169 .bmAttributes = UE_BULK, 170 }; 171 172 static const struct usb_temp_endpoint_desc bulk_out_ep = { 173 .pPacketSize = &bulk_mps, 174 #ifdef USB_HIP_OUT_EP_0 175 .bEndpointAddress = USB_HIP_OUT_EP_0, 176 #else 177 .bEndpointAddress = UE_DIR_OUT, 178 #endif 179 .bmAttributes = UE_BULK, 180 }; 181 182 static const struct usb_temp_endpoint_desc intr_in_ep = { 183 .pPacketSize = &intr_mps, 184 .bEndpointAddress = UE_DIR_IN, 185 .bmAttributes = UE_INTERRUPT, 186 }; 187 188 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = { 189 &intr_in_ep, 190 NULL, 191 }; 192 193 static const struct usb_temp_interface_desc eth_control_interface = { 194 .ppEndpoints = eth_intr_endpoints, 195 .ppRawDesc = eth_control_if_desc, 196 .bInterfaceClass = UICLASS_CDC, 197 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 198 .bInterfaceProtocol = UIPROTO_CDC_NONE, 199 .iInterface = MULTI_ETH_CONTROL_INDEX, 200 }; 201 202 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = { 203 &bulk_in_ep, 204 &bulk_out_ep, 205 NULL, 206 }; 207 208 static const struct usb_temp_interface_desc eth_data_null_interface = { 209 .ppEndpoints = NULL, /* no endpoints */ 210 .bInterfaceClass = UICLASS_CDC_DATA, 211 .bInterfaceSubClass = UISUBCLASS_DATA, 212 .bInterfaceProtocol = 0, 213 .iInterface = MULTI_ETH_DATA_INDEX, 214 }; 215 216 static const struct usb_temp_interface_desc eth_data_interface = { 217 .ppEndpoints = eth_data_endpoints, 218 .bInterfaceClass = UICLASS_CDC_DATA, 219 .bInterfaceSubClass = UISUBCLASS_DATA, 220 .bInterfaceProtocol = 0, 221 .iInterface = MULTI_ETH_DATA_INDEX, 222 .isAltInterface = 1, /* this is an alternate setting */ 223 }; 224 225 static const struct usb_temp_packet_size modem_bulk_mps = { 226 .mps[USB_SPEED_LOW] = 8, 227 .mps[USB_SPEED_FULL] = 64, 228 .mps[USB_SPEED_HIGH] = 512, 229 }; 230 231 static const struct usb_temp_packet_size modem_intr_mps = { 232 .mps[USB_SPEED_LOW] = 8, 233 .mps[USB_SPEED_FULL] = 8, 234 .mps[USB_SPEED_HIGH] = 8, 235 }; 236 237 static const struct usb_temp_interval modem_intr_interval = { 238 .bInterval[USB_SPEED_LOW] = 8, /* 8ms */ 239 .bInterval[USB_SPEED_FULL] = 8, /* 8ms */ 240 .bInterval[USB_SPEED_HIGH] = 7, /* 8ms */ 241 }; 242 243 static const struct usb_temp_endpoint_desc modem_ep_0 = { 244 .pPacketSize = &modem_intr_mps, 245 .pIntervals = &modem_intr_interval, 246 .bEndpointAddress = UE_DIR_IN, 247 .bmAttributes = UE_INTERRUPT, 248 }; 249 250 static const struct usb_temp_endpoint_desc modem_ep_1 = { 251 .pPacketSize = &modem_bulk_mps, 252 .bEndpointAddress = UE_DIR_OUT, 253 .bmAttributes = UE_BULK, 254 }; 255 256 static const struct usb_temp_endpoint_desc modem_ep_2 = { 257 .pPacketSize = &modem_bulk_mps, 258 .bEndpointAddress = UE_DIR_IN, 259 .bmAttributes = UE_BULK, 260 }; 261 262 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = { 263 &modem_ep_0, 264 NULL, 265 }; 266 267 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = { 268 &modem_ep_1, 269 &modem_ep_2, 270 NULL, 271 }; 272 273 static const uint8_t modem_raw_desc_0[] = { 274 0x05, 0x24, 0x00, 0x10, 0x01 275 }; 276 277 static const uint8_t modem_raw_desc_1[] = { 278 0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1 279 }; 280 281 static const uint8_t modem_raw_desc_2[] = { 282 0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1 283 }; 284 285 static const uint8_t modem_raw_desc_3[] = { 286 0x04, 0x24, 0x02, 0x07 287 }; 288 289 static const void *modem_iface_0_desc[] = { 290 &modem_raw_desc_0, 291 &modem_raw_desc_1, 292 &modem_raw_desc_2, 293 &modem_raw_desc_3, 294 NULL, 295 }; 296 297 static const struct usb_temp_interface_desc modem_iface_0 = { 298 .ppRawDesc = modem_iface_0_desc, 299 .ppEndpoints = modem_iface_0_ep, 300 .bInterfaceClass = UICLASS_CDC, 301 .bInterfaceSubClass = UISUBCLASS_ABSTRACT_CONTROL_MODEL, 302 .bInterfaceProtocol = UIPROTO_CDC_AT, 303 .iInterface = MULTI_MODEM_INDEX, 304 }; 305 306 static const struct usb_temp_interface_desc modem_iface_1 = { 307 .ppEndpoints = modem_iface_1_ep, 308 .bInterfaceClass = UICLASS_CDC_DATA, 309 .bInterfaceSubClass = UISUBCLASS_DATA, 310 .bInterfaceProtocol = 0, 311 .iInterface = MULTI_MODEM_INDEX, 312 }; 313 314 static const struct usb_temp_packet_size msc_bulk_mps = { 315 .mps[USB_SPEED_FULL] = 64, 316 .mps[USB_SPEED_HIGH] = 512, 317 }; 318 319 static const struct usb_temp_endpoint_desc msc_bulk_in_ep = { 320 .pPacketSize = &msc_bulk_mps, 321 #ifdef USB_HIP_IN_EP_0 322 .bEndpointAddress = USB_HIP_IN_EP_0, 323 #else 324 .bEndpointAddress = UE_DIR_IN, 325 #endif 326 .bmAttributes = UE_BULK, 327 }; 328 329 static const struct usb_temp_endpoint_desc msc_bulk_out_ep = { 330 .pPacketSize = &msc_bulk_mps, 331 #ifdef USB_HIP_OUT_EP_0 332 .bEndpointAddress = USB_HIP_OUT_EP_0, 333 #else 334 .bEndpointAddress = UE_DIR_OUT, 335 #endif 336 .bmAttributes = UE_BULK, 337 }; 338 339 static const struct usb_temp_endpoint_desc *msc_data_endpoints[] = { 340 &msc_bulk_in_ep, 341 &msc_bulk_out_ep, 342 NULL, 343 }; 344 345 static const struct usb_temp_interface_desc msc_data_interface = { 346 .ppEndpoints = msc_data_endpoints, 347 .bInterfaceClass = UICLASS_MASS, 348 .bInterfaceSubClass = UISUBCLASS_SCSI, 349 .bInterfaceProtocol = UIPROTO_MASS_BBB, 350 .iInterface = MULTI_STORAGE_INDEX, 351 }; 352 353 static const struct usb_temp_interface_desc *multi_interfaces[] = { 354 &modem_iface_0, 355 &modem_iface_1, 356 ð_control_interface, 357 ð_data_null_interface, 358 ð_data_interface, 359 &msc_data_interface, 360 NULL, 361 }; 362 363 static const struct usb_temp_config_desc multi_config_desc = { 364 .ppIfaceDesc = multi_interfaces, 365 .bmAttributes = UC_BUS_POWERED, 366 .bMaxPower = 25, /* 50 mA */ 367 .iConfiguration = MULTI_CONFIGURATION_INDEX, 368 }; 369 static const struct usb_temp_config_desc *multi_configs[] = { 370 &multi_config_desc, 371 NULL, 372 }; 373 374 struct usb_temp_device_desc usb_template_multi = { 375 .getStringDesc = &multi_get_string_desc, 376 .ppConfigDesc = multi_configs, 377 .idVendor = USB_TEMPLATE_VENDOR, 378 .idProduct = 0x0001, 379 .bcdDevice = 0x0100, 380 .bDeviceClass = UDCLASS_IN_INTERFACE, 381 .bDeviceSubClass = 0, 382 .bDeviceProtocol = 0, 383 .iManufacturer = MULTI_MANUFACTURER_INDEX, 384 .iProduct = MULTI_PRODUCT_INDEX, 385 .iSerialNumber = MULTI_SERIAL_NUMBER_INDEX, 386 }; 387 388 /*------------------------------------------------------------------------* 389 * multi_get_string_desc 390 * 391 * Return values: 392 * NULL: Failure. No such string. 393 * Else: Success. Pointer to string descriptor is returned. 394 *------------------------------------------------------------------------*/ 395 static const void * 396 multi_get_string_desc(uint16_t lang_id, uint8_t string_index) 397 { 398 static const void *ptr[MULTI_MAX_INDEX] = { 399 [MULTI_LANG_INDEX] = &usb_string_lang_en, 400 [MULTI_MODEM_INDEX] = &multi_modem, 401 [MULTI_ETH_MAC_INDEX] = &multi_eth_mac, 402 [MULTI_ETH_CONTROL_INDEX] = &multi_eth_control, 403 [MULTI_ETH_DATA_INDEX] = &multi_eth_data, 404 [MULTI_STORAGE_INDEX] = &multi_storage, 405 [MULTI_CONFIGURATION_INDEX] = &multi_configuration, 406 [MULTI_MANUFACTURER_INDEX] = &multi_manufacturer, 407 [MULTI_PRODUCT_INDEX] = &multi_product, 408 [MULTI_SERIAL_NUMBER_INDEX] = &multi_serial_number, 409 }; 410 411 if (string_index == 0) { 412 return (&usb_string_lang_en); 413 } 414 if (lang_id != 0x0409) { 415 return (NULL); 416 } 417 if (string_index < MULTI_MAX_INDEX) { 418 return (ptr[string_index]); 419 } 420 return (NULL); 421 } 422 423 static void 424 multi_init(void *arg __unused) 425 { 426 struct sysctl_oid *parent; 427 char parent_name[3]; 428 429 usb_make_str_desc(&multi_modem, sizeof(multi_modem), 430 MULTI_DEFAULT_MODEM); 431 usb_make_str_desc(&multi_eth_mac, sizeof(multi_eth_mac), 432 MULTI_DEFAULT_ETH_MAC); 433 usb_make_str_desc(&multi_eth_control, sizeof(multi_eth_control), 434 MULTI_DEFAULT_ETH_CONTROL); 435 usb_make_str_desc(&multi_eth_data, sizeof(multi_eth_data), 436 MULTI_DEFAULT_ETH_DATA); 437 usb_make_str_desc(&multi_storage, sizeof(multi_storage), 438 MULTI_DEFAULT_STORAGE); 439 usb_make_str_desc(&multi_configuration, sizeof(multi_configuration), 440 MULTI_DEFAULT_CONFIGURATION); 441 usb_make_str_desc(&multi_manufacturer, sizeof(multi_manufacturer), 442 MULTI_DEFAULT_MANUFACTURER); 443 usb_make_str_desc(&multi_product, sizeof(multi_product), 444 MULTI_DEFAULT_PRODUCT); 445 usb_make_str_desc(&multi_serial_number, sizeof(multi_serial_number), 446 MULTI_DEFAULT_SERIAL_NUMBER); 447 448 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MULTI); 449 sysctl_ctx_init(&multi_ctx_list); 450 451 parent = SYSCTL_ADD_NODE(&multi_ctx_list, 452 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO, 453 parent_name, CTLFLAG_RW, 454 0, "USB Multifunction device side template"); 455 SYSCTL_ADD_U16(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 456 "vendor_id", CTLFLAG_RWTUN, 457 &usb_template_multi.idVendor, 1, "Vendor identifier"); 458 SYSCTL_ADD_U16(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 459 "product_id", CTLFLAG_RWTUN, 460 &usb_template_multi.idProduct, 1, "Product identifier"); 461 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 462 "eth_mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 463 &multi_eth_mac, sizeof(multi_eth_mac), usb_temp_sysctl, 464 "A", "Ethernet MAC address string"); 465 #if 0 466 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 467 "modem", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 468 &multi_modem, sizeof(multi_modem), usb_temp_sysctl, 469 "A", "Modem interface string"); 470 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 471 "eth_control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 472 &multi_eth_control, sizeof(multi_eth_data), usb_temp_sysctl, 473 "A", "Ethernet control interface string"); 474 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 475 "eth_data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 476 &multi_eth_data, sizeof(multi_eth_data), usb_temp_sysctl, 477 "A", "Ethernet data interface string"); 478 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 479 "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 480 &multi_storage, sizeof(multi_storage), usb_temp_sysctl, 481 "A", "Storage interface string"); 482 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 483 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 484 &multi_configuration, sizeof(multi_configuration), usb_temp_sysctl, 485 "A", "Configuration string"); 486 #endif 487 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 488 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 489 &multi_manufacturer, sizeof(multi_manufacturer), usb_temp_sysctl, 490 "A", "Manufacturer string"); 491 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 492 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 493 &multi_product, sizeof(multi_product), usb_temp_sysctl, 494 "A", "Product string"); 495 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 496 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 497 &multi_serial_number, sizeof(multi_serial_number), usb_temp_sysctl, 498 "A", "Serial number string"); 499 } 500 501 static void 502 multi_uninit(void *arg __unused) 503 { 504 505 sysctl_ctx_free(&multi_ctx_list); 506 } 507 508 SYSINIT(multi_init, SI_SUB_LOCK, SI_ORDER_FIRST, multi_init, NULL); 509 SYSUNINIT(multi_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, multi_uninit, NULL); 510