3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
10 * Portions of this software were developed by Edward Tomasz Napierala
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
36 * This file contains the USB templates for an USB Mass Storage Device.
39 #ifdef USB_GLOBAL_INCLUDE_FILE
40 #include USB_GLOBAL_INCLUDE_FILE
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>
50 #include <sys/module.h>
52 #include <sys/mutex.h>
53 #include <sys/condvar.h>
54 #include <sys/sysctl.h>
56 #include <sys/unistd.h>
57 #include <sys/callout.h>
58 #include <sys/malloc.h>
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>
67 #include <dev/usb/template/usb_template.h>
68 #endif /* USB_GLOBAL_INCLUDE_FILE */
72 CDCEEM_INTERFACE_INDEX,
73 CDCEEM_CONFIGURATION_INDEX,
74 CDCEEM_MANUFACTURER_INDEX,
76 CDCEEM_SERIAL_NUMBER_INDEX,
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"
88 static struct usb_string_descriptor cdceem_interface;
89 static struct usb_string_descriptor cdceem_configuration;
90 static struct usb_string_descriptor cdceem_manufacturer;
91 static struct usb_string_descriptor cdceem_product;
92 static struct usb_string_descriptor cdceem_serial_number;
94 static struct sysctl_ctx_list cdceem_ctx_list;
98 static usb_temp_get_string_desc_t cdceem_get_string_desc;
100 static const struct usb_temp_packet_size bulk_mps = {
101 .mps[USB_SPEED_FULL] = 64,
102 .mps[USB_SPEED_HIGH] = 512,
105 static const struct usb_temp_endpoint_desc bulk_in_ep = {
106 .pPacketSize = &bulk_mps,
107 #ifdef USB_HIP_IN_EP_0
108 .bEndpointAddress = USB_HIP_IN_EP_0,
110 .bEndpointAddress = UE_DIR_IN,
112 .bmAttributes = UE_BULK,
115 static const struct usb_temp_endpoint_desc bulk_out_ep = {
116 .pPacketSize = &bulk_mps,
117 #ifdef USB_HIP_OUT_EP_0
118 .bEndpointAddress = USB_HIP_OUT_EP_0,
120 .bEndpointAddress = UE_DIR_OUT,
122 .bmAttributes = UE_BULK,
125 static const struct usb_temp_endpoint_desc *cdceem_data_endpoints[] = {
131 static const struct usb_temp_interface_desc cdceem_data_interface = {
132 .ppEndpoints = cdceem_data_endpoints,
133 .bInterfaceClass = UICLASS_CDC,
134 .bInterfaceSubClass = UISUBCLASS_ETHERNET_EMULATION_MODEL,
135 .bInterfaceProtocol = UIPROTO_CDC_EEM,
136 .iInterface = CDCEEM_INTERFACE_INDEX,
139 static const struct usb_temp_interface_desc *cdceem_interfaces[] = {
140 &cdceem_data_interface,
144 static const struct usb_temp_config_desc cdceem_config_desc = {
145 .ppIfaceDesc = cdceem_interfaces,
148 .iConfiguration = CDCEEM_CONFIGURATION_INDEX,
151 static const struct usb_temp_config_desc *cdceem_configs[] = {
156 struct usb_temp_device_desc usb_template_cdceem = {
157 .getStringDesc = &cdceem_get_string_desc,
158 .ppConfigDesc = cdceem_configs,
159 .idVendor = CDCEEM_DEFAULT_VENDOR_ID,
160 .idProduct = CDCEEM_DEFAULT_PRODUCT_ID,
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,
170 /*------------------------------------------------------------------------*
171 * cdceem_get_string_desc
174 * NULL: Failure. No such string.
175 * Else: Success. Pointer to string descriptor is returned.
176 *------------------------------------------------------------------------*/
178 cdceem_get_string_desc(uint16_t lang_id, uint8_t string_index)
180 static const void *ptr[CDCEEM_MAX_INDEX] = {
181 [CDCEEM_LANG_INDEX] = &usb_string_lang_en,
182 [CDCEEM_INTERFACE_INDEX] = &cdceem_interface,
183 [CDCEEM_CONFIGURATION_INDEX] = &cdceem_configuration,
184 [CDCEEM_MANUFACTURER_INDEX] = &cdceem_manufacturer,
185 [CDCEEM_PRODUCT_INDEX] = &cdceem_product,
186 [CDCEEM_SERIAL_NUMBER_INDEX] = &cdceem_serial_number,
189 if (string_index == 0) {
190 return (&usb_string_lang_en);
192 if (lang_id != 0x0409) {
195 if (string_index < CDCEEM_MAX_INDEX) {
196 return (ptr[string_index]);
202 cdceem_init(void *arg __unused)
204 struct sysctl_oid *parent;
207 usb_make_str_desc(&cdceem_interface, sizeof(cdceem_interface),
208 CDCEEM_DEFAULT_INTERFACE);
209 usb_make_str_desc(&cdceem_configuration, sizeof(cdceem_configuration),
210 CDCEEM_DEFAULT_CONFIGURATION);
211 usb_make_str_desc(&cdceem_manufacturer, sizeof(cdceem_manufacturer),
212 CDCEEM_DEFAULT_MANUFACTURER);
213 usb_make_str_desc(&cdceem_product, sizeof(cdceem_product),
214 CDCEEM_DEFAULT_PRODUCT);
215 usb_make_str_desc(&cdceem_serial_number, sizeof(cdceem_serial_number),
216 CDCEEM_DEFAULT_SERIAL_NUMBER);
218 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_CDCEEM);
219 sysctl_ctx_init(&cdceem_ctx_list);
221 parent = SYSCTL_ADD_NODE(&cdceem_ctx_list,
222 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
223 parent_name, CTLFLAG_RW,
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");
232 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
233 "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
234 &cdceem_interface, sizeof(cdceem_interface), usb_temp_sysctl,
235 "A", "Interface string");
236 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
237 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
238 &cdceem_configuration, sizeof(cdceem_configuration), usb_temp_sysctl,
239 "A", "Configuration string");
241 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
242 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
243 &cdceem_manufacturer, sizeof(cdceem_manufacturer), usb_temp_sysctl,
244 "A", "Manufacturer string");
245 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
246 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
247 &cdceem_product, sizeof(cdceem_product), usb_temp_sysctl,
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,
251 &cdceem_serial_number, sizeof(cdceem_serial_number), usb_temp_sysctl,
252 "A", "Serial number string");
256 cdceem_uninit(void *arg __unused)
259 sysctl_ctx_free(&cdceem_ctx_list);
262 SYSINIT(cdceem_init, SI_SUB_LOCK, SI_ORDER_FIRST, cdceem_init, NULL);
263 SYSUNINIT(cdceem_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, cdceem_uninit, NULL);