5 * Copyright (c) 2007 Hans Petter Selasky <hselasky@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * This file contains the USB templates for a CDC USB ethernet device.
34 #include <sys/stdint.h>
35 #include <sys/stddef.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>
42 #include <sys/linker_set.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_cdc.h>
58 #include <dev/usb/template/usb_template.h>
63 STRING_ETH_CONTROL_INDEX,
64 STRING_ETH_DATA_INDEX,
65 STRING_ETH_CONFIG_INDEX,
66 STRING_ETH_VENDOR_INDEX,
67 STRING_ETH_PRODUCT_INDEX,
68 STRING_ETH_SERIAL_INDEX,
73 0x09, 0x04, /* American English */
76 '2', 0, 'A', 0, '2', 0, '3', 0, \
77 '4', 0, '5', 0, '6', 0, '7', 0, \
78 '8', 0, '9', 0, 'A', 0, 'B', 0,
80 #define STRING_ETH_CONTROL \
81 'U', 0, 'S', 0, 'B', 0, ' ', 0, \
82 'E', 0, 't', 0, 'h', 0, 'e', 0, \
83 'r', 0, 'n', 0, 'e', 0, 't', 0, \
84 ' ', 0, 'C', 0, 'o', 0, 'm', 0, \
85 'm', 0, ' ', 0, 'i', 0, 'n', 0, \
86 't', 0, 'e', 0, 'r', 0, 'f', 0, \
87 'a', 0, 'c', 0, 'e', 0,
89 #define STRING_ETH_DATA \
90 'U', 0, 'S', 0, 'B', 0, ' ', 0, \
91 'E', 0, 't', 0, 'h', 0, 'e', 0, \
92 'r', 0, 'n', 0, 'e', 0, 't', 0, \
93 ' ', 0, 'D', 0, 'a', 0, 't', 0, \
94 'a', 0, ' ', 0, 'i', 0, 'n', 0, \
95 't', 0, 'e', 0, 'r', 0, 'f', 0, \
96 'a', 0, 'c', 0, 'e', 0,
98 #define STRING_ETH_CONFIG \
99 'D', 0, 'e', 0, 'f', 0, 'a', 0, \
100 'u', 0, 'l', 0, 't', 0, ' ', 0, \
101 'c', 0, 'o', 0, 'n', 0, 'f', 0, \
104 #define STRING_ETH_VENDOR \
105 'F', 0, 'r', 0, 'e', 0, 'e', 0, \
106 'B', 0, 'S', 0, 'D', 0, ' ', 0, \
107 'f', 0, 'o', 0, 'u', 0, 'n', 0, \
108 'd', 0, 'a', 0, 't', 0, 'i', 0, \
111 #define STRING_ETH_PRODUCT \
112 'U', 0, 'S', 0, 'B', 0, ' ', 0, \
113 'E', 0, 't', 0, 'h', 0, 'e', 0, \
114 'r', 0, 'n', 0, 'e', 0, 't', 0, \
115 ' ', 0, 'A', 0, 'd', 0, 'a', 0, \
116 'p', 0, 't', 0, 'e', 0, 'r', 0,
118 #define STRING_ETH_SERIAL \
119 'D', 0, 'e', 0, 'c', 0, 'e', 0, \
120 'm', 0, 'b', 0, 'e', 0, 'r', 0, \
121 ' ', 0, '2', 0, '0', 0, '0', 0, \
124 /* make the real string descriptors */
126 USB_MAKE_STRING_DESC(STRING_LANG, string_lang);
127 USB_MAKE_STRING_DESC(STRING_MAC, string_mac);
128 USB_MAKE_STRING_DESC(STRING_ETH_CONTROL, string_eth_control);
129 USB_MAKE_STRING_DESC(STRING_ETH_DATA, string_eth_data);
130 USB_MAKE_STRING_DESC(STRING_ETH_CONFIG, string_eth_config);
131 USB_MAKE_STRING_DESC(STRING_ETH_VENDOR, string_eth_vendor);
132 USB_MAKE_STRING_DESC(STRING_ETH_PRODUCT, string_eth_product);
133 USB_MAKE_STRING_DESC(STRING_ETH_SERIAL, string_eth_serial);
137 static usb_temp_get_string_desc_t eth_get_string_desc;
139 static const struct usb_cdc_union_descriptor eth_union_desc = {
140 .bLength = sizeof(eth_union_desc),
141 .bDescriptorType = UDESC_CS_INTERFACE,
142 .bDescriptorSubtype = UDESCSUB_CDC_UNION,
143 .bMasterInterface = 0, /* this is automatically updated */
144 .bSlaveInterface[0] = 1, /* this is automatically updated */
147 static const struct usb_cdc_header_descriptor eth_header_desc = {
148 .bLength = sizeof(eth_header_desc),
149 .bDescriptorType = UDESC_CS_INTERFACE,
150 .bDescriptorSubtype = UDESCSUB_CDC_HEADER,
155 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
156 .bLength = sizeof(eth_enf_desc),
157 .bDescriptorType = UDESC_CS_INTERFACE,
158 .bDescriptorSubtype = UDESCSUB_CDC_ENF,
159 .iMacAddress = STRING_MAC_INDEX,
160 .bmEthernetStatistics = {0, 0, 0, 0},
161 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
162 .wNumberMCFilters = {0, 0},
163 .bNumberPowerFilters = 0,
166 static const void *eth_control_if_desc[] = {
173 static const struct usb_temp_packet_size bulk_mps = {
174 .mps[USB_SPEED_FULL] = 64,
175 .mps[USB_SPEED_HIGH] = 512,
178 static const struct usb_temp_packet_size intr_mps = {
179 .mps[USB_SPEED_FULL] = 8,
180 .mps[USB_SPEED_HIGH] = 8,
183 static const struct usb_temp_endpoint_desc bulk_in_ep = {
184 .pPacketSize = &bulk_mps,
185 #ifdef USB_HIP_IN_EP_0
186 .bEndpointAddress = USB_HIP_IN_EP_0,
188 .bEndpointAddress = UE_DIR_IN,
190 .bmAttributes = UE_BULK,
193 static const struct usb_temp_endpoint_desc bulk_out_ep = {
194 .pPacketSize = &bulk_mps,
195 #ifdef USB_HIP_OUT_EP_0
196 .bEndpointAddress = USB_HIP_OUT_EP_0,
198 .bEndpointAddress = UE_DIR_OUT,
200 .bmAttributes = UE_BULK,
203 static const struct usb_temp_endpoint_desc intr_in_ep = {
204 .pPacketSize = &intr_mps,
205 .bEndpointAddress = UE_DIR_IN,
206 .bmAttributes = UE_INTERRUPT,
209 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
214 static const struct usb_temp_interface_desc eth_control_interface = {
215 .ppEndpoints = eth_intr_endpoints,
216 .ppRawDesc = eth_control_if_desc,
217 .bInterfaceClass = UICLASS_CDC,
218 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
219 .bInterfaceProtocol = 0,
220 .iInterface = STRING_ETH_CONTROL_INDEX,
223 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
229 static const struct usb_temp_interface_desc eth_data_null_interface = {
230 .ppEndpoints = NULL, /* no endpoints */
231 .bInterfaceClass = UICLASS_CDC_DATA,
232 .bInterfaceSubClass = 0,
233 .bInterfaceProtocol = 0,
234 .iInterface = STRING_ETH_DATA_INDEX,
237 static const struct usb_temp_interface_desc eth_data_interface = {
238 .ppEndpoints = eth_data_endpoints,
239 .bInterfaceClass = UICLASS_CDC_DATA,
240 .bInterfaceSubClass = UISUBCLASS_DATA,
241 .bInterfaceProtocol = 0,
242 .iInterface = STRING_ETH_DATA_INDEX,
243 .isAltInterface = 1, /* this is an alternate setting */
246 static const struct usb_temp_interface_desc *eth_interfaces[] = {
247 ð_control_interface,
248 ð_data_null_interface,
253 static const struct usb_temp_config_desc eth_config_desc = {
254 .ppIfaceDesc = eth_interfaces,
255 .bmAttributes = UC_BUS_POWERED,
256 .bMaxPower = 25, /* 50 mA */
257 .iConfiguration = STRING_ETH_CONFIG_INDEX,
260 static const struct usb_temp_config_desc *eth_configs[] = {
265 const struct usb_temp_device_desc usb_template_cdce = {
266 .getStringDesc = ð_get_string_desc,
267 .ppConfigDesc = eth_configs,
271 .bDeviceClass = UDCLASS_COMM,
272 .bDeviceSubClass = 0,
273 .bDeviceProtocol = 0,
274 .iManufacturer = STRING_ETH_VENDOR_INDEX,
275 .iProduct = STRING_ETH_PRODUCT_INDEX,
276 .iSerialNumber = STRING_ETH_SERIAL_INDEX,
279 /*------------------------------------------------------------------------*
280 * eth_get_string_desc
283 * NULL: Failure. No such string.
284 * Else: Success. Pointer to string descriptor is returned.
285 *------------------------------------------------------------------------*/
287 eth_get_string_desc(uint16_t lang_id, uint8_t string_index)
289 static const void *ptr[STRING_ETH_MAX] = {
290 [STRING_LANG_INDEX] = &string_lang,
291 [STRING_MAC_INDEX] = &string_mac,
292 [STRING_ETH_CONTROL_INDEX] = &string_eth_control,
293 [STRING_ETH_DATA_INDEX] = &string_eth_data,
294 [STRING_ETH_CONFIG_INDEX] = &string_eth_config,
295 [STRING_ETH_VENDOR_INDEX] = &string_eth_vendor,
296 [STRING_ETH_PRODUCT_INDEX] = &string_eth_product,
297 [STRING_ETH_SERIAL_INDEX] = &string_eth_serial,
300 if (string_index == 0) {
301 return (&string_lang);
303 if (lang_id != 0x0409) {
306 if (string_index < STRING_ETH_MAX) {
307 return (ptr[string_index]);