[U-Boot] [PATCH V3 0/4] introduce Rockchip rockusb

rockusb is a protocol run between host pc and device. it help people get device info, flash image to device. this patch implement rockusb on device side
Changes in v3: -switch back to use g_dnl code -address comment from Lukasz and Simon -fix checkpatch error
Changes in v2: -switch to use usb masstorage framework -add readme for rockusb
Eddie Cai (4): usb: rockchip: add the rockusb gadget usb: rockchip: add rockusb command rockchip:usb: add a simple readme for rockusb rockchip: usb: enable rockusb on rk3288 based board
arch/arm/include/asm/arch-rockchip/f_rockusb.h | 104 ++++ cmd/Kconfig | 11 + cmd/Makefile | 1 + cmd/rockusb.c | 74 +++ configs/evb-rk3288_defconfig | 3 + configs/fennec-rk3288_defconfig | 2 + configs/firefly-rk3288_defconfig | 2 + configs/miqi-rk3288_defconfig | 2 + configs/popmetal-rk3288_defconfig | 2 + configs/rock2_defconfig | 3 + configs/sandbox_defconfig | 3 +- configs/tinker-rk3288_defconfig | 2 + doc/README.rockusb | 37 ++ drivers/usb/gadget/Makefile | 1 + drivers/usb/gadget/f_rockusb.c | 724 +++++++++++++++++++++++++ 15 files changed, 970 insertions(+), 1 deletion(-) create mode 100644 arch/arm/include/asm/arch-rockchip/f_rockusb.h create mode 100644 cmd/rockusb.c create mode 100644 doc/README.rockusb create mode 100644 drivers/usb/gadget/f_rockusb.c

