1 /* $FreeBSD$ */ 2 /*- 3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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_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" 84 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; 90 91 static struct sysctl_ctx_list msc_ctx_list; 92 93 /* prototypes */ 94 95 static usb_temp_get_string_desc_t msc_get_string_desc; 96 97 static const struct usb_temp_packet_size bulk_mps = { 98 .mps[USB_SPEED_FULL] = 64, 99 .mps[USB_SPEED_HIGH] = 512, 100 }; 101 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, 106 #else 107 .bEndpointAddress = UE_DIR_IN, 108 #endif 109 .bmAttributes = UE_BULK, 110 }; 111 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, 116 #else 117 .bEndpointAddress = UE_DIR_OUT, 118 #endif 119 .bmAttributes = UE_BULK, 120 }; 121 122 static const struct usb_temp_endpoint_desc *msc_data_endpoints[] = { 123 &bulk_in_ep, 124 &bulk_out_ep, 125 NULL, 126 }; 127 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, 134 }; 135 136 static const struct usb_temp_interface_desc *msc_interfaces[] = { 137 &msc_data_interface, 138 NULL, 139 }; 140 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, 146 }; 147 148 static const struct usb_temp_config_desc *msc_configs[] = { 149 &msc_config_desc, 150 NULL, 151 }; 152 153 struct usb_temp_device_desc usb_template_msc = { 154 .getStringDesc = &msc_get_string_desc, 155 .ppConfigDesc = msc_configs, 156 .idVendor = USB_TEMPLATE_VENDOR, 157 .idProduct = 0x0012, 158 .bcdDevice = 0x0100, 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, 165 }; 166 167 /*------------------------------------------------------------------------* 168 * msc_get_string_desc 169 * 170 * Return values: 171 * NULL: Failure. No such string. 172 * Else: Success. Pointer to string descriptor is returned. 173 *------------------------------------------------------------------------*/ 174 static const void * 175 msc_get_string_desc(uint16_t lang_id, uint8_t string_index) 176 { 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, 184 }; 185 186 if (string_index == 0) { 187 return (&usb_string_lang_en); 188 } 189 if (lang_id != 0x0409) { 190 return (NULL); 191 } 192 if (string_index < MSC_MAX_INDEX) { 193 return (ptr[string_index]); 194 } 195 return (NULL); 196 } 197 198 static void 199 msc_init(void *arg __unused) 200 { 201 struct sysctl_oid *parent; 202 char parent_name[3]; 203 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); 214 215 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MSC); 216 sysctl_ctx_init(&msc_ctx_list); 217 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"); 228 #if 0 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"); 237 #endif 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"); 250 } 251 252 static void 253 msc_uninit(void *arg __unused) 254 { 255 256 sysctl_ctx_free(&msc_ctx_list); 257 } 258 259 SYSINIT(msc_init, SI_SUB_LOCK, SI_ORDER_FIRST, msc_init, NULL); 260 SYSUNINIT(msc_init, SI_SUB_LOCK, SI_ORDER_FIRST, msc_uninit, NULL); 261