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