xref: /freebsd/sys/dev/usb/template/usb_template_msc.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1 /* $FreeBSD$ */
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause
4  *
5  * Copyright (c) 2008 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 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 	MSC_LANG_INDEX,
71 	MSC_INTERFACE_INDEX,
72 	MSC_CONFIGURATION_INDEX,
73 	MSC_MANUFACTURER_INDEX,
74 	MSC_PRODUCT_INDEX,
75 	MSC_SERIAL_NUMBER_INDEX,
76 	MSC_MAX_INDEX,
77 };
78 
79 #define	MSC_DEFAULT_VENDOR_ID		USB_TEMPLATE_VENDOR
80 #define	MSC_DEFAULT_PRODUCT_ID		0x27df
81 #define	MSC_DEFAULT_INTERFACE		"USB Mass Storage Interface"
82 #define	MSC_DEFAULT_CONFIGURATION	"Default Config"
83 #define	MSC_DEFAULT_MANUFACTURER	USB_TEMPLATE_MANUFACTURER
84 #define	MSC_DEFAULT_PRODUCT		"USB Memory Stick"
85 #define	MSC_DEFAULT_SERIAL_NUMBER	"March 2008"
86 
87 static struct usb_string_descriptor	msc_interface;
88 static struct usb_string_descriptor	msc_configuration;
89 static struct usb_string_descriptor	msc_manufacturer;
90 static struct usb_string_descriptor	msc_product;
91 static struct usb_string_descriptor	msc_serial_number;
92 
93 static struct sysctl_ctx_list		msc_ctx_list;
94 
95 /* prototypes */
96 
97 static usb_temp_get_string_desc_t msc_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 *msc_data_endpoints[] = {
125 	&bulk_in_ep,
126 	&bulk_out_ep,
127 	NULL,
128 };
129 
130 static const struct usb_temp_interface_desc msc_data_interface = {
131 	.ppEndpoints = msc_data_endpoints,
132 	.bInterfaceClass = UICLASS_MASS,
133 	.bInterfaceSubClass = UISUBCLASS_SCSI,
134 	.bInterfaceProtocol = UIPROTO_MASS_BBB,
135 	.iInterface = MSC_INTERFACE_INDEX,
136 };
137 
138 static const struct usb_temp_interface_desc *msc_interfaces[] = {
139 	&msc_data_interface,
140 	NULL,
141 };
142 
143 static const struct usb_temp_config_desc msc_config_desc = {
144 	.ppIfaceDesc = msc_interfaces,
145 	.bmAttributes = 0,
146 	.bMaxPower = 0,
147 	.iConfiguration = MSC_CONFIGURATION_INDEX,
148 };
149 
150 static const struct usb_temp_config_desc *msc_configs[] = {
151 	&msc_config_desc,
152 	NULL,
153 };
154 
155 struct usb_temp_device_desc usb_template_msc = {
156 	.getStringDesc = &msc_get_string_desc,
157 	.ppConfigDesc = msc_configs,
158 	.idVendor = MSC_DEFAULT_VENDOR_ID,
159 	.idProduct = MSC_DEFAULT_PRODUCT_ID,
160 	.bcdDevice = 0x0100,
161 	.bDeviceClass = UDCLASS_COMM,
162 	.bDeviceSubClass = 0,
163 	.bDeviceProtocol = 0,
164 	.iManufacturer = MSC_MANUFACTURER_INDEX,
165 	.iProduct = MSC_PRODUCT_INDEX,
166 	.iSerialNumber = MSC_SERIAL_NUMBER_INDEX,
167 };
168 
169 /*------------------------------------------------------------------------*
170  *	msc_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 msc_get_string_desc(uint16_t lang_id, uint8_t string_index)
178 {
179 	static const void *ptr[MSC_MAX_INDEX] = {
180 		[MSC_LANG_INDEX] = &usb_string_lang_en,
181 		[MSC_INTERFACE_INDEX] = &msc_interface,
182 		[MSC_CONFIGURATION_INDEX] = &msc_configuration,
183 		[MSC_MANUFACTURER_INDEX] = &msc_manufacturer,
184 		[MSC_PRODUCT_INDEX] = &msc_product,
185 		[MSC_SERIAL_NUMBER_INDEX] = &msc_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 < MSC_MAX_INDEX) {
195 		return (ptr[string_index]);
196 	}
197 	return (NULL);
198 }
199 
200 static void
201 msc_init(void *arg __unused)
202 {
203 	struct sysctl_oid *parent;
204 	char parent_name[3];
205 
206 	usb_make_str_desc(&msc_interface, sizeof(msc_interface),
207 	    MSC_DEFAULT_INTERFACE);
208 	usb_make_str_desc(&msc_configuration, sizeof(msc_configuration),
209 	    MSC_DEFAULT_CONFIGURATION);
210 	usb_make_str_desc(&msc_manufacturer, sizeof(msc_manufacturer),
211 	    MSC_DEFAULT_MANUFACTURER);
212 	usb_make_str_desc(&msc_product, sizeof(msc_product),
213 	    MSC_DEFAULT_PRODUCT);
214 	usb_make_str_desc(&msc_serial_number, sizeof(msc_serial_number),
215 	    MSC_DEFAULT_SERIAL_NUMBER);
216 
217 	snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MSC);
218 	sysctl_ctx_init(&msc_ctx_list);
219 
220 	parent = SYSCTL_ADD_NODE(&msc_ctx_list,
221 	    SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
222 	    parent_name, CTLFLAG_RW | CTLFLAG_MPSAFE,
223 	    0, "USB Mass Storage device side template");
224 	SYSCTL_ADD_U16(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
225 	    "vendor_id", CTLFLAG_RWTUN,
226 	    &usb_template_msc.idVendor, 1, "Vendor identifier");
227 	SYSCTL_ADD_U16(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
228 	    "product_id", CTLFLAG_RWTUN,
229 	    &usb_template_msc.idProduct, 1, "Product identifier");
230 #if 0
231 	SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
232 	    "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
233 	    &msc_interface, sizeof(msc_interface), usb_temp_sysctl,
234 	    "A", "Interface string");
235 	SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
236 	    "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
237 	    &msc_configuration, sizeof(msc_configuration), usb_temp_sysctl,
238 	    "A", "Configuration string");
239 #endif
240 	SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
241 	    "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
242 	    &msc_manufacturer, sizeof(msc_manufacturer), usb_temp_sysctl,
243 	    "A", "Manufacturer string");
244 	SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
245 	    "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
246 	    &msc_product, sizeof(msc_product), usb_temp_sysctl,
247 	    "A", "Product string");
248 	SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
249 	    "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
250 	    &msc_serial_number, sizeof(msc_serial_number), usb_temp_sysctl,
251 	    "A", "Serial number string");
252 }
253 
254 static void
255 msc_uninit(void *arg __unused)
256 {
257 
258 	sysctl_ctx_free(&msc_ctx_list);
259 }
260 
261 SYSINIT(msc_init, SI_SUB_LOCK, SI_ORDER_FIRST, msc_init, NULL);
262 SYSUNINIT(msc_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, msc_uninit, NULL);
263