xref: /freebsd/sys/dev/usb/template/usb_template_msc.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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  * All rights reserved.
7  *
8  * Portions of this software were developed by Edward Tomasz Napierala
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * This file contains the USB templates for an USB Mass Storage Device.
35  */
36 
37 #ifdef USB_GLOBAL_INCLUDE_FILE
38 #include USB_GLOBAL_INCLUDE_FILE
39 #else
40 #include <sys/stdint.h>
41 #include <sys/stddef.h>
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/bus.h>
48 #include <sys/module.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/condvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/sx.h>
54 #include <sys/unistd.h>
55 #include <sys/callout.h>
56 #include <sys/malloc.h>
57 #include <sys/priv.h>
58 
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_ioctl.h>
63 #include <dev/usb/usb_util.h>
64 
65 #include <dev/usb/template/usb_template.h>
66 #endif			/* USB_GLOBAL_INCLUDE_FILE */
67 
68 enum {
69 	MSC_LANG_INDEX,
70 	MSC_INTERFACE_INDEX,
71 	MSC_CONFIGURATION_INDEX,
72 	MSC_MANUFACTURER_INDEX,
73 	MSC_PRODUCT_INDEX,
74 	MSC_SERIAL_NUMBER_INDEX,
75 	MSC_MAX_INDEX,
76 };
77 
78 #define	MSC_DEFAULT_VENDOR_ID		USB_TEMPLATE_VENDOR
79 #define	MSC_DEFAULT_PRODUCT_ID		0x27df
80 #define	MSC_DEFAULT_INTERFACE		"USB Mass Storage Interface"
81 #define	MSC_DEFAULT_CONFIGURATION	"Default Config"
82 #define	MSC_DEFAULT_MANUFACTURER	USB_TEMPLATE_MANUFACTURER
83 #define	MSC_DEFAULT_PRODUCT		"USB Memory Stick"
84 #define	MSC_DEFAULT_SERIAL_NUMBER	"March 2008"
85 
86 static struct usb_string_descriptor	msc_interface;
87 static struct usb_string_descriptor	msc_configuration;
88 static struct usb_string_descriptor	msc_manufacturer;
89 static struct usb_string_descriptor	msc_product;
90 static struct usb_string_descriptor	msc_serial_number;
91 
92 static struct sysctl_ctx_list		msc_ctx_list;
93 
94 /* prototypes */
95 
96 static usb_temp_get_string_desc_t msc_get_string_desc;
97 
98 static const struct usb_temp_packet_size bulk_mps = {
99 	.mps[USB_SPEED_FULL] = 64,
100 	.mps[USB_SPEED_HIGH] = 512,
101 };
102 
103 static const struct usb_temp_endpoint_desc bulk_in_ep = {
104 	.pPacketSize = &bulk_mps,
105 #ifdef USB_HIP_IN_EP_0
106 	.bEndpointAddress = USB_HIP_IN_EP_0,
107 #else
108 	.bEndpointAddress = UE_DIR_IN,
109 #endif
110 	.bmAttributes = UE_BULK,
111 };
112 
113 static const struct usb_temp_endpoint_desc bulk_out_ep = {
114 	.pPacketSize = &bulk_mps,
115 #ifdef USB_HIP_OUT_EP_0
116 	.bEndpointAddress = USB_HIP_OUT_EP_0,
117 #else
118 	.bEndpointAddress = UE_DIR_OUT,
119 #endif
120 	.bmAttributes = UE_BULK,
121 };
122 
123 static const struct usb_temp_endpoint_desc *msc_data_endpoints[] = {
124 	&bulk_in_ep,
125 	&bulk_out_ep,
126 	NULL,
127 };
128 
129 static const struct usb_temp_interface_desc msc_data_interface = {
130 	.ppEndpoints = msc_data_endpoints,
131 	.bInterfaceClass = UICLASS_MASS,
132 	.bInterfaceSubClass = UISUBCLASS_SCSI,
133 	.bInterfaceProtocol = UIPROTO_MASS_BBB,
134 	.iInterface = MSC_INTERFACE_INDEX,
135 };
136 
137 static const struct usb_temp_interface_desc *msc_interfaces[] = {
138 	&msc_data_interface,
139 	NULL,
140 };
141 
142 static const struct usb_temp_config_desc msc_config_desc = {
143 	.ppIfaceDesc = msc_interfaces,
144 	.bmAttributes = 0,
145 	.bMaxPower = 0,
146 	.iConfiguration = MSC_CONFIGURATION_INDEX,
147 };
148 
149 static const struct usb_temp_config_desc *msc_configs[] = {
150 	&msc_config_desc,
151 	NULL,
152 };
153 
154 struct usb_temp_device_desc usb_template_msc = {
155 	.getStringDesc = &msc_get_string_desc,
156 	.ppConfigDesc = msc_configs,
157 	.idVendor = MSC_DEFAULT_VENDOR_ID,
158 	.idProduct = MSC_DEFAULT_PRODUCT_ID,
159 	.bcdDevice = 0x0100,
160 	.bDeviceClass = UDCLASS_COMM,
161 	.bDeviceSubClass = 0,
162 	.bDeviceProtocol = 0,
163 	.iManufacturer = MSC_MANUFACTURER_INDEX,
164 	.iProduct = MSC_PRODUCT_INDEX,
165 	.iSerialNumber = MSC_SERIAL_NUMBER_INDEX,
166 };
167 
168 /*------------------------------------------------------------------------*
169  *	msc_get_string_desc
170  *
171  * Return values:
172  * NULL: Failure. No such string.
173  * Else: Success. Pointer to string descriptor is returned.
174  *------------------------------------------------------------------------*/
175 static const void *
176 msc_get_string_desc(uint16_t lang_id, uint8_t string_index)
177 {
178 	static const void *ptr[MSC_MAX_INDEX] = {
179 		[MSC_LANG_INDEX] = &usb_string_lang_en,
180 		[MSC_INTERFACE_INDEX] = &msc_interface,
181 		[MSC_CONFIGURATION_INDEX] = &msc_configuration,
182 		[MSC_MANUFACTURER_INDEX] = &msc_manufacturer,
183 		[MSC_PRODUCT_INDEX] = &msc_product,
184 		[MSC_SERIAL_NUMBER_INDEX] = &msc_serial_number,
185 	};
186 
187 	if (string_index == 0) {
188 		return (&usb_string_lang_en);
189 	}
190 	if (lang_id != 0x0409) {
191 		return (NULL);
192 	}
193 	if (string_index < MSC_MAX_INDEX) {
194 		return (ptr[string_index]);
195 	}
196 	return (NULL);
197 }
198 
199 static void
200 msc_init(void *arg __unused)
201 {
202 	struct sysctl_oid *parent;
203 	char parent_name[3];
204 
205 	usb_make_str_desc(&msc_interface, sizeof(msc_interface),
206 	    MSC_DEFAULT_INTERFACE);
207 	usb_make_str_desc(&msc_configuration, sizeof(msc_configuration),
208 	    MSC_DEFAULT_CONFIGURATION);
209 	usb_make_str_desc(&msc_manufacturer, sizeof(msc_manufacturer),
210 	    MSC_DEFAULT_MANUFACTURER);
211 	usb_make_str_desc(&msc_product, sizeof(msc_product),
212 	    MSC_DEFAULT_PRODUCT);
213 	usb_make_str_desc(&msc_serial_number, sizeof(msc_serial_number),
214 	    MSC_DEFAULT_SERIAL_NUMBER);
215 
216 	snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MSC);
217 	sysctl_ctx_init(&msc_ctx_list);
218 
219 	parent = SYSCTL_ADD_NODE(&msc_ctx_list,
220 	    SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
221 	    parent_name, CTLFLAG_RW | CTLFLAG_MPSAFE,
222 	    0, "USB Mass Storage device side template");
223 	SYSCTL_ADD_U16(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
224 	    "vendor_id", CTLFLAG_RWTUN,
225 	    &usb_template_msc.idVendor, 1, "Vendor identifier");
226 	SYSCTL_ADD_U16(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
227 	    "product_id", CTLFLAG_RWTUN,
228 	    &usb_template_msc.idProduct, 1, "Product identifier");
229 #if 0
230 	SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
231 	    "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
232 	    &msc_interface, sizeof(msc_interface), usb_temp_sysctl,
233 	    "A", "Interface string");
234 	SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
235 	    "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
236 	    &msc_configuration, sizeof(msc_configuration), usb_temp_sysctl,
237 	    "A", "Configuration string");
238 #endif
239 	SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
240 	    "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
241 	    &msc_manufacturer, sizeof(msc_manufacturer), usb_temp_sysctl,
242 	    "A", "Manufacturer string");
243 	SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
244 	    "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
245 	    &msc_product, sizeof(msc_product), usb_temp_sysctl,
246 	    "A", "Product string");
247 	SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
248 	    "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
249 	    &msc_serial_number, sizeof(msc_serial_number), usb_temp_sysctl,
250 	    "A", "Serial number string");
251 }
252 
253 static void
254 msc_uninit(void *arg __unused)
255 {
256 
257 	sysctl_ctx_free(&msc_ctx_list);
258 }
259 
260 SYSINIT(msc_init, SI_SUB_LOCK, SI_ORDER_FIRST, msc_init, NULL);
261 SYSUNINIT(msc_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, msc_uninit, NULL);
262