2 * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * This file contains the USB template for USB Networking and Serial
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #ifdef USB_GLOBAL_INCLUDE_FILE
38 #include USB_GLOBAL_INCLUDE_FILE
40 #include <sys/stdint.h>
41 #include <sys/stddef.h>
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/module.h>
50 #include <sys/mutex.h>
51 #include <sys/condvar.h>
52 #include <sys/sysctl.h>
54 #include <sys/unistd.h>
55 #include <sys/callout.h>
56 #include <sys/malloc.h>
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_cdc.h>
64 #include <dev/usb/template/usb_template.h>
65 #endif /* USB_GLOBAL_INCLUDE_FILE */
67 #define MODEM_IFACE_0 0
68 #define MODEM_IFACE_1 1
74 STRING_ETH_CONTROL_INDEX,
75 STRING_ETH_DATA_INDEX,
76 STRING_ETH_CONFIG_INDEX,
84 #define STRING_MODEM \
85 "U\0S\0B\0 \0M\0o\0d\0e\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e"
87 #define STRING_ETH_MAC \
88 "2\0A\0002\0003\0004\0005\0006\0007\08\09\0A\0B"
90 #define STRING_ETH_CONTROL \
91 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 " \
92 "\0C\0o\0m\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e"
94 #define STRING_ETH_DATA \
95 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0D\0a\0t\0a\0 " \
96 "\0I\0n\0t\0e\0r\0f\0a\0c\0e"
98 #define STRING_CONFIG \
99 "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g\0u\0r\0a\0t\0i\0o\0n"
101 #define STRING_VENDOR \
102 "T\0h\0e\0 \0F\0r\0e\0e\0B\0S\0D\0 \0P\0r\0o\0j\0e\0c\0t"
104 #define STRING_PRODUCT \
105 "S\0E\0R\0I\0A\0L\0N\0E\0T"
107 #define STRING_SERIAL \
108 "J\0a\0n\0u\0a\0r\0y\0 \0002\0000\0001\0005"
110 /* make the real string descriptors */
112 USB_MAKE_STRING_DESC(STRING_MODEM, string_modem);
113 USB_MAKE_STRING_DESC(STRING_ETH_MAC, string_eth_mac);
114 USB_MAKE_STRING_DESC(STRING_ETH_CONTROL, string_eth_control);
115 USB_MAKE_STRING_DESC(STRING_ETH_DATA, string_eth_data);
116 USB_MAKE_STRING_DESC(STRING_CONFIG, string_serialnet_config);
117 USB_MAKE_STRING_DESC(STRING_VENDOR, string_serialnet_vendor);
118 USB_MAKE_STRING_DESC(STRING_PRODUCT, string_serialnet_product);
119 USB_MAKE_STRING_DESC(STRING_SERIAL, string_serialnet_serial);
123 static usb_temp_get_string_desc_t serialnet_get_string_desc;
125 static const struct usb_cdc_union_descriptor eth_union_desc = {
126 .bLength = sizeof(eth_union_desc),
127 .bDescriptorType = UDESC_CS_INTERFACE,
128 .bDescriptorSubtype = UDESCSUB_CDC_UNION,
129 .bMasterInterface = 0, /* this is automatically updated */
130 .bSlaveInterface[0] = 1, /* this is automatically updated */
133 static const struct usb_cdc_header_descriptor eth_header_desc = {
134 .bLength = sizeof(eth_header_desc),
135 .bDescriptorType = UDESC_CS_INTERFACE,
136 .bDescriptorSubtype = UDESCSUB_CDC_HEADER,
141 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
142 .bLength = sizeof(eth_enf_desc),
143 .bDescriptorType = UDESC_CS_INTERFACE,
144 .bDescriptorSubtype = UDESCSUB_CDC_ENF,
145 .iMacAddress = STRING_ETH_MAC_INDEX,
146 .bmEthernetStatistics = {0, 0, 0, 0},
147 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
148 .wNumberMCFilters = {0, 0},
149 .bNumberPowerFilters = 0,
152 static const void *eth_control_if_desc[] = {
159 static const struct usb_temp_packet_size bulk_mps = {
160 .mps[USB_SPEED_FULL] = 64,
161 .mps[USB_SPEED_HIGH] = 512,
164 static const struct usb_temp_packet_size intr_mps = {
165 .mps[USB_SPEED_FULL] = 8,
166 .mps[USB_SPEED_HIGH] = 8,
169 static const struct usb_temp_endpoint_desc bulk_in_ep = {
170 .pPacketSize = &bulk_mps,
171 #ifdef USB_HIP_IN_EP_0
172 .bEndpointAddress = USB_HIP_IN_EP_0,
174 .bEndpointAddress = UE_DIR_IN,
176 .bmAttributes = UE_BULK,
179 static const struct usb_temp_endpoint_desc bulk_out_ep = {
180 .pPacketSize = &bulk_mps,
181 #ifdef USB_HIP_OUT_EP_0
182 .bEndpointAddress = USB_HIP_OUT_EP_0,
184 .bEndpointAddress = UE_DIR_OUT,
186 .bmAttributes = UE_BULK,
189 static const struct usb_temp_endpoint_desc intr_in_ep = {
190 .pPacketSize = &intr_mps,
191 .bEndpointAddress = UE_DIR_IN,
192 .bmAttributes = UE_INTERRUPT,
195 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
200 static const struct usb_temp_interface_desc eth_control_interface = {
201 .ppEndpoints = eth_intr_endpoints,
202 .ppRawDesc = eth_control_if_desc,
203 .bInterfaceClass = UICLASS_CDC,
204 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
205 .bInterfaceProtocol = 0,
206 .iInterface = STRING_ETH_CONTROL_INDEX,
209 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
215 static const struct usb_temp_interface_desc eth_data_null_interface = {
216 .ppEndpoints = NULL, /* no endpoints */
217 .bInterfaceClass = UICLASS_CDC_DATA,
218 .bInterfaceSubClass = 0,
219 .bInterfaceProtocol = 0,
220 .iInterface = STRING_ETH_DATA_INDEX,
223 static const struct usb_temp_interface_desc eth_data_interface = {
224 .ppEndpoints = eth_data_endpoints,
225 .bInterfaceClass = UICLASS_CDC_DATA,
226 .bInterfaceSubClass = UISUBCLASS_DATA,
227 .bInterfaceProtocol = 0,
228 .iInterface = STRING_ETH_DATA_INDEX,
229 .isAltInterface = 1, /* this is an alternate setting */
232 static const struct usb_temp_packet_size modem_bulk_mps = {
233 .mps[USB_SPEED_LOW] = 8,
234 .mps[USB_SPEED_FULL] = 64,
235 .mps[USB_SPEED_HIGH] = 512,
238 static const struct usb_temp_packet_size modem_intr_mps = {
239 .mps[USB_SPEED_LOW] = 8,
240 .mps[USB_SPEED_FULL] = 8,
241 .mps[USB_SPEED_HIGH] = 8,
244 static const struct usb_temp_interval modem_intr_interval = {
245 .bInterval[USB_SPEED_LOW] = 8, /* 8ms */
246 .bInterval[USB_SPEED_FULL] = 8, /* 8ms */
247 .bInterval[USB_SPEED_HIGH] = 7, /* 8ms */
250 static const struct usb_temp_endpoint_desc modem_ep_0 = {
251 .pPacketSize = &modem_intr_mps,
252 .pIntervals = &modem_intr_interval,
253 .bEndpointAddress = UE_DIR_IN,
254 .bmAttributes = UE_INTERRUPT,
257 static const struct usb_temp_endpoint_desc modem_ep_1 = {
258 .pPacketSize = &modem_bulk_mps,
259 .bEndpointAddress = UE_DIR_OUT,
260 .bmAttributes = UE_BULK,
263 static const struct usb_temp_endpoint_desc modem_ep_2 = {
264 .pPacketSize = &modem_bulk_mps,
265 .bEndpointAddress = UE_DIR_IN,
266 .bmAttributes = UE_BULK,
269 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
274 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
280 static const uint8_t modem_raw_desc_0[] = {
281 0x05, 0x24, 0x00, 0x10, 0x01
284 static const uint8_t modem_raw_desc_1[] = {
285 0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
288 static const uint8_t modem_raw_desc_2[] = {
289 0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
292 static const uint8_t modem_raw_desc_3[] = {
293 0x04, 0x24, 0x02, 0x07
296 static const void *modem_iface_0_desc[] = {
304 static const struct usb_temp_interface_desc modem_iface_0 = {
305 .ppRawDesc = modem_iface_0_desc,
306 .ppEndpoints = modem_iface_0_ep,
307 .bInterfaceClass = 2,
308 .bInterfaceSubClass = 2,
309 .bInterfaceProtocol = 1,
310 .iInterface = STRING_MODEM_INDEX,
313 static const struct usb_temp_interface_desc modem_iface_1 = {
314 .ppEndpoints = modem_iface_1_ep,
315 .bInterfaceClass = 10,
316 .bInterfaceSubClass = 0,
317 .bInterfaceProtocol = 0,
318 .iInterface = STRING_MODEM_INDEX,
321 static const struct usb_temp_interface_desc *serialnet_interfaces[] = {
324 ð_control_interface,
325 ð_data_null_interface,
330 static const struct usb_temp_config_desc serialnet_config_desc = {
331 .ppIfaceDesc = serialnet_interfaces,
332 .bmAttributes = UC_BUS_POWERED,
333 .bMaxPower = 25, /* 50 mA */
334 .iConfiguration = STRING_CONFIG_INDEX,
336 static const struct usb_temp_config_desc *serialnet_configs[] = {
337 &serialnet_config_desc,
341 const struct usb_temp_device_desc usb_template_serialnet = {
342 .getStringDesc = &serialnet_get_string_desc,
343 .ppConfigDesc = serialnet_configs,
344 .idVendor = USB_TEMPLATE_VENDOR,
347 .bDeviceClass = UDCLASS_COMM,
348 .bDeviceSubClass = 0,
349 .bDeviceProtocol = 0,
350 .iManufacturer = STRING_VENDOR_INDEX,
351 .iProduct = STRING_PRODUCT_INDEX,
352 .iSerialNumber = STRING_SERIAL_INDEX,
355 /*------------------------------------------------------------------------*
356 * serialnet_get_string_desc
359 * NULL: Failure. No such string.
360 * Else: Success. Pointer to string descriptor is returned.
361 *------------------------------------------------------------------------*/
363 serialnet_get_string_desc(uint16_t lang_id, uint8_t string_index)
365 static const void *ptr[STRING_MAX] = {
366 [STRING_LANG_INDEX] = &usb_string_lang_en,
367 [STRING_MODEM_INDEX] = &string_modem,
368 [STRING_ETH_MAC_INDEX] = &string_eth_mac,
369 [STRING_ETH_CONTROL_INDEX] = &string_eth_control,
370 [STRING_ETH_DATA_INDEX] = &string_eth_data,
371 [STRING_CONFIG_INDEX] = &string_serialnet_config,
372 [STRING_VENDOR_INDEX] = &string_serialnet_vendor,
373 [STRING_PRODUCT_INDEX] = &string_serialnet_product,
374 [STRING_SERIAL_INDEX] = &string_serialnet_serial,
377 if (string_index == 0) {
378 return (&usb_string_lang_en);
380 if (lang_id != 0x0409) {
383 if (string_index < STRING_MAX) {
384 return (ptr[string_index]);