183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+ 2bf2b72beSEddie Cai /* 3bf2b72beSEddie Cai * (C) Copyright 2017 4bf2b72beSEddie Cai * 5bf2b72beSEddie Cai * Eddie Cai <eddie.cai.linux@gmail.com> 6bf2b72beSEddie Cai */ 7bf2b72beSEddie Cai #include <config.h> 8bf2b72beSEddie Cai #include <common.h> 9bf2b72beSEddie Cai #include <errno.h> 10bf2b72beSEddie Cai #include <malloc.h> 11bf2b72beSEddie Cai #include <memalign.h> 12bf2b72beSEddie Cai #include <linux/usb/ch9.h> 13bf2b72beSEddie Cai #include <linux/usb/gadget.h> 14bf2b72beSEddie Cai #include <linux/usb/composite.h> 15bf2b72beSEddie Cai #include <linux/compiler.h> 16bf2b72beSEddie Cai #include <version.h> 17bf2b72beSEddie Cai #include <g_dnl.h> 18bf2b72beSEddie Cai #include <asm/arch/f_rockusb.h> 19bf2b72beSEddie Cai 20bf2b72beSEddie Cai static inline struct f_rockusb *func_to_rockusb(struct usb_function *f) 21bf2b72beSEddie Cai { 22bf2b72beSEddie Cai return container_of(f, struct f_rockusb, usb_function); 23bf2b72beSEddie Cai } 24bf2b72beSEddie Cai 25bf2b72beSEddie Cai static struct usb_endpoint_descriptor fs_ep_in = { 26bf2b72beSEddie Cai .bLength = USB_DT_ENDPOINT_SIZE, 27bf2b72beSEddie Cai .bDescriptorType = USB_DT_ENDPOINT, 28bf2b72beSEddie Cai .bEndpointAddress = USB_DIR_IN, 29bf2b72beSEddie Cai .bmAttributes = USB_ENDPOINT_XFER_BULK, 30bf2b72beSEddie Cai .wMaxPacketSize = cpu_to_le16(64), 31bf2b72beSEddie Cai }; 32bf2b72beSEddie Cai 33bf2b72beSEddie Cai static struct usb_endpoint_descriptor fs_ep_out = { 34bf2b72beSEddie Cai .bLength = USB_DT_ENDPOINT_SIZE, 35bf2b72beSEddie Cai .bDescriptorType = USB_DT_ENDPOINT, 36bf2b72beSEddie Cai .bEndpointAddress = USB_DIR_OUT, 37bf2b72beSEddie Cai .bmAttributes = USB_ENDPOINT_XFER_BULK, 38bf2b72beSEddie Cai .wMaxPacketSize = cpu_to_le16(64), 39bf2b72beSEddie Cai }; 40bf2b72beSEddie Cai 41bf2b72beSEddie Cai static struct usb_endpoint_descriptor hs_ep_in = { 42bf2b72beSEddie Cai .bLength = USB_DT_ENDPOINT_SIZE, 43bf2b72beSEddie Cai .bDescriptorType = USB_DT_ENDPOINT, 44bf2b72beSEddie Cai .bEndpointAddress = USB_DIR_IN, 45bf2b72beSEddie Cai .bmAttributes = USB_ENDPOINT_XFER_BULK, 46bf2b72beSEddie Cai .wMaxPacketSize = cpu_to_le16(512), 47bf2b72beSEddie Cai }; 48bf2b72beSEddie Cai 49bf2b72beSEddie Cai static struct usb_endpoint_descriptor hs_ep_out = { 50bf2b72beSEddie Cai .bLength = USB_DT_ENDPOINT_SIZE, 51bf2b72beSEddie Cai .bDescriptorType = USB_DT_ENDPOINT, 52bf2b72beSEddie Cai .bEndpointAddress = USB_DIR_OUT, 53bf2b72beSEddie Cai .bmAttributes = USB_ENDPOINT_XFER_BULK, 54bf2b72beSEddie Cai .wMaxPacketSize = cpu_to_le16(512), 55bf2b72beSEddie Cai }; 56bf2b72beSEddie Cai 57bf2b72beSEddie Cai static struct usb_interface_descriptor interface_desc = { 58bf2b72beSEddie Cai .bLength = USB_DT_INTERFACE_SIZE, 59bf2b72beSEddie Cai .bDescriptorType = USB_DT_INTERFACE, 60bf2b72beSEddie Cai .bInterfaceNumber = 0x00, 61bf2b72beSEddie Cai .bAlternateSetting = 0x00, 62bf2b72beSEddie Cai .bNumEndpoints = 0x02, 63bf2b72beSEddie Cai .bInterfaceClass = ROCKUSB_INTERFACE_CLASS, 64bf2b72beSEddie Cai .bInterfaceSubClass = ROCKUSB_INTERFACE_SUB_CLASS, 65bf2b72beSEddie Cai .bInterfaceProtocol = ROCKUSB_INTERFACE_PROTOCOL, 66bf2b72beSEddie Cai }; 67bf2b72beSEddie Cai 68bf2b72beSEddie Cai static struct usb_descriptor_header *rkusb_fs_function[] = { 69bf2b72beSEddie Cai (struct usb_descriptor_header *)&interface_desc, 70bf2b72beSEddie Cai (struct usb_descriptor_header *)&fs_ep_in, 71bf2b72beSEddie Cai (struct usb_descriptor_header *)&fs_ep_out, 72bf2b72beSEddie Cai }; 73bf2b72beSEddie Cai 74bf2b72beSEddie Cai static struct usb_descriptor_header *rkusb_hs_function[] = { 75bf2b72beSEddie Cai (struct usb_descriptor_header *)&interface_desc, 76bf2b72beSEddie Cai (struct usb_descriptor_header *)&hs_ep_in, 77bf2b72beSEddie Cai (struct usb_descriptor_header *)&hs_ep_out, 78bf2b72beSEddie Cai NULL, 79bf2b72beSEddie Cai }; 80bf2b72beSEddie Cai 81bf2b72beSEddie Cai static const char rkusb_name[] = "Rockchip Rockusb"; 82bf2b72beSEddie Cai 83bf2b72beSEddie Cai static struct usb_string rkusb_string_defs[] = { 84bf2b72beSEddie Cai [0].s = rkusb_name, 85bf2b72beSEddie Cai { } /* end of list */ 86bf2b72beSEddie Cai }; 87bf2b72beSEddie Cai 88bf2b72beSEddie Cai static struct usb_gadget_strings stringtab_rkusb = { 89bf2b72beSEddie Cai .language = 0x0409, /* en-us */ 90bf2b72beSEddie Cai .strings = rkusb_string_defs, 91bf2b72beSEddie Cai }; 92bf2b72beSEddie Cai 93bf2b72beSEddie Cai static struct usb_gadget_strings *rkusb_strings[] = { 94bf2b72beSEddie Cai &stringtab_rkusb, 95bf2b72beSEddie Cai NULL, 96bf2b72beSEddie Cai }; 97bf2b72beSEddie Cai 98bf2b72beSEddie Cai static struct f_rockusb *rockusb_func; 99bf2b72beSEddie Cai static void rx_handler_command(struct usb_ep *ep, struct usb_request *req); 100bf2b72beSEddie Cai static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size); 101bf2b72beSEddie Cai 102bf2b72beSEddie Cai struct f_rockusb *get_rkusb(void) 103bf2b72beSEddie Cai { 104bf2b72beSEddie Cai struct f_rockusb *f_rkusb = rockusb_func; 105bf2b72beSEddie Cai 106bf2b72beSEddie Cai if (!f_rkusb) { 107bf2b72beSEddie Cai f_rkusb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_rkusb)); 108bf2b72beSEddie Cai if (!f_rkusb) 109bf2b72beSEddie Cai return 0; 110bf2b72beSEddie Cai 111bf2b72beSEddie Cai rockusb_func = f_rkusb; 112bf2b72beSEddie Cai memset(f_rkusb, 0, sizeof(*f_rkusb)); 113bf2b72beSEddie Cai } 114bf2b72beSEddie Cai 115bf2b72beSEddie Cai if (!f_rkusb->buf_head) { 116bf2b72beSEddie Cai f_rkusb->buf_head = memalign(CONFIG_SYS_CACHELINE_SIZE, 117bf2b72beSEddie Cai RKUSB_BUF_SIZE); 118bf2b72beSEddie Cai if (!f_rkusb->buf_head) 119bf2b72beSEddie Cai return 0; 120bf2b72beSEddie Cai 121bf2b72beSEddie Cai f_rkusb->buf = f_rkusb->buf_head; 122bf2b72beSEddie Cai memset(f_rkusb->buf_head, 0, RKUSB_BUF_SIZE); 123bf2b72beSEddie Cai } 124bf2b72beSEddie Cai return f_rkusb; 125bf2b72beSEddie Cai } 126bf2b72beSEddie Cai 127bf2b72beSEddie Cai static struct usb_endpoint_descriptor *rkusb_ep_desc( 128bf2b72beSEddie Cai struct usb_gadget *g, 129bf2b72beSEddie Cai struct usb_endpoint_descriptor *fs, 130bf2b72beSEddie Cai struct usb_endpoint_descriptor *hs) 131bf2b72beSEddie Cai { 132bf2b72beSEddie Cai if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) 133bf2b72beSEddie Cai return hs; 134bf2b72beSEddie Cai return fs; 135bf2b72beSEddie Cai } 136bf2b72beSEddie Cai 137bf2b72beSEddie Cai static void rockusb_complete(struct usb_ep *ep, struct usb_request *req) 138bf2b72beSEddie Cai { 139bf2b72beSEddie Cai int status = req->status; 140bf2b72beSEddie Cai 141bf2b72beSEddie Cai if (!status) 142bf2b72beSEddie Cai return; 143bf2b72beSEddie Cai debug("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual); 144bf2b72beSEddie Cai } 145bf2b72beSEddie Cai 146bf2b72beSEddie Cai /* config the rockusb device*/ 147bf2b72beSEddie Cai static int rockusb_bind(struct usb_configuration *c, struct usb_function *f) 148bf2b72beSEddie Cai { 149bf2b72beSEddie Cai int id; 150bf2b72beSEddie Cai struct usb_gadget *gadget = c->cdev->gadget; 151bf2b72beSEddie Cai struct f_rockusb *f_rkusb = func_to_rockusb(f); 152bf2b72beSEddie Cai const char *s; 153bf2b72beSEddie Cai 154bf2b72beSEddie Cai id = usb_interface_id(c, f); 155bf2b72beSEddie Cai if (id < 0) 156bf2b72beSEddie Cai return id; 157bf2b72beSEddie Cai interface_desc.bInterfaceNumber = id; 158bf2b72beSEddie Cai 159bf2b72beSEddie Cai id = usb_string_id(c->cdev); 160bf2b72beSEddie Cai if (id < 0) 161bf2b72beSEddie Cai return id; 162bf2b72beSEddie Cai 163bf2b72beSEddie Cai rkusb_string_defs[0].id = id; 164bf2b72beSEddie Cai interface_desc.iInterface = id; 165bf2b72beSEddie Cai 166bf2b72beSEddie Cai f_rkusb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in); 167bf2b72beSEddie Cai if (!f_rkusb->in_ep) 168bf2b72beSEddie Cai return -ENODEV; 169bf2b72beSEddie Cai f_rkusb->in_ep->driver_data = c->cdev; 170bf2b72beSEddie Cai 171bf2b72beSEddie Cai f_rkusb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out); 172bf2b72beSEddie Cai if (!f_rkusb->out_ep) 173bf2b72beSEddie Cai return -ENODEV; 174bf2b72beSEddie Cai f_rkusb->out_ep->driver_data = c->cdev; 175bf2b72beSEddie Cai 176bf2b72beSEddie Cai f->descriptors = rkusb_fs_function; 177bf2b72beSEddie Cai 178bf2b72beSEddie Cai if (gadget_is_dualspeed(gadget)) { 179bf2b72beSEddie Cai hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress; 180bf2b72beSEddie Cai hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress; 181bf2b72beSEddie Cai f->hs_descriptors = rkusb_hs_function; 182bf2b72beSEddie Cai } 183bf2b72beSEddie Cai 184bf2b72beSEddie Cai s = env_get("serial#"); 185bf2b72beSEddie Cai if (s) 186bf2b72beSEddie Cai g_dnl_set_serialnumber((char *)s); 187bf2b72beSEddie Cai 188bf2b72beSEddie Cai return 0; 189bf2b72beSEddie Cai } 190bf2b72beSEddie Cai 191bf2b72beSEddie Cai static void rockusb_unbind(struct usb_configuration *c, struct usb_function *f) 192bf2b72beSEddie Cai { 193bf2b72beSEddie Cai /* clear the configuration*/ 194bf2b72beSEddie Cai memset(rockusb_func, 0, sizeof(*rockusb_func)); 195bf2b72beSEddie Cai } 196bf2b72beSEddie Cai 197bf2b72beSEddie Cai static void rockusb_disable(struct usb_function *f) 198bf2b72beSEddie Cai { 199bf2b72beSEddie Cai struct f_rockusb *f_rkusb = func_to_rockusb(f); 200bf2b72beSEddie Cai 201bf2b72beSEddie Cai usb_ep_disable(f_rkusb->out_ep); 202bf2b72beSEddie Cai usb_ep_disable(f_rkusb->in_ep); 203bf2b72beSEddie Cai 204bf2b72beSEddie Cai if (f_rkusb->out_req) { 205bf2b72beSEddie Cai free(f_rkusb->out_req->buf); 206bf2b72beSEddie Cai usb_ep_free_request(f_rkusb->out_ep, f_rkusb->out_req); 207bf2b72beSEddie Cai f_rkusb->out_req = NULL; 208bf2b72beSEddie Cai } 209bf2b72beSEddie Cai if (f_rkusb->in_req) { 210bf2b72beSEddie Cai free(f_rkusb->in_req->buf); 211bf2b72beSEddie Cai usb_ep_free_request(f_rkusb->in_ep, f_rkusb->in_req); 212bf2b72beSEddie Cai f_rkusb->in_req = NULL; 213bf2b72beSEddie Cai } 214bf2b72beSEddie Cai if (f_rkusb->buf_head) { 215bf2b72beSEddie Cai free(f_rkusb->buf_head); 216bf2b72beSEddie Cai f_rkusb->buf_head = NULL; 217bf2b72beSEddie Cai f_rkusb->buf = NULL; 218bf2b72beSEddie Cai } 219bf2b72beSEddie Cai } 220bf2b72beSEddie Cai 221bf2b72beSEddie Cai static struct usb_request *rockusb_start_ep(struct usb_ep *ep) 222bf2b72beSEddie Cai { 223bf2b72beSEddie Cai struct usb_request *req; 224bf2b72beSEddie Cai 225bf2b72beSEddie Cai req = usb_ep_alloc_request(ep, 0); 226bf2b72beSEddie Cai if (!req) 227bf2b72beSEddie Cai return NULL; 228bf2b72beSEddie Cai 229bf2b72beSEddie Cai req->length = EP_BUFFER_SIZE; 230bf2b72beSEddie Cai req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE); 231bf2b72beSEddie Cai if (!req->buf) { 232bf2b72beSEddie Cai usb_ep_free_request(ep, req); 233bf2b72beSEddie Cai return NULL; 234bf2b72beSEddie Cai } 235bf2b72beSEddie Cai memset(req->buf, 0, req->length); 236bf2b72beSEddie Cai 237bf2b72beSEddie Cai return req; 238bf2b72beSEddie Cai } 239bf2b72beSEddie Cai 240bf2b72beSEddie Cai static int rockusb_set_alt(struct usb_function *f, unsigned int interface, 241bf2b72beSEddie Cai unsigned int alt) 242bf2b72beSEddie Cai { 243bf2b72beSEddie Cai int ret; 244bf2b72beSEddie Cai struct usb_composite_dev *cdev = f->config->cdev; 245bf2b72beSEddie Cai struct usb_gadget *gadget = cdev->gadget; 246bf2b72beSEddie Cai struct f_rockusb *f_rkusb = func_to_rockusb(f); 247bf2b72beSEddie Cai const struct usb_endpoint_descriptor *d; 248bf2b72beSEddie Cai 249bf2b72beSEddie Cai debug("%s: func: %s intf: %d alt: %d\n", 250bf2b72beSEddie Cai __func__, f->name, interface, alt); 251bf2b72beSEddie Cai 252bf2b72beSEddie Cai d = rkusb_ep_desc(gadget, &fs_ep_out, &hs_ep_out); 253bf2b72beSEddie Cai ret = usb_ep_enable(f_rkusb->out_ep, d); 254bf2b72beSEddie Cai if (ret) { 255bf2b72beSEddie Cai printf("failed to enable out ep\n"); 256bf2b72beSEddie Cai return ret; 257bf2b72beSEddie Cai } 258bf2b72beSEddie Cai 259bf2b72beSEddie Cai f_rkusb->out_req = rockusb_start_ep(f_rkusb->out_ep); 260bf2b72beSEddie Cai if (!f_rkusb->out_req) { 261bf2b72beSEddie Cai printf("failed to alloc out req\n"); 262bf2b72beSEddie Cai ret = -EINVAL; 263bf2b72beSEddie Cai goto err; 264bf2b72beSEddie Cai } 265bf2b72beSEddie Cai f_rkusb->out_req->complete = rx_handler_command; 266bf2b72beSEddie Cai 267bf2b72beSEddie Cai d = rkusb_ep_desc(gadget, &fs_ep_in, &hs_ep_in); 268bf2b72beSEddie Cai ret = usb_ep_enable(f_rkusb->in_ep, d); 269bf2b72beSEddie Cai if (ret) { 270bf2b72beSEddie Cai printf("failed to enable in ep\n"); 271bf2b72beSEddie Cai goto err; 272bf2b72beSEddie Cai } 273bf2b72beSEddie Cai 274bf2b72beSEddie Cai f_rkusb->in_req = rockusb_start_ep(f_rkusb->in_ep); 275bf2b72beSEddie Cai if (!f_rkusb->in_req) { 276bf2b72beSEddie Cai printf("failed alloc req in\n"); 277bf2b72beSEddie Cai ret = -EINVAL; 278bf2b72beSEddie Cai goto err; 279bf2b72beSEddie Cai } 280bf2b72beSEddie Cai f_rkusb->in_req->complete = rockusb_complete; 281bf2b72beSEddie Cai 282bf2b72beSEddie Cai ret = usb_ep_queue(f_rkusb->out_ep, f_rkusb->out_req, 0); 283bf2b72beSEddie Cai if (ret) 284bf2b72beSEddie Cai goto err; 285bf2b72beSEddie Cai 286bf2b72beSEddie Cai return 0; 287bf2b72beSEddie Cai err: 288bf2b72beSEddie Cai rockusb_disable(f); 289bf2b72beSEddie Cai return ret; 290bf2b72beSEddie Cai } 291bf2b72beSEddie Cai 292bf2b72beSEddie Cai static int rockusb_add(struct usb_configuration *c) 293bf2b72beSEddie Cai { 294bf2b72beSEddie Cai struct f_rockusb *f_rkusb = get_rkusb(); 295bf2b72beSEddie Cai int status; 296bf2b72beSEddie Cai 297bf2b72beSEddie Cai debug("%s: cdev: 0x%p\n", __func__, c->cdev); 298bf2b72beSEddie Cai 299bf2b72beSEddie Cai f_rkusb->usb_function.name = "f_rockusb"; 300bf2b72beSEddie Cai f_rkusb->usb_function.bind = rockusb_bind; 301bf2b72beSEddie Cai f_rkusb->usb_function.unbind = rockusb_unbind; 302bf2b72beSEddie Cai f_rkusb->usb_function.set_alt = rockusb_set_alt; 303bf2b72beSEddie Cai f_rkusb->usb_function.disable = rockusb_disable; 304bf2b72beSEddie Cai f_rkusb->usb_function.strings = rkusb_strings; 305bf2b72beSEddie Cai 306bf2b72beSEddie Cai status = usb_add_function(c, &f_rkusb->usb_function); 307bf2b72beSEddie Cai if (status) { 308bf2b72beSEddie Cai free(f_rkusb); 309bf2b72beSEddie Cai rockusb_func = f_rkusb; 310bf2b72beSEddie Cai } 311bf2b72beSEddie Cai return status; 312bf2b72beSEddie Cai } 313bf2b72beSEddie Cai 314bf2b72beSEddie Cai void rockusb_dev_init(char *dev_type, int dev_index) 315bf2b72beSEddie Cai { 316bf2b72beSEddie Cai struct f_rockusb *f_rkusb = get_rkusb(); 317bf2b72beSEddie Cai 318bf2b72beSEddie Cai f_rkusb->dev_type = dev_type; 319bf2b72beSEddie Cai f_rkusb->dev_index = dev_index; 320bf2b72beSEddie Cai } 321bf2b72beSEddie Cai 322bf2b72beSEddie Cai DECLARE_GADGET_BIND_CALLBACK(usb_dnl_rockusb, rockusb_add); 323bf2b72beSEddie Cai 324bf2b72beSEddie Cai static int rockusb_tx_write(const char *buffer, unsigned int buffer_size) 325bf2b72beSEddie Cai { 326bf2b72beSEddie Cai struct usb_request *in_req = rockusb_func->in_req; 327bf2b72beSEddie Cai int ret; 328bf2b72beSEddie Cai 329bf2b72beSEddie Cai memcpy(in_req->buf, buffer, buffer_size); 330bf2b72beSEddie Cai in_req->length = buffer_size; 331bf2b72beSEddie Cai usb_ep_dequeue(rockusb_func->in_ep, in_req); 332bf2b72beSEddie Cai ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0); 333bf2b72beSEddie Cai if (ret) 334bf2b72beSEddie Cai printf("Error %d on queue\n", ret); 335bf2b72beSEddie Cai return 0; 336bf2b72beSEddie Cai } 337bf2b72beSEddie Cai 338bf2b72beSEddie Cai static int rockusb_tx_write_str(const char *buffer) 339bf2b72beSEddie Cai { 340bf2b72beSEddie Cai return rockusb_tx_write(buffer, strlen(buffer)); 341bf2b72beSEddie Cai } 342bf2b72beSEddie Cai 343bf2b72beSEddie Cai #ifdef DEBUG 344bf2b72beSEddie Cai static void printcbw(char *buf) 345bf2b72beSEddie Cai { 346bf2b72beSEddie Cai ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, 347bf2b72beSEddie Cai sizeof(struct fsg_bulk_cb_wrap)); 348bf2b72beSEddie Cai 349bf2b72beSEddie Cai memcpy((char *)cbw, buf, USB_BULK_CB_WRAP_LEN); 350bf2b72beSEddie Cai 351bf2b72beSEddie Cai debug("cbw: signature:%x\n", cbw->signature); 352bf2b72beSEddie Cai debug("cbw: tag=%x\n", cbw->tag); 353bf2b72beSEddie Cai debug("cbw: data_transfer_length=%d\n", cbw->data_transfer_length); 354bf2b72beSEddie Cai debug("cbw: flags=%x\n", cbw->flags); 355bf2b72beSEddie Cai debug("cbw: lun=%d\n", cbw->lun); 356bf2b72beSEddie Cai debug("cbw: length=%d\n", cbw->length); 357bf2b72beSEddie Cai debug("cbw: ucOperCode=%x\n", cbw->CDB[0]); 358bf2b72beSEddie Cai debug("cbw: ucReserved=%x\n", cbw->CDB[1]); 359bf2b72beSEddie Cai debug("cbw: dwAddress:%x %x %x %x\n", cbw->CDB[5], cbw->CDB[4], 360bf2b72beSEddie Cai cbw->CDB[3], cbw->CDB[2]); 361bf2b72beSEddie Cai debug("cbw: ucReserved2=%x\n", cbw->CDB[6]); 362bf2b72beSEddie Cai debug("cbw: uslength:%x %x\n", cbw->CDB[8], cbw->CDB[7]); 363bf2b72beSEddie Cai } 364bf2b72beSEddie Cai 365bf2b72beSEddie Cai static void printcsw(char *buf) 366bf2b72beSEddie Cai { 367bf2b72beSEddie Cai ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw, 368bf2b72beSEddie Cai sizeof(struct bulk_cs_wrap)); 369bf2b72beSEddie Cai memcpy((char *)csw, buf, USB_BULK_CS_WRAP_LEN); 370bf2b72beSEddie Cai debug("csw: signature:%x\n", csw->signature); 371bf2b72beSEddie Cai debug("csw: tag:%x\n", csw->tag); 372bf2b72beSEddie Cai debug("csw: residue:%x\n", csw->residue); 373bf2b72beSEddie Cai debug("csw: status:%x\n", csw->status); 374bf2b72beSEddie Cai } 375bf2b72beSEddie Cai #endif 376bf2b72beSEddie Cai 377bf2b72beSEddie Cai static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size) 378bf2b72beSEddie Cai { 379bf2b72beSEddie Cai ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw, 380bf2b72beSEddie Cai sizeof(struct bulk_cs_wrap)); 381bf2b72beSEddie Cai csw->signature = cpu_to_le32(USB_BULK_CS_SIG); 382bf2b72beSEddie Cai csw->tag = tag; 383bf2b72beSEddie Cai csw->residue = cpu_to_be32(residue); 384bf2b72beSEddie Cai csw->status = status; 385bf2b72beSEddie Cai #ifdef DEBUG 386bf2b72beSEddie Cai printcsw((char *)&csw); 387bf2b72beSEddie Cai #endif 388bf2b72beSEddie Cai return rockusb_tx_write((char *)csw, size); 389bf2b72beSEddie Cai } 390bf2b72beSEddie Cai 391cad66e32SAlberto Panizzo static void tx_handler_send_csw(struct usb_ep *ep, struct usb_request *req) 392cad66e32SAlberto Panizzo { 393cad66e32SAlberto Panizzo struct f_rockusb *f_rkusb = get_rkusb(); 394cad66e32SAlberto Panizzo int status = req->status; 395cad66e32SAlberto Panizzo 396cad66e32SAlberto Panizzo if (status) 397cad66e32SAlberto Panizzo debug("status: %d ep '%s' trans: %d\n", 398cad66e32SAlberto Panizzo status, ep->name, req->actual); 399cad66e32SAlberto Panizzo 400cad66e32SAlberto Panizzo /* Return back to default in_req complete function after sending CSW */ 401cad66e32SAlberto Panizzo req->complete = rockusb_complete; 402cad66e32SAlberto Panizzo rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD, USB_BULK_CS_WRAP_LEN); 403cad66e32SAlberto Panizzo } 404cad66e32SAlberto Panizzo 405bf2b72beSEddie Cai static unsigned int rx_bytes_expected(struct usb_ep *ep) 406bf2b72beSEddie Cai { 407bf2b72beSEddie Cai struct f_rockusb *f_rkusb = get_rkusb(); 408bf2b72beSEddie Cai int rx_remain = f_rkusb->dl_size - f_rkusb->dl_bytes; 409bf2b72beSEddie Cai unsigned int rem; 410bf2b72beSEddie Cai unsigned int maxpacket = ep->maxpacket; 411bf2b72beSEddie Cai 412bf2b72beSEddie Cai if (rx_remain <= 0) 413bf2b72beSEddie Cai return 0; 414bf2b72beSEddie Cai else if (rx_remain > EP_BUFFER_SIZE) 415bf2b72beSEddie Cai return EP_BUFFER_SIZE; 416bf2b72beSEddie Cai 417bf2b72beSEddie Cai rem = rx_remain % maxpacket; 418bf2b72beSEddie Cai if (rem > 0) 419bf2b72beSEddie Cai rx_remain = rx_remain + (maxpacket - rem); 420bf2b72beSEddie Cai 421bf2b72beSEddie Cai return rx_remain; 422bf2b72beSEddie Cai } 423bf2b72beSEddie Cai 424bf2b72beSEddie Cai /* usb_request complete call back to handle down load image */ 425bf2b72beSEddie Cai static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req) 426bf2b72beSEddie Cai { 427bf2b72beSEddie Cai struct f_rockusb *f_rkusb = get_rkusb(); 428bf2b72beSEddie Cai unsigned int transfer_size = 0; 429bf2b72beSEddie Cai const unsigned char *buffer = req->buf; 430bf2b72beSEddie Cai unsigned int buffer_size = req->actual; 431bf2b72beSEddie Cai 432bf2b72beSEddie Cai transfer_size = f_rkusb->dl_size - f_rkusb->dl_bytes; 433bf2b72beSEddie Cai if (!f_rkusb->desc) { 434bf2b72beSEddie Cai char *type = f_rkusb->dev_type; 435bf2b72beSEddie Cai int index = f_rkusb->dev_index; 436bf2b72beSEddie Cai 437bf2b72beSEddie Cai f_rkusb->desc = blk_get_dev(type, index); 438bf2b72beSEddie Cai if (!f_rkusb->desc || 439bf2b72beSEddie Cai f_rkusb->desc->type == DEV_TYPE_UNKNOWN) { 440bf2b72beSEddie Cai puts("invalid mmc device\n"); 441bf2b72beSEddie Cai rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL, 442bf2b72beSEddie Cai USB_BULK_CS_WRAP_LEN); 443bf2b72beSEddie Cai return; 444bf2b72beSEddie Cai } 445bf2b72beSEddie Cai } 446bf2b72beSEddie Cai 447bf2b72beSEddie Cai if (req->status != 0) { 448bf2b72beSEddie Cai printf("Bad status: %d\n", req->status); 449bf2b72beSEddie Cai rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL, 450bf2b72beSEddie Cai USB_BULK_CS_WRAP_LEN); 451bf2b72beSEddie Cai return; 452bf2b72beSEddie Cai } 453bf2b72beSEddie Cai 454bf2b72beSEddie Cai if (buffer_size < transfer_size) 455bf2b72beSEddie Cai transfer_size = buffer_size; 456bf2b72beSEddie Cai 457bf2b72beSEddie Cai memcpy((void *)f_rkusb->buf, buffer, transfer_size); 458bf2b72beSEddie Cai f_rkusb->dl_bytes += transfer_size; 459bf2b72beSEddie Cai int blks = 0, blkcnt = transfer_size / 512; 460bf2b72beSEddie Cai 461bf2b72beSEddie Cai debug("dl %x bytes, %x blks, write lba %x, dl_size:%x, dl_bytes:%x, ", 462bf2b72beSEddie Cai transfer_size, blkcnt, f_rkusb->lba, f_rkusb->dl_size, 463bf2b72beSEddie Cai f_rkusb->dl_bytes); 464bf2b72beSEddie Cai blks = blk_dwrite(f_rkusb->desc, f_rkusb->lba, blkcnt, f_rkusb->buf); 465bf2b72beSEddie Cai if (blks != blkcnt) { 466bf2b72beSEddie Cai printf("failed writing to device %s: %d\n", f_rkusb->dev_type, 467bf2b72beSEddie Cai f_rkusb->dev_index); 468bf2b72beSEddie Cai rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL, 469bf2b72beSEddie Cai USB_BULK_CS_WRAP_LEN); 470bf2b72beSEddie Cai return; 471bf2b72beSEddie Cai } 472bf2b72beSEddie Cai f_rkusb->lba += blkcnt; 473bf2b72beSEddie Cai 474bf2b72beSEddie Cai /* Check if transfer is done */ 475bf2b72beSEddie Cai if (f_rkusb->dl_bytes >= f_rkusb->dl_size) { 476bf2b72beSEddie Cai req->complete = rx_handler_command; 477bf2b72beSEddie Cai req->length = EP_BUFFER_SIZE; 478bf2b72beSEddie Cai f_rkusb->buf = f_rkusb->buf_head; 479bf2b72beSEddie Cai printf("transfer 0x%x bytes done\n", f_rkusb->dl_size); 480bf2b72beSEddie Cai f_rkusb->dl_size = 0; 481bf2b72beSEddie Cai rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD, 482bf2b72beSEddie Cai USB_BULK_CS_WRAP_LEN); 483bf2b72beSEddie Cai } else { 484bf2b72beSEddie Cai req->length = rx_bytes_expected(ep); 485bf2b72beSEddie Cai if (f_rkusb->buf == f_rkusb->buf_head) 486bf2b72beSEddie Cai f_rkusb->buf = f_rkusb->buf_head + EP_BUFFER_SIZE; 487bf2b72beSEddie Cai else 488bf2b72beSEddie Cai f_rkusb->buf = f_rkusb->buf_head; 489bf2b72beSEddie Cai 490bf2b72beSEddie Cai debug("remain %x bytes, %x sectors\n", req->length, 491bf2b72beSEddie Cai req->length / 512); 492bf2b72beSEddie Cai } 493bf2b72beSEddie Cai 494bf2b72beSEddie Cai req->actual = 0; 495bf2b72beSEddie Cai usb_ep_queue(ep, req, 0); 496bf2b72beSEddie Cai } 497bf2b72beSEddie Cai 498bf2b72beSEddie Cai static void cb_test_unit_ready(struct usb_ep *ep, struct usb_request *req) 499bf2b72beSEddie Cai { 500bf2b72beSEddie Cai ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, 501bf2b72beSEddie Cai sizeof(struct fsg_bulk_cb_wrap)); 502bf2b72beSEddie Cai 503bf2b72beSEddie Cai memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); 504bf2b72beSEddie Cai 505bf2b72beSEddie Cai rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, 506bf2b72beSEddie Cai CSW_GOOD, USB_BULK_CS_WRAP_LEN); 507bf2b72beSEddie Cai } 508bf2b72beSEddie Cai 509bf2b72beSEddie Cai static void cb_read_storage_id(struct usb_ep *ep, struct usb_request *req) 510bf2b72beSEddie Cai { 511bf2b72beSEddie Cai ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, 512bf2b72beSEddie Cai sizeof(struct fsg_bulk_cb_wrap)); 513cad66e32SAlberto Panizzo struct f_rockusb *f_rkusb = get_rkusb(); 514bf2b72beSEddie Cai char emmc_id[] = "EMMC "; 515bf2b72beSEddie Cai 516bf2b72beSEddie Cai printf("read storage id\n"); 517bf2b72beSEddie Cai memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); 518cad66e32SAlberto Panizzo 519cad66e32SAlberto Panizzo /* Prepare for sending subsequent CSW_GOOD */ 520cad66e32SAlberto Panizzo f_rkusb->tag = cbw->tag; 521cad66e32SAlberto Panizzo f_rkusb->in_req->complete = tx_handler_send_csw; 522cad66e32SAlberto Panizzo 523bf2b72beSEddie Cai rockusb_tx_write_str(emmc_id); 524bf2b72beSEddie Cai } 525bf2b72beSEddie Cai 526*e4b34a76SAlberto Panizzo int __weak rk_get_bootrom_chip_version(unsigned int *chip_info, int size) 527*e4b34a76SAlberto Panizzo { 528*e4b34a76SAlberto Panizzo return 0; 529*e4b34a76SAlberto Panizzo } 530*e4b34a76SAlberto Panizzo 531*e4b34a76SAlberto Panizzo static void cb_get_chip_version(struct usb_ep *ep, struct usb_request *req) 532*e4b34a76SAlberto Panizzo { 533*e4b34a76SAlberto Panizzo ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, 534*e4b34a76SAlberto Panizzo sizeof(struct fsg_bulk_cb_wrap)); 535*e4b34a76SAlberto Panizzo struct f_rockusb *f_rkusb = get_rkusb(); 536*e4b34a76SAlberto Panizzo unsigned int chip_info[4], i; 537*e4b34a76SAlberto Panizzo 538*e4b34a76SAlberto Panizzo memset(chip_info, 0, sizeof(chip_info)); 539*e4b34a76SAlberto Panizzo rk_get_bootrom_chip_version(chip_info, 4); 540*e4b34a76SAlberto Panizzo 541*e4b34a76SAlberto Panizzo /* 542*e4b34a76SAlberto Panizzo * Chip Version is a string saved in BOOTROM address space Little Endian 543*e4b34a76SAlberto Panizzo * 544*e4b34a76SAlberto Panizzo * Ex for rk3288: 0x33323041 0x32303134 0x30383133 0x56323030 545*e4b34a76SAlberto Panizzo * which brings: 320A20140813V200 546*e4b34a76SAlberto Panizzo * 547*e4b34a76SAlberto Panizzo * Note that memory version do invert MSB/LSB so printing the char 548*e4b34a76SAlberto Panizzo * buffer will show: A02341023180002V 549*e4b34a76SAlberto Panizzo */ 550*e4b34a76SAlberto Panizzo printf("read chip version: "); 551*e4b34a76SAlberto Panizzo for (i = 0; i < 4; i++) { 552*e4b34a76SAlberto Panizzo printf("%c%c%c%c", 553*e4b34a76SAlberto Panizzo (chip_info[i] >> 24) & 0xFF, 554*e4b34a76SAlberto Panizzo (chip_info[i] >> 16) & 0xFF, 555*e4b34a76SAlberto Panizzo (chip_info[i] >> 8) & 0xFF, 556*e4b34a76SAlberto Panizzo (chip_info[i] >> 0) & 0xFF); 557*e4b34a76SAlberto Panizzo } 558*e4b34a76SAlberto Panizzo printf("\n"); 559*e4b34a76SAlberto Panizzo memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); 560*e4b34a76SAlberto Panizzo 561*e4b34a76SAlberto Panizzo /* Prepare for sending subsequent CSW_GOOD */ 562*e4b34a76SAlberto Panizzo f_rkusb->tag = cbw->tag; 563*e4b34a76SAlberto Panizzo f_rkusb->in_req->complete = tx_handler_send_csw; 564*e4b34a76SAlberto Panizzo 565*e4b34a76SAlberto Panizzo rockusb_tx_write((char *)chip_info, sizeof(chip_info)); 566*e4b34a76SAlberto Panizzo } 567*e4b34a76SAlberto Panizzo 568bf2b72beSEddie Cai static void cb_write_lba(struct usb_ep *ep, struct usb_request *req) 569bf2b72beSEddie Cai { 570bf2b72beSEddie Cai ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, 571bf2b72beSEddie Cai sizeof(struct fsg_bulk_cb_wrap)); 572bf2b72beSEddie Cai struct f_rockusb *f_rkusb = get_rkusb(); 573bf2b72beSEddie Cai int sector_count; 574bf2b72beSEddie Cai 575bf2b72beSEddie Cai memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); 576bf2b72beSEddie Cai sector_count = (int)get_unaligned_be16(&cbw->CDB[7]); 577bf2b72beSEddie Cai f_rkusb->lba = get_unaligned_be32(&cbw->CDB[2]); 578bf2b72beSEddie Cai f_rkusb->dl_size = sector_count * 512; 579bf2b72beSEddie Cai f_rkusb->dl_bytes = 0; 580bf2b72beSEddie Cai f_rkusb->tag = cbw->tag; 581bf2b72beSEddie Cai debug("require write %x bytes, %x sectors to lba %x\n", 582bf2b72beSEddie Cai f_rkusb->dl_size, sector_count, f_rkusb->lba); 583bf2b72beSEddie Cai 584bf2b72beSEddie Cai if (f_rkusb->dl_size == 0) { 585bf2b72beSEddie Cai rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, 586bf2b72beSEddie Cai CSW_FAIL, USB_BULK_CS_WRAP_LEN); 587bf2b72beSEddie Cai } else { 588bf2b72beSEddie Cai req->complete = rx_handler_dl_image; 589bf2b72beSEddie Cai req->length = rx_bytes_expected(ep); 590bf2b72beSEddie Cai } 591bf2b72beSEddie Cai } 592bf2b72beSEddie Cai 593bf2b72beSEddie Cai void __weak rkusb_set_reboot_flag(int flag) 594bf2b72beSEddie Cai { 595bf2b72beSEddie Cai struct f_rockusb *f_rkusb = get_rkusb(); 596bf2b72beSEddie Cai 597bf2b72beSEddie Cai printf("rockkusb set reboot flag: %d\n", f_rkusb->reboot_flag); 598bf2b72beSEddie Cai } 599bf2b72beSEddie Cai 600bf2b72beSEddie Cai static void compl_do_reset(struct usb_ep *ep, struct usb_request *req) 601bf2b72beSEddie Cai { 602bf2b72beSEddie Cai struct f_rockusb *f_rkusb = get_rkusb(); 603bf2b72beSEddie Cai 604bf2b72beSEddie Cai rkusb_set_reboot_flag(f_rkusb->reboot_flag); 605bf2b72beSEddie Cai do_reset(NULL, 0, 0, NULL); 606bf2b72beSEddie Cai } 607bf2b72beSEddie Cai 608bf2b72beSEddie Cai static void cb_reboot(struct usb_ep *ep, struct usb_request *req) 609bf2b72beSEddie Cai { 610bf2b72beSEddie Cai ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, 611bf2b72beSEddie Cai sizeof(struct fsg_bulk_cb_wrap)); 612bf2b72beSEddie Cai struct f_rockusb *f_rkusb = get_rkusb(); 613bf2b72beSEddie Cai 614bf2b72beSEddie Cai memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); 615bf2b72beSEddie Cai f_rkusb->reboot_flag = cbw->CDB[1]; 616bf2b72beSEddie Cai rockusb_func->in_req->complete = compl_do_reset; 617bf2b72beSEddie Cai rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD, 618bf2b72beSEddie Cai USB_BULK_CS_WRAP_LEN); 619bf2b72beSEddie Cai } 620bf2b72beSEddie Cai 621bf2b72beSEddie Cai static void cb_not_support(struct usb_ep *ep, struct usb_request *req) 622bf2b72beSEddie Cai { 623bf2b72beSEddie Cai ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, 624bf2b72beSEddie Cai sizeof(struct fsg_bulk_cb_wrap)); 625bf2b72beSEddie Cai 626bf2b72beSEddie Cai memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); 627bf2b72beSEddie Cai printf("Rockusb command %x not support yet\n", cbw->CDB[0]); 628bf2b72beSEddie Cai rockusb_tx_write_csw(cbw->tag, 0, CSW_FAIL, USB_BULK_CS_WRAP_LEN); 629bf2b72beSEddie Cai } 630bf2b72beSEddie Cai 631bf2b72beSEddie Cai static const struct cmd_dispatch_info cmd_dispatch_info[] = { 632bf2b72beSEddie Cai { 633bf2b72beSEddie Cai .cmd = K_FW_TEST_UNIT_READY, 634bf2b72beSEddie Cai .cb = cb_test_unit_ready, 635bf2b72beSEddie Cai }, 636bf2b72beSEddie Cai { 637bf2b72beSEddie Cai .cmd = K_FW_READ_FLASH_ID, 638bf2b72beSEddie Cai .cb = cb_read_storage_id, 639bf2b72beSEddie Cai }, 640bf2b72beSEddie Cai { 641bf2b72beSEddie Cai .cmd = K_FW_SET_DEVICE_ID, 642bf2b72beSEddie Cai .cb = cb_not_support, 643bf2b72beSEddie Cai }, 644bf2b72beSEddie Cai { 645bf2b72beSEddie Cai .cmd = K_FW_TEST_BAD_BLOCK, 646bf2b72beSEddie Cai .cb = cb_not_support, 647bf2b72beSEddie Cai }, 648bf2b72beSEddie Cai { 649bf2b72beSEddie Cai .cmd = K_FW_READ_10, 650bf2b72beSEddie Cai .cb = cb_not_support, 651bf2b72beSEddie Cai }, 652bf2b72beSEddie Cai { 653bf2b72beSEddie Cai .cmd = K_FW_WRITE_10, 654bf2b72beSEddie Cai .cb = cb_not_support, 655bf2b72beSEddie Cai }, 656bf2b72beSEddie Cai { 657bf2b72beSEddie Cai .cmd = K_FW_ERASE_10, 658bf2b72beSEddie Cai .cb = cb_not_support, 659bf2b72beSEddie Cai }, 660bf2b72beSEddie Cai { 661bf2b72beSEddie Cai .cmd = K_FW_WRITE_SPARE, 662bf2b72beSEddie Cai .cb = cb_not_support, 663bf2b72beSEddie Cai }, 664bf2b72beSEddie Cai { 665bf2b72beSEddie Cai .cmd = K_FW_READ_SPARE, 666bf2b72beSEddie Cai .cb = cb_not_support, 667bf2b72beSEddie Cai }, 668bf2b72beSEddie Cai { 669bf2b72beSEddie Cai .cmd = K_FW_ERASE_10_FORCE, 670bf2b72beSEddie Cai .cb = cb_not_support, 671bf2b72beSEddie Cai }, 672bf2b72beSEddie Cai { 673bf2b72beSEddie Cai .cmd = K_FW_GET_VERSION, 674bf2b72beSEddie Cai .cb = cb_not_support, 675bf2b72beSEddie Cai }, 676bf2b72beSEddie Cai { 677bf2b72beSEddie Cai .cmd = K_FW_LBA_READ_10, 678bf2b72beSEddie Cai .cb = cb_not_support, 679bf2b72beSEddie Cai }, 680bf2b72beSEddie Cai { 681bf2b72beSEddie Cai .cmd = K_FW_LBA_WRITE_10, 682bf2b72beSEddie Cai .cb = cb_write_lba, 683bf2b72beSEddie Cai }, 684bf2b72beSEddie Cai { 685bf2b72beSEddie Cai .cmd = K_FW_ERASE_SYS_DISK, 686bf2b72beSEddie Cai .cb = cb_not_support, 687bf2b72beSEddie Cai }, 688bf2b72beSEddie Cai { 689bf2b72beSEddie Cai .cmd = K_FW_SDRAM_READ_10, 690bf2b72beSEddie Cai .cb = cb_not_support, 691bf2b72beSEddie Cai }, 692bf2b72beSEddie Cai { 693bf2b72beSEddie Cai .cmd = K_FW_SDRAM_WRITE_10, 694bf2b72beSEddie Cai .cb = cb_not_support, 695bf2b72beSEddie Cai }, 696bf2b72beSEddie Cai { 697bf2b72beSEddie Cai .cmd = K_FW_SDRAM_EXECUTE, 698bf2b72beSEddie Cai .cb = cb_not_support, 699bf2b72beSEddie Cai }, 700bf2b72beSEddie Cai { 701bf2b72beSEddie Cai .cmd = K_FW_READ_FLASH_INFO, 702bf2b72beSEddie Cai .cb = cb_not_support, 703bf2b72beSEddie Cai }, 704bf2b72beSEddie Cai { 705bf2b72beSEddie Cai .cmd = K_FW_GET_CHIP_VER, 706*e4b34a76SAlberto Panizzo .cb = cb_get_chip_version, 707bf2b72beSEddie Cai }, 708bf2b72beSEddie Cai { 709bf2b72beSEddie Cai .cmd = K_FW_LOW_FORMAT, 710bf2b72beSEddie Cai .cb = cb_not_support, 711bf2b72beSEddie Cai }, 712bf2b72beSEddie Cai { 713bf2b72beSEddie Cai .cmd = K_FW_SET_RESET_FLAG, 714bf2b72beSEddie Cai .cb = cb_not_support, 715bf2b72beSEddie Cai }, 716bf2b72beSEddie Cai { 717bf2b72beSEddie Cai .cmd = K_FW_SPI_READ_10, 718bf2b72beSEddie Cai .cb = cb_not_support, 719bf2b72beSEddie Cai }, 720bf2b72beSEddie Cai { 721bf2b72beSEddie Cai .cmd = K_FW_SPI_WRITE_10, 722bf2b72beSEddie Cai .cb = cb_not_support, 723bf2b72beSEddie Cai }, 724bf2b72beSEddie Cai { 725bf2b72beSEddie Cai .cmd = K_FW_SESSION, 726bf2b72beSEddie Cai .cb = cb_not_support, 727bf2b72beSEddie Cai }, 728bf2b72beSEddie Cai { 729bf2b72beSEddie Cai .cmd = K_FW_RESET, 730bf2b72beSEddie Cai .cb = cb_reboot, 731bf2b72beSEddie Cai }, 732bf2b72beSEddie Cai }; 733bf2b72beSEddie Cai 734bf2b72beSEddie Cai static void rx_handler_command(struct usb_ep *ep, struct usb_request *req) 735bf2b72beSEddie Cai { 736bf2b72beSEddie Cai void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL; 737bf2b72beSEddie Cai 738bf2b72beSEddie Cai ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw, 739bf2b72beSEddie Cai sizeof(struct fsg_bulk_cb_wrap)); 740bf2b72beSEddie Cai char *cmdbuf = req->buf; 741bf2b72beSEddie Cai int i; 742bf2b72beSEddie Cai 743bf2b72beSEddie Cai if (req->status || req->length == 0) 744bf2b72beSEddie Cai return; 745bf2b72beSEddie Cai 746bf2b72beSEddie Cai memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN); 747bf2b72beSEddie Cai #ifdef DEBUG 748bf2b72beSEddie Cai printcbw(req->buf); 749bf2b72beSEddie Cai #endif 750bf2b72beSEddie Cai 751bf2b72beSEddie Cai for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) { 752bf2b72beSEddie Cai if (cmd_dispatch_info[i].cmd == cbw->CDB[0]) { 753bf2b72beSEddie Cai func_cb = cmd_dispatch_info[i].cb; 754bf2b72beSEddie Cai break; 755bf2b72beSEddie Cai } 756bf2b72beSEddie Cai } 757bf2b72beSEddie Cai 758bf2b72beSEddie Cai if (!func_cb) { 759bf2b72beSEddie Cai printf("unknown command: %s\n", (char *)req->buf); 760bf2b72beSEddie Cai rockusb_tx_write_str("FAILunknown command"); 761bf2b72beSEddie Cai } else { 762bf2b72beSEddie Cai if (req->actual < req->length) { 763bf2b72beSEddie Cai u8 *buf = (u8 *)req->buf; 764bf2b72beSEddie Cai 765bf2b72beSEddie Cai buf[req->actual] = 0; 766bf2b72beSEddie Cai func_cb(ep, req); 767bf2b72beSEddie Cai } else { 768bf2b72beSEddie Cai puts("buffer overflow\n"); 769bf2b72beSEddie Cai rockusb_tx_write_str("FAILbuffer overflow"); 770bf2b72beSEddie Cai } 771bf2b72beSEddie Cai } 772bf2b72beSEddie Cai 773bf2b72beSEddie Cai *cmdbuf = '\0'; 774bf2b72beSEddie Cai req->actual = 0; 775bf2b72beSEddie Cai usb_ep_queue(ep, req, 0); 776bf2b72beSEddie Cai } 777