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