this patch implement rockusb protocol on the device side. this is based on USB download gadget infrastructure. the rockusb function implements the rd, wl, rid commands. it can work with rkdeveloptool.
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3: -split the macro to f_rockusb.h -use ALLOC_CACHE_ALIGN_BUFFER to define cache safe struct inside the function. -fix checkpatch error
--- arch/arm/include/asm/arch-rockchip/f_rockusb.h | 104 ++++ drivers/usb/gadget/Makefile | 1 + drivers/usb/gadget/f_rockusb.c | 724 +++++++++++++++++++++++++ 3 files changed, 829 insertions(+) create mode 100644 arch/arm/include/asm/arch-rockchip/f_rockusb.h create mode 100644 drivers/usb/gadget/f_rockusb.c
diff --git a/arch/arm/include/asm/arch-rockchip/f_rockusb.h b/arch/arm/include/asm/arch-rockchip/f_rockusb.h new file mode 100644 index 0000000..10b5446 --- /dev/null +++ b/arch/arm/include/asm/arch-rockchip/f_rockusb.h @@ -0,0 +1,104 @@ +/* + * (C) Copyright 2017 + * + * Eddie Cai eddie.cai.linux@gmail.com + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#ifndef __F_ROCKUSB_H +#define __F_ROCKUSB_H +#define ROCKUSB_VERSION "0.1" + +#define ROCKUSB_INTERFACE_CLASS 0xff +#define ROCKUSB_INTERFACE_SUB_CLASS 0x06 +#define ROCKUSB_INTERFACE_PROTOCOL 0x05 + +#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200) +#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040) +#define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040) + +#define EP_BUFFER_SIZE 4096 +/* + * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size + * (64 or 512 or 1024), else we break on certain controllers like DWC3 + * that expect bulk OUT requests to be divisible by maxpacket size. + */ + +#define CONFIG_ROCKUSB_BUF_ADDR CONFIG_SYS_LOAD_ADDR +#define CONFIG_ROCKUSB_BUF_SIZE 0x08000000 + +#define RKUSB_STATUS_IDLE 0 +#define RKUSB_STATUS_CMD 1 +#define RKUSB_STATUS_RXDATA 2 +#define RKUSB_STATUS_TXDATA 3 +#define RKUSB_STATUS_CSW 4 +#define RKUSB_STATUS_RXDATA_PREPARE 5 +#define RKUSB_STATUS_TXDATA_PREPARE 6 + +enum { + TEST_UNIT_READY = 0, + READ_FLASH_ID = 0x01, + TEST_BAD_BLOCK = 0x03, + READ_SECTOR = 0x04, + WRITE_SECTOR = 0x05, + ERASE_NORMAL = 0x06, + ERASE_FORCE = 0x0B, + READ_LBA = 0x14, + WRITE_LBA = 0x15, + ERASE_SYSTEMDISK = 0x16, + READ_SDRAM = 0x17, + WRITE_SDRAM = 0x18, + EXECUTE_SDRAM = 0x19, + READ_FLASH_INFO = 0x1A, + READ_CHIP_INFO = 0x1B, + SET_RESET_FLAG = 0x1E, + WRITE_EFUSE = 0x1F, + READ_EFUSE = 0x20, + READ_SPI_FLASH = 0x21, + WRITE_SPI_FLASH = 0x22, + WRITE_NEW_EFUSE = 0x23, + READ_NEW_EFUSE = 0x24, + DEVICE_RESET = 0xFF +} USB_OPERATION_CODE; + +#define K_FW_TEST_UNIT_READY 0x00 +#define K_FW_READ_FLASH_ID 0x01 +#define K_FW_SET_DEVICE_ID 0x02 +#define K_FW_TEST_BAD_BLOCK 0x03 +#define K_FW_READ_10 0x04 +#define K_FW_WRITE_10 0x05 +#define K_FW_ERASE_10 0x06 +#define K_FW_WRITE_SPARE 0x07 +#define K_FW_READ_SPARE 0x08 + +#define K_FW_ERASE_10_FORCE 0x0b +#define K_FW_GET_VERSION 0x0c + +#define K_FW_LBA_READ_10 0x14 +#define K_FW_LBA_WRITE_10 0x15 +#define K_FW_ERASE_SYS_DISK 0x16 +#define K_FW_SDRAM_READ_10 0x17 +#define K_FW_SDRAM_WRITE_10 0x18 +#define K_FW_SDRAM_EXECUTE 0x19 +#define K_FW_READ_FLASH_INFO 0x1A +#define K_FW_GET_CHIP_VER 0x1B +#define K_FW_LOW_FORMAT 0x1C +#define K_FW_SET_RESET_FLAG 0x1E +#define K_FW_SPI_READ_10 0x21 +#define K_FW_SPI_WRITE_10 0x22 + +#define K_FW_SESSION 0X30 +#define K_FW_RESET 0xff + +#define CBW_DIRECTION_OUT 0x00 +#define CBW_DIRECTION_IN 0x80 + +struct cmd_dispatch_info { + int cmd; + void (*cb)(struct usb_ep *ep, struct usb_request *req); +}; + +void rockusb_dev_init(char *dev_type, int dev_index); +#endif /* __F_ROCKUSB_H */ + diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile index 0fbbb7c..0eb27a3 100644 --- a/drivers/usb/gadget/Makefile +++ b/drivers/usb/gadget/Makefile @@ -28,6 +28,7 @@ obj-$(CONFIG_USB_FUNCTION_THOR) += f_thor.o obj-$(CONFIG_USB_FUNCTION_DFU) += f_dfu.o obj-$(CONFIG_USB_FUNCTION_MASS_STORAGE) += f_mass_storage.o obj-$(CONFIG_USB_FUNCTION_FASTBOOT) += f_fastboot.o +obj-$(CONFIG_USB_FUNCTION_ROCKUSB) += f_rockusb.o endif endif ifdef CONFIG_USB_ETHER diff --git a/drivers/usb/gadget/f_rockusb.c b/drivers/usb/gadget/f_rockusb.c new file mode 100644 index 0000000..0c666c2 --- /dev/null +++ b/drivers/usb/gadget/f_rockusb.c @@ -0,0 +1,724 @@ +/* + * (C) Copyright 2017 + * + * Eddie Cai eddie.cai.linux@gmail.com + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> +#include <common.h> +#include <errno.h> +#include <malloc.h> +#include <memalign.h> +#include <linux/usb/ch9.h> +#include <linux/usb/gadget.h> +#include <linux/usb/composite.h> +#include <linux/compiler.h> +#include <version.h> +#include <g_dnl.h> +#include <asm/arch/f_rockusb.h> + +struct f_rockusb { + struct usb_function usb_function; + /* IN/OUT EP's and corresponding requests */ + struct usb_ep *in_ep, *out_ep; + struct usb_request *in_req, *out_req; +}; + +static inline struct f_rockusb *func_to_rockusb(struct usb_function *f) +{ + return container_of(f, struct f_rockusb, usb_function); +} + +static struct f_rockusb *rockusb_func; + +static struct usb_endpoint_descriptor fs_ep_in = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = cpu_to_le16(64), +}; + +static struct usb_endpoint_descriptor fs_ep_out = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = USB_DIR_OUT, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = cpu_to_le16(64), +}; + +static struct usb_endpoint_descriptor hs_ep_in = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = cpu_to_le16(512), +}; + +static struct usb_endpoint_descriptor hs_ep_out = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = USB_DIR_OUT, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = cpu_to_le16(512), +}; + +static struct usb_interface_descriptor interface_desc = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0x00, + .bAlternateSetting = 0x00, + .bNumEndpoints = 0x02, + .bInterfaceClass = ROCKUSB_INTERFACE_CLASS, + .bInterfaceSubClass = ROCKUSB_INTERFACE_SUB_CLASS, + .bInterfaceProtocol = ROCKUSB_INTERFACE_PROTOCOL, +}; + +static struct usb_descriptor_header *rkusb_fs_function[] = { + (struct usb_descriptor_header *)&interface_desc, + (struct usb_descriptor_header *)&fs_ep_in, + (struct usb_descriptor_header *)&fs_ep_out, +}; + +static struct usb_descriptor_header *rkusb_hs_function[] = { + (struct usb_descriptor_header *)&interface_desc, + (struct usb_descriptor_header *)&hs_ep_in, + (struct usb_descriptor_header *)&hs_ep_out, + NULL, +}; + +static struct usb_endpoint_descriptor * +rkusb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, + struct usb_endpoint_descriptor *hs) +{ + if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) + return hs; + return fs; +} + +/* + * static strings, in UTF-8 + */ +static const char rkusb_name[] = "Rockchip Rockusb"; + +static struct usb_string rkusb_string_defs[] = { + [0].s = rkusb_name, + { } /* end of list */ +}; + +static struct usb_gadget_strings stringtab_rkusb = { + .language = 0x0409, /* en-us */ + .strings = rkusb_string_defs, +}; + +static struct usb_gadget_strings *rkusb_strings[] = { + &stringtab_rkusb, + NULL, +}; + +static void rx_handler_command(struct usb_ep *ep, struct usb_request *req); +static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size); + +static void rockusb_complete(struct usb_ep *ep, struct usb_request *req) +{ + int status = req->status; + if (!status) + return; + debug("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual); +} + +static int rockusb_bind(struct usb_configuration *c, struct usb_function *f) +{ + int id; + struct usb_gadget *gadget = c->cdev->gadget; + struct f_rockusb *f_rkusb = func_to_rockusb(f); + const char *s; + + id = usb_interface_id(c, f); + if (id < 0) + return id; + interface_desc.bInterfaceNumber = id; + + id = usb_string_id(c->cdev); + if (id < 0) + return id; + + rkusb_string_defs[0].id = id; + interface_desc.iInterface = id; + + f_rkusb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in); + if (!f_rkusb->in_ep) + return -ENODEV; + f_rkusb->in_ep->driver_data = c->cdev; + + f_rkusb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out); + if (!f_rkusb->out_ep) + return -ENODEV; + f_rkusb->out_ep->driver_data = c->cdev; + + f->descriptors = rkusb_fs_function; + + if (gadget_is_dualspeed(gadget)) { + hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress; + hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress; + f->hs_descriptors = rkusb_hs_function; + } + + s = getenv("serial#"); + if (s) + g_dnl_set_serialnumber((char *)s); + + return 0; +} + +static void rockusb_unbind(struct usb_configuration *c, struct usb_function *f) +{ + memset(rockusb_func, 0, sizeof(*rockusb_func)); +} + +static void rockusb_disable(struct usb_function *f) +{ + struct f_rockusb *f_rkusb = func_to_rockusb(f); + + usb_ep_disable(f_rkusb->out_ep); + usb_ep_disable(f_rkusb->in_ep); + + if (f_rkusb->out_req) { + free(f_rkusb->out_req->buf); + usb_ep_free_request(f_rkusb->out_ep, f_rkusb->out_req); + f_rkusb->out_req = NULL; + } + if (f_rkusb->in_req) { + free(f_rkusb->in_req->buf); + usb_ep_free_request(f_rkusb->in_ep, f_rkusb->in_req); + f_rkusb->in_req = NULL; + } +} + +static struct usb_request *rockusb_start_ep(struct usb_ep *ep) +{ + struct usb_request *req; + + req = usb_ep_alloc_request(ep, 0); + if (!req) + return NULL; + + req->length = EP_BUFFER_SIZE; + req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE); + if (!req->buf) { + usb_ep_free_request(ep, req); + return NULL; + } + + memset(req->buf, 0, req->length); + return req; +} + +static int rockusb_set_alt(struct usb_function *f, + unsigned interface, unsigned alt) +{ + int ret; + struct usb_composite_dev *cdev = f->config->cdev; + struct usb_gadget *gadget = cdev->gadget; + struct f_rockusb *f_rkusb = func_to_rockusb(f); + const struct usb_endpoint_descriptor *d; + + debug("%s: func: %s intf: %d alt: %d\n", + __func__, f->name, interface, alt); + + d = rkusb_ep_desc(gadget, &fs_ep_out, &hs_ep_out); + ret = usb_ep_enable(f_rkusb->out_ep, d); + if (ret) { + puts("failed to enable out ep\n"); + return ret; + } + + f_rkusb->out_req = rockusb_start_ep(f_rkusb->out_ep); + if (!f_rkusb->out_req) { + puts("failed to alloc out req\n"); + ret = -EINVAL; + goto err; + } + f_rkusb->out_req->complete = rx_handler_command; + + d = rkusb_ep_desc(gadget, &fs_ep_in, &hs_ep_in); + ret = usb_ep_enable(f_rkusb->in_ep, d); + if (ret) { + puts("failed to enable in ep\n"); + goto err; + } + + f_rkusb->in_req = rockusb_start_ep(f_rkusb->in_ep); + if (!f_rkusb->in_req) { + puts("failed alloc req in\n"); + ret = -EINVAL; + goto err; + } + f_rkusb->in_req->complete = rockusb_complete; + + ret = usb_ep_queue(f_rkusb->out_ep, f_rkusb->out_req, 0); + if (ret) + goto err; + + return 0; +err: + rockusb_disable(f); + return ret; +} + +static int rockusb_add(struct usb_configuration *c) +{ + struct f_rockusb *f_rkusb = rockusb_func; + int status; + + debug("%s: cdev: 0x%p\n", __func__, c->cdev); + + if (!f_rkusb) { + f_rkusb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_rkusb)); + if (!f_rkusb) + return -ENOMEM; + + rockusb_func = f_rkusb; + memset(f_rkusb, 0, sizeof(*f_rkusb)); + } + + f_rkusb->usb_function.name = "f_rockusb"; + f_rkusb->usb_function.bind = rockusb_bind; + f_rkusb->usb_function.unbind = rockusb_unbind; + f_rkusb->usb_function.set_alt = rockusb_set_alt; + f_rkusb->usb_function.disable = rockusb_disable; + f_rkusb->usb_function.strings = rkusb_strings; + + status = usb_add_function(c, &f_rkusb->usb_function); + if (status) { + free(f_rkusb); + rockusb_func = f_rkusb; + } + return status; +} + +char *rockusb_dev_type = 0; +int rockusb_dev_index = 0; + +void rockusb_dev_init(char *dev_type, int dev_index) +{ + rockusb_dev_type = dev_type; + rockusb_dev_index = dev_index; +} + +DECLARE_GADGET_BIND_CALLBACK(usb_dnl_rockusb, rockusb_add); + +static int rockusb_tx_write(const char *buffer, unsigned int buffer_size) +{ + struct usb_request *in_req = rockusb_func->in_req; + int ret; + + memcpy(in_req->buf, buffer, buffer_size); + in_req->length = buffer_size; + usb_ep_dequeue(rockusb_func->in_ep, in_req); + ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0); + if (ret) + printf("Error %d on queue\n", ret); + return 0; +} + +static int rockusb_tx_write_str(const char *buffer) +{ + return rockusb_tx_write(buffer, strlen(buffer)); +} + +/* Bulk-only data structures */ + +/* Command Block Wrapper */ +struct fsg_bulk_cb_wrap { + __le32 signature; /* Contains 'USBC' */ + u32 tag; /* Unique per command id */ + __le32 data_transfer_length; /* Size of the data */ + u8 flags; /* Direction in bit 7 */ + u8 lun; /* lun (normally 0) */ + u8 length; /* Of the CDB, <= MAX_COMMAND_SIZE */ + u8 CDB[16]; /* Command Data Block */ +}; + +#define USB_BULK_CB_WRAP_LEN 31 +#define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */ +#define USB_BULK_IN_FLAG 0x80 + +/* Command status Wrapper */ +struct bulk_cs_wrap { + __le32 signature; /* Should = 'USBS' */ + u32 tag; /* Same as original command */ + __le32 residue; /* Amount not transferred */ + u8 status; /* See below */ +}; + +#define USB_BULK_CS_WRAP_LEN 13 +#define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */ +#define USB_STATUS_PASS 0 +#define USB_STATUS_FAIL 1 +#define USB_STATUS_PHASE_ERROR 2 + +#define CSW_GOOD 0x00 +#define CSW_FAIL 0x01 + +#ifdef DEBUG +static void printcbw(char *buf) +{ + ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, + sizeof(struct fsg_bulk_cb_wrap)); + + memcpy((char *)cbw, buf, USB_BULK_CB_WRAP_LEN); + + debug("cbw: signature:%x\n", cbw->signature); + debug("cbw: tag=%x\n", cbw->tag); + debug("cbw: data_transfer_length=%d\n", cbw->data_transfer_length); + debug("cbw: flags=%x\n", cbw->flags); + debug("cbw: lun=%d\n", cbw->lun); + debug("cbw: length=%d\n", cbw->length); + debug("cbw: ucOperCode=%x\n", cbw->CDB[0]); + debug("cbw: ucReserved=%x\n", cbw->CDB[1]); + debug("cbw: dwAddress:%x %x %x %x\n", cbw->CDB[5], cbw->CDB[4], + cbw->CDB[3], cbw->CDB[2]); + debug("cbw: ucReserved2=%x\n", cbw->CDB[6]); + debug("cbw: uslength:%x %x\n", cbw->CDB[8], cbw->CDB[7]); +} + +static void printcsw(char *buf) +{ + ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw, + sizeof(struct bulk_cs_wrap)); + memcpy((char *)csw, buf, USB_BULK_CS_WRAP_LEN); + debug("csw: signature:%x\n", csw->signature); + debug("csw: tag:%x\n", csw->tag); + debug("csw: residue:%x\n", csw->residue); + debug("csw: status:%x\n", csw->status); +} +#endif + +static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size) +{ + ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw, + sizeof(struct bulk_cs_wrap)); + csw->signature = cpu_to_le32(USB_BULK_CS_SIG); + csw->tag = tag; + csw->residue = cpu_to_be32(residue); + csw->status = status; +#ifdef DEBUG + printcsw((char *)&csw); +#endif + return rockusb_tx_write((char *)csw, size); +} + +static unsigned int download_tag; +static unsigned int download_lba; +static unsigned int download_size; +static unsigned int download_bytes; +struct blk_desc *download_desc = 0; + +static unsigned int rx_bytes_expected(struct usb_ep *ep) +{ + int rx_remain = download_size - download_bytes; + unsigned int rem; + unsigned int maxpacket = ep->maxpacket; + + if (rx_remain <= 0) + return 0; + else if (rx_remain > EP_BUFFER_SIZE) + return EP_BUFFER_SIZE; + + rem = rx_remain % maxpacket; + if (rem > 0) + rx_remain = rx_remain + (maxpacket - rem); + + return rx_remain; +} + +static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req) +{ + unsigned int transfer_size = download_size - download_bytes; + const unsigned char *buffer = req->buf; + unsigned int buffer_size = req->actual; + + if (!download_desc) { + printf("rx_handler_dl_image blk_get_dev\n"); + download_desc = blk_get_dev(rockusb_dev_type, + rockusb_dev_index); + if (!download_desc || + download_desc->type == DEV_TYPE_UNKNOWN) { + error("invalid mmc device\n"); + rockusb_tx_write_csw(download_tag, 0, CSW_FAIL, + USB_BULK_CS_WRAP_LEN); + return; + } + } + + if (req->status != 0) { + printf("Bad status: %d\n", req->status); + rockusb_tx_write_csw(download_tag, 0, + CSW_FAIL, USB_BULK_CS_WRAP_LEN); + return; + } + + if (buffer_size < transfer_size) + transfer_size = buffer_size; + + memcpy((void *)CONFIG_ROCKUSB_BUF_ADDR + download_bytes, + buffer, transfer_size); + download_bytes += transfer_size; + + /* Check if transfer is done */ + if (download_bytes >= download_size) { + int blks = 0, blkcnt = download_size/512; + printf("download %d bytes finished, start writing to lba %x\n", + download_bytes, download_lba); + blks = blk_dwrite(download_desc, download_lba, blkcnt, + (char *)CONFIG_ROCKUSB_BUF_ADDR); + if (blks != blkcnt) { + error("failed writing to device %s: %d\n", + rockusb_dev_type, rockusb_dev_index); + rockusb_tx_write_csw(download_tag, 0, + CSW_FAIL, USB_BULK_CS_WRAP_LEN); + return; + } + + req->complete = rx_handler_command; + req->length = EP_BUFFER_SIZE; + download_size = 0; + printf("done\n"); + rockusb_tx_write_csw(download_tag, 0, CSW_GOOD, + USB_BULK_CS_WRAP_LEN); + } else { + req->length = rx_bytes_expected(ep); + } + + req->actual = 0; + usb_ep_queue(ep, req, 0); +} + +static void cb_test_unit_ready(struct usb_ep *ep, struct usb_request *req) +{ + ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, + sizeof(struct fsg_bulk_cb_wrap)); + + memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); + + rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, + CSW_GOOD, USB_BULK_CS_WRAP_LEN); +} + +static void cb_read_storage_id(struct usb_ep *ep, struct usb_request *req) +{ + ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, + sizeof(struct fsg_bulk_cb_wrap)); + char emmc_id[] = "EMMC "; + + printf("cb_read_storage_id\n"); + memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); + rockusb_tx_write_str(emmc_id); + rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD, + USB_BULK_CS_WRAP_LEN); +} + +static void cb_write_lba(struct usb_ep *ep, struct usb_request *req) +{ + ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, + sizeof(struct fsg_bulk_cb_wrap)); + int sector_count; + + memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); + sector_count = (int)get_unaligned_be16(&cbw->CDB[7]); + download_lba = get_unaligned_be32(&cbw->CDB[2]); + download_size = sector_count * 512; + download_bytes = 0; + download_tag = cbw->tag; + if ((0 == download_size) || (download_size > CONFIG_ROCKUSB_BUF_SIZE)) { + rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, + CSW_FAIL, USB_BULK_CS_WRAP_LEN); + } else { + req->complete = rx_handler_dl_image; + req->length = rx_bytes_expected(ep); + } +} + +static int reboot_flag; + +int __weak rkusb_set_reboot_flag(int flag) +{ + printf("rkusb_set_reboot_flag: %d\n", reboot_flag); + return -ENOSYS; +} + +static void compl_do_reset(struct usb_ep *ep, struct usb_request *req) +{ + rkusb_set_reboot_flag(reboot_flag); + do_reset(NULL, 0, 0, NULL); +} + +static void cb_reboot(struct usb_ep *ep, struct usb_request *req) +{ + ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, + sizeof(struct fsg_bulk_cb_wrap)); + reboot_flag = 0; + memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); + reboot_flag = cbw->CDB[1]; + rockusb_func->in_req->complete = compl_do_reset; + rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD, + USB_BULK_CS_WRAP_LEN); +} + +static void cb_not_support(struct usb_ep *ep, struct usb_request *req) +{ + ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, + sizeof(struct fsg_bulk_cb_wrap)); + + memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); + printf("Rockusb command %x not support yet\n", cbw->CDB[0]); + rockusb_tx_write_csw(cbw->tag, 0, CSW_FAIL, USB_BULK_CS_WRAP_LEN); +} + +static const struct cmd_dispatch_info cmd_dispatch_info[] = { + { + .cmd = K_FW_TEST_UNIT_READY, + .cb = cb_test_unit_ready, + }, + { + .cmd = K_FW_READ_FLASH_ID, + .cb = cb_read_storage_id, + }, + { + .cmd = K_FW_SET_DEVICE_ID, + .cb = cb_not_support, + }, + { + .cmd = K_FW_TEST_BAD_BLOCK, + .cb = cb_not_support, + }, + { + .cmd = K_FW_READ_10, + .cb = cb_not_support, + }, + { + .cmd = K_FW_WRITE_10, + .cb = cb_not_support, + }, + { + .cmd = K_FW_ERASE_10, + .cb = cb_not_support, + }, + { + .cmd = K_FW_WRITE_SPARE, + .cb = cb_not_support, + }, + { + .cmd = K_FW_READ_SPARE, + .cb = cb_not_support, + }, + { + .cmd = K_FW_ERASE_10_FORCE, + .cb = cb_not_support, + }, + { + .cmd = K_FW_GET_VERSION, + .cb = cb_not_support, + }, + { + .cmd = K_FW_LBA_READ_10, + .cb = cb_not_support, + }, + { + .cmd = K_FW_LBA_WRITE_10, + .cb = cb_write_lba, + }, + { + .cmd = K_FW_ERASE_SYS_DISK, + .cb = cb_not_support, + }, + { + .cmd = K_FW_SDRAM_READ_10, + .cb = cb_not_support, + }, + { + .cmd = K_FW_SDRAM_WRITE_10, + .cb = cb_not_support, + }, + { + .cmd = K_FW_SDRAM_EXECUTE, + .cb = cb_not_support, + }, + { + .cmd = K_FW_READ_FLASH_INFO, + .cb = cb_not_support, + }, + { + .cmd = K_FW_GET_CHIP_VER, + .cb = cb_not_support, + }, + { + .cmd = K_FW_LOW_FORMAT, + .cb = cb_not_support, + }, + { + .cmd = K_FW_SET_RESET_FLAG, + .cb = cb_not_support, + }, + { + .cmd = K_FW_SPI_READ_10, + .cb = cb_not_support, + }, + { + .cmd = K_FW_SPI_WRITE_10, + .cb = cb_not_support, + }, + { + .cmd = K_FW_SESSION, + .cb = cb_not_support, + }, + { + .cmd = K_FW_RESET, + .cb = cb_reboot, + }, +}; + +static void rx_handler_command(struct usb_ep *ep, struct usb_request *req) +{ + void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL; + ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, + sizeof(struct fsg_bulk_cb_wrap)); + char *cmdbuf = req->buf; + int i; + + if (req->status || req->length == 0) + return; + + memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); +#ifdef DEBUG + printcbw(req->buf); +#endif + + for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) { + if (cmd_dispatch_info[i].cmd == cbw->CDB[0]) { + func_cb = cmd_dispatch_info[i].cb; + break; + } + } + + if (!func_cb) { + error("unknown command: %s", (char *)req->buf); + rockusb_tx_write_str("FAILunknown command"); + } else { + if (req->actual < req->length) { + u8 *buf = (u8 *)req->buf; + buf[req->actual] = 0; + func_cb(ep, req); + } else { + error("buffer overflow"); + rockusb_tx_write_str("FAILbuffer overflow"); + } + } + + *cmdbuf = '\0'; + req->actual = 0; + usb_ep_queue(ep, req, 0); +}

