1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 * This file contains the USB template for USB Networking and Serial 38 */ 39 40 #ifdef USB_GLOBAL_INCLUDE_FILE 41 #include USB_GLOBAL_INCLUDE_FILE 42 #else 43 #include <sys/stdint.h> 44 #include <sys/stddef.h> 45 #include <sys/param.h> 46 #include <sys/queue.h> 47 #include <sys/types.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/bus.h> 51 #include <sys/module.h> 52 #include <sys/lock.h> 53 #include <sys/mutex.h> 54 #include <sys/condvar.h> 55 #include <sys/sysctl.h> 56 #include <sys/sx.h> 57 #include <sys/unistd.h> 58 #include <sys/callout.h> 59 #include <sys/malloc.h> 60 #include <sys/priv.h> 61 62 #include <dev/usb/usb.h> 63 #include <dev/usb/usbdi.h> 64 #include <dev/usb/usb_core.h> 65 #include <dev/usb/usb_cdc.h> 66 #include <dev/usb/usb_ioctl.h> 67 #include <dev/usb/usb_util.h> 68 69 #include <dev/usb/template/usb_template.h> 70 #endif /* USB_GLOBAL_INCLUDE_FILE */ 71 72 #define MODEM_IFACE_0 0 73 #define MODEM_IFACE_1 1 74 75 enum { 76 SERIALNET_LANG_INDEX, 77 SERIALNET_MODEM_INDEX, 78 SERIALNET_ETH_MAC_INDEX, 79 SERIALNET_ETH_CONTROL_INDEX, 80 SERIALNET_ETH_DATA_INDEX, 81 SERIALNET_CONFIGURATION_INDEX, 82 SERIALNET_MANUFACTURER_INDEX, 83 SERIALNET_PRODUCT_INDEX, 84 SERIALNET_SERIAL_NUMBER_INDEX, 85 SERIALNET_MAX_INDEX, 86 }; 87 88 #define SERIALNET_DEFAULT_VENDOR_ID USB_TEMPLATE_VENDOR 89 #define SERIALNET_DEFAULT_PRODUCT_ID 0x05dc 90 #define SERIALNET_DEFAULT_MODEM "Virtual serial port" 91 #define SERIALNET_DEFAULT_ETH_MAC "2A02030405060789AB" 92 #define SERIALNET_DEFAULT_ETH_CONTROL "USB Ethernet Comm Interface" 93 #define SERIALNET_DEFAULT_ETH_DATA "USB Ethernet Data Interface" 94 #define SERIALNET_DEFAULT_CONFIGURATION "Default configuration" 95 #define SERIALNET_DEFAULT_MANUFACTURER USB_TEMPLATE_MANUFACTURER 96 #define SERIALNET_DEFAULT_PRODUCT "Serial/Ethernet device" 97 /* 98 * The reason for this being called like this is that OSX 99 * derives the device node name from it, resulting in a somewhat 100 * user-friendly "/dev/cu.usbmodemFreeBSD1". And yes, the "1" 101 * needs to be there, otherwise OSX will mangle it. 102 */ 103 #define SERIALNET_DEFAULT_SERIAL_NUMBER "FreeBSD1" 104 105 static struct usb_string_descriptor serialnet_modem; 106 static struct usb_string_descriptor serialnet_eth_mac; 107 static struct usb_string_descriptor serialnet_eth_control; 108 static struct usb_string_descriptor serialnet_eth_data; 109 static struct usb_string_descriptor serialnet_configuration; 110 static struct usb_string_descriptor serialnet_manufacturer; 111 static struct usb_string_descriptor serialnet_product; 112 static struct usb_string_descriptor serialnet_serial_number; 113 114 static struct sysctl_ctx_list serialnet_ctx_list; 115 116 /* prototypes */ 117 118 static usb_temp_get_string_desc_t serialnet_get_string_desc; 119 120 static const struct usb_cdc_union_descriptor eth_union_desc = { 121 .bLength = sizeof(eth_union_desc), 122 .bDescriptorType = UDESC_CS_INTERFACE, 123 .bDescriptorSubtype = UDESCSUB_CDC_UNION, 124 .bMasterInterface = 0, /* this is automatically updated */ 125 .bSlaveInterface[0] = 1, /* this is automatically updated */ 126 }; 127 128 static const struct usb_cdc_header_descriptor eth_header_desc = { 129 .bLength = sizeof(eth_header_desc), 130 .bDescriptorType = UDESC_CS_INTERFACE, 131 .bDescriptorSubtype = UDESCSUB_CDC_HEADER, 132 .bcdCDC[0] = 0x10, 133 .bcdCDC[1] = 0x01, 134 }; 135 136 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = { 137 .bLength = sizeof(eth_enf_desc), 138 .bDescriptorType = UDESC_CS_INTERFACE, 139 .bDescriptorSubtype = UDESCSUB_CDC_ENF, 140 .iMacAddress = SERIALNET_ETH_MAC_INDEX, 141 .bmEthernetStatistics = {0, 0, 0, 0}, 142 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */ 143 .wNumberMCFilters = {0, 0}, 144 .bNumberPowerFilters = 0, 145 }; 146 147 static const void *eth_control_if_desc[] = { 148 ð_union_desc, 149 ð_header_desc, 150 ð_enf_desc, 151 NULL, 152 }; 153 154 static const struct usb_temp_packet_size bulk_mps = { 155 .mps[USB_SPEED_FULL] = 64, 156 .mps[USB_SPEED_HIGH] = 512, 157 }; 158 159 static const struct usb_temp_packet_size intr_mps = { 160 .mps[USB_SPEED_FULL] = 8, 161 .mps[USB_SPEED_HIGH] = 8, 162 }; 163 164 static const struct usb_temp_endpoint_desc bulk_in_ep = { 165 .pPacketSize = &bulk_mps, 166 #ifdef USB_HIP_IN_EP_0 167 .bEndpointAddress = USB_HIP_IN_EP_0, 168 #else 169 .bEndpointAddress = UE_DIR_IN, 170 #endif 171 .bmAttributes = UE_BULK, 172 }; 173 174 static const struct usb_temp_endpoint_desc bulk_out_ep = { 175 .pPacketSize = &bulk_mps, 176 #ifdef USB_HIP_OUT_EP_0 177 .bEndpointAddress = USB_HIP_OUT_EP_0, 178 #else 179 .bEndpointAddress = UE_DIR_OUT, 180 #endif 181 .bmAttributes = UE_BULK, 182 }; 183 184 static const struct usb_temp_endpoint_desc intr_in_ep = { 185 .pPacketSize = &intr_mps, 186 .bEndpointAddress = UE_DIR_IN, 187 .bmAttributes = UE_INTERRUPT, 188 }; 189 190 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = { 191 &intr_in_ep, 192 NULL, 193 }; 194 195 static const struct usb_temp_interface_desc eth_control_interface = { 196 .ppEndpoints = eth_intr_endpoints, 197 .ppRawDesc = eth_control_if_desc, 198 .bInterfaceClass = UICLASS_CDC, 199 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 200 .bInterfaceProtocol = UIPROTO_CDC_NONE, 201 .iInterface = SERIALNET_ETH_CONTROL_INDEX, 202 }; 203 204 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = { 205 &bulk_in_ep, 206 &bulk_out_ep, 207 NULL, 208 }; 209 210 static const struct usb_temp_interface_desc eth_data_null_interface = { 211 .ppEndpoints = NULL, /* no endpoints */ 212 .bInterfaceClass = UICLASS_CDC_DATA, 213 .bInterfaceSubClass = UISUBCLASS_DATA, 214 .bInterfaceProtocol = 0, 215 .iInterface = SERIALNET_ETH_DATA_INDEX, 216 }; 217 218 static const struct usb_temp_interface_desc eth_data_interface = { 219 .ppEndpoints = eth_data_endpoints, 220 .bInterfaceClass = UICLASS_CDC_DATA, 221 .bInterfaceSubClass = UISUBCLASS_DATA, 222 .bInterfaceProtocol = 0, 223 .iInterface = SERIALNET_ETH_DATA_INDEX, 224 .isAltInterface = 1, /* this is an alternate setting */ 225 }; 226 227 static const struct usb_temp_packet_size modem_bulk_mps = { 228 .mps[USB_SPEED_LOW] = 8, 229 .mps[USB_SPEED_FULL] = 64, 230 .mps[USB_SPEED_HIGH] = 512, 231 }; 232 233 static const struct usb_temp_packet_size modem_intr_mps = { 234 .mps[USB_SPEED_LOW] = 8, 235 .mps[USB_SPEED_FULL] = 8, 236 .mps[USB_SPEED_HIGH] = 8, 237 }; 238 239 static const struct usb_temp_interval modem_intr_interval = { 240 .bInterval[USB_SPEED_LOW] = 8, /* 8ms */ 241 .bInterval[USB_SPEED_FULL] = 8, /* 8ms */ 242 .bInterval[USB_SPEED_HIGH] = 7, /* 8ms */ 243 }; 244 245 static const struct usb_temp_endpoint_desc modem_ep_0 = { 246 .pPacketSize = &modem_intr_mps, 247 .pIntervals = &modem_intr_interval, 248 .bEndpointAddress = UE_DIR_IN, 249 .bmAttributes = UE_INTERRUPT, 250 }; 251 252 static const struct usb_temp_endpoint_desc modem_ep_1 = { 253 .pPacketSize = &modem_bulk_mps, 254 .bEndpointAddress = UE_DIR_OUT, 255 .bmAttributes = UE_BULK, 256 }; 257 258 static const struct usb_temp_endpoint_desc modem_ep_2 = { 259 .pPacketSize = &modem_bulk_mps, 260 .bEndpointAddress = UE_DIR_IN, 261 .bmAttributes = UE_BULK, 262 }; 263 264 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = { 265 &modem_ep_0, 266 NULL, 267 }; 268 269 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = { 270 &modem_ep_1, 271 &modem_ep_2, 272 NULL, 273 }; 274 275 static const uint8_t modem_raw_desc_0[] = { 276 0x05, 0x24, 0x00, 0x10, 0x01 277 }; 278 279 static const uint8_t modem_raw_desc_1[] = { 280 0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1 281 }; 282 283 static const uint8_t modem_raw_desc_2[] = { 284 0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1 285 }; 286 287 static const uint8_t modem_raw_desc_3[] = { 288 0x04, 0x24, 0x02, 0x07 289 }; 290 291 static const void *modem_iface_0_desc[] = { 292 &modem_raw_desc_0, 293 &modem_raw_desc_1, 294 &modem_raw_desc_2, 295 &modem_raw_desc_3, 296 NULL, 297 }; 298 299 static const struct usb_temp_interface_desc modem_iface_0 = { 300 .ppRawDesc = modem_iface_0_desc, 301 .ppEndpoints = modem_iface_0_ep, 302 .bInterfaceClass = UICLASS_CDC, 303 .bInterfaceSubClass = UISUBCLASS_ABSTRACT_CONTROL_MODEL, 304 .bInterfaceProtocol = UIPROTO_CDC_NONE, 305 .iInterface = SERIALNET_MODEM_INDEX, 306 }; 307 308 static const struct usb_temp_interface_desc modem_iface_1 = { 309 .ppEndpoints = modem_iface_1_ep, 310 .bInterfaceClass = UICLASS_CDC_DATA, 311 .bInterfaceSubClass = UISUBCLASS_DATA, 312 .bInterfaceProtocol = 0, 313 .iInterface = SERIALNET_MODEM_INDEX, 314 }; 315 316 static const struct usb_temp_interface_desc *serialnet_interfaces[] = { 317 &modem_iface_0, 318 &modem_iface_1, 319 ð_control_interface, 320 ð_data_null_interface, 321 ð_data_interface, 322 NULL, 323 }; 324 325 static const struct usb_temp_config_desc serialnet_config_desc = { 326 .ppIfaceDesc = serialnet_interfaces, 327 .bmAttributes = 0, 328 .bMaxPower = 0, 329 .iConfiguration = SERIALNET_CONFIGURATION_INDEX, 330 }; 331 static const struct usb_temp_config_desc *serialnet_configs[] = { 332 &serialnet_config_desc, 333 NULL, 334 }; 335 336 struct usb_temp_device_desc usb_template_serialnet = { 337 .getStringDesc = &serialnet_get_string_desc, 338 .ppConfigDesc = serialnet_configs, 339 .idVendor = SERIALNET_DEFAULT_VENDOR_ID, 340 .idProduct = SERIALNET_DEFAULT_PRODUCT_ID, 341 .bcdDevice = 0x0100, 342 .bDeviceClass = UDCLASS_IN_INTERFACE, 343 .bDeviceSubClass = 0, 344 .bDeviceProtocol = 0, 345 .iManufacturer = SERIALNET_MANUFACTURER_INDEX, 346 .iProduct = SERIALNET_PRODUCT_INDEX, 347 .iSerialNumber = SERIALNET_SERIAL_NUMBER_INDEX, 348 }; 349 350 /*------------------------------------------------------------------------* 351 * serialnet_get_string_desc 352 * 353 * Return values: 354 * NULL: Failure. No such string. 355 * Else: Success. Pointer to string descriptor is returned. 356 *------------------------------------------------------------------------*/ 357 static const void * 358 serialnet_get_string_desc(uint16_t lang_id, uint8_t string_index) 359 { 360 static const void *ptr[SERIALNET_MAX_INDEX] = { 361 [SERIALNET_LANG_INDEX] = &usb_string_lang_en, 362 [SERIALNET_MODEM_INDEX] = &serialnet_modem, 363 [SERIALNET_ETH_MAC_INDEX] = &serialnet_eth_mac, 364 [SERIALNET_ETH_CONTROL_INDEX] = &serialnet_eth_control, 365 [SERIALNET_ETH_DATA_INDEX] = &serialnet_eth_data, 366 [SERIALNET_CONFIGURATION_INDEX] = &serialnet_configuration, 367 [SERIALNET_MANUFACTURER_INDEX] = &serialnet_manufacturer, 368 [SERIALNET_PRODUCT_INDEX] = &serialnet_product, 369 [SERIALNET_SERIAL_NUMBER_INDEX] = &serialnet_serial_number, 370 }; 371 372 if (string_index == 0) { 373 return (&usb_string_lang_en); 374 } 375 if (lang_id != 0x0409) { 376 return (NULL); 377 } 378 if (string_index < SERIALNET_MAX_INDEX) { 379 return (ptr[string_index]); 380 } 381 return (NULL); 382 } 383 384 static void 385 serialnet_init(void *arg __unused) 386 { 387 struct sysctl_oid *parent; 388 char parent_name[3]; 389 390 usb_make_str_desc(&serialnet_modem, sizeof(serialnet_modem), 391 SERIALNET_DEFAULT_MODEM); 392 usb_make_str_desc(&serialnet_eth_mac, sizeof(serialnet_eth_mac), 393 SERIALNET_DEFAULT_ETH_MAC); 394 usb_make_str_desc(&serialnet_eth_control, sizeof(serialnet_eth_control), 395 SERIALNET_DEFAULT_ETH_CONTROL); 396 usb_make_str_desc(&serialnet_eth_data, sizeof(serialnet_eth_data), 397 SERIALNET_DEFAULT_ETH_DATA); 398 usb_make_str_desc(&serialnet_configuration, sizeof(serialnet_configuration), 399 SERIALNET_DEFAULT_CONFIGURATION); 400 usb_make_str_desc(&serialnet_manufacturer, sizeof(serialnet_manufacturer), 401 SERIALNET_DEFAULT_MANUFACTURER); 402 usb_make_str_desc(&serialnet_product, sizeof(serialnet_product), 403 SERIALNET_DEFAULT_PRODUCT); 404 usb_make_str_desc(&serialnet_serial_number, sizeof(serialnet_serial_number), 405 SERIALNET_DEFAULT_SERIAL_NUMBER); 406 407 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_SERIALNET); 408 sysctl_ctx_init(&serialnet_ctx_list); 409 410 parent = SYSCTL_ADD_NODE(&serialnet_ctx_list, 411 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO, 412 parent_name, CTLFLAG_RW | CTLFLAG_MPSAFE, 413 0, "USB CDC Serial/Ethernet device side template"); 414 SYSCTL_ADD_U16(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 415 "vendor_id", CTLFLAG_RWTUN, 416 &usb_template_serialnet.idVendor, 1, "Vendor identifier"); 417 SYSCTL_ADD_U16(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 418 "product_id", CTLFLAG_RWTUN, 419 &usb_template_serialnet.idProduct, 1, "Product identifier"); 420 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 421 "eth_mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 422 &serialnet_eth_mac, sizeof(serialnet_eth_mac), usb_temp_sysctl, 423 "A", "Ethernet MAC address string"); 424 #if 0 425 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 426 "modem", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 427 &serialnet_modem, sizeof(serialnet_modem), usb_temp_sysctl, 428 "A", "Modem interface string"); 429 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 430 "eth_control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 431 &serialnet_eth_control, sizeof(serialnet_eth_data), usb_temp_sysctl, 432 "A", "Ethernet control interface string"); 433 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 434 "eth_data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 435 &serialnet_eth_data, sizeof(serialnet_eth_data), usb_temp_sysctl, 436 "A", "Ethernet data interface string"); 437 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 438 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 439 &serialnet_configuration, sizeof(serialnet_configuration), usb_temp_sysctl, 440 "A", "Configuration string"); 441 #endif 442 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 443 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 444 &serialnet_manufacturer, sizeof(serialnet_manufacturer), usb_temp_sysctl, 445 "A", "Manufacturer string"); 446 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 447 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 448 &serialnet_product, sizeof(serialnet_product), usb_temp_sysctl, 449 "A", "Product string"); 450 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 451 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 452 &serialnet_serial_number, sizeof(serialnet_serial_number), usb_temp_sysctl, 453 "A", "Serial number string"); 454 } 455 456 static void 457 serialnet_uninit(void *arg __unused) 458 { 459 460 sysctl_ctx_free(&serialnet_ctx_list); 461 } 462 463 SYSINIT(serialnet_init, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_init, NULL); 464 SYSUNINIT(serialnet_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_uninit, NULL); 465