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