Hi Eddie,
On 28 April 2017 at 18:41, Eddie Cai eddie.cai.linux@gmail.com wrote:
this patch implement rockusb protocol on the device side. this is based on USB download gadget infrastructure. the rockusb function implements the rd, wl, rid commands. it can work with rkdeveloptool.
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3: -split the macro to f_rockusb.h -use ALLOC_CACHE_ALIGN_BUFFER to define cache safe struct inside the function. -fix checkpatch error
arch/arm/include/asm/arch-rockchip/f_rockusb.h | 104 ++++ drivers/usb/gadget/Makefile | 1 + drivers/usb/gadget/f_rockusb.c | 724 +++++++++++++++++++++++++ 3 files changed, 829 insertions(+) create mode 100644 arch/arm/include/asm/arch-rockchip/f_rockusb.h create mode 100644 drivers/usb/gadget/f_rockusb.c
diff --git a/arch/arm/include/asm/arch-rockchip/f_rockusb.h b/arch/arm/include/asm/arch-rockchip/f_rockusb.h new file mode 100644 index 0000000..10b5446 --- /dev/null +++ b/arch/arm/include/asm/arch-rockchip/f_rockusb.h @@ -0,0 +1,104 @@ +/*
- (C) Copyright 2017
- Eddie Cai eddie.cai.linux@gmail.com
- SPDX-License-Identifier: GPL-2.0+
- */
+#ifndef __F_ROCKUSB_H +#define __F_ROCKUSB_H +#define ROCKUSB_VERSION "0.1"
+#define ROCKUSB_INTERFACE_CLASS 0xff +#define ROCKUSB_INTERFACE_SUB_CLASS 0x06 +#define ROCKUSB_INTERFACE_PROTOCOL 0x05
+#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200) +#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040) +#define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
+#define EP_BUFFER_SIZE 4096 +/*
- EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
- (64 or 512 or 1024), else we break on certain controllers like DWC3
- that expect bulk OUT requests to be divisible by maxpacket size.
- */
+#define CONFIG_ROCKUSB_BUF_ADDR CONFIG_SYS_LOAD_ADDR +#define CONFIG_ROCKUSB_BUF_SIZE 0x08000000
+#define RKUSB_STATUS_IDLE 0 +#define RKUSB_STATUS_CMD 1 +#define RKUSB_STATUS_RXDATA 2 +#define RKUSB_STATUS_TXDATA 3 +#define RKUSB_STATUS_CSW 4 +#define RKUSB_STATUS_RXDATA_PREPARE 5 +#define RKUSB_STATUS_TXDATA_PREPARE 6
+enum {
TEST_UNIT_READY = 0,
READ_FLASH_ID = 0x01,
TEST_BAD_BLOCK = 0x03,
READ_SECTOR = 0x04,
WRITE_SECTOR = 0x05,
ERASE_NORMAL = 0x06,
ERASE_FORCE = 0x0B,
READ_LBA = 0x14,
WRITE_LBA = 0x15,
ERASE_SYSTEMDISK = 0x16,
READ_SDRAM = 0x17,
WRITE_SDRAM = 0x18,
EXECUTE_SDRAM = 0x19,
READ_FLASH_INFO = 0x1A,
READ_CHIP_INFO = 0x1B,
SET_RESET_FLAG = 0x1E,
WRITE_EFUSE = 0x1F,
READ_EFUSE = 0x20,
READ_SPI_FLASH = 0x21,
WRITE_SPI_FLASH = 0x22,
WRITE_NEW_EFUSE = 0x23,
READ_NEW_EFUSE = 0x24,
DEVICE_RESET = 0xFF
+} USB_OPERATION_CODE;
Do you mean to declare a variable? Perhaps instead:
enum op_code { ... };
+#define K_FW_TEST_UNIT_READY 0x00
Can these be an enum also?
+#define K_FW_READ_FLASH_ID 0x01 +#define K_FW_SET_DEVICE_ID 0x02 +#define K_FW_TEST_BAD_BLOCK 0x03 +#define K_FW_READ_10 0x04 +#define K_FW_WRITE_10 0x05 +#define K_FW_ERASE_10 0x06 +#define K_FW_WRITE_SPARE 0x07 +#define K_FW_READ_SPARE 0x08
+#define K_FW_ERASE_10_FORCE 0x0b +#define K_FW_GET_VERSION 0x0c
+#define K_FW_LBA_READ_10 0x14 +#define K_FW_LBA_WRITE_10 0x15 +#define K_FW_ERASE_SYS_DISK 0x16 +#define K_FW_SDRAM_READ_10 0x17 +#define K_FW_SDRAM_WRITE_10 0x18 +#define K_FW_SDRAM_EXECUTE 0x19 +#define K_FW_READ_FLASH_INFO 0x1A +#define K_FW_GET_CHIP_VER 0x1B +#define K_FW_LOW_FORMAT 0x1C +#define K_FW_SET_RESET_FLAG 0x1E +#define K_FW_SPI_READ_10 0x21 +#define K_FW_SPI_WRITE_10 0x22
+#define K_FW_SESSION 0X30 +#define K_FW_RESET 0xff
+#define CBW_DIRECTION_OUT 0x00 +#define CBW_DIRECTION_IN 0x80
+struct cmd_dispatch_info {
int cmd;
enum
void (*cb)(struct usb_ep *ep, struct usb_request *req);
Function comment
+};
+void rockusb_dev_init(char *dev_type, int dev_index);
Function comment
+#endif /* __F_ROCKUSB_H */
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile index 0fbbb7c..0eb27a3 100644 --- a/drivers/usb/gadget/Makefile +++ b/drivers/usb/gadget/Makefile @@ -28,6 +28,7 @@ obj-$(CONFIG_USB_FUNCTION_THOR) += f_thor.o obj-$(CONFIG_USB_FUNCTION_DFU) += f_dfu.o obj-$(CONFIG_USB_FUNCTION_MASS_STORAGE) += f_mass_storage.o obj-$(CONFIG_USB_FUNCTION_FASTBOOT) += f_fastboot.o +obj-$(CONFIG_USB_FUNCTION_ROCKUSB) += f_rockusb.o endif endif ifdef CONFIG_USB_ETHER diff --git a/drivers/usb/gadget/f_rockusb.c b/drivers/usb/gadget/f_rockusb.c new file mode 100644 index 0000000..0c666c2 --- /dev/null +++ b/drivers/usb/gadget/f_rockusb.c @@ -0,0 +1,724 @@ +/*
- (C) Copyright 2017
- Eddie Cai eddie.cai.linux@gmail.com
- SPDX-License-Identifier: GPL-2.0+
- */
+#include <config.h> +#include <common.h> +#include <errno.h> +#include <malloc.h> +#include <memalign.h> +#include <linux/usb/ch9.h> +#include <linux/usb/gadget.h> +#include <linux/usb/composite.h> +#include <linux/compiler.h> +#include <version.h> +#include <g_dnl.h> +#include <asm/arch/f_rockusb.h>
+struct f_rockusb {
struct usb_function usb_function;
/* IN/OUT EP's and corresponding requests */
struct usb_ep *in_ep, *out_ep;
struct usb_request *in_req, *out_req;
+};
+static inline struct f_rockusb *func_to_rockusb(struct usb_function *f) +{
return container_of(f, struct f_rockusb, usb_function);
+}
+static struct f_rockusb *rockusb_func;
+static struct usb_endpoint_descriptor fs_ep_in = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(64),
+};
+static struct usb_endpoint_descriptor fs_ep_out = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(64),
+};
+static struct usb_endpoint_descriptor hs_ep_in = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(512),
+};
+static struct usb_endpoint_descriptor hs_ep_out = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(512),
+};
+static struct usb_interface_descriptor interface_desc = {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0x00,
.bAlternateSetting = 0x00,
.bNumEndpoints = 0x02,
.bInterfaceClass = ROCKUSB_INTERFACE_CLASS,
.bInterfaceSubClass = ROCKUSB_INTERFACE_SUB_CLASS,
.bInterfaceProtocol = ROCKUSB_INTERFACE_PROTOCOL,
+};
+static struct usb_descriptor_header *rkusb_fs_function[] = {
(struct usb_descriptor_header *)&interface_desc,
(struct usb_descriptor_header *)&fs_ep_in,
(struct usb_descriptor_header *)&fs_ep_out,
+};
+static struct usb_descriptor_header *rkusb_hs_function[] = {
(struct usb_descriptor_header *)&interface_desc,
(struct usb_descriptor_header *)&hs_ep_in,
(struct usb_descriptor_header *)&hs_ep_out,
NULL,
+};
+static struct usb_endpoint_descriptor * +rkusb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
Can you put the function name on the same line as the * ?
struct usb_endpoint_descriptor *hs)
+{
if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
return hs;
return fs;
+}
+/*
- static strings, in UTF-8
- */
+static const char rkusb_name[] = "Rockchip Rockusb";
+static struct usb_string rkusb_string_defs[] = {
[0].s = rkusb_name,
{ } /* end of list */
+};
+static struct usb_gadget_strings stringtab_rkusb = {
.language = 0x0409, /* en-us */
.strings = rkusb_string_defs,
+};
+static struct usb_gadget_strings *rkusb_strings[] = {
&stringtab_rkusb,
NULL,
+};
Can you put all data declarations together at the top of the file?
+static void rx_handler_command(struct usb_ep *ep, struct usb_request *req); +static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size);
+static void rockusb_complete(struct usb_ep *ep, struct usb_request *req) +{
int status = req->status;
if (!status)
return;
debug("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
+}
+static int rockusb_bind(struct usb_configuration *c, struct usb_function *f)
Function comment
+{
int id;
struct usb_gadget *gadget = c->cdev->gadget;
struct f_rockusb *f_rkusb = func_to_rockusb(f);
const char *s;
id = usb_interface_id(c, f);
if (id < 0)
return id;
interface_desc.bInterfaceNumber = id;
id = usb_string_id(c->cdev);
if (id < 0)
return id;
rkusb_string_defs[0].id = id;
interface_desc.iInterface = id;
f_rkusb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
if (!f_rkusb->in_ep)
return -ENODEV;
f_rkusb->in_ep->driver_data = c->cdev;
f_rkusb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
if (!f_rkusb->out_ep)
return -ENODEV;
f_rkusb->out_ep->driver_data = c->cdev;
f->descriptors = rkusb_fs_function;
if (gadget_is_dualspeed(gadget)) {
hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
f->hs_descriptors = rkusb_hs_function;
}
s = getenv("serial#");
if (s)
g_dnl_set_serialnumber((char *)s);
return 0;
+}
+static void rockusb_unbind(struct usb_configuration *c, struct usb_function *f) +{
memset(rockusb_func, 0, sizeof(*rockusb_func));
Why do you zero it? Can you add a comment?
+}
+static void rockusb_disable(struct usb_function *f) +{
struct f_rockusb *f_rkusb = func_to_rockusb(f);
usb_ep_disable(f_rkusb->out_ep);
usb_ep_disable(f_rkusb->in_ep);
if (f_rkusb->out_req) {
free(f_rkusb->out_req->buf);
usb_ep_free_request(f_rkusb->out_ep, f_rkusb->out_req);
f_rkusb->out_req = NULL;
}
if (f_rkusb->in_req) {
free(f_rkusb->in_req->buf);
usb_ep_free_request(f_rkusb->in_ep, f_rkusb->in_req);
f_rkusb->in_req = NULL;
}
+}
+static struct usb_request *rockusb_start_ep(struct usb_ep *ep) +{
struct usb_request *req;
req = usb_ep_alloc_request(ep, 0);
if (!req)
return NULL;
req->length = EP_BUFFER_SIZE;
req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
if (!req->buf) {
usb_ep_free_request(ep, req);
return NULL;
}
memset(req->buf, 0, req->length);
blank line before return
return req;
+}
+static int rockusb_set_alt(struct usb_function *f,
unsigned interface, unsigned alt)
+{
int ret;
struct usb_composite_dev *cdev = f->config->cdev;
struct usb_gadget *gadget = cdev->gadget;
struct f_rockusb *f_rkusb = func_to_rockusb(f);
const struct usb_endpoint_descriptor *d;
debug("%s: func: %s intf: %d alt: %d\n",
__func__, f->name, interface, alt);
d = rkusb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
ret = usb_ep_enable(f_rkusb->out_ep, d);
if (ret) {
puts("failed to enable out ep\n");
return ret;
}
f_rkusb->out_req = rockusb_start_ep(f_rkusb->out_ep);
if (!f_rkusb->out_req) {
puts("failed to alloc out req\n");
printf() instead of puts()
ret = -EINVAL;
goto err;
}
f_rkusb->out_req->complete = rx_handler_command;
d = rkusb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
ret = usb_ep_enable(f_rkusb->in_ep, d);
if (ret) {
puts("failed to enable in ep\n");
goto err;
}
f_rkusb->in_req = rockusb_start_ep(f_rkusb->in_ep);
if (!f_rkusb->in_req) {
puts("failed alloc req in\n");
ret = -EINVAL;
goto err;
}
f_rkusb->in_req->complete = rockusb_complete;
ret = usb_ep_queue(f_rkusb->out_ep, f_rkusb->out_req, 0);
if (ret)
goto err;
return 0;
+err:
rockusb_disable(f);
return ret;
+}
+static int rockusb_add(struct usb_configuration *c) +{
struct f_rockusb *f_rkusb = rockusb_func;
int status;
debug("%s: cdev: 0x%p\n", __func__, c->cdev);
if (!f_rkusb) {
f_rkusb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_rkusb));
if (!f_rkusb)
return -ENOMEM;
rockusb_func = f_rkusb;
memset(f_rkusb, 0, sizeof(*f_rkusb));
}
f_rkusb->usb_function.name = "f_rockusb";
f_rkusb->usb_function.bind = rockusb_bind;
f_rkusb->usb_function.unbind = rockusb_unbind;
f_rkusb->usb_function.set_alt = rockusb_set_alt;
f_rkusb->usb_function.disable = rockusb_disable;
f_rkusb->usb_function.strings = rkusb_strings;
status = usb_add_function(c, &f_rkusb->usb_function);
if (status) {
free(f_rkusb);
rockusb_func = f_rkusb;
}
return status;
+}
+char *rockusb_dev_type = 0; +int rockusb_dev_index = 0;
Move to top
I wonder if you should group all your data in a struct?
+void rockusb_dev_init(char *dev_type, int dev_index) +{
rockusb_dev_type = dev_type;
rockusb_dev_index = dev_index;
+}
+DECLARE_GADGET_BIND_CALLBACK(usb_dnl_rockusb, rockusb_add);
+static int rockusb_tx_write(const char *buffer, unsigned int buffer_size) +{
struct usb_request *in_req = rockusb_func->in_req;
int ret;
memcpy(in_req->buf, buffer, buffer_size);
in_req->length = buffer_size;
usb_ep_dequeue(rockusb_func->in_ep, in_req);
ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0);
if (ret)
printf("Error %d on queue\n", ret);
return 0;
+}
+static int rockusb_tx_write_str(const char *buffer) +{
return rockusb_tx_write(buffer, strlen(buffer));
+}
+/* Bulk-only data structures */
+/* Command Block Wrapper */ +struct fsg_bulk_cb_wrap {
__le32 signature; /* Contains 'USBC' */
u32 tag; /* Unique per command id */
__le32 data_transfer_length; /* Size of the data */
u8 flags; /* Direction in bit 7 */
u8 lun; /* lun (normally 0) */
u8 length; /* Of the CDB, <= MAX_COMMAND_SIZE */
u8 CDB[16]; /* Command Data Block */
+};
+#define USB_BULK_CB_WRAP_LEN 31 +#define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */ +#define USB_BULK_IN_FLAG 0x80
+/* Command status Wrapper */ +struct bulk_cs_wrap {
__le32 signature; /* Should = 'USBS' */
u32 tag; /* Same as original command */
__le32 residue; /* Amount not transferred */
u8 status; /* See below */
+};
+#define USB_BULK_CS_WRAP_LEN 13 +#define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */ +#define USB_STATUS_PASS 0 +#define USB_STATUS_FAIL 1 +#define USB_STATUS_PHASE_ERROR 2
+#define CSW_GOOD 0x00 +#define CSW_FAIL 0x01
+#ifdef DEBUG +static void printcbw(char *buf) +{
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
Does this need to be aligned if you are just using it for debug?
memcpy((char *)cbw, buf, USB_BULK_CB_WRAP_LEN);
debug("cbw: signature:%x\n", cbw->signature);
debug("cbw: tag=%x\n", cbw->tag);
debug("cbw: data_transfer_length=%d\n", cbw->data_transfer_length);
debug("cbw: flags=%x\n", cbw->flags);
debug("cbw: lun=%d\n", cbw->lun);
debug("cbw: length=%d\n", cbw->length);
debug("cbw: ucOperCode=%x\n", cbw->CDB[0]);
debug("cbw: ucReserved=%x\n", cbw->CDB[1]);
debug("cbw: dwAddress:%x %x %x %x\n", cbw->CDB[5], cbw->CDB[4],
cbw->CDB[3], cbw->CDB[2]);
debug("cbw: ucReserved2=%x\n", cbw->CDB[6]);
debug("cbw: uslength:%x %x\n", cbw->CDB[8], cbw->CDB[7]);
+}
+static void printcsw(char *buf) +{
ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
sizeof(struct bulk_cs_wrap));
memcpy((char *)csw, buf, USB_BULK_CS_WRAP_LEN);
debug("csw: signature:%x\n", csw->signature);
debug("csw: tag:%x\n", csw->tag);
debug("csw: residue:%x\n", csw->residue);
debug("csw: status:%x\n", csw->status);
+} +#endif
+static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size) +{
ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
sizeof(struct bulk_cs_wrap));
csw->signature = cpu_to_le32(USB_BULK_CS_SIG);
csw->tag = tag;
csw->residue = cpu_to_be32(residue);
csw->status = status;
+#ifdef DEBUG
printcsw((char *)&csw);
+#endif
return rockusb_tx_write((char *)csw, size);
+}
+static unsigned int download_tag; +static unsigned int download_lba; +static unsigned int download_size; +static unsigned int download_bytes; +struct blk_desc *download_desc = 0;
+static unsigned int rx_bytes_expected(struct usb_ep *ep) +{
int rx_remain = download_size - download_bytes;
unsigned int rem;
unsigned int maxpacket = ep->maxpacket;
if (rx_remain <= 0)
return 0;
else if (rx_remain > EP_BUFFER_SIZE)
return EP_BUFFER_SIZE;
rem = rx_remain % maxpacket;
if (rem > 0)
rx_remain = rx_remain + (maxpacket - rem);
return rx_remain;
+}
+static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
Function comments
+{
unsigned int transfer_size = download_size - download_bytes;
const unsigned char *buffer = req->buf;
unsigned int buffer_size = req->actual;
if (!download_desc) {
printf("rx_handler_dl_image blk_get_dev\n");
download_desc = blk_get_dev(rockusb_dev_type,
rockusb_dev_index);
if (!download_desc ||
download_desc->type == DEV_TYPE_UNKNOWN) {
error("invalid mmc device\n");
rockusb_tx_write_csw(download_tag, 0, CSW_FAIL,
USB_BULK_CS_WRAP_LEN);
return;
}
}
if (req->status != 0) {
printf("Bad status: %d\n", req->status);
rockusb_tx_write_csw(download_tag, 0,
CSW_FAIL, USB_BULK_CS_WRAP_LEN);
return;
}
if (buffer_size < transfer_size)
transfer_size = buffer_size;
memcpy((void *)CONFIG_ROCKUSB_BUF_ADDR + download_bytes,
buffer, transfer_size);
download_bytes += transfer_size;
/* Check if transfer is done */
if (download_bytes >= download_size) {
int blks = 0, blkcnt = download_size/512;
printf("download %d bytes finished, start writing to lba %x\n",
download_bytes, download_lba);
blks = blk_dwrite(download_desc, download_lba, blkcnt,
(char *)CONFIG_ROCKUSB_BUF_ADDR);
if (blks != blkcnt) {
error("failed writing to device %s: %d\n",
rockusb_dev_type, rockusb_dev_index);
rockusb_tx_write_csw(download_tag, 0,
CSW_FAIL, USB_BULK_CS_WRAP_LEN);
return;
}
req->complete = rx_handler_command;
req->length = EP_BUFFER_SIZE;
download_size = 0;
printf("done\n");
rockusb_tx_write_csw(download_tag, 0, CSW_GOOD,
USB_BULK_CS_WRAP_LEN);
} else {
req->length = rx_bytes_expected(ep);
}
req->actual = 0;
usb_ep_queue(ep, req, 0);
+}
+static void cb_test_unit_ready(struct usb_ep *ep, struct usb_request *req) +{
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
CSW_GOOD, USB_BULK_CS_WRAP_LEN);
+}
+static void cb_read_storage_id(struct usb_ep *ep, struct usb_request *req) +{
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
char emmc_id[] = "EMMC ";
printf("cb_read_storage_id\n");
memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
rockusb_tx_write_str(emmc_id);
rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
USB_BULK_CS_WRAP_LEN);
+}
+static void cb_write_lba(struct usb_ep *ep, struct usb_request *req) +{
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
int sector_count;
memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
download_lba = get_unaligned_be32(&cbw->CDB[2]);
download_size = sector_count * 512;
download_bytes = 0;
download_tag = cbw->tag;
if ((0 == download_size) || (download_size > CONFIG_ROCKUSB_BUF_SIZE)) {
rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
CSW_FAIL, USB_BULK_CS_WRAP_LEN);
} else {
req->complete = rx_handler_dl_image;
req->length = rx_bytes_expected(ep);
}
+}
+static int reboot_flag;
+int __weak rkusb_set_reboot_flag(int flag) +{
printf("rkusb_set_reboot_flag: %d\n", reboot_flag);
return -ENOSYS;
+}
+static void compl_do_reset(struct usb_ep *ep, struct usb_request *req) +{
rkusb_set_reboot_flag(reboot_flag);
do_reset(NULL, 0, 0, NULL);
+}
+static void cb_reboot(struct usb_ep *ep, struct usb_request *req) +{
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
reboot_flag = 0;
memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
reboot_flag = cbw->CDB[1];
rockusb_func->in_req->complete = compl_do_reset;
rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
USB_BULK_CS_WRAP_LEN);
+}
+static void cb_not_support(struct usb_ep *ep, struct usb_request *req) +{
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
printf("Rockusb command %x not support yet\n", cbw->CDB[0]);
rockusb_tx_write_csw(cbw->tag, 0, CSW_FAIL, USB_BULK_CS_WRAP_LEN);
+}
+static const struct cmd_dispatch_info cmd_dispatch_info[] = {
{
.cmd = K_FW_TEST_UNIT_READY,
.cb = cb_test_unit_ready,
},
{
.cmd = K_FW_READ_FLASH_ID,
.cb = cb_read_storage_id,
},
{
.cmd = K_FW_SET_DEVICE_ID,
.cb = cb_not_support,
},
{
.cmd = K_FW_TEST_BAD_BLOCK,
.cb = cb_not_support,
},
{
.cmd = K_FW_READ_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_WRITE_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_ERASE_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_WRITE_SPARE,
.cb = cb_not_support,
},
{
.cmd = K_FW_READ_SPARE,
.cb = cb_not_support,
},
{
.cmd = K_FW_ERASE_10_FORCE,
.cb = cb_not_support,
},
{
.cmd = K_FW_GET_VERSION,
.cb = cb_not_support,
},
{
.cmd = K_FW_LBA_READ_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_LBA_WRITE_10,
.cb = cb_write_lba,
},
{
.cmd = K_FW_ERASE_SYS_DISK,
.cb = cb_not_support,
},
{
.cmd = K_FW_SDRAM_READ_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_SDRAM_WRITE_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_SDRAM_EXECUTE,
.cb = cb_not_support,
},
{
.cmd = K_FW_READ_FLASH_INFO,
.cb = cb_not_support,
},
{
.cmd = K_FW_GET_CHIP_VER,
.cb = cb_not_support,
},
{
.cmd = K_FW_LOW_FORMAT,
.cb = cb_not_support,
},
{
.cmd = K_FW_SET_RESET_FLAG,
.cb = cb_not_support,
},
{
.cmd = K_FW_SPI_READ_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_SPI_WRITE_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_SESSION,
.cb = cb_not_support,
},
{
.cmd = K_FW_RESET,
.cb = cb_reboot,
},
+};
+static void rx_handler_command(struct usb_ep *ep, struct usb_request *req) +{
void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
char *cmdbuf = req->buf;
int i;
if (req->status || req->length == 0)
return;
memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
+#ifdef DEBUG
printcbw(req->buf);
+#endif
for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
if (cmd_dispatch_info[i].cmd == cbw->CDB[0]) {
func_cb = cmd_dispatch_info[i].cb;
break;
}
}
if (!func_cb) {
error("unknown command: %s", (char *)req->buf);
rockusb_tx_write_str("FAILunknown command");
} else {
if (req->actual < req->length) {
u8 *buf = (u8 *)req->buf;
buf[req->actual] = 0;
func_cb(ep, req);
} else {
error("buffer overflow");
rockusb_tx_write_str("FAILbuffer overflow");
}
}
*cmdbuf = '\0';
req->actual = 0;
usb_ep_queue(ep, req, 0);
+}
1.9.1
Regards, Simon

