xref: /freebsd/sys/dev/usb/template/usb_template_cdce.c (revision 74ca7bf1d4c7173d5575ba168bc4b5f6d181ff5a)
1 /* $FreeBSD$ */
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (c) 2007 Hans Petter Selasky <hselasky@FreeBSD.org>
6  * Copyright (c) 2018 The FreeBSD Foundation
7  * All rights reserved.
8  *
9  * Portions of this software were developed by Edward Tomasz Napierala
10  * under sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * This file contains the USB templates for a CDC USB ethernet device.
36  */
37 
38 #ifdef USB_GLOBAL_INCLUDE_FILE
39 #include USB_GLOBAL_INCLUDE_FILE
40 #else
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>
48 #include <sys/bus.h>
49 #include <sys/module.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/condvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/sx.h>
55 #include <sys/unistd.h>
56 #include <sys/callout.h>
57 #include <sys/malloc.h>
58 #include <sys/priv.h>
59 
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>
66 
67 #include <dev/usb/template/usb_template.h>
68 #endif			/* USB_GLOBAL_INCLUDE_FILE */
69 
70 enum {
71 	ETH_LANG_INDEX,
72 	ETH_MAC_INDEX,
73 	ETH_CONTROL_INDEX,
74 	ETH_DATA_INDEX,
75 	ETH_CONFIGURATION_INDEX,
76 	ETH_MANUFACTURER_INDEX,
77 	ETH_PRODUCT_INDEX,
78 	ETH_SERIAL_NUMBER_INDEX,
79 	ETH_MAX_INDEX,
80 };
81 
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"
89 
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;
97 
98 static struct sysctl_ctx_list		eth_ctx_list;
99 
100 /* prototypes */
101 
102 static usb_temp_get_string_desc_t eth_get_string_desc;
103 
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 */
110 };
111 
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,
116 	.bcdCDC[0] = 0x10,
117 	.bcdCDC[1] = 0x01,
118 };
119 
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,
129 };
130 
131 static const void *eth_control_if_desc[] = {
132 	&eth_union_desc,
133 	&eth_header_desc,
134 	&eth_enf_desc,
135 	NULL,
136 };
137 
138 static const struct usb_temp_packet_size bulk_mps = {
139 	.mps[USB_SPEED_FULL] = 64,
140 	.mps[USB_SPEED_HIGH] = 512,
141 };
142 
143 static const struct usb_temp_packet_size intr_mps = {
144 	.mps[USB_SPEED_FULL] = 8,
145 	.mps[USB_SPEED_HIGH] = 8,
146 };
147 
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,
152 #else
153 	.bEndpointAddress = UE_DIR_IN,
154 #endif
155 	.bmAttributes = UE_BULK,
156 };
157 
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,
162 #else
163 	.bEndpointAddress = UE_DIR_OUT,
164 #endif
165 	.bmAttributes = UE_BULK,
166 };
167 
168 static const struct usb_temp_endpoint_desc intr_in_ep = {
169 	.pPacketSize = &intr_mps,
170 	.bEndpointAddress = UE_DIR_IN,
171 	.bmAttributes = UE_INTERRUPT,
172 };
173 
174 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
175 	&intr_in_ep,
176 	NULL,
177 };
178 
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,
186 };
187 
188 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
189 	&bulk_in_ep,
190 	&bulk_out_ep,
191 	NULL,
192 };
193 
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,
200 };
201 
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 */
209 };
210 
211 static const struct usb_temp_interface_desc *eth_interfaces[] = {
212 	&eth_control_interface,
213 	&eth_data_null_interface,
214 	&eth_data_interface,
215 	NULL,
216 };
217 
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,
223 };
224 
225 static const struct usb_temp_config_desc *eth_configs[] = {
226 	&eth_config_desc,
227 	NULL,
228 };
229 
230 struct usb_temp_device_desc usb_template_cdce = {
231 	.getStringDesc = &eth_get_string_desc,
232 	.ppConfigDesc = eth_configs,
233 	.idVendor = USB_TEMPLATE_VENDOR,
234 	.idProduct = 0x0001,
235 	.bcdDevice = 0x0100,
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,
242 };
243 
244 /*------------------------------------------------------------------------*
245  *	eth_get_string_desc
246  *
247  * Return values:
248  * NULL: Failure. No such string.
249  * Else: Success. Pointer to string descriptor is returned.
250  *------------------------------------------------------------------------*/
251 static const void *
252 eth_get_string_desc(uint16_t lang_id, uint8_t string_index)
253 {
254 	static const void *ptr[ETH_MAX_INDEX] = {
255 		[ETH_LANG_INDEX] = &usb_string_lang_en,
256 		[ETH_MAC_INDEX] = &eth_mac,
257 		[ETH_CONTROL_INDEX] = &eth_control,
258 		[ETH_DATA_INDEX] = &eth_data,
259 		[ETH_CONFIGURATION_INDEX] = &eth_configuration,
260 		[ETH_MANUFACTURER_INDEX] = &eth_manufacturer,
261 		[ETH_PRODUCT_INDEX] = &eth_product,
262 		[ETH_SERIAL_NUMBER_INDEX] = &eth_serial_number,
263 	};
264 
265 	if (string_index == 0) {
266 		return (&usb_string_lang_en);
267 	}
268 	if (lang_id != 0x0409) {
269 		return (NULL);
270 	}
271 	if (string_index < ETH_MAX_INDEX) {
272 		return (ptr[string_index]);
273 	}
274 	return (NULL);
275 }
276 
277 static void
278 eth_init(void *arg __unused)
279 {
280 	struct sysctl_oid *parent;
281 	char parent_name[3];
282 
283 	usb_make_str_desc(&eth_mac, sizeof(eth_mac),
284 	    ETH_DEFAULT_MAC);
285 	usb_make_str_desc(&eth_control, sizeof(eth_control),
286 	    ETH_DEFAULT_CONTROL);
287 	usb_make_str_desc(&eth_data, sizeof(eth_data),
288 	    ETH_DEFAULT_DATA);
289 	usb_make_str_desc(&eth_configuration, sizeof(eth_configuration),
290 	    ETH_DEFAULT_CONFIG);
291 	usb_make_str_desc(&eth_manufacturer, sizeof(eth_manufacturer),
292 	    ETH_DEFAULT_MANUFACTURER);
293 	usb_make_str_desc(&eth_product, sizeof(eth_product),
294 	    ETH_DEFAULT_PRODUCT);
295 	usb_make_str_desc(&eth_serial_number, sizeof(eth_serial_number),
296 	    ETH_DEFAULT_SERIAL_NUMBER);
297 
298 	snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_CDCE);
299 	sysctl_ctx_init(&eth_ctx_list);
300 
301 	parent = SYSCTL_ADD_NODE(&eth_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(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
306 	    "vendor_id", CTLFLAG_RWTUN,
307 	    &usb_template_cdce.idVendor, 1, "Vendor identifier");
308 	SYSCTL_ADD_U16(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
309 	    "product_id", CTLFLAG_RWTUN,
310 	    &usb_template_cdce.idProduct, 1, "Product identifier");
311 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
312 	    "mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
313 	    &eth_mac, sizeof(eth_mac), usb_temp_sysctl,
314 	    "A", "MAC address string");
315 #if 0
316 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
317 	    "control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
318 	    &eth_control, sizeof(eth_control), usb_temp_sysctl,
319 	    "A", "Control interface string");
320 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
321 	    "data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
322 	    &eth_data, sizeof(eth_data), usb_temp_sysctl,
323 	    "A", "Data interface string");
324 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
325 	    "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
326 	    &eth_configuration, sizeof(eth_configuration), usb_temp_sysctl,
327 	    "A", "Configuration string");
328 #endif
329 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
330 	    "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
331 	    &eth_manufacturer, sizeof(eth_manufacturer), usb_temp_sysctl,
332 	    "A", "Manufacturer string");
333 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
334 	    "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
335 	    &eth_product, sizeof(eth_product), usb_temp_sysctl,
336 	    "A", "Product string");
337 	SYSCTL_ADD_PROC(&eth_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
338 	    "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
339 	    &eth_serial_number, sizeof(eth_serial_number), usb_temp_sysctl,
340 	    "A", "Serial number string");
341 }
342 
343 static void
344 eth_uninit(void *arg __unused)
345 {
346 
347 	sysctl_ctx_free(&eth_ctx_list);
348 }
349 
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);
352