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