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_CONFIGURATION_INDEX,
85 SERIALNET_MANUFACTURER_INDEX,
86 SERIALNET_PRODUCT_INDEX,
87 SERIALNET_SERIAL_NUMBER_INDEX,
91 #define SERIALNET_DEFAULT_MODEM "USB Modem Interface"
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 "The FreeBSD Project"
97 #define SERIALNET_DEFAULT_PRODUCT "SERIALNET"
98 #define SERIALNET_DEFAULT_SERIAL_NUMBER "January 2015"
100 static struct usb_string_descriptor serialnet_modem;
101 static struct usb_string_descriptor serialnet_eth_mac;
102 static struct usb_string_descriptor serialnet_eth_control;
103 static struct usb_string_descriptor serialnet_eth_data;
104 static struct usb_string_descriptor serialnet_configuration;
105 static struct usb_string_descriptor serialnet_manufacturer;
106 static struct usb_string_descriptor serialnet_product;
107 static struct usb_string_descriptor serialnet_serial_number;
109 static struct sysctl_ctx_list serialnet_ctx_list;
113 static usb_temp_get_string_desc_t serialnet_get_string_desc;
115 static const struct usb_cdc_union_descriptor eth_union_desc = {
116 .bLength = sizeof(eth_union_desc),
117 .bDescriptorType = UDESC_CS_INTERFACE,
118 .bDescriptorSubtype = UDESCSUB_CDC_UNION,
119 .bMasterInterface = 0, /* this is automatically updated */
120 .bSlaveInterface[0] = 1, /* this is automatically updated */
123 static const struct usb_cdc_header_descriptor eth_header_desc = {
124 .bLength = sizeof(eth_header_desc),
125 .bDescriptorType = UDESC_CS_INTERFACE,
126 .bDescriptorSubtype = UDESCSUB_CDC_HEADER,
131 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
132 .bLength = sizeof(eth_enf_desc),
133 .bDescriptorType = UDESC_CS_INTERFACE,
134 .bDescriptorSubtype = UDESCSUB_CDC_ENF,
135 .iMacAddress = SERIALNET_ETH_MAC_INDEX,
136 .bmEthernetStatistics = {0, 0, 0, 0},
137 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
138 .wNumberMCFilters = {0, 0},
139 .bNumberPowerFilters = 0,
142 static const void *eth_control_if_desc[] = {
149 static const struct usb_temp_packet_size bulk_mps = {
150 .mps[USB_SPEED_FULL] = 64,
151 .mps[USB_SPEED_HIGH] = 512,
154 static const struct usb_temp_packet_size intr_mps = {
155 .mps[USB_SPEED_FULL] = 8,
156 .mps[USB_SPEED_HIGH] = 8,
159 static const struct usb_temp_endpoint_desc bulk_in_ep = {
160 .pPacketSize = &bulk_mps,
161 #ifdef USB_HIP_IN_EP_0
162 .bEndpointAddress = USB_HIP_IN_EP_0,
164 .bEndpointAddress = UE_DIR_IN,
166 .bmAttributes = UE_BULK,
169 static const struct usb_temp_endpoint_desc bulk_out_ep = {
170 .pPacketSize = &bulk_mps,
171 #ifdef USB_HIP_OUT_EP_0
172 .bEndpointAddress = USB_HIP_OUT_EP_0,
174 .bEndpointAddress = UE_DIR_OUT,
176 .bmAttributes = UE_BULK,
179 static const struct usb_temp_endpoint_desc intr_in_ep = {
180 .pPacketSize = &intr_mps,
181 .bEndpointAddress = UE_DIR_IN,
182 .bmAttributes = UE_INTERRUPT,
185 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
190 static const struct usb_temp_interface_desc eth_control_interface = {
191 .ppEndpoints = eth_intr_endpoints,
192 .ppRawDesc = eth_control_if_desc,
193 .bInterfaceClass = UICLASS_CDC,
194 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
195 .bInterfaceProtocol = UIPROTO_CDC_NONE,
196 .iInterface = SERIALNET_ETH_CONTROL_INDEX,
199 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
205 static const struct usb_temp_interface_desc eth_data_null_interface = {
206 .ppEndpoints = NULL, /* no endpoints */
207 .bInterfaceClass = UICLASS_CDC_DATA,
208 .bInterfaceSubClass = UISUBCLASS_DATA,
209 .bInterfaceProtocol = 0,
210 .iInterface = SERIALNET_ETH_DATA_INDEX,
213 static const struct usb_temp_interface_desc eth_data_interface = {
214 .ppEndpoints = eth_data_endpoints,
215 .bInterfaceClass = UICLASS_CDC_DATA,
216 .bInterfaceSubClass = UISUBCLASS_DATA,
217 .bInterfaceProtocol = 0,
218 .iInterface = SERIALNET_ETH_DATA_INDEX,
219 .isAltInterface = 1, /* this is an alternate setting */
222 static const struct usb_temp_packet_size modem_bulk_mps = {
223 .mps[USB_SPEED_LOW] = 8,
224 .mps[USB_SPEED_FULL] = 64,
225 .mps[USB_SPEED_HIGH] = 512,
228 static const struct usb_temp_packet_size modem_intr_mps = {
229 .mps[USB_SPEED_LOW] = 8,
230 .mps[USB_SPEED_FULL] = 8,
231 .mps[USB_SPEED_HIGH] = 8,
234 static const struct usb_temp_interval modem_intr_interval = {
235 .bInterval[USB_SPEED_LOW] = 8, /* 8ms */
236 .bInterval[USB_SPEED_FULL] = 8, /* 8ms */
237 .bInterval[USB_SPEED_HIGH] = 7, /* 8ms */
240 static const struct usb_temp_endpoint_desc modem_ep_0 = {
241 .pPacketSize = &modem_intr_mps,
242 .pIntervals = &modem_intr_interval,
243 .bEndpointAddress = UE_DIR_IN,
244 .bmAttributes = UE_INTERRUPT,
247 static const struct usb_temp_endpoint_desc modem_ep_1 = {
248 .pPacketSize = &modem_bulk_mps,
249 .bEndpointAddress = UE_DIR_OUT,
250 .bmAttributes = UE_BULK,
253 static const struct usb_temp_endpoint_desc modem_ep_2 = {
254 .pPacketSize = &modem_bulk_mps,
255 .bEndpointAddress = UE_DIR_IN,
256 .bmAttributes = UE_BULK,
259 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
264 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
270 static const uint8_t modem_raw_desc_0[] = {
271 0x05, 0x24, 0x00, 0x10, 0x01
274 static const uint8_t modem_raw_desc_1[] = {
275 0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
278 static const uint8_t modem_raw_desc_2[] = {
279 0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
282 static const uint8_t modem_raw_desc_3[] = {
283 0x04, 0x24, 0x02, 0x07
286 static const void *modem_iface_0_desc[] = {
294 static const struct usb_temp_interface_desc modem_iface_0 = {
295 .ppRawDesc = modem_iface_0_desc,
296 .ppEndpoints = modem_iface_0_ep,
297 .bInterfaceClass = UICLASS_CDC,
298 .bInterfaceSubClass = UISUBCLASS_ABSTRACT_CONTROL_MODEL,
299 .bInterfaceProtocol = UIPROTO_CDC_AT,
300 .iInterface = SERIALNET_MODEM_INDEX,
303 static const struct usb_temp_interface_desc modem_iface_1 = {
304 .ppEndpoints = modem_iface_1_ep,
305 .bInterfaceClass = UICLASS_CDC_DATA,
306 .bInterfaceSubClass = UISUBCLASS_DATA,
307 .bInterfaceProtocol = 0,
308 .iInterface = SERIALNET_MODEM_INDEX,
311 static const struct usb_temp_interface_desc *serialnet_interfaces[] = {
314 ð_control_interface,
315 ð_data_null_interface,
320 static const struct usb_temp_config_desc serialnet_config_desc = {
321 .ppIfaceDesc = serialnet_interfaces,
322 .bmAttributes = UC_BUS_POWERED,
323 .bMaxPower = 25, /* 50 mA */
324 .iConfiguration = SERIALNET_CONFIGURATION_INDEX,
326 static const struct usb_temp_config_desc *serialnet_configs[] = {
327 &serialnet_config_desc,
331 struct usb_temp_device_desc usb_template_serialnet = {
332 .getStringDesc = &serialnet_get_string_desc,
333 .ppConfigDesc = serialnet_configs,
334 .idVendor = USB_TEMPLATE_VENDOR,
337 .bDeviceClass = UDCLASS_COMM,
338 .bDeviceSubClass = 0,
339 .bDeviceProtocol = 0,
340 .iManufacturer = SERIALNET_MANUFACTURER_INDEX,
341 .iProduct = SERIALNET_PRODUCT_INDEX,
342 .iSerialNumber = SERIALNET_SERIAL_NUMBER_INDEX,
345 /*------------------------------------------------------------------------*
346 * serialnet_get_string_desc
349 * NULL: Failure. No such string.
350 * Else: Success. Pointer to string descriptor is returned.
351 *------------------------------------------------------------------------*/
353 serialnet_get_string_desc(uint16_t lang_id, uint8_t string_index)
355 static const void *ptr[SERIALNET_MAX_INDEX] = {
356 [SERIALNET_LANG_INDEX] = &usb_string_lang_en,
357 [SERIALNET_MODEM_INDEX] = &serialnet_modem,
358 [SERIALNET_ETH_MAC_INDEX] = &serialnet_eth_mac,
359 [SERIALNET_ETH_CONTROL_INDEX] = &serialnet_eth_control,
360 [SERIALNET_ETH_DATA_INDEX] = &serialnet_eth_data,
361 [SERIALNET_CONFIGURATION_INDEX] = &serialnet_configuration,
362 [SERIALNET_MANUFACTURER_INDEX] = &serialnet_manufacturer,
363 [SERIALNET_PRODUCT_INDEX] = &serialnet_product,
364 [SERIALNET_SERIAL_NUMBER_INDEX] = &serialnet_serial_number,
367 if (string_index == 0) {
368 return (&usb_string_lang_en);
370 if (lang_id != 0x0409) {
373 if (string_index < SERIALNET_MAX_INDEX) {
374 return (ptr[string_index]);
380 serialnet_init(void *arg __unused)
382 struct sysctl_oid *parent;
385 usb_make_str_desc(&serialnet_modem, sizeof(serialnet_modem),
386 SERIALNET_DEFAULT_MODEM);
387 usb_make_str_desc(&serialnet_eth_mac, sizeof(serialnet_eth_mac),
388 SERIALNET_DEFAULT_ETH_MAC);
389 usb_make_str_desc(&serialnet_eth_control, sizeof(serialnet_eth_control),
390 SERIALNET_DEFAULT_ETH_CONTROL);
391 usb_make_str_desc(&serialnet_eth_data, sizeof(serialnet_eth_data),
392 SERIALNET_DEFAULT_ETH_DATA);
393 usb_make_str_desc(&serialnet_configuration, sizeof(serialnet_configuration),
394 SERIALNET_DEFAULT_CONFIGURATION);
395 usb_make_str_desc(&serialnet_manufacturer, sizeof(serialnet_manufacturer),
396 SERIALNET_DEFAULT_MANUFACTURER);
397 usb_make_str_desc(&serialnet_product, sizeof(serialnet_product),
398 SERIALNET_DEFAULT_PRODUCT);
399 usb_make_str_desc(&serialnet_serial_number, sizeof(serialnet_serial_number),
400 SERIALNET_DEFAULT_SERIAL_NUMBER);
402 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_SERIALNET);
403 sysctl_ctx_init(&serialnet_ctx_list);
405 parent = SYSCTL_ADD_NODE(&serialnet_ctx_list,
406 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
407 parent_name, CTLFLAG_RW,
408 0, "USB Mass Storage device side template");
409 SYSCTL_ADD_U16(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
410 "vendor_id", CTLFLAG_RWTUN,
411 &usb_template_serialnet.idVendor, 1, "Vendor identifier");
412 SYSCTL_ADD_U16(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
413 "product_id", CTLFLAG_RWTUN,
414 &usb_template_serialnet.idProduct, 1, "Product identifier");
415 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
416 "eth_mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
417 &serialnet_eth_mac, sizeof(serialnet_eth_mac), usb_temp_sysctl,
418 "A", "Ethernet MAC address string");
420 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
421 "modem", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
422 &serialnet_modem, sizeof(serialnet_modem), usb_temp_sysctl,
423 "A", "Modem interface string");
424 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
425 "eth_control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
426 &serialnet_eth_control, sizeof(serialnet_eth_data), usb_temp_sysctl,
427 "A", "Ethernet control interface string");
428 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
429 "eth_data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
430 &serialnet_eth_data, sizeof(serialnet_eth_data), usb_temp_sysctl,
431 "A", "Ethernet data interface string");
432 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
433 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
434 &serialnet_configuration, sizeof(serialnet_configuration), usb_temp_sysctl,
435 "A", "Configuration string");
437 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
438 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
439 &serialnet_manufacturer, sizeof(serialnet_manufacturer), usb_temp_sysctl,
440 "A", "Manufacturer string");
441 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
442 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
443 &serialnet_product, sizeof(serialnet_product), usb_temp_sysctl,
444 "A", "Product string");
445 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
446 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
447 &serialnet_serial_number, sizeof(serialnet_serial_number), usb_temp_sysctl,
448 "A", "Serial number string");
452 serialnet_uninit(void *arg __unused)
455 sysctl_ctx_free(&serialnet_ctx_list);
458 SYSINIT(serialnet_init, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_init, NULL);
459 SYSUNINIT(serialnet_init, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_uninit, NULL);