1 /*-
2  * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
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.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30 /*
31  * This file contains the USB template for USB Networking and Serial
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #ifdef USB_GLOBAL_INCLUDE_FILE
38 #include USB_GLOBAL_INCLUDE_FILE
39 #else
40 #include <sys/stdint.h>
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/bus.h>
47 #include <sys/module.h>
48 #include <sys/lock.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/unistd.h>
52 #include <sys/callout.h>
53 #include <sys/malloc.h>
54 #include <sys/caps.h>
55 
56 #include <bus/u4b/usb.h>
57 #include <bus/u4b/usbdi.h>
58 #include <bus/u4b/usb_core.h>
59 #include <bus/u4b/usb_cdc.h>
60 
61 #include <bus/u4b/template/usb_template.h>
62 #endif		/* USB_GLOBAL_INCLUDE_FILE */
63 
64 #define	MODEM_IFACE_0 0
65 #define	MODEM_IFACE_1 1
66 
67 enum {
68 	STRING_LANG_INDEX,
69 	STRING_MODEM_INDEX,
70 	STRING_ETH_MAC_INDEX,
71 	STRING_ETH_CONTROL_INDEX,
72 	STRING_ETH_DATA_INDEX,
73 	STRING_ETH_CONFIG_INDEX,
74 	STRING_CONFIG_INDEX,
75 	STRING_VENDOR_INDEX,
76 	STRING_PRODUCT_INDEX,
77 	STRING_SERIAL_INDEX,
78 	STRING_MAX,
79 };
80 
81 #define	STRING_MODEM \
82   "U\0S\0B\0 \0M\0o\0d\0e\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e"
83 
84 #define	STRING_ETH_MAC \
85   "2\0A\0002\0003\0004\0005\0006\0007\08\09\0A\0B"
86 
87 #define	STRING_ETH_CONTROL \
88   "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 " \
89   "\0C\0o\0m\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e"
90 
91 #define	STRING_ETH_DATA \
92   "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0D\0a\0t\0a\0 " \
93   "\0I\0n\0t\0e\0r\0f\0a\0c\0e"
94 
95 #define	STRING_CONFIG \
96   "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g\0u\0r\0a\0t\0i\0o\0n"
97 
98 #define	STRING_VENDOR \
99   "T\0h\0e\0 \0F\0r\0e\0e\0B\0S\0D\0 \0P\0r\0o\0j\0e\0c\0t"
100 
101 #define	STRING_PRODUCT \
102   "S\0E\0R\0I\0A\0L\0N\0E\0T"
103 
104 #define	STRING_SERIAL \
105   "J\0a\0n\0u\0a\0r\0y\0 \0002\0000\0001\0005"
106 
107 /* make the real string descriptors */
108 
109 USB_MAKE_STRING_DESC(STRING_MODEM, string_modem);
110 USB_MAKE_STRING_DESC(STRING_ETH_MAC, string_eth_mac);
111 USB_MAKE_STRING_DESC(STRING_ETH_CONTROL, string_eth_control);
112 USB_MAKE_STRING_DESC(STRING_ETH_DATA, string_eth_data);
113 USB_MAKE_STRING_DESC(STRING_CONFIG, string_serialnet_config);
114 USB_MAKE_STRING_DESC(STRING_VENDOR, string_serialnet_vendor);
115 USB_MAKE_STRING_DESC(STRING_PRODUCT, string_serialnet_product);
116 USB_MAKE_STRING_DESC(STRING_SERIAL, string_serialnet_serial);
117 
118 /* prototypes */
119 
120 static usb_temp_get_string_desc_t serialnet_get_string_desc;
121 
122 static const struct usb_cdc_union_descriptor eth_union_desc = {
123 	.bLength = sizeof(eth_union_desc),
124 	.bDescriptorType = UDESC_CS_INTERFACE,
125 	.bDescriptorSubtype = UDESCSUB_CDC_UNION,
126 	.bMasterInterface = 0,		/* this is automatically updated */
127 	.bSlaveInterface[0] = 1,	/* this is automatically updated */
128 };
129 
130 static const struct usb_cdc_header_descriptor eth_header_desc = {
131 	.bLength = sizeof(eth_header_desc),
132 	.bDescriptorType = UDESC_CS_INTERFACE,
133 	.bDescriptorSubtype = UDESCSUB_CDC_HEADER,
134 	.bcdCDC[0] = 0x10,
135 	.bcdCDC[1] = 0x01,
136 };
137 
138 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
139 	.bLength = sizeof(eth_enf_desc),
140 	.bDescriptorType = UDESC_CS_INTERFACE,
141 	.bDescriptorSubtype = UDESCSUB_CDC_ENF,
142 	.iMacAddress = STRING_ETH_MAC_INDEX,
143 	.bmEthernetStatistics = {0, 0, 0, 0},
144 	.wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
145 	.wNumberMCFilters = {0, 0},
146 	.bNumberPowerFilters = 0,
147 };
148 
149 static const void *eth_control_if_desc[] = {
150 	&eth_union_desc,
151 	&eth_header_desc,
152 	&eth_enf_desc,
153 	NULL,
154 };
155 
156 static const struct usb_temp_packet_size bulk_mps = {
157 	.mps[USB_SPEED_FULL] = 64,
158 	.mps[USB_SPEED_HIGH] = 512,
159 };
160 
161 static const struct usb_temp_packet_size intr_mps = {
162 	.mps[USB_SPEED_FULL] = 8,
163 	.mps[USB_SPEED_HIGH] = 8,
164 };
165 
166 static const struct usb_temp_endpoint_desc bulk_in_ep = {
167 	.pPacketSize = &bulk_mps,
168 #ifdef USB_HIP_IN_EP_0
169 	.bEndpointAddress = USB_HIP_IN_EP_0,
170 #else
171 	.bEndpointAddress = UE_DIR_IN,
172 #endif
173 	.bmAttributes = UE_BULK,
174 };
175 
176 static const struct usb_temp_endpoint_desc bulk_out_ep = {
177 	.pPacketSize = &bulk_mps,
178 #ifdef USB_HIP_OUT_EP_0
179 	.bEndpointAddress = USB_HIP_OUT_EP_0,
180 #else
181 	.bEndpointAddress = UE_DIR_OUT,
182 #endif
183 	.bmAttributes = UE_BULK,
184 };
185 
186 static const struct usb_temp_endpoint_desc intr_in_ep = {
187 	.pPacketSize = &intr_mps,
188 	.bEndpointAddress = UE_DIR_IN,
189 	.bmAttributes = UE_INTERRUPT,
190 };
191 
192 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
193 	&intr_in_ep,
194 	NULL,
195 };
196 
197 static const struct usb_temp_interface_desc eth_control_interface = {
198 	.ppEndpoints = eth_intr_endpoints,
199 	.ppRawDesc = eth_control_if_desc,
200 	.bInterfaceClass = UICLASS_CDC,
201 	.bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
202 	.bInterfaceProtocol = 0,
203 	.iInterface = STRING_ETH_CONTROL_INDEX,
204 };
205 
206 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
207 	&bulk_in_ep,
208 	&bulk_out_ep,
209 	NULL,
210 };
211 
212 static const struct usb_temp_interface_desc eth_data_null_interface = {
213 	.ppEndpoints = NULL,		/* no endpoints */
214 	.bInterfaceClass = UICLASS_CDC_DATA,
215 	.bInterfaceSubClass = 0,
216 	.bInterfaceProtocol = 0,
217 	.iInterface = STRING_ETH_DATA_INDEX,
218 };
219 
220 static const struct usb_temp_interface_desc eth_data_interface = {
221 	.ppEndpoints = eth_data_endpoints,
222 	.bInterfaceClass = UICLASS_CDC_DATA,
223 	.bInterfaceSubClass = UISUBCLASS_DATA,
224 	.bInterfaceProtocol = 0,
225 	.iInterface = STRING_ETH_DATA_INDEX,
226 	.isAltInterface = 1,		/* this is an alternate setting */
227 };
228 
229 static const struct usb_temp_packet_size modem_bulk_mps = {
230 	.mps[USB_SPEED_LOW] = 8,
231 	.mps[USB_SPEED_FULL] = 64,
232 	.mps[USB_SPEED_HIGH] = 512,
233 };
234 
235 static const struct usb_temp_packet_size modem_intr_mps = {
236 	.mps[USB_SPEED_LOW] = 8,
237 	.mps[USB_SPEED_FULL] = 8,
238 	.mps[USB_SPEED_HIGH] = 8,
239 };
240 
241 static const struct usb_temp_interval modem_intr_interval = {
242 	.bInterval[USB_SPEED_LOW] = 8,	/* 8ms */
243 	.bInterval[USB_SPEED_FULL] = 8,	/* 8ms */
244 	.bInterval[USB_SPEED_HIGH] = 7,	/* 8ms */
245 };
246 
247 static const struct usb_temp_endpoint_desc modem_ep_0 = {
248 	.pPacketSize = &modem_intr_mps,
249 	.pIntervals = &modem_intr_interval,
250 	.bEndpointAddress = UE_DIR_IN,
251 	.bmAttributes = UE_INTERRUPT,
252 };
253 
254 static const struct usb_temp_endpoint_desc modem_ep_1 = {
255 	.pPacketSize = &modem_bulk_mps,
256 	.bEndpointAddress = UE_DIR_OUT,
257 	.bmAttributes = UE_BULK,
258 };
259 
260 static const struct usb_temp_endpoint_desc modem_ep_2 = {
261 	.pPacketSize = &modem_bulk_mps,
262 	.bEndpointAddress = UE_DIR_IN,
263 	.bmAttributes = UE_BULK,
264 };
265 
266 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
267 	&modem_ep_0,
268 	NULL,
269 };
270 
271 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
272 	&modem_ep_1,
273 	&modem_ep_2,
274 	NULL,
275 };
276 
277 static const uint8_t modem_raw_desc_0[] = {
278 	0x05, 0x24, 0x00, 0x10, 0x01
279 };
280 
281 static const uint8_t modem_raw_desc_1[] = {
282 	0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
283 };
284 
285 static const uint8_t modem_raw_desc_2[] = {
286 	0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
287 };
288 
289 static const uint8_t modem_raw_desc_3[] = {
290 	0x04, 0x24, 0x02, 0x07
291 };
292 
293 static const void *modem_iface_0_desc[] = {
294 	&modem_raw_desc_0,
295 	&modem_raw_desc_1,
296 	&modem_raw_desc_2,
297 	&modem_raw_desc_3,
298 	NULL,
299 };
300 
301 static const struct usb_temp_interface_desc modem_iface_0 = {
302 	.ppRawDesc = modem_iface_0_desc,
303 	.ppEndpoints = modem_iface_0_ep,
304 	.bInterfaceClass = 2,
305 	.bInterfaceSubClass = 2,
306 	.bInterfaceProtocol = 1,
307 	.iInterface = STRING_MODEM_INDEX,
308 };
309 
310 static const struct usb_temp_interface_desc modem_iface_1 = {
311 	.ppEndpoints = modem_iface_1_ep,
312 	.bInterfaceClass = 10,
313 	.bInterfaceSubClass = 0,
314 	.bInterfaceProtocol = 0,
315 	.iInterface = STRING_MODEM_INDEX,
316 };
317 
318 static const struct usb_temp_interface_desc *serialnet_interfaces[] = {
319 	&modem_iface_0,
320 	&modem_iface_1,
321 	&eth_control_interface,
322 	&eth_data_null_interface,
323 	&eth_data_interface,
324 	NULL,
325 };
326 
327 static const struct usb_temp_config_desc serialnet_config_desc = {
328 	.ppIfaceDesc = serialnet_interfaces,
329 	.bmAttributes = UC_BUS_POWERED,
330 	.bMaxPower = 25,		/* 50 mA */
331 	.iConfiguration = STRING_CONFIG_INDEX,
332 };
333 static const struct usb_temp_config_desc *serialnet_configs[] = {
334 	&serialnet_config_desc,
335 	NULL,
336 };
337 
338 const struct usb_temp_device_desc usb_template_serialnet = {
339 	.getStringDesc = &serialnet_get_string_desc,
340 	.ppConfigDesc = serialnet_configs,
341 	.idVendor = USB_TEMPLATE_VENDOR,
342 	.idProduct = 0x0001,
343 	.bcdDevice = 0x0100,
344 	.bDeviceClass = UDCLASS_COMM,
345 	.bDeviceSubClass = 0,
346 	.bDeviceProtocol = 0,
347 	.iManufacturer = STRING_VENDOR_INDEX,
348 	.iProduct = STRING_PRODUCT_INDEX,
349 	.iSerialNumber = STRING_SERIAL_INDEX,
350 };
351 
352 /*------------------------------------------------------------------------*
353  *	serialnet_get_string_desc
354  *
355  * Return values:
356  * NULL: Failure. No such string.
357  * Else: Success. Pointer to string descriptor is returned.
358  *------------------------------------------------------------------------*/
359 static const void *
serialnet_get_string_desc(uint16_t lang_id,uint8_t string_index)360 serialnet_get_string_desc(uint16_t lang_id, uint8_t string_index)
361 {
362 	static const void *ptr[STRING_MAX] = {
363 		[STRING_LANG_INDEX] = &usb_string_lang_en,
364 		[STRING_MODEM_INDEX] = &string_modem,
365 		[STRING_ETH_MAC_INDEX] = &string_eth_mac,
366 		[STRING_ETH_CONTROL_INDEX] = &string_eth_control,
367 		[STRING_ETH_DATA_INDEX] = &string_eth_data,
368 		[STRING_CONFIG_INDEX] = &string_serialnet_config,
369 		[STRING_VENDOR_INDEX] = &string_serialnet_vendor,
370 		[STRING_PRODUCT_INDEX] = &string_serialnet_product,
371 		[STRING_SERIAL_INDEX] = &string_serialnet_serial,
372 	};
373 
374 	if (string_index == 0) {
375 		return (&usb_string_lang_en);
376 	}
377 	if (lang_id != 0x0409) {
378 		return (NULL);
379 	}
380 	if (string_index < STRING_MAX) {
381 		return (ptr[string_index]);
382 	}
383 	return (NULL);
384 }
385