1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
5  * Copyright (c) 2018 The FreeBSD Foundation
6  * Copyright (c) 2019 Edward Tomasz Napierala <trasz@FreeBSD.org>
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 an USB Mass Storage 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_ioctl.h>
64 #include <dev/usb/usb_util.h>
65 
66 #include <dev/usb/template/usb_template.h>
67 #endif			/* USB_GLOBAL_INCLUDE_FILE */
68 
69 enum {
70 	CDCEEM_LANG_INDEX,
71 	CDCEEM_INTERFACE_INDEX,
72 	CDCEEM_CONFIGURATION_INDEX,
73 	CDCEEM_MANUFACTURER_INDEX,
74 	CDCEEM_PRODUCT_INDEX,
75 	CDCEEM_SERIAL_NUMBER_INDEX,
76 	CDCEEM_MAX_INDEX,
77 };
78 
79 #define	CDCEEM_DEFAULT_VENDOR_ID	USB_TEMPLATE_VENDOR
80 #define	CDCEEM_DEFAULT_PRODUCT_ID	0x27df
81 #define	CDCEEM_DEFAULT_INTERFACE	"USB CDC EEM Interface"
82 #define	CDCEEM_DEFAULT_CONFIGURATION	"Default Config"
83 #define	CDCEEM_DEFAULT_MANUFACTURER	USB_TEMPLATE_MANUFACTURER
84 #define	CDCEEM_DEFAULT_PRODUCT		"CDC EEM"
85 #define	CDCEEM_DEFAULT_SERIAL_NUMBER	"March 2008"
86 
87 static struct usb_string_descriptor	cdceem_interface;
88 static struct usb_string_descriptor	cdceem_configuration;
89 static struct usb_string_descriptor	cdceem_manufacturer;
90 static struct usb_string_descriptor	cdceem_product;
91 static struct usb_string_descriptor	cdceem_serial_number;
92 
93 static struct sysctl_ctx_list		cdceem_ctx_list;
94 
95 /* prototypes */
96 
97 static usb_temp_get_string_desc_t cdceem_get_string_desc;
98 
99 static const struct usb_temp_packet_size bulk_mps = {
100 	.mps[USB_SPEED_FULL] = 64,
101 	.mps[USB_SPEED_HIGH] = 512,
102 };
103 
104 static const struct usb_temp_endpoint_desc bulk_in_ep = {
105 	.pPacketSize = &bulk_mps,
106 #ifdef USB_HIP_IN_EP_0
107 	.bEndpointAddress = USB_HIP_IN_EP_0,
108 #else
109 	.bEndpointAddress = UE_DIR_IN,
110 #endif
111 	.bmAttributes = UE_BULK,
112 };
113 
114 static const struct usb_temp_endpoint_desc bulk_out_ep = {
115 	.pPacketSize = &bulk_mps,
116 #ifdef USB_HIP_OUT_EP_0
117 	.bEndpointAddress = USB_HIP_OUT_EP_0,
118 #else
119 	.bEndpointAddress = UE_DIR_OUT,
120 #endif
121 	.bmAttributes = UE_BULK,
122 };
123 
124 static const struct usb_temp_endpoint_desc *cdceem_data_endpoints[] = {
125 	&bulk_in_ep,
126 	&bulk_out_ep,
127 	NULL,
128 };
129 
130 static const struct usb_temp_interface_desc cdceem_data_interface = {
131 	.ppEndpoints = cdceem_data_endpoints,
132 	.bInterfaceClass = UICLASS_CDC,
133 	.bInterfaceSubClass = UISUBCLASS_ETHERNET_EMULATION_MODEL,
134 	.bInterfaceProtocol = UIPROTO_CDC_EEM,
135 	.iInterface = CDCEEM_INTERFACE_INDEX,
136 };
137 
138 static const struct usb_temp_interface_desc *cdceem_interfaces[] = {
139 	&cdceem_data_interface,
140 	NULL,
141 };
142 
143 static const struct usb_temp_config_desc cdceem_config_desc = {
144 	.ppIfaceDesc = cdceem_interfaces,
145 	.bmAttributes = 0,
146 	.bMaxPower = 0,
147 	.iConfiguration = CDCEEM_CONFIGURATION_INDEX,
148 };
149 
150 static const struct usb_temp_config_desc *cdceem_configs[] = {
151 	&cdceem_config_desc,
152 	NULL,
153 };
154 
155 struct usb_temp_device_desc usb_template_cdceem = {
156 	.getStringDesc = &cdceem_get_string_desc,
157 	.ppConfigDesc = cdceem_configs,
158 	.idVendor = CDCEEM_DEFAULT_VENDOR_ID,
159 	.idProduct = CDCEEM_DEFAULT_PRODUCT_ID,
160 	.bcdDevice = 0x0100,
161 	.bDeviceClass = UDCLASS_COMM,
162 	.bDeviceSubClass = 0,
163 	.bDeviceProtocol = 0,
164 	.iManufacturer = CDCEEM_MANUFACTURER_INDEX,
165 	.iProduct = CDCEEM_PRODUCT_INDEX,
166 	.iSerialNumber = CDCEEM_SERIAL_NUMBER_INDEX,
167 };
168 
169 /*------------------------------------------------------------------------*
170  *	cdceem_get_string_desc
171  *
172  * Return values:
173  * NULL: Failure. No such string.
174  * Else: Success. Pointer to string descriptor is returned.
175  *------------------------------------------------------------------------*/
176 static const void *
177 cdceem_get_string_desc(uint16_t lang_id, uint8_t string_index)
178 {
179 	static const void *ptr[CDCEEM_MAX_INDEX] = {
180 		[CDCEEM_LANG_INDEX] = &usb_string_lang_en,
181 		[CDCEEM_INTERFACE_INDEX] = &cdceem_interface,
182 		[CDCEEM_CONFIGURATION_INDEX] = &cdceem_configuration,
183 		[CDCEEM_MANUFACTURER_INDEX] = &cdceem_manufacturer,
184 		[CDCEEM_PRODUCT_INDEX] = &cdceem_product,
185 		[CDCEEM_SERIAL_NUMBER_INDEX] = &cdceem_serial_number,
186 	};
187 
188 	if (string_index == 0) {
189 		return (&usb_string_lang_en);
190 	}
191 	if (lang_id != 0x0409) {
192 		return (NULL);
193 	}
194 	if (string_index < CDCEEM_MAX_INDEX) {
195 		return (ptr[string_index]);
196 	}
197 	return (NULL);
198 }
199 
200 static void
201 cdceem_init(void *arg __unused)
202 {
203 	struct sysctl_oid *parent;
204 	char parent_name[3];
205 
206 	usb_make_str_desc(&cdceem_interface, sizeof(cdceem_interface),
207 	    CDCEEM_DEFAULT_INTERFACE);
208 	usb_make_str_desc(&cdceem_configuration, sizeof(cdceem_configuration),
209 	    CDCEEM_DEFAULT_CONFIGURATION);
210 	usb_make_str_desc(&cdceem_manufacturer, sizeof(cdceem_manufacturer),
211 	    CDCEEM_DEFAULT_MANUFACTURER);
212 	usb_make_str_desc(&cdceem_product, sizeof(cdceem_product),
213 	    CDCEEM_DEFAULT_PRODUCT);
214 	usb_make_str_desc(&cdceem_serial_number, sizeof(cdceem_serial_number),
215 	    CDCEEM_DEFAULT_SERIAL_NUMBER);
216 
217 	snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_CDCEEM);
218 	sysctl_ctx_init(&cdceem_ctx_list);
219 
220 	parent = SYSCTL_ADD_NODE(&cdceem_ctx_list,
221 	    SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
222 	    parent_name, CTLFLAG_RW | CTLFLAG_MPSAFE,
223 	    0, "USB CDC EEM device side template");
224 	SYSCTL_ADD_U16(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
225 	    "vendor_id", CTLFLAG_RWTUN,
226 	    &usb_template_cdceem.idVendor, 1, "Vendor identifier");
227 	SYSCTL_ADD_U16(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
228 	    "product_id", CTLFLAG_RWTUN,
229 	    &usb_template_cdceem.idProduct, 1, "Product identifier");
230 #if 0
231 	SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
232 	    "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
233 	    &cdceem_interface, sizeof(cdceem_interface), usb_temp_sysctl,
234 	    "A", "Interface string");
235 	SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
236 	    "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
237 	    &cdceem_configuration, sizeof(cdceem_configuration), usb_temp_sysctl,
238 	    "A", "Configuration string");
239 #endif
240 	SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
241 	    "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
242 	    &cdceem_manufacturer, sizeof(cdceem_manufacturer), usb_temp_sysctl,
243 	    "A", "Manufacturer string");
244 	SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
245 	    "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
246 	    &cdceem_product, sizeof(cdceem_product), usb_temp_sysctl,
247 	    "A", "Product string");
248 	SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
249 	    "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
250 	    &cdceem_serial_number, sizeof(cdceem_serial_number), usb_temp_sysctl,
251 	    "A", "Serial number string");
252 }
253 
254 static void
255 cdceem_uninit(void *arg __unused)
256 {
257 
258 	sysctl_ctx_free(&cdceem_ctx_list);
259 }
260 
261 SYSINIT(cdceem_init, SI_SUB_LOCK, SI_ORDER_FIRST, cdceem_init, NULL);
262 SYSUNINIT(cdceem_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, cdceem_uninit, NULL);
263