f_rockusb.c 24 KB

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