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/module.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usb_cdc.h>
57 #include <dev/usb/template/usb_template.h>
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,
72 0x09, 0x04, /* American English */
75 '2', 0, 'A', 0, '2', 0, '3', 0, \
76 '4', 0, '5', 0, '6', 0, '7', 0, \
77 '8', 0, '9', 0, 'A', 0, 'B', 0,
79 #define STRING_ETH_CONTROL \
80 'U', 0, 'S', 0, 'B', 0, ' ', 0, \
81 'E', 0, 't', 0, 'h', 0, 'e', 0, \
82 'r', 0, 'n', 0, 'e', 0, 't', 0, \
83 ' ', 0, 'C', 0, 'o', 0, 'm', 0, \
84 'm', 0, ' ', 0, 'i', 0, 'n', 0, \
85 't', 0, 'e', 0, 'r', 0, 'f', 0, \
86 'a', 0, 'c', 0, 'e', 0,
88 #define STRING_ETH_DATA \
89 'U', 0, 'S', 0, 'B', 0, ' ', 0, \
90 'E', 0, 't', 0, 'h', 0, 'e', 0, \
91 'r', 0, 'n', 0, 'e', 0, 't', 0, \
92 ' ', 0, 'D', 0, 'a', 0, 't', 0, \
93 'a', 0, ' ', 0, 'i', 0, 'n', 0, \
94 't', 0, 'e', 0, 'r', 0, 'f', 0, \
95 'a', 0, 'c', 0, 'e', 0,
97 #define STRING_ETH_CONFIG \
98 'D', 0, 'e', 0, 'f', 0, 'a', 0, \
99 'u', 0, 'l', 0, 't', 0, ' ', 0, \
100 'c', 0, 'o', 0, 'n', 0, 'f', 0, \
103 #define STRING_ETH_VENDOR \
104 'F', 0, 'r', 0, 'e', 0, 'e', 0, \
105 'B', 0, 'S', 0, 'D', 0, ' ', 0, \
106 'f', 0, 'o', 0, 'u', 0, 'n', 0, \
107 'd', 0, 'a', 0, 't', 0, 'i', 0, \
110 #define STRING_ETH_PRODUCT \
111 'U', 0, 'S', 0, 'B', 0, ' ', 0, \
112 'E', 0, 't', 0, 'h', 0, 'e', 0, \
113 'r', 0, 'n', 0, 'e', 0, 't', 0, \
114 ' ', 0, 'A', 0, 'd', 0, 'a', 0, \
115 'p', 0, 't', 0, 'e', 0, 'r', 0,
117 #define STRING_ETH_SERIAL \
118 'D', 0, 'e', 0, 'c', 0, 'e', 0, \
119 'm', 0, 'b', 0, 'e', 0, 'r', 0, \
120 ' ', 0, '2', 0, '0', 0, '0', 0, \
123 /* make the real string descriptors */
125 USB_MAKE_STRING_DESC(STRING_LANG, string_lang);
126 USB_MAKE_STRING_DESC(STRING_MAC, string_mac);
127 USB_MAKE_STRING_DESC(STRING_ETH_CONTROL, string_eth_control);
128 USB_MAKE_STRING_DESC(STRING_ETH_DATA, string_eth_data);
129 USB_MAKE_STRING_DESC(STRING_ETH_CONFIG, string_eth_config);
130 USB_MAKE_STRING_DESC(STRING_ETH_VENDOR, string_eth_vendor);
131 USB_MAKE_STRING_DESC(STRING_ETH_PRODUCT, string_eth_product);
132 USB_MAKE_STRING_DESC(STRING_ETH_SERIAL, string_eth_serial);
136 static usb_temp_get_string_desc_t eth_get_string_desc;
138 static const struct usb_cdc_union_descriptor eth_union_desc = {
139 .bLength = sizeof(eth_union_desc),
140 .bDescriptorType = UDESC_CS_INTERFACE,
141 .bDescriptorSubtype = UDESCSUB_CDC_UNION,
142 .bMasterInterface = 0, /* this is automatically updated */
143 .bSlaveInterface[0] = 1, /* this is automatically updated */
146 static const struct usb_cdc_header_descriptor eth_header_desc = {
147 .bLength = sizeof(eth_header_desc),
148 .bDescriptorType = UDESC_CS_INTERFACE,
149 .bDescriptorSubtype = UDESCSUB_CDC_HEADER,
154 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
155 .bLength = sizeof(eth_enf_desc),
156 .bDescriptorType = UDESC_CS_INTERFACE,
157 .bDescriptorSubtype = UDESCSUB_CDC_ENF,
158 .iMacAddress = STRING_MAC_INDEX,
159 .bmEthernetStatistics = {0, 0, 0, 0},
160 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
161 .wNumberMCFilters = {0, 0},
162 .bNumberPowerFilters = 0,
165 static const void *eth_control_if_desc[] = {
172 static const struct usb_temp_packet_size bulk_mps = {
173 .mps[USB_SPEED_FULL] = 64,
174 .mps[USB_SPEED_HIGH] = 512,
177 static const struct usb_temp_packet_size intr_mps = {
178 .mps[USB_SPEED_FULL] = 8,
179 .mps[USB_SPEED_HIGH] = 8,
182 static const struct usb_temp_endpoint_desc bulk_in_ep = {
183 .pPacketSize = &bulk_mps,
184 #ifdef USB_HIP_IN_EP_0
185 .bEndpointAddress = USB_HIP_IN_EP_0,
187 .bEndpointAddress = UE_DIR_IN,
189 .bmAttributes = UE_BULK,
192 static const struct usb_temp_endpoint_desc bulk_out_ep = {
193 .pPacketSize = &bulk_mps,
194 #ifdef USB_HIP_OUT_EP_0
195 .bEndpointAddress = USB_HIP_OUT_EP_0,
197 .bEndpointAddress = UE_DIR_OUT,
199 .bmAttributes = UE_BULK,
202 static const struct usb_temp_endpoint_desc intr_in_ep = {
203 .pPacketSize = &intr_mps,
204 .bEndpointAddress = UE_DIR_IN,
205 .bmAttributes = UE_INTERRUPT,
208 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
213 static const struct usb_temp_interface_desc eth_control_interface = {
214 .ppEndpoints = eth_intr_endpoints,
215 .ppRawDesc = eth_control_if_desc,
216 .bInterfaceClass = UICLASS_CDC,
217 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
218 .bInterfaceProtocol = 0,
219 .iInterface = STRING_ETH_CONTROL_INDEX,
222 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
228 static const struct usb_temp_interface_desc eth_data_null_interface = {
229 .ppEndpoints = NULL, /* no endpoints */
230 .bInterfaceClass = UICLASS_CDC_DATA,
231 .bInterfaceSubClass = 0,
232 .bInterfaceProtocol = 0,
233 .iInterface = STRING_ETH_DATA_INDEX,
236 static const struct usb_temp_interface_desc eth_data_interface = {
237 .ppEndpoints = eth_data_endpoints,
238 .bInterfaceClass = UICLASS_CDC_DATA,
239 .bInterfaceSubClass = UISUBCLASS_DATA,
240 .bInterfaceProtocol = 0,
241 .iInterface = STRING_ETH_DATA_INDEX,
242 .isAltInterface = 1, /* this is an alternate setting */
245 static const struct usb_temp_interface_desc *eth_interfaces[] = {
246 ð_control_interface,
247 ð_data_null_interface,
252 static const struct usb_temp_config_desc eth_config_desc = {
253 .ppIfaceDesc = eth_interfaces,
254 .bmAttributes = UC_BUS_POWERED,
255 .bMaxPower = 25, /* 50 mA */
256 .iConfiguration = STRING_ETH_CONFIG_INDEX,
259 static const struct usb_temp_config_desc *eth_configs[] = {
264 const struct usb_temp_device_desc usb_template_cdce = {
265 .getStringDesc = ð_get_string_desc,
266 .ppConfigDesc = eth_configs,
267 .idVendor = USB_TEMPLATE_VENDOR,
270 .bDeviceClass = UDCLASS_COMM,
271 .bDeviceSubClass = 0,
272 .bDeviceProtocol = 0,
273 .iManufacturer = STRING_ETH_VENDOR_INDEX,
274 .iProduct = STRING_ETH_PRODUCT_INDEX,
275 .iSerialNumber = STRING_ETH_SERIAL_INDEX,
278 /*------------------------------------------------------------------------*
279 * eth_get_string_desc
282 * NULL: Failure. No such string.
283 * Else: Success. Pointer to string descriptor is returned.
284 *------------------------------------------------------------------------*/
286 eth_get_string_desc(uint16_t lang_id, uint8_t string_index)
288 static const void *ptr[STRING_ETH_MAX] = {
289 [STRING_LANG_INDEX] = &string_lang,
290 [STRING_MAC_INDEX] = &string_mac,
291 [STRING_ETH_CONTROL_INDEX] = &string_eth_control,
292 [STRING_ETH_DATA_INDEX] = &string_eth_data,
293 [STRING_ETH_CONFIG_INDEX] = &string_eth_config,
294 [STRING_ETH_VENDOR_INDEX] = &string_eth_vendor,
295 [STRING_ETH_PRODUCT_INDEX] = &string_eth_product,
296 [STRING_ETH_SERIAL_INDEX] = &string_eth_serial,
299 if (string_index == 0) {
300 return (&string_lang);
302 if (lang_id != 0x0409) {
305 if (string_index < STRING_ETH_MAX) {
306 return (ptr[string_index]);