Hi Simon
2017-04-30 11:49 GMT+08:00 Simon Glass sjg@chromium.org:
Hi Eddie,
On 28 April 2017 at 18:41, Eddie Cai eddie.cai.linux@gmail.com wrote:
this patch implement rockusb protocol on the device side. this is based on USB download gadget infrastructure. the rockusb function implements the rd, wl, rid commands. it can work with rkdeveloptool.
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3: -split the macro to f_rockusb.h -use ALLOC_CACHE_ALIGN_BUFFER to define cache safe struct inside the function. -fix checkpatch error
arch/arm/include/asm/arch-rockchip/f_rockusb.h | 104 ++++ drivers/usb/gadget/Makefile | 1 + drivers/usb/gadget/f_rockusb.c | 724 +++++++++++++++++++++++++ 3 files changed, 829 insertions(+) create mode 100644 arch/arm/include/asm/arch-rockchip/f_rockusb.h create mode 100644 drivers/usb/gadget/f_rockusb.c
diff --git a/arch/arm/include/asm/arch-rockchip/f_rockusb.h b/arch/arm/include/asm/arch-rockchip/f_rockusb.h new file mode 100644 index 0000000..10b5446 --- /dev/null +++ b/arch/arm/include/asm/arch-rockchip/f_rockusb.h @@ -0,0 +1,104 @@ +/*
- (C) Copyright 2017
- Eddie Cai eddie.cai.linux@gmail.com
- SPDX-License-Identifier: GPL-2.0+
- */
+#ifndef __F_ROCKUSB_H +#define __F_ROCKUSB_H +#define ROCKUSB_VERSION "0.1"
+#define ROCKUSB_INTERFACE_CLASS 0xff +#define ROCKUSB_INTERFACE_SUB_CLASS 0x06 +#define ROCKUSB_INTERFACE_PROTOCOL 0x05
+#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200) +#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040) +#define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
+#define EP_BUFFER_SIZE 4096 +/*
- EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
- (64 or 512 or 1024), else we break on certain controllers like DWC3
- that expect bulk OUT requests to be divisible by maxpacket size.
- */
+#define CONFIG_ROCKUSB_BUF_ADDR CONFIG_SYS_LOAD_ADDR +#define CONFIG_ROCKUSB_BUF_SIZE 0x08000000
+#define RKUSB_STATUS_IDLE 0 +#define RKUSB_STATUS_CMD 1 +#define RKUSB_STATUS_RXDATA 2 +#define RKUSB_STATUS_TXDATA 3 +#define RKUSB_STATUS_CSW 4 +#define RKUSB_STATUS_RXDATA_PREPARE 5 +#define RKUSB_STATUS_TXDATA_PREPARE 6
+enum {
TEST_UNIT_READY = 0,
READ_FLASH_ID = 0x01,
TEST_BAD_BLOCK = 0x03,
READ_SECTOR = 0x04,
WRITE_SECTOR = 0x05,
ERASE_NORMAL = 0x06,
ERASE_FORCE = 0x0B,
READ_LBA = 0x14,
WRITE_LBA = 0x15,
ERASE_SYSTEMDISK = 0x16,
READ_SDRAM = 0x17,
WRITE_SDRAM = 0x18,
EXECUTE_SDRAM = 0x19,
READ_FLASH_INFO = 0x1A,
READ_CHIP_INFO = 0x1B,
SET_RESET_FLAG = 0x1E,
WRITE_EFUSE = 0x1F,
READ_EFUSE = 0x20,
READ_SPI_FLASH = 0x21,
WRITE_SPI_FLASH = 0x22,
WRITE_NEW_EFUSE = 0x23,
READ_NEW_EFUSE = 0x24,
DEVICE_RESET = 0xFF
+} USB_OPERATION_CODE;
Do you mean to declare a variable? Perhaps instead:
enum op_code {
OK
... };
+#define K_FW_TEST_UNIT_READY 0x00
Can these be an enum also?
OK
+#define K_FW_READ_FLASH_ID 0x01 +#define K_FW_SET_DEVICE_ID 0x02 +#define K_FW_TEST_BAD_BLOCK 0x03 +#define K_FW_READ_10 0x04 +#define K_FW_WRITE_10 0x05 +#define K_FW_ERASE_10 0x06 +#define K_FW_WRITE_SPARE 0x07 +#define K_FW_READ_SPARE 0x08
+#define K_FW_ERASE_10_FORCE 0x0b +#define K_FW_GET_VERSION 0x0c
+#define K_FW_LBA_READ_10 0x14 +#define K_FW_LBA_WRITE_10 0x15 +#define K_FW_ERASE_SYS_DISK 0x16 +#define K_FW_SDRAM_READ_10 0x17 +#define K_FW_SDRAM_WRITE_10 0x18 +#define K_FW_SDRAM_EXECUTE 0x19 +#define K_FW_READ_FLASH_INFO 0x1A +#define K_FW_GET_CHIP_VER 0x1B +#define K_FW_LOW_FORMAT 0x1C +#define K_FW_SET_RESET_FLAG 0x1E +#define K_FW_SPI_READ_10 0x21 +#define K_FW_SPI_WRITE_10 0x22
+#define K_FW_SESSION 0X30 +#define K_FW_RESET 0xff
+#define CBW_DIRECTION_OUT 0x00 +#define CBW_DIRECTION_IN 0x80
+struct cmd_dispatch_info {
int cmd;
enum
OK
void (*cb)(struct usb_ep *ep, struct usb_request *req);
Function comment
OK
+};
+void rockusb_dev_init(char *dev_type, int dev_index);
Function comment
OK
+#endif /* __F_ROCKUSB_H */
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile index 0fbbb7c..0eb27a3 100644 --- a/drivers/usb/gadget/Makefile +++ b/drivers/usb/gadget/Makefile @@ -28,6 +28,7 @@ obj-$(CONFIG_USB_FUNCTION_THOR) += f_thor.o obj-$(CONFIG_USB_FUNCTION_DFU) += f_dfu.o obj-$(CONFIG_USB_FUNCTION_MASS_STORAGE) += f_mass_storage.o obj-$(CONFIG_USB_FUNCTION_FASTBOOT) += f_fastboot.o +obj-$(CONFIG_USB_FUNCTION_ROCKUSB) += f_rockusb.o endif endif ifdef CONFIG_USB_ETHER diff --git a/drivers/usb/gadget/f_rockusb.c b/drivers/usb/gadget/f_rockusb.c new file mode 100644 index 0000000..0c666c2 --- /dev/null +++ b/drivers/usb/gadget/f_rockusb.c @@ -0,0 +1,724 @@ +/*
- (C) Copyright 2017
- Eddie Cai eddie.cai.linux@gmail.com
- SPDX-License-Identifier: GPL-2.0+
- */
+#include <config.h> +#include <common.h> +#include <errno.h> +#include <malloc.h> +#include <memalign.h> +#include <linux/usb/ch9.h> +#include <linux/usb/gadget.h> +#include <linux/usb/composite.h> +#include <linux/compiler.h> +#include <version.h> +#include <g_dnl.h> +#include <asm/arch/f_rockusb.h>
+struct f_rockusb {
struct usb_function usb_function;
/* IN/OUT EP's and corresponding requests */
struct usb_ep *in_ep, *out_ep;
struct usb_request *in_req, *out_req;
+};
+static inline struct f_rockusb *func_to_rockusb(struct usb_function *f) +{
return container_of(f, struct f_rockusb, usb_function);
+}
+static struct f_rockusb *rockusb_func;
+static struct usb_endpoint_descriptor fs_ep_in = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(64),
+};
+static struct usb_endpoint_descriptor fs_ep_out = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(64),
+};
+static struct usb_endpoint_descriptor hs_ep_in = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(512),
+};
+static struct usb_endpoint_descriptor hs_ep_out = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(512),
+};
+static struct usb_interface_descriptor interface_desc = {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0x00,
.bAlternateSetting = 0x00,
.bNumEndpoints = 0x02,
.bInterfaceClass = ROCKUSB_INTERFACE_CLASS,
.bInterfaceSubClass = ROCKUSB_INTERFACE_SUB_CLASS,
.bInterfaceProtocol = ROCKUSB_INTERFACE_PROTOCOL,
+};
+static struct usb_descriptor_header *rkusb_fs_function[] = {
(struct usb_descriptor_header *)&interface_desc,
(struct usb_descriptor_header *)&fs_ep_in,
(struct usb_descriptor_header *)&fs_ep_out,
+};
+static struct usb_descriptor_header *rkusb_hs_function[] = {
(struct usb_descriptor_header *)&interface_desc,
(struct usb_descriptor_header *)&hs_ep_in,
(struct usb_descriptor_header *)&hs_ep_out,
NULL,
+};
+static struct usb_endpoint_descriptor * +rkusb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
Can you put the function name on the same line as the * ?
OK
struct usb_endpoint_descriptor *hs)
+{
if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
return hs;
return fs;
+}
+/*
- static strings, in UTF-8
- */
+static const char rkusb_name[] = "Rockchip Rockusb";
+static struct usb_string rkusb_string_defs[] = {
[0].s = rkusb_name,
{ } /* end of list */
+};
+static struct usb_gadget_strings stringtab_rkusb = {
.language = 0x0409, /* en-us */
.strings = rkusb_string_defs,
+};
+static struct usb_gadget_strings *rkusb_strings[] = {
&stringtab_rkusb,
NULL,
+};
Can you put all data declarations together at the top of the file?
OK
+static void rx_handler_command(struct usb_ep *ep, struct usb_request *req); +static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size);
+static void rockusb_complete(struct usb_ep *ep, struct usb_request *req) +{
int status = req->status;
if (!status)
return;
debug("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
+}
+static int rockusb_bind(struct usb_configuration *c, struct usb_function *f)
Function comment
OK
+{
int id;
struct usb_gadget *gadget = c->cdev->gadget;
struct f_rockusb *f_rkusb = func_to_rockusb(f);
const char *s;
id = usb_interface_id(c, f);
if (id < 0)
return id;
interface_desc.bInterfaceNumber = id;
id = usb_string_id(c->cdev);
if (id < 0)
return id;
rkusb_string_defs[0].id = id;
interface_desc.iInterface = id;
f_rkusb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
if (!f_rkusb->in_ep)
return -ENODEV;
f_rkusb->in_ep->driver_data = c->cdev;
f_rkusb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
if (!f_rkusb->out_ep)
return -ENODEV;
f_rkusb->out_ep->driver_data = c->cdev;
f->descriptors = rkusb_fs_function;
if (gadget_is_dualspeed(gadget)) {
hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
f->hs_descriptors = rkusb_hs_function;
}
s = getenv("serial#");
if (s)
g_dnl_set_serialnumber((char *)s);
return 0;
+}
+static void rockusb_unbind(struct usb_configuration *c, struct usb_function *f) +{
memset(rockusb_func, 0, sizeof(*rockusb_func));
Why do you zero it? Can you add a comment?
OK
+}
+static void rockusb_disable(struct usb_function *f) +{
struct f_rockusb *f_rkusb = func_to_rockusb(f);
usb_ep_disable(f_rkusb->out_ep);
usb_ep_disable(f_rkusb->in_ep);
if (f_rkusb->out_req) {
free(f_rkusb->out_req->buf);
usb_ep_free_request(f_rkusb->out_ep, f_rkusb->out_req);
f_rkusb->out_req = NULL;
}
if (f_rkusb->in_req) {
free(f_rkusb->in_req->buf);
usb_ep_free_request(f_rkusb->in_ep, f_rkusb->in_req);
f_rkusb->in_req = NULL;
}
+}
+static struct usb_request *rockusb_start_ep(struct usb_ep *ep) +{
struct usb_request *req;
req = usb_ep_alloc_request(ep, 0);
if (!req)
return NULL;
req->length = EP_BUFFER_SIZE;
req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
if (!req->buf) {
usb_ep_free_request(ep, req);
return NULL;
}
memset(req->buf, 0, req->length);
blank line before return
OK
return req;
+}
+static int rockusb_set_alt(struct usb_function *f,
unsigned interface, unsigned alt)
+{
int ret;
struct usb_composite_dev *cdev = f->config->cdev;
struct usb_gadget *gadget = cdev->gadget;
struct f_rockusb *f_rkusb = func_to_rockusb(f);
const struct usb_endpoint_descriptor *d;
debug("%s: func: %s intf: %d alt: %d\n",
__func__, f->name, interface, alt);
d = rkusb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
ret = usb_ep_enable(f_rkusb->out_ep, d);
if (ret) {
puts("failed to enable out ep\n");
return ret;
}
f_rkusb->out_req = rockusb_start_ep(f_rkusb->out_ep);
if (!f_rkusb->out_req) {
puts("failed to alloc out req\n");
printf() instead of puts()
OK
ret = -EINVAL;
goto err;
}
f_rkusb->out_req->complete = rx_handler_command;
d = rkusb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
ret = usb_ep_enable(f_rkusb->in_ep, d);
if (ret) {
puts("failed to enable in ep\n");
goto err;
}
f_rkusb->in_req = rockusb_start_ep(f_rkusb->in_ep);
if (!f_rkusb->in_req) {
puts("failed alloc req in\n");
ret = -EINVAL;
goto err;
}
f_rkusb->in_req->complete = rockusb_complete;
ret = usb_ep_queue(f_rkusb->out_ep, f_rkusb->out_req, 0);
if (ret)
goto err;
return 0;
+err:
rockusb_disable(f);
return ret;
+}
+static int rockusb_add(struct usb_configuration *c) +{
struct f_rockusb *f_rkusb = rockusb_func;
int status;
debug("%s: cdev: 0x%p\n", __func__, c->cdev);
if (!f_rkusb) {
f_rkusb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_rkusb));
if (!f_rkusb)
return -ENOMEM;
rockusb_func = f_rkusb;
memset(f_rkusb, 0, sizeof(*f_rkusb));
}
f_rkusb->usb_function.name = "f_rockusb";
f_rkusb->usb_function.bind = rockusb_bind;
f_rkusb->usb_function.unbind = rockusb_unbind;
f_rkusb->usb_function.set_alt = rockusb_set_alt;
f_rkusb->usb_function.disable = rockusb_disable;
f_rkusb->usb_function.strings = rkusb_strings;
status = usb_add_function(c, &f_rkusb->usb_function);
if (status) {
free(f_rkusb);
rockusb_func = f_rkusb;
}
return status;
+}
+char *rockusb_dev_type = 0; +int rockusb_dev_index = 0;
Move to top
OK
I wonder if you should group all your data in a struct?
this data is useless if we just want to talk to cpu, get cpu info etc. So no need to group it in a struct. i will make it static to avoid other module use it
+void rockusb_dev_init(char *dev_type, int dev_index) +{
rockusb_dev_type = dev_type;
rockusb_dev_index = dev_index;
+}
+DECLARE_GADGET_BIND_CALLBACK(usb_dnl_rockusb, rockusb_add);
+static int rockusb_tx_write(const char *buffer, unsigned int buffer_size) +{
struct usb_request *in_req = rockusb_func->in_req;
int ret;
memcpy(in_req->buf, buffer, buffer_size);
in_req->length = buffer_size;
usb_ep_dequeue(rockusb_func->in_ep, in_req);
ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0);
if (ret)
printf("Error %d on queue\n", ret);
return 0;
+}
+static int rockusb_tx_write_str(const char *buffer) +{
return rockusb_tx_write(buffer, strlen(buffer));
+}
+/* Bulk-only data structures */
+/* Command Block Wrapper */ +struct fsg_bulk_cb_wrap {
__le32 signature; /* Contains 'USBC' */
u32 tag; /* Unique per command id */
__le32 data_transfer_length; /* Size of the data */
u8 flags; /* Direction in bit 7 */
u8 lun; /* lun (normally 0) */
u8 length; /* Of the CDB, <= MAX_COMMAND_SIZE */
u8 CDB[16]; /* Command Data Block */
+};
+#define USB_BULK_CB_WRAP_LEN 31 +#define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */ +#define USB_BULK_IN_FLAG 0x80
+/* Command status Wrapper */ +struct bulk_cs_wrap {
__le32 signature; /* Should = 'USBS' */
u32 tag; /* Same as original command */
__le32 residue; /* Amount not transferred */
u8 status; /* See below */
+};
+#define USB_BULK_CS_WRAP_LEN 13 +#define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */ +#define USB_STATUS_PASS 0 +#define USB_STATUS_FAIL 1 +#define USB_STATUS_PHASE_ERROR 2
+#define CSW_GOOD 0x00 +#define CSW_FAIL 0x01
+#ifdef DEBUG +static void printcbw(char *buf) +{
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
Does this need to be aligned if you are just using it for debug?
Yes, fsg_bulk_cb_wrap needs to be aligned.
memcpy((char *)cbw, buf, USB_BULK_CB_WRAP_LEN);
debug("cbw: signature:%x\n", cbw->signature);
debug("cbw: tag=%x\n", cbw->tag);
debug("cbw: data_transfer_length=%d\n", cbw->data_transfer_length);
debug("cbw: flags=%x\n", cbw->flags);
debug("cbw: lun=%d\n", cbw->lun);
debug("cbw: length=%d\n", cbw->length);
debug("cbw: ucOperCode=%x\n", cbw->CDB[0]);
debug("cbw: ucReserved=%x\n", cbw->CDB[1]);
debug("cbw: dwAddress:%x %x %x %x\n", cbw->CDB[5], cbw->CDB[4],
cbw->CDB[3], cbw->CDB[2]);
debug("cbw: ucReserved2=%x\n", cbw->CDB[6]);
debug("cbw: uslength:%x %x\n", cbw->CDB[8], cbw->CDB[7]);
+}
+static void printcsw(char *buf) +{
ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
sizeof(struct bulk_cs_wrap));
memcpy((char *)csw, buf, USB_BULK_CS_WRAP_LEN);
debug("csw: signature:%x\n", csw->signature);
debug("csw: tag:%x\n", csw->tag);
debug("csw: residue:%x\n", csw->residue);
debug("csw: status:%x\n", csw->status);
+} +#endif
+static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size) +{
ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
sizeof(struct bulk_cs_wrap));
csw->signature = cpu_to_le32(USB_BULK_CS_SIG);
csw->tag = tag;
csw->residue = cpu_to_be32(residue);
csw->status = status;
+#ifdef DEBUG
printcsw((char *)&csw);
+#endif
return rockusb_tx_write((char *)csw, size);
+}
+static unsigned int download_tag; +static unsigned int download_lba; +static unsigned int download_size; +static unsigned int download_bytes; +struct blk_desc *download_desc = 0;
+static unsigned int rx_bytes_expected(struct usb_ep *ep) +{
int rx_remain = download_size - download_bytes;
unsigned int rem;
unsigned int maxpacket = ep->maxpacket;
if (rx_remain <= 0)
return 0;
else if (rx_remain > EP_BUFFER_SIZE)
return EP_BUFFER_SIZE;
rem = rx_remain % maxpacket;
if (rem > 0)
rx_remain = rx_remain + (maxpacket - rem);
return rx_remain;
+}
+static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
Function comments
OK
+{
unsigned int transfer_size = download_size - download_bytes;
const unsigned char *buffer = req->buf;
unsigned int buffer_size = req->actual;
if (!download_desc) {
printf("rx_handler_dl_image blk_get_dev\n");
download_desc = blk_get_dev(rockusb_dev_type,
rockusb_dev_index);
if (!download_desc ||
download_desc->type == DEV_TYPE_UNKNOWN) {
error("invalid mmc device\n");
rockusb_tx_write_csw(download_tag, 0, CSW_FAIL,
USB_BULK_CS_WRAP_LEN);
return;
}
}
if (req->status != 0) {
printf("Bad status: %d\n", req->status);
rockusb_tx_write_csw(download_tag, 0,
CSW_FAIL, USB_BULK_CS_WRAP_LEN);
return;
}
if (buffer_size < transfer_size)
transfer_size = buffer_size;
memcpy((void *)CONFIG_ROCKUSB_BUF_ADDR + download_bytes,
buffer, transfer_size);
download_bytes += transfer_size;
/* Check if transfer is done */
if (download_bytes >= download_size) {
int blks = 0, blkcnt = download_size/512;
printf("download %d bytes finished, start writing to lba %x\n",
download_bytes, download_lba);
blks = blk_dwrite(download_desc, download_lba, blkcnt,
(char *)CONFIG_ROCKUSB_BUF_ADDR);
if (blks != blkcnt) {
error("failed writing to device %s: %d\n",
rockusb_dev_type, rockusb_dev_index);
rockusb_tx_write_csw(download_tag, 0,
CSW_FAIL, USB_BULK_CS_WRAP_LEN);
return;
}
req->complete = rx_handler_command;
req->length = EP_BUFFER_SIZE;
download_size = 0;
printf("done\n");
rockusb_tx_write_csw(download_tag, 0, CSW_GOOD,
USB_BULK_CS_WRAP_LEN);
} else {
req->length = rx_bytes_expected(ep);
}
req->actual = 0;
usb_ep_queue(ep, req, 0);
+}
+static void cb_test_unit_ready(struct usb_ep *ep, struct usb_request *req) +{
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
CSW_GOOD, USB_BULK_CS_WRAP_LEN);
+}
+static void cb_read_storage_id(struct usb_ep *ep, struct usb_request *req) +{
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
char emmc_id[] = "EMMC ";
printf("cb_read_storage_id\n");
memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
rockusb_tx_write_str(emmc_id);
rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
USB_BULK_CS_WRAP_LEN);
+}
+static void cb_write_lba(struct usb_ep *ep, struct usb_request *req) +{
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
int sector_count;
memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
download_lba = get_unaligned_be32(&cbw->CDB[2]);
download_size = sector_count * 512;
download_bytes = 0;
download_tag = cbw->tag;
if ((0 == download_size) || (download_size > CONFIG_ROCKUSB_BUF_SIZE)) {
rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
CSW_FAIL, USB_BULK_CS_WRAP_LEN);
} else {
req->complete = rx_handler_dl_image;
req->length = rx_bytes_expected(ep);
}
+}
+static int reboot_flag;
+int __weak rkusb_set_reboot_flag(int flag) +{
printf("rkusb_set_reboot_flag: %d\n", reboot_flag);
return -ENOSYS;
+}
+static void compl_do_reset(struct usb_ep *ep, struct usb_request *req) +{
rkusb_set_reboot_flag(reboot_flag);
do_reset(NULL, 0, 0, NULL);
+}
+static void cb_reboot(struct usb_ep *ep, struct usb_request *req) +{
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
reboot_flag = 0;
memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
reboot_flag = cbw->CDB[1];
rockusb_func->in_req->complete = compl_do_reset;
rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
USB_BULK_CS_WRAP_LEN);
+}
+static void cb_not_support(struct usb_ep *ep, struct usb_request *req) +{
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
printf("Rockusb command %x not support yet\n", cbw->CDB[0]);
rockusb_tx_write_csw(cbw->tag, 0, CSW_FAIL, USB_BULK_CS_WRAP_LEN);
+}
+static const struct cmd_dispatch_info cmd_dispatch_info[] = {
{
.cmd = K_FW_TEST_UNIT_READY,
.cb = cb_test_unit_ready,
},
{
.cmd = K_FW_READ_FLASH_ID,
.cb = cb_read_storage_id,
},
{
.cmd = K_FW_SET_DEVICE_ID,
.cb = cb_not_support,
},
{
.cmd = K_FW_TEST_BAD_BLOCK,
.cb = cb_not_support,
},
{
.cmd = K_FW_READ_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_WRITE_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_ERASE_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_WRITE_SPARE,
.cb = cb_not_support,
},
{
.cmd = K_FW_READ_SPARE,
.cb = cb_not_support,
},
{
.cmd = K_FW_ERASE_10_FORCE,
.cb = cb_not_support,
},
{
.cmd = K_FW_GET_VERSION,
.cb = cb_not_support,
},
{
.cmd = K_FW_LBA_READ_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_LBA_WRITE_10,
.cb = cb_write_lba,
},
{
.cmd = K_FW_ERASE_SYS_DISK,
.cb = cb_not_support,
},
{
.cmd = K_FW_SDRAM_READ_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_SDRAM_WRITE_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_SDRAM_EXECUTE,
.cb = cb_not_support,
},
{
.cmd = K_FW_READ_FLASH_INFO,
.cb = cb_not_support,
},
{
.cmd = K_FW_GET_CHIP_VER,
.cb = cb_not_support,
},
{
.cmd = K_FW_LOW_FORMAT,
.cb = cb_not_support,
},
{
.cmd = K_FW_SET_RESET_FLAG,
.cb = cb_not_support,
},
{
.cmd = K_FW_SPI_READ_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_SPI_WRITE_10,
.cb = cb_not_support,
},
{
.cmd = K_FW_SESSION,
.cb = cb_not_support,
},
{
.cmd = K_FW_RESET,
.cb = cb_reboot,
},
+};
+static void rx_handler_command(struct usb_ep *ep, struct usb_request *req) +{
void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
sizeof(struct fsg_bulk_cb_wrap));
char *cmdbuf = req->buf;
int i;
if (req->status || req->length == 0)
return;
memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
+#ifdef DEBUG
printcbw(req->buf);
+#endif
for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
if (cmd_dispatch_info[i].cmd == cbw->CDB[0]) {
func_cb = cmd_dispatch_info[i].cb;
break;
}
}
if (!func_cb) {
error("unknown command: %s", (char *)req->buf);
rockusb_tx_write_str("FAILunknown command");
} else {
if (req->actual < req->length) {
u8 *buf = (u8 *)req->buf;
buf[req->actual] = 0;
func_cb(ep, req);
} else {
error("buffer overflow");
rockusb_tx_write_str("FAILbuffer overflow");
}
}
*cmdbuf = '\0';
req->actual = 0;
usb_ep_queue(ep, req, 0);
+}
1.9.1
Regards, Simon

