f_rockusb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2017
  4. *
  5. * Eddie Cai <eddie.cai.linux@gmail.com>
  6. */
  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. static inline struct f_rockusb *func_to_rockusb(struct usb_function *f)
  20. {
  21. return container_of(f, struct f_rockusb, usb_function);
  22. }
  23. static struct usb_endpoint_descriptor fs_ep_in = {
  24. .bLength = USB_DT_ENDPOINT_SIZE,
  25. .bDescriptorType = USB_DT_ENDPOINT,
  26. .bEndpointAddress = USB_DIR_IN,
  27. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  28. .wMaxPacketSize = cpu_to_le16(64),
  29. };
  30. static struct usb_endpoint_descriptor fs_ep_out = {
  31. .bLength = USB_DT_ENDPOINT_SIZE,
  32. .bDescriptorType = USB_DT_ENDPOINT,
  33. .bEndpointAddress = USB_DIR_OUT,
  34. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  35. .wMaxPacketSize = cpu_to_le16(64),
  36. };
  37. static struct usb_endpoint_descriptor hs_ep_in = {
  38. .bLength = USB_DT_ENDPOINT_SIZE,
  39. .bDescriptorType = USB_DT_ENDPOINT,
  40. .bEndpointAddress = USB_DIR_IN,
  41. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  42. .wMaxPacketSize = cpu_to_le16(512),
  43. };
  44. static struct usb_endpoint_descriptor hs_ep_out = {
  45. .bLength = USB_DT_ENDPOINT_SIZE,
  46. .bDescriptorType = USB_DT_ENDPOINT,
  47. .bEndpointAddress = USB_DIR_OUT,
  48. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  49. .wMaxPacketSize = cpu_to_le16(512),
  50. };
  51. static struct usb_interface_descriptor interface_desc = {
  52. .bLength = USB_DT_INTERFACE_SIZE,
  53. .bDescriptorType = USB_DT_INTERFACE,
  54. .bInterfaceNumber = 0x00,
  55. .bAlternateSetting = 0x00,
  56. .bNumEndpoints = 0x02,
  57. .bInterfaceClass = ROCKUSB_INTERFACE_CLASS,
  58. .bInterfaceSubClass = ROCKUSB_INTERFACE_SUB_CLASS,
  59. .bInterfaceProtocol = ROCKUSB_INTERFACE_PROTOCOL,
  60. };
  61. static struct usb_descriptor_header *rkusb_fs_function[] = {
  62. (struct usb_descriptor_header *)&interface_desc,
  63. (struct usb_descriptor_header *)&fs_ep_in,
  64. (struct usb_descriptor_header *)&fs_ep_out,
  65. };
  66. static struct usb_descriptor_header *rkusb_hs_function[] = {
  67. (struct usb_descriptor_header *)&interface_desc,
  68. (struct usb_descriptor_header *)&hs_ep_in,
  69. (struct usb_descriptor_header *)&hs_ep_out,
  70. NULL,
  71. };
  72. static const char rkusb_name[] = "Rockchip Rockusb";
  73. static struct usb_string rkusb_string_defs[] = {
  74. [0].s = rkusb_name,
  75. { } /* end of list */
  76. };
  77. static struct usb_gadget_strings stringtab_rkusb = {
  78. .language = 0x0409, /* en-us */
  79. .strings = rkusb_string_defs,
  80. };
  81. static struct usb_gadget_strings *rkusb_strings[] = {
  82. &stringtab_rkusb,
  83. NULL,
  84. };
  85. static struct f_rockusb *rockusb_func;
  86. static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
  87. static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size);
  88. struct f_rockusb *get_rkusb(void)
  89. {
  90. struct f_rockusb *f_rkusb = rockusb_func;
  91. if (!f_rkusb) {
  92. f_rkusb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_rkusb));
  93. if (!f_rkusb)
  94. return 0;
  95. rockusb_func = f_rkusb;
  96. memset(f_rkusb, 0, sizeof(*f_rkusb));
  97. }
  98. if (!f_rkusb->buf_head) {
  99. f_rkusb->buf_head = memalign(CONFIG_SYS_CACHELINE_SIZE,
  100. RKUSB_BUF_SIZE);
  101. if (!f_rkusb->buf_head)
  102. return 0;
  103. f_rkusb->buf = f_rkusb->buf_head;
  104. memset(f_rkusb->buf_head, 0, RKUSB_BUF_SIZE);
  105. }
  106. return f_rkusb;
  107. }
  108. static struct usb_endpoint_descriptor *rkusb_ep_desc(
  109. struct usb_gadget *g,
  110. struct usb_endpoint_descriptor *fs,
  111. struct usb_endpoint_descriptor *hs)
  112. {
  113. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  114. return hs;
  115. return fs;
  116. }
  117. static void rockusb_complete(struct usb_ep *ep, struct usb_request *req)
  118. {
  119. int status = req->status;
  120. if (!status)
  121. return;
  122. debug("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
  123. }
  124. /* config the rockusb device*/
  125. static int rockusb_bind(struct usb_configuration *c, struct usb_function *f)
  126. {
  127. int id;
  128. struct usb_gadget *gadget = c->cdev->gadget;
  129. struct f_rockusb *f_rkusb = func_to_rockusb(f);
  130. const char *s;
  131. id = usb_interface_id(c, f);
  132. if (id < 0)
  133. return id;
  134. interface_desc.bInterfaceNumber = id;
  135. id = usb_string_id(c->cdev);
  136. if (id < 0)
  137. return id;
  138. rkusb_string_defs[0].id = id;
  139. interface_desc.iInterface = id;
  140. f_rkusb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
  141. if (!f_rkusb->in_ep)
  142. return -ENODEV;
  143. f_rkusb->in_ep->driver_data = c->cdev;
  144. f_rkusb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
  145. if (!f_rkusb->out_ep)
  146. return -ENODEV;
  147. f_rkusb->out_ep->driver_data = c->cdev;
  148. f->descriptors = rkusb_fs_function;
  149. if (gadget_is_dualspeed(gadget)) {
  150. hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
  151. hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
  152. f->hs_descriptors = rkusb_hs_function;
  153. }
  154. s = env_get("serial#");
  155. if (s)
  156. g_dnl_set_serialnumber((char *)s);
  157. return 0;
  158. }
  159. static void rockusb_unbind(struct usb_configuration *c, struct usb_function *f)
  160. {
  161. /* clear the configuration*/
  162. memset(rockusb_func, 0, sizeof(*rockusb_func));
  163. }
  164. static void rockusb_disable(struct usb_function *f)
  165. {
  166. struct f_rockusb *f_rkusb = func_to_rockusb(f);
  167. usb_ep_disable(f_rkusb->out_ep);
  168. usb_ep_disable(f_rkusb->in_ep);
  169. if (f_rkusb->out_req) {
  170. free(f_rkusb->out_req->buf);
  171. usb_ep_free_request(f_rkusb->out_ep, f_rkusb->out_req);
  172. f_rkusb->out_req = NULL;
  173. }
  174. if (f_rkusb->in_req) {
  175. free(f_rkusb->in_req->buf);
  176. usb_ep_free_request(f_rkusb->in_ep, f_rkusb->in_req);
  177. f_rkusb->in_req = NULL;
  178. }
  179. if (f_rkusb->buf_head) {
  180. free(f_rkusb->buf_head);
  181. f_rkusb->buf_head = NULL;
  182. f_rkusb->buf = NULL;
  183. }
  184. }
  185. static struct usb_request *rockusb_start_ep(struct usb_ep *ep)
  186. {
  187. struct usb_request *req;
  188. req = usb_ep_alloc_request(ep, 0);
  189. if (!req)
  190. return NULL;
  191. req->length = EP_BUFFER_SIZE;
  192. req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
  193. if (!req->buf) {
  194. usb_ep_free_request(ep, req);
  195. return NULL;
  196. }
  197. memset(req->buf, 0, req->length);
  198. return req;
  199. }
  200. static int rockusb_set_alt(struct usb_function *f, unsigned int interface,
  201. unsigned int alt)
  202. {
  203. int ret;
  204. struct usb_composite_dev *cdev = f->config->cdev;
  205. struct usb_gadget *gadget = cdev->gadget;
  206. struct f_rockusb *f_rkusb = func_to_rockusb(f);
  207. const struct usb_endpoint_descriptor *d;
  208. debug("%s: func: %s intf: %d alt: %d\n",
  209. __func__, f->name, interface, alt);
  210. d = rkusb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
  211. ret = usb_ep_enable(f_rkusb->out_ep, d);
  212. if (ret) {
  213. printf("failed to enable out ep\n");
  214. return ret;
  215. }
  216. f_rkusb->out_req = rockusb_start_ep(f_rkusb->out_ep);
  217. if (!f_rkusb->out_req) {
  218. printf("failed to alloc out req\n");
  219. ret = -EINVAL;
  220. goto err;
  221. }
  222. f_rkusb->out_req->complete = rx_handler_command;
  223. d = rkusb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
  224. ret = usb_ep_enable(f_rkusb->in_ep, d);
  225. if (ret) {
  226. printf("failed to enable in ep\n");
  227. goto err;
  228. }
  229. f_rkusb->in_req = rockusb_start_ep(f_rkusb->in_ep);
  230. if (!f_rkusb->in_req) {
  231. printf("failed alloc req in\n");
  232. ret = -EINVAL;
  233. goto err;
  234. }
  235. f_rkusb->in_req->complete = rockusb_complete;
  236. ret = usb_ep_queue(f_rkusb->out_ep, f_rkusb->out_req, 0);
  237. if (ret)
  238. goto err;
  239. return 0;
  240. err:
  241. rockusb_disable(f);
  242. return ret;
  243. }
  244. static int rockusb_add(struct usb_configuration *c)
  245. {
  246. struct f_rockusb *f_rkusb = get_rkusb();
  247. int status;
  248. debug("%s: cdev: 0x%p\n", __func__, c->cdev);
  249. f_rkusb->usb_function.name = "f_rockusb";
  250. f_rkusb->usb_function.bind = rockusb_bind;
  251. f_rkusb->usb_function.unbind = rockusb_unbind;
  252. f_rkusb->usb_function.set_alt = rockusb_set_alt;
  253. f_rkusb->usb_function.disable = rockusb_disable;
  254. f_rkusb->usb_function.strings = rkusb_strings;
  255. status = usb_add_function(c, &f_rkusb->usb_function);
  256. if (status) {
  257. free(f_rkusb);
  258. rockusb_func = f_rkusb;
  259. }
  260. return status;
  261. }
  262. void rockusb_dev_init(char *dev_type, int dev_index)
  263. {
  264. struct f_rockusb *f_rkusb = get_rkusb();
  265. f_rkusb->dev_type = dev_type;
  266. f_rkusb->dev_index = dev_index;
  267. }
  268. DECLARE_GADGET_BIND_CALLBACK(usb_dnl_rockusb, rockusb_add);
  269. static int rockusb_tx_write(const char *buffer, unsigned int buffer_size)
  270. {
  271. struct usb_request *in_req = rockusb_func->in_req;
  272. int ret;
  273. memcpy(in_req->buf, buffer, buffer_size);
  274. in_req->length = buffer_size;
  275. usb_ep_dequeue(rockusb_func->in_ep, in_req);
  276. ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0);
  277. if (ret)
  278. printf("Error %d on queue\n", ret);
  279. return 0;
  280. }
  281. static int rockusb_tx_write_str(const char *buffer)
  282. {
  283. return rockusb_tx_write(buffer, strlen(buffer));
  284. }
  285. #ifdef DEBUG
  286. static void printcbw(char *buf)
  287. {
  288. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  289. sizeof(struct fsg_bulk_cb_wrap));
  290. memcpy((char *)cbw, buf, USB_BULK_CB_WRAP_LEN);
  291. debug("cbw: signature:%x\n", cbw->signature);
  292. debug("cbw: tag=%x\n", cbw->tag);
  293. debug("cbw: data_transfer_length=%d\n", cbw->data_transfer_length);
  294. debug("cbw: flags=%x\n", cbw->flags);
  295. debug("cbw: lun=%d\n", cbw->lun);
  296. debug("cbw: length=%d\n", cbw->length);
  297. debug("cbw: ucOperCode=%x\n", cbw->CDB[0]);
  298. debug("cbw: ucReserved=%x\n", cbw->CDB[1]);
  299. debug("cbw: dwAddress:%x %x %x %x\n", cbw->CDB[5], cbw->CDB[4],
  300. cbw->CDB[3], cbw->CDB[2]);
  301. debug("cbw: ucReserved2=%x\n", cbw->CDB[6]);
  302. debug("cbw: uslength:%x %x\n", cbw->CDB[8], cbw->CDB[7]);
  303. }
  304. static void printcsw(char *buf)
  305. {
  306. ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
  307. sizeof(struct bulk_cs_wrap));
  308. memcpy((char *)csw, buf, USB_BULK_CS_WRAP_LEN);
  309. debug("csw: signature:%x\n", csw->signature);
  310. debug("csw: tag:%x\n", csw->tag);
  311. debug("csw: residue:%x\n", csw->residue);
  312. debug("csw: status:%x\n", csw->status);
  313. }
  314. #endif
  315. static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size)
  316. {
  317. ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
  318. sizeof(struct bulk_cs_wrap));
  319. csw->signature = cpu_to_le32(USB_BULK_CS_SIG);
  320. csw->tag = tag;
  321. csw->residue = cpu_to_be32(residue);
  322. csw->status = status;
  323. #ifdef DEBUG
  324. printcsw((char *)&csw);
  325. #endif
  326. return rockusb_tx_write((char *)csw, size);
  327. }
  328. static unsigned int rx_bytes_expected(struct usb_ep *ep)
  329. {
  330. struct f_rockusb *f_rkusb = get_rkusb();
  331. int rx_remain = f_rkusb->dl_size - f_rkusb->dl_bytes;
  332. unsigned int rem;
  333. unsigned int maxpacket = ep->maxpacket;
  334. if (rx_remain <= 0)
  335. return 0;
  336. else if (rx_remain > EP_BUFFER_SIZE)
  337. return EP_BUFFER_SIZE;
  338. rem = rx_remain % maxpacket;
  339. if (rem > 0)
  340. rx_remain = rx_remain + (maxpacket - rem);
  341. return rx_remain;
  342. }
  343. /* usb_request complete call back to handle down load image */
  344. static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
  345. {
  346. struct f_rockusb *f_rkusb = get_rkusb();
  347. unsigned int transfer_size = 0;
  348. const unsigned char *buffer = req->buf;
  349. unsigned int buffer_size = req->actual;
  350. transfer_size = f_rkusb->dl_size - f_rkusb->dl_bytes;
  351. if (!f_rkusb->desc) {
  352. char *type = f_rkusb->dev_type;
  353. int index = f_rkusb->dev_index;
  354. f_rkusb->desc = blk_get_dev(type, index);
  355. if (!f_rkusb->desc ||
  356. f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
  357. puts("invalid mmc device\n");
  358. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
  359. USB_BULK_CS_WRAP_LEN);
  360. return;
  361. }
  362. }
  363. if (req->status != 0) {
  364. printf("Bad status: %d\n", req->status);
  365. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
  366. USB_BULK_CS_WRAP_LEN);
  367. return;
  368. }
  369. if (buffer_size < transfer_size)
  370. transfer_size = buffer_size;
  371. memcpy((void *)f_rkusb->buf, buffer, transfer_size);
  372. f_rkusb->dl_bytes += transfer_size;
  373. int blks = 0, blkcnt = transfer_size / 512;
  374. debug("dl %x bytes, %x blks, write lba %x, dl_size:%x, dl_bytes:%x, ",
  375. transfer_size, blkcnt, f_rkusb->lba, f_rkusb->dl_size,
  376. f_rkusb->dl_bytes);
  377. blks = blk_dwrite(f_rkusb->desc, f_rkusb->lba, blkcnt, f_rkusb->buf);
  378. if (blks != blkcnt) {
  379. printf("failed writing to device %s: %d\n", f_rkusb->dev_type,
  380. f_rkusb->dev_index);
  381. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
  382. USB_BULK_CS_WRAP_LEN);
  383. return;
  384. }
  385. f_rkusb->lba += blkcnt;
  386. /* Check if transfer is done */
  387. if (f_rkusb->dl_bytes >= f_rkusb->dl_size) {
  388. req->complete = rx_handler_command;
  389. req->length = EP_BUFFER_SIZE;
  390. f_rkusb->buf = f_rkusb->buf_head;
  391. printf("transfer 0x%x bytes done\n", f_rkusb->dl_size);
  392. f_rkusb->dl_size = 0;
  393. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD,
  394. USB_BULK_CS_WRAP_LEN);
  395. } else {
  396. req->length = rx_bytes_expected(ep);
  397. if (f_rkusb->buf == f_rkusb->buf_head)
  398. f_rkusb->buf = f_rkusb->buf_head + EP_BUFFER_SIZE;
  399. else
  400. f_rkusb->buf = f_rkusb->buf_head;
  401. debug("remain %x bytes, %x sectors\n", req->length,
  402. req->length / 512);
  403. }
  404. req->actual = 0;
  405. usb_ep_queue(ep, req, 0);
  406. }
  407. static void cb_test_unit_ready(struct usb_ep *ep, struct usb_request *req)
  408. {
  409. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  410. sizeof(struct fsg_bulk_cb_wrap));
  411. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  412. rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
  413. CSW_GOOD, USB_BULK_CS_WRAP_LEN);
  414. }
  415. static void cb_read_storage_id(struct usb_ep *ep, struct usb_request *req)
  416. {
  417. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  418. sizeof(struct fsg_bulk_cb_wrap));
  419. char emmc_id[] = "EMMC ";
  420. printf("read storage id\n");
  421. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  422. rockusb_tx_write_str(emmc_id);
  423. rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
  424. USB_BULK_CS_WRAP_LEN);
  425. }
  426. static void cb_write_lba(struct usb_ep *ep, struct usb_request *req)
  427. {
  428. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  429. sizeof(struct fsg_bulk_cb_wrap));
  430. struct f_rockusb *f_rkusb = get_rkusb();
  431. int sector_count;
  432. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  433. sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
  434. f_rkusb->lba = get_unaligned_be32(&cbw->CDB[2]);
  435. f_rkusb->dl_size = sector_count * 512;
  436. f_rkusb->dl_bytes = 0;
  437. f_rkusb->tag = cbw->tag;
  438. debug("require write %x bytes, %x sectors to lba %x\n",
  439. f_rkusb->dl_size, sector_count, f_rkusb->lba);
  440. if (f_rkusb->dl_size == 0) {
  441. rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
  442. CSW_FAIL, USB_BULK_CS_WRAP_LEN);
  443. } else {
  444. req->complete = rx_handler_dl_image;
  445. req->length = rx_bytes_expected(ep);
  446. }
  447. }
  448. void __weak rkusb_set_reboot_flag(int flag)
  449. {
  450. struct f_rockusb *f_rkusb = get_rkusb();
  451. printf("rockkusb set reboot flag: %d\n", f_rkusb->reboot_flag);
  452. }
  453. static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
  454. {
  455. struct f_rockusb *f_rkusb = get_rkusb();
  456. rkusb_set_reboot_flag(f_rkusb->reboot_flag);
  457. do_reset(NULL, 0, 0, NULL);
  458. }
  459. static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
  460. {
  461. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  462. sizeof(struct fsg_bulk_cb_wrap));
  463. struct f_rockusb *f_rkusb = get_rkusb();
  464. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  465. f_rkusb->reboot_flag = cbw->CDB[1];
  466. rockusb_func->in_req->complete = compl_do_reset;
  467. rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
  468. USB_BULK_CS_WRAP_LEN);
  469. }
  470. static void cb_not_support(struct usb_ep *ep, struct usb_request *req)
  471. {
  472. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  473. sizeof(struct fsg_bulk_cb_wrap));
  474. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  475. printf("Rockusb command %x not support yet\n", cbw->CDB[0]);
  476. rockusb_tx_write_csw(cbw->tag, 0, CSW_FAIL, USB_BULK_CS_WRAP_LEN);
  477. }
  478. static const struct cmd_dispatch_info cmd_dispatch_info[] = {
  479. {
  480. .cmd = K_FW_TEST_UNIT_READY,
  481. .cb = cb_test_unit_ready,
  482. },
  483. {
  484. .cmd = K_FW_READ_FLASH_ID,
  485. .cb = cb_read_storage_id,
  486. },
  487. {
  488. .cmd = K_FW_SET_DEVICE_ID,
  489. .cb = cb_not_support,
  490. },
  491. {
  492. .cmd = K_FW_TEST_BAD_BLOCK,
  493. .cb = cb_not_support,
  494. },
  495. {
  496. .cmd = K_FW_READ_10,
  497. .cb = cb_not_support,
  498. },
  499. {
  500. .cmd = K_FW_WRITE_10,
  501. .cb = cb_not_support,
  502. },
  503. {
  504. .cmd = K_FW_ERASE_10,
  505. .cb = cb_not_support,
  506. },
  507. {
  508. .cmd = K_FW_WRITE_SPARE,
  509. .cb = cb_not_support,
  510. },
  511. {
  512. .cmd = K_FW_READ_SPARE,
  513. .cb = cb_not_support,
  514. },
  515. {
  516. .cmd = K_FW_ERASE_10_FORCE,
  517. .cb = cb_not_support,
  518. },
  519. {
  520. .cmd = K_FW_GET_VERSION,
  521. .cb = cb_not_support,
  522. },
  523. {
  524. .cmd = K_FW_LBA_READ_10,
  525. .cb = cb_not_support,
  526. },
  527. {
  528. .cmd = K_FW_LBA_WRITE_10,
  529. .cb = cb_write_lba,
  530. },
  531. {
  532. .cmd = K_FW_ERASE_SYS_DISK,
  533. .cb = cb_not_support,
  534. },
  535. {
  536. .cmd = K_FW_SDRAM_READ_10,
  537. .cb = cb_not_support,
  538. },
  539. {
  540. .cmd = K_FW_SDRAM_WRITE_10,
  541. .cb = cb_not_support,
  542. },
  543. {
  544. .cmd = K_FW_SDRAM_EXECUTE,
  545. .cb = cb_not_support,
  546. },
  547. {
  548. .cmd = K_FW_READ_FLASH_INFO,
  549. .cb = cb_not_support,
  550. },
  551. {
  552. .cmd = K_FW_GET_CHIP_VER,
  553. .cb = cb_not_support,
  554. },
  555. {
  556. .cmd = K_FW_LOW_FORMAT,
  557. .cb = cb_not_support,
  558. },
  559. {
  560. .cmd = K_FW_SET_RESET_FLAG,
  561. .cb = cb_not_support,
  562. },
  563. {
  564. .cmd = K_FW_SPI_READ_10,
  565. .cb = cb_not_support,
  566. },
  567. {
  568. .cmd = K_FW_SPI_WRITE_10,
  569. .cb = cb_not_support,
  570. },
  571. {
  572. .cmd = K_FW_SESSION,
  573. .cb = cb_not_support,
  574. },
  575. {
  576. .cmd = K_FW_RESET,
  577. .cb = cb_reboot,
  578. },
  579. };
  580. static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
  581. {
  582. void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
  583. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  584. sizeof(struct fsg_bulk_cb_wrap));
  585. char *cmdbuf = req->buf;
  586. int i;
  587. if (req->status || req->length == 0)
  588. return;
  589. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  590. #ifdef DEBUG
  591. printcbw(req->buf);
  592. #endif
  593. for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
  594. if (cmd_dispatch_info[i].cmd == cbw->CDB[0]) {
  595. func_cb = cmd_dispatch_info[i].cb;
  596. break;
  597. }
  598. }
  599. if (!func_cb) {
  600. printf("unknown command: %s\n", (char *)req->buf);
  601. rockusb_tx_write_str("FAILunknown command");
  602. } else {
  603. if (req->actual < req->length) {
  604. u8 *buf = (u8 *)req->buf;
  605. buf[req->actual] = 0;
  606. func_cb(ep, req);
  607. } else {
  608. puts("buffer overflow\n");
  609. rockusb_tx_write_str("FAILbuffer overflow");
  610. }
  611. }
  612. *cmdbuf = '\0';
  613. req->actual = 0;
  614. usb_ep_queue(ep, req, 0);
  615. }