FreeBSD kernel usb device Code
usb_template_serialnet.c
Go to the documentation of this file.
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
73#endif /* USB_GLOBAL_INCLUDE_FILE */
74
75#define MODEM_IFACE_0 0
76#define MODEM_IFACE_1 1
77
78enum {
89};
90
91#define SERIALNET_DEFAULT_VENDOR_ID USB_TEMPLATE_VENDOR
92#define SERIALNET_DEFAULT_PRODUCT_ID 0x05dc
93#define SERIALNET_DEFAULT_MODEM "Virtual serial port"
94#define SERIALNET_DEFAULT_ETH_MAC "2A02030405060789AB"
95#define SERIALNET_DEFAULT_ETH_CONTROL "USB Ethernet Comm Interface"
96#define SERIALNET_DEFAULT_ETH_DATA "USB Ethernet Data Interface"
97#define SERIALNET_DEFAULT_CONFIGURATION "Default configuration"
98#define SERIALNET_DEFAULT_MANUFACTURER USB_TEMPLATE_MANUFACTURER
99#define SERIALNET_DEFAULT_PRODUCT "Serial/Ethernet device"
100/*
101 * The reason for this being called like this is that OSX
102 * derives the device node name from it, resulting in a somewhat
103 * user-friendly "/dev/cu.usbmodemFreeBSD1". And yes, the "1"
104 * needs to be there, otherwise OSX will mangle it.
105 */
106#define SERIALNET_DEFAULT_SERIAL_NUMBER "FreeBSD1"
107
116
117static struct sysctl_ctx_list serialnet_ctx_list;
118
119/* prototypes */
120
122
124 .bLength = sizeof(eth_union_desc),
127 .bMasterInterface = 0, /* this is automatically updated */
128 .bSlaveInterface[0] = 1, /* this is automatically updated */
129};
130
132 .bLength = sizeof(eth_header_desc),
135 .bcdCDC[0] = 0x10,
136 .bcdCDC[1] = 0x01,
137};
138
140 .bLength = sizeof(eth_enf_desc),
144 .bmEthernetStatistics = {0, 0, 0, 0},
145 .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
146 .wNumberMCFilters = {0, 0},
147 .bNumberPowerFilters = 0,
148};
149
150static const void *eth_control_if_desc[] = {
154 NULL,
155};
156
157static const struct usb_temp_packet_size bulk_mps = {
158 .mps[USB_SPEED_FULL] = 64,
159 .mps[USB_SPEED_HIGH] = 512,
160};
161
162static const struct usb_temp_packet_size intr_mps = {
163 .mps[USB_SPEED_FULL] = 8,
164 .mps[USB_SPEED_HIGH] = 8,
165};
166
167static const struct usb_temp_endpoint_desc bulk_in_ep = {
169#ifdef USB_HIP_IN_EP_0
170 .bEndpointAddress = USB_HIP_IN_EP_0,
171#else
172 .bEndpointAddress = UE_DIR_IN,
173#endif
174 .bmAttributes = UE_BULK,
175};
176
177static const struct usb_temp_endpoint_desc bulk_out_ep = {
179#ifdef USB_HIP_OUT_EP_0
180 .bEndpointAddress = USB_HIP_OUT_EP_0,
181#else
182 .bEndpointAddress = UE_DIR_OUT,
183#endif
184 .bmAttributes = UE_BULK,
185};
186
187static const struct usb_temp_endpoint_desc intr_in_ep = {
189 .bEndpointAddress = UE_DIR_IN,
190 .bmAttributes = UE_INTERRUPT,
191};
192
194 &intr_in_ep,
195 NULL,
196};
197
201 .bInterfaceClass = UICLASS_CDC,
203 .bInterfaceProtocol = UIPROTO_CDC_NONE,
204 .iInterface = SERIALNET_ETH_CONTROL_INDEX,
205};
206
208 &bulk_in_ep,
210 NULL,
211};
212
214 .ppEndpoints = NULL, /* no endpoints */
215 .bInterfaceClass = UICLASS_CDC_DATA,
216 .bInterfaceSubClass = UISUBCLASS_DATA,
217 .bInterfaceProtocol = 0,
218 .iInterface = SERIALNET_ETH_DATA_INDEX,
219};
220
223 .bInterfaceClass = UICLASS_CDC_DATA,
224 .bInterfaceSubClass = UISUBCLASS_DATA,
225 .bInterfaceProtocol = 0,
226 .iInterface = SERIALNET_ETH_DATA_INDEX,
227 .isAltInterface = 1, /* this is an alternate setting */
228};
229
231 .mps[USB_SPEED_LOW] = 8,
232 .mps[USB_SPEED_FULL] = 64,
233 .mps[USB_SPEED_HIGH] = 512,
234};
235
237 .mps[USB_SPEED_LOW] = 8,
238 .mps[USB_SPEED_FULL] = 8,
239 .mps[USB_SPEED_HIGH] = 8,
240};
241
243 .bInterval[USB_SPEED_LOW] = 8, /* 8ms */
244 .bInterval[USB_SPEED_FULL] = 8, /* 8ms */
245 .bInterval[USB_SPEED_HIGH] = 7, /* 8ms */
246};
247
248static const struct usb_temp_endpoint_desc modem_ep_0 = {
250 .pIntervals = &modem_intr_interval,
251 .bEndpointAddress = UE_DIR_IN,
252 .bmAttributes = UE_INTERRUPT,
253};
254
255static const struct usb_temp_endpoint_desc modem_ep_1 = {
257 .bEndpointAddress = UE_DIR_OUT,
258 .bmAttributes = UE_BULK,
259};
260
261static const struct usb_temp_endpoint_desc modem_ep_2 = {
263 .bEndpointAddress = UE_DIR_IN,
264 .bmAttributes = UE_BULK,
265};
266
267static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
268 &modem_ep_0,
269 NULL,
270};
271
272static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
273 &modem_ep_1,
274 &modem_ep_2,
275 NULL,
276};
277
278static const uint8_t modem_raw_desc_0[] = {
279 0x05, 0x24, 0x00, 0x10, 0x01
280};
281
282static const uint8_t modem_raw_desc_1[] = {
283 0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
284};
285
286static const uint8_t modem_raw_desc_2[] = {
287 0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
288};
289
290static const uint8_t modem_raw_desc_3[] = {
291 0x04, 0x24, 0x02, 0x07
292};
293
294static const void *modem_iface_0_desc[] = {
299 NULL,
300};
301
304 .ppEndpoints = modem_iface_0_ep,
305 .bInterfaceClass = UICLASS_CDC,
306 .bInterfaceSubClass = UISUBCLASS_ABSTRACT_CONTROL_MODEL,
307 .bInterfaceProtocol = UIPROTO_CDC_NONE,
308 .iInterface = SERIALNET_MODEM_INDEX,
309};
310
313 .bInterfaceClass = UICLASS_CDC_DATA,
314 .bInterfaceSubClass = UISUBCLASS_DATA,
315 .bInterfaceProtocol = 0,
316 .iInterface = SERIALNET_MODEM_INDEX,
317};
318
325 NULL,
326};
327
330 .bmAttributes = 0,
331 .bMaxPower = 0,
332 .iConfiguration = SERIALNET_CONFIGURATION_INDEX,
333};
334static const struct usb_temp_config_desc *serialnet_configs[] = {
336 NULL,
337};
338
341 .ppConfigDesc = serialnet_configs,
342 .idVendor = SERIALNET_DEFAULT_VENDOR_ID,
343 .idProduct = SERIALNET_DEFAULT_PRODUCT_ID,
344 .bcdDevice = 0x0100,
345 .bDeviceClass = UDCLASS_IN_INTERFACE,
346 .bDeviceSubClass = 0,
347 .bDeviceProtocol = 0,
348 .iManufacturer = SERIALNET_MANUFACTURER_INDEX,
349 .iProduct = SERIALNET_PRODUCT_INDEX,
350 .iSerialNumber = SERIALNET_SERIAL_NUMBER_INDEX,
351};
352
353/*------------------------------------------------------------------------*
354 * serialnet_get_string_desc
355 *
356 * Return values:
357 * NULL: Failure. No such string.
358 * Else: Success. Pointer to string descriptor is returned.
359 *------------------------------------------------------------------------*/
360static const void *
361serialnet_get_string_desc(uint16_t lang_id, uint8_t string_index)
362{
363 static const void *ptr[SERIALNET_MAX_INDEX] = {
373 };
374
375 if (string_index == 0) {
376 return (&usb_string_lang_en);
377 }
378 if (lang_id != 0x0409) {
379 return (NULL);
380 }
381 if (string_index < SERIALNET_MAX_INDEX) {
382 return (ptr[string_index]);
383 }
384 return (NULL);
385}
386
387static void
388serialnet_init(void *arg __unused)
389{
390 struct sysctl_oid *parent;
391 char parent_name[3];
392
409
410 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_SERIALNET);
411 sysctl_ctx_init(&serialnet_ctx_list);
412
413 parent = SYSCTL_ADD_NODE(&serialnet_ctx_list,
414 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
415 parent_name, CTLFLAG_RW | CTLFLAG_MPSAFE,
416 0, "USB CDC Serial/Ethernet device side template");
417 SYSCTL_ADD_U16(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
418 "vendor_id", CTLFLAG_RWTUN,
419 &usb_template_serialnet.idVendor, 1, "Vendor identifier");
420 SYSCTL_ADD_U16(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
421 "product_id", CTLFLAG_RWTUN,
422 &usb_template_serialnet.idProduct, 1, "Product identifier");
423 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
424 "eth_mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
426 "A", "Ethernet MAC address string");
427#if 0
428 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
429 "modem", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
431 "A", "Modem interface string");
432 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
433 "eth_control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
435 "A", "Ethernet control interface string");
436 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
437 "eth_data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
439 "A", "Ethernet data interface string");
440 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
441 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
443 "A", "Configuration string");
444#endif
445 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
446 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
448 "A", "Manufacturer string");
449 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
450 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
452 "A", "Product string");
453 SYSCTL_ADD_PROC(&serialnet_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
454 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
456 "A", "Serial number string");
457}
458
459static void
460serialnet_uninit(void *arg __unused)
461{
462
463 sysctl_ctx_free(&serialnet_ctx_list);
464}
465
466SYSINIT(serialnet_init, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_init, NULL);
467SYSUNINIT(serialnet_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_uninit, NULL);
uByte bSlaveInterface[1]
Definition: usb_cdc.h:90
const struct usb_temp_interface_desc ** ppIfaceDesc
Definition: usb_template.h:79
usb_temp_get_string_desc_t * getStringDesc
Definition: usb_template.h:86
const struct usb_temp_packet_size * pPacketSize
Definition: usb_template.h:57
const void ** ppRawDesc
Definition: usb_template.h:56
const struct usb_temp_endpoint_desc ** ppEndpoints
Definition: usb_template.h:70
const void ** ppRawDesc
Definition: usb_template.h:69
uint8_t bInterval[USB_SPEED_MAX]
Definition: usb_template.h:52
uint16_t mps[USB_SPEED_MAX]
Definition: usb_template.h:48
#define UIPROTO_CDC_NONE
Definition: usb.h:449
#define UE_INTERRUPT
Definition: usb.h:544
#define UICLASS_CDC_DATA
Definition: usb.h:485
#define UE_BULK
Definition: usb.h:543
#define UE_DIR_IN
Definition: usb.h:531
#define UICLASS_CDC
Definition: usb.h:434
@ USB_SPEED_LOW
Definition: usb.h:753
@ USB_SPEED_FULL
Definition: usb.h:754
@ USB_SPEED_HIGH
Definition: usb.h:755
#define UE_DIR_OUT
Definition: usb.h:532
#define UISUBCLASS_ABSTRACT_CONTROL_MODEL
Definition: usb.h:436
#define UISUBCLASS_DATA
Definition: usb.h:486
#define UDESC_CS_INTERFACE
Definition: usb.h:212
#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL
Definition: usb.h:440
#define UDCLASS_IN_INTERFACE
Definition: usb.h:371
#define UDESCSUB_CDC_UNION
Definition: usb_cdc.h:45
#define UDESCSUB_CDC_HEADER
Definition: usb_cdc.h:39
#define UDESCSUB_CDC_ENF
Definition: usb_cdc.h:54
const struct usb_string_lang usb_string_lang_en
Definition: usb_core.c:63
@ USB_TEMP_SERIALNET
Definition: usb_ioctl.h:60
int usb_temp_sysctl(SYSCTL_HANDLER_ARGS)
Definition: usb_template.c:173
const void *() usb_temp_get_string_desc_t(uint16_t lang_id, uint8_t string_index)
Definition: usb_template.h:44
static struct usb_string_descriptor serialnet_eth_mac
static const struct usb_temp_interface_desc eth_control_interface
SYSINIT(serialnet_init, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_init, NULL)
static const struct usb_temp_interface_desc modem_iface_1
static struct usb_string_descriptor serialnet_eth_data
#define SERIALNET_DEFAULT_VENDOR_ID
static const struct usb_temp_config_desc serialnet_config_desc
static const struct usb_temp_interface_desc eth_data_null_interface
#define SERIALNET_DEFAULT_ETH_MAC
static const uint8_t modem_raw_desc_0[]
static const struct usb_cdc_ethernet_descriptor eth_enf_desc
static void serialnet_uninit(void *arg __unused)
static const struct usb_temp_packet_size bulk_mps
#define SERIALNET_DEFAULT_MANUFACTURER
static const struct usb_temp_interval modem_intr_interval
#define SERIALNET_DEFAULT_ETH_CONTROL
static struct sysctl_ctx_list serialnet_ctx_list
SYSUNINIT(serialnet_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, serialnet_uninit, NULL)
@ SERIALNET_SERIAL_NUMBER_INDEX
@ SERIALNET_CONFIGURATION_INDEX
@ SERIALNET_MODEM_INDEX
@ SERIALNET_ETH_DATA_INDEX
@ SERIALNET_MAX_INDEX
@ SERIALNET_PRODUCT_INDEX
@ SERIALNET_MANUFACTURER_INDEX
@ SERIALNET_ETH_MAC_INDEX
@ SERIALNET_LANG_INDEX
@ SERIALNET_ETH_CONTROL_INDEX
#define SERIALNET_DEFAULT_SERIAL_NUMBER
#define SERIALNET_DEFAULT_MODEM
struct usb_temp_device_desc usb_template_serialnet
static struct usb_string_descriptor serialnet_manufacturer
static struct usb_string_descriptor serialnet_serial_number
static const struct usb_cdc_union_descriptor eth_union_desc
static const struct usb_temp_packet_size modem_intr_mps
static const struct usb_temp_endpoint_desc * eth_intr_endpoints[]
static struct usb_string_descriptor serialnet_product
static const struct usb_temp_endpoint_desc bulk_out_ep
static const struct usb_temp_packet_size modem_bulk_mps
#define SERIALNET_DEFAULT_CONFIGURATION
static const struct usb_temp_endpoint_desc bulk_in_ep
__FBSDID("$FreeBSD$")
static void serialnet_init(void *arg __unused)
static const struct usb_temp_packet_size intr_mps
static const void * eth_control_if_desc[]
static const uint8_t modem_raw_desc_1[]
static struct usb_string_descriptor serialnet_eth_control
static struct usb_string_descriptor serialnet_configuration
static struct usb_string_descriptor serialnet_modem
static const struct usb_temp_endpoint_desc modem_ep_0
static const uint8_t modem_raw_desc_3[]
static const struct usb_temp_endpoint_desc intr_in_ep
#define MODEM_IFACE_1
static const struct usb_temp_config_desc * serialnet_configs[]
static const struct usb_temp_endpoint_desc * eth_data_endpoints[]
static const struct usb_temp_endpoint_desc * modem_iface_0_ep[]
#define SERIALNET_DEFAULT_PRODUCT_ID
static const struct usb_temp_endpoint_desc * modem_iface_1_ep[]
static usb_temp_get_string_desc_t serialnet_get_string_desc
#define SERIALNET_DEFAULT_ETH_DATA
static const void * modem_iface_0_desc[]
static const struct usb_temp_interface_desc * serialnet_interfaces[]
#define MODEM_IFACE_0
#define SERIALNET_DEFAULT_PRODUCT
static const struct usb_temp_endpoint_desc modem_ep_1
static const struct usb_temp_interface_desc modem_iface_0
static const struct usb_cdc_header_descriptor eth_header_desc
static const uint8_t modem_raw_desc_2[]
static const struct usb_temp_interface_desc eth_data_interface
static const struct usb_temp_endpoint_desc modem_ep_2
uint8_t usb_make_str_desc(void *ptr, uint16_t max_len, const char *s)
Definition: usb_util.c:193