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/module.h> 43 #include <sys/lock.h> 44 #include <sys/mutex.h> 45 #include <sys/condvar.h> 46 #include <sys/sysctl.h> 47 #include <sys/sx.h> 48 #include <sys/unistd.h> 49 #include <sys/callout.h> 50 #include <sys/malloc.h> 51 #include <sys/priv.h> 52 53 #include <dev/usb/usb.h> 54 #include <dev/usb/usbdi.h> 55 56 #include <dev/usb/template/usb_template.h> 57 58 enum { 59 STRING_LANG_INDEX, 60 STRING_MSC_DATA_INDEX, 61 STRING_MSC_CONFIG_INDEX, 62 STRING_MSC_VENDOR_INDEX, 63 STRING_MSC_PRODUCT_INDEX, 64 STRING_MSC_SERIAL_INDEX, 65 STRING_MSC_MAX, 66 }; 67 68 #define STRING_LANG \ 69 0x09, 0x04, /* American English */ 70 71 #define STRING_MSC_DATA \ 72 'U', 0, 'S', 0, 'B', 0, ' ', 0, \ 73 'M', 0, 'a', 0, 's', 0, 's', 0, \ 74 ' ', 0, 'S', 0, 't', 0, 'o', 0, \ 75 'r', 0, 'a', 0, 'g', 0, 'e', 0, \ 76 ' ', 0, 'I', 0, 'n', 0, 't', 0, \ 77 'e', 0, 'r', 0, 'f', 0, 'a', 0, \ 78 'c', 0, 'e', 0, 79 80 #define STRING_MSC_CONFIG \ 81 'D', 0, 'e', 0, 'f', 0, 'a', 0, \ 82 'u', 0, 'l', 0, 't', 0, ' ', 0, \ 83 'c', 0, 'o', 0, 'n', 0, 'f', 0, \ 84 'i', 0, 'g', 0, 85 86 #define STRING_MSC_VENDOR \ 87 'F', 0, 'r', 0, 'e', 0, 'e', 0, \ 88 'B', 0, 'S', 0, 'D', 0, ' ', 0, \ 89 'f', 0, 'o', 0, 'u', 0, 'n', 0, \ 90 'd', 0, 'a', 0, 't', 0, 'i', 0, \ 91 'o', 0, 'n', 0, 92 93 #define STRING_MSC_PRODUCT \ 94 'U', 0, 'S', 0, 'B', 0, ' ', 0, \ 95 'M', 0, 'e', 0, 'm', 0, 'o', 0, \ 96 'r', 0, 'y', 0, ' ', 0, 'S', 0, \ 97 't', 0, 'i', 0, 'c', 0, 'k', 0 98 99 #define STRING_MSC_SERIAL \ 100 'M', 0, 'a', 0, 'r', 0, 'c', 0, \ 101 'h', 0, ' ', 0, '2', 0, '0', 0, \ 102 '0', 0, '8', 0, 103 104 /* make the real string descriptors */ 105 106 USB_MAKE_STRING_DESC(STRING_LANG, string_lang); 107 USB_MAKE_STRING_DESC(STRING_MSC_DATA, string_msc_data); 108 USB_MAKE_STRING_DESC(STRING_MSC_CONFIG, string_msc_config); 109 USB_MAKE_STRING_DESC(STRING_MSC_VENDOR, string_msc_vendor); 110 USB_MAKE_STRING_DESC(STRING_MSC_PRODUCT, string_msc_product); 111 USB_MAKE_STRING_DESC(STRING_MSC_SERIAL, string_msc_serial); 112 113 /* prototypes */ 114 115 static usb_temp_get_string_desc_t msc_get_string_desc; 116 117 static const struct usb_temp_packet_size bulk_mps = { 118 .mps[USB_SPEED_FULL] = 64, 119 .mps[USB_SPEED_HIGH] = 512, 120 }; 121 122 static const struct usb_temp_endpoint_desc bulk_in_ep = { 123 .pPacketSize = &bulk_mps, 124 #ifdef USB_HIP_IN_EP_0 125 .bEndpointAddress = USB_HIP_IN_EP_0, 126 #else 127 .bEndpointAddress = UE_DIR_IN, 128 #endif 129 .bmAttributes = UE_BULK, 130 }; 131 132 static const struct usb_temp_endpoint_desc bulk_out_ep = { 133 .pPacketSize = &bulk_mps, 134 #ifdef USB_HIP_OUT_EP_0 135 .bEndpointAddress = USB_HIP_OUT_EP_0, 136 #else 137 .bEndpointAddress = UE_DIR_OUT, 138 #endif 139 .bmAttributes = UE_BULK, 140 }; 141 142 static const struct usb_temp_endpoint_desc *msc_data_endpoints[] = { 143 &bulk_in_ep, 144 &bulk_out_ep, 145 NULL, 146 }; 147 148 static const struct usb_temp_interface_desc msc_data_interface = { 149 .ppEndpoints = msc_data_endpoints, 150 .bInterfaceClass = UICLASS_MASS, 151 .bInterfaceSubClass = UISUBCLASS_SCSI, 152 .bInterfaceProtocol = UIPROTO_MASS_BBB, 153 .iInterface = STRING_MSC_DATA_INDEX, 154 }; 155 156 static const struct usb_temp_interface_desc *msc_interfaces[] = { 157 &msc_data_interface, 158 NULL, 159 }; 160 161 static const struct usb_temp_config_desc msc_config_desc = { 162 .ppIfaceDesc = msc_interfaces, 163 .bmAttributes = UC_BUS_POWERED, 164 .bMaxPower = 25, /* 50 mA */ 165 .iConfiguration = STRING_MSC_CONFIG_INDEX, 166 }; 167 168 static const struct usb_temp_config_desc *msc_configs[] = { 169 &msc_config_desc, 170 NULL, 171 }; 172 173 const struct usb_temp_device_desc usb_template_msc = { 174 .getStringDesc = &msc_get_string_desc, 175 .ppConfigDesc = msc_configs, 176 .idVendor = 0x0001, 177 .idProduct = 0x0001, 178 .bcdDevice = 0x0100, 179 .bDeviceClass = UDCLASS_COMM, 180 .bDeviceSubClass = 0, 181 .bDeviceProtocol = 0, 182 .iManufacturer = STRING_MSC_VENDOR_INDEX, 183 .iProduct = STRING_MSC_PRODUCT_INDEX, 184 .iSerialNumber = STRING_MSC_SERIAL_INDEX, 185 }; 186 187 /*------------------------------------------------------------------------* 188 * msc_get_string_desc 189 * 190 * Return values: 191 * NULL: Failure. No such string. 192 * Else: Success. Pointer to string descriptor is returned. 193 *------------------------------------------------------------------------*/ 194 static const void * 195 msc_get_string_desc(uint16_t lang_id, uint8_t string_index) 196 { 197 static const void *ptr[STRING_MSC_MAX] = { 198 [STRING_LANG_INDEX] = &string_lang, 199 [STRING_MSC_DATA_INDEX] = &string_msc_data, 200 [STRING_MSC_CONFIG_INDEX] = &string_msc_config, 201 [STRING_MSC_VENDOR_INDEX] = &string_msc_vendor, 202 [STRING_MSC_PRODUCT_INDEX] = &string_msc_product, 203 [STRING_MSC_SERIAL_INDEX] = &string_msc_serial, 204 }; 205 206 if (string_index == 0) { 207 return (&string_lang); 208 } 209 if (lang_id != 0x0409) { 210 return (NULL); 211 } 212 if (string_index < STRING_MSC_MAX) { 213 return (ptr[string_index]); 214 } 215 return (NULL); 216 } 217