xref: /freebsd/sys/dev/usb/template/usb_template_cdce.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2007 Hans Petter Selasky <hselasky@FreeBSD.org>
5  * Copyright (c) 2018 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Edward Tomasz Napierala
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * This file contains the USB templates for a CDC USB ethernet device.
35  */
36 
37 #ifdef USB_GLOBAL_INCLUDE_FILE
38 #include USB_GLOBAL_INCLUDE_FILE
39 #else
40 #include <sys/stdint.h>
41 #include <sys/stddef.h>
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/bus.h>
48 #include <sys/module.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/condvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/sx.h>
54 #include <sys/unistd.h>
55 #include <sys/callout.h>
56 #include <sys/malloc.h>
57 #include <sys/priv.h>
58 
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_cdc.h>
63 #include <dev/usb/usb_ioctl.h>
64 #include <dev/usb/usb_util.h>
65 
66 #include <dev/usb/template/usb_template.h>
67 #endif			/* USB_GLOBAL_INCLUDE_FILE */
68 
69 enum {
70 	ETH_LANG_INDEX,
71 	ETH_MAC_INDEX,
72 	ETH_CONTROL_INDEX,
73 	ETH_DATA_INDEX,
74 	ETH_CONFIGURATION_INDEX,
75 	ETH_MANUFACTURER_INDEX,
76 	ETH_PRODUCT_INDEX,
77 	ETH_SERIAL_NUMBER_INDEX,
78 	ETH_MAX_INDEX,
79 };
80 
81 #define	ETH_DEFAULT_VENDOR_ID		USB_TEMPLATE_VENDOR
82 #define	ETH_DEFAULT_PRODUCT_ID		0x27e1
83 #define	ETH_DEFAULT_MAC			"2A02030405060789AB"
84 #define	ETH_DEFAULT_CONTROL		"USB Ethernet Comm Interface"
85 #define	ETH_DEFAULT_DATA		"USB Ethernet Data Interface"
86 #define	ETH_DEFAULT_CONFIG		"Default Config"
87 #define	ETH_DEFAULT_MANUFACTURER	USB_TEMPLATE_MANUFACTURER
88 #define	ETH_DEFAULT_PRODUCT		"USB Ethernet Adapter"
89 #define	ETH_DEFAULT_SERIAL_NUMBER	"December 2007"
90 
91 static struct usb_string_descriptor	eth_mac;
92 static struct usb_string_descriptor	eth_control;
93 static struct usb_string_descriptor	eth_data;
94 static struct usb_string_descriptor	eth_configuration;
95 static struct usb_string_descriptor	eth_manufacturer;
96 static struct usb_string_descriptor	eth_product;
97 static struct usb_string_descriptor	eth_serial_number;
98 
99 static struct sysctl_ctx_list		eth_ctx_list;
100 
101 /* prototypes */
102 
103 static usb_temp_get_string_desc_t eth_get_string_desc;
104 
105 static const struct usb_cdc_union_descriptor eth_union_desc = {
106 	.bLength = sizeof(eth_union_desc),
107 	.bDescriptorType = UDESC_CS_INTERFACE,
108 	.bDescriptorSubtype = UDESCSUB_CDC_UNION,
109 	.bMasterInterface = 0,		/* this is automatically updated */
110 	.bSlaveInterface[0] = 1,	/* this is automatically updated */
111 };
112 
113 static const struct usb_cdc_header_descriptor eth_header_desc = {
114 	.bLength = sizeof(eth_header_desc),
115 	.bDescriptorType = UDESC_CS_INTERFACE,
116 	.bDescriptorSubtype = UDESCSUB_CDC_HEADER,
117 	.bcdCDC[0] = 0x10,
118 	.bcdCDC[1] = 0x01,
119 };
120 
121 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
122 	.bLength = sizeof(eth_enf_desc),
123 	.bDescriptorType = UDESC_CS_INTERFACE,
124 	.bDescriptorSubtype = UDESCSUB_CDC_ENF,
125 	.iMacAddress = ETH_MAC_INDEX,
126 	.bmEthernetStatistics = {0, 0, 0, 0},
127 	.wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
128 	.wNumberMCFilters = {0, 0},
129 	.bNumberPowerFilters = 0,
130 };
131 
132 static const void *eth_control_if_desc[] = {
133 	&eth_union_desc,
134 	&eth_header_desc,
135 	&eth_enf_desc,
136 	NULL,
137 };
138 
139 static const struct usb_temp_packet_size bulk_mps = {
140 	.mps[USB_SPEED_FULL] = 64,
141 	.mps[USB_SPEED_HIGH] = 512,
142 };
143 
144 static const struct usb_temp_packet_size intr_mps = {
145 	.mps[USB_SPEED_FULL] = 8,
146 	.mps[USB_SPEED_HIGH] = 8,
147 };
148 
149 static const struct usb_temp_endpoint_desc bulk_in_ep = {
150 	.pPacketSize = &bulk_mps,
151 #ifdef USB_HIP_IN_EP_0
152 	.bEndpointAddress = USB_HIP_IN_EP_0,
153 #else
154 	.bEndpointAddress = UE_DIR_IN,
155 #endif
156 	.bmAttributes = UE_BULK,
157 };
158 
159 static const struct usb_temp_endpoint_desc bulk_out_ep = {
160 	.pPacketSize = &bulk_mps,
161 #ifdef USB_HIP_OUT_EP_0
162 	.bEndpointAddress = USB_HIP_OUT_EP_0,
163 #else
164 	.bEndpointAddress = UE_DIR_OUT,
165 #endif
166 	.bmAttributes = UE_BULK,
167 };
168 
169 static const struct usb_temp_endpoint_desc intr_in_ep = {
170 	.pPacketSize = &intr_mps,
171 	.bEndpointAddress = UE_DIR_IN,
172 	.bmAttributes = UE_INTERRUPT,
173 };
174 
175 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
176 	&intr_in_ep,
177 	NULL,
178 };
179 
180 static const struct usb_temp_interface_desc eth_control_interface = {
181 	.ppEndpoints = eth_intr_endpoints,
182 	.ppRawDesc = eth_control_if_desc,
183 	.bInterfaceClass = UICLASS_CDC,
184 	.bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
185 	.bInterfaceProtocol = 0,
186 	.iInterface = ETH_CONTROL_INDEX,
187 };
188 
189 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
190 	&bulk_in_ep,
191 	&bulk_out_ep,
192 	NULL,
193 };
194 
195 static const struct usb_temp_interface_desc eth_data_null_interface = {
196 	.ppEndpoints = NULL,		/* no endpoints */
197 	.bInterfaceClass = UICLASS_CDC_DATA,
198 	.bInterfaceSubClass = 0,
199 	.bInterfaceProtocol = 0,
200 	.iInterface = ETH_DATA_INDEX,
201 };
202 
203 static const struct usb_temp_interface_desc eth_data_interface = {
204 	.ppEndpoints = eth_data_endpoints,
205 	.bInterfaceClass = UICLASS_CDC_DATA,
206 	.bInterfaceSubClass = UISUBCLASS_DATA,
207 	.bInterfaceProtocol = 0,
208 	.iInterface = ETH_DATA_INDEX,
209 	.isAltInterface = 1,		/* this is an alternate setting */
210 };
211 
212 static const struct usb_temp_interface_desc *eth_interfaces[] = {
213 	&eth_control_interface,
214 	&eth_data_null_interface,
215 	&eth_data_interface,
216 	NULL,
217 };
218 
219 static const struct usb_temp_config_desc eth_config_desc = {
220 	.ppIfaceDesc = eth_interfaces,
221 	.bmAttributes = 0,
222 	.bMaxPower = 0,
223 	.iConfiguration = ETH_CONFIGURATION_INDEX,
224 };
225 
226 static const struct usb_temp_config_desc *eth_configs[] = {
227 	&eth_config_desc,
228 	NULL,
229 };
230 
231 struct usb_temp_device_desc usb_template_cdce = {
232 	.getStringDesc = &eth_get_string_desc,
233 	.ppConfigDesc = eth_configs,
234 	.idVendor = ETH_DEFAULT_VENDOR_ID,
235 	.idProduct = ETH_DEFAULT_PRODUCT_ID,
236 	.bcdDevice = 0x0100,
237 	.bDeviceClass = UDCLASS_COMM,
238 	.bDeviceSubClass = 0,
239 	.bDeviceProtocol = 0,
240 	.iManufacturer = ETH_MANUFACTURER_INDEX,
241 	.iProduct = ETH_PRODUCT_INDEX,
242 	.iSerialNumber = ETH_SERIAL_NUMBER_INDEX,
243 };
244 
245 /*------------------------------------------------------------------------*
246  *	eth_get_string_desc
247  *
248  * Return values:
249  * NULL: Failure. No such string.
250  * Else: Success. Pointer to string descriptor is returned.
251  *------------------------------------------------------------------------*/
252 static const void *
253 eth_get_string_desc(uint16_t lang_id, uint8_t string_index)
254 {
255 	static const void *ptr[ETH_MAX_INDEX] = {
256 		[ETH_LANG_INDEX] = &usb_string_lang_en,
257 		[ETH_MAC_INDEX] = &eth_mac,
258 		[ETH_CONTROL_INDEX] = &eth_control,
259 		[ETH_DATA_INDEX] = &eth_data,
260 		[ETH_CONFIGURATION_INDEX] = &eth_configuration,
261 		[ETH_MANUFACTURER_INDEX] = &eth_manufacturer,
262 		[ETH_PRODUCT_INDEX] = &eth_product,
263 		[ETH_SERIAL_NUMBER_INDEX] = &eth_serial_number,
264 	};
265 
266 	if (string_index == 0) {
267 		return (&usb_string_lang_en);
268 	}
269 	if (lang_id != 0x0409) {
270 		return (NULL);
271 	}
272 	if (string_index < ETH_MAX_INDEX) {
273 		return (ptr[string_index]);
274 	}
275 	return (NULL);
276 }
277 
278 static void
279 eth_init(void *arg __unused)
280 {
281 	struct sysctl_oid *parent;
282 	char parent_name[3];
283 
284 	usb_make_str_desc(&eth_mac, sizeof(eth_mac),
285 	    ETH_DEFAULT_MAC);
286 	usb_make_str_desc(&eth_control, sizeof(eth_control),
287 	    ETH_DEFAULT_CONTROL);
288 	usb_make_str_desc(&eth_data, sizeof(eth_data),
289 	    ETH_DEFAULT_DATA);
290 	usb_make_str_desc(&eth_configuration, sizeof(eth_configuration),
291 	    ETH_DEFAULT_CONFIG);
292 	usb_make_str_desc(&eth_manufacturer, sizeof(eth_manufacturer),
293 	    ETH_DEFAULT_MANUFACTURER);
294 	usb_make_str_desc(&eth_product, sizeof(eth_product),
295 	    ETH_DEFAULT_PRODUCT);
296 	usb_make_str_desc(&eth_serial_number, sizeof(eth_serial_number),
297 	    ETH_DEFAULT_SERIAL_NUMBER);
298 
299 	snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_CDCE);
300 	sysctl_ctx_init(&eth_ctx_list);
301 
302 	parent = SYSCTL_ADD_NODE(&eth_ctx_list,
303 	    SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
304 	    parent_name, CTLFLAG_RW | CTLFLAG_MPSAFE,
305 	    0, "USB CDC Ethernet device side template");
306 	SYSCTL_ADD_U16(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
307 	    "vendor_id", CTLFLAG_RWTUN,
308 	    &usb_template_cdce.idVendor, 1, "Vendor identifier");
309 	SYSCTL_ADD_U16(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
310 	    "product_id", CTLFLAG_RWTUN,
311 	    &usb_template_cdce.idProduct, 1, "Product identifier");
312 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
313 	    "mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
314 	    &eth_mac, sizeof(eth_mac), usb_temp_sysctl,
315 	    "A", "MAC address string");
316 #if 0
317 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
318 	    "control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
319 	    &eth_control, sizeof(eth_control), usb_temp_sysctl,
320 	    "A", "Control interface string");
321 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
322 	    "data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
323 	    &eth_data, sizeof(eth_data), usb_temp_sysctl,
324 	    "A", "Data interface string");
325 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
326 	    "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
327 	    &eth_configuration, sizeof(eth_configuration), usb_temp_sysctl,
328 	    "A", "Configuration string");
329 #endif
330 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
331 	    "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
332 	    &eth_manufacturer, sizeof(eth_manufacturer), usb_temp_sysctl,
333 	    "A", "Manufacturer string");
334 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
335 	    "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
336 	    &eth_product, sizeof(eth_product), usb_temp_sysctl,
337 	    "A", "Product string");
338 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
339 	    "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
340 	    &eth_serial_number, sizeof(eth_serial_number), usb_temp_sysctl,
341 	    "A", "Serial number string");
342 }
343 
344 static void
345 eth_uninit(void *arg __unused)
346 {
347 
348 	sysctl_ctx_free(&eth_ctx_list);
349 }
350 
351 SYSINIT(eth_init, SI_SUB_LOCK, SI_ORDER_FIRST, eth_init, NULL);
352 SYSUNINIT(eth_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, eth_uninit, NULL);
353