xref: /freebsd/sys/dev/usb/template/usb_template_serialnet.c (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
5  * Copyright (c) 2018 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * This software was developed by SRI International and the University of
9  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
10  * ("CTSRD"), as part of the DARPA CRASH research programme.
11  *
12  * Portions of this software were developed by Edward Tomasz Napierala
13  * under sponsorship from the FreeBSD Foundation.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 /*
37  * This file contains the USB template for USB Networking and Serial
38  */
39 
40 #include <sys/cdefs.h>
41 #ifdef USB_GLOBAL_INCLUDE_FILE
42 #include USB_GLOBAL_INCLUDE_FILE
43 #else
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/bus.h>
52 #include <sys/module.h>
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/sx.h>
58 #include <sys/unistd.h>
59 #include <sys/callout.h>
60 #include <sys/malloc.h>
61 #include <sys/priv.h>
62 
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usb_core.h>
66 #include <dev/usb/usb_cdc.h>
67 #include <dev/usb/usb_ioctl.h>
68 #include <dev/usb/usb_util.h>
69 
70 #include <dev/usb/template/usb_template.h>
71 #endif		/* USB_GLOBAL_INCLUDE_FILE */
72 
73 #define	MODEM_IFACE_0 0
74 #define	MODEM_IFACE_1 1
75 
76 enum {
77 	SERIALNET_LANG_INDEX,
78 	SERIALNET_MODEM_INDEX,
79 	SERIALNET_ETH_MAC_INDEX,
80 	SERIALNET_ETH_CONTROL_INDEX,
81 	SERIALNET_ETH_DATA_INDEX,
82 	SERIALNET_CONFIGURATION_INDEX,
83 	SERIALNET_MANUFACTURER_INDEX,
84 	SERIALNET_PRODUCT_INDEX,
85 	SERIALNET_SERIAL_NUMBER_INDEX,
86 	SERIALNET_MAX_INDEX,
87 };
88 
89 #define	SERIALNET_DEFAULT_VENDOR_ID	USB_TEMPLATE_VENDOR
90 #define	SERIALNET_DEFAULT_PRODUCT_ID	0x05dc
91 #define	SERIALNET_DEFAULT_MODEM		"Virtual serial port"
92 #define	SERIALNET_DEFAULT_ETH_MAC	"2A02030405060789AB"
93 #define	SERIALNET_DEFAULT_ETH_CONTROL	"USB Ethernet Comm Interface"
94 #define	SERIALNET_DEFAULT_ETH_DATA	"USB Ethernet Data Interface"
95 #define	SERIALNET_DEFAULT_CONFIGURATION	"Default configuration"
96 #define	SERIALNET_DEFAULT_MANUFACTURER	USB_TEMPLATE_MANUFACTURER
97 #define	SERIALNET_DEFAULT_PRODUCT	"Serial/Ethernet device"
98 /*
99  * The reason for this being called like this is that OSX
100  * derives the device node name from it, resulting in a somewhat
101  * user-friendly "/dev/cu.usbmodemFreeBSD1".  And yes, the "1"
102  * needs to be there, otherwise OSX will mangle it.
103  */
104 #define SERIALNET_DEFAULT_SERIAL_NUMBER	"FreeBSD1"
105 
106 static struct usb_string_descriptor	serialnet_modem;
107 static struct usb_string_descriptor	serialnet_eth_mac;
108 static struct usb_string_descriptor	serialnet_eth_control;
109 static struct usb_string_descriptor	serialnet_eth_data;
110 static struct usb_string_descriptor	serialnet_configuration;
111 static struct usb_string_descriptor	serialnet_manufacturer;
112 static struct usb_string_descriptor	serialnet_product;
113 static struct usb_string_descriptor	serialnet_serial_number;
114 
115 static struct sysctl_ctx_list		serialnet_ctx_list;
116 
117 /* prototypes */
118 
119 static usb_temp_get_string_desc_t serialnet_get_string_desc;
120 
121 static const struct usb_cdc_union_descriptor eth_union_desc = {
122 	.bLength = sizeof(eth_union_desc),
123 	.bDescriptorType = UDESC_CS_INTERFACE,
124 	.bDescriptorSubtype = UDESCSUB_CDC_UNION,
125 	.bMasterInterface = 0,		/* this is automatically updated */
126 	.bSlaveInterface[0] = 1,	/* this is automatically updated */
127 };
128 
129 static const struct usb_cdc_header_descriptor eth_header_desc = {
130 	.bLength = sizeof(eth_header_desc),
131 	.bDescriptorType = UDESC_CS_INTERFACE,
132 	.bDescriptorSubtype = UDESCSUB_CDC_HEADER,
133 	.bcdCDC[0] = 0x10,
134 	.bcdCDC[1] = 0x01,
135 };
136 
137 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
138 	.bLength = sizeof(eth_enf_desc),
139 	.bDescriptorType = UDESC_CS_INTERFACE,
140 	.bDescriptorSubtype = UDESCSUB_CDC_ENF,
141 	.iMacAddress = SERIALNET_ETH_MAC_INDEX,
142 	.bmEthernetStatistics = {0, 0, 0, 0},
143 	.wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
144 	.wNumberMCFilters = {0, 0},
145 	.bNumberPowerFilters = 0,
146 };
147 
148 static const void *eth_control_if_desc[] = {
149 	&eth_union_desc,
150 	&eth_header_desc,
151 	&eth_enf_desc,
152 	NULL,
153 };
154 
155 static const struct usb_temp_packet_size bulk_mps = {
156 	.mps[USB_SPEED_FULL] = 64,
157 	.mps[USB_SPEED_HIGH] = 512,
158 };
159 
160 static const struct usb_temp_packet_size intr_mps = {
161 	.mps[USB_SPEED_FULL] = 8,
162 	.mps[USB_SPEED_HIGH] = 8,
163 };
164 
165 static const struct usb_temp_endpoint_desc bulk_in_ep = {
166 	.pPacketSize = &bulk_mps,
167 #ifdef USB_HIP_IN_EP_0
168 	.bEndpointAddress = USB_HIP_IN_EP_0,
169 #else
170 	.bEndpointAddress = UE_DIR_IN,
171 #endif
172 	.bmAttributes = UE_BULK,
173 };
174 
175 static const struct usb_temp_endpoint_desc bulk_out_ep = {
176 	.pPacketSize = &bulk_mps,
177 #ifdef USB_HIP_OUT_EP_0
178 	.bEndpointAddress = USB_HIP_OUT_EP_0,
179 #else
180 	.bEndpointAddress = UE_DIR_OUT,
181 #endif
182 	.bmAttributes = UE_BULK,
183 };
184 
185 static const struct usb_temp_endpoint_desc intr_in_ep = {
186 	.pPacketSize = &intr_mps,
187 	.bEndpointAddress = UE_DIR_IN,
188 	.bmAttributes = UE_INTERRUPT,
189 };
190 
191 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
192 	&intr_in_ep,
193 	NULL,
194 };
195 
196 static const struct usb_temp_interface_desc eth_control_interface = {
197 	.ppEndpoints = eth_intr_endpoints,
198 	.ppRawDesc = eth_control_if_desc,
199 	.bInterfaceClass = UICLASS_CDC,
200 	.bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
201 	.bInterfaceProtocol = UIPROTO_CDC_NONE,
202 	.iInterface = SERIALNET_ETH_CONTROL_INDEX,
203 };
204 
205 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
206 	&bulk_in_ep,
207 	&bulk_out_ep,
208 	NULL,
209 };
210 
211 static const struct usb_temp_interface_desc eth_data_null_interface = {
212 	.ppEndpoints = NULL,		/* no endpoints */
213 	.bInterfaceClass = UICLASS_CDC_DATA,
214 	.bInterfaceSubClass = UISUBCLASS_DATA,
215 	.bInterfaceProtocol = 0,
216 	.iInterface = SERIALNET_ETH_DATA_INDEX,
217 };
218 
219 static const struct usb_temp_interface_desc eth_data_interface = {
220 	.ppEndpoints = eth_data_endpoints,
221 	.bInterfaceClass = UICLASS_CDC_DATA,
222 	.bInterfaceSubClass = UISUBCLASS_DATA,
223 	.bInterfaceProtocol = 0,
224 	.iInterface = SERIALNET_ETH_DATA_INDEX,
225 	.isAltInterface = 1,		/* this is an alternate setting */
226 };
227 
228 static const struct usb_temp_packet_size modem_bulk_mps = {
229 	.mps[USB_SPEED_LOW] = 8,
230 	.mps[USB_SPEED_FULL] = 64,
231 	.mps[USB_SPEED_HIGH] = 512,
232 };
233 
234 static const struct usb_temp_packet_size modem_intr_mps = {
235 	.mps[USB_SPEED_LOW] = 8,
236 	.mps[USB_SPEED_FULL] = 8,
237 	.mps[USB_SPEED_HIGH] = 8,
238 };
239 
240 static const struct usb_temp_interval modem_intr_interval = {
241 	.bInterval[USB_SPEED_LOW] = 8,	/* 8ms */
242 	.bInterval[USB_SPEED_FULL] = 8,	/* 8ms */
243 	.bInterval[USB_SPEED_HIGH] = 7,	/* 8ms */
244 };
245 
246 static const struct usb_temp_endpoint_desc modem_ep_0 = {
247 	.pPacketSize = &modem_intr_mps,
248 	.pIntervals = &modem_intr_interval,
249 	.bEndpointAddress = UE_DIR_IN,
250 	.bmAttributes = UE_INTERRUPT,
251 };
252 
253 static const struct usb_temp_endpoint_desc modem_ep_1 = {
254 	.pPacketSize = &modem_bulk_mps,
255 	.bEndpointAddress = UE_DIR_OUT,
256 	.bmAttributes = UE_BULK,
257 };
258 
259 static const struct usb_temp_endpoint_desc modem_ep_2 = {
260 	.pPacketSize = &modem_bulk_mps,
261 	.bEndpointAddress = UE_DIR_IN,
262 	.bmAttributes = UE_BULK,
263 };
264 
265 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
266 	&modem_ep_0,
267 	NULL,
268 };
269 
270 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
271 	&modem_ep_1,
272 	&modem_ep_2,
273 	NULL,
274 };
275 
276 static const uint8_t modem_raw_desc_0[] = {
277 	0x05, 0x24, 0x00, 0x10, 0x01
278 };
279 
280 static const uint8_t modem_raw_desc_1[] = {
281 	0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
282 };
283 
284 static const uint8_t modem_raw_desc_2[] = {
285 	0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
286 };
287 
288 static const uint8_t modem_raw_desc_3[] = {
289 	0x04, 0x24, 0x02, 0x07
290 };
291 
292 static const void *modem_iface_0_desc[] = {
293 	&modem_raw_desc_0,
294 	&modem_raw_desc_1,
295 	&modem_raw_desc_2,
296 	&modem_raw_desc_3,
297 	NULL,
298 };
299 
300 static const struct usb_temp_interface_desc modem_iface_0 = {
301 	.ppRawDesc = modem_iface_0_desc,
302 	.ppEndpoints = modem_iface_0_ep,
303 	.bInterfaceClass = UICLASS_CDC,
304 	.bInterfaceSubClass = UISUBCLASS_ABSTRACT_CONTROL_MODEL,
305 	.bInterfaceProtocol = UIPROTO_CDC_NONE,
306 	.iInterface = SERIALNET_MODEM_INDEX,
307 };
308 
309 static const struct usb_temp_interface_desc modem_iface_1 = {
310 	.ppEndpoints = modem_iface_1_ep,
311 	.bInterfaceClass = UICLASS_CDC_DATA,
312 	.bInterfaceSubClass = UISUBCLASS_DATA,
313 	.bInterfaceProtocol = 0,
314 	.iInterface = SERIALNET_MODEM_INDEX,
315 };
316 
317 static const struct usb_temp_interface_desc *serialnet_interfaces[] = {
318 	&modem_iface_0,
319 	&modem_iface_1,
320 	&eth_control_interface,
321 	&eth_data_null_interface,
322 	&eth_data_interface,
323 	NULL,
324 };
325 
326 static const struct usb_temp_config_desc serialnet_config_desc = {
327 	.ppIfaceDesc = serialnet_interfaces,
328 	.bmAttributes = 0,
329 	.bMaxPower = 0,
330 	.iConfiguration = SERIALNET_CONFIGURATION_INDEX,
331 };
332 static const struct usb_temp_config_desc *serialnet_configs[] = {
333 	&serialnet_config_desc,
334 	NULL,
335 };
336 
337 struct usb_temp_device_desc usb_template_serialnet = {
338 	.getStringDesc = &serialnet_get_string_desc,
339 	.ppConfigDesc = serialnet_configs,
340 	.idVendor = SERIALNET_DEFAULT_VENDOR_ID,
341 	.idProduct = SERIALNET_DEFAULT_PRODUCT_ID,
342 	.bcdDevice = 0x0100,
343 	.bDeviceClass = UDCLASS_IN_INTERFACE,
344 	.bDeviceSubClass = 0,
345 	.bDeviceProtocol = 0,
346 	.iManufacturer = SERIALNET_MANUFACTURER_INDEX,
347 	.iProduct = SERIALNET_PRODUCT_INDEX,
348 	.iSerialNumber = SERIALNET_SERIAL_NUMBER_INDEX,
349 };
350 
351 /*------------------------------------------------------------------------*
352  *	serialnet_get_string_desc
353  *
354  * Return values:
355  * NULL: Failure. No such string.
356  * Else: Success. Pointer to string descriptor is returned.
357  *------------------------------------------------------------------------*/
358 static const void *
359 serialnet_get_string_desc(uint16_t lang_id, uint8_t string_index)
360 {
361 	static const void *ptr[SERIALNET_MAX_INDEX] = {
362 		[SERIALNET_LANG_INDEX] = &usb_string_lang_en,
363 		[SERIALNET_MODEM_INDEX] = &serialnet_modem,
364 		[SERIALNET_ETH_MAC_INDEX] = &serialnet_eth_mac,
365 		[SERIALNET_ETH_CONTROL_INDEX] = &serialnet_eth_control,
366 		[SERIALNET_ETH_DATA_INDEX] = &serialnet_eth_data,
367 		[SERIALNET_CONFIGURATION_INDEX] = &serialnet_configuration,
368 		[SERIALNET_MANUFACTURER_INDEX] = &serialnet_manufacturer,
369 		[SERIALNET_PRODUCT_INDEX] = &serialnet_product,
370 		[SERIALNET_SERIAL_NUMBER_INDEX] = &serialnet_serial_number,
371 	};
372 
373 	if (string_index == 0) {
374 		return (&usb_string_lang_en);
375 	}
376 	if (lang_id != 0x0409) {
377 		return (NULL);
378 	}
379 	if (string_index < SERIALNET_MAX_INDEX) {
380 		return (ptr[string_index]);
381 	}
382 	return (NULL);
383 }
384 
385 static void
386 serialnet_init(void *arg __unused)
387 {
388 	struct sysctl_oid *parent;
389 	char parent_name[3];
390 
391 	usb_make_str_desc(&serialnet_modem, sizeof(serialnet_modem),
392 	    SERIALNET_DEFAULT_MODEM);
393 	usb_make_str_desc(&serialnet_eth_mac, sizeof(serialnet_eth_mac),
394 	    SERIALNET_DEFAULT_ETH_MAC);
395 	usb_make_str_desc(&serialnet_eth_control, sizeof(serialnet_eth_control),
396 	    SERIALNET_DEFAULT_ETH_CONTROL);
397 	usb_make_str_desc(&serialnet_eth_data, sizeof(serialnet_eth_data),
398 	    SERIALNET_DEFAULT_ETH_DATA);
399 	usb_make_str_desc(&serialnet_configuration, sizeof(serialnet_configuration),
400 	    SERIALNET_DEFAULT_CONFIGURATION);
401 	usb_make_str_desc(&serialnet_manufacturer, sizeof(serialnet_manufacturer),
402 	    SERIALNET_DEFAULT_MANUFACTURER);
403 	usb_make_str_desc(&serialnet_product, sizeof(serialnet_product),
404 	    SERIALNET_DEFAULT_PRODUCT);
405 	usb_make_str_desc(&serialnet_serial_number, sizeof(serialnet_serial_number),
406 	    SERIALNET_DEFAULT_SERIAL_NUMBER);
407 
408 	snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_SERIALNET);
409 	sysctl_ctx_init(&serialnet_ctx_list);
410 
411 	parent = SYSCTL_ADD_NODE(&serialnet_ctx_list,
412 	    SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
413 	    parent_name, CTLFLAG_RW | CTLFLAG_MPSAFE,
414 	    0, "USB CDC Serial/Ethernet device side template");
415 	SYSCTL_ADD_U16(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
416 	    "vendor_id", CTLFLAG_RWTUN,
417 	    &usb_template_serialnet.idVendor, 1, "Vendor identifier");
418 	SYSCTL_ADD_U16(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
419 	    "product_id", CTLFLAG_RWTUN,
420 	    &usb_template_serialnet.idProduct, 1, "Product identifier");
421 	SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
422 	    "eth_mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
423 	    &serialnet_eth_mac, sizeof(serialnet_eth_mac), usb_temp_sysctl,
424 	    "A", "Ethernet MAC address string");
425 #if 0
426 	SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
427 	    "modem", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
428 	    &serialnet_modem, sizeof(serialnet_modem), usb_temp_sysctl,
429 	    "A", "Modem interface string");
430 	SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
431 	    "eth_control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
432 	    &serialnet_eth_control, sizeof(serialnet_eth_data), usb_temp_sysctl,
433 	    "A", "Ethernet control interface string");
434 	SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
435 	    "eth_data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
436 	    &serialnet_eth_data, sizeof(serialnet_eth_data), usb_temp_sysctl,
437 	    "A", "Ethernet data interface string");
438 	SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
439 	    "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
440 	    &serialnet_configuration, sizeof(serialnet_configuration), usb_temp_sysctl,
441 	    "A", "Configuration string");
442 #endif
443 	SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
444 	    "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
445 	    &serialnet_manufacturer, sizeof(serialnet_manufacturer), usb_temp_sysctl,
446 	    "A", "Manufacturer string");
447 	SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
448 	    "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
449 	    &serialnet_product, sizeof(serialnet_product), usb_temp_sysctl,
450 	    "A", "Product string");
451 	SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
452 	    "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
453 	    &serialnet_serial_number, sizeof(serialnet_serial_number), usb_temp_sysctl,
454 	    "A", "Serial number string");
455 }
456 
457 static void
458 serialnet_uninit(void *arg __unused)
459 {
460 
461 	sysctl_ctx_free(&serialnet_ctx_list);
462 }
463 
464 SYSINIT(serialnet_init, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_init, NULL);
465 SYSUNINIT(serialnet_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_uninit, NULL);
466