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 | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 10 | #include <os.h> |
| 11 | #include <scsi.h> |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 12 | #include <scsi_emul.h> |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 13 | #include <usb.h> |
| 14 | |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 15 | /* |
| 16 | * This driver emulates a flash stick using the UFI command specification and |
| 17 | * the BBB (bulk/bulk/bulk) protocol. It supports only a single logical unit |
| 18 | * number (LUN 0). |
| 19 | */ |
| 20 | |
| 21 | enum { |
| 22 | SANDBOX_FLASH_EP_OUT = 1, /* endpoints */ |
| 23 | SANDBOX_FLASH_EP_IN = 2, |
| 24 | SANDBOX_FLASH_BLOCK_LEN = 512, |
| 25 | }; |
| 26 | |
Simon Glass | 31d7816 | 2015-11-08 23:47:53 -0700 | [diff] [blame] | 27 | enum { |
| 28 | STRINGID_MANUFACTURER = 1, |
| 29 | STRINGID_PRODUCT, |
| 30 | STRINGID_SERIAL, |
| 31 | |
| 32 | STRINGID_COUNT, |
| 33 | }; |
| 34 | |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 35 | /** |
| 36 | * struct sandbox_flash_priv - private state for this driver |
| 37 | * |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 38 | * @eminfo: emulator state |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 39 | * @error: true if there is an error condition |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 40 | * @tag: Tag value from last command |
| 41 | * @fd: File descriptor of backing file |
| 42 | * @file_size: Size of file in bytes |
| 43 | * @status_buff: Data buffer for outgoing status |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 44 | * @buff: Data buffer for outgoing data |
| 45 | */ |
| 46 | struct sandbox_flash_priv { |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 47 | struct scsi_emul_info eminfo; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 48 | bool error; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 49 | u32 tag; |
| 50 | int fd; |
| 51 | loff_t file_size; |
| 52 | struct umass_bbb_csw status; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 53 | u8 buff[512]; |
| 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 | |
| 213 | assert(!resp || resp == priv->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 | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 226 | setup_response(priv, priv->buff, |
| 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: { |
| 242 | struct scsi_inquiry_resp *resp = (void *)priv->buff; |
| 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; |
| 248 | strncpy(resp->vendor, |
Simon Glass | 31d7816 | 2015-11-08 23:47:53 -0700 | [diff] [blame] | 249 | plat->flash_strings[STRINGID_MANUFACTURER - 1].s, |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 250 | sizeof(resp->vendor)); |
Simon Glass | 31d7816 | 2015-11-08 23:47:53 -0700 | [diff] [blame] | 251 | strncpy(resp->product, |
| 252 | plat->flash_strings[STRINGID_PRODUCT - 1].s, |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 253 | sizeof(resp->product)); |
| 254 | strncpy(resp->revision, "1.0", sizeof(resp->revision)); |
| 255 | setup_response(priv, resp, sizeof(*resp)); |
| 256 | break; |
| 257 | } |
| 258 | case SCSI_TST_U_RDY: |
| 259 | setup_response(priv, NULL, 0); |
| 260 | break; |
| 261 | case SCSI_RD_CAPAC: { |
| 262 | struct scsi_read_capacity_resp *resp = (void *)priv->buff; |
| 263 | uint blocks; |
| 264 | |
| 265 | if (priv->file_size) |
| 266 | blocks = priv->file_size / SANDBOX_FLASH_BLOCK_LEN - 1; |
| 267 | else |
| 268 | blocks = 0; |
| 269 | resp->last_block_addr = cpu_to_be32(blocks); |
| 270 | resp->block_len = cpu_to_be32(SANDBOX_FLASH_BLOCK_LEN); |
| 271 | setup_response(priv, resp, sizeof(*resp)); |
| 272 | break; |
| 273 | } |
| 274 | case SCSI_READ10: { |
| 275 | struct scsi_read10_req *req = (void *)buff; |
| 276 | |
| 277 | handle_read(priv, be32_to_cpu(req->lba), |
Simon Glass | e241fc3 | 2022-09-21 16:21:34 +0200 | [diff] [blame] | 278 | be16_to_cpu(req->xfer_len)); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 279 | break; |
| 280 | } |
| 281 | default: |
| 282 | debug("Command not supported: %x\n", req->cmd[0]); |
| 283 | return -EPROTONOSUPPORT; |
| 284 | } |
| 285 | |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 286 | info->phase = info->transfer_len ? SCSIPH_DATA : SCSIPH_STATUS; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev, |
| 291 | unsigned long pipe, void *buff, int len) |
| 292 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 293 | struct sandbox_flash_plat *plat = dev_get_plat(dev); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 294 | struct sandbox_flash_priv *priv = dev_get_priv(dev); |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 295 | struct scsi_emul_info *info = &priv->eminfo; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 296 | int ep = usb_pipeendpoint(pipe); |
| 297 | struct umass_bbb_cbw *cbw = buff; |
| 298 | |
| 299 | 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^] | 300 | dev->name, pipe, ep, len, info->phase); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 301 | switch (ep) { |
| 302 | case SANDBOX_FLASH_EP_OUT: |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 303 | switch (info->phase) { |
Simon Glass | e6e05ad | 2022-09-21 16:21:35 +0200 | [diff] [blame] | 304 | case SCSIPH_START: |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 305 | info->alloc_len = 0; |
| 306 | info->read_len = 0; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 307 | if (priv->error || len != UMASS_BBB_CBW_SIZE || |
| 308 | cbw->dCBWSignature != CBWSIGNATURE) |
| 309 | goto err; |
| 310 | if ((cbw->bCBWFlags & CBWFLAGS_SBZ) || |
| 311 | cbw->bCBWLUN != 0) |
| 312 | goto err; |
| 313 | if (cbw->bCDBLength < 1 || cbw->bCDBLength >= 0x10) |
| 314 | goto err; |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 315 | info->transfer_len = cbw->dCBWDataTransferLength; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 316 | priv->tag = cbw->dCBWTag; |
Simon Glass | 31d7816 | 2015-11-08 23:47:53 -0700 | [diff] [blame] | 317 | return handle_ufi_command(plat, priv, cbw->CBWCDB, |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 318 | cbw->bCDBLength); |
Simon Glass | e6e05ad | 2022-09-21 16:21:35 +0200 | [diff] [blame] | 319 | case SCSIPH_DATA: |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 320 | debug("data out\n"); |
| 321 | break; |
| 322 | default: |
| 323 | break; |
| 324 | } |
| 325 | case SANDBOX_FLASH_EP_IN: |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 326 | switch (info->phase) { |
Simon Glass | e6e05ad | 2022-09-21 16:21:35 +0200 | [diff] [blame] | 327 | case SCSIPH_DATA: |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 328 | debug("data in, len=%x, alloc_len=%x, info->read_len=%x\n", |
| 329 | len, info->alloc_len, info->read_len); |
| 330 | if (info->read_len) { |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 331 | ulong bytes_read; |
| 332 | |
Sean Anderson | 0383185 | 2022-03-23 18:24:38 -0400 | [diff] [blame] | 333 | if (priv->fd == -1) |
| 334 | return -EIO; |
| 335 | |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 336 | bytes_read = os_read(priv->fd, buff, len); |
| 337 | if (bytes_read != len) |
| 338 | return -EIO; |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 339 | info->read_len -= len / SANDBOX_FLASH_BLOCK_LEN; |
| 340 | if (!info->read_len) |
| 341 | info->phase = SCSIPH_STATUS; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 342 | } else { |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 343 | if (info->alloc_len && len > info->alloc_len) |
| 344 | len = info->alloc_len; |
Andrew Scull | 50456c9 | 2022-04-03 10:39:12 +0000 | [diff] [blame] | 345 | if (len > sizeof(priv->buff)) |
| 346 | len = sizeof(priv->buff); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 347 | memcpy(buff, priv->buff, len); |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 348 | info->phase = SCSIPH_STATUS; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 349 | } |
| 350 | return len; |
Simon Glass | e6e05ad | 2022-09-21 16:21:35 +0200 | [diff] [blame] | 351 | case SCSIPH_STATUS: |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 352 | debug("status in, len=%x\n", len); |
| 353 | if (len > sizeof(priv->status)) |
| 354 | len = sizeof(priv->status); |
| 355 | memcpy(buff, &priv->status, len); |
Simon Glass | 4c92a4d | 2022-09-21 16:21:36 +0200 | [diff] [blame^] | 356 | info->phase = SCSIPH_START; |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 357 | return len; |
| 358 | default: |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | err: |
| 363 | priv->error = true; |
| 364 | debug("%s: Detected transfer error\n", __func__); |
| 365 | return 0; |
| 366 | } |
| 367 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 368 | static int sandbox_flash_of_to_plat(struct udevice *dev) |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 369 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 370 | struct sandbox_flash_plat *plat = dev_get_plat(dev); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 371 | |
Simon Glass | 82bf00e | 2017-05-18 20:09:39 -0600 | [diff] [blame] | 372 | plat->pathname = dev_read_string(dev, "sandbox,filepath"); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static int sandbox_flash_bind(struct udevice *dev) |
| 378 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 379 | struct sandbox_flash_plat *plat = dev_get_plat(dev); |
Simon Glass | 31d7816 | 2015-11-08 23:47:53 -0700 | [diff] [blame] | 380 | struct usb_string *fs; |
| 381 | |
| 382 | fs = plat->flash_strings; |
| 383 | fs[0].id = STRINGID_MANUFACTURER; |
| 384 | fs[0].s = "sandbox"; |
| 385 | fs[1].id = STRINGID_PRODUCT; |
| 386 | fs[1].s = "flash"; |
| 387 | fs[2].id = STRINGID_SERIAL; |
| 388 | fs[2].s = dev->name; |
| 389 | |
Bin Meng | d502cf1 | 2017-10-01 06:19:36 -0700 | [diff] [blame] | 390 | return usb_emul_setup_device(dev, plat->flash_strings, flash_desc_list); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static int sandbox_flash_probe(struct udevice *dev) |
| 394 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 395 | struct sandbox_flash_plat *plat = dev_get_plat(dev); |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 396 | struct sandbox_flash_priv *priv = dev_get_priv(dev); |
| 397 | |
| 398 | priv->fd = os_open(plat->pathname, OS_O_RDONLY); |
| 399 | if (priv->fd != -1) |
| 400 | return os_get_filesize(plat->pathname, &priv->file_size); |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static const struct dm_usb_ops sandbox_usb_flash_ops = { |
| 406 | .control = sandbox_flash_control, |
| 407 | .bulk = sandbox_flash_bulk, |
| 408 | }; |
| 409 | |
| 410 | static const struct udevice_id sandbox_usb_flash_ids[] = { |
| 411 | { .compatible = "sandbox,usb-flash" }, |
| 412 | { } |
| 413 | }; |
| 414 | |
| 415 | U_BOOT_DRIVER(usb_sandbox_flash) = { |
| 416 | .name = "usb_sandbox_flash", |
| 417 | .id = UCLASS_USB_EMUL, |
| 418 | .of_match = sandbox_usb_flash_ids, |
| 419 | .bind = sandbox_flash_bind, |
| 420 | .probe = sandbox_flash_probe, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 421 | .of_to_plat = sandbox_flash_of_to_plat, |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 422 | .ops = &sandbox_usb_flash_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 423 | .priv_auto = sizeof(struct sandbox_flash_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 424 | .plat_auto = sizeof(struct sandbox_flash_plat), |
Simon Glass | b144644 | 2015-03-25 12:22:39 -0600 | [diff] [blame] | 425 | }; |