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