Hi Eddie,
On 2 May 2017 at 02:47, Eddie Cai eddie.cai.linux@gmail.com wrote:
Hi Simon
2017-04-30 11:49 GMT+08:00 Simon Glass sjg@chromium.org:
Hi Eddie,
On 28 April 2017 at 18:41, Eddie Cai eddie.cai.linux@gmail.com wrote:
this patch implement rockusb protocol on the device side. this is based on USB download gadget infrastructure. the rockusb function implements the rd, wl, rid commands. it can work with rkdeveloptool.
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3: -split the macro to f_rockusb.h -use ALLOC_CACHE_ALIGN_BUFFER to define cache safe struct inside the function. -fix checkpatch error
arch/arm/include/asm/arch-rockchip/f_rockusb.h | 104 ++++ drivers/usb/gadget/Makefile | 1 + drivers/usb/gadget/f_rockusb.c | 724 +++++++++++++++++++++++++ 3 files changed, 829 insertions(+) create mode 100644 arch/arm/include/asm/arch-rockchip/f_rockusb.h create mode 100644 drivers/usb/gadget/f_rockusb.c
[..]
+char *rockusb_dev_type = 0; +int rockusb_dev_index = 0;
Move to top
OK
I wonder if you should group all your data in a struct?
this data is useless if we just want to talk to cpu, get cpu info etc. So no need to group it in a struct. i will make it static to avoid other module use it
The thing is that we should convert USB gadgets to driver model at some point. When we do, we cannot have static local data - it all needs to go into a driver-model struct.
So I think it is better to do this now, and have a single static struct locally, then pass it around between the functions in this file. That way we have less work to do with DM conversion.
Regards, Simon

