3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2007 Hans Petter Selasky <hselasky@FreeBSD.org>
6 * Copyright (c) 2018 The FreeBSD Foundation
9 * Portions of this software were developed by Edward Tomasz Napierala
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * This file contains the USB templates for a CDC USB ethernet device.
38 #ifdef USB_GLOBAL_INCLUDE_FILE
39 #include USB_GLOBAL_INCLUDE_FILE
41 #include <sys/stdint.h>
42 #include <sys/stddef.h>
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/module.h>
51 #include <sys/mutex.h>
52 #include <sys/condvar.h>
53 #include <sys/sysctl.h>
55 #include <sys/unistd.h>
56 #include <sys/callout.h>
57 #include <sys/malloc.h>
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usb_core.h>
63 #include <dev/usb/usb_cdc.h>
64 #include <dev/usb/usb_ioctl.h>
65 #include <dev/usb/usb_util.h>
67 #include <dev/usb/template/usb_template.h>
68 #endif /* USB_GLOBAL_INCLUDE_FILE */
75 ETH_CONFIGURATION_INDEX,
76 ETH_MANUFACTURER_INDEX,
78 ETH_SERIAL_NUMBER_INDEX,
82 #define ETH_DEFAULT_MAC "2A02030405060789AB"
83 #define ETH_DEFAULT_CONTROL "USB Ethernet Comm Interface"
84 #define ETH_DEFAULT_DATA "USB Ethernet Data Interface"
85 #define ETH_DEFAULT_CONFIG "Default Config"
86 #define ETH_DEFAULT_MANUFACTURER "FreeBSD foundation"
87 #define ETH_DEFAULT_PRODUCT "USB Ethernet Adapter"
88 #define ETH_DEFAULT_SERIAL_NUMBER "December 2007"
90 static struct usb_string_descriptor eth_mac;
91 static struct usb_string_descriptor eth_control;
92 static struct usb_string_descriptor eth_data;
93 static struct usb_string_descriptor eth_configuration;
94 static struct usb_string_descriptor eth_manufacturer;
95 static struct usb_string_descriptor eth_product;
96 static struct usb_string_descriptor eth_serial_number;
98 static struct sysctl_ctx_list eth_ctx_list;
102 static usb_temp_get_string_desc_t eth_get_string_desc;
104 static const struct usb_cdc_union_descriptor eth_union_desc = {
105 .bLength = sizeof(eth_union_desc),
106 .bDescriptorType = UDESC_CS_INTERFACE,
107 .bDescriptorSubtype = UDESCSUB_CDC_UNION,
108 .bMasterInterface = 0, /* this is automatically updated */
109 .bSlaveInterface[0] = 1, /* this is automatically updated */
112 static const struct usb_cdc_header_descriptor eth_header_desc = {
113 .bLength = sizeof(eth_header_desc),
114 .bDescriptorType = UDESC_CS_INTERFACE,
115 .bDescriptorSubtype = UDESCSUB_CDC_HEADER,
120 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
121 .bLength = sizeof(eth_enf_desc),
122 .bDescriptorType = UDESC_CS_INTERFACE,
123 .bDescriptorSubtype = UDESCSUB_CDC_ENF,
124 .iMacAddress = ETH_MAC_INDEX,
125 .bmEthernetStatistics = {0, 0, 0, 0},
126 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
127 .wNumberMCFilters = {0, 0},
128 .bNumberPowerFilters = 0,
131 static const void *eth_control_if_desc[] = {
138 static const struct usb_temp_packet_size bulk_mps = {
139 .mps[USB_SPEED_FULL] = 64,
140 .mps[USB_SPEED_HIGH] = 512,
143 static const struct usb_temp_packet_size intr_mps = {
144 .mps[USB_SPEED_FULL] = 8,
145 .mps[USB_SPEED_HIGH] = 8,
148 static const struct usb_temp_endpoint_desc bulk_in_ep = {
149 .pPacketSize = &bulk_mps,
150 #ifdef USB_HIP_IN_EP_0
151 .bEndpointAddress = USB_HIP_IN_EP_0,
153 .bEndpointAddress = UE_DIR_IN,
155 .bmAttributes = UE_BULK,
158 static const struct usb_temp_endpoint_desc bulk_out_ep = {
159 .pPacketSize = &bulk_mps,
160 #ifdef USB_HIP_OUT_EP_0
161 .bEndpointAddress = USB_HIP_OUT_EP_0,
163 .bEndpointAddress = UE_DIR_OUT,
165 .bmAttributes = UE_BULK,
168 static const struct usb_temp_endpoint_desc intr_in_ep = {
169 .pPacketSize = &intr_mps,
170 .bEndpointAddress = UE_DIR_IN,
171 .bmAttributes = UE_INTERRUPT,
174 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
179 static const struct usb_temp_interface_desc eth_control_interface = {
180 .ppEndpoints = eth_intr_endpoints,
181 .ppRawDesc = eth_control_if_desc,
182 .bInterfaceClass = UICLASS_CDC,
183 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
184 .bInterfaceProtocol = 0,
185 .iInterface = ETH_CONTROL_INDEX,
188 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
194 static const struct usb_temp_interface_desc eth_data_null_interface = {
195 .ppEndpoints = NULL, /* no endpoints */
196 .bInterfaceClass = UICLASS_CDC_DATA,
197 .bInterfaceSubClass = 0,
198 .bInterfaceProtocol = 0,
199 .iInterface = ETH_DATA_INDEX,
202 static const struct usb_temp_interface_desc eth_data_interface = {
203 .ppEndpoints = eth_data_endpoints,
204 .bInterfaceClass = UICLASS_CDC_DATA,
205 .bInterfaceSubClass = UISUBCLASS_DATA,
206 .bInterfaceProtocol = 0,
207 .iInterface = ETH_DATA_INDEX,
208 .isAltInterface = 1, /* this is an alternate setting */
211 static const struct usb_temp_interface_desc *eth_interfaces[] = {
212 ð_control_interface,
213 ð_data_null_interface,
218 static const struct usb_temp_config_desc eth_config_desc = {
219 .ppIfaceDesc = eth_interfaces,
220 .bmAttributes = UC_BUS_POWERED,
221 .bMaxPower = 25, /* 50 mA */
222 .iConfiguration = ETH_CONFIGURATION_INDEX,
225 static const struct usb_temp_config_desc *eth_configs[] = {
230 struct usb_temp_device_desc usb_template_cdce = {
231 .getStringDesc = ð_get_string_desc,
232 .ppConfigDesc = eth_configs,
233 .idVendor = USB_TEMPLATE_VENDOR,
236 .bDeviceClass = UDCLASS_COMM,
237 .bDeviceSubClass = 0,
238 .bDeviceProtocol = 0,
239 .iManufacturer = ETH_MANUFACTURER_INDEX,
240 .iProduct = ETH_PRODUCT_INDEX,
241 .iSerialNumber = ETH_SERIAL_NUMBER_INDEX,
244 /*------------------------------------------------------------------------*
245 * eth_get_string_desc
248 * NULL: Failure. No such string.
249 * Else: Success. Pointer to string descriptor is returned.
250 *------------------------------------------------------------------------*/
252 eth_get_string_desc(uint16_t lang_id, uint8_t string_index)
254 static const void *ptr[ETH_MAX_INDEX] = {
255 [ETH_LANG_INDEX] = &usb_string_lang_en,
256 [ETH_MAC_INDEX] = ð_mac,
257 [ETH_CONTROL_INDEX] = ð_control,
258 [ETH_DATA_INDEX] = ð_data,
259 [ETH_CONFIGURATION_INDEX] = ð_configuration,
260 [ETH_MANUFACTURER_INDEX] = ð_manufacturer,
261 [ETH_PRODUCT_INDEX] = ð_product,
262 [ETH_SERIAL_NUMBER_INDEX] = ð_serial_number,
265 if (string_index == 0) {
266 return (&usb_string_lang_en);
268 if (lang_id != 0x0409) {
271 if (string_index < ETH_MAX_INDEX) {
272 return (ptr[string_index]);
278 eth_init(void *arg __unused)
280 struct sysctl_oid *parent;
283 usb_make_str_desc(ð_mac, sizeof(eth_mac),
285 usb_make_str_desc(ð_control, sizeof(eth_control),
286 ETH_DEFAULT_CONTROL);
287 usb_make_str_desc(ð_data, sizeof(eth_data),
289 usb_make_str_desc(ð_configuration, sizeof(eth_configuration),
291 usb_make_str_desc(ð_manufacturer, sizeof(eth_manufacturer),
292 ETH_DEFAULT_MANUFACTURER);
293 usb_make_str_desc(ð_product, sizeof(eth_product),
294 ETH_DEFAULT_PRODUCT);
295 usb_make_str_desc(ð_serial_number, sizeof(eth_serial_number),
296 ETH_DEFAULT_SERIAL_NUMBER);
298 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_CDCE);
299 sysctl_ctx_init(ð_ctx_list);
301 parent = SYSCTL_ADD_NODE(ð_ctx_list,
302 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
303 parent_name, CTLFLAG_RW,
304 0, "USB CDC Ethernet device side template");
305 SYSCTL_ADD_U16(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
306 "vendor_id", CTLFLAG_RWTUN,
307 &usb_template_cdce.idVendor, 1, "Vendor identifier");
308 SYSCTL_ADD_U16(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
309 "product_id", CTLFLAG_RWTUN,
310 &usb_template_cdce.idProduct, 1, "Product identifier");
311 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
312 "mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
313 ð_mac, sizeof(eth_mac), usb_temp_sysctl,
314 "A", "MAC address string");
316 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
317 "control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
318 ð_control, sizeof(eth_control), usb_temp_sysctl,
319 "A", "Control interface string");
320 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
321 "data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
322 ð_data, sizeof(eth_data), usb_temp_sysctl,
323 "A", "Data interface string");
324 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
325 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
326 ð_configuration, sizeof(eth_configuration), usb_temp_sysctl,
327 "A", "Configuration string");
329 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
330 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
331 ð_manufacturer, sizeof(eth_manufacturer), usb_temp_sysctl,
332 "A", "Manufacturer string");
333 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
334 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
335 ð_product, sizeof(eth_product), usb_temp_sysctl,
336 "A", "Product string");
337 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
338 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
339 ð_serial_number, sizeof(eth_serial_number), usb_temp_sysctl,
340 "A", "Serial number string");
344 eth_uninit(void *arg __unused)
347 sysctl_ctx_free(ð_ctx_list);
350 SYSINIT(eth_init, SI_SUB_LOCK, SI_ORDER_FIRST, eth_init, NULL);
351 SYSUNINIT(eth_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, eth_uninit, NULL);