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
9 * Portions of this software were developed by Edward Tomasz Napierala
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
35 * This file contains the USB templates for an USB Mass Storage Device.
38 #ifdef USB_GLOBAL_INCLUDE_FILE
39 #include USB_GLOBAL_INCLUDE_FILE
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>
49 #include <sys/module.h>
51 #include <sys/mutex.h>
52 #include <sys/condvar.h>
53 #include <sys/sysctl.h>
55 #include <sys/unistd.h>
56 #include <sys/callout.h>
57 #include <sys/malloc.h>
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>
66 #include <dev/usb/template/usb_template.h>
67 #endif /* USB_GLOBAL_INCLUDE_FILE */
72 MSC_CONFIGURATION_INDEX,
73 MSC_MANUFACTURER_INDEX,
75 MSC_SERIAL_NUMBER_INDEX,
79 #define MSC_DEFAULT_INTERFACE "USB Mass Storage Interface"
80 #define MSC_DEFAULT_CONFIGURATION "Default Config"
81 #define MSC_DEFAULT_MANUFACTURER "FreeBSD foundation"
82 #define MSC_DEFAULT_PRODUCT "USB Memory Stick"
83 #define MSC_DEFAULT_SERIAL_NUMBER "March 2008"
85 static struct usb_string_descriptor msc_interface;
86 static struct usb_string_descriptor msc_configuration;
87 static struct usb_string_descriptor msc_manufacturer;
88 static struct usb_string_descriptor msc_product;
89 static struct usb_string_descriptor msc_serial_number;
91 static struct sysctl_ctx_list msc_ctx_list;
95 static usb_temp_get_string_desc_t msc_get_string_desc;
97 static const struct usb_temp_packet_size bulk_mps = {
98 .mps[USB_SPEED_FULL] = 64,
99 .mps[USB_SPEED_HIGH] = 512,
102 static const struct usb_temp_endpoint_desc bulk_in_ep = {
103 .pPacketSize = &bulk_mps,
104 #ifdef USB_HIP_IN_EP_0
105 .bEndpointAddress = USB_HIP_IN_EP_0,
107 .bEndpointAddress = UE_DIR_IN,
109 .bmAttributes = UE_BULK,
112 static const struct usb_temp_endpoint_desc bulk_out_ep = {
113 .pPacketSize = &bulk_mps,
114 #ifdef USB_HIP_OUT_EP_0
115 .bEndpointAddress = USB_HIP_OUT_EP_0,
117 .bEndpointAddress = UE_DIR_OUT,
119 .bmAttributes = UE_BULK,
122 static const struct usb_temp_endpoint_desc *msc_data_endpoints[] = {
128 static const struct usb_temp_interface_desc msc_data_interface = {
129 .ppEndpoints = msc_data_endpoints,
130 .bInterfaceClass = UICLASS_MASS,
131 .bInterfaceSubClass = UISUBCLASS_SCSI,
132 .bInterfaceProtocol = UIPROTO_MASS_BBB,
133 .iInterface = MSC_INTERFACE_INDEX,
136 static const struct usb_temp_interface_desc *msc_interfaces[] = {
141 static const struct usb_temp_config_desc msc_config_desc = {
142 .ppIfaceDesc = msc_interfaces,
143 .bmAttributes = UC_BUS_POWERED,
144 .bMaxPower = 25, /* 50 mA */
145 .iConfiguration = MSC_CONFIGURATION_INDEX,
148 static const struct usb_temp_config_desc *msc_configs[] = {
153 struct usb_temp_device_desc usb_template_msc = {
154 .getStringDesc = &msc_get_string_desc,
155 .ppConfigDesc = msc_configs,
156 .idVendor = USB_TEMPLATE_VENDOR,
159 .bDeviceClass = UDCLASS_COMM,
160 .bDeviceSubClass = 0,
161 .bDeviceProtocol = 0,
162 .iManufacturer = MSC_MANUFACTURER_INDEX,
163 .iProduct = MSC_PRODUCT_INDEX,
164 .iSerialNumber = MSC_SERIAL_NUMBER_INDEX,
167 /*------------------------------------------------------------------------*
168 * msc_get_string_desc
171 * NULL: Failure. No such string.
172 * Else: Success. Pointer to string descriptor is returned.
173 *------------------------------------------------------------------------*/
175 msc_get_string_desc(uint16_t lang_id, uint8_t string_index)
177 static const void *ptr[MSC_MAX_INDEX] = {
178 [MSC_LANG_INDEX] = &usb_string_lang_en,
179 [MSC_INTERFACE_INDEX] = &msc_interface,
180 [MSC_CONFIGURATION_INDEX] = &msc_configuration,
181 [MSC_MANUFACTURER_INDEX] = &msc_manufacturer,
182 [MSC_PRODUCT_INDEX] = &msc_product,
183 [MSC_SERIAL_NUMBER_INDEX] = &msc_serial_number,
186 if (string_index == 0) {
187 return (&usb_string_lang_en);
189 if (lang_id != 0x0409) {
192 if (string_index < MSC_MAX_INDEX) {
193 return (ptr[string_index]);
199 msc_init(void *arg __unused)
201 struct sysctl_oid *parent;
204 usb_make_str_desc(&msc_interface, sizeof(msc_interface),
205 MSC_DEFAULT_INTERFACE);
206 usb_make_str_desc(&msc_configuration, sizeof(msc_configuration),
207 MSC_DEFAULT_CONFIGURATION);
208 usb_make_str_desc(&msc_manufacturer, sizeof(msc_manufacturer),
209 MSC_DEFAULT_MANUFACTURER);
210 usb_make_str_desc(&msc_product, sizeof(msc_product),
211 MSC_DEFAULT_PRODUCT);
212 usb_make_str_desc(&msc_serial_number, sizeof(msc_serial_number),
213 MSC_DEFAULT_SERIAL_NUMBER);
215 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MSC);
216 sysctl_ctx_init(&msc_ctx_list);
218 parent = SYSCTL_ADD_NODE(&msc_ctx_list,
219 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
220 parent_name, CTLFLAG_RW,
221 0, "USB Mass Storage device side template");
222 SYSCTL_ADD_U16(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
223 "vendor_id", CTLFLAG_RWTUN,
224 &usb_template_msc.idVendor, 1, "Vendor identifier");
225 SYSCTL_ADD_U16(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
226 "product_id", CTLFLAG_RWTUN,
227 &usb_template_msc.idProduct, 1, "Product identifier");
229 SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
230 "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
231 &msc_interface, sizeof(msc_interface), usb_temp_sysctl,
232 "A", "Interface string");
233 SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
234 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
235 &msc_configuration, sizeof(msc_configuration), usb_temp_sysctl,
236 "A", "Configuration string");
238 SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
239 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
240 &msc_manufacturer, sizeof(msc_manufacturer), usb_temp_sysctl,
241 "A", "Manufacturer string");
242 SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
243 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
244 &msc_product, sizeof(msc_product), usb_temp_sysctl,
245 "A", "Product string");
246 SYSCTL_ADD_PROC(&msc_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
247 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
248 &msc_serial_number, sizeof(msc_serial_number), usb_temp_sysctl,
249 "A", "Serial number string");
253 msc_uninit(void *arg __unused)
256 sysctl_ctx_free(&msc_ctx_list);
259 SYSINIT(msc_init, SI_SUB_LOCK, SI_ORDER_FIRST, msc_init, NULL);
260 SYSUNINIT(msc_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, msc_uninit, NULL);