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_VENDOR_ID USB_TEMPLATE_VENDOR
83 #define ETH_DEFAULT_PRODUCT_ID 0x27e1
84 #define ETH_DEFAULT_MAC "2A02030405060789AB"
85 #define ETH_DEFAULT_CONTROL "USB Ethernet Comm Interface"
86 #define ETH_DEFAULT_DATA "USB Ethernet Data Interface"
87 #define ETH_DEFAULT_CONFIG "Default Config"
88 #define ETH_DEFAULT_MANUFACTURER USB_TEMPLATE_MANUFACTURER
89 #define ETH_DEFAULT_PRODUCT "USB Ethernet Adapter"
90 #define ETH_DEFAULT_SERIAL_NUMBER "December 2007"
92 static struct usb_string_descriptor eth_mac;
93 static struct usb_string_descriptor eth_control;
94 static struct usb_string_descriptor eth_data;
95 static struct usb_string_descriptor eth_configuration;
96 static struct usb_string_descriptor eth_manufacturer;
97 static struct usb_string_descriptor eth_product;
98 static struct usb_string_descriptor eth_serial_number;
100 static struct sysctl_ctx_list eth_ctx_list;
104 static usb_temp_get_string_desc_t eth_get_string_desc;
106 static const struct usb_cdc_union_descriptor eth_union_desc = {
107 .bLength = sizeof(eth_union_desc),
108 .bDescriptorType = UDESC_CS_INTERFACE,
109 .bDescriptorSubtype = UDESCSUB_CDC_UNION,
110 .bMasterInterface = 0, /* this is automatically updated */
111 .bSlaveInterface[0] = 1, /* this is automatically updated */
114 static const struct usb_cdc_header_descriptor eth_header_desc = {
115 .bLength = sizeof(eth_header_desc),
116 .bDescriptorType = UDESC_CS_INTERFACE,
117 .bDescriptorSubtype = UDESCSUB_CDC_HEADER,
122 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
123 .bLength = sizeof(eth_enf_desc),
124 .bDescriptorType = UDESC_CS_INTERFACE,
125 .bDescriptorSubtype = UDESCSUB_CDC_ENF,
126 .iMacAddress = ETH_MAC_INDEX,
127 .bmEthernetStatistics = {0, 0, 0, 0},
128 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
129 .wNumberMCFilters = {0, 0},
130 .bNumberPowerFilters = 0,
133 static const void *eth_control_if_desc[] = {
140 static const struct usb_temp_packet_size bulk_mps = {
141 .mps[USB_SPEED_FULL] = 64,
142 .mps[USB_SPEED_HIGH] = 512,
145 static const struct usb_temp_packet_size intr_mps = {
146 .mps[USB_SPEED_FULL] = 8,
147 .mps[USB_SPEED_HIGH] = 8,
150 static const struct usb_temp_endpoint_desc bulk_in_ep = {
151 .pPacketSize = &bulk_mps,
152 #ifdef USB_HIP_IN_EP_0
153 .bEndpointAddress = USB_HIP_IN_EP_0,
155 .bEndpointAddress = UE_DIR_IN,
157 .bmAttributes = UE_BULK,
160 static const struct usb_temp_endpoint_desc bulk_out_ep = {
161 .pPacketSize = &bulk_mps,
162 #ifdef USB_HIP_OUT_EP_0
163 .bEndpointAddress = USB_HIP_OUT_EP_0,
165 .bEndpointAddress = UE_DIR_OUT,
167 .bmAttributes = UE_BULK,
170 static const struct usb_temp_endpoint_desc intr_in_ep = {
171 .pPacketSize = &intr_mps,
172 .bEndpointAddress = UE_DIR_IN,
173 .bmAttributes = UE_INTERRUPT,
176 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
181 static const struct usb_temp_interface_desc eth_control_interface = {
182 .ppEndpoints = eth_intr_endpoints,
183 .ppRawDesc = eth_control_if_desc,
184 .bInterfaceClass = UICLASS_CDC,
185 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
186 .bInterfaceProtocol = 0,
187 .iInterface = ETH_CONTROL_INDEX,
190 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
196 static const struct usb_temp_interface_desc eth_data_null_interface = {
197 .ppEndpoints = NULL, /* no endpoints */
198 .bInterfaceClass = UICLASS_CDC_DATA,
199 .bInterfaceSubClass = 0,
200 .bInterfaceProtocol = 0,
201 .iInterface = ETH_DATA_INDEX,
204 static const struct usb_temp_interface_desc eth_data_interface = {
205 .ppEndpoints = eth_data_endpoints,
206 .bInterfaceClass = UICLASS_CDC_DATA,
207 .bInterfaceSubClass = UISUBCLASS_DATA,
208 .bInterfaceProtocol = 0,
209 .iInterface = ETH_DATA_INDEX,
210 .isAltInterface = 1, /* this is an alternate setting */
213 static const struct usb_temp_interface_desc *eth_interfaces[] = {
214 ð_control_interface,
215 ð_data_null_interface,
220 static const struct usb_temp_config_desc eth_config_desc = {
221 .ppIfaceDesc = eth_interfaces,
224 .iConfiguration = ETH_CONFIGURATION_INDEX,
227 static const struct usb_temp_config_desc *eth_configs[] = {
232 struct usb_temp_device_desc usb_template_cdce = {
233 .getStringDesc = ð_get_string_desc,
234 .ppConfigDesc = eth_configs,
235 .idVendor = ETH_DEFAULT_VENDOR_ID,
236 .idProduct = ETH_DEFAULT_PRODUCT_ID,
238 .bDeviceClass = UDCLASS_COMM,
239 .bDeviceSubClass = 0,
240 .bDeviceProtocol = 0,
241 .iManufacturer = ETH_MANUFACTURER_INDEX,
242 .iProduct = ETH_PRODUCT_INDEX,
243 .iSerialNumber = ETH_SERIAL_NUMBER_INDEX,
246 /*------------------------------------------------------------------------*
247 * eth_get_string_desc
250 * NULL: Failure. No such string.
251 * Else: Success. Pointer to string descriptor is returned.
252 *------------------------------------------------------------------------*/
254 eth_get_string_desc(uint16_t lang_id, uint8_t string_index)
256 static const void *ptr[ETH_MAX_INDEX] = {
257 [ETH_LANG_INDEX] = &usb_string_lang_en,
258 [ETH_MAC_INDEX] = ð_mac,
259 [ETH_CONTROL_INDEX] = ð_control,
260 [ETH_DATA_INDEX] = ð_data,
261 [ETH_CONFIGURATION_INDEX] = ð_configuration,
262 [ETH_MANUFACTURER_INDEX] = ð_manufacturer,
263 [ETH_PRODUCT_INDEX] = ð_product,
264 [ETH_SERIAL_NUMBER_INDEX] = ð_serial_number,
267 if (string_index == 0) {
268 return (&usb_string_lang_en);
270 if (lang_id != 0x0409) {
273 if (string_index < ETH_MAX_INDEX) {
274 return (ptr[string_index]);
280 eth_init(void *arg __unused)
282 struct sysctl_oid *parent;
285 usb_make_str_desc(ð_mac, sizeof(eth_mac),
287 usb_make_str_desc(ð_control, sizeof(eth_control),
288 ETH_DEFAULT_CONTROL);
289 usb_make_str_desc(ð_data, sizeof(eth_data),
291 usb_make_str_desc(ð_configuration, sizeof(eth_configuration),
293 usb_make_str_desc(ð_manufacturer, sizeof(eth_manufacturer),
294 ETH_DEFAULT_MANUFACTURER);
295 usb_make_str_desc(ð_product, sizeof(eth_product),
296 ETH_DEFAULT_PRODUCT);
297 usb_make_str_desc(ð_serial_number, sizeof(eth_serial_number),
298 ETH_DEFAULT_SERIAL_NUMBER);
300 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_CDCE);
301 sysctl_ctx_init(ð_ctx_list);
303 parent = SYSCTL_ADD_NODE(ð_ctx_list,
304 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
305 parent_name, CTLFLAG_RW,
306 0, "USB CDC Ethernet device side template");
307 SYSCTL_ADD_U16(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
308 "vendor_id", CTLFLAG_RWTUN,
309 &usb_template_cdce.idVendor, 1, "Vendor identifier");
310 SYSCTL_ADD_U16(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
311 "product_id", CTLFLAG_RWTUN,
312 &usb_template_cdce.idProduct, 1, "Product identifier");
313 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
314 "mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
315 ð_mac, sizeof(eth_mac), usb_temp_sysctl,
316 "A", "MAC address string");
318 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
319 "control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
320 ð_control, sizeof(eth_control), usb_temp_sysctl,
321 "A", "Control interface string");
322 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
323 "data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
324 ð_data, sizeof(eth_data), usb_temp_sysctl,
325 "A", "Data interface string");
326 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
327 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
328 ð_configuration, sizeof(eth_configuration), usb_temp_sysctl,
329 "A", "Configuration string");
331 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
332 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
333 ð_manufacturer, sizeof(eth_manufacturer), usb_temp_sysctl,
334 "A", "Manufacturer string");
335 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
336 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
337 ð_product, sizeof(eth_product), usb_temp_sysctl,
338 "A", "Product string");
339 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
340 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
341 ð_serial_number, sizeof(eth_serial_number), usb_temp_sysctl,
342 "A", "Serial number string");
346 eth_uninit(void *arg __unused)
349 sysctl_ctx_free(ð_ctx_list);
352 SYSINIT(eth_init, SI_SUB_LOCK, SI_ORDER_FIRST, eth_init, NULL);
353 SYSUNINIT(eth_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, eth_uninit, NULL);