1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * This file contains the USB templates for an USB Mass Storage Device. 30 */ 31 32 #ifdef USB_GLOBAL_INCLUDE_FILE 33 #include USB_GLOBAL_INCLUDE_FILE 34 #else 35 #include <sys/stdint.h> 36 #include <sys/stddef.h> 37 #include <sys/param.h> 38 #include <sys/queue.h> 39 #include <sys/types.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/bus.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 #include <dev/usb/usb_core.h> 57 58 #include <dev/usb/template/usb_template.h> 59 #endif /* USB_GLOBAL_INCLUDE_FILE */ 60 61 enum { 62 STRING_LANG_INDEX, 63 STRING_MSC_DATA_INDEX, 64 STRING_MSC_CONFIG_INDEX, 65 STRING_MSC_VENDOR_INDEX, 66 STRING_MSC_PRODUCT_INDEX, 67 STRING_MSC_SERIAL_INDEX, 68 STRING_MSC_MAX, 69 }; 70 71 #define STRING_MSC_DATA \ 72 "U\0S\0B\0 \0M\0a\0s\0s\0 \0S\0t\0o\0r\0a\0g\0e\0 " \ 73 "\0I\0n\0t\0e\0r\0f\0a\0c\0e" 74 75 #define STRING_MSC_CONFIG \ 76 "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g" 77 78 #define STRING_MSC_VENDOR \ 79 "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n" 80 81 #define STRING_MSC_PRODUCT \ 82 "U\0S\0B\0 \0M\0e\0m\0o\0r\0y\0 \0S\0t\0i\0c\0k" 83 84 #define STRING_MSC_SERIAL \ 85 "M\0a\0r\0c\0h\0 \0002\0000\0000\08" 86 87 /* make the real string descriptors */ 88 89 USB_MAKE_STRING_DESC(STRING_MSC_DATA, string_msc_data); 90 USB_MAKE_STRING_DESC(STRING_MSC_CONFIG, string_msc_config); 91 USB_MAKE_STRING_DESC(STRING_MSC_VENDOR, string_msc_vendor); 92 USB_MAKE_STRING_DESC(STRING_MSC_PRODUCT, string_msc_product); 93 USB_MAKE_STRING_DESC(STRING_MSC_SERIAL, string_msc_serial); 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 = STRING_MSC_DATA_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 = UC_BUS_POWERED, 146 .bMaxPower = 25, /* 50 mA */ 147 .iConfiguration = STRING_MSC_CONFIG_INDEX, 148 }; 149 150 static const struct usb_temp_config_desc *msc_configs[] = { 151 &msc_config_desc, 152 NULL, 153 }; 154 155 const struct usb_temp_device_desc usb_template_msc = { 156 .getStringDesc = &msc_get_string_desc, 157 .ppConfigDesc = msc_configs, 158 .idVendor = USB_TEMPLATE_VENDOR, 159 .idProduct = 0x0012, 160 .bcdDevice = 0x0100, 161 .bDeviceClass = UDCLASS_COMM, 162 .bDeviceSubClass = 0, 163 .bDeviceProtocol = 0, 164 .iManufacturer = STRING_MSC_VENDOR_INDEX, 165 .iProduct = STRING_MSC_PRODUCT_INDEX, 166 .iSerialNumber = STRING_MSC_SERIAL_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[STRING_MSC_MAX] = { 180 [STRING_LANG_INDEX] = &usb_string_lang_en, 181 [STRING_MSC_DATA_INDEX] = &string_msc_data, 182 [STRING_MSC_CONFIG_INDEX] = &string_msc_config, 183 [STRING_MSC_VENDOR_INDEX] = &string_msc_vendor, 184 [STRING_MSC_PRODUCT_INDEX] = &string_msc_product, 185 [STRING_MSC_SERIAL_INDEX] = &string_msc_serial, 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 < STRING_MSC_MAX) { 195 return (ptr[string_index]); 196 } 197 return (NULL); 198 } 199