2017-05-09 0:38 GMT+08:00 Simon Glass sjg@chromium.org:
Hi Eddie,
On 2 May 2017 at 02:47, Eddie Cai eddie.cai.linux@gmail.com wrote:
Hi Simon
2017-04-30 11:49 GMT+08:00 Simon Glass sjg@chromium.org:
Hi Eddie,
On 28 April 2017 at 18:41, Eddie Cai eddie.cai.linux@gmail.com wrote:
this patch implement rockusb protocol on the device side. this is based on USB download gadget infrastructure. the rockusb function implements the rd, wl, rid commands. it can work with rkdeveloptool.
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3: -split the macro to f_rockusb.h -use ALLOC_CACHE_ALIGN_BUFFER to define cache safe struct inside the function. -fix checkpatch error
arch/arm/include/asm/arch-rockchip/f_rockusb.h | 104 ++++ drivers/usb/gadget/Makefile | 1 + drivers/usb/gadget/f_rockusb.c | 724 +++++++++++++++++++++++++ 3 files changed, 829 insertions(+) create mode 100644 arch/arm/include/asm/arch-rockchip/f_rockusb.h create mode 100644 drivers/usb/gadget/f_rockusb.c
[..]
+char *rockusb_dev_type = 0; +int rockusb_dev_index = 0;
Move to top
OK
I wonder if you should group all your data in a struct?
this data is useless if we just want to talk to cpu, get cpu info etc. So no need to group it in a struct. i will make it static to avoid other module use it
The thing is that we should convert USB gadgets to driver model at some point. When we do, we cannot have static local data - it all needs to go into a driver-model struct.
So I think it is better to do this now, and have a single static struct locally, then pass it around between the functions in this file. That way we have less work to do with DM conversion.
OK, i will do it in next version
Regards, Simon

this patch add rockusb command. the usage is rockusb <USB_controller> <devtype> <dev[:part]> e.g. rockusb 0 mmc 0
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3: -fix comment from Simon and Lukasz -fix checkpactch error
--- cmd/Kconfig | 11 +++++++++ cmd/Makefile | 1 + cmd/rockusb.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 cmd/rockusb.c
diff --git a/cmd/Kconfig b/cmd/Kconfig index 13dc46a..89f3008 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -509,6 +509,17 @@ config CMD_DFU Enables the command "dfu" which is used to have U-Boot create a DFU class device via USB.
+config CMD_ROCKUSB + bool "rockusb" + select USB_FUNCTION_ROCKUSB + help + enables the rockusb command + +config USB_FUNCTION_ROCKUSB + bool "Enable USB rockusb gadget" + help + This enables the USB part of the rockusb gadget. + config CMD_USB_MASS_STORAGE bool "UMS usb mass storage" help diff --git a/cmd/Makefile b/cmd/Makefile index 97c862f..8c071f4 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -109,6 +109,7 @@ obj-$(CONFIG_CMD_READ) += read.o obj-$(CONFIG_CMD_REGINFO) += reginfo.o obj-$(CONFIG_CMD_REISER) += reiser.o obj-$(CONFIG_CMD_REMOTEPROC) += remoteproc.o +obj-$(CONFIG_CMD_ROCKUSB) += rockusb.o obj-$(CONFIG_SANDBOX) += host.o obj-$(CONFIG_CMD_SATA) += sata.o obj-$(CONFIG_CMD_SF) += sf.o diff --git a/cmd/rockusb.c b/cmd/rockusb.c new file mode 100644 index 0000000..ae2baa6 --- /dev/null +++ b/cmd/rockusb.c @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2017 Eddie Cai eddie.cai.linux@gmail.com + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <common.h> +#include <command.h> +#include <console.h> +#include <g_dnl.h> +#include <usb.h> +#include <asm/arch/f_rockusb.h> + +static int do_rockusb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) +{ + int controller_index, dev_index; + char *usb_controller; + char *devtype; + char *devnum; + int ret; + + if (argc < 2) + return CMD_RET_USAGE; + + usb_controller = argv[1]; + controller_index = simple_strtoul(usb_controller, NULL, 0); + + if (argc >= 4) { + devtype = argv[2]; + devnum = argv[3]; + } else { + return CMD_RET_USAGE; + } + dev_index = simple_strtoul(devnum, NULL, 0); + rockusb_dev_init(devtype, dev_index); + + ret = board_usb_init(controller_index, USB_INIT_DEVICE); + if (ret) { + error("USB init failed: %d", ret); + return CMD_RET_FAILURE; + } + + g_dnl_clear_detach(); + ret = g_dnl_register("usb_dnl_rockusb"); + if (ret) + return CMD_RET_FAILURE; + + if (!g_dnl_board_usb_cable_connected()) { + puts("\rUSB cable not detected, Command exit.\n"); + ret = CMD_RET_FAILURE; + goto exit; + } + + while (1) { + if (g_dnl_detach()) + break; + if (ctrlc()) + break; + usb_gadget_handle_interrupts(controller_index); + } + ret = CMD_RET_SUCCESS; + +exit: + g_dnl_unregister(); + g_dnl_clear_detach(); + board_usb_cleanup(controller_index, USB_INIT_DEVICE); + + return ret; +} + +U_BOOT_CMD(rockusb, 4, 1, do_rockusb, + "use the rockusb protocol", + "<USB_controller> <devtype> <dev[:part]> e.g. rockusb 0 mmc 0\n" +);

