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