Add missing SPDX tags; the rest of the license text is the same as in other
[freebsd.git] / sys / dev / usb / template / usb_template_serialnet.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
5  * Copyright (c) 2018 The FreeBSD Foundation
6  * All rights reserved.
7  *
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.
11  *
12  * Portions of this software were developed by Edward Tomasz Napierala
13  * under sponsorship from the FreeBSD Foundation.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
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.
23  *
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
34  * SUCH DAMAGE.
35  */
36 /*
37  * This file contains the USB template for USB Networking and Serial
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #ifdef USB_GLOBAL_INCLUDE_FILE
44 #include USB_GLOBAL_INCLUDE_FILE
45 #else
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>
53 #include <sys/bus.h>
54 #include <sys/module.h>
55 #include <sys/lock.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/sx.h>
60 #include <sys/unistd.h>
61 #include <sys/callout.h>
62 #include <sys/malloc.h>
63 #include <sys/priv.h>
64
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>
71
72 #include <dev/usb/template/usb_template.h>
73 #endif          /* USB_GLOBAL_INCLUDE_FILE */
74
75 #define MODEM_IFACE_0 0
76 #define MODEM_IFACE_1 1
77
78 enum {
79         SERIALNET_LANG_INDEX,
80         SERIALNET_MODEM_INDEX,
81         SERIALNET_ETH_MAC_INDEX,
82         SERIALNET_ETH_CONTROL_INDEX,
83         SERIALNET_ETH_DATA_INDEX,
84         SERIALNET_ETH_CONFIG_INDEX,
85         SERIALNET_CONFIGURATION_INDEX,
86         SERIALNET_MANUFACTURER_INDEX,
87         SERIALNET_PRODUCT_INDEX,
88         SERIALNET_SERIAL_NUMBER_INDEX,
89         SERIALNET_MAX_INDEX,
90 };
91
92 #define SERIALNET_DEFAULT_MODEM         "USB Modem Interface"
93 #define SERIALNET_DEFAULT_ETH_MAC       "2A02030405060789AB"
94 #define SERIALNET_DEFAULT_ETH_CONTROL   "USB Ethernet Comm Interface"
95 #define SERIALNET_DEFAULT_ETH_DATA      "USB Ethernet Data Interface"
96 #define SERIALNET_DEFAULT_CONFIGURATION "Default configuration"
97 #define SERIALNET_DEFAULT_MANUFACTURER  "The FreeBSD Project"
98 #define SERIALNET_DEFAULT_PRODUCT       "SERIALNET"
99 #define SERIALNET_DEFAULT_SERIAL_NUMBER "January 2015"
100
101 static struct usb_string_descriptor     serialnet_modem;
102 static struct usb_string_descriptor     serialnet_eth_mac;
103 static struct usb_string_descriptor     serialnet_eth_control;
104 static struct usb_string_descriptor     serialnet_eth_data;
105 static struct usb_string_descriptor     serialnet_configuration;
106 static struct usb_string_descriptor     serialnet_manufacturer;
107 static struct usb_string_descriptor     serialnet_product;
108 static struct usb_string_descriptor     serialnet_serial_number;
109
110 static struct sysctl_ctx_list           serialnet_ctx_list;
111
112 /* prototypes */
113
114 static usb_temp_get_string_desc_t serialnet_get_string_desc;
115
116 static const struct usb_cdc_union_descriptor eth_union_desc = {
117         .bLength = sizeof(eth_union_desc),
118         .bDescriptorType = UDESC_CS_INTERFACE,
119         .bDescriptorSubtype = UDESCSUB_CDC_UNION,
120         .bMasterInterface = 0,          /* this is automatically updated */
121         .bSlaveInterface[0] = 1,        /* this is automatically updated */
122 };
123
124 static const struct usb_cdc_header_descriptor eth_header_desc = {
125         .bLength = sizeof(eth_header_desc),
126         .bDescriptorType = UDESC_CS_INTERFACE,
127         .bDescriptorSubtype = UDESCSUB_CDC_HEADER,
128         .bcdCDC[0] = 0x10,
129         .bcdCDC[1] = 0x01,
130 };
131
132 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
133         .bLength = sizeof(eth_enf_desc),
134         .bDescriptorType = UDESC_CS_INTERFACE,
135         .bDescriptorSubtype = UDESCSUB_CDC_ENF,
136         .iMacAddress = SERIALNET_ETH_MAC_INDEX,
137         .bmEthernetStatistics = {0, 0, 0, 0},
138         .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
139         .wNumberMCFilters = {0, 0},
140         .bNumberPowerFilters = 0,
141 };
142
143 static const void *eth_control_if_desc[] = {
144         &eth_union_desc,
145         &eth_header_desc,
146         &eth_enf_desc,
147         NULL,
148 };
149
150 static const struct usb_temp_packet_size bulk_mps = {
151         .mps[USB_SPEED_FULL] = 64,
152         .mps[USB_SPEED_HIGH] = 512,
153 };
154
155 static const struct usb_temp_packet_size intr_mps = {
156         .mps[USB_SPEED_FULL] = 8,
157         .mps[USB_SPEED_HIGH] = 8,
158 };
159
160 static const struct usb_temp_endpoint_desc bulk_in_ep = {
161         .pPacketSize = &bulk_mps,
162 #ifdef USB_HIP_IN_EP_0
163         .bEndpointAddress = USB_HIP_IN_EP_0,
164 #else
165         .bEndpointAddress = UE_DIR_IN,
166 #endif
167         .bmAttributes = UE_BULK,
168 };
169
170 static const struct usb_temp_endpoint_desc bulk_out_ep = {
171         .pPacketSize = &bulk_mps,
172 #ifdef USB_HIP_OUT_EP_0
173         .bEndpointAddress = USB_HIP_OUT_EP_0,
174 #else
175         .bEndpointAddress = UE_DIR_OUT,
176 #endif
177         .bmAttributes = UE_BULK,
178 };
179
180 static const struct usb_temp_endpoint_desc intr_in_ep = {
181         .pPacketSize = &intr_mps,
182         .bEndpointAddress = UE_DIR_IN,
183         .bmAttributes = UE_INTERRUPT,
184 };
185
186 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
187         &intr_in_ep,
188         NULL,
189 };
190
191 static const struct usb_temp_interface_desc eth_control_interface = {
192         .ppEndpoints = eth_intr_endpoints,
193         .ppRawDesc = eth_control_if_desc,
194         .bInterfaceClass = UICLASS_CDC,
195         .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
196         .bInterfaceProtocol = UIPROTO_CDC_NONE,
197         .iInterface = SERIALNET_ETH_CONTROL_INDEX,
198 };
199
200 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
201         &bulk_in_ep,
202         &bulk_out_ep,
203         NULL,
204 };
205
206 static const struct usb_temp_interface_desc eth_data_null_interface = {
207         .ppEndpoints = NULL,            /* no endpoints */
208         .bInterfaceClass = UICLASS_CDC_DATA,
209         .bInterfaceSubClass = UISUBCLASS_DATA,
210         .bInterfaceProtocol = 0,
211         .iInterface = SERIALNET_ETH_DATA_INDEX,
212 };
213
214 static const struct usb_temp_interface_desc eth_data_interface = {
215         .ppEndpoints = eth_data_endpoints,
216         .bInterfaceClass = UICLASS_CDC_DATA,
217         .bInterfaceSubClass = UISUBCLASS_DATA,
218         .bInterfaceProtocol = 0,
219         .iInterface = SERIALNET_ETH_DATA_INDEX,
220         .isAltInterface = 1,            /* this is an alternate setting */
221 };
222
223 static const struct usb_temp_packet_size modem_bulk_mps = {
224         .mps[USB_SPEED_LOW] = 8,
225         .mps[USB_SPEED_FULL] = 64,
226         .mps[USB_SPEED_HIGH] = 512,
227 };
228
229 static const struct usb_temp_packet_size modem_intr_mps = {
230         .mps[USB_SPEED_LOW] = 8,
231         .mps[USB_SPEED_FULL] = 8,
232         .mps[USB_SPEED_HIGH] = 8,
233 };
234
235 static const struct usb_temp_interval modem_intr_interval = {
236         .bInterval[USB_SPEED_LOW] = 8,  /* 8ms */
237         .bInterval[USB_SPEED_FULL] = 8, /* 8ms */
238         .bInterval[USB_SPEED_HIGH] = 7, /* 8ms */
239 };
240
241 static const struct usb_temp_endpoint_desc modem_ep_0 = {
242         .pPacketSize = &modem_intr_mps,
243         .pIntervals = &modem_intr_interval,
244         .bEndpointAddress = UE_DIR_IN,
245         .bmAttributes = UE_INTERRUPT,
246 };
247
248 static const struct usb_temp_endpoint_desc modem_ep_1 = {
249         .pPacketSize = &modem_bulk_mps,
250         .bEndpointAddress = UE_DIR_OUT,
251         .bmAttributes = UE_BULK,
252 };
253
254 static const struct usb_temp_endpoint_desc modem_ep_2 = {
255         .pPacketSize = &modem_bulk_mps,
256         .bEndpointAddress = UE_DIR_IN,
257         .bmAttributes = UE_BULK,
258 };
259
260 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
261         &modem_ep_0,
262         NULL,
263 };
264
265 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
266         &modem_ep_1,
267         &modem_ep_2,
268         NULL,
269 };
270
271 static const uint8_t modem_raw_desc_0[] = {
272         0x05, 0x24, 0x00, 0x10, 0x01
273 };
274
275 static const uint8_t modem_raw_desc_1[] = {
276         0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
277 };
278
279 static const uint8_t modem_raw_desc_2[] = {
280         0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
281 };
282
283 static const uint8_t modem_raw_desc_3[] = {
284         0x04, 0x24, 0x02, 0x07
285 };
286
287 static const void *modem_iface_0_desc[] = {
288         &modem_raw_desc_0,
289         &modem_raw_desc_1,
290         &modem_raw_desc_2,
291         &modem_raw_desc_3,
292         NULL,
293 };
294
295 static const struct usb_temp_interface_desc modem_iface_0 = {
296         .ppRawDesc = modem_iface_0_desc,
297         .ppEndpoints = modem_iface_0_ep,
298         .bInterfaceClass = UICLASS_CDC,
299         .bInterfaceSubClass = UISUBCLASS_ABSTRACT_CONTROL_MODEL,
300         .bInterfaceProtocol = UIPROTO_CDC_AT,
301         .iInterface = SERIALNET_MODEM_INDEX,
302 };
303
304 static const struct usb_temp_interface_desc modem_iface_1 = {
305         .ppEndpoints = modem_iface_1_ep,
306         .bInterfaceClass = UICLASS_CDC_DATA,
307         .bInterfaceSubClass = UISUBCLASS_DATA,
308         .bInterfaceProtocol = 0,
309         .iInterface = SERIALNET_MODEM_INDEX,
310 };
311
312 static const struct usb_temp_interface_desc *serialnet_interfaces[] = {
313         &modem_iface_0,
314         &modem_iface_1,
315         &eth_control_interface,
316         &eth_data_null_interface,
317         &eth_data_interface,
318         NULL,
319 };
320
321 static const struct usb_temp_config_desc serialnet_config_desc = {
322         .ppIfaceDesc = serialnet_interfaces,
323         .bmAttributes = UC_BUS_POWERED,
324         .bMaxPower = 25,                /* 50 mA */
325         .iConfiguration = SERIALNET_CONFIGURATION_INDEX,
326 };
327 static const struct usb_temp_config_desc *serialnet_configs[] = {
328         &serialnet_config_desc,
329         NULL,
330 };
331
332 struct usb_temp_device_desc usb_template_serialnet = {
333         .getStringDesc = &serialnet_get_string_desc,
334         .ppConfigDesc = serialnet_configs,
335         .idVendor = USB_TEMPLATE_VENDOR,
336         .idProduct = 0x0001,
337         .bcdDevice = 0x0100,
338         .bDeviceClass = UDCLASS_COMM,
339         .bDeviceSubClass = 0,
340         .bDeviceProtocol = 0,
341         .iManufacturer = SERIALNET_MANUFACTURER_INDEX,
342         .iProduct = SERIALNET_PRODUCT_INDEX,
343         .iSerialNumber = SERIALNET_SERIAL_NUMBER_INDEX,
344 };
345
346 /*------------------------------------------------------------------------*
347  *      serialnet_get_string_desc
348  *
349  * Return values:
350  * NULL: Failure. No such string.
351  * Else: Success. Pointer to string descriptor is returned.
352  *------------------------------------------------------------------------*/
353 static const void *
354 serialnet_get_string_desc(uint16_t lang_id, uint8_t string_index)
355 {
356         static const void *ptr[SERIALNET_MAX_INDEX] = {
357                 [SERIALNET_LANG_INDEX] = &usb_string_lang_en,
358                 [SERIALNET_MODEM_INDEX] = &serialnet_modem,
359                 [SERIALNET_ETH_MAC_INDEX] = &serialnet_eth_mac,
360                 [SERIALNET_ETH_CONTROL_INDEX] = &serialnet_eth_control,
361                 [SERIALNET_ETH_DATA_INDEX] = &serialnet_eth_data,
362                 [SERIALNET_CONFIGURATION_INDEX] = &serialnet_configuration,
363                 [SERIALNET_MANUFACTURER_INDEX] = &serialnet_manufacturer,
364                 [SERIALNET_PRODUCT_INDEX] = &serialnet_product,
365                 [SERIALNET_SERIAL_NUMBER_INDEX] = &serialnet_serial_number,
366         };
367
368         if (string_index == 0) {
369                 return (&usb_string_lang_en);
370         }
371         if (lang_id != 0x0409) {
372                 return (NULL);
373         }
374         if (string_index < SERIALNET_MAX_INDEX) {
375                 return (ptr[string_index]);
376         }
377         return (NULL);
378 }
379
380 static void
381 serialnet_init(void *arg __unused)
382 {
383         struct sysctl_oid *parent;
384         char parent_name[3];
385
386         usb_make_str_desc(&serialnet_modem, sizeof(serialnet_modem),
387             SERIALNET_DEFAULT_MODEM);
388         usb_make_str_desc(&serialnet_eth_mac, sizeof(serialnet_eth_mac),
389             SERIALNET_DEFAULT_ETH_MAC);
390         usb_make_str_desc(&serialnet_eth_control, sizeof(serialnet_eth_control),
391             SERIALNET_DEFAULT_ETH_CONTROL);
392         usb_make_str_desc(&serialnet_eth_data, sizeof(serialnet_eth_data),
393             SERIALNET_DEFAULT_ETH_DATA);
394         usb_make_str_desc(&serialnet_configuration, sizeof(serialnet_configuration),
395             SERIALNET_DEFAULT_CONFIGURATION);
396         usb_make_str_desc(&serialnet_manufacturer, sizeof(serialnet_manufacturer),
397             SERIALNET_DEFAULT_MANUFACTURER);
398         usb_make_str_desc(&serialnet_product, sizeof(serialnet_product),
399             SERIALNET_DEFAULT_PRODUCT);
400         usb_make_str_desc(&serialnet_serial_number, sizeof(serialnet_serial_number),
401             SERIALNET_DEFAULT_SERIAL_NUMBER);
402
403         snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_SERIALNET);
404         sysctl_ctx_init(&serialnet_ctx_list);
405
406         parent = SYSCTL_ADD_NODE(&serialnet_ctx_list,
407             SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
408             parent_name, CTLFLAG_RW,
409             0, "USB Mass Storage device side template");
410         SYSCTL_ADD_U16(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
411             "vendor_id", CTLFLAG_RWTUN,
412             &usb_template_serialnet.idVendor, 1, "Vendor identifier");
413         SYSCTL_ADD_U16(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
414             "product_id", CTLFLAG_RWTUN,
415             &usb_template_serialnet.idProduct, 1, "Product identifier");
416         SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
417             "eth_mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
418             &serialnet_eth_mac, sizeof(serialnet_eth_mac), usb_temp_sysctl,
419             "A", "Ethernet MAC address string");
420 #if 0
421         SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
422             "modem", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
423             &serialnet_modem, sizeof(serialnet_modem), usb_temp_sysctl,
424             "A", "Modem interface string");
425         SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
426             "eth_control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
427             &serialnet_eth_control, sizeof(serialnet_eth_data), usb_temp_sysctl,
428             "A", "Ethernet control interface string");
429         SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
430             "eth_data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
431             &serialnet_eth_data, sizeof(serialnet_eth_data), usb_temp_sysctl,
432             "A", "Ethernet data interface string");
433         SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
434             "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
435             &serialnet_configuration, sizeof(serialnet_configuration), usb_temp_sysctl,
436             "A", "Configuration string");
437 #endif
438         SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
439             "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
440             &serialnet_manufacturer, sizeof(serialnet_manufacturer), usb_temp_sysctl,
441             "A", "Manufacturer string");
442         SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
443             "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
444             &serialnet_product, sizeof(serialnet_product), usb_temp_sysctl,
445             "A", "Product string");
446         SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
447             "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
448             &serialnet_serial_number, sizeof(serialnet_serial_number), usb_temp_sysctl,
449             "A", "Serial number string");
450 }
451
452 static void
453 serialnet_uninit(void *arg __unused)
454 {
455
456         sysctl_ctx_free(&serialnet_ctx_list);
457 }
458
459 SYSINIT(serialnet_init, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_init, NULL);
460 SYSUNINIT(serialnet_init, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_uninit, NULL);