Hi Eddie,
On 28 April 2017 at 18:41, Eddie Cai eddie.cai.linux@gmail.com wrote:
this patch add rockusb command. the usage is rockusb <USB_controller> <devtype> <dev[:part]> e.g. rockusb 0 mmc 0
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3: -fix comment from Simon and Lukasz -fix checkpactch error
cmd/Kconfig | 11 +++++++++ cmd/Makefile | 1 + cmd/rockusb.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 cmd/rockusb.c
Reviewed-by: Simon Glass sjg@chromium.org
Nits below
diff --git a/cmd/Kconfig b/cmd/Kconfig index 13dc46a..89f3008 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -509,6 +509,17 @@ config CMD_DFU Enables the command "dfu" which is used to have U-Boot create a DFU class device via USB.
+config CMD_ROCKUSB
bool "rockusb"
select USB_FUNCTION_ROCKUSB
help
enables the rockusb command
Can you please expand a bit:
- what it is for - link to docs on how to get tool, how to run it - depends on ROCKCHIP or similar?
+config USB_FUNCTION_ROCKUSB
bool "Enable USB rockusb gadget"
help
This enables the USB part of the rockusb gadget.
Should this depend on the above, or vice versa?
Also wondering whether this one should go in the drivers/usb directory?
config CMD_USB_MASS_STORAGE bool "UMS usb mass storage" help diff --git a/cmd/Makefile b/cmd/Makefile index 97c862f..8c071f4 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -109,6 +109,7 @@ obj-$(CONFIG_CMD_READ) += read.o obj-$(CONFIG_CMD_REGINFO) += reginfo.o obj-$(CONFIG_CMD_REISER) += reiser.o obj-$(CONFIG_CMD_REMOTEPROC) += remoteproc.o +obj-$(CONFIG_CMD_ROCKUSB) += rockusb.o obj-$(CONFIG_SANDBOX) += host.o obj-$(CONFIG_CMD_SATA) += sata.o obj-$(CONFIG_CMD_SF) += sf.o diff --git a/cmd/rockusb.c b/cmd/rockusb.c new file mode 100644 index 0000000..ae2baa6 --- /dev/null +++ b/cmd/rockusb.c @@ -0,0 +1,74 @@ +/*
- Copyright (C) 2017 Eddie Cai eddie.cai.linux@gmail.com
- SPDX-License-Identifier: GPL-2.0+
- */
+#include <common.h> +#include <command.h> +#include <console.h> +#include <g_dnl.h> +#include <usb.h> +#include <asm/arch/f_rockusb.h>
+static int do_rockusb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) +{
int controller_index, dev_index;
char *usb_controller;
char *devtype;
char *devnum;
int ret;
if (argc < 2)
return CMD_RET_USAGE;
usb_controller = argv[1];
controller_index = simple_strtoul(usb_controller, NULL, 0);
if (argc >= 4) {
devtype = argv[2];
devnum = argv[3];
} else {
return CMD_RET_USAGE;
}
dev_index = simple_strtoul(devnum, NULL, 0);
rockusb_dev_init(devtype, dev_index);
ret = board_usb_init(controller_index, USB_INIT_DEVICE);
if (ret) {
error("USB init failed: %d", ret);
return CMD_RET_FAILURE;
}
g_dnl_clear_detach();
ret = g_dnl_register("usb_dnl_rockusb");
if (ret)
return CMD_RET_FAILURE;
if (!g_dnl_board_usb_cable_connected()) {
puts("\rUSB cable not detected, Command exit.\n");
ret = CMD_RET_FAILURE;
goto exit;
}
while (1) {
if (g_dnl_detach())
break;
if (ctrlc())
break;
usb_gadget_handle_interrupts(controller_index);
}
ret = CMD_RET_SUCCESS;
+exit:
g_dnl_unregister();
g_dnl_clear_detach();
board_usb_cleanup(controller_index, USB_INIT_DEVICE);
return ret;
+}
+U_BOOT_CMD(rockusb, 4, 1, do_rockusb,
"use the rockusb protocol",
"<USB_controller> <devtype> <dev[:part]> e.g. rockusb 0 mmc 0\n"
+);
1.9.1
Regards, Simon

HI Simon
2017-04-30 11:49 GMT+08:00 Simon Glass sjg@chromium.org:
Hi Eddie,
On 28 April 2017 at 18:41, Eddie Cai eddie.cai.linux@gmail.com wrote:
this patch add rockusb command. the usage is rockusb <USB_controller> <devtype> <dev[:part]> e.g. rockusb 0 mmc 0
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3: -fix comment from Simon and Lukasz -fix checkpactch error
cmd/Kconfig | 11 +++++++++ cmd/Makefile | 1 + cmd/rockusb.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 cmd/rockusb.c
Reviewed-by: Simon Glass sjg@chromium.org
Nits below
diff --git a/cmd/Kconfig b/cmd/Kconfig index 13dc46a..89f3008 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -509,6 +509,17 @@ config CMD_DFU Enables the command "dfu" which is used to have U-Boot create a DFU class device via USB.
+config CMD_ROCKUSB
bool "rockusb"
select USB_FUNCTION_ROCKUSB
help
enables the rockusb command
Can you please expand a bit:
- what it is for
- link to docs on how to get tool, how to run it
- depends on ROCKCHIP or similar?
OK
+config USB_FUNCTION_ROCKUSB
bool "Enable USB rockusb gadget"
help
This enables the USB part of the rockusb gadget.
Should this depend on the above, or vice versa?
CMD_ROCKUSB should depend on USB_FUNCTION_ROCKUSB
Also wondering whether this one should go in the drivers/usb directory?
OK, i will move to drivers/usb/gadget/Kconfig in next version
config CMD_USB_MASS_STORAGE bool "UMS usb mass storage" help diff --git a/cmd/Makefile b/cmd/Makefile index 97c862f..8c071f4 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -109,6 +109,7 @@ obj-$(CONFIG_CMD_READ) += read.o obj-$(CONFIG_CMD_REGINFO) += reginfo.o obj-$(CONFIG_CMD_REISER) += reiser.o obj-$(CONFIG_CMD_REMOTEPROC) += remoteproc.o +obj-$(CONFIG_CMD_ROCKUSB) += rockusb.o obj-$(CONFIG_SANDBOX) += host.o obj-$(CONFIG_CMD_SATA) += sata.o obj-$(CONFIG_CMD_SF) += sf.o diff --git a/cmd/rockusb.c b/cmd/rockusb.c new file mode 100644 index 0000000..ae2baa6 --- /dev/null +++ b/cmd/rockusb.c @@ -0,0 +1,74 @@ +/*
- Copyright (C) 2017 Eddie Cai eddie.cai.linux@gmail.com
- SPDX-License-Identifier: GPL-2.0+
- */
+#include <common.h> +#include <command.h> +#include <console.h> +#include <g_dnl.h> +#include <usb.h> +#include <asm/arch/f_rockusb.h>
+static int do_rockusb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) +{
int controller_index, dev_index;
char *usb_controller;
char *devtype;
char *devnum;
int ret;
if (argc < 2)
return CMD_RET_USAGE;
usb_controller = argv[1];
controller_index = simple_strtoul(usb_controller, NULL, 0);
if (argc >= 4) {
devtype = argv[2];
devnum = argv[3];
} else {
return CMD_RET_USAGE;
}
dev_index = simple_strtoul(devnum, NULL, 0);
rockusb_dev_init(devtype, dev_index);
ret = board_usb_init(controller_index, USB_INIT_DEVICE);
if (ret) {
error("USB init failed: %d", ret);
return CMD_RET_FAILURE;
}
g_dnl_clear_detach();
ret = g_dnl_register("usb_dnl_rockusb");
if (ret)
return CMD_RET_FAILURE;
if (!g_dnl_board_usb_cable_connected()) {
puts("\rUSB cable not detected, Command exit.\n");
ret = CMD_RET_FAILURE;
goto exit;
}
while (1) {
if (g_dnl_detach())
break;
if (ctrlc())
break;
usb_gadget_handle_interrupts(controller_index);
}
ret = CMD_RET_SUCCESS;
+exit:
g_dnl_unregister();
g_dnl_clear_detach();
board_usb_cleanup(controller_index, USB_INIT_DEVICE);
return ret;
+}
+U_BOOT_CMD(rockusb, 4, 1, do_rockusb,
"use the rockusb protocol",
"<USB_controller> <devtype> <dev[:part]> e.g. rockusb 0 mmc 0\n"
+);
1.9.1
Regards, Simon

add a simple readme to introduce rockusb and tell people how to use it
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3: -fix checkpatch error --- doc/README.rockusb | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 doc/README.rockusb
diff --git a/doc/README.rockusb b/doc/README.rockusb new file mode 100644 index 0000000..262596e --- /dev/null +++ b/doc/README.rockusb @@ -0,0 +1,37 @@ +Rockusb (Rockchip USB protocol) +===================================================== + +Overview +-------- + +Rockusb protocol is widely used with Rockchip devices. +It can read/write info, image to/from devices. This +document briefly describes how to use Rockusb for upgrading +firmware (e.g. kernel, u-boot, rootfs, etc.). + +Tools +-------- +There are many tools can support Rockusb protocol. rkdeveloptool +(https://github.com/rockchip-linux/rkdeveloptool) is open source, +It is maintained by Rockchip. People don't want to build from source +can download from here +(https://github.com/rockchip-linux/rkbin/blob/master/tools/rkdeveloptool) + +Usage +-------- +The Usage of Rockusb command is +rockusb <USB_controller> <devtype> <dev[:part]> +e.g. rockusb 0 mmc 0 +On your U-Boot console, type this command to enter rockusb mode. +On your host PC. use lsusb command. you should see a usb device +using 0x2207 as its USB verdor id. Then use rkdeveloptool +sudo rkdeveloptool wl <BeginSec> <File> +to flash U-Boot image use command +sudo rkdeveloptool wl 64 <U-Boot binary> +There are plenty of Rockusb command. but wl(write lba) and +rd(reboot) command. These two command can let people flash +image to device. + +To do +----- +* Fully support Rockusb protocol

Hi Eddie,
On 28 April 2017 at 18:41, Eddie Cai eddie.cai.linux@gmail.com wrote:
add a simple readme to introduce rockusb and tell people how to use it
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3:
-fix checkpatch error
doc/README.rockusb | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 doc/README.rockusb
diff --git a/doc/README.rockusb b/doc/README.rockusb new file mode 100644 index 0000000..262596e --- /dev/null +++ b/doc/README.rockusb @@ -0,0 +1,37 @@ +Rockusb (Rockchip USB protocol) +=====================================================
+Overview +--------
+Rockusb protocol is widely used with Rockchip devices. +It can read/write info, image to/from devices. This +document briefly describes how to use Rockusb for upgrading +firmware (e.g. kernel, u-boot, rootfs, etc.).
+Tools +-------- +There are many tools can support Rockusb protocol. rkdeveloptool +(https://github.com/rockchip-linux/rkdeveloptool) is open source, +It is maintained by Rockchip. People don't want to build from source +can download from here +(https://github.com/rockchip-linux/rkbin/blob/master/tools/rkdeveloptool)
+Usage +-------- +The Usage of Rockusb command is
is:
blank line here
+rockusb <USB_controller> <devtype> <dev[:part]>
blank line
+e.g. rockusb 0 mmc 0
blanke line
+On your U-Boot console, type this command to enter rockusb mode. +On your host PC. use lsusb command. you should see a usb device +using 0x2207 as its USB verdor id. Then use rkdeveloptool
blank line
+sudo rkdeveloptool wl <BeginSec> <File>
What are the parameters for? What do they mean?
blank line
+to flash U-Boot image use command
blank line
+sudo rkdeveloptool wl 64 <U-Boot binary>
blank line
Which U-Boot file? Please be specific
+There are plenty of Rockusb command. but wl(write lba) and +rd(reboot) command. These two command can let people flash +image to device.
+To do +-----
+* Fully support Rockusb protocol
1.9.1
Regards, Simon

2017-04-30 11:49 GMT+08:00 Simon Glass sjg@chromium.org:
Hi Eddie,
On 28 April 2017 at 18:41, Eddie Cai eddie.cai.linux@gmail.com wrote:
add a simple readme to introduce rockusb and tell people how to use it
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3:
-fix checkpatch error
doc/README.rockusb | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 doc/README.rockusb
diff --git a/doc/README.rockusb b/doc/README.rockusb new file mode 100644 index 0000000..262596e --- /dev/null +++ b/doc/README.rockusb @@ -0,0 +1,37 @@ +Rockusb (Rockchip USB protocol) +=====================================================
+Overview +--------
+Rockusb protocol is widely used with Rockchip devices. +It can read/write info, image to/from devices. This +document briefly describes how to use Rockusb for upgrading +firmware (e.g. kernel, u-boot, rootfs, etc.).
+Tools +-------- +There are many tools can support Rockusb protocol. rkdeveloptool +(https://github.com/rockchip-linux/rkdeveloptool) is open source, +It is maintained by Rockchip. People don't want to build from source +can download from here +(https://github.com/rockchip-linux/rkbin/blob/master/tools/rkdeveloptool)
+Usage +-------- +The Usage of Rockusb command is
is:
blank line here
OK
+rockusb <USB_controller> <devtype> <dev[:part]>
blank line
OK
+e.g. rockusb 0 mmc 0
blanke line
OK
+On your U-Boot console, type this command to enter rockusb mode. +On your host PC. use lsusb command. you should see a usb device +using 0x2207 as its USB verdor id. Then use rkdeveloptool
blank line
OK
+sudo rkdeveloptool wl <BeginSec> <File>
What are the parameters for? What do they mean?
I will add it in next version
blank line
OK
+to flash U-Boot image use command
blank line
+sudo rkdeveloptool wl 64 <U-Boot binary>
blank line
Which U-Boot file? Please be specific
I will add it in next version
+There are plenty of Rockusb command. but wl(write lba) and +rd(reboot) command. These two command can let people flash +image to device.
+To do +-----
+* Fully support Rockusb protocol
1.9.1
Regards, Simon

all rk3288 based boards support rockusb, so enable it
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3: -move to defconfig --- configs/evb-rk3288_defconfig | 3 +++ configs/fennec-rk3288_defconfig | 2 ++ configs/firefly-rk3288_defconfig | 2 ++ configs/miqi-rk3288_defconfig | 2 ++ configs/popmetal-rk3288_defconfig | 2 ++ configs/rock2_defconfig | 3 +++ configs/sandbox_defconfig | 3 ++- configs/tinker-rk3288_defconfig | 2 ++ 8 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/configs/evb-rk3288_defconfig b/configs/evb-rk3288_defconfig index aad2533..e380988 100644 --- a/configs/evb-rk3288_defconfig +++ b/configs/evb-rk3288_defconfig @@ -17,6 +17,9 @@ CONFIG_CMD_MMC=y CONFIG_CMD_SF=y CONFIG_CMD_SPI=y CONFIG_CMD_I2C=y +CONFIG_CMD_USB=y +CONFIG_CMD_ROCKUSB=y +CONFIG_USB_FUNCTION_ROCKUSB=y CONFIG_CMD_GPIO=y # CONFIG_CMD_SETEXPR is not set CONFIG_CMD_CACHE=y diff --git a/configs/fennec-rk3288_defconfig b/configs/fennec-rk3288_defconfig index d1b0ffc..6f0a2de 100644 --- a/configs/fennec-rk3288_defconfig +++ b/configs/fennec-rk3288_defconfig @@ -18,6 +18,8 @@ CONFIG_CMD_SF=y CONFIG_CMD_SPI=y CONFIG_CMD_I2C=y CONFIG_CMD_USB=y +CONFIG_CMD_ROCKUSB=y +CONFIG_USB_FUNCTION_ROCKUSB=y CONFIG_CMD_GPIO=y # CONFIG_CMD_SETEXPR is not set CONFIG_CMD_CACHE=y diff --git a/configs/firefly-rk3288_defconfig b/configs/firefly-rk3288_defconfig index b0741d7..b2c2c72 100644 --- a/configs/firefly-rk3288_defconfig +++ b/configs/firefly-rk3288_defconfig @@ -17,6 +17,8 @@ CONFIG_CMD_SF=y CONFIG_CMD_SPI=y CONFIG_CMD_I2C=y CONFIG_CMD_USB=y +CONFIG_CMD_ROCKUSB=y +CONFIG_USB_FUNCTION_ROCKUSB=y CONFIG_CMD_GPIO=y # CONFIG_CMD_SETEXPR is not set CONFIG_CMD_CACHE=y diff --git a/configs/miqi-rk3288_defconfig b/configs/miqi-rk3288_defconfig index 203824b..278b690 100644 --- a/configs/miqi-rk3288_defconfig +++ b/configs/miqi-rk3288_defconfig @@ -17,6 +17,8 @@ CONFIG_CMD_SF=y CONFIG_CMD_SPI=y CONFIG_CMD_I2C=y CONFIG_CMD_USB=y +CONFIG_CMD_ROCKUSB=y +CONFIG_USB_FUNCTION_ROCKUSB=y CONFIG_CMD_GPIO=y # CONFIG_CMD_SETEXPR is not set CONFIG_CMD_CACHE=y diff --git a/configs/popmetal-rk3288_defconfig b/configs/popmetal-rk3288_defconfig index dfc84b9..5f7cdee 100644 --- a/configs/popmetal-rk3288_defconfig +++ b/configs/popmetal-rk3288_defconfig @@ -18,6 +18,8 @@ CONFIG_CMD_SF=y CONFIG_CMD_SPI=y CONFIG_CMD_I2C=y CONFIG_CMD_USB=y +CONFIG_CMD_ROCKUSB=y +CONFIG_USB_FUNCTION_ROCKUSB=y CONFIG_CMD_GPIO=y # CONFIG_CMD_SETEXPR is not set CONFIG_CMD_CACHE=y diff --git a/configs/rock2_defconfig b/configs/rock2_defconfig index e9a32a9..accf14c 100644 --- a/configs/rock2_defconfig +++ b/configs/rock2_defconfig @@ -16,6 +16,9 @@ CONFIG_CMD_MMC=y CONFIG_CMD_SF=y CONFIG_CMD_SPI=y CONFIG_CMD_I2C=y +CONFIG_CMD_USB=y +CONFIG_CMD_ROCKUSB=y +CONFIG_USB_FUNCTION_ROCKUSB=y CONFIG_CMD_GPIO=y # CONFIG_CMD_SETEXPR is not set CONFIG_CMD_CACHE=y diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig index 9814ea3..707f0d6 100644 --- a/configs/sandbox_defconfig +++ b/configs/sandbox_defconfig @@ -33,7 +33,8 @@ CONFIG_CMD_SF=y CONFIG_CMD_SPI=y CONFIG_CMD_I2C=y CONFIG_CMD_USB=y -CONFIG_CMD_REMOTEPROC=y +CONFIG_CMD_ROCKUSB=y +CONFIG_USB_FUNCTION_ROCKUSB=y CONFIG_CMD_GPIO=y CONFIG_CMD_TFTPPUT=y CONFIG_CMD_TFTPSRV=y diff --git a/configs/tinker-rk3288_defconfig b/configs/tinker-rk3288_defconfig index cec3938..2b0e2c8 100644 --- a/configs/tinker-rk3288_defconfig +++ b/configs/tinker-rk3288_defconfig @@ -18,6 +18,8 @@ CONFIG_CMD_SF=y CONFIG_CMD_SPI=y CONFIG_CMD_I2C=y CONFIG_CMD_USB=y +CONFIG_CMD_ROCKUSB=y +CONFIG_USB_FUNCTION_ROCKUSB=y CONFIG_CMD_GPIO=y # CONFIG_CMD_SETEXPR is not set CONFIG_CMD_CACHE=y

On 28 April 2017 at 18:41, Eddie Cai eddie.cai.linux@gmail.com wrote:
all rk3288 based boards support rockusb, so enable it
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3:
-move to defconfig
configs/evb-rk3288_defconfig | 3 +++ configs/fennec-rk3288_defconfig | 2 ++ configs/firefly-rk3288_defconfig | 2 ++ configs/miqi-rk3288_defconfig | 2 ++ configs/popmetal-rk3288_defconfig | 2 ++ configs/rock2_defconfig | 3 +++ configs/sandbox_defconfig | 3 ++- configs/tinker-rk3288_defconfig | 2 ++ 8 files changed, 18 insertions(+), 1 deletion(-)
Reviewed-by: Simon Glass sjg@chromium.org
I wonder if you could make it default to y in Kconfig?

2017-04-30 11:49 GMT+08:00 Simon Glass sjg@chromium.org:
On 28 April 2017 at 18:41, Eddie Cai eddie.cai.linux@gmail.com wrote:
all rk3288 based boards support rockusb, so enable it
Signed-off-by: Eddie Cai eddie.cai.linux@gmail.com
Changes in v3:
-move to defconfig
configs/evb-rk3288_defconfig | 3 +++ configs/fennec-rk3288_defconfig | 2 ++ configs/firefly-rk3288_defconfig | 2 ++ configs/miqi-rk3288_defconfig | 2 ++ configs/popmetal-rk3288_defconfig | 2 ++ configs/rock2_defconfig | 3 +++ configs/sandbox_defconfig | 3 ++- configs/tinker-rk3288_defconfig | 2 ++ 8 files changed, 18 insertions(+), 1 deletion(-)
Reviewed-by: Simon Glass sjg@chromium.org
Thanks SImon
I wonder if you could make it default to y in Kconfig?
Good idea. i will add it in next version

On Sat, 29 Apr 2017 08:41:34 +0800 Eddie Cai eddie.cai.linux@gmail.com wrote:
rockusb is a protocol run between host pc and device. it help people get device info, flash image to device. this patch implement rockusb on device side
Changes in v3: -switch back to use g_dnl code -address comment from Lukasz and Simon -fix checkpatch error
Changes in v2: -switch to use usb masstorage framework -add readme for rockusb
Eddie Cai (4): usb: rockchip: add the rockusb gadget usb: rockchip: add rockusb command rockchip:usb: add a simple readme for rockusb rockchip: usb: enable rockusb on rk3288 based board
arch/arm/include/asm/arch-rockchip/f_rockusb.h | 104 ++++ cmd/Kconfig | 11 + cmd/Makefile | 1 + cmd/rockusb.c | 74 +++ configs/evb-rk3288_defconfig | 3 + configs/fennec-rk3288_defconfig | 2 + configs/firefly-rk3288_defconfig | 2 + configs/miqi-rk3288_defconfig | 2 + configs/popmetal-rk3288_defconfig | 2 + configs/rock2_defconfig | 3 + configs/sandbox_defconfig | 3 +- configs/tinker-rk3288_defconfig | 2 + doc/README.rockusb | 37 ++ drivers/usb/gadget/Makefile | 1 + drivers/usb/gadget/f_rockusb.c | 724 +++++++++++++++++++++++++ 15 files changed, 970 insertions(+), 1 deletion(-) create mode 100644 arch/arm/include/asm/arch-rockchip/f_rockusb.h create mode 100644 cmd/rockusb.c create mode 100644 doc/README.rockusb create mode 100644 drivers/usb/gadget/f_rockusb.c
I've built test your patches for "rockchip" and following errors emerged:
https://travis-ci.org/lmajewski/u-boot-dfu/jobs/227226144
Please fix those issues.
Best regards,
Lukasz Majewski
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de

Hi Lukasz
2017-04-30 5:46 GMT+08:00 Lukasz Majewski lukma@denx.de:
On Sat, 29 Apr 2017 08:41:34 +0800 Eddie Cai eddie.cai.linux@gmail.com wrote:
rockusb is a protocol run between host pc and device. it help people get device info, flash image to device. this patch implement rockusb on device side
Changes in v3: -switch back to use g_dnl code -address comment from Lukasz and Simon -fix checkpatch error
Changes in v2: -switch to use usb masstorage framework -add readme for rockusb
Eddie Cai (4): usb: rockchip: add the rockusb gadget usb: rockchip: add rockusb command rockchip:usb: add a simple readme for rockusb rockchip: usb: enable rockusb on rk3288 based board
arch/arm/include/asm/arch-rockchip/f_rockusb.h | 104 ++++ cmd/Kconfig | 11 + cmd/Makefile | 1 + cmd/rockusb.c | 74 +++ configs/evb-rk3288_defconfig | 3 + configs/fennec-rk3288_defconfig | 2 + configs/firefly-rk3288_defconfig | 2 + configs/miqi-rk3288_defconfig | 2 + configs/popmetal-rk3288_defconfig | 2 + configs/rock2_defconfig | 3 + configs/sandbox_defconfig | 3 +- configs/tinker-rk3288_defconfig | 2 + doc/README.rockusb | 37 ++ drivers/usb/gadget/Makefile | 1 + drivers/usb/gadget/f_rockusb.c | 724 +++++++++++++++++++++++++ 15 files changed, 970 insertions(+), 1 deletion(-) create mode 100644 arch/arm/include/asm/arch-rockchip/f_rockusb.h create mode 100644 cmd/rockusb.c create mode 100644 doc/README.rockusb create mode 100644 drivers/usb/gadget/f_rockusb.c
I've built test your patches for "rockchip" and following errors emerged:
https://travis-ci.org/lmajewski/u-boot-dfu/jobs/227226144
Please fix those issues.
I don't understand how you got those error. Here is my test branch git clone https://github.com/eddiecailinux/u-boot -b rockusb-v3 i use below command to build u-boot CROSS_COMPILE=arm-linux-gnueabihf- make firefly-rk3288_defconfig all
Best regards,
Lukasz Majewski
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de

Hi Eddie,
Hi Lukasz
2017-04-30 5:46 GMT+08:00 Lukasz Majewski lukma@denx.de:
On Sat, 29 Apr 2017 08:41:34 +0800 Eddie Cai eddie.cai.linux@gmail.com wrote:
rockusb is a protocol run between host pc and device. it help people get device info, flash image to device. this patch implement rockusb on device side
Changes in v3: -switch back to use g_dnl code -address comment from Lukasz and Simon -fix checkpatch error
Changes in v2: -switch to use usb masstorage framework -add readme for rockusb
Eddie Cai (4): usb: rockchip: add the rockusb gadget usb: rockchip: add rockusb command rockchip:usb: add a simple readme for rockusb rockchip: usb: enable rockusb on rk3288 based board
arch/arm/include/asm/arch-rockchip/f_rockusb.h | 104 ++++ cmd/Kconfig | 11 + cmd/Makefile | 1 + cmd/rockusb.c | 74 +++ configs/evb-rk3288_defconfig | 3 + configs/fennec-rk3288_defconfig | 2 + configs/firefly-rk3288_defconfig | 2 + configs/miqi-rk3288_defconfig | 2 + configs/popmetal-rk3288_defconfig | 2 + configs/rock2_defconfig | 3 + configs/sandbox_defconfig | 3 +- configs/tinker-rk3288_defconfig | 2 + doc/README.rockusb | 37 ++ drivers/usb/gadget/Makefile | 1 + drivers/usb/gadget/f_rockusb.c | 724 +++++++++++++++++++++++++ 15 files changed, 970 insertions(+), 1 deletion(-) create mode 100644 arch/arm/include/asm/arch-rockchip/f_rockusb.h create mode 100644 cmd/rockusb.c create mode 100644 doc/README.rockusb create mode 100644 drivers/usb/gadget/f_rockusb.c
I've built test your patches for "rockchip" and following errors emerged:
^^^^^^^^^^^^^^^^^^^^ [1]
Please fix those issues.
I don't understand how you got those error.
In the last commit you have enabled the "rockchip" protocol support for other RK boards.
Have you checked if it is possible to build those boards after your change?
To avoid build break regressions we do use "buildman".
For this test I've build all "rockchip" devices (as marked in boards.cfg file).
After applying your patches I've found following errors -> [1]
The buildman configuration can be read from log posted in [1].
Here is my test branch git clone https://github.com/eddiecailinux/u-boot -b rockusb-v3 i use below command to build u-boot CROSS_COMPILE=arm-linux-gnueabihf- make firefly-rk3288_defconfig all
But this is only for one single board -> firefly-rk3288. You also should check if other boards are not affected by your code.
Best regards,
Lukasz Majewski
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de
Best regards,
Lukasz Majewski
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de

HI Lukasz
2017-05-02 18:47 GMT+08:00 Lukasz Majewski lukma@denx.de:
Hi Eddie,
Hi Lukasz
2017-04-30 5:46 GMT+08:00 Lukasz Majewski lukma@denx.de:
On Sat, 29 Apr 2017 08:41:34 +0800 Eddie Cai eddie.cai.linux@gmail.com wrote:
rockusb is a protocol run between host pc and device. it help people get device info, flash image to device. this patch implement rockusb on device side
Changes in v3: -switch back to use g_dnl code -address comment from Lukasz and Simon -fix checkpatch error
Changes in v2: -switch to use usb masstorage framework -add readme for rockusb
Eddie Cai (4): usb: rockchip: add the rockusb gadget usb: rockchip: add rockusb command rockchip:usb: add a simple readme for rockusb rockchip: usb: enable rockusb on rk3288 based board
arch/arm/include/asm/arch-rockchip/f_rockusb.h | 104 ++++ cmd/Kconfig | 11 + cmd/Makefile | 1 + cmd/rockusb.c | 74 +++ configs/evb-rk3288_defconfig | 3 + configs/fennec-rk3288_defconfig | 2 + configs/firefly-rk3288_defconfig | 2 + configs/miqi-rk3288_defconfig | 2 + configs/popmetal-rk3288_defconfig | 2 + configs/rock2_defconfig | 3 + configs/sandbox_defconfig | 3 +- configs/tinker-rk3288_defconfig | 2 + doc/README.rockusb | 37 ++ drivers/usb/gadget/Makefile | 1 + drivers/usb/gadget/f_rockusb.c | 724 +++++++++++++++++++++++++ 15 files changed, 970 insertions(+), 1 deletion(-) create mode 100644 arch/arm/include/asm/arch-rockchip/f_rockusb.h create mode 100644 cmd/rockusb.c create mode 100644 doc/README.rockusb create mode 100644 drivers/usb/gadget/f_rockusb.c
I've built test your patches for "rockchip" and following errors emerged:
^^^^^^^^^^^^^^^^^^^^ [1]
Please fix those issues.
I don't understand how you got those error.
In the last commit you have enabled the "rockchip" protocol support for other RK boards.
Have you checked if it is possible to build those boards after your change?
To avoid build break regressions we do use "buildman".
For this test I've build all "rockchip" devices (as marked in boards.cfg file).
After applying your patches I've found following errors -> [1]
The buildman configuration can be read from log posted in [1].
Here is my test branch git clone https://github.com/eddiecailinux/u-boot -b rockusb-v3 i use below command to build u-boot CROSS_COMPILE=arm-linux-gnueabihf- make firefly-rk3288_defconfig all
But this is only for one single board -> firefly-rk3288. You also should check if other boards are not affected by your code.
got it. thanks for reminding.
Best regards,
Lukasz Majewski
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de
Best regards,
Lukasz Majewski
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de
participants (3)
-
Eddie Cai
-
Lukasz Majewski
-
Simon Glass