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 * USB template for CDC ACM (serial), CDC ECM (network), and CDC MSC (storage).
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
82 MULTI_ETH_CONTROL_INDEX,
85 MULTI_CONFIGURATION_INDEX,
86 MULTI_MANUFACTURER_INDEX,
88 MULTI_SERIAL_NUMBER_INDEX,
92 #define MULTI_DEFAULT_VENDOR_ID USB_TEMPLATE_VENDOR
93 #define MULTI_DEFAULT_PRODUCT_ID 0x05dc
94 #define MULTI_DEFAULT_MODEM "Virtual serial port"
95 #define MULTI_DEFAULT_ETH_MAC "2A02030405060789AB"
96 #define MULTI_DEFAULT_ETH_CONTROL "Ethernet Comm Interface"
97 #define MULTI_DEFAULT_ETH_DATA "Ethernet Data Interface"
98 #define MULTI_DEFAULT_STORAGE "Mass Storage Interface"
99 #define MULTI_DEFAULT_CONFIGURATION "Default configuration"
100 #define MULTI_DEFAULT_MANUFACTURER USB_TEMPLATE_MANUFACTURER
101 #define MULTI_DEFAULT_PRODUCT "Multifunction Device"
103 * The reason for this being called like this is that OSX
104 * derives the device node name from it, resulting in a somewhat
105 * user-friendly "/dev/cu.usbmodemFreeBSD1". And yes, the "1"
106 * needs to be there, otherwise OSX will mangle it.
108 #define MULTI_DEFAULT_SERIAL_NUMBER "FreeBSD1"
110 static struct usb_string_descriptor multi_modem;
111 static struct usb_string_descriptor multi_eth_mac;
112 static struct usb_string_descriptor multi_eth_control;
113 static struct usb_string_descriptor multi_eth_data;
114 static struct usb_string_descriptor multi_storage;
115 static struct usb_string_descriptor multi_configuration;
116 static struct usb_string_descriptor multi_manufacturer;
117 static struct usb_string_descriptor multi_product;
118 static struct usb_string_descriptor multi_serial_number;
120 static struct sysctl_ctx_list multi_ctx_list;
124 static usb_temp_get_string_desc_t multi_get_string_desc;
126 static const struct usb_cdc_union_descriptor eth_union_desc = {
127 .bLength = sizeof(eth_union_desc),
128 .bDescriptorType = UDESC_CS_INTERFACE,
129 .bDescriptorSubtype = UDESCSUB_CDC_UNION,
130 .bMasterInterface = 0, /* this is automatically updated */
131 .bSlaveInterface[0] = 1, /* this is automatically updated */
134 static const struct usb_cdc_header_descriptor eth_header_desc = {
135 .bLength = sizeof(eth_header_desc),
136 .bDescriptorType = UDESC_CS_INTERFACE,
137 .bDescriptorSubtype = UDESCSUB_CDC_HEADER,
142 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
143 .bLength = sizeof(eth_enf_desc),
144 .bDescriptorType = UDESC_CS_INTERFACE,
145 .bDescriptorSubtype = UDESCSUB_CDC_ENF,
146 .iMacAddress = MULTI_ETH_MAC_INDEX,
147 .bmEthernetStatistics = {0, 0, 0, 0},
148 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
149 .wNumberMCFilters = {0, 0},
150 .bNumberPowerFilters = 0,
153 static const void *eth_control_if_desc[] = {
160 static const struct usb_temp_packet_size bulk_mps = {
161 .mps[USB_SPEED_FULL] = 64,
162 .mps[USB_SPEED_HIGH] = 512,
165 static const struct usb_temp_packet_size intr_mps = {
166 .mps[USB_SPEED_FULL] = 8,
167 .mps[USB_SPEED_HIGH] = 8,
170 static const struct usb_temp_endpoint_desc bulk_in_ep = {
171 .pPacketSize = &bulk_mps,
172 #ifdef USB_HIP_IN_EP_0
173 .bEndpointAddress = USB_HIP_IN_EP_0,
175 .bEndpointAddress = UE_DIR_IN,
177 .bmAttributes = UE_BULK,
180 static const struct usb_temp_endpoint_desc bulk_out_ep = {
181 .pPacketSize = &bulk_mps,
182 #ifdef USB_HIP_OUT_EP_0
183 .bEndpointAddress = USB_HIP_OUT_EP_0,
185 .bEndpointAddress = UE_DIR_OUT,
187 .bmAttributes = UE_BULK,
190 static const struct usb_temp_endpoint_desc intr_in_ep = {
191 .pPacketSize = &intr_mps,
192 .bEndpointAddress = UE_DIR_IN,
193 .bmAttributes = UE_INTERRUPT,
196 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
201 static const struct usb_temp_interface_desc eth_control_interface = {
202 .ppEndpoints = eth_intr_endpoints,
203 .ppRawDesc = eth_control_if_desc,
204 .bInterfaceClass = UICLASS_CDC,
205 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
206 .bInterfaceProtocol = UIPROTO_CDC_NONE,
207 .iInterface = MULTI_ETH_CONTROL_INDEX,
210 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
216 static const struct usb_temp_interface_desc eth_data_null_interface = {
217 .ppEndpoints = NULL, /* no endpoints */
218 .bInterfaceClass = UICLASS_CDC_DATA,
219 .bInterfaceSubClass = UISUBCLASS_DATA,
220 .bInterfaceProtocol = 0,
221 .iInterface = MULTI_ETH_DATA_INDEX,
224 static const struct usb_temp_interface_desc eth_data_interface = {
225 .ppEndpoints = eth_data_endpoints,
226 .bInterfaceClass = UICLASS_CDC_DATA,
227 .bInterfaceSubClass = UISUBCLASS_DATA,
228 .bInterfaceProtocol = 0,
229 .iInterface = MULTI_ETH_DATA_INDEX,
230 .isAltInterface = 1, /* this is an alternate setting */
233 static const struct usb_temp_packet_size modem_bulk_mps = {
234 .mps[USB_SPEED_LOW] = 8,
235 .mps[USB_SPEED_FULL] = 64,
236 .mps[USB_SPEED_HIGH] = 512,
239 static const struct usb_temp_packet_size modem_intr_mps = {
240 .mps[USB_SPEED_LOW] = 8,
241 .mps[USB_SPEED_FULL] = 8,
242 .mps[USB_SPEED_HIGH] = 8,
245 static const struct usb_temp_interval modem_intr_interval = {
246 .bInterval[USB_SPEED_LOW] = 8, /* 8ms */
247 .bInterval[USB_SPEED_FULL] = 8, /* 8ms */
248 .bInterval[USB_SPEED_HIGH] = 7, /* 8ms */
251 static const struct usb_temp_endpoint_desc modem_ep_0 = {
252 .pPacketSize = &modem_intr_mps,
253 .pIntervals = &modem_intr_interval,
254 .bEndpointAddress = UE_DIR_IN,
255 .bmAttributes = UE_INTERRUPT,
258 static const struct usb_temp_endpoint_desc modem_ep_1 = {
259 .pPacketSize = &modem_bulk_mps,
260 .bEndpointAddress = UE_DIR_OUT,
261 .bmAttributes = UE_BULK,
264 static const struct usb_temp_endpoint_desc modem_ep_2 = {
265 .pPacketSize = &modem_bulk_mps,
266 .bEndpointAddress = UE_DIR_IN,
267 .bmAttributes = UE_BULK,
270 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
275 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
281 static const uint8_t modem_raw_desc_0[] = {
282 0x05, 0x24, 0x00, 0x10, 0x01
285 static const uint8_t modem_raw_desc_1[] = {
286 0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
289 static const uint8_t modem_raw_desc_2[] = {
290 0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
293 static const uint8_t modem_raw_desc_3[] = {
294 0x04, 0x24, 0x02, 0x07
297 static const void *modem_iface_0_desc[] = {
305 static const struct usb_temp_interface_desc modem_iface_0 = {
306 .ppRawDesc = modem_iface_0_desc,
307 .ppEndpoints = modem_iface_0_ep,
308 .bInterfaceClass = UICLASS_CDC,
309 .bInterfaceSubClass = UISUBCLASS_ABSTRACT_CONTROL_MODEL,
310 .bInterfaceProtocol = UIPROTO_CDC_NONE,
311 .iInterface = MULTI_MODEM_INDEX,
314 static const struct usb_temp_interface_desc modem_iface_1 = {
315 .ppEndpoints = modem_iface_1_ep,
316 .bInterfaceClass = UICLASS_CDC_DATA,
317 .bInterfaceSubClass = UISUBCLASS_DATA,
318 .bInterfaceProtocol = 0,
319 .iInterface = MULTI_MODEM_INDEX,
322 static const struct usb_temp_packet_size msc_bulk_mps = {
323 .mps[USB_SPEED_FULL] = 64,
324 .mps[USB_SPEED_HIGH] = 512,
327 static const struct usb_temp_endpoint_desc msc_bulk_in_ep = {
328 .pPacketSize = &msc_bulk_mps,
329 #ifdef USB_HIP_IN_EP_0
330 .bEndpointAddress = USB_HIP_IN_EP_0,
332 .bEndpointAddress = UE_DIR_IN,
334 .bmAttributes = UE_BULK,
337 static const struct usb_temp_endpoint_desc msc_bulk_out_ep = {
338 .pPacketSize = &msc_bulk_mps,
339 #ifdef USB_HIP_OUT_EP_0
340 .bEndpointAddress = USB_HIP_OUT_EP_0,
342 .bEndpointAddress = UE_DIR_OUT,
344 .bmAttributes = UE_BULK,
347 static const struct usb_temp_endpoint_desc *msc_data_endpoints[] = {
353 static const struct usb_temp_interface_desc msc_data_interface = {
354 .ppEndpoints = msc_data_endpoints,
355 .bInterfaceClass = UICLASS_MASS,
356 .bInterfaceSubClass = UISUBCLASS_SCSI,
357 .bInterfaceProtocol = UIPROTO_MASS_BBB,
358 .iInterface = MULTI_STORAGE_INDEX,
361 static const struct usb_temp_interface_desc *multi_interfaces[] = {
364 ð_control_interface,
365 ð_data_null_interface,
371 static const struct usb_temp_config_desc multi_config_desc = {
372 .ppIfaceDesc = multi_interfaces,
375 .iConfiguration = MULTI_CONFIGURATION_INDEX,
377 static const struct usb_temp_config_desc *multi_configs[] = {
382 struct usb_temp_device_desc usb_template_multi = {
383 .getStringDesc = &multi_get_string_desc,
384 .ppConfigDesc = multi_configs,
385 .idVendor = MULTI_DEFAULT_VENDOR_ID,
386 .idProduct = MULTI_DEFAULT_PRODUCT_ID,
388 .bDeviceClass = UDCLASS_IN_INTERFACE,
389 .bDeviceSubClass = 0,
390 .bDeviceProtocol = 0,
391 .iManufacturer = MULTI_MANUFACTURER_INDEX,
392 .iProduct = MULTI_PRODUCT_INDEX,
393 .iSerialNumber = MULTI_SERIAL_NUMBER_INDEX,
396 /*------------------------------------------------------------------------*
397 * multi_get_string_desc
400 * NULL: Failure. No such string.
401 * Else: Success. Pointer to string descriptor is returned.
402 *------------------------------------------------------------------------*/
404 multi_get_string_desc(uint16_t lang_id, uint8_t string_index)
406 static const void *ptr[MULTI_MAX_INDEX] = {
407 [MULTI_LANG_INDEX] = &usb_string_lang_en,
408 [MULTI_MODEM_INDEX] = &multi_modem,
409 [MULTI_ETH_MAC_INDEX] = &multi_eth_mac,
410 [MULTI_ETH_CONTROL_INDEX] = &multi_eth_control,
411 [MULTI_ETH_DATA_INDEX] = &multi_eth_data,
412 [MULTI_STORAGE_INDEX] = &multi_storage,
413 [MULTI_CONFIGURATION_INDEX] = &multi_configuration,
414 [MULTI_MANUFACTURER_INDEX] = &multi_manufacturer,
415 [MULTI_PRODUCT_INDEX] = &multi_product,
416 [MULTI_SERIAL_NUMBER_INDEX] = &multi_serial_number,
419 if (string_index == 0) {
420 return (&usb_string_lang_en);
422 if (lang_id != 0x0409) {
425 if (string_index < MULTI_MAX_INDEX) {
426 return (ptr[string_index]);
432 multi_init(void *arg __unused)
434 struct sysctl_oid *parent;
437 usb_make_str_desc(&multi_modem, sizeof(multi_modem),
438 MULTI_DEFAULT_MODEM);
439 usb_make_str_desc(&multi_eth_mac, sizeof(multi_eth_mac),
440 MULTI_DEFAULT_ETH_MAC);
441 usb_make_str_desc(&multi_eth_control, sizeof(multi_eth_control),
442 MULTI_DEFAULT_ETH_CONTROL);
443 usb_make_str_desc(&multi_eth_data, sizeof(multi_eth_data),
444 MULTI_DEFAULT_ETH_DATA);
445 usb_make_str_desc(&multi_storage, sizeof(multi_storage),
446 MULTI_DEFAULT_STORAGE);
447 usb_make_str_desc(&multi_configuration, sizeof(multi_configuration),
448 MULTI_DEFAULT_CONFIGURATION);
449 usb_make_str_desc(&multi_manufacturer, sizeof(multi_manufacturer),
450 MULTI_DEFAULT_MANUFACTURER);
451 usb_make_str_desc(&multi_product, sizeof(multi_product),
452 MULTI_DEFAULT_PRODUCT);
453 usb_make_str_desc(&multi_serial_number, sizeof(multi_serial_number),
454 MULTI_DEFAULT_SERIAL_NUMBER);
456 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MULTI);
457 sysctl_ctx_init(&multi_ctx_list);
459 parent = SYSCTL_ADD_NODE(&multi_ctx_list,
460 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
461 parent_name, CTLFLAG_RW,
462 0, "USB Multifunction device side template");
463 SYSCTL_ADD_U16(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
464 "vendor_id", CTLFLAG_RWTUN,
465 &usb_template_multi.idVendor, 1, "Vendor identifier");
466 SYSCTL_ADD_U16(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
467 "product_id", CTLFLAG_RWTUN,
468 &usb_template_multi.idProduct, 1, "Product identifier");
469 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
470 "eth_mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
471 &multi_eth_mac, sizeof(multi_eth_mac), usb_temp_sysctl,
472 "A", "Ethernet MAC address string");
474 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
475 "modem", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
476 &multi_modem, sizeof(multi_modem), usb_temp_sysctl,
477 "A", "Modem interface string");
478 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
479 "eth_control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
480 &multi_eth_control, sizeof(multi_eth_data), usb_temp_sysctl,
481 "A", "Ethernet control interface string");
482 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
483 "eth_data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
484 &multi_eth_data, sizeof(multi_eth_data), usb_temp_sysctl,
485 "A", "Ethernet data interface string");
486 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
487 "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
488 &multi_storage, sizeof(multi_storage), usb_temp_sysctl,
489 "A", "Storage interface string");
490 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
491 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
492 &multi_configuration, sizeof(multi_configuration), usb_temp_sysctl,
493 "A", "Configuration string");
495 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
496 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
497 &multi_manufacturer, sizeof(multi_manufacturer), usb_temp_sysctl,
498 "A", "Manufacturer string");
499 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
500 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
501 &multi_product, sizeof(multi_product), usb_temp_sysctl,
502 "A", "Product string");
503 SYSCTL_ADD_PROC(&multi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
504 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
505 &multi_serial_number, sizeof(multi_serial_number), usb_temp_sysctl,
506 "A", "Serial number string");
510 multi_uninit(void *arg __unused)
513 sysctl_ctx_free(&multi_ctx_list);
516 SYSINIT(multi_init, SI_SUB_LOCK, SI_ORDER_FIRST, multi_init, NULL);
517 SYSUNINIT(multi_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, multi_uninit, NULL);