1 /* $FreeBSD: head/sys/dev/usb/template/usb_template_cdce.c 246125 2013-01-30 16:05:54Z hselasky $ */ 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/param.h> 37 #include <sys/queue.h> 38 #include <sys/types.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/bus.h> 42 #include <sys/module.h> 43 #include <sys/lock.h> 44 #include <sys/condvar.h> 45 #include <sys/sysctl.h> 46 #include <sys/unistd.h> 47 #include <sys/callout.h> 48 #include <sys/malloc.h> 49 #include <sys/caps.h> 50 51 #include <bus/u4b/usb.h> 52 #include <bus/u4b/usbdi.h> 53 #include <bus/u4b/usb_core.h> 54 #include <bus/u4b/usb_cdc.h> 55 56 #include <bus/u4b/template/usb_template.h> 57 #endif /* USB_GLOBAL_INCLUDE_FILE */ 58 59 enum { 60 STRING_LANG_INDEX, 61 STRING_MAC_INDEX, 62 STRING_ETH_CONTROL_INDEX, 63 STRING_ETH_DATA_INDEX, 64 STRING_ETH_CONFIG_INDEX, 65 STRING_ETH_VENDOR_INDEX, 66 STRING_ETH_PRODUCT_INDEX, 67 STRING_ETH_SERIAL_INDEX, 68 STRING_ETH_MAX, 69 }; 70 71 #define STRING_MAC \ 72 "2\0A\0002\0003\0004\0005\0006\0007\08\09\0A\0B" 73 74 #define STRING_ETH_CONTROL \ 75 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 " \ 76 "\0C\0o\0m\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e" 77 78 #define STRING_ETH_DATA \ 79 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0D\0a\0t\0a\0 " \ 80 "\0I\0n\0t\0e\0r\0f\0a\0c\0e" 81 82 #define STRING_ETH_CONFIG \ 83 "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g" 84 85 #define STRING_ETH_VENDOR \ 86 "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n" 87 88 #define STRING_ETH_PRODUCT \ 89 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0A\0d\0a\0p\0t\0e\0r" 90 91 #define STRING_ETH_SERIAL \ 92 "D\0e\0c\0e\0m\0b\0e\0r\0 \0002\0000\0000\0007" 93 94 /* make the real string descriptors */ 95 96 USB_MAKE_STRING_DESC(STRING_MAC, string_mac); 97 USB_MAKE_STRING_DESC(STRING_ETH_CONTROL, string_eth_control); 98 USB_MAKE_STRING_DESC(STRING_ETH_DATA, string_eth_data); 99 USB_MAKE_STRING_DESC(STRING_ETH_CONFIG, string_eth_config); 100 USB_MAKE_STRING_DESC(STRING_ETH_VENDOR, string_eth_vendor); 101 USB_MAKE_STRING_DESC(STRING_ETH_PRODUCT, string_eth_product); 102 USB_MAKE_STRING_DESC(STRING_ETH_SERIAL, string_eth_serial); 103 104 /* prototypes */ 105 106 static usb_temp_get_string_desc_t eth_get_string_desc; 107 108 static const struct usb_cdc_union_descriptor eth_union_desc = { 109 .bLength = sizeof(eth_union_desc), 110 .bDescriptorType = UDESC_CS_INTERFACE, 111 .bDescriptorSubtype = UDESCSUB_CDC_UNION, 112 .bMasterInterface = 0, /* this is automatically updated */ 113 .bSlaveInterface[0] = 1, /* this is automatically updated */ 114 }; 115 116 static const struct usb_cdc_header_descriptor eth_header_desc = { 117 .bLength = sizeof(eth_header_desc), 118 .bDescriptorType = UDESC_CS_INTERFACE, 119 .bDescriptorSubtype = UDESCSUB_CDC_HEADER, 120 .bcdCDC[0] = 0x10, 121 .bcdCDC[1] = 0x01, 122 }; 123 124 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = { 125 .bLength = sizeof(eth_enf_desc), 126 .bDescriptorType = UDESC_CS_INTERFACE, 127 .bDescriptorSubtype = UDESCSUB_CDC_ENF, 128 .iMacAddress = STRING_MAC_INDEX, 129 .bmEthernetStatistics = {0, 0, 0, 0}, 130 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */ 131 .wNumberMCFilters = {0, 0}, 132 .bNumberPowerFilters = 0, 133 }; 134 135 static const void *eth_control_if_desc[] = { 136 ð_union_desc, 137 ð_header_desc, 138 ð_enf_desc, 139 NULL, 140 }; 141 142 static const struct usb_temp_packet_size bulk_mps = { 143 .mps[USB_SPEED_FULL] = 64, 144 .mps[USB_SPEED_HIGH] = 512, 145 }; 146 147 static const struct usb_temp_packet_size intr_mps = { 148 .mps[USB_SPEED_FULL] = 8, 149 .mps[USB_SPEED_HIGH] = 8, 150 }; 151 152 static const struct usb_temp_endpoint_desc bulk_in_ep = { 153 .pPacketSize = &bulk_mps, 154 #ifdef USB_HIP_IN_EP_0 155 .bEndpointAddress = USB_HIP_IN_EP_0, 156 #else 157 .bEndpointAddress = UE_DIR_IN, 158 #endif 159 .bmAttributes = UE_BULK, 160 }; 161 162 static const struct usb_temp_endpoint_desc bulk_out_ep = { 163 .pPacketSize = &bulk_mps, 164 #ifdef USB_HIP_OUT_EP_0 165 .bEndpointAddress = USB_HIP_OUT_EP_0, 166 #else 167 .bEndpointAddress = UE_DIR_OUT, 168 #endif 169 .bmAttributes = UE_BULK, 170 }; 171 172 static const struct usb_temp_endpoint_desc intr_in_ep = { 173 .pPacketSize = &intr_mps, 174 .bEndpointAddress = UE_DIR_IN, 175 .bmAttributes = UE_INTERRUPT, 176 }; 177 178 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = { 179 &intr_in_ep, 180 NULL, 181 }; 182 183 static const struct usb_temp_interface_desc eth_control_interface = { 184 .ppEndpoints = eth_intr_endpoints, 185 .ppRawDesc = eth_control_if_desc, 186 .bInterfaceClass = UICLASS_CDC, 187 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 188 .bInterfaceProtocol = 0, 189 .iInterface = STRING_ETH_CONTROL_INDEX, 190 }; 191 192 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = { 193 &bulk_in_ep, 194 &bulk_out_ep, 195 NULL, 196 }; 197 198 static const struct usb_temp_interface_desc eth_data_null_interface = { 199 .ppEndpoints = NULL, /* no endpoints */ 200 .bInterfaceClass = UICLASS_CDC_DATA, 201 .bInterfaceSubClass = 0, 202 .bInterfaceProtocol = 0, 203 .iInterface = STRING_ETH_DATA_INDEX, 204 }; 205 206 static const struct usb_temp_interface_desc eth_data_interface = { 207 .ppEndpoints = eth_data_endpoints, 208 .bInterfaceClass = UICLASS_CDC_DATA, 209 .bInterfaceSubClass = UISUBCLASS_DATA, 210 .bInterfaceProtocol = 0, 211 .iInterface = STRING_ETH_DATA_INDEX, 212 .isAltInterface = 1, /* this is an alternate setting */ 213 }; 214 215 static const struct usb_temp_interface_desc *eth_interfaces[] = { 216 ð_control_interface, 217 ð_data_null_interface, 218 ð_data_interface, 219 NULL, 220 }; 221 222 static const struct usb_temp_config_desc eth_config_desc = { 223 .ppIfaceDesc = eth_interfaces, 224 .bmAttributes = UC_BUS_POWERED, 225 .bMaxPower = 25, /* 50 mA */ 226 .iConfiguration = STRING_ETH_CONFIG_INDEX, 227 }; 228 229 static const struct usb_temp_config_desc *eth_configs[] = { 230 ð_config_desc, 231 NULL, 232 }; 233 234 const struct usb_temp_device_desc usb_template_cdce = { 235 .getStringDesc = ð_get_string_desc, 236 .ppConfigDesc = eth_configs, 237 .idVendor = USB_TEMPLATE_VENDOR, 238 .idProduct = 0x0001, 239 .bcdDevice = 0x0100, 240 .bDeviceClass = UDCLASS_COMM, 241 .bDeviceSubClass = 0, 242 .bDeviceProtocol = 0, 243 .iManufacturer = STRING_ETH_VENDOR_INDEX, 244 .iProduct = STRING_ETH_PRODUCT_INDEX, 245 .iSerialNumber = STRING_ETH_SERIAL_INDEX, 246 }; 247 248 /*------------------------------------------------------------------------* 249 * eth_get_string_desc 250 * 251 * Return values: 252 * NULL: Failure. No such string. 253 * Else: Success. Pointer to string descriptor is returned. 254 *------------------------------------------------------------------------*/ 255 static const void * 256 eth_get_string_desc(uint16_t lang_id, uint8_t string_index) 257 { 258 static const void *ptr[STRING_ETH_MAX] = { 259 [STRING_LANG_INDEX] = &usb_string_lang_en, 260 [STRING_MAC_INDEX] = &string_mac, 261 [STRING_ETH_CONTROL_INDEX] = &string_eth_control, 262 [STRING_ETH_DATA_INDEX] = &string_eth_data, 263 [STRING_ETH_CONFIG_INDEX] = &string_eth_config, 264 [STRING_ETH_VENDOR_INDEX] = &string_eth_vendor, 265 [STRING_ETH_PRODUCT_INDEX] = &string_eth_product, 266 [STRING_ETH_SERIAL_INDEX] = &string_eth_serial, 267 }; 268 269 if (string_index == 0) { 270 return (&usb_string_lang_en); 271 } 272 if (lang_id != 0x0409) { 273 return (NULL); 274 } 275 if (string_index < STRING_ETH_MAX) { 276 return (ptr[string_index]); 277 } 278 return (NULL); 279 } 280