Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 2 | /* |
| 3 | * storage_common.c -- Common definitions for mass storage functionality |
| 4 | * |
| 5 | * Copyright (C) 2003-2008 Alan Stern |
| 6 | * Copyeight (C) 2009 Samsung Electronics |
| 7 | * Author: Michal Nazarewicz (m.nazarewicz@samsung.com) |
| 8 | * |
| 9 | * Ported to u-boot: |
| 10 | * Andrzej Pietrasiewicz <andrzej.p@samsung.com> |
| 11 | * |
| 12 | * Code refactoring & cleanup: |
| 13 | * Ćukasz Majewski <l.majewski@samsung.com> |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 14 | */ |
| 15 | |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 16 | /* |
| 17 | * This file requires the following identifiers used in USB strings to |
| 18 | * be defined (each of type pointer to char): |
| 19 | * - fsg_string_manufacturer -- name of the manufacturer |
| 20 | * - fsg_string_product -- name of the product |
| 21 | * - fsg_string_serial -- product's serial |
| 22 | * - fsg_string_config -- name of the configuration |
| 23 | * - fsg_string_interface -- name of the interface |
| 24 | * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS |
| 25 | * macro is defined prior to including this file. |
| 26 | */ |
| 27 | |
| 28 | /* |
| 29 | * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and |
| 30 | * fsg_hs_intr_in_desc objects as well as |
| 31 | * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES |
| 32 | * macros are not defined. |
| 33 | * |
| 34 | * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER, |
| 35 | * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not |
| 36 | * defined (as well as corresponding entries in string tables are |
| 37 | * missing) and FSG_STRING_INTERFACE has value of zero. |
| 38 | * |
| 39 | * When FSG_NO_OTG is defined fsg_otg_desc won't be defined. |
| 40 | */ |
| 41 | |
| 42 | /* |
| 43 | * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included |
| 44 | * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN |
| 45 | * characters rather then a pointer to void. |
| 46 | */ |
| 47 | |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 48 | /* #include <asm/unaligned.h> */ |
| 49 | |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 50 | /* |
| 51 | * Thanks to NetChip Technologies for donating this product ID. |
| 52 | * |
| 53 | * DO NOT REUSE THESE IDs with any other driver!! Ever!! |
| 54 | * Instead: allocate your own, using normal USB-IF procedures. |
| 55 | */ |
| 56 | #define FSG_VENDOR_ID 0x0525 /* NetChip */ |
| 57 | #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */ |
| 58 | |
| 59 | /*-------------------------------------------------------------------------*/ |
| 60 | |
| 61 | #ifndef DEBUG |
| 62 | #undef VERBOSE_DEBUG |
| 63 | #undef DUMP_MSGS |
| 64 | #endif /* !DEBUG */ |
| 65 | |
| 66 | #ifdef VERBOSE_DEBUG |
| 67 | #define VLDBG LDBG |
| 68 | #else |
| 69 | #define VLDBG(lun, fmt, args...) do { } while (0) |
| 70 | #endif /* VERBOSE_DEBUG */ |
| 71 | |
| 72 | /* |
| 73 | #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args) |
| 74 | #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args) |
| 75 | #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args) |
| 76 | #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args) |
| 77 | */ |
| 78 | |
| 79 | #define LDBG(lun, fmt, args...) do { } while (0) |
| 80 | #define LERROR(lun, fmt, args...) do { } while (0) |
| 81 | #define LWARN(lun, fmt, args...) do { } while (0) |
| 82 | #define LINFO(lun, fmt, args...) do { } while (0) |
| 83 | |
| 84 | /* |
| 85 | * Keep those macros in sync with those in |
| 86 | * include/linux/usb/composite.h or else GCC will complain. If they |
| 87 | * are identical (the same names of arguments, white spaces in the |
| 88 | * same places) GCC will allow redefinition otherwise (even if some |
| 89 | * white space is removed or added) warning will be issued. |
| 90 | * |
| 91 | * Those macros are needed here because File Storage Gadget does not |
| 92 | * include the composite.h header. For composite gadgets those macros |
| 93 | * are redundant since composite.h is included any way. |
| 94 | * |
| 95 | * One could check whether those macros are already defined (which |
| 96 | * would indicate composite.h had been included) or not (which would |
| 97 | * indicate we were in FSG) but this is not done because a warning is |
| 98 | * desired if definitions here differ from the ones in composite.h. |
| 99 | * |
| 100 | * We want the definitions to match and be the same in File Storage |
| 101 | * Gadget as well as Mass Storage Function (and so composite gadgets |
| 102 | * using MSF). If someone changes them in composite.h it will produce |
| 103 | * a warning in this file when building MSF. |
| 104 | */ |
| 105 | |
| 106 | #define DBG(d, fmt, args...) debug(fmt , ## args) |
| 107 | #define VDBG(d, fmt, args...) debug(fmt , ## args) |
| 108 | /* #define ERROR(d, fmt, args...) printf(fmt , ## args) */ |
| 109 | /* #define WARNING(d, fmt, args...) printf(fmt , ## args) */ |
| 110 | /* #define INFO(d, fmt, args...) printf(fmt , ## args) */ |
| 111 | |
| 112 | /* #define DBG(d, fmt, args...) do { } while (0) */ |
| 113 | /* #define VDBG(d, fmt, args...) do { } while (0) */ |
| 114 | #define ERROR(d, fmt, args...) do { } while (0) |
| 115 | #define WARNING(d, fmt, args...) do { } while (0) |
| 116 | #define INFO(d, fmt, args...) do { } while (0) |
| 117 | |
| 118 | #ifdef DUMP_MSGS |
| 119 | |
| 120 | /* dump_msg(fsg, const char * label, const u8 * buf, unsigned length); */ |
| 121 | # define dump_msg(fsg, label, buf, length) do { \ |
| 122 | if (length < 512) { \ |
| 123 | DBG(fsg, "%s, length %u:\n", label, length); \ |
Alexey Brodkin | 2d2fa49 | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 124 | print_hex_dump("", DUMP_PREFIX_OFFSET, \ |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 125 | 16, 1, buf, length, 0); \ |
| 126 | } \ |
| 127 | } while (0) |
| 128 | |
| 129 | # define dump_cdb(fsg) do { } while (0) |
| 130 | |
| 131 | #else |
| 132 | |
| 133 | # define dump_msg(fsg, /* const char * */ label, \ |
| 134 | /* const u8 * */ buf, /* unsigned */ length) do { } while (0) |
| 135 | |
| 136 | # ifdef VERBOSE_DEBUG |
| 137 | |
| 138 | # define dump_cdb(fsg) \ |
Alexey Brodkin | 2d2fa49 | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 139 | print_hex_dump("SCSI CDB: ", DUMP_PREFIX_NONE, \ |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 140 | 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \ |
| 141 | |
| 142 | # else |
| 143 | |
| 144 | # define dump_cdb(fsg) do { } while (0) |
| 145 | |
| 146 | # endif /* VERBOSE_DEBUG */ |
| 147 | |
| 148 | #endif /* DUMP_MSGS */ |
| 149 | |
| 150 | /*-------------------------------------------------------------------------*/ |
| 151 | |
| 152 | /* SCSI device types */ |
| 153 | #define TYPE_DISK 0x00 |
| 154 | #define TYPE_CDROM 0x05 |
| 155 | |
| 156 | /* USB protocol value = the transport method */ |
| 157 | #define USB_PR_CBI 0x00 /* Control/Bulk/Interrupt */ |
| 158 | #define USB_PR_CB 0x01 /* Control/Bulk w/o interrupt */ |
| 159 | #define USB_PR_BULK 0x50 /* Bulk-only */ |
| 160 | |
| 161 | /* USB subclass value = the protocol encapsulation */ |
| 162 | #define USB_SC_RBC 0x01 /* Reduced Block Commands (flash) */ |
| 163 | #define USB_SC_8020 0x02 /* SFF-8020i, MMC-2, ATAPI (CD-ROM) */ |
| 164 | #define USB_SC_QIC 0x03 /* QIC-157 (tape) */ |
| 165 | #define USB_SC_UFI 0x04 /* UFI (floppy) */ |
| 166 | #define USB_SC_8070 0x05 /* SFF-8070i (removable) */ |
| 167 | #define USB_SC_SCSI 0x06 /* Transparent SCSI */ |
| 168 | |
| 169 | /* Bulk-only data structures */ |
| 170 | |
| 171 | /* Command Block Wrapper */ |
| 172 | struct fsg_bulk_cb_wrap { |
| 173 | __le32 Signature; /* Contains 'USBC' */ |
| 174 | u32 Tag; /* Unique per command id */ |
| 175 | __le32 DataTransferLength; /* Size of the data */ |
| 176 | u8 Flags; /* Direction in bit 7 */ |
| 177 | u8 Lun; /* LUN (normally 0) */ |
| 178 | u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */ |
| 179 | u8 CDB[16]; /* Command Data Block */ |
| 180 | }; |
| 181 | |
| 182 | #define USB_BULK_CB_WRAP_LEN 31 |
| 183 | #define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */ |
| 184 | #define USB_BULK_IN_FLAG 0x80 |
| 185 | |
| 186 | /* Command Status Wrapper */ |
| 187 | struct bulk_cs_wrap { |
| 188 | __le32 Signature; /* Should = 'USBS' */ |
| 189 | u32 Tag; /* Same as original command */ |
| 190 | __le32 Residue; /* Amount not transferred */ |
| 191 | u8 Status; /* See below */ |
| 192 | }; |
| 193 | |
| 194 | #define USB_BULK_CS_WRAP_LEN 13 |
| 195 | #define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */ |
| 196 | #define USB_STATUS_PASS 0 |
| 197 | #define USB_STATUS_FAIL 1 |
| 198 | #define USB_STATUS_PHASE_ERROR 2 |
| 199 | |
| 200 | /* Bulk-only class specific requests */ |
| 201 | #define USB_BULK_RESET_REQUEST 0xff |
| 202 | #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe |
| 203 | |
| 204 | /* CBI Interrupt data structure */ |
| 205 | struct interrupt_data { |
| 206 | u8 bType; |
| 207 | u8 bValue; |
| 208 | }; |
| 209 | |
| 210 | #define CBI_INTERRUPT_DATA_LEN 2 |
| 211 | |
| 212 | /* CBI Accept Device-Specific Command request */ |
| 213 | #define USB_CBI_ADSC_REQUEST 0x00 |
| 214 | |
| 215 | /* Length of a SCSI Command Data Block */ |
| 216 | #define MAX_COMMAND_SIZE 16 |
| 217 | |
| 218 | /* SCSI commands that we recognize */ |
| 219 | #define SC_FORMAT_UNIT 0x04 |
| 220 | #define SC_INQUIRY 0x12 |
| 221 | #define SC_MODE_SELECT_6 0x15 |
| 222 | #define SC_MODE_SELECT_10 0x55 |
| 223 | #define SC_MODE_SENSE_6 0x1a |
| 224 | #define SC_MODE_SENSE_10 0x5a |
| 225 | #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e |
| 226 | #define SC_READ_6 0x08 |
| 227 | #define SC_READ_10 0x28 |
| 228 | #define SC_READ_12 0xa8 |
| 229 | #define SC_READ_CAPACITY 0x25 |
| 230 | #define SC_READ_FORMAT_CAPACITIES 0x23 |
| 231 | #define SC_READ_HEADER 0x44 |
| 232 | #define SC_READ_TOC 0x43 |
| 233 | #define SC_RELEASE 0x17 |
| 234 | #define SC_REQUEST_SENSE 0x03 |
| 235 | #define SC_RESERVE 0x16 |
| 236 | #define SC_SEND_DIAGNOSTIC 0x1d |
| 237 | #define SC_START_STOP_UNIT 0x1b |
| 238 | #define SC_SYNCHRONIZE_CACHE 0x35 |
| 239 | #define SC_TEST_UNIT_READY 0x00 |
| 240 | #define SC_VERIFY 0x2f |
| 241 | #define SC_WRITE_6 0x0a |
| 242 | #define SC_WRITE_10 0x2a |
| 243 | #define SC_WRITE_12 0xaa |
| 244 | |
| 245 | /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */ |
| 246 | #define SS_NO_SENSE 0 |
| 247 | #define SS_COMMUNICATION_FAILURE 0x040800 |
| 248 | #define SS_INVALID_COMMAND 0x052000 |
| 249 | #define SS_INVALID_FIELD_IN_CDB 0x052400 |
| 250 | #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100 |
| 251 | #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500 |
| 252 | #define SS_MEDIUM_NOT_PRESENT 0x023a00 |
| 253 | #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302 |
| 254 | #define SS_NOT_READY_TO_READY_TRANSITION 0x062800 |
| 255 | #define SS_RESET_OCCURRED 0x062900 |
| 256 | #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900 |
| 257 | #define SS_UNRECOVERED_READ_ERROR 0x031100 |
| 258 | #define SS_WRITE_ERROR 0x030c02 |
| 259 | #define SS_WRITE_PROTECTED 0x072700 |
| 260 | |
| 261 | #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */ |
| 262 | #define ASC(x) ((u8) ((x) >> 8)) |
| 263 | #define ASCQ(x) ((u8) (x)) |
| 264 | |
| 265 | struct device_attribute { int i; }; |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 266 | #define ETOOSMALL 525 |
| 267 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 268 | #include <log.h> |
Caleb Connolly | 00e9e0c | 2024-03-20 14:30:50 +0000 | [diff] [blame] | 269 | #include <linux/log2.h> |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 270 | #include <usb_mass_storage.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 271 | #include <dm/device_compat.h> |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 272 | |
| 273 | /*-------------------------------------------------------------------------*/ |
| 274 | |
| 275 | struct fsg_lun { |
| 276 | loff_t file_length; |
| 277 | loff_t num_sectors; |
| 278 | |
| 279 | unsigned int initially_ro:1; |
| 280 | unsigned int ro:1; |
| 281 | unsigned int removable:1; |
| 282 | unsigned int cdrom:1; |
| 283 | unsigned int prevent_medium_removal:1; |
| 284 | unsigned int registered:1; |
| 285 | unsigned int info_valid:1; |
| 286 | unsigned int nofua:1; |
| 287 | |
| 288 | u32 sense_data; |
| 289 | u32 sense_data_info; |
| 290 | u32 unit_attention_data; |
Caleb Connolly | 00e9e0c | 2024-03-20 14:30:50 +0000 | [diff] [blame] | 291 | unsigned int blkbits; |
| 292 | unsigned int blksize; /* logical block size of bound block device */ |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 293 | |
| 294 | struct device dev; |
| 295 | }; |
| 296 | |
| 297 | #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL) |
| 298 | #if 0 |
| 299 | static struct fsg_lun *fsg_lun_from_dev(struct device *dev) |
| 300 | { |
| 301 | return container_of(dev, struct fsg_lun, dev); |
| 302 | } |
| 303 | #endif |
| 304 | |
| 305 | /* Big enough to hold our biggest descriptor */ |
| 306 | #define EP0_BUFSIZE 256 |
| 307 | #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */ |
| 308 | |
| 309 | /* Number of buffers we will use. 2 is enough for double-buffering */ |
| 310 | #define FSG_NUM_BUFFERS 2 |
| 311 | |
| 312 | /* Default size of buffer length. */ |
Marek Vasut | 91215be | 2017-10-19 21:45:53 +0200 | [diff] [blame] | 313 | #define FSG_BUFLEN ((u32)131072) |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 314 | |
| 315 | /* Maximal number of LUNs supported in mass storage function */ |
| 316 | #define FSG_MAX_LUNS 8 |
| 317 | |
| 318 | enum fsg_buffer_state { |
| 319 | BUF_STATE_EMPTY = 0, |
| 320 | BUF_STATE_FULL, |
| 321 | BUF_STATE_BUSY |
| 322 | }; |
| 323 | |
| 324 | struct fsg_buffhd { |
| 325 | #ifdef FSG_BUFFHD_STATIC_BUFFER |
| 326 | char buf[FSG_BUFLEN]; |
| 327 | #else |
| 328 | void *buf; |
| 329 | #endif |
| 330 | enum fsg_buffer_state state; |
| 331 | struct fsg_buffhd *next; |
| 332 | |
| 333 | /* |
| 334 | * The NetChip 2280 is faster, and handles some protocol faults |
| 335 | * better, if we don't submit any short bulk-out read requests. |
| 336 | * So we will record the intended request length here. |
| 337 | */ |
| 338 | unsigned int bulk_out_intended_length; |
| 339 | |
| 340 | struct usb_request *inreq; |
| 341 | int inreq_busy; |
| 342 | struct usb_request *outreq; |
| 343 | int outreq_busy; |
| 344 | }; |
| 345 | |
| 346 | enum fsg_state { |
| 347 | /* This one isn't used anywhere */ |
| 348 | FSG_STATE_COMMAND_PHASE = -10, |
| 349 | FSG_STATE_DATA_PHASE, |
| 350 | FSG_STATE_STATUS_PHASE, |
| 351 | |
| 352 | FSG_STATE_IDLE = 0, |
| 353 | FSG_STATE_ABORT_BULK_OUT, |
| 354 | FSG_STATE_RESET, |
| 355 | FSG_STATE_INTERFACE_CHANGE, |
| 356 | FSG_STATE_CONFIG_CHANGE, |
| 357 | FSG_STATE_DISCONNECT, |
| 358 | FSG_STATE_EXIT, |
| 359 | FSG_STATE_TERMINATED |
| 360 | }; |
| 361 | |
| 362 | enum data_direction { |
| 363 | DATA_DIR_UNKNOWN = 0, |
| 364 | DATA_DIR_FROM_HOST, |
| 365 | DATA_DIR_TO_HOST, |
| 366 | DATA_DIR_NONE |
| 367 | }; |
| 368 | |
| 369 | /*-------------------------------------------------------------------------*/ |
| 370 | |
| 371 | static inline u32 get_unaligned_be24(u8 *buf) |
| 372 | { |
| 373 | return 0xffffff & (u32) get_unaligned_be32(buf - 1); |
| 374 | } |
| 375 | |
| 376 | /*-------------------------------------------------------------------------*/ |
| 377 | |
| 378 | enum { |
| 379 | #ifndef FSG_NO_DEVICE_STRINGS |
| 380 | FSG_STRING_MANUFACTURER = 1, |
| 381 | FSG_STRING_PRODUCT, |
| 382 | FSG_STRING_SERIAL, |
| 383 | FSG_STRING_CONFIG, |
| 384 | #endif |
| 385 | FSG_STRING_INTERFACE |
| 386 | }; |
| 387 | |
| 388 | #ifndef FSG_NO_OTG |
| 389 | static struct usb_otg_descriptor |
| 390 | fsg_otg_desc = { |
| 391 | .bLength = sizeof fsg_otg_desc, |
| 392 | .bDescriptorType = USB_DT_OTG, |
| 393 | |
| 394 | .bmAttributes = USB_OTG_SRP, |
| 395 | }; |
| 396 | #endif |
| 397 | |
| 398 | /* There is only one interface. */ |
| 399 | |
| 400 | static struct usb_interface_descriptor |
| 401 | fsg_intf_desc = { |
| 402 | .bLength = sizeof fsg_intf_desc, |
| 403 | .bDescriptorType = USB_DT_INTERFACE, |
| 404 | |
| 405 | .bNumEndpoints = 2, /* Adjusted during fsg_bind() */ |
| 406 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, |
| 407 | .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */ |
| 408 | .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */ |
| 409 | .iInterface = FSG_STRING_INTERFACE, |
| 410 | }; |
| 411 | |
| 412 | /* |
| 413 | * Three full-speed endpoint descriptors: bulk-in, bulk-out, and |
| 414 | * interrupt-in. |
| 415 | */ |
| 416 | |
| 417 | static struct usb_endpoint_descriptor |
| 418 | fsg_fs_bulk_in_desc = { |
| 419 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 420 | .bDescriptorType = USB_DT_ENDPOINT, |
| 421 | |
| 422 | .bEndpointAddress = USB_DIR_IN, |
| 423 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 424 | /* wMaxPacketSize set by autoconfiguration */ |
| 425 | }; |
| 426 | |
| 427 | static struct usb_endpoint_descriptor |
| 428 | fsg_fs_bulk_out_desc = { |
| 429 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 430 | .bDescriptorType = USB_DT_ENDPOINT, |
| 431 | |
| 432 | .bEndpointAddress = USB_DIR_OUT, |
| 433 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 434 | /* wMaxPacketSize set by autoconfiguration */ |
| 435 | }; |
| 436 | |
| 437 | #ifndef FSG_NO_INTR_EP |
| 438 | |
| 439 | static struct usb_endpoint_descriptor |
| 440 | fsg_fs_intr_in_desc = { |
| 441 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 442 | .bDescriptorType = USB_DT_ENDPOINT, |
| 443 | |
| 444 | .bEndpointAddress = USB_DIR_IN, |
| 445 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 446 | .wMaxPacketSize = cpu_to_le16(2), |
| 447 | .bInterval = 32, /* frames -> 32 ms */ |
| 448 | }; |
| 449 | |
| 450 | #ifndef FSG_NO_OTG |
| 451 | # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2 |
| 452 | #else |
| 453 | # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1 |
| 454 | #endif |
| 455 | |
| 456 | #endif |
| 457 | |
| 458 | static struct usb_descriptor_header *fsg_fs_function[] = { |
| 459 | #ifndef FSG_NO_OTG |
| 460 | (struct usb_descriptor_header *) &fsg_otg_desc, |
| 461 | #endif |
| 462 | (struct usb_descriptor_header *) &fsg_intf_desc, |
| 463 | (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc, |
| 464 | (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc, |
| 465 | #ifndef FSG_NO_INTR_EP |
| 466 | (struct usb_descriptor_header *) &fsg_fs_intr_in_desc, |
| 467 | #endif |
| 468 | NULL, |
| 469 | }; |
| 470 | |
| 471 | /* |
| 472 | * USB 2.0 devices need to expose both high speed and full speed |
| 473 | * descriptors, unless they only run at full speed. |
| 474 | * |
| 475 | * That means alternate endpoint descriptors (bigger packets) |
| 476 | * and a "device qualifier" ... plus more construction options |
| 477 | * for the configuration descriptor. |
| 478 | */ |
| 479 | static struct usb_endpoint_descriptor |
| 480 | fsg_hs_bulk_in_desc = { |
| 481 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 482 | .bDescriptorType = USB_DT_ENDPOINT, |
| 483 | |
| 484 | /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */ |
| 485 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 486 | .wMaxPacketSize = cpu_to_le16(512), |
| 487 | }; |
| 488 | |
| 489 | static struct usb_endpoint_descriptor |
| 490 | fsg_hs_bulk_out_desc = { |
| 491 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 492 | .bDescriptorType = USB_DT_ENDPOINT, |
| 493 | |
| 494 | /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */ |
| 495 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 496 | .wMaxPacketSize = cpu_to_le16(512), |
| 497 | .bInterval = 1, /* NAK every 1 uframe */ |
| 498 | }; |
| 499 | |
| 500 | #ifndef FSG_NO_INTR_EP |
| 501 | |
| 502 | static struct usb_endpoint_descriptor |
| 503 | fsg_hs_intr_in_desc = { |
| 504 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 505 | .bDescriptorType = USB_DT_ENDPOINT, |
| 506 | |
| 507 | /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */ |
| 508 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 509 | .wMaxPacketSize = cpu_to_le16(2), |
| 510 | .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */ |
| 511 | }; |
| 512 | |
| 513 | #ifndef FSG_NO_OTG |
| 514 | # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2 |
| 515 | #else |
| 516 | # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1 |
| 517 | #endif |
| 518 | |
| 519 | #endif |
| 520 | |
| 521 | static struct usb_descriptor_header *fsg_hs_function[] = { |
| 522 | #ifndef FSG_NO_OTG |
| 523 | (struct usb_descriptor_header *) &fsg_otg_desc, |
| 524 | #endif |
| 525 | (struct usb_descriptor_header *) &fsg_intf_desc, |
| 526 | (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc, |
| 527 | (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc, |
| 528 | #ifndef FSG_NO_INTR_EP |
| 529 | (struct usb_descriptor_header *) &fsg_hs_intr_in_desc, |
| 530 | #endif |
| 531 | NULL, |
| 532 | }; |
| 533 | |
| 534 | /* Maxpacket and other transfer characteristics vary by speed. */ |
| 535 | static struct usb_endpoint_descriptor * |
| 536 | fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, |
| 537 | struct usb_endpoint_descriptor *hs) |
| 538 | { |
| 539 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 540 | return hs; |
| 541 | return fs; |
| 542 | } |
| 543 | |
| 544 | /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */ |
| 545 | static struct usb_string fsg_strings[] = { |
| 546 | #ifndef FSG_NO_DEVICE_STRINGS |
| 547 | {FSG_STRING_MANUFACTURER, fsg_string_manufacturer}, |
| 548 | {FSG_STRING_PRODUCT, fsg_string_product}, |
| 549 | {FSG_STRING_SERIAL, fsg_string_serial}, |
| 550 | {FSG_STRING_CONFIG, fsg_string_config}, |
| 551 | #endif |
| 552 | {FSG_STRING_INTERFACE, fsg_string_interface}, |
| 553 | {} |
| 554 | }; |
| 555 | |
| 556 | static struct usb_gadget_strings fsg_stringtab = { |
| 557 | .language = 0x0409, /* en-us */ |
| 558 | .strings = fsg_strings, |
| 559 | }; |
| 560 | |
| 561 | /*-------------------------------------------------------------------------*/ |
| 562 | |
| 563 | /* |
| 564 | * If the next two routines are called while the gadget is registered, |
| 565 | * the caller must own fsg->filesem for writing. |
| 566 | */ |
| 567 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 568 | static int fsg_lun_open(struct fsg_lun *curlun, unsigned int num_sectors, |
Caleb Connolly | 00e9e0c | 2024-03-20 14:30:50 +0000 | [diff] [blame] | 569 | unsigned int sector_size, const char *filename) |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 570 | { |
| 571 | int ro; |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 572 | |
| 573 | /* R/W if we can, R/O if we must */ |
| 574 | ro = curlun->initially_ro; |
| 575 | |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 576 | curlun->ro = ro; |
Caleb Connolly | 00e9e0c | 2024-03-20 14:30:50 +0000 | [diff] [blame] | 577 | curlun->file_length = num_sectors * sector_size; |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 578 | curlun->num_sectors = num_sectors; |
Caleb Connolly | 00e9e0c | 2024-03-20 14:30:50 +0000 | [diff] [blame] | 579 | curlun->blksize = sector_size; |
| 580 | curlun->blkbits = order_base_2(sector_size >> 9) + 9; |
| 581 | debug("blksize: %u\n", sector_size); |
| 582 | debug("open backing file: '%s'\n", filename); |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 583 | |
Przemyslaw Marczak | 3dab730 | 2013-10-23 14:30:44 +0200 | [diff] [blame] | 584 | return 0; |
Lukasz Majewski | 8647cb5 | 2013-03-05 12:10:15 +0100 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static void fsg_lun_close(struct fsg_lun *curlun) |
| 588 | { |
| 589 | } |
| 590 | |
| 591 | /*-------------------------------------------------------------------------*/ |
| 592 | |
| 593 | /* |
| 594 | * Sync the file data, don't bother with the metadata. |
| 595 | * This code was copied from fs/buffer.c:sys_fdatasync(). |
| 596 | */ |
| 597 | static int fsg_lun_fsync_sub(struct fsg_lun *curlun) |
| 598 | { |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static void store_cdrom_address(u8 *dest, int msf, u32 addr) |
| 603 | { |
| 604 | if (msf) { |
| 605 | /* Convert to Minutes-Seconds-Frames */ |
| 606 | addr >>= 2; /* Convert to 2048-byte frames */ |
| 607 | addr += 2*75; /* Lead-in occupies 2 seconds */ |
| 608 | dest[3] = addr % 75; /* Frames */ |
| 609 | addr /= 75; |
| 610 | dest[2] = addr % 60; /* Seconds */ |
| 611 | addr /= 60; |
| 612 | dest[1] = addr; /* Minutes */ |
| 613 | dest[0] = 0; /* Reserved */ |
| 614 | } else { |
| 615 | /* Absolute sector */ |
| 616 | put_unaligned_be32(addr, dest); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /*-------------------------------------------------------------------------*/ |