1 #include <sys/cdefs.h> 2 __FBSDID("$FreeBSD$"); 3 4 /*- 5 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * This file contains the USB templates for an USB Mass Storage Device. 32 */ 33 34 #include <sys/stdint.h> 35 #include <sys/stddef.h> 36 #include <sys/param.h> 37 #include <sys/queue.h> 38 #include <sys/types.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/bus.h> 42 #include <sys/linker_set.h> 43 #include <sys/module.h> 44 #include <sys/lock.h> 45 #include <sys/mutex.h> 46 #include <sys/condvar.h> 47 #include <sys/sysctl.h> 48 #include <sys/sx.h> 49 #include <sys/unistd.h> 50 #include <sys/callout.h> 51 #include <sys/malloc.h> 52 #include <sys/priv.h> 53 54 #include <dev/usb/usb.h> 55 #include <dev/usb/usbdi.h> 56 57 #include <dev/usb/template/usb_template.h> 58 59 enum { 60 STRING_LANG_INDEX, 61 STRING_MSC_DATA_INDEX, 62 STRING_MSC_CONFIG_INDEX, 63 STRING_MSC_VENDOR_INDEX, 64 STRING_MSC_PRODUCT_INDEX, 65 STRING_MSC_SERIAL_INDEX, 66 STRING_MSC_MAX, 67 }; 68 69 #define STRING_LANG \ 70 0x09, 0x04, /* American English */ 71 72 #define STRING_MSC_DATA \ 73 'U', 0, 'S', 0, 'B', 0, ' ', 0, \ 74 'M', 0, 'a', 0, 's', 0, 's', 0, \ 75 ' ', 0, 'S', 0, 't', 0, 'o', 0, \ 76 'r', 0, 'a', 0, 'g', 0, 'e', 0, \ 77 ' ', 0, 'I', 0, 'n', 0, 't', 0, \ 78 'e', 0, 'r', 0, 'f', 0, 'a', 0, \ 79 'c', 0, 'e', 0, 80 81 #define STRING_MSC_CONFIG \ 82 'D', 0, 'e', 0, 'f', 0, 'a', 0, \ 83 'u', 0, 'l', 0, 't', 0, ' ', 0, \ 84 'c', 0, 'o', 0, 'n', 0, 'f', 0, \ 85 'i', 0, 'g', 0, 86 87 #define STRING_MSC_VENDOR \ 88 'F', 0, 'r', 0, 'e', 0, 'e', 0, \ 89 'B', 0, 'S', 0, 'D', 0, ' ', 0, \ 90 'f', 0, 'o', 0, 'u', 0, 'n', 0, \ 91 'd', 0, 'a', 0, 't', 0, 'i', 0, \ 92 'o', 0, 'n', 0, 93 94 #define STRING_MSC_PRODUCT \ 95 'U', 0, 'S', 0, 'B', 0, ' ', 0, \ 96 'M', 0, 'e', 0, 'm', 0, 'o', 0, \ 97 'r', 0, 'y', 0, ' ', 0, 'S', 0, \ 98 't', 0, 'i', 0, 'c', 0, 'k', 0 99 100 #define STRING_MSC_SERIAL \ 101 'M', 0, 'a', 0, 'r', 0, 'c', 0, \ 102 'h', 0, ' ', 0, '2', 0, '0', 0, \ 103 '0', 0, '8', 0, 104 105 /* make the real string descriptors */ 106 107 USB_MAKE_STRING_DESC(STRING_LANG, string_lang); 108 USB_MAKE_STRING_DESC(STRING_MSC_DATA, string_msc_data); 109 USB_MAKE_STRING_DESC(STRING_MSC_CONFIG, string_msc_config); 110 USB_MAKE_STRING_DESC(STRING_MSC_VENDOR, string_msc_vendor); 111 USB_MAKE_STRING_DESC(STRING_MSC_PRODUCT, string_msc_product); 112 USB_MAKE_STRING_DESC(STRING_MSC_SERIAL, string_msc_serial); 113 114 /* prototypes */ 115 116 static usb_temp_get_string_desc_t msc_get_string_desc; 117 118 static const struct usb_temp_packet_size bulk_mps = { 119 .mps[USB_SPEED_FULL] = 64, 120 .mps[USB_SPEED_HIGH] = 512, 121 }; 122 123 static const struct usb_temp_endpoint_desc bulk_in_ep = { 124 .pPacketSize = &bulk_mps, 125 #ifdef USB_HIP_IN_EP_0 126 .bEndpointAddress = USB_HIP_IN_EP_0, 127 #else 128 .bEndpointAddress = UE_DIR_IN, 129 #endif 130 .bmAttributes = UE_BULK, 131 }; 132 133 static const struct usb_temp_endpoint_desc bulk_out_ep = { 134 .pPacketSize = &bulk_mps, 135 #ifdef USB_HIP_OUT_EP_0 136 .bEndpointAddress = USB_HIP_OUT_EP_0, 137 #else 138 .bEndpointAddress = UE_DIR_OUT, 139 #endif 140 .bmAttributes = UE_BULK, 141 }; 142 143 static const struct usb_temp_endpoint_desc *msc_data_endpoints[] = { 144 &bulk_in_ep, 145 &bulk_out_ep, 146 NULL, 147 }; 148 149 static const struct usb_temp_interface_desc msc_data_interface = { 150 .ppEndpoints = msc_data_endpoints, 151 .bInterfaceClass = UICLASS_MASS, 152 .bInterfaceSubClass = UISUBCLASS_SCSI, 153 .bInterfaceProtocol = UIPROTO_MASS_BBB, 154 .iInterface = STRING_MSC_DATA_INDEX, 155 }; 156 157 static const struct usb_temp_interface_desc *msc_interfaces[] = { 158 &msc_data_interface, 159 NULL, 160 }; 161 162 static const struct usb_temp_config_desc msc_config_desc = { 163 .ppIfaceDesc = msc_interfaces, 164 .bmAttributes = UC_BUS_POWERED, 165 .bMaxPower = 25, /* 50 mA */ 166 .iConfiguration = STRING_MSC_CONFIG_INDEX, 167 }; 168 169 static const struct usb_temp_config_desc *msc_configs[] = { 170 &msc_config_desc, 171 NULL, 172 }; 173 174 const struct usb_temp_device_desc usb_template_msc = { 175 .getStringDesc = &msc_get_string_desc, 176 .ppConfigDesc = msc_configs, 177 .idVendor = 0x0001, 178 .idProduct = 0x0001, 179 .bcdDevice = 0x0100, 180 .bDeviceClass = UDCLASS_COMM, 181 .bDeviceSubClass = 0, 182 .bDeviceProtocol = 0, 183 .iManufacturer = STRING_MSC_VENDOR_INDEX, 184 .iProduct = STRING_MSC_PRODUCT_INDEX, 185 .iSerialNumber = STRING_MSC_SERIAL_INDEX, 186 }; 187 188 /*------------------------------------------------------------------------* 189 * msc_get_string_desc 190 * 191 * Return values: 192 * NULL: Failure. No such string. 193 * Else: Success. Pointer to string descriptor is returned. 194 *------------------------------------------------------------------------*/ 195 static const void * 196 msc_get_string_desc(uint16_t lang_id, uint8_t string_index) 197 { 198 static const void *ptr[STRING_MSC_MAX] = { 199 [STRING_LANG_INDEX] = &string_lang, 200 [STRING_MSC_DATA_INDEX] = &string_msc_data, 201 [STRING_MSC_CONFIG_INDEX] = &string_msc_config, 202 [STRING_MSC_VENDOR_INDEX] = &string_msc_vendor, 203 [STRING_MSC_PRODUCT_INDEX] = &string_msc_product, 204 [STRING_MSC_SERIAL_INDEX] = &string_msc_serial, 205 }; 206 207 if (string_index == 0) { 208 return (&string_lang); 209 } 210 if (lang_id != 0x0409) { 211 return (NULL); 212 } 213 if (string_index < STRING_MSC_MAX) { 214 return (ptr[string_index]); 215 } 216 return (NULL); 217 } 218