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/param.h> 42 #include <sys/queue.h> 43 #include <sys/types.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/bus.h> 47 #include <sys/module.h> 48 #include <sys/lock.h> 49 #include <sys/mutex.h> 50 #include <sys/condvar.h> 51 #include <sys/sysctl.h> 52 #include <sys/unistd.h> 53 #include <sys/callout.h> 54 #include <sys/malloc.h> 55 #include <sys/priv.h> 56 57 #include <bus/u4b/usb.h> 58 #include <bus/u4b/usbdi.h> 59 #include <bus/u4b/usb_core.h> 60 #include <bus/u4b/usb_cdc.h> 61 62 #include <bus/u4b/template/usb_template.h> 63 #endif /* USB_GLOBAL_INCLUDE_FILE */ 64 65 #define MODEM_IFACE_0 0 66 #define MODEM_IFACE_1 1 67 68 enum { 69 STRING_LANG_INDEX, 70 STRING_MODEM_INDEX, 71 STRING_ETH_MAC_INDEX, 72 STRING_ETH_CONTROL_INDEX, 73 STRING_ETH_DATA_INDEX, 74 STRING_ETH_CONFIG_INDEX, 75 STRING_CONFIG_INDEX, 76 STRING_VENDOR_INDEX, 77 STRING_PRODUCT_INDEX, 78 STRING_SERIAL_INDEX, 79 STRING_MAX, 80 }; 81 82 #define STRING_MODEM \ 83 "U\0S\0B\0 \0M\0o\0d\0e\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e" 84 85 #define STRING_ETH_MAC \ 86 "2\0A\0002\0003\0004\0005\0006\0007\08\09\0A\0B" 87 88 #define STRING_ETH_CONTROL \ 89 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 " \ 90 "\0C\0o\0m\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e" 91 92 #define STRING_ETH_DATA \ 93 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0D\0a\0t\0a\0 " \ 94 "\0I\0n\0t\0e\0r\0f\0a\0c\0e" 95 96 #define STRING_CONFIG \ 97 "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g\0u\0r\0a\0t\0i\0o\0n" 98 99 #define STRING_VENDOR \ 100 "T\0h\0e\0 \0F\0r\0e\0e\0B\0S\0D\0 \0P\0r\0o\0j\0e\0c\0t" 101 102 #define STRING_PRODUCT \ 103 "S\0E\0R\0I\0A\0L\0N\0E\0T" 104 105 #define STRING_SERIAL \ 106 "J\0a\0n\0u\0a\0r\0y\0 \0002\0000\0001\0005" 107 108 /* make the real string descriptors */ 109 110 USB_MAKE_STRING_DESC(STRING_MODEM, string_modem); 111 USB_MAKE_STRING_DESC(STRING_ETH_MAC, string_eth_mac); 112 USB_MAKE_STRING_DESC(STRING_ETH_CONTROL, string_eth_control); 113 USB_MAKE_STRING_DESC(STRING_ETH_DATA, string_eth_data); 114 USB_MAKE_STRING_DESC(STRING_CONFIG, string_serialnet_config); 115 USB_MAKE_STRING_DESC(STRING_VENDOR, string_serialnet_vendor); 116 USB_MAKE_STRING_DESC(STRING_PRODUCT, string_serialnet_product); 117 USB_MAKE_STRING_DESC(STRING_SERIAL, string_serialnet_serial); 118 119 /* prototypes */ 120 121 static usb_temp_get_string_desc_t serialnet_get_string_desc; 122 123 static const struct usb_cdc_union_descriptor eth_union_desc = { 124 .bLength = sizeof(eth_union_desc), 125 .bDescriptorType = UDESC_CS_INTERFACE, 126 .bDescriptorSubtype = UDESCSUB_CDC_UNION, 127 .bMasterInterface = 0, /* this is automatically updated */ 128 .bSlaveInterface[0] = 1, /* this is automatically updated */ 129 }; 130 131 static const struct usb_cdc_header_descriptor eth_header_desc = { 132 .bLength = sizeof(eth_header_desc), 133 .bDescriptorType = UDESC_CS_INTERFACE, 134 .bDescriptorSubtype = UDESCSUB_CDC_HEADER, 135 .bcdCDC[0] = 0x10, 136 .bcdCDC[1] = 0x01, 137 }; 138 139 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = { 140 .bLength = sizeof(eth_enf_desc), 141 .bDescriptorType = UDESC_CS_INTERFACE, 142 .bDescriptorSubtype = UDESCSUB_CDC_ENF, 143 .iMacAddress = STRING_ETH_MAC_INDEX, 144 .bmEthernetStatistics = {0, 0, 0, 0}, 145 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */ 146 .wNumberMCFilters = {0, 0}, 147 .bNumberPowerFilters = 0, 148 }; 149 150 static const void *eth_control_if_desc[] = { 151 ð_union_desc, 152 ð_header_desc, 153 ð_enf_desc, 154 NULL, 155 }; 156 157 static const struct usb_temp_packet_size bulk_mps = { 158 .mps[USB_SPEED_FULL] = 64, 159 .mps[USB_SPEED_HIGH] = 512, 160 }; 161 162 static const struct usb_temp_packet_size intr_mps = { 163 .mps[USB_SPEED_FULL] = 8, 164 .mps[USB_SPEED_HIGH] = 8, 165 }; 166 167 static const struct usb_temp_endpoint_desc bulk_in_ep = { 168 .pPacketSize = &bulk_mps, 169 #ifdef USB_HIP_IN_EP_0 170 .bEndpointAddress = USB_HIP_IN_EP_0, 171 #else 172 .bEndpointAddress = UE_DIR_IN, 173 #endif 174 .bmAttributes = UE_BULK, 175 }; 176 177 static const struct usb_temp_endpoint_desc bulk_out_ep = { 178 .pPacketSize = &bulk_mps, 179 #ifdef USB_HIP_OUT_EP_0 180 .bEndpointAddress = USB_HIP_OUT_EP_0, 181 #else 182 .bEndpointAddress = UE_DIR_OUT, 183 #endif 184 .bmAttributes = UE_BULK, 185 }; 186 187 static const struct usb_temp_endpoint_desc intr_in_ep = { 188 .pPacketSize = &intr_mps, 189 .bEndpointAddress = UE_DIR_IN, 190 .bmAttributes = UE_INTERRUPT, 191 }; 192 193 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = { 194 &intr_in_ep, 195 NULL, 196 }; 197 198 static const struct usb_temp_interface_desc eth_control_interface = { 199 .ppEndpoints = eth_intr_endpoints, 200 .ppRawDesc = eth_control_if_desc, 201 .bInterfaceClass = UICLASS_CDC, 202 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 203 .bInterfaceProtocol = 0, 204 .iInterface = STRING_ETH_CONTROL_INDEX, 205 }; 206 207 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = { 208 &bulk_in_ep, 209 &bulk_out_ep, 210 NULL, 211 }; 212 213 static const struct usb_temp_interface_desc eth_data_null_interface = { 214 .ppEndpoints = NULL, /* no endpoints */ 215 .bInterfaceClass = UICLASS_CDC_DATA, 216 .bInterfaceSubClass = 0, 217 .bInterfaceProtocol = 0, 218 .iInterface = STRING_ETH_DATA_INDEX, 219 }; 220 221 static const struct usb_temp_interface_desc eth_data_interface = { 222 .ppEndpoints = eth_data_endpoints, 223 .bInterfaceClass = UICLASS_CDC_DATA, 224 .bInterfaceSubClass = UISUBCLASS_DATA, 225 .bInterfaceProtocol = 0, 226 .iInterface = STRING_ETH_DATA_INDEX, 227 .isAltInterface = 1, /* this is an alternate setting */ 228 }; 229 230 static const struct usb_temp_packet_size modem_bulk_mps = { 231 .mps[USB_SPEED_LOW] = 8, 232 .mps[USB_SPEED_FULL] = 64, 233 .mps[USB_SPEED_HIGH] = 512, 234 }; 235 236 static const struct usb_temp_packet_size modem_intr_mps = { 237 .mps[USB_SPEED_LOW] = 8, 238 .mps[USB_SPEED_FULL] = 8, 239 .mps[USB_SPEED_HIGH] = 8, 240 }; 241 242 static const struct usb_temp_interval modem_intr_interval = { 243 .bInterval[USB_SPEED_LOW] = 8, /* 8ms */ 244 .bInterval[USB_SPEED_FULL] = 8, /* 8ms */ 245 .bInterval[USB_SPEED_HIGH] = 7, /* 8ms */ 246 }; 247 248 static const struct usb_temp_endpoint_desc modem_ep_0 = { 249 .pPacketSize = &modem_intr_mps, 250 .pIntervals = &modem_intr_interval, 251 .bEndpointAddress = UE_DIR_IN, 252 .bmAttributes = UE_INTERRUPT, 253 }; 254 255 static const struct usb_temp_endpoint_desc modem_ep_1 = { 256 .pPacketSize = &modem_bulk_mps, 257 .bEndpointAddress = UE_DIR_OUT, 258 .bmAttributes = UE_BULK, 259 }; 260 261 static const struct usb_temp_endpoint_desc modem_ep_2 = { 262 .pPacketSize = &modem_bulk_mps, 263 .bEndpointAddress = UE_DIR_IN, 264 .bmAttributes = UE_BULK, 265 }; 266 267 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = { 268 &modem_ep_0, 269 NULL, 270 }; 271 272 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = { 273 &modem_ep_1, 274 &modem_ep_2, 275 NULL, 276 }; 277 278 static const uint8_t modem_raw_desc_0[] = { 279 0x05, 0x24, 0x00, 0x10, 0x01 280 }; 281 282 static const uint8_t modem_raw_desc_1[] = { 283 0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1 284 }; 285 286 static const uint8_t modem_raw_desc_2[] = { 287 0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1 288 }; 289 290 static const uint8_t modem_raw_desc_3[] = { 291 0x04, 0x24, 0x02, 0x07 292 }; 293 294 static const void *modem_iface_0_desc[] = { 295 &modem_raw_desc_0, 296 &modem_raw_desc_1, 297 &modem_raw_desc_2, 298 &modem_raw_desc_3, 299 NULL, 300 }; 301 302 static const struct usb_temp_interface_desc modem_iface_0 = { 303 .ppRawDesc = modem_iface_0_desc, 304 .ppEndpoints = modem_iface_0_ep, 305 .bInterfaceClass = 2, 306 .bInterfaceSubClass = 2, 307 .bInterfaceProtocol = 1, 308 .iInterface = STRING_MODEM_INDEX, 309 }; 310 311 static const struct usb_temp_interface_desc modem_iface_1 = { 312 .ppEndpoints = modem_iface_1_ep, 313 .bInterfaceClass = 10, 314 .bInterfaceSubClass = 0, 315 .bInterfaceProtocol = 0, 316 .iInterface = STRING_MODEM_INDEX, 317 }; 318 319 static const struct usb_temp_interface_desc *serialnet_interfaces[] = { 320 &modem_iface_0, 321 &modem_iface_1, 322 ð_control_interface, 323 ð_data_null_interface, 324 ð_data_interface, 325 NULL, 326 }; 327 328 static const struct usb_temp_config_desc serialnet_config_desc = { 329 .ppIfaceDesc = serialnet_interfaces, 330 .bmAttributes = UC_BUS_POWERED, 331 .bMaxPower = 25, /* 50 mA */ 332 .iConfiguration = STRING_CONFIG_INDEX, 333 }; 334 static const struct usb_temp_config_desc *serialnet_configs[] = { 335 &serialnet_config_desc, 336 NULL, 337 }; 338 339 const struct usb_temp_device_desc usb_template_serialnet = { 340 .getStringDesc = &serialnet_get_string_desc, 341 .ppConfigDesc = serialnet_configs, 342 .idVendor = USB_TEMPLATE_VENDOR, 343 .idProduct = 0x0001, 344 .bcdDevice = 0x0100, 345 .bDeviceClass = UDCLASS_COMM, 346 .bDeviceSubClass = 0, 347 .bDeviceProtocol = 0, 348 .iManufacturer = STRING_VENDOR_INDEX, 349 .iProduct = STRING_PRODUCT_INDEX, 350 .iSerialNumber = STRING_SERIAL_INDEX, 351 }; 352 353 /*------------------------------------------------------------------------* 354 * serialnet_get_string_desc 355 * 356 * Return values: 357 * NULL: Failure. No such string. 358 * Else: Success. Pointer to string descriptor is returned. 359 *------------------------------------------------------------------------*/ 360 static const void * 361 serialnet_get_string_desc(uint16_t lang_id, uint8_t string_index) 362 { 363 static const void *ptr[STRING_MAX] = { 364 [STRING_LANG_INDEX] = &usb_string_lang_en, 365 [STRING_MODEM_INDEX] = &string_modem, 366 [STRING_ETH_MAC_INDEX] = &string_eth_mac, 367 [STRING_ETH_CONTROL_INDEX] = &string_eth_control, 368 [STRING_ETH_DATA_INDEX] = &string_eth_data, 369 [STRING_CONFIG_INDEX] = &string_serialnet_config, 370 [STRING_VENDOR_INDEX] = &string_serialnet_vendor, 371 [STRING_PRODUCT_INDEX] = &string_serialnet_product, 372 [STRING_SERIAL_INDEX] = &string_serialnet_serial, 373 }; 374 375 if (string_index == 0) { 376 return (&usb_string_lang_en); 377 } 378 if (lang_id != 0x0409) { 379 return (NULL); 380 } 381 if (string_index < STRING_MAX) { 382 return (ptr[string_index]); 383 } 384 return (NULL); 385 } 386