1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2007 Hans Petter Selasky <hselasky@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * This file contains the USB templates for a CDC USB ethernet device. 30 */ 31 32 #ifdef USB_GLOBAL_INCLUDE_FILE 33 #include USB_GLOBAL_INCLUDE_FILE 34 #else 35 #include <sys/stdint.h> 36 #include <sys/stddef.h> 37 #include <sys/param.h> 38 #include <sys/queue.h> 39 #include <sys/types.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/bus.h> 43 #include <sys/module.h> 44 #include <sys/lock.h> 45 #include <sys/mutex.h> 46 #include <sys/condvar.h> 47 #include <sys/sysctl.h> 48 #include <sys/sx.h> 49 #include <sys/unistd.h> 50 #include <sys/callout.h> 51 #include <sys/malloc.h> 52 #include <sys/priv.h> 53 54 #include <dev/usb/usb.h> 55 #include <dev/usb/usbdi.h> 56 #include <dev/usb/usb_core.h> 57 #include <dev/usb/usb_cdc.h> 58 59 #include <dev/usb/template/usb_template.h> 60 #endif /* USB_GLOBAL_INCLUDE_FILE */ 61 62 enum { 63 STRING_LANG_INDEX, 64 STRING_MAC_INDEX, 65 STRING_ETH_CONTROL_INDEX, 66 STRING_ETH_DATA_INDEX, 67 STRING_ETH_CONFIG_INDEX, 68 STRING_ETH_VENDOR_INDEX, 69 STRING_ETH_PRODUCT_INDEX, 70 STRING_ETH_SERIAL_INDEX, 71 STRING_ETH_MAX, 72 }; 73 74 #define STRING_MAC \ 75 "2\0A\0002\0003\0004\0005\0006\0007\08\09\0A\0B" 76 77 #define STRING_ETH_CONTROL \ 78 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 " \ 79 "\0C\0o\0m\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e" 80 81 #define STRING_ETH_DATA \ 82 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0D\0a\0t\0a\0 " \ 83 "\0I\0n\0t\0e\0r\0f\0a\0c\0e" 84 85 #define STRING_ETH_CONFIG \ 86 "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g" 87 88 #define STRING_ETH_VENDOR \ 89 "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n" 90 91 #define STRING_ETH_PRODUCT \ 92 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0A\0d\0a\0p\0t\0e\0r" 93 94 #define STRING_ETH_SERIAL \ 95 "D\0e\0c\0e\0m\0b\0e\0r\0 \0002\0000\0000\0007" 96 97 /* make the real string descriptors */ 98 99 USB_MAKE_STRING_DESC(STRING_MAC, string_mac); 100 USB_MAKE_STRING_DESC(STRING_ETH_CONTROL, string_eth_control); 101 USB_MAKE_STRING_DESC(STRING_ETH_DATA, string_eth_data); 102 USB_MAKE_STRING_DESC(STRING_ETH_CONFIG, string_eth_config); 103 USB_MAKE_STRING_DESC(STRING_ETH_VENDOR, string_eth_vendor); 104 USB_MAKE_STRING_DESC(STRING_ETH_PRODUCT, string_eth_product); 105 USB_MAKE_STRING_DESC(STRING_ETH_SERIAL, string_eth_serial); 106 107 /* prototypes */ 108 109 static usb_temp_get_string_desc_t eth_get_string_desc; 110 111 static const struct usb_cdc_union_descriptor eth_union_desc = { 112 .bLength = sizeof(eth_union_desc), 113 .bDescriptorType = UDESC_CS_INTERFACE, 114 .bDescriptorSubtype = UDESCSUB_CDC_UNION, 115 .bMasterInterface = 0, /* this is automatically updated */ 116 .bSlaveInterface[0] = 1, /* this is automatically updated */ 117 }; 118 119 static const struct usb_cdc_header_descriptor eth_header_desc = { 120 .bLength = sizeof(eth_header_desc), 121 .bDescriptorType = UDESC_CS_INTERFACE, 122 .bDescriptorSubtype = UDESCSUB_CDC_HEADER, 123 .bcdCDC[0] = 0x10, 124 .bcdCDC[1] = 0x01, 125 }; 126 127 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = { 128 .bLength = sizeof(eth_enf_desc), 129 .bDescriptorType = UDESC_CS_INTERFACE, 130 .bDescriptorSubtype = UDESCSUB_CDC_ENF, 131 .iMacAddress = STRING_MAC_INDEX, 132 .bmEthernetStatistics = {0, 0, 0, 0}, 133 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */ 134 .wNumberMCFilters = {0, 0}, 135 .bNumberPowerFilters = 0, 136 }; 137 138 static const void *eth_control_if_desc[] = { 139 ð_union_desc, 140 ð_header_desc, 141 ð_enf_desc, 142 NULL, 143 }; 144 145 static const struct usb_temp_packet_size bulk_mps = { 146 .mps[USB_SPEED_FULL] = 64, 147 .mps[USB_SPEED_HIGH] = 512, 148 }; 149 150 static const struct usb_temp_packet_size intr_mps = { 151 .mps[USB_SPEED_FULL] = 8, 152 .mps[USB_SPEED_HIGH] = 8, 153 }; 154 155 static const struct usb_temp_endpoint_desc bulk_in_ep = { 156 .pPacketSize = &bulk_mps, 157 #ifdef USB_HIP_IN_EP_0 158 .bEndpointAddress = USB_HIP_IN_EP_0, 159 #else 160 .bEndpointAddress = UE_DIR_IN, 161 #endif 162 .bmAttributes = UE_BULK, 163 }; 164 165 static const struct usb_temp_endpoint_desc bulk_out_ep = { 166 .pPacketSize = &bulk_mps, 167 #ifdef USB_HIP_OUT_EP_0 168 .bEndpointAddress = USB_HIP_OUT_EP_0, 169 #else 170 .bEndpointAddress = UE_DIR_OUT, 171 #endif 172 .bmAttributes = UE_BULK, 173 }; 174 175 static const struct usb_temp_endpoint_desc intr_in_ep = { 176 .pPacketSize = &intr_mps, 177 .bEndpointAddress = UE_DIR_IN, 178 .bmAttributes = UE_INTERRUPT, 179 }; 180 181 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = { 182 &intr_in_ep, 183 NULL, 184 }; 185 186 static const struct usb_temp_interface_desc eth_control_interface = { 187 .ppEndpoints = eth_intr_endpoints, 188 .ppRawDesc = eth_control_if_desc, 189 .bInterfaceClass = UICLASS_CDC, 190 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 191 .bInterfaceProtocol = 0, 192 .iInterface = STRING_ETH_CONTROL_INDEX, 193 }; 194 195 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = { 196 &bulk_in_ep, 197 &bulk_out_ep, 198 NULL, 199 }; 200 201 static const struct usb_temp_interface_desc eth_data_null_interface = { 202 .ppEndpoints = NULL, /* no endpoints */ 203 .bInterfaceClass = UICLASS_CDC_DATA, 204 .bInterfaceSubClass = 0, 205 .bInterfaceProtocol = 0, 206 .iInterface = STRING_ETH_DATA_INDEX, 207 }; 208 209 static const struct usb_temp_interface_desc eth_data_interface = { 210 .ppEndpoints = eth_data_endpoints, 211 .bInterfaceClass = UICLASS_CDC_DATA, 212 .bInterfaceSubClass = UISUBCLASS_DATA, 213 .bInterfaceProtocol = 0, 214 .iInterface = STRING_ETH_DATA_INDEX, 215 .isAltInterface = 1, /* this is an alternate setting */ 216 }; 217 218 static const struct usb_temp_interface_desc *eth_interfaces[] = { 219 ð_control_interface, 220 ð_data_null_interface, 221 ð_data_interface, 222 NULL, 223 }; 224 225 static const struct usb_temp_config_desc eth_config_desc = { 226 .ppIfaceDesc = eth_interfaces, 227 .bmAttributes = UC_BUS_POWERED, 228 .bMaxPower = 25, /* 50 mA */ 229 .iConfiguration = STRING_ETH_CONFIG_INDEX, 230 }; 231 232 static const struct usb_temp_config_desc *eth_configs[] = { 233 ð_config_desc, 234 NULL, 235 }; 236 237 const struct usb_temp_device_desc usb_template_cdce = { 238 .getStringDesc = ð_get_string_desc, 239 .ppConfigDesc = eth_configs, 240 .idVendor = USB_TEMPLATE_VENDOR, 241 .idProduct = 0x0001, 242 .bcdDevice = 0x0100, 243 .bDeviceClass = UDCLASS_COMM, 244 .bDeviceSubClass = 0, 245 .bDeviceProtocol = 0, 246 .iManufacturer = STRING_ETH_VENDOR_INDEX, 247 .iProduct = STRING_ETH_PRODUCT_INDEX, 248 .iSerialNumber = STRING_ETH_SERIAL_INDEX, 249 }; 250 251 /*------------------------------------------------------------------------* 252 * eth_get_string_desc 253 * 254 * Return values: 255 * NULL: Failure. No such string. 256 * Else: Success. Pointer to string descriptor is returned. 257 *------------------------------------------------------------------------*/ 258 static const void * 259 eth_get_string_desc(uint16_t lang_id, uint8_t string_index) 260 { 261 static const void *ptr[STRING_ETH_MAX] = { 262 [STRING_LANG_INDEX] = &usb_string_lang_en, 263 [STRING_MAC_INDEX] = &string_mac, 264 [STRING_ETH_CONTROL_INDEX] = &string_eth_control, 265 [STRING_ETH_DATA_INDEX] = &string_eth_data, 266 [STRING_ETH_CONFIG_INDEX] = &string_eth_config, 267 [STRING_ETH_VENDOR_INDEX] = &string_eth_vendor, 268 [STRING_ETH_PRODUCT_INDEX] = &string_eth_product, 269 [STRING_ETH_SERIAL_INDEX] = &string_eth_serial, 270 }; 271 272 if (string_index == 0) { 273 return (&usb_string_lang_en); 274 } 275 if (lang_id != 0x0409) { 276 return (NULL); 277 } 278 if (string_index < STRING_ETH_MAX) { 279 return (ptr[string_index]); 280 } 281 return (NULL); 282 } 283