f_rockusb.c 24 KB

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