1 /*- 2 * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com> 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 /* 31 * This file contains the USB template for USB Networking and Serial 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #ifdef USB_GLOBAL_INCLUDE_FILE 38 #include USB_GLOBAL_INCLUDE_FILE 39 #else 40 #include <sys/stdint.h> 41 #include <sys/stddef.h> 42 #include <sys/param.h> 43 #include <sys/queue.h> 44 #include <sys/types.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/bus.h> 48 #include <sys/module.h> 49 #include <sys/lock.h> 50 #include <sys/mutex.h> 51 #include <sys/condvar.h> 52 #include <sys/sysctl.h> 53 #include <sys/sx.h> 54 #include <sys/unistd.h> 55 #include <sys/callout.h> 56 #include <sys/malloc.h> 57 #include <sys/priv.h> 58 59 #include <dev/usb/usb.h> 60 #include <dev/usb/usbdi.h> 61 #include <dev/usb/usb_core.h> 62 #include <dev/usb/usb_cdc.h> 63 64 #include <dev/usb/template/usb_template.h> 65 #endif /* USB_GLOBAL_INCLUDE_FILE */ 66 67 #define MODEM_IFACE_0 0 68 #define MODEM_IFACE_1 1 69 70 enum { 71 STRING_LANG_INDEX, 72 STRING_MODEM_INDEX, 73 STRING_ETH_MAC_INDEX, 74 STRING_ETH_CONTROL_INDEX, 75 STRING_ETH_DATA_INDEX, 76 STRING_ETH_CONFIG_INDEX, 77 STRING_CONFIG_INDEX, 78 STRING_VENDOR_INDEX, 79 STRING_PRODUCT_INDEX, 80 STRING_SERIAL_INDEX, 81 STRING_MAX, 82 }; 83 84 #define STRING_MODEM \ 85 "U\0S\0B\0 \0M\0o\0d\0e\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e" 86 87 #define STRING_ETH_MAC \ 88 "2\0A\0002\0003\0004\0005\0006\0007\08\09\0A\0B" 89 90 #define STRING_ETH_CONTROL \ 91 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 " \ 92 "\0C\0o\0m\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e" 93 94 #define STRING_ETH_DATA \ 95 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0D\0a\0t\0a\0 " \ 96 "\0I\0n\0t\0e\0r\0f\0a\0c\0e" 97 98 #define STRING_CONFIG \ 99 "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g\0u\0r\0a\0t\0i\0o\0n" 100 101 #define STRING_VENDOR \ 102 "T\0h\0e\0 \0F\0r\0e\0e\0B\0S\0D\0 \0P\0r\0o\0j\0e\0c\0t" 103 104 #define STRING_PRODUCT \ 105 "S\0E\0R\0I\0A\0L\0N\0E\0T" 106 107 #define STRING_SERIAL \ 108 "J\0a\0n\0u\0a\0r\0y\0 \0002\0000\0001\0005" 109 110 /* make the real string descriptors */ 111 112 USB_MAKE_STRING_DESC(STRING_MODEM, string_modem); 113 USB_MAKE_STRING_DESC(STRING_ETH_MAC, string_eth_mac); 114 USB_MAKE_STRING_DESC(STRING_ETH_CONTROL, string_eth_control); 115 USB_MAKE_STRING_DESC(STRING_ETH_DATA, string_eth_data); 116 USB_MAKE_STRING_DESC(STRING_CONFIG, string_serialnet_config); 117 USB_MAKE_STRING_DESC(STRING_VENDOR, string_serialnet_vendor); 118 USB_MAKE_STRING_DESC(STRING_PRODUCT, string_serialnet_product); 119 USB_MAKE_STRING_DESC(STRING_SERIAL, string_serialnet_serial); 120 121 /* prototypes */ 122 123 static usb_temp_get_string_desc_t serialnet_get_string_desc; 124 125 static const struct usb_cdc_union_descriptor eth_union_desc = { 126 .bLength = sizeof(eth_union_desc), 127 .bDescriptorType = UDESC_CS_INTERFACE, 128 .bDescriptorSubtype = UDESCSUB_CDC_UNION, 129 .bMasterInterface = 0, /* this is automatically updated */ 130 .bSlaveInterface[0] = 1, /* this is automatically updated */ 131 }; 132 133 static const struct usb_cdc_header_descriptor eth_header_desc = { 134 .bLength = sizeof(eth_header_desc), 135 .bDescriptorType = UDESC_CS_INTERFACE, 136 .bDescriptorSubtype = UDESCSUB_CDC_HEADER, 137 .bcdCDC[0] = 0x10, 138 .bcdCDC[1] = 0x01, 139 }; 140 141 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = { 142 .bLength = sizeof(eth_enf_desc), 143 .bDescriptorType = UDESC_CS_INTERFACE, 144 .bDescriptorSubtype = UDESCSUB_CDC_ENF, 145 .iMacAddress = STRING_ETH_MAC_INDEX, 146 .bmEthernetStatistics = {0, 0, 0, 0}, 147 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */ 148 .wNumberMCFilters = {0, 0}, 149 .bNumberPowerFilters = 0, 150 }; 151 152 static const void *eth_control_if_desc[] = { 153 ð_union_desc, 154 ð_header_desc, 155 ð_enf_desc, 156 NULL, 157 }; 158 159 static const struct usb_temp_packet_size bulk_mps = { 160 .mps[USB_SPEED_FULL] = 64, 161 .mps[USB_SPEED_HIGH] = 512, 162 }; 163 164 static const struct usb_temp_packet_size intr_mps = { 165 .mps[USB_SPEED_FULL] = 8, 166 .mps[USB_SPEED_HIGH] = 8, 167 }; 168 169 static const struct usb_temp_endpoint_desc bulk_in_ep = { 170 .pPacketSize = &bulk_mps, 171 #ifdef USB_HIP_IN_EP_0 172 .bEndpointAddress = USB_HIP_IN_EP_0, 173 #else 174 .bEndpointAddress = UE_DIR_IN, 175 #endif 176 .bmAttributes = UE_BULK, 177 }; 178 179 static const struct usb_temp_endpoint_desc bulk_out_ep = { 180 .pPacketSize = &bulk_mps, 181 #ifdef USB_HIP_OUT_EP_0 182 .bEndpointAddress = USB_HIP_OUT_EP_0, 183 #else 184 .bEndpointAddress = UE_DIR_OUT, 185 #endif 186 .bmAttributes = UE_BULK, 187 }; 188 189 static const struct usb_temp_endpoint_desc intr_in_ep = { 190 .pPacketSize = &intr_mps, 191 .bEndpointAddress = UE_DIR_IN, 192 .bmAttributes = UE_INTERRUPT, 193 }; 194 195 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = { 196 &intr_in_ep, 197 NULL, 198 }; 199 200 static const struct usb_temp_interface_desc eth_control_interface = { 201 .ppEndpoints = eth_intr_endpoints, 202 .ppRawDesc = eth_control_if_desc, 203 .bInterfaceClass = UICLASS_CDC, 204 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 205 .bInterfaceProtocol = 0, 206 .iInterface = STRING_ETH_CONTROL_INDEX, 207 }; 208 209 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = { 210 &bulk_in_ep, 211 &bulk_out_ep, 212 NULL, 213 }; 214 215 static const struct usb_temp_interface_desc eth_data_null_interface = { 216 .ppEndpoints = NULL, /* no endpoints */ 217 .bInterfaceClass = UICLASS_CDC_DATA, 218 .bInterfaceSubClass = 0, 219 .bInterfaceProtocol = 0, 220 .iInterface = STRING_ETH_DATA_INDEX, 221 }; 222 223 static const struct usb_temp_interface_desc eth_data_interface = { 224 .ppEndpoints = eth_data_endpoints, 225 .bInterfaceClass = UICLASS_CDC_DATA, 226 .bInterfaceSubClass = UISUBCLASS_DATA, 227 .bInterfaceProtocol = 0, 228 .iInterface = STRING_ETH_DATA_INDEX, 229 .isAltInterface = 1, /* this is an alternate setting */ 230 }; 231 232 static const struct usb_temp_packet_size modem_bulk_mps = { 233 .mps[USB_SPEED_LOW] = 8, 234 .mps[USB_SPEED_FULL] = 64, 235 .mps[USB_SPEED_HIGH] = 512, 236 }; 237 238 static const struct usb_temp_packet_size modem_intr_mps = { 239 .mps[USB_SPEED_LOW] = 8, 240 .mps[USB_SPEED_FULL] = 8, 241 .mps[USB_SPEED_HIGH] = 8, 242 }; 243 244 static const struct usb_temp_interval modem_intr_interval = { 245 .bInterval[USB_SPEED_LOW] = 8, /* 8ms */ 246 .bInterval[USB_SPEED_FULL] = 8, /* 8ms */ 247 .bInterval[USB_SPEED_HIGH] = 7, /* 8ms */ 248 }; 249 250 static const struct usb_temp_endpoint_desc modem_ep_0 = { 251 .pPacketSize = &modem_intr_mps, 252 .pIntervals = &modem_intr_interval, 253 .bEndpointAddress = UE_DIR_IN, 254 .bmAttributes = UE_INTERRUPT, 255 }; 256 257 static const struct usb_temp_endpoint_desc modem_ep_1 = { 258 .pPacketSize = &modem_bulk_mps, 259 .bEndpointAddress = UE_DIR_OUT, 260 .bmAttributes = UE_BULK, 261 }; 262 263 static const struct usb_temp_endpoint_desc modem_ep_2 = { 264 .pPacketSize = &modem_bulk_mps, 265 .bEndpointAddress = UE_DIR_IN, 266 .bmAttributes = UE_BULK, 267 }; 268 269 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = { 270 &modem_ep_0, 271 NULL, 272 }; 273 274 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = { 275 &modem_ep_1, 276 &modem_ep_2, 277 NULL, 278 }; 279 280 static const uint8_t modem_raw_desc_0[] = { 281 0x05, 0x24, 0x00, 0x10, 0x01 282 }; 283 284 static const uint8_t modem_raw_desc_1[] = { 285 0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1 286 }; 287 288 static const uint8_t modem_raw_desc_2[] = { 289 0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1 290 }; 291 292 static const uint8_t modem_raw_desc_3[] = { 293 0x04, 0x24, 0x02, 0x07 294 }; 295 296 static const void *modem_iface_0_desc[] = { 297 &modem_raw_desc_0, 298 &modem_raw_desc_1, 299 &modem_raw_desc_2, 300 &modem_raw_desc_3, 301 NULL, 302 }; 303 304 static const struct usb_temp_interface_desc modem_iface_0 = { 305 .ppRawDesc = modem_iface_0_desc, 306 .ppEndpoints = modem_iface_0_ep, 307 .bInterfaceClass = 2, 308 .bInterfaceSubClass = 2, 309 .bInterfaceProtocol = 1, 310 .iInterface = STRING_MODEM_INDEX, 311 }; 312 313 static const struct usb_temp_interface_desc modem_iface_1 = { 314 .ppEndpoints = modem_iface_1_ep, 315 .bInterfaceClass = 10, 316 .bInterfaceSubClass = 0, 317 .bInterfaceProtocol = 0, 318 .iInterface = STRING_MODEM_INDEX, 319 }; 320 321 static const struct usb_temp_interface_desc *serialnet_interfaces[] = { 322 &modem_iface_0, 323 &modem_iface_1, 324 ð_control_interface, 325 ð_data_null_interface, 326 ð_data_interface, 327 NULL, 328 }; 329 330 static const struct usb_temp_config_desc serialnet_config_desc = { 331 .ppIfaceDesc = serialnet_interfaces, 332 .bmAttributes = UC_BUS_POWERED, 333 .bMaxPower = 25, /* 50 mA */ 334 .iConfiguration = STRING_CONFIG_INDEX, 335 }; 336 static const struct usb_temp_config_desc *serialnet_configs[] = { 337 &serialnet_config_desc, 338 NULL, 339 }; 340 341 const struct usb_temp_device_desc usb_template_serialnet = { 342 .getStringDesc = &serialnet_get_string_desc, 343 .ppConfigDesc = serialnet_configs, 344 .idVendor = USB_TEMPLATE_VENDOR, 345 .idProduct = 0x0001, 346 .bcdDevice = 0x0100, 347 .bDeviceClass = UDCLASS_COMM, 348 .bDeviceSubClass = 0, 349 .bDeviceProtocol = 0, 350 .iManufacturer = STRING_VENDOR_INDEX, 351 .iProduct = STRING_PRODUCT_INDEX, 352 .iSerialNumber = STRING_SERIAL_INDEX, 353 }; 354 355 /*------------------------------------------------------------------------* 356 * serialnet_get_string_desc 357 * 358 * Return values: 359 * NULL: Failure. No such string. 360 * Else: Success. Pointer to string descriptor is returned. 361 *------------------------------------------------------------------------*/ 362 static const void * 363 serialnet_get_string_desc(uint16_t lang_id, uint8_t string_index) 364 { 365 static const void *ptr[STRING_MAX] = { 366 [STRING_LANG_INDEX] = &usb_string_lang_en, 367 [STRING_MODEM_INDEX] = &string_modem, 368 [STRING_ETH_MAC_INDEX] = &string_eth_mac, 369 [STRING_ETH_CONTROL_INDEX] = &string_eth_control, 370 [STRING_ETH_DATA_INDEX] = &string_eth_data, 371 [STRING_CONFIG_INDEX] = &string_serialnet_config, 372 [STRING_VENDOR_INDEX] = &string_serialnet_vendor, 373 [STRING_PRODUCT_INDEX] = &string_serialnet_product, 374 [STRING_SERIAL_INDEX] = &string_serialnet_serial, 375 }; 376 377 if (string_index == 0) { 378 return (&usb_string_lang_en); 379 } 380 if (lang_id != 0x0409) { 381 return (NULL); 382 } 383 if (string_index < STRING_MAX) { 384 return (ptr[string_index]); 385 } 386 return (NULL); 387 } 388