Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 293c43f | 2022-09-21 16:21:37 +0200 | [diff] [blame] | 10 | #include <malloc.h> |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 11 | #include <os.h> |
| 12 | #include <scsi.h> |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 13 | #include <scsi_emul.h> |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 14 | #include <usb.h> |
| 15 | |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 16 | /* |
| 17 | * This driver emulates a flash stick using the UFI command specification and |
| 18 | * the BBB (bulk/bulk/bulk) protocol. It supports only a single logical unit |
| 19 | * number (LUN 0). |
| 20 | */ |
| 21 | |
| 22 | enum { |
| 23 | SANDBOX_FLASH_EP_OUT = 1, /* endpoints */ |
| 24 | SANDBOX_FLASH_EP_IN = 2, |
| 25 | SANDBOX_FLASH_BLOCK_LEN = 512, |
Simon Glass | 293c43f | 2022-09-21 16:21:37 +0200 | [diff] [blame] | 26 | SANDBOX_FLASH_BUF_SIZE = 512, |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 27 | }; |
| 28 | |
Simon Glass | 31d7816 | 2015-11-08 23:47:53 -0700 | [diff] [blame] | 29 | enum { |
| 30 | STRINGID_MANUFACTURER = 1, |
| 31 | STRINGID_PRODUCT, |
| 32 | STRINGID_SERIAL, |
| 33 | |
| 34 | STRINGID_COUNT, |
| 35 | }; |
| 36 | |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 37 | /** |
| 38 | * struct sandbox_flash_priv - private state for this driver |
| 39 | * |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 40 | * @eminfo: emulator state |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 41 | * @error: true if there is an error condition |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 42 | * @tag: Tag value from last command |
| 43 | * @fd: File descriptor of backing file |
| 44 | * @file_size: Size of file in bytes |
| 45 | * @status_buff: Data buffer for outgoing status |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 46 | */ |
| 47 | struct sandbox_flash_priv { |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 48 | struct scsi_emul_info eminfo; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 49 | bool error; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 50 | u32 tag; |
| 51 | int fd; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 52 | struct umass_bbb_csw status; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct sandbox_flash_plat { |
| 56 | const char *pathname; |
Simon Glass | 31d7816 | 2015-11-08 23:47:53 -0700 | [diff] [blame] | 57 | struct usb_string flash_strings[STRINGID_COUNT]; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 58 | }; |
| 59 | |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 60 | static struct usb_device_descriptor flash_device_desc = { |
| 61 | .bLength = sizeof(flash_device_desc), |
| 62 | .bDescriptorType = USB_DT_DEVICE, |
| 63 | |
| 64 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
| 65 | |
| 66 | .bDeviceClass = 0, |
| 67 | .bDeviceSubClass = 0, |
| 68 | .bDeviceProtocol = 0, |
| 69 | |
| 70 | .idVendor = __constant_cpu_to_le16(0x1234), |
| 71 | .idProduct = __constant_cpu_to_le16(0x5678), |
| 72 | .iManufacturer = STRINGID_MANUFACTURER, |
| 73 | .iProduct = STRINGID_PRODUCT, |
| 74 | .iSerialNumber = STRINGID_SERIAL, |
| 75 | .bNumConfigurations = 1, |
| 76 | }; |
| 77 | |
| 78 | static struct usb_config_descriptor flash_config0 = { |
| 79 | .bLength = sizeof(flash_config0), |
| 80 | .bDescriptorType = USB_DT_CONFIG, |
| 81 | |
| 82 | /* wTotalLength is set up by usb-emul-uclass */ |
| 83 | .bNumInterfaces = 1, |
| 84 | .bConfigurationValue = 0, |
| 85 | .iConfiguration = 0, |
| 86 | .bmAttributes = 1 << 7, |
| 87 | .bMaxPower = 50, |
| 88 | }; |
| 89 | |
| 90 | static struct usb_interface_descriptor flash_interface0 = { |
| 91 | .bLength = sizeof(flash_interface0), |
| 92 | .bDescriptorType = USB_DT_INTERFACE, |
| 93 | |
| 94 | .bInterfaceNumber = 0, |
| 95 | .bAlternateSetting = 0, |
| 96 | .bNumEndpoints = 2, |
| 97 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, |
| 98 | .bInterfaceSubClass = US_SC_UFI, |
| 99 | .bInterfaceProtocol = US_PR_BULK, |
| 100 | .iInterface = 0, |
| 101 | }; |
| 102 | |
| 103 | static struct usb_endpoint_descriptor flash_endpoint0_out = { |
| 104 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 105 | .bDescriptorType = USB_DT_ENDPOINT, |
| 106 | |
| 107 | .bEndpointAddress = SANDBOX_FLASH_EP_OUT, |
| 108 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 109 | .wMaxPacketSize = __constant_cpu_to_le16(1024), |
| 110 | .bInterval = 0, |
| 111 | }; |
| 112 | |
| 113 | static struct usb_endpoint_descriptor flash_endpoint1_in = { |
| 114 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 115 | .bDescriptorType = USB_DT_ENDPOINT, |
| 116 | |
| 117 | .bEndpointAddress = SANDBOX_FLASH_EP_IN | USB_ENDPOINT_DIR_MASK, |
| 118 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 119 | .wMaxPacketSize = __constant_cpu_to_le16(1024), |
| 120 | .bInterval = 0, |
| 121 | }; |
| 122 | |
| 123 | static void *flash_desc_list[] = { |
| 124 | &flash_device_desc, |
| 125 | &flash_config0, |
| 126 | &flash_interface0, |
| 127 | &flash_endpoint0_out, |
| 128 | &flash_endpoint1_in, |
| 129 | NULL, |
| 130 | }; |
| 131 | |
| 132 | static int sandbox_flash_control(struct udevice *dev, struct usb_device *udev, |
| 133 | unsigned long pipe, void *buff, int len, |
| 134 | struct devrequest *setup) |
| 135 | { |
| 136 | struct sandbox_flash_priv *priv = dev_get_priv(dev); |
| 137 | |
| 138 | if (pipe == usb_rcvctrlpipe(udev, 0)) { |
| 139 | switch (setup->request) { |
| 140 | case US_BBB_RESET: |
| 141 | priv->error = false; |
| 142 | return 0; |
| 143 | case US_BBB_GET_MAX_LUN: |
| 144 | *(char *)buff = '\0'; |
| 145 | return 1; |
| 146 | default: |
| 147 | debug("request=%x\n", setup->request); |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | debug("pipe=%lx\n", pipe); |
| 152 | |
| 153 | return -EIO; |
| 154 | } |
| 155 | |
| 156 | static void setup_fail_response(struct sandbox_flash_priv *priv) |
| 157 | { |
| 158 | struct umass_bbb_csw *csw = &priv->status; |
| 159 | |
| 160 | csw->dCSWSignature = CSWSIGNATURE; |
| 161 | csw->dCSWTag = priv->tag; |
| 162 | csw->dCSWDataResidue = 0; |
| 163 | csw->bCSWStatus = CSWSTATUS_FAILED; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * setup_response() - set up a response to send back to the host |
| 168 | * |
| 169 | * @priv: Sandbox flash private data |
| 170 | * @resp: Response to send, or NULL if none |
| 171 | * @size: Size of response |
| 172 | */ |
Simon Glass | 8cfdea7 | 2022-09-21 16:21:41 +0200 | [diff] [blame] | 173 | static void setup_response(struct sandbox_flash_priv *priv) |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 174 | { |
| 175 | struct umass_bbb_csw *csw = &priv->status; |
| 176 | |
| 177 | csw->dCSWSignature = CSWSIGNATURE; |
| 178 | csw->dCSWTag = priv->tag; |
| 179 | csw->dCSWDataResidue = 0; |
| 180 | csw->bCSWStatus = CSWSTATUS_GOOD; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 181 | } |
| 182 | |
Simon Glass | 657efaa | 2022-09-21 16:21:42 +0200 | [diff] [blame] | 183 | /** |
| 184 | * handle_read() - prepare for reading data from the backing file |
| 185 | * |
| 186 | * This seeks to the correct file position and sets info->buff_used to the |
| 187 | * correct size. |
| 188 | * |
| 189 | * @priv: Private information |
| 190 | * @lba: Start block to read from |
| 191 | * @transfer_length: Number of blocks to read |
| 192 | * @return 0 if OK, -EIO on failure |
| 193 | */ |
| 194 | static int handle_read(struct sandbox_flash_priv *priv, ulong lba, |
| 195 | ulong transfer_len) |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 196 | { |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 197 | struct scsi_emul_info *info = &priv->eminfo; |
| 198 | |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 199 | debug("%s: lba=%lx, transfer_len=%lx\n", __func__, lba, transfer_len); |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 200 | info->read_len = transfer_len; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 201 | if (priv->fd != -1) { |
Simon Glass | d0962c9 | 2022-09-21 16:21:39 +0200 | [diff] [blame] | 202 | os_lseek(priv->fd, lba * info->block_size, OS_SEEK_SET); |
Simon Glass | 8cfdea7 | 2022-09-21 16:21:41 +0200 | [diff] [blame] | 203 | info->buff_used = transfer_len * info->block_size; |
Simon Glass | 657efaa | 2022-09-21 16:21:42 +0200 | [diff] [blame] | 204 | return 0; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 205 | } |
Simon Glass | 657efaa | 2022-09-21 16:21:42 +0200 | [diff] [blame] | 206 | |
| 207 | return -EIO; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 208 | } |
| 209 | |
Simon Glass | 31d7816 | 2015-11-08 23:47:53 -0700 | [diff] [blame] | 210 | static int handle_ufi_command(struct sandbox_flash_plat *plat, |
| 211 | struct sandbox_flash_priv *priv, const void *buff, |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 212 | int len) |
| 213 | { |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 214 | struct scsi_emul_info *info = &priv->eminfo; |
Simon Glass | a4eff9f | 2017-06-14 21:28:29 -0600 | [diff] [blame] | 215 | const struct scsi_cmd *req = buff; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 216 | |
Simon Glass | 8cfdea7 | 2022-09-21 16:21:41 +0200 | [diff] [blame] | 217 | info->buff_used = 0; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 218 | switch (*req->cmd) { |
| 219 | case SCSI_INQUIRY: { |
Simon Glass | 293c43f | 2022-09-21 16:21:37 +0200 | [diff] [blame] | 220 | struct scsi_inquiry_resp *resp = (void *)info->buff; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 221 | |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 222 | info->alloc_len = req->cmd[4]; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 223 | memset(resp, '\0', sizeof(*resp)); |
| 224 | resp->data_format = 1; |
| 225 | resp->additional_len = 0x1f; |
Simon Glass | 37884f0 | 2022-09-21 16:21:38 +0200 | [diff] [blame] | 226 | strncpy(resp->vendor, info->vendor, sizeof(resp->vendor)); |
| 227 | strncpy(resp->product, info->product, sizeof(resp->product)); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 228 | strncpy(resp->revision, "1.0", sizeof(resp->revision)); |
Simon Glass | 8cfdea7 | 2022-09-21 16:21:41 +0200 | [diff] [blame] | 229 | info->buff_used = sizeof(*resp); |
| 230 | setup_response(priv); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | case SCSI_TST_U_RDY: |
Simon Glass | 8cfdea7 | 2022-09-21 16:21:41 +0200 | [diff] [blame] | 234 | setup_response(priv); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 235 | break; |
| 236 | case SCSI_RD_CAPAC: { |
Simon Glass | 293c43f | 2022-09-21 16:21:37 +0200 | [diff] [blame] | 237 | struct scsi_read_capacity_resp *resp = (void *)info->buff; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 238 | uint blocks; |
| 239 | |
Simon Glass | 4313c3e | 2022-09-21 16:21:40 +0200 | [diff] [blame] | 240 | if (info->file_size) |
| 241 | blocks = info->file_size / info->block_size - 1; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 242 | else |
| 243 | blocks = 0; |
| 244 | resp->last_block_addr = cpu_to_be32(blocks); |
Simon Glass | d0962c9 | 2022-09-21 16:21:39 +0200 | [diff] [blame] | 245 | resp->block_len = cpu_to_be32(info->block_size); |
Simon Glass | 8cfdea7 | 2022-09-21 16:21:41 +0200 | [diff] [blame] | 246 | info->buff_used = sizeof(*resp); |
| 247 | setup_response(priv); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 248 | break; |
| 249 | } |
| 250 | case SCSI_READ10: { |
| 251 | struct scsi_read10_req *req = (void *)buff; |
| 252 | |
Simon Glass | 657efaa | 2022-09-21 16:21:42 +0200 | [diff] [blame] | 253 | if (!handle_read(priv, be32_to_cpu(req->lba), |
| 254 | be16_to_cpu(req->xfer_len))) |
| 255 | setup_response(priv); |
| 256 | else |
| 257 | setup_fail_response(priv); |
| 258 | |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 259 | break; |
| 260 | } |
| 261 | default: |
| 262 | debug("Command not supported: %x\n", req->cmd[0]); |
| 263 | return -EPROTONOSUPPORT; |
| 264 | } |
| 265 | |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 266 | info->phase = info->transfer_len ? SCSIPH_DATA : SCSIPH_STATUS; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev, |
| 271 | unsigned long pipe, void *buff, int len) |
| 272 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 273 | struct sandbox_flash_plat *plat = dev_get_plat(dev); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 274 | struct sandbox_flash_priv *priv = dev_get_priv(dev); |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 275 | struct scsi_emul_info *info = &priv->eminfo; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 276 | int ep = usb_pipeendpoint(pipe); |
| 277 | struct umass_bbb_cbw *cbw = buff; |
| 278 | |
| 279 | debug("%s: dev=%s, pipe=%lx, ep=%x, len=%x, phase=%d\n", __func__, |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 280 | dev->name, pipe, ep, len, info->phase); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 281 | switch (ep) { |
| 282 | case SANDBOX_FLASH_EP_OUT: |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 283 | switch (info->phase) { |
Simon Glass | e6e05ad | 2022-09-21 16:21:35 +0200 | [diff] [blame] | 284 | case SCSIPH_START: |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 285 | info->alloc_len = 0; |
| 286 | info->read_len = 0; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 287 | if (priv->error || len != UMASS_BBB_CBW_SIZE || |
| 288 | cbw->dCBWSignature != CBWSIGNATURE) |
| 289 | goto err; |
| 290 | if ((cbw->bCBWFlags & CBWFLAGS_SBZ) || |
| 291 | cbw->bCBWLUN != 0) |
| 292 | goto err; |
| 293 | if (cbw->bCDBLength < 1 || cbw->bCDBLength >= 0x10) |
| 294 | goto err; |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 295 | info->transfer_len = cbw->dCBWDataTransferLength; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 296 | priv->tag = cbw->dCBWTag; |
Simon Glass | 31d7816 | 2015-11-08 23:47:53 -0700 | [diff] [blame] | 297 | return handle_ufi_command(plat, priv, cbw->CBWCDB, |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 298 | cbw->bCDBLength); |
Simon Glass | e6e05ad | 2022-09-21 16:21:35 +0200 | [diff] [blame] | 299 | case SCSIPH_DATA: |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 300 | debug("data out\n"); |
| 301 | break; |
| 302 | default: |
| 303 | break; |
| 304 | } |
| 305 | case SANDBOX_FLASH_EP_IN: |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 306 | switch (info->phase) { |
Simon Glass | e6e05ad | 2022-09-21 16:21:35 +0200 | [diff] [blame] | 307 | case SCSIPH_DATA: |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 308 | debug("data in, len=%x, alloc_len=%x, info->read_len=%x\n", |
| 309 | len, info->alloc_len, info->read_len); |
| 310 | if (info->read_len) { |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 311 | ulong bytes_read; |
| 312 | |
Sean Anderson | 0383185 | 2022-03-23 18:24:38 -0400 | [diff] [blame] | 313 | if (priv->fd == -1) |
| 314 | return -EIO; |
| 315 | |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 316 | bytes_read = os_read(priv->fd, buff, len); |
| 317 | if (bytes_read != len) |
| 318 | return -EIO; |
Simon Glass | d0962c9 | 2022-09-21 16:21:39 +0200 | [diff] [blame] | 319 | info->read_len -= len / info->block_size; |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 320 | if (!info->read_len) |
| 321 | info->phase = SCSIPH_STATUS; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 322 | } else { |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 323 | if (info->alloc_len && len > info->alloc_len) |
| 324 | len = info->alloc_len; |
Simon Glass | 293c43f | 2022-09-21 16:21:37 +0200 | [diff] [blame] | 325 | if (len > SANDBOX_FLASH_BUF_SIZE) |
| 326 | len = SANDBOX_FLASH_BUF_SIZE; |
| 327 | memcpy(buff, info->buff, len); |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 328 | info->phase = SCSIPH_STATUS; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 329 | } |
| 330 | return len; |
Simon Glass | e6e05ad | 2022-09-21 16:21:35 +0200 | [diff] [blame] | 331 | case SCSIPH_STATUS: |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 332 | debug("status in, len=%x\n", len); |
| 333 | if (len > sizeof(priv->status)) |
| 334 | len = sizeof(priv->status); |
| 335 | memcpy(buff, &priv->status, len); |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame] | 336 | info->phase = SCSIPH_START; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 337 | return len; |
| 338 | default: |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | err: |
| 343 | priv->error = true; |
| 344 | debug("%s: Detected transfer error\n", __func__); |
| 345 | return 0; |
| 346 | } |
| 347 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 348 | static int sandbox_flash_of_to_plat(struct udevice *dev) |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 349 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 350 | struct sandbox_flash_plat *plat = dev_get_plat(dev); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 351 | |
Simon Glass | 82bf00e | 2017-05-18 20:09:39 -0600 | [diff] [blame] | 352 | plat->pathname = dev_read_string(dev, "sandbox,filepath"); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static int sandbox_flash_bind(struct udevice *dev) |
| 358 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 359 | struct sandbox_flash_plat *plat = dev_get_plat(dev); |
Simon Glass | 31d7816 | 2015-11-08 23:47:53 -0700 | [diff] [blame] | 360 | struct usb_string *fs; |
| 361 | |
| 362 | fs = plat->flash_strings; |
| 363 | fs[0].id = STRINGID_MANUFACTURER; |
| 364 | fs[0].s = "sandbox"; |
| 365 | fs[1].id = STRINGID_PRODUCT; |
| 366 | fs[1].s = "flash"; |
| 367 | fs[2].id = STRINGID_SERIAL; |
| 368 | fs[2].s = dev->name; |
| 369 | |
Bin Meng | d502cf1 | 2017-10-01 06:19:36 -0700 | [diff] [blame] | 370 | return usb_emul_setup_device(dev, plat->flash_strings, flash_desc_list); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static int sandbox_flash_probe(struct udevice *dev) |
| 374 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 375 | struct sandbox_flash_plat *plat = dev_get_plat(dev); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 376 | struct sandbox_flash_priv *priv = dev_get_priv(dev); |
Simon Glass | 293c43f | 2022-09-21 16:21:37 +0200 | [diff] [blame] | 377 | struct scsi_emul_info *info = &priv->eminfo; |
| 378 | int ret; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 379 | |
| 380 | priv->fd = os_open(plat->pathname, OS_O_RDONLY); |
Simon Glass | 293c43f | 2022-09-21 16:21:37 +0200 | [diff] [blame] | 381 | if (priv->fd != -1) { |
Simon Glass | 4313c3e | 2022-09-21 16:21:40 +0200 | [diff] [blame] | 382 | ret = os_get_filesize(plat->pathname, &info->file_size); |
Simon Glass | 293c43f | 2022-09-21 16:21:37 +0200 | [diff] [blame] | 383 | if (ret) |
| 384 | return log_msg_ret("sz", ret); |
| 385 | } |
| 386 | info->buff = malloc(SANDBOX_FLASH_BUF_SIZE); |
| 387 | if (!info->buff) |
| 388 | return log_ret(-ENOMEM); |
Simon Glass | 37884f0 | 2022-09-21 16:21:38 +0200 | [diff] [blame] | 389 | info->vendor = plat->flash_strings[STRINGID_MANUFACTURER - 1].s; |
| 390 | info->product = plat->flash_strings[STRINGID_PRODUCT - 1].s; |
Simon Glass | d0962c9 | 2022-09-21 16:21:39 +0200 | [diff] [blame] | 391 | info->block_size = SANDBOX_FLASH_BLOCK_LEN; |
Simon Glass | 293c43f | 2022-09-21 16:21:37 +0200 | [diff] [blame] | 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int sandbox_flash_remove(struct udevice *dev) |
| 397 | { |
| 398 | struct sandbox_flash_priv *priv = dev_get_priv(dev); |
| 399 | struct scsi_emul_info *info = &priv->eminfo; |
| 400 | |
| 401 | free(info->buff); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static const struct dm_usb_ops sandbox_usb_flash_ops = { |
| 407 | .control = sandbox_flash_control, |
| 408 | .bulk = sandbox_flash_bulk, |
| 409 | }; |
| 410 | |
| 411 | static const struct udevice_id sandbox_usb_flash_ids[] = { |
| 412 | { .compatible = "sandbox,usb-flash" }, |
| 413 | { } |
| 414 | }; |
| 415 | |
| 416 | U_BOOT_DRIVER(usb_sandbox_flash) = { |
| 417 | .name = "usb_sandbox_flash", |
| 418 | .id = UCLASS_USB_EMUL, |
| 419 | .of_match = sandbox_usb_flash_ids, |
| 420 | .bind = sandbox_flash_bind, |
| 421 | .probe = sandbox_flash_probe, |
Simon Glass | 293c43f | 2022-09-21 16:21:37 +0200 | [diff] [blame] | 422 | .remove = sandbox_flash_remove, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 423 | .of_to_plat = sandbox_flash_of_to_plat, |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 424 | .ops = &sandbox_usb_flash_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 425 | .priv_auto = sizeof(struct sandbox_flash_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 426 | .plat_auto = sizeof(struct sandbox_flash_plat), |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 427 | }; |