FreeBSD kernel usb device Code
usb_template_cdceem.c
Go to the documentation of this file.
1/* $FreeBSD$ */
2/*-
3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 *
5 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
6 * Copyright (c) 2018 The FreeBSD Foundation
7 * Copyright (c) 2019 Edward Tomasz Napierala <trasz@FreeBSD.org>
8 * All rights reserved.
9 *
10 * Portions of this software were developed by Edward Tomasz Napierala
11 * under sponsorship from the FreeBSD Foundation.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35/*
36 * This file contains the USB templates for an USB Mass Storage Device.
37 */
38
39#ifdef USB_GLOBAL_INCLUDE_FILE
40#include USB_GLOBAL_INCLUDE_FILE
41#else
42#include <sys/stdint.h>
43#include <sys/stddef.h>
44#include <sys/param.h>
45#include <sys/queue.h>
46#include <sys/types.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/bus.h>
50#include <sys/module.h>
51#include <sys/lock.h>
52#include <sys/mutex.h>
53#include <sys/condvar.h>
54#include <sys/sysctl.h>
55#include <sys/sx.h>
56#include <sys/unistd.h>
57#include <sys/callout.h>
58#include <sys/malloc.h>
59#include <sys/priv.h>
60
61#include <dev/usb/usb.h>
62#include <dev/usb/usbdi.h>
63#include <dev/usb/usb_core.h>
64#include <dev/usb/usb_ioctl.h>
65#include <dev/usb/usb_util.h>
66
68#endif /* USB_GLOBAL_INCLUDE_FILE */
69
70enum {
78};
79
80#define CDCEEM_DEFAULT_VENDOR_ID USB_TEMPLATE_VENDOR
81#define CDCEEM_DEFAULT_PRODUCT_ID 0x27df
82#define CDCEEM_DEFAULT_INTERFACE "USB CDC EEM Interface"
83#define CDCEEM_DEFAULT_CONFIGURATION "Default Config"
84#define CDCEEM_DEFAULT_MANUFACTURER USB_TEMPLATE_MANUFACTURER
85#define CDCEEM_DEFAULT_PRODUCT "CDC EEM"
86#define CDCEEM_DEFAULT_SERIAL_NUMBER "March 2008"
87
93
94static struct sysctl_ctx_list cdceem_ctx_list;
95
96/* prototypes */
97
99
100static const struct usb_temp_packet_size bulk_mps = {
101 .mps[USB_SPEED_FULL] = 64,
102 .mps[USB_SPEED_HIGH] = 512,
103};
104
105static const struct usb_temp_endpoint_desc bulk_in_ep = {
107#ifdef USB_HIP_IN_EP_0
108 .bEndpointAddress = USB_HIP_IN_EP_0,
109#else
110 .bEndpointAddress = UE_DIR_IN,
111#endif
112 .bmAttributes = UE_BULK,
113};
114
115static const struct usb_temp_endpoint_desc bulk_out_ep = {
117#ifdef USB_HIP_OUT_EP_0
118 .bEndpointAddress = USB_HIP_OUT_EP_0,
119#else
120 .bEndpointAddress = UE_DIR_OUT,
121#endif
122 .bmAttributes = UE_BULK,
123};
124
126 &bulk_in_ep,
128 NULL,
129};
130
133 .bInterfaceClass = UICLASS_CDC,
134 .bInterfaceSubClass = UISUBCLASS_ETHERNET_EMULATION_MODEL,
135 .bInterfaceProtocol = UIPROTO_CDC_EEM,
136 .iInterface = CDCEEM_INTERFACE_INDEX,
137};
138
141 NULL,
142};
143
146 .bmAttributes = 0,
147 .bMaxPower = 0,
148 .iConfiguration = CDCEEM_CONFIGURATION_INDEX,
149};
150
151static const struct usb_temp_config_desc *cdceem_configs[] = {
153 NULL,
154};
155
158 .ppConfigDesc = cdceem_configs,
159 .idVendor = CDCEEM_DEFAULT_VENDOR_ID,
160 .idProduct = CDCEEM_DEFAULT_PRODUCT_ID,
161 .bcdDevice = 0x0100,
162 .bDeviceClass = UDCLASS_COMM,
163 .bDeviceSubClass = 0,
164 .bDeviceProtocol = 0,
165 .iManufacturer = CDCEEM_MANUFACTURER_INDEX,
166 .iProduct = CDCEEM_PRODUCT_INDEX,
167 .iSerialNumber = CDCEEM_SERIAL_NUMBER_INDEX,
168};
169
170/*------------------------------------------------------------------------*
171 * cdceem_get_string_desc
172 *
173 * Return values:
174 * NULL: Failure. No such string.
175 * Else: Success. Pointer to string descriptor is returned.
176 *------------------------------------------------------------------------*/
177static const void *
178cdceem_get_string_desc(uint16_t lang_id, uint8_t string_index)
179{
180 static const void *ptr[CDCEEM_MAX_INDEX] = {
187 };
188
189 if (string_index == 0) {
190 return (&usb_string_lang_en);
191 }
192 if (lang_id != 0x0409) {
193 return (NULL);
194 }
195 if (string_index < CDCEEM_MAX_INDEX) {
196 return (ptr[string_index]);
197 }
198 return (NULL);
199}
200
201static void
202cdceem_init(void *arg __unused)
203{
204 struct sysctl_oid *parent;
205 char parent_name[3];
206
217
218 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_CDCEEM);
219 sysctl_ctx_init(&cdceem_ctx_list);
220
221 parent = SYSCTL_ADD_NODE(&cdceem_ctx_list,
222 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
223 parent_name, CTLFLAG_RW | CTLFLAG_MPSAFE,
224 0, "USB CDC EEM device side template");
225 SYSCTL_ADD_U16(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
226 "vendor_id", CTLFLAG_RWTUN,
227 &usb_template_cdceem.idVendor, 1, "Vendor identifier");
228 SYSCTL_ADD_U16(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
229 "product_id", CTLFLAG_RWTUN,
230 &usb_template_cdceem.idProduct, 1, "Product identifier");
231#if 0
232 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
233 "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
235 "A", "Interface string");
236 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
237 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
239 "A", "Configuration string");
240#endif
241 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
242 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
244 "A", "Manufacturer string");
245 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
246 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
248 "A", "Product string");
249 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
250 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
252 "A", "Serial number string");
253}
254
255static void
256cdceem_uninit(void *arg __unused)
257{
258
259 sysctl_ctx_free(&cdceem_ctx_list);
260}
261
262SYSINIT(cdceem_init, SI_SUB_LOCK, SI_ORDER_FIRST, cdceem_init, NULL);
263SYSUNINIT(cdceem_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, cdceem_uninit, NULL);
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 struct usb_temp_endpoint_desc ** ppEndpoints
Definition: usb_template.h:70
uint16_t mps[USB_SPEED_MAX]
Definition: usb_template.h:48
#define UE_BULK
Definition: usb.h:543
#define UIPROTO_CDC_EEM
Definition: usb.h:451
#define UE_DIR_IN
Definition: usb.h:531
#define UISUBCLASS_ETHERNET_EMULATION_MODEL
Definition: usb.h:446
#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 UDCLASS_COMM
Definition: usb.h:372
const struct usb_string_lang usb_string_lang_en
Definition: usb_core.c:63
@ USB_TEMP_CDCEEM
Definition: usb_ioctl.h:63
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 const struct usb_temp_interface_desc * cdceem_interfaces[]
static struct usb_string_descriptor cdceem_product
#define CDCEEM_DEFAULT_VENDOR_ID
static void cdceem_init(void *arg __unused)
static void cdceem_uninit(void *arg __unused)
struct usb_temp_device_desc usb_template_cdceem
#define CDCEEM_DEFAULT_CONFIGURATION
#define CDCEEM_DEFAULT_MANUFACTURER
static struct usb_string_descriptor cdceem_interface
static const struct usb_temp_interface_desc cdceem_data_interface
static const struct usb_temp_config_desc cdceem_config_desc
#define CDCEEM_DEFAULT_PRODUCT_ID
static const struct usb_temp_packet_size bulk_mps
static struct usb_string_descriptor cdceem_configuration
static struct usb_string_descriptor cdceem_serial_number
#define CDCEEM_DEFAULT_INTERFACE
static usb_temp_get_string_desc_t cdceem_get_string_desc
@ CDCEEM_CONFIGURATION_INDEX
@ CDCEEM_SERIAL_NUMBER_INDEX
@ CDCEEM_PRODUCT_INDEX
@ CDCEEM_MAX_INDEX
@ CDCEEM_MANUFACTURER_INDEX
@ CDCEEM_INTERFACE_INDEX
@ CDCEEM_LANG_INDEX
static const struct usb_temp_endpoint_desc bulk_out_ep
#define CDCEEM_DEFAULT_SERIAL_NUMBER
static const struct usb_temp_endpoint_desc bulk_in_ep
#define CDCEEM_DEFAULT_PRODUCT
static const struct usb_temp_config_desc * cdceem_configs[]
SYSINIT(cdceem_init, SI_SUB_LOCK, SI_ORDER_FIRST, cdceem_init, NULL)
static struct sysctl_ctx_list cdceem_ctx_list
static const struct usb_temp_endpoint_desc * cdceem_data_endpoints[]
SYSUNINIT(cdceem_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, cdceem_uninit, NULL)
static struct usb_string_descriptor cdceem_manufacturer
uint8_t usb_make_str_desc(void *ptr, uint16_t max_len, const char *s)
Definition: usb_util.c:193