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