2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
5 * Copyright (c) 2018 The FreeBSD Foundation
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.
12 * Portions of this software were developed by Edward Tomasz Napierala
13 * under sponsorship from the FreeBSD Foundation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
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.
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
37 * This file contains the USB template for USB Networking and Serial
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #ifdef USB_GLOBAL_INCLUDE_FILE
44 #include USB_GLOBAL_INCLUDE_FILE
46 #include <sys/stdint.h>
47 #include <sys/stddef.h>
48 #include <sys/param.h>
49 #include <sys/queue.h>
50 #include <sys/types.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
54 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/sysctl.h>
60 #include <sys/unistd.h>
61 #include <sys/callout.h>
62 #include <sys/malloc.h>
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usb_core.h>
68 #include <dev/usb/usb_cdc.h>
69 #include <dev/usb/usb_ioctl.h>
70 #include <dev/usb/usb_util.h>
72 #include <dev/usb/template/usb_template.h>
73 #endif /* USB_GLOBAL_INCLUDE_FILE */
75 #define MODEM_IFACE_0 0
76 #define MODEM_IFACE_1 1
80 SERIALNET_MODEM_INDEX,
81 SERIALNET_ETH_MAC_INDEX,
82 SERIALNET_ETH_CONTROL_INDEX,
83 SERIALNET_ETH_DATA_INDEX,
84 SERIALNET_ETH_CONFIG_INDEX,
85 SERIALNET_CONFIGURATION_INDEX,
86 SERIALNET_MANUFACTURER_INDEX,
87 SERIALNET_PRODUCT_INDEX,
88 SERIALNET_SERIAL_NUMBER_INDEX,
92 #define SERIALNET_DEFAULT_MODEM "USB Modem Interface"
93 #define SERIALNET_DEFAULT_ETH_MAC "2A02030405060789AB"
94 #define SERIALNET_DEFAULT_ETH_CONTROL "USB Ethernet Comm Interface"
95 #define SERIALNET_DEFAULT_ETH_DATA "USB Ethernet Data Interface"
96 #define SERIALNET_DEFAULT_CONFIGURATION "Default configuration"
97 #define SERIALNET_DEFAULT_MANUFACTURER "The FreeBSD Project"
98 #define SERIALNET_DEFAULT_PRODUCT "SERIALNET"
99 #define SERIALNET_DEFAULT_SERIAL_NUMBER "January 2015"
101 static struct usb_string_descriptor serialnet_modem;
102 static struct usb_string_descriptor serialnet_eth_mac;
103 static struct usb_string_descriptor serialnet_eth_control;
104 static struct usb_string_descriptor serialnet_eth_data;
105 static struct usb_string_descriptor serialnet_configuration;
106 static struct usb_string_descriptor serialnet_manufacturer;
107 static struct usb_string_descriptor serialnet_product;
108 static struct usb_string_descriptor serialnet_serial_number;
110 static struct sysctl_ctx_list serialnet_ctx_list;
114 static usb_temp_get_string_desc_t serialnet_get_string_desc;
116 static const struct usb_cdc_union_descriptor eth_union_desc = {
117 .bLength = sizeof(eth_union_desc),
118 .bDescriptorType = UDESC_CS_INTERFACE,
119 .bDescriptorSubtype = UDESCSUB_CDC_UNION,
120 .bMasterInterface = 0, /* this is automatically updated */
121 .bSlaveInterface[0] = 1, /* this is automatically updated */
124 static const struct usb_cdc_header_descriptor eth_header_desc = {
125 .bLength = sizeof(eth_header_desc),
126 .bDescriptorType = UDESC_CS_INTERFACE,
127 .bDescriptorSubtype = UDESCSUB_CDC_HEADER,
132 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
133 .bLength = sizeof(eth_enf_desc),
134 .bDescriptorType = UDESC_CS_INTERFACE,
135 .bDescriptorSubtype = UDESCSUB_CDC_ENF,
136 .iMacAddress = SERIALNET_ETH_MAC_INDEX,
137 .bmEthernetStatistics = {0, 0, 0, 0},
138 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
139 .wNumberMCFilters = {0, 0},
140 .bNumberPowerFilters = 0,
143 static const void *eth_control_if_desc[] = {
150 static const struct usb_temp_packet_size bulk_mps = {
151 .mps[USB_SPEED_FULL] = 64,
152 .mps[USB_SPEED_HIGH] = 512,
155 static const struct usb_temp_packet_size intr_mps = {
156 .mps[USB_SPEED_FULL] = 8,
157 .mps[USB_SPEED_HIGH] = 8,
160 static const struct usb_temp_endpoint_desc bulk_in_ep = {
161 .pPacketSize = &bulk_mps,
162 #ifdef USB_HIP_IN_EP_0
163 .bEndpointAddress = USB_HIP_IN_EP_0,
165 .bEndpointAddress = UE_DIR_IN,
167 .bmAttributes = UE_BULK,
170 static const struct usb_temp_endpoint_desc bulk_out_ep = {
171 .pPacketSize = &bulk_mps,
172 #ifdef USB_HIP_OUT_EP_0
173 .bEndpointAddress = USB_HIP_OUT_EP_0,
175 .bEndpointAddress = UE_DIR_OUT,
177 .bmAttributes = UE_BULK,
180 static const struct usb_temp_endpoint_desc intr_in_ep = {
181 .pPacketSize = &intr_mps,
182 .bEndpointAddress = UE_DIR_IN,
183 .bmAttributes = UE_INTERRUPT,
186 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
191 static const struct usb_temp_interface_desc eth_control_interface = {
192 .ppEndpoints = eth_intr_endpoints,
193 .ppRawDesc = eth_control_if_desc,
194 .bInterfaceClass = UICLASS_CDC,
195 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
196 .bInterfaceProtocol = UIPROTO_CDC_NONE,
197 .iInterface = SERIALNET_ETH_CONTROL_INDEX,
200 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
206 static const struct usb_temp_interface_desc eth_data_null_interface = {
207 .ppEndpoints = NULL, /* no endpoints */
208 .bInterfaceClass = UICLASS_CDC_DATA,
209 .bInterfaceSubClass = UISUBCLASS_DATA,
210 .bInterfaceProtocol = 0,
211 .iInterface = SERIALNET_ETH_DATA_INDEX,
214 static const struct usb_temp_interface_desc eth_data_interface = {
215 .ppEndpoints = eth_data_endpoints,
216 .bInterfaceClass = UICLASS_CDC_DATA,
217 .bInterfaceSubClass = UISUBCLASS_DATA,
218 .bInterfaceProtocol = 0,
219 .iInterface = SERIALNET_ETH_DATA_INDEX,
220 .isAltInterface = 1, /* this is an alternate setting */
223 static const struct usb_temp_packet_size modem_bulk_mps = {
224 .mps[USB_SPEED_LOW] = 8,
225 .mps[USB_SPEED_FULL] = 64,
226 .mps[USB_SPEED_HIGH] = 512,
229 static const struct usb_temp_packet_size modem_intr_mps = {
230 .mps[USB_SPEED_LOW] = 8,
231 .mps[USB_SPEED_FULL] = 8,
232 .mps[USB_SPEED_HIGH] = 8,
235 static const struct usb_temp_interval modem_intr_interval = {
236 .bInterval[USB_SPEED_LOW] = 8, /* 8ms */
237 .bInterval[USB_SPEED_FULL] = 8, /* 8ms */
238 .bInterval[USB_SPEED_HIGH] = 7, /* 8ms */
241 static const struct usb_temp_endpoint_desc modem_ep_0 = {
242 .pPacketSize = &modem_intr_mps,
243 .pIntervals = &modem_intr_interval,
244 .bEndpointAddress = UE_DIR_IN,
245 .bmAttributes = UE_INTERRUPT,
248 static const struct usb_temp_endpoint_desc modem_ep_1 = {
249 .pPacketSize = &modem_bulk_mps,
250 .bEndpointAddress = UE_DIR_OUT,
251 .bmAttributes = UE_BULK,
254 static const struct usb_temp_endpoint_desc modem_ep_2 = {
255 .pPacketSize = &modem_bulk_mps,
256 .bEndpointAddress = UE_DIR_IN,
257 .bmAttributes = UE_BULK,
260 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
265 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
271 static const uint8_t modem_raw_desc_0[] = {
272 0x05, 0x24, 0x00, 0x10, 0x01
275 static const uint8_t modem_raw_desc_1[] = {
276 0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
279 static const uint8_t modem_raw_desc_2[] = {
280 0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
283 static const uint8_t modem_raw_desc_3[] = {
284 0x04, 0x24, 0x02, 0x07
287 static const void *modem_iface_0_desc[] = {
295 static const struct usb_temp_interface_desc modem_iface_0 = {
296 .ppRawDesc = modem_iface_0_desc,
297 .ppEndpoints = modem_iface_0_ep,
298 .bInterfaceClass = UICLASS_CDC,
299 .bInterfaceSubClass = UISUBCLASS_ABSTRACT_CONTROL_MODEL,
300 .bInterfaceProtocol = UIPROTO_CDC_AT,
301 .iInterface = SERIALNET_MODEM_INDEX,
304 static const struct usb_temp_interface_desc modem_iface_1 = {
305 .ppEndpoints = modem_iface_1_ep,
306 .bInterfaceClass = UICLASS_CDC_DATA,
307 .bInterfaceSubClass = UISUBCLASS_DATA,
308 .bInterfaceProtocol = 0,
309 .iInterface = SERIALNET_MODEM_INDEX,
312 static const struct usb_temp_interface_desc *serialnet_interfaces[] = {
315 ð_control_interface,
316 ð_data_null_interface,
321 static const struct usb_temp_config_desc serialnet_config_desc = {
322 .ppIfaceDesc = serialnet_interfaces,
323 .bmAttributes = UC_BUS_POWERED,
324 .bMaxPower = 25, /* 50 mA */
325 .iConfiguration = SERIALNET_CONFIGURATION_INDEX,
327 static const struct usb_temp_config_desc *serialnet_configs[] = {
328 &serialnet_config_desc,
332 struct usb_temp_device_desc usb_template_serialnet = {
333 .getStringDesc = &serialnet_get_string_desc,
334 .ppConfigDesc = serialnet_configs,
335 .idVendor = USB_TEMPLATE_VENDOR,
338 .bDeviceClass = UDCLASS_COMM,
339 .bDeviceSubClass = 0,
340 .bDeviceProtocol = 0,
341 .iManufacturer = SERIALNET_MANUFACTURER_INDEX,
342 .iProduct = SERIALNET_PRODUCT_INDEX,
343 .iSerialNumber = SERIALNET_SERIAL_NUMBER_INDEX,
346 /*------------------------------------------------------------------------*
347 * serialnet_get_string_desc
350 * NULL: Failure. No such string.
351 * Else: Success. Pointer to string descriptor is returned.
352 *------------------------------------------------------------------------*/
354 serialnet_get_string_desc(uint16_t lang_id, uint8_t string_index)
356 static const void *ptr[SERIALNET_MAX_INDEX] = {
357 [SERIALNET_LANG_INDEX] = &usb_string_lang_en,
358 [SERIALNET_MODEM_INDEX] = &serialnet_modem,
359 [SERIALNET_ETH_MAC_INDEX] = &serialnet_eth_mac,
360 [SERIALNET_ETH_CONTROL_INDEX] = &serialnet_eth_control,
361 [SERIALNET_ETH_DATA_INDEX] = &serialnet_eth_data,
362 [SERIALNET_CONFIGURATION_INDEX] = &serialnet_configuration,
363 [SERIALNET_MANUFACTURER_INDEX] = &serialnet_manufacturer,
364 [SERIALNET_PRODUCT_INDEX] = &serialnet_product,
365 [SERIALNET_SERIAL_NUMBER_INDEX] = &serialnet_serial_number,
368 if (string_index == 0) {
369 return (&usb_string_lang_en);
371 if (lang_id != 0x0409) {
374 if (string_index < SERIALNET_MAX_INDEX) {
375 return (ptr[string_index]);
381 serialnet_init(void *arg __unused)
383 struct sysctl_oid *parent;
386 usb_make_str_desc(&serialnet_modem, sizeof(serialnet_modem),
387 SERIALNET_DEFAULT_MODEM);
388 usb_make_str_desc(&serialnet_eth_mac, sizeof(serialnet_eth_mac),
389 SERIALNET_DEFAULT_ETH_MAC);
390 usb_make_str_desc(&serialnet_eth_control, sizeof(serialnet_eth_control),
391 SERIALNET_DEFAULT_ETH_CONTROL);
392 usb_make_str_desc(&serialnet_eth_data, sizeof(serialnet_eth_data),
393 SERIALNET_DEFAULT_ETH_DATA);
394 usb_make_str_desc(&serialnet_configuration, sizeof(serialnet_configuration),
395 SERIALNET_DEFAULT_CONFIGURATION);
396 usb_make_str_desc(&serialnet_manufacturer, sizeof(serialnet_manufacturer),
397 SERIALNET_DEFAULT_MANUFACTURER);
398 usb_make_str_desc(&serialnet_product, sizeof(serialnet_product),
399 SERIALNET_DEFAULT_PRODUCT);
400 usb_make_str_desc(&serialnet_serial_number, sizeof(serialnet_serial_number),
401 SERIALNET_DEFAULT_SERIAL_NUMBER);
403 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_SERIALNET);
404 sysctl_ctx_init(&serialnet_ctx_list);
406 parent = SYSCTL_ADD_NODE(&serialnet_ctx_list,
407 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
408 parent_name, CTLFLAG_RW,
409 0, "USB Mass Storage device side template");
410 SYSCTL_ADD_U16(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
411 "vendor_id", CTLFLAG_RWTUN,
412 &usb_template_serialnet.idVendor, 1, "Vendor identifier");
413 SYSCTL_ADD_U16(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
414 "product_id", CTLFLAG_RWTUN,
415 &usb_template_serialnet.idProduct, 1, "Product identifier");
416 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
417 "eth_mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
418 &serialnet_eth_mac, sizeof(serialnet_eth_mac), usb_temp_sysctl,
419 "A", "Ethernet MAC address string");
421 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
422 "modem", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
423 &serialnet_modem, sizeof(serialnet_modem), usb_temp_sysctl,
424 "A", "Modem interface string");
425 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
426 "eth_control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
427 &serialnet_eth_control, sizeof(serialnet_eth_data), usb_temp_sysctl,
428 "A", "Ethernet control interface string");
429 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
430 "eth_data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
431 &serialnet_eth_data, sizeof(serialnet_eth_data), usb_temp_sysctl,
432 "A", "Ethernet data interface string");
433 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
434 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
435 &serialnet_configuration, sizeof(serialnet_configuration), usb_temp_sysctl,
436 "A", "Configuration string");
438 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
439 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
440 &serialnet_manufacturer, sizeof(serialnet_manufacturer), usb_temp_sysctl,
441 "A", "Manufacturer string");
442 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
443 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
444 &serialnet_product, sizeof(serialnet_product), usb_temp_sysctl,
445 "A", "Product string");
446 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
447 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
448 &serialnet_serial_number, sizeof(serialnet_serial_number), usb_temp_sysctl,
449 "A", "Serial number string");
453 serialnet_uninit(void *arg __unused)
456 sysctl_ctx_free(&serialnet_ctx_list);
459 SYSINIT(serialnet_init, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_init, NULL);
460 SYSUNINIT(serialnet_init, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_uninit, NULL);