1 /* $FreeBSD$ */
2 /*-
3 * Copyright (c) 2007 Hans Petter Selasky <hselasky@FreeBSD.org>
4 * Copyright (c) 2018 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * This file contains the USB templates for a CDC USB ethernet device.
34 */
35
36 #ifdef USB_GLOBAL_INCLUDE_FILE
37 #include USB_GLOBAL_INCLUDE_FILE
38 #else
39 #include <sys/stdint.h>
40 #include <sys/stddef.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/mutex.h>
50 #include <sys/condvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/sx.h>
53 #include <sys/unistd.h>
54 #include <sys/callout.h>
55 #include <sys/malloc.h>
56 #include <sys/priv.h>
57
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usb_core.h>
61 #include <dev/usb/usb_cdc.h>
62 #include <dev/usb/usb_ioctl.h>
63 #include <dev/usb/usb_util.h>
64
65 #include <dev/usb/template/usb_template.h>
66 #endif /* USB_GLOBAL_INCLUDE_FILE */
67
68 enum {
69 ETH_LANG_INDEX,
70 ETH_MAC_INDEX,
71 ETH_CONTROL_INDEX,
72 ETH_DATA_INDEX,
73 ETH_CONFIGURATION_INDEX,
74 ETH_MANUFACTURER_INDEX,
75 ETH_PRODUCT_INDEX,
76 ETH_SERIAL_NUMBER_INDEX,
77 ETH_MAX_INDEX,
78 };
79
80 #define ETH_DEFAULT_MAC "2A02030405060789AB"
81 #define ETH_DEFAULT_CONTROL "USB Ethernet Comm Interface"
82 #define ETH_DEFAULT_DATA "USB Ethernet Data Interface"
83 #define ETH_DEFAULT_CONFIG "Default Config"
84 #define ETH_DEFAULT_MANUFACTURER "FreeBSD foundation"
85 #define ETH_DEFAULT_PRODUCT "USB Ethernet Adapter"
86 #define ETH_DEFAULT_SERIAL_NUMBER "December 2007"
87
88 static struct usb_string_descriptor eth_mac;
89 static struct usb_string_descriptor eth_control;
90 static struct usb_string_descriptor eth_data;
91 static struct usb_string_descriptor eth_configuration;
92 static struct usb_string_descriptor eth_manufacturer;
93 static struct usb_string_descriptor eth_product;
94 static struct usb_string_descriptor eth_serial_number;
95
96 static struct sysctl_ctx_list eth_ctx_list;
97
98 /* prototypes */
99
100 static usb_temp_get_string_desc_t eth_get_string_desc;
101
102 static const struct usb_cdc_union_descriptor eth_union_desc = {
103 .bLength = sizeof(eth_union_desc),
104 .bDescriptorType = UDESC_CS_INTERFACE,
105 .bDescriptorSubtype = UDESCSUB_CDC_UNION,
106 .bMasterInterface = 0, /* this is automatically updated */
107 .bSlaveInterface[0] = 1, /* this is automatically updated */
108 };
109
110 static const struct usb_cdc_header_descriptor eth_header_desc = {
111 .bLength = sizeof(eth_header_desc),
112 .bDescriptorType = UDESC_CS_INTERFACE,
113 .bDescriptorSubtype = UDESCSUB_CDC_HEADER,
114 .bcdCDC[0] = 0x10,
115 .bcdCDC[1] = 0x01,
116 };
117
118 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
119 .bLength = sizeof(eth_enf_desc),
120 .bDescriptorType = UDESC_CS_INTERFACE,
121 .bDescriptorSubtype = UDESCSUB_CDC_ENF,
122 .iMacAddress = ETH_MAC_INDEX,
123 .bmEthernetStatistics = {0, 0, 0, 0},
124 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
125 .wNumberMCFilters = {0, 0},
126 .bNumberPowerFilters = 0,
127 };
128
129 static const void *eth_control_if_desc[] = {
130 ð_union_desc,
131 ð_header_desc,
132 ð_enf_desc,
133 NULL,
134 };
135
136 static const struct usb_temp_packet_size bulk_mps = {
137 .mps[USB_SPEED_FULL] = 64,
138 .mps[USB_SPEED_HIGH] = 512,
139 };
140
141 static const struct usb_temp_packet_size intr_mps = {
142 .mps[USB_SPEED_FULL] = 8,
143 .mps[USB_SPEED_HIGH] = 8,
144 };
145
146 static const struct usb_temp_endpoint_desc bulk_in_ep = {
147 .pPacketSize = &bulk_mps,
148 #ifdef USB_HIP_IN_EP_0
149 .bEndpointAddress = USB_HIP_IN_EP_0,
150 #else
151 .bEndpointAddress = UE_DIR_IN,
152 #endif
153 .bmAttributes = UE_BULK,
154 };
155
156 static const struct usb_temp_endpoint_desc bulk_out_ep = {
157 .pPacketSize = &bulk_mps,
158 #ifdef USB_HIP_OUT_EP_0
159 .bEndpointAddress = USB_HIP_OUT_EP_0,
160 #else
161 .bEndpointAddress = UE_DIR_OUT,
162 #endif
163 .bmAttributes = UE_BULK,
164 };
165
166 static const struct usb_temp_endpoint_desc intr_in_ep = {
167 .pPacketSize = &intr_mps,
168 .bEndpointAddress = UE_DIR_IN,
169 .bmAttributes = UE_INTERRUPT,
170 };
171
172 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
173 &intr_in_ep,
174 NULL,
175 };
176
177 static const struct usb_temp_interface_desc eth_control_interface = {
178 .ppEndpoints = eth_intr_endpoints,
179 .ppRawDesc = eth_control_if_desc,
180 .bInterfaceClass = UICLASS_CDC,
181 .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
182 .bInterfaceProtocol = 0,
183 .iInterface = ETH_CONTROL_INDEX,
184 };
185
186 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
187 &bulk_in_ep,
188 &bulk_out_ep,
189 NULL,
190 };
191
192 static const struct usb_temp_interface_desc eth_data_null_interface = {
193 .ppEndpoints = NULL, /* no endpoints */
194 .bInterfaceClass = UICLASS_CDC_DATA,
195 .bInterfaceSubClass = 0,
196 .bInterfaceProtocol = 0,
197 .iInterface = ETH_DATA_INDEX,
198 };
199
200 static const struct usb_temp_interface_desc eth_data_interface = {
201 .ppEndpoints = eth_data_endpoints,
202 .bInterfaceClass = UICLASS_CDC_DATA,
203 .bInterfaceSubClass = UISUBCLASS_DATA,
204 .bInterfaceProtocol = 0,
205 .iInterface = ETH_DATA_INDEX,
206 .isAltInterface = 1, /* this is an alternate setting */
207 };
208
209 static const struct usb_temp_interface_desc *eth_interfaces[] = {
210 ð_control_interface,
211 ð_data_null_interface,
212 ð_data_interface,
213 NULL,
214 };
215
216 static const struct usb_temp_config_desc eth_config_desc = {
217 .ppIfaceDesc = eth_interfaces,
218 .bmAttributes = UC_BUS_POWERED,
219 .bMaxPower = 25, /* 50 mA */
220 .iConfiguration = ETH_CONFIGURATION_INDEX,
221 };
222
223 static const struct usb_temp_config_desc *eth_configs[] = {
224 ð_config_desc,
225 NULL,
226 };
227
228 struct usb_temp_device_desc usb_template_cdce = {
229 .getStringDesc = ð_get_string_desc,
230 .ppConfigDesc = eth_configs,
231 .idVendor = USB_TEMPLATE_VENDOR,
232 .idProduct = 0x0001,
233 .bcdDevice = 0x0100,
234 .bDeviceClass = UDCLASS_COMM,
235 .bDeviceSubClass = 0,
236 .bDeviceProtocol = 0,
237 .iManufacturer = ETH_MANUFACTURER_INDEX,
238 .iProduct = ETH_PRODUCT_INDEX,
239 .iSerialNumber = ETH_SERIAL_NUMBER_INDEX,
240 };
241
242 /*------------------------------------------------------------------------*
243 * eth_get_string_desc
244 *
245 * Return values:
246 * NULL: Failure. No such string.
247 * Else: Success. Pointer to string descriptor is returned.
248 *------------------------------------------------------------------------*/
249 static const void *
eth_get_string_desc(uint16_t lang_id,uint8_t string_index)250 eth_get_string_desc(uint16_t lang_id, uint8_t string_index)
251 {
252 static const void *ptr[ETH_MAX_INDEX] = {
253 [ETH_LANG_INDEX] = &usb_string_lang_en,
254 [ETH_MAC_INDEX] = ð_mac,
255 [ETH_CONTROL_INDEX] = ð_control,
256 [ETH_DATA_INDEX] = ð_data,
257 [ETH_CONFIGURATION_INDEX] = ð_configuration,
258 [ETH_MANUFACTURER_INDEX] = ð_manufacturer,
259 [ETH_PRODUCT_INDEX] = ð_product,
260 [ETH_SERIAL_NUMBER_INDEX] = ð_serial_number,
261 };
262
263 if (string_index == 0) {
264 return (&usb_string_lang_en);
265 }
266 if (lang_id != 0x0409) {
267 return (NULL);
268 }
269 if (string_index < ETH_MAX_INDEX) {
270 return (ptr[string_index]);
271 }
272 return (NULL);
273 }
274
275 static void
eth_init(void * arg __unused)276 eth_init(void *arg __unused)
277 {
278 struct sysctl_oid *parent;
279 char parent_name[3];
280
281 usb_make_str_desc(ð_mac, sizeof(eth_mac),
282 ETH_DEFAULT_MAC);
283 usb_make_str_desc(ð_control, sizeof(eth_control),
284 ETH_DEFAULT_CONTROL);
285 usb_make_str_desc(ð_data, sizeof(eth_data),
286 ETH_DEFAULT_DATA);
287 usb_make_str_desc(ð_configuration, sizeof(eth_configuration),
288 ETH_DEFAULT_CONFIG);
289 usb_make_str_desc(ð_manufacturer, sizeof(eth_manufacturer),
290 ETH_DEFAULT_MANUFACTURER);
291 usb_make_str_desc(ð_product, sizeof(eth_product),
292 ETH_DEFAULT_PRODUCT);
293 usb_make_str_desc(ð_serial_number, sizeof(eth_serial_number),
294 ETH_DEFAULT_SERIAL_NUMBER);
295
296 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_CDCE);
297 sysctl_ctx_init(ð_ctx_list);
298
299 parent = SYSCTL_ADD_NODE(ð_ctx_list,
300 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
301 parent_name, CTLFLAG_RW,
302 0, "USB CDC Ethernet device side template");
303 SYSCTL_ADD_U16(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
304 "vendor_id", CTLFLAG_RWTUN,
305 &usb_template_cdce.idVendor, 1, "Vendor identifier");
306 SYSCTL_ADD_U16(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
307 "product_id", CTLFLAG_RWTUN,
308 &usb_template_cdce.idProduct, 1, "Product identifier");
309 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
310 "mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
311 ð_mac, sizeof(eth_mac), usb_temp_sysctl,
312 "A", "MAC address string");
313 #if 0
314 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
315 "control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
316 ð_control, sizeof(eth_control), usb_temp_sysctl,
317 "A", "Control interface string");
318 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
319 "data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
320 ð_data, sizeof(eth_data), usb_temp_sysctl,
321 "A", "Data interface string");
322 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
323 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
324 ð_configuration, sizeof(eth_configuration), usb_temp_sysctl,
325 "A", "Configuration string");
326 #endif
327 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
328 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
329 ð_manufacturer, sizeof(eth_manufacturer), usb_temp_sysctl,
330 "A", "Manufacturer string");
331 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
332 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
333 ð_product, sizeof(eth_product), usb_temp_sysctl,
334 "A", "Product string");
335 SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
336 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
337 ð_serial_number, sizeof(eth_serial_number), usb_temp_sysctl,
338 "A", "Serial number string");
339 }
340
341 static void
eth_uninit(void * arg __unused)342 eth_uninit(void *arg __unused)
343 {
344
345 sysctl_ctx_free(ð_ctx_list);
346 }
347
348 SYSINIT(eth_init, SI_SUB_LOCK, SI_ORDER_FIRST, eth_init, NULL);
349 SYSUNINIT(eth_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, eth_uninit, NULL);
350