3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 #ifdef USB_GLOBAL_INCLUDE_FILE
35 #include USB_GLOBAL_INCLUDE_FILE
37 #include <sys/stdint.h>
38 #include <sys/stddef.h>
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/condvar.h>
49 #include <sys/sysctl.h>
51 #include <sys/unistd.h>
52 #include <sys/callout.h>
53 #include <sys/malloc.h>
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_cdc.h>
61 #include <dev/usb/template/usb_template.h>
62 #endif /* USB_GLOBAL_INCLUDE_FILE */
67 STRING_ETH_CONTROL_INDEX,
68 STRING_ETH_DATA_INDEX,
69 STRING_ETH_CONFIG_INDEX,
70 STRING_ETH_VENDOR_INDEX,
71 STRING_ETH_PRODUCT_INDEX,
72 STRING_ETH_SERIAL_INDEX,
77 "2\0A\0002\0003\0004\0005\0006\0007\08\09\0A\0B"
79 #define STRING_ETH_CONTROL \
80 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 " \
81 "\0C\0o\0m\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e"
83 #define STRING_ETH_DATA \
84 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0D\0a\0t\0a\0 " \
85 "\0I\0n\0t\0e\0r\0f\0a\0c\0e"
87 #define STRING_ETH_CONFIG \
88 "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g"
90 #define STRING_ETH_VENDOR \
91 "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n"
93 #define STRING_ETH_PRODUCT \
94 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0A\0d\0a\0p\0t\0e\0r"
96 #define STRING_ETH_SERIAL \
97 "D\0e\0c\0e\0m\0b\0e\0r\0 \0002\0000\0000\0007"
99 /* make the real string descriptors */
101 USB_MAKE_STRING_DESC(STRING_MAC, string_mac);
102 USB_MAKE_STRING_DESC(STRING_ETH_CONTROL, string_eth_control);
103 USB_MAKE_STRING_DESC(STRING_ETH_DATA, string_eth_data);
104 USB_MAKE_STRING_DESC(STRING_ETH_CONFIG, string_eth_config);
105 USB_MAKE_STRING_DESC(STRING_ETH_VENDOR, string_eth_vendor);
106 USB_MAKE_STRING_DESC(STRING_ETH_PRODUCT, string_eth_product);
107 USB_MAKE_STRING_DESC(STRING_ETH_SERIAL, string_eth_serial);
111 static usb_temp_get_string_desc_t eth_get_string_desc;
113 static const struct usb_cdc_union_descriptor eth_union_desc = {
114 .bLength = sizeof(eth_union_desc),
115 .bDescriptorType = UDESC_CS_INTERFACE,
116 .bDescriptorSubtype = UDESCSUB_CDC_UNION,
117 .bMasterInterface = 0, /* this is automatically updated */
118 .bSlaveInterface[0] = 1, /* this is automatically updated */
121 static const struct usb_cdc_header_descriptor eth_header_desc = {
122 .bLength = sizeof(eth_header_desc),
123 .bDescriptorType = UDESC_CS_INTERFACE,
124 .bDescriptorSubtype = UDESCSUB_CDC_HEADER,
129 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
130 .bLength = sizeof(eth_enf_desc),
131 .bDescriptorType = UDESC_CS_INTERFACE,
132 .bDescriptorSubtype = UDESCSUB_CDC_ENF,
133 .iMacAddress = STRING_MAC_INDEX,
134 .bmEthernetStatistics = {0, 0, 0, 0},
135 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
136 .wNumberMCFilters = {0, 0},
137 .bNumberPowerFilters = 0,
140 static const void *eth_control_if_desc[] = {
147 static const struct usb_temp_packet_size bulk_mps = {
148 .mps[USB_SPEED_FULL] = 64,
149 .mps[USB_SPEED_HIGH] = 512,
152 static const struct usb_temp_packet_size intr_mps = {
153 .mps[USB_SPEED_FULL] = 8,
154 .mps[USB_SPEED_HIGH] = 8,
157 static const struct usb_temp_endpoint_desc bulk_in_ep = {
158 .pPacketSize = &bulk_mps,
159 #ifdef USB_HIP_IN_EP_0
160 .bEndpointAddress = USB_HIP_IN_EP_0,
162 .bEndpointAddress = UE_DIR_IN,
164 .bmAttributes = UE_BULK,
167 static const struct usb_temp_endpoint_desc bulk_out_ep = {
168 .pPacketSize = &bulk_mps,
169 #ifdef USB_HIP_OUT_EP_0
170 .bEndpointAddress = USB_HIP_OUT_EP_0,
172 .bEndpointAddress = UE_DIR_OUT,
174 .bmAttributes = UE_BULK,
177 static const struct usb_temp_endpoint_desc intr_in_ep = {
178 .pPacketSize = &intr_mps,
179 .bEndpointAddress = UE_DIR_IN,
180 .bmAttributes = UE_INTERRUPT,
183 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
188 static const struct usb_temp_interface_desc eth_control_interface = {
189 .ppEndpoints = eth_intr_endpoints,
190 .ppRawDesc = eth_control_if_desc,
191 .bInterfaceClass = UICLASS_CDC,
192 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
193 .bInterfaceProtocol = 0,
194 .iInterface = STRING_ETH_CONTROL_INDEX,
197 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
203 static const struct usb_temp_interface_desc eth_data_null_interface = {
204 .ppEndpoints = NULL, /* no endpoints */
205 .bInterfaceClass = UICLASS_CDC_DATA,
206 .bInterfaceSubClass = 0,
207 .bInterfaceProtocol = 0,
208 .iInterface = STRING_ETH_DATA_INDEX,
211 static const struct usb_temp_interface_desc eth_data_interface = {
212 .ppEndpoints = eth_data_endpoints,
213 .bInterfaceClass = UICLASS_CDC_DATA,
214 .bInterfaceSubClass = UISUBCLASS_DATA,
215 .bInterfaceProtocol = 0,
216 .iInterface = STRING_ETH_DATA_INDEX,
217 .isAltInterface = 1, /* this is an alternate setting */
220 static const struct usb_temp_interface_desc *eth_interfaces[] = {
221 ð_control_interface,
222 ð_data_null_interface,
227 static const struct usb_temp_config_desc eth_config_desc = {
228 .ppIfaceDesc = eth_interfaces,
229 .bmAttributes = UC_BUS_POWERED,
230 .bMaxPower = 25, /* 50 mA */
231 .iConfiguration = STRING_ETH_CONFIG_INDEX,
234 static const struct usb_temp_config_desc *eth_configs[] = {
239 const struct usb_temp_device_desc usb_template_cdce = {
240 .getStringDesc = ð_get_string_desc,
241 .ppConfigDesc = eth_configs,
242 .idVendor = USB_TEMPLATE_VENDOR,
245 .bDeviceClass = UDCLASS_COMM,
246 .bDeviceSubClass = 0,
247 .bDeviceProtocol = 0,
248 .iManufacturer = STRING_ETH_VENDOR_INDEX,
249 .iProduct = STRING_ETH_PRODUCT_INDEX,
250 .iSerialNumber = STRING_ETH_SERIAL_INDEX,
253 /*------------------------------------------------------------------------*
254 * eth_get_string_desc
257 * NULL: Failure. No such string.
258 * Else: Success. Pointer to string descriptor is returned.
259 *------------------------------------------------------------------------*/
261 eth_get_string_desc(uint16_t lang_id, uint8_t string_index)
263 static const void *ptr[STRING_ETH_MAX] = {
264 [STRING_LANG_INDEX] = &usb_string_lang_en,
265 [STRING_MAC_INDEX] = &string_mac,
266 [STRING_ETH_CONTROL_INDEX] = &string_eth_control,
267 [STRING_ETH_DATA_INDEX] = &string_eth_data,
268 [STRING_ETH_CONFIG_INDEX] = &string_eth_config,
269 [STRING_ETH_VENDOR_INDEX] = &string_eth_vendor,
270 [STRING_ETH_PRODUCT_INDEX] = &string_eth_product,
271 [STRING_ETH_SERIAL_INDEX] = &string_eth_serial,
274 if (string_index == 0) {
275 return (&usb_string_lang_en);
277 if (lang_id != 0x0409) {
280 if (string_index < STRING_ETH_MAX) {
281 return (ptr[string_index]);