2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
14 DECLARE_GLOBAL_DATA_PTR;
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
23 SANDBOX_FLASH_EP_OUT = 1, /* endpoints */
24 SANDBOX_FLASH_EP_IN = 2,
25 SANDBOX_FLASH_BLOCK_LEN = 512,
35 * struct sandbox_flash_priv - private state for this driver
37 * @error: true if there is an error condition
38 * @alloc_len: Allocation length from the last incoming command
39 * @transfer_len: Transfer length from CBW header
40 * @read_len: Number of blocks of data left in the current read command
41 * @tag: Tag value from last command
42 * @fd: File descriptor of backing file
43 * @file_size: Size of file in bytes
44 * @status_buff: Data buffer for outgoing status
45 * @buff_used: Number of bytes ready to transfer back to host
46 * @buff: Data buffer for outgoing data
48 struct sandbox_flash_priv {
57 struct umass_bbb_csw status;
62 struct sandbox_flash_plat {
66 struct scsi_inquiry_resp {
78 struct scsi_read_capacity_resp {
83 struct __packed scsi_read10_req {
93 STRINGID_MANUFACTURER = 1,
100 static struct usb_string flash_strings[] = {
101 {STRINGID_MANUFACTURER, "sandbox"},
102 {STRINGID_PRODUCT, "flash"},
103 {STRINGID_SERIAL, "2345"},
107 static struct usb_device_descriptor flash_device_desc = {
108 .bLength = sizeof(flash_device_desc),
109 .bDescriptorType = USB_DT_DEVICE,
111 .bcdUSB = __constant_cpu_to_le16(0x0200),
114 .bDeviceSubClass = 0,
115 .bDeviceProtocol = 0,
117 .idVendor = __constant_cpu_to_le16(0x1234),
118 .idProduct = __constant_cpu_to_le16(0x5678),
119 .iManufacturer = STRINGID_MANUFACTURER,
120 .iProduct = STRINGID_PRODUCT,
121 .iSerialNumber = STRINGID_SERIAL,
122 .bNumConfigurations = 1,
125 static struct usb_config_descriptor flash_config0 = {
126 .bLength = sizeof(flash_config0),
127 .bDescriptorType = USB_DT_CONFIG,
129 /* wTotalLength is set up by usb-emul-uclass */
131 .bConfigurationValue = 0,
133 .bmAttributes = 1 << 7,
137 static struct usb_interface_descriptor flash_interface0 = {
138 .bLength = sizeof(flash_interface0),
139 .bDescriptorType = USB_DT_INTERFACE,
141 .bInterfaceNumber = 0,
142 .bAlternateSetting = 0,
144 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
145 .bInterfaceSubClass = US_SC_UFI,
146 .bInterfaceProtocol = US_PR_BULK,
150 static struct usb_endpoint_descriptor flash_endpoint0_out = {
151 .bLength = USB_DT_ENDPOINT_SIZE,
152 .bDescriptorType = USB_DT_ENDPOINT,
154 .bEndpointAddress = SANDBOX_FLASH_EP_OUT,
155 .bmAttributes = USB_ENDPOINT_XFER_BULK,
156 .wMaxPacketSize = __constant_cpu_to_le16(1024),
160 static struct usb_endpoint_descriptor flash_endpoint1_in = {
161 .bLength = USB_DT_ENDPOINT_SIZE,
162 .bDescriptorType = USB_DT_ENDPOINT,
164 .bEndpointAddress = SANDBOX_FLASH_EP_IN | USB_ENDPOINT_DIR_MASK,
165 .bmAttributes = USB_ENDPOINT_XFER_BULK,
166 .wMaxPacketSize = __constant_cpu_to_le16(1024),
170 static void *flash_desc_list[] = {
174 &flash_endpoint0_out,
179 static int sandbox_flash_control(struct udevice *dev, struct usb_device *udev,
180 unsigned long pipe, void *buff, int len,
181 struct devrequest *setup)
183 struct sandbox_flash_priv *priv = dev_get_priv(dev);
185 if (pipe == usb_rcvctrlpipe(udev, 0)) {
186 switch (setup->request) {
190 case US_BBB_GET_MAX_LUN:
191 *(char *)buff = '\0';
194 debug("request=%x\n", setup->request);
198 debug("pipe=%lx\n", pipe);
203 static void setup_fail_response(struct sandbox_flash_priv *priv)
205 struct umass_bbb_csw *csw = &priv->status;
207 csw->dCSWSignature = CSWSIGNATURE;
208 csw->dCSWTag = priv->tag;
209 csw->dCSWDataResidue = 0;
210 csw->bCSWStatus = CSWSTATUS_FAILED;
215 * setup_response() - set up a response to send back to the host
217 * @priv: Sandbox flash private data
218 * @resp: Response to send, or NULL if none
219 * @size: Size of response
221 static void setup_response(struct sandbox_flash_priv *priv, void *resp,
224 struct umass_bbb_csw *csw = &priv->status;
226 csw->dCSWSignature = CSWSIGNATURE;
227 csw->dCSWTag = priv->tag;
228 csw->dCSWDataResidue = 0;
229 csw->bCSWStatus = CSWSTATUS_GOOD;
231 assert(!resp || resp == priv->buff);
232 priv->buff_used = size;
235 static void handle_read(struct sandbox_flash_priv *priv, ulong lba,
238 debug("%s: lba=%lx, transfer_len=%lx\n", __func__, lba, transfer_len);
239 if (priv->fd != -1) {
240 os_lseek(priv->fd, lba * SANDBOX_FLASH_BLOCK_LEN, OS_SEEK_SET);
241 priv->read_len = transfer_len;
242 setup_response(priv, priv->buff,
243 transfer_len * SANDBOX_FLASH_BLOCK_LEN);
245 setup_fail_response(priv);
249 static int handle_ufi_command(struct sandbox_flash_priv *priv, const void *buff,
252 const struct SCSI_cmd_block *req = buff;
256 struct scsi_inquiry_resp *resp = (void *)priv->buff;
258 priv->alloc_len = req->cmd[4];
259 memset(resp, '\0', sizeof(*resp));
260 resp->data_format = 1;
261 resp->additional_len = 0x1f;
262 strncpy(resp->vendor,
263 flash_strings[STRINGID_MANUFACTURER - 1].s,
264 sizeof(resp->vendor));
265 strncpy(resp->product, flash_strings[STRINGID_PRODUCT - 1].s,
266 sizeof(resp->product));
267 strncpy(resp->revision, "1.0", sizeof(resp->revision));
268 setup_response(priv, resp, sizeof(*resp));
272 setup_response(priv, NULL, 0);
274 case SCSI_RD_CAPAC: {
275 struct scsi_read_capacity_resp *resp = (void *)priv->buff;
279 blocks = priv->file_size / SANDBOX_FLASH_BLOCK_LEN - 1;
282 resp->last_block_addr = cpu_to_be32(blocks);
283 resp->block_len = cpu_to_be32(SANDBOX_FLASH_BLOCK_LEN);
284 setup_response(priv, resp, sizeof(*resp));
288 struct scsi_read10_req *req = (void *)buff;
290 handle_read(priv, be32_to_cpu(req->lba),
291 be16_to_cpu(req->transfer_len));
295 debug("Command not supported: %x\n", req->cmd[0]);
296 return -EPROTONOSUPPORT;
299 priv->phase = priv->transfer_len ? PHASE_DATA : PHASE_STATUS;
303 static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev,
304 unsigned long pipe, void *buff, int len)
306 struct sandbox_flash_priv *priv = dev_get_priv(dev);
307 int ep = usb_pipeendpoint(pipe);
308 struct umass_bbb_cbw *cbw = buff;
310 debug("%s: dev=%s, pipe=%lx, ep=%x, len=%x, phase=%d\n", __func__,
311 dev->name, pipe, ep, len, priv->phase);
313 case SANDBOX_FLASH_EP_OUT:
314 switch (priv->phase) {
318 if (priv->error || len != UMASS_BBB_CBW_SIZE ||
319 cbw->dCBWSignature != CBWSIGNATURE)
321 if ((cbw->bCBWFlags & CBWFLAGS_SBZ) ||
324 if (cbw->bCDBLength < 1 || cbw->bCDBLength >= 0x10)
326 priv->transfer_len = cbw->dCBWDataTransferLength;
327 priv->tag = cbw->dCBWTag;
328 return handle_ufi_command(priv, cbw->CBWCDB,
336 case SANDBOX_FLASH_EP_IN:
337 switch (priv->phase) {
339 debug("data in, len=%x, alloc_len=%x, priv->read_len=%x\n",
340 len, priv->alloc_len, priv->read_len);
341 if (priv->read_len) {
344 bytes_read = os_read(priv->fd, buff, len);
345 if (bytes_read != len)
347 priv->read_len -= len / SANDBOX_FLASH_BLOCK_LEN;
349 priv->phase = PHASE_STATUS;
351 if (priv->alloc_len && len > priv->alloc_len)
352 len = priv->alloc_len;
353 memcpy(buff, priv->buff, len);
354 priv->phase = PHASE_STATUS;
358 debug("status in, len=%x\n", len);
359 if (len > sizeof(priv->status))
360 len = sizeof(priv->status);
361 memcpy(buff, &priv->status, len);
362 priv->phase = PHASE_START;
370 debug("%s: Detected transfer error\n", __func__);
374 static int sandbox_flash_ofdata_to_platdata(struct udevice *dev)
376 struct sandbox_flash_plat *plat = dev_get_platdata(dev);
377 const void *blob = gd->fdt_blob;
379 plat->pathname = fdt_getprop(blob, dev->of_offset, "sandbox,filepath",
385 static int sandbox_flash_bind(struct udevice *dev)
387 return usb_emul_setup_device(dev, PACKET_SIZE_64, flash_strings,
391 static int sandbox_flash_probe(struct udevice *dev)
393 struct sandbox_flash_plat *plat = dev_get_platdata(dev);
394 struct sandbox_flash_priv *priv = dev_get_priv(dev);
396 priv->fd = os_open(plat->pathname, OS_O_RDONLY);
398 return os_get_filesize(plat->pathname, &priv->file_size);
403 static const struct dm_usb_ops sandbox_usb_flash_ops = {
404 .control = sandbox_flash_control,
405 .bulk = sandbox_flash_bulk,
408 static const struct udevice_id sandbox_usb_flash_ids[] = {
409 { .compatible = "sandbox,usb-flash" },
413 U_BOOT_DRIVER(usb_sandbox_flash) = {
414 .name = "usb_sandbox_flash",
415 .id = UCLASS_USB_EMUL,
416 .of_match = sandbox_usb_flash_ids,
417 .bind = sandbox_flash_bind,
418 .probe = sandbox_flash_probe,
419 .ofdata_to_platdata = sandbox_flash_ofdata_to_platdata,
420 .ops = &sandbox_usb_flash_ops,
421 .priv_auto_alloc_size = sizeof(struct sandbox_flash_priv),
422 .platdata_auto_alloc_size = sizeof(struct sandbox_flash_plat),