3 * Copyright (c) 2007 Hans Petter Selasky <hselasky@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 * This file contains the USB templates for a CDC USB ethernet device.
32 #ifdef USB_GLOBAL_INCLUDE_FILE
33 #include USB_GLOBAL_INCLUDE_FILE
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>
43 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
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>
59 #include <dev/usb/template/usb_template.h>
60 #endif /* USB_GLOBAL_INCLUDE_FILE */
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,
75 "2\0A\0002\0003\0004\0005\0006\0007\08\09\0A\0B"
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"
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"
85 #define STRING_ETH_CONFIG \
86 "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g"
88 #define STRING_ETH_VENDOR \
89 "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n"
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"
94 #define STRING_ETH_SERIAL \
95 "D\0e\0c\0e\0m\0b\0e\0r\0 \0002\0000\0000\0007"
97 /* make the real string descriptors */
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);
109 static usb_temp_get_string_desc_t eth_get_string_desc;
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 */
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,
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,
138 static const void *eth_control_if_desc[] = {
145 static const struct usb_temp_packet_size bulk_mps = {
146 .mps[USB_SPEED_FULL] = 64,
147 .mps[USB_SPEED_HIGH] = 512,
150 static const struct usb_temp_packet_size intr_mps = {
151 .mps[USB_SPEED_FULL] = 8,
152 .mps[USB_SPEED_HIGH] = 8,
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,
160 .bEndpointAddress = UE_DIR_IN,
162 .bmAttributes = UE_BULK,
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,
170 .bEndpointAddress = UE_DIR_OUT,
172 .bmAttributes = UE_BULK,
175 static const struct usb_temp_endpoint_desc intr_in_ep = {
176 .pPacketSize = &intr_mps,
177 .bEndpointAddress = UE_DIR_IN,
178 .bmAttributes = UE_INTERRUPT,
181 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
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,
195 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
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,
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 */
218 static const struct usb_temp_interface_desc *eth_interfaces[] = {
219 ð_control_interface,
220 ð_data_null_interface,
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,
232 static const struct usb_temp_config_desc *eth_configs[] = {
237 const struct usb_temp_device_desc usb_template_cdce = {
238 .getStringDesc = ð_get_string_desc,
239 .ppConfigDesc = eth_configs,
240 .idVendor = USB_TEMPLATE_VENDOR,
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,
251 /*------------------------------------------------------------------------*
252 * eth_get_string_desc
255 * NULL: Failure. No such string.
256 * Else: Success. Pointer to string descriptor is returned.
257 *------------------------------------------------------------------------*/
259 eth_get_string_desc(uint16_t lang_id, uint8_t string_index)
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,
272 if (string_index == 0) {
273 return (&usb_string_lang_en);
275 if (lang_id != 0x0409) {
278 if (string_index < STRING_ETH_MAX) {
279 return (ptr[string_index]);