f_rockusb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. debug("Transferring 0x%x bytes\n", buffer_size);
  276. usb_ep_dequeue(rockusb_func->in_ep, in_req);
  277. ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0);
  278. if (ret)
  279. printf("Error %d on queue\n", ret);
  280. return 0;
  281. }
  282. static int rockusb_tx_write_str(const char *buffer)
  283. {
  284. return rockusb_tx_write(buffer, strlen(buffer));
  285. }
  286. #ifdef DEBUG
  287. static void printcbw(char *buf)
  288. {
  289. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  290. sizeof(struct fsg_bulk_cb_wrap));
  291. memcpy((char *)cbw, buf, USB_BULK_CB_WRAP_LEN);
  292. debug("cbw: signature:%x\n", cbw->signature);
  293. debug("cbw: tag=%x\n", cbw->tag);
  294. debug("cbw: data_transfer_length=%d\n", cbw->data_transfer_length);
  295. debug("cbw: flags=%x\n", cbw->flags);
  296. debug("cbw: lun=%d\n", cbw->lun);
  297. debug("cbw: length=%d\n", cbw->length);
  298. debug("cbw: ucOperCode=%x\n", cbw->CDB[0]);
  299. debug("cbw: ucReserved=%x\n", cbw->CDB[1]);
  300. debug("cbw: dwAddress:%x %x %x %x\n", cbw->CDB[5], cbw->CDB[4],
  301. cbw->CDB[3], cbw->CDB[2]);
  302. debug("cbw: ucReserved2=%x\n", cbw->CDB[6]);
  303. debug("cbw: uslength:%x %x\n", cbw->CDB[8], cbw->CDB[7]);
  304. }
  305. static void printcsw(char *buf)
  306. {
  307. ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
  308. sizeof(struct bulk_cs_wrap));
  309. memcpy((char *)csw, buf, USB_BULK_CS_WRAP_LEN);
  310. debug("csw: signature:%x\n", csw->signature);
  311. debug("csw: tag:%x\n", csw->tag);
  312. debug("csw: residue:%x\n", csw->residue);
  313. debug("csw: status:%x\n", csw->status);
  314. }
  315. #endif
  316. static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size)
  317. {
  318. ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
  319. sizeof(struct bulk_cs_wrap));
  320. csw->signature = cpu_to_le32(USB_BULK_CS_SIG);
  321. csw->tag = tag;
  322. csw->residue = cpu_to_be32(residue);
  323. csw->status = status;
  324. #ifdef DEBUG
  325. printcsw((char *)&csw);
  326. #endif
  327. return rockusb_tx_write((char *)csw, size);
  328. }
  329. static void tx_handler_send_csw(struct usb_ep *ep, struct usb_request *req)
  330. {
  331. struct f_rockusb *f_rkusb = get_rkusb();
  332. int status = req->status;
  333. if (status)
  334. debug("status: %d ep '%s' trans: %d\n",
  335. status, ep->name, req->actual);
  336. /* Return back to default in_req complete function after sending CSW */
  337. req->complete = rockusb_complete;
  338. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD, USB_BULK_CS_WRAP_LEN);
  339. }
  340. static unsigned int rx_bytes_expected(struct usb_ep *ep)
  341. {
  342. struct f_rockusb *f_rkusb = get_rkusb();
  343. int rx_remain = f_rkusb->dl_size - f_rkusb->dl_bytes;
  344. unsigned int rem;
  345. unsigned int maxpacket = ep->maxpacket;
  346. if (rx_remain <= 0)
  347. return 0;
  348. else if (rx_remain > EP_BUFFER_SIZE)
  349. return EP_BUFFER_SIZE;
  350. rem = rx_remain % maxpacket;
  351. if (rem > 0)
  352. rx_remain = rx_remain + (maxpacket - rem);
  353. return rx_remain;
  354. }
  355. /* usb_request complete call back to handle upload image */
  356. static void tx_handler_ul_image(struct usb_ep *ep, struct usb_request *req)
  357. {
  358. ALLOC_CACHE_ALIGN_BUFFER(char, rbuffer, RKBLOCK_BUF_SIZE);
  359. struct f_rockusb *f_rkusb = get_rkusb();
  360. struct usb_request *in_req = rockusb_func->in_req;
  361. int ret;
  362. /* Print error status of previous transfer */
  363. if (req->status)
  364. debug("status: %d ep '%s' trans: %d len %d\n", req->status,
  365. ep->name, req->actual, req->length);
  366. /* On transfer complete reset in_req and feedback host with CSW_GOOD */
  367. if (f_rkusb->ul_bytes >= f_rkusb->ul_size) {
  368. in_req->length = 0;
  369. in_req->complete = rockusb_complete;
  370. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD,
  371. USB_BULK_CS_WRAP_LEN);
  372. return;
  373. }
  374. /* Proceed with current chunk */
  375. unsigned int transfer_size = f_rkusb->ul_size - f_rkusb->ul_bytes;
  376. if (transfer_size > RKBLOCK_BUF_SIZE)
  377. transfer_size = RKBLOCK_BUF_SIZE;
  378. /* Read at least one block */
  379. unsigned int blkcount = (transfer_size + f_rkusb->desc->blksz - 1) /
  380. f_rkusb->desc->blksz;
  381. debug("ul %x bytes, %x blks, read lba %x, ul_size:%x, ul_bytes:%x, ",
  382. transfer_size, blkcount, f_rkusb->lba,
  383. f_rkusb->ul_size, f_rkusb->ul_bytes);
  384. int blks = blk_dread(f_rkusb->desc, f_rkusb->lba, blkcount, rbuffer);
  385. if (blks != blkcount) {
  386. printf("failed reading from device %s: %d\n",
  387. f_rkusb->dev_type, f_rkusb->dev_index);
  388. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
  389. USB_BULK_CS_WRAP_LEN);
  390. return;
  391. }
  392. f_rkusb->lba += blkcount;
  393. f_rkusb->ul_bytes += transfer_size;
  394. /* Proceed with USB request */
  395. memcpy(in_req->buf, rbuffer, transfer_size);
  396. in_req->length = transfer_size;
  397. in_req->complete = tx_handler_ul_image;
  398. printf("Uploading 0x%x bytes\n", transfer_size);
  399. usb_ep_dequeue(rockusb_func->in_ep, in_req);
  400. ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0);
  401. if (ret)
  402. printf("Error %d on queue\n", ret);
  403. }
  404. /* usb_request complete call back to handle down load image */
  405. static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
  406. {
  407. struct f_rockusb *f_rkusb = get_rkusb();
  408. unsigned int transfer_size = 0;
  409. const unsigned char *buffer = req->buf;
  410. unsigned int buffer_size = req->actual;
  411. transfer_size = f_rkusb->dl_size - f_rkusb->dl_bytes;
  412. if (!f_rkusb->desc) {
  413. char *type = f_rkusb->dev_type;
  414. int index = f_rkusb->dev_index;
  415. f_rkusb->desc = blk_get_dev(type, index);
  416. if (!f_rkusb->desc ||
  417. f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
  418. puts("invalid mmc device\n");
  419. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
  420. USB_BULK_CS_WRAP_LEN);
  421. return;
  422. }
  423. }
  424. if (req->status != 0) {
  425. printf("Bad status: %d\n", req->status);
  426. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
  427. USB_BULK_CS_WRAP_LEN);
  428. return;
  429. }
  430. if (buffer_size < transfer_size)
  431. transfer_size = buffer_size;
  432. memcpy((void *)f_rkusb->buf, buffer, transfer_size);
  433. f_rkusb->dl_bytes += transfer_size;
  434. int blks = 0, blkcnt = transfer_size / 512;
  435. debug("dl %x bytes, %x blks, write lba %x, dl_size:%x, dl_bytes:%x, ",
  436. transfer_size, blkcnt, f_rkusb->lba, f_rkusb->dl_size,
  437. f_rkusb->dl_bytes);
  438. blks = blk_dwrite(f_rkusb->desc, f_rkusb->lba, blkcnt, f_rkusb->buf);
  439. if (blks != blkcnt) {
  440. printf("failed writing to device %s: %d\n", f_rkusb->dev_type,
  441. f_rkusb->dev_index);
  442. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
  443. USB_BULK_CS_WRAP_LEN);
  444. return;
  445. }
  446. f_rkusb->lba += blkcnt;
  447. /* Check if transfer is done */
  448. if (f_rkusb->dl_bytes >= f_rkusb->dl_size) {
  449. req->complete = rx_handler_command;
  450. req->length = EP_BUFFER_SIZE;
  451. f_rkusb->buf = f_rkusb->buf_head;
  452. printf("transfer 0x%x bytes done\n", f_rkusb->dl_size);
  453. f_rkusb->dl_size = 0;
  454. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD,
  455. USB_BULK_CS_WRAP_LEN);
  456. } else {
  457. req->length = rx_bytes_expected(ep);
  458. if (f_rkusb->buf == f_rkusb->buf_head)
  459. f_rkusb->buf = f_rkusb->buf_head + EP_BUFFER_SIZE;
  460. else
  461. f_rkusb->buf = f_rkusb->buf_head;
  462. debug("remain %x bytes, %x sectors\n", req->length,
  463. req->length / 512);
  464. }
  465. req->actual = 0;
  466. usb_ep_queue(ep, req, 0);
  467. }
  468. static void cb_test_unit_ready(struct usb_ep *ep, struct usb_request *req)
  469. {
  470. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  471. sizeof(struct fsg_bulk_cb_wrap));
  472. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  473. rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
  474. CSW_GOOD, USB_BULK_CS_WRAP_LEN);
  475. }
  476. static void cb_read_storage_id(struct usb_ep *ep, struct usb_request *req)
  477. {
  478. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  479. sizeof(struct fsg_bulk_cb_wrap));
  480. struct f_rockusb *f_rkusb = get_rkusb();
  481. char emmc_id[] = "EMMC ";
  482. printf("read storage id\n");
  483. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  484. /* Prepare for sending subsequent CSW_GOOD */
  485. f_rkusb->tag = cbw->tag;
  486. f_rkusb->in_req->complete = tx_handler_send_csw;
  487. rockusb_tx_write_str(emmc_id);
  488. }
  489. int __weak rk_get_bootrom_chip_version(unsigned int *chip_info, int size)
  490. {
  491. return 0;
  492. }
  493. static void cb_get_chip_version(struct usb_ep *ep, struct usb_request *req)
  494. {
  495. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  496. sizeof(struct fsg_bulk_cb_wrap));
  497. struct f_rockusb *f_rkusb = get_rkusb();
  498. unsigned int chip_info[4], i;
  499. memset(chip_info, 0, sizeof(chip_info));
  500. rk_get_bootrom_chip_version(chip_info, 4);
  501. /*
  502. * Chip Version is a string saved in BOOTROM address space Little Endian
  503. *
  504. * Ex for rk3288: 0x33323041 0x32303134 0x30383133 0x56323030
  505. * which brings: 320A20140813V200
  506. *
  507. * Note that memory version do invert MSB/LSB so printing the char
  508. * buffer will show: A02341023180002V
  509. */
  510. printf("read chip version: ");
  511. for (i = 0; i < 4; i++) {
  512. printf("%c%c%c%c",
  513. (chip_info[i] >> 24) & 0xFF,
  514. (chip_info[i] >> 16) & 0xFF,
  515. (chip_info[i] >> 8) & 0xFF,
  516. (chip_info[i] >> 0) & 0xFF);
  517. }
  518. printf("\n");
  519. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  520. /* Prepare for sending subsequent CSW_GOOD */
  521. f_rkusb->tag = cbw->tag;
  522. f_rkusb->in_req->complete = tx_handler_send_csw;
  523. rockusb_tx_write((char *)chip_info, sizeof(chip_info));
  524. }
  525. static void cb_read_lba(struct usb_ep *ep, struct usb_request *req)
  526. {
  527. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  528. sizeof(struct fsg_bulk_cb_wrap));
  529. struct f_rockusb *f_rkusb = get_rkusb();
  530. int sector_count;
  531. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  532. sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
  533. f_rkusb->tag = cbw->tag;
  534. if (!f_rkusb->desc) {
  535. char *type = f_rkusb->dev_type;
  536. int index = f_rkusb->dev_index;
  537. f_rkusb->desc = blk_get_dev(type, index);
  538. if (!f_rkusb->desc ||
  539. f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
  540. printf("invalid device \"%s\", %d\n", type, index);
  541. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
  542. USB_BULK_CS_WRAP_LEN);
  543. return;
  544. }
  545. }
  546. f_rkusb->lba = get_unaligned_be32(&cbw->CDB[2]);
  547. f_rkusb->ul_size = sector_count * f_rkusb->desc->blksz;
  548. f_rkusb->ul_bytes = 0;
  549. debug("require read %x bytes, %x sectors from lba %x\n",
  550. f_rkusb->ul_size, sector_count, f_rkusb->lba);
  551. if (f_rkusb->ul_size == 0) {
  552. rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
  553. CSW_FAIL, USB_BULK_CS_WRAP_LEN);
  554. return;
  555. }
  556. /* Start right now sending first chunk */
  557. tx_handler_ul_image(ep, req);
  558. }
  559. static void cb_write_lba(struct usb_ep *ep, struct usb_request *req)
  560. {
  561. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  562. sizeof(struct fsg_bulk_cb_wrap));
  563. struct f_rockusb *f_rkusb = get_rkusb();
  564. int sector_count;
  565. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  566. sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
  567. f_rkusb->lba = get_unaligned_be32(&cbw->CDB[2]);
  568. f_rkusb->dl_size = sector_count * 512;
  569. f_rkusb->dl_bytes = 0;
  570. f_rkusb->tag = cbw->tag;
  571. debug("require write %x bytes, %x sectors to lba %x\n",
  572. f_rkusb->dl_size, sector_count, f_rkusb->lba);
  573. if (f_rkusb->dl_size == 0) {
  574. rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
  575. CSW_FAIL, USB_BULK_CS_WRAP_LEN);
  576. } else {
  577. req->complete = rx_handler_dl_image;
  578. req->length = rx_bytes_expected(ep);
  579. }
  580. }
  581. static void cb_erase_lba(struct usb_ep *ep, struct usb_request *req)
  582. {
  583. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  584. sizeof(struct fsg_bulk_cb_wrap));
  585. struct f_rockusb *f_rkusb = get_rkusb();
  586. int sector_count, lba, blks;
  587. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  588. sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
  589. f_rkusb->tag = cbw->tag;
  590. if (!f_rkusb->desc) {
  591. char *type = f_rkusb->dev_type;
  592. int index = f_rkusb->dev_index;
  593. f_rkusb->desc = blk_get_dev(type, index);
  594. if (!f_rkusb->desc ||
  595. f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
  596. printf("invalid device \"%s\", %d\n", type, index);
  597. rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
  598. USB_BULK_CS_WRAP_LEN);
  599. return;
  600. }
  601. }
  602. lba = get_unaligned_be32(&cbw->CDB[2]);
  603. debug("require erase %x sectors from lba %x\n",
  604. sector_count, lba);
  605. blks = blk_derase(f_rkusb->desc, lba, sector_count);
  606. if (blks != sector_count) {
  607. printf("failed erasing device %s: %d\n", f_rkusb->dev_type,
  608. f_rkusb->dev_index);
  609. rockusb_tx_write_csw(f_rkusb->tag,
  610. cbw->data_transfer_length, CSW_FAIL,
  611. USB_BULK_CS_WRAP_LEN);
  612. return;
  613. }
  614. rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
  615. USB_BULK_CS_WRAP_LEN);
  616. }
  617. void __weak rkusb_set_reboot_flag(int flag)
  618. {
  619. struct f_rockusb *f_rkusb = get_rkusb();
  620. printf("rockkusb set reboot flag: %d\n", f_rkusb->reboot_flag);
  621. }
  622. static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
  623. {
  624. struct f_rockusb *f_rkusb = get_rkusb();
  625. rkusb_set_reboot_flag(f_rkusb->reboot_flag);
  626. do_reset(NULL, 0, 0, NULL);
  627. }
  628. static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
  629. {
  630. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  631. sizeof(struct fsg_bulk_cb_wrap));
  632. struct f_rockusb *f_rkusb = get_rkusb();
  633. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  634. f_rkusb->reboot_flag = cbw->CDB[1];
  635. rockusb_func->in_req->complete = compl_do_reset;
  636. rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
  637. USB_BULK_CS_WRAP_LEN);
  638. }
  639. static void cb_not_support(struct usb_ep *ep, struct usb_request *req)
  640. {
  641. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  642. sizeof(struct fsg_bulk_cb_wrap));
  643. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  644. printf("Rockusb command %x not support yet\n", cbw->CDB[0]);
  645. rockusb_tx_write_csw(cbw->tag, 0, CSW_FAIL, USB_BULK_CS_WRAP_LEN);
  646. }
  647. static const struct cmd_dispatch_info cmd_dispatch_info[] = {
  648. {
  649. .cmd = K_FW_TEST_UNIT_READY,
  650. .cb = cb_test_unit_ready,
  651. },
  652. {
  653. .cmd = K_FW_READ_FLASH_ID,
  654. .cb = cb_read_storage_id,
  655. },
  656. {
  657. .cmd = K_FW_SET_DEVICE_ID,
  658. .cb = cb_not_support,
  659. },
  660. {
  661. .cmd = K_FW_TEST_BAD_BLOCK,
  662. .cb = cb_not_support,
  663. },
  664. {
  665. .cmd = K_FW_READ_10,
  666. .cb = cb_not_support,
  667. },
  668. {
  669. .cmd = K_FW_WRITE_10,
  670. .cb = cb_not_support,
  671. },
  672. {
  673. .cmd = K_FW_ERASE_10,
  674. .cb = cb_not_support,
  675. },
  676. {
  677. .cmd = K_FW_WRITE_SPARE,
  678. .cb = cb_not_support,
  679. },
  680. {
  681. .cmd = K_FW_READ_SPARE,
  682. .cb = cb_not_support,
  683. },
  684. {
  685. .cmd = K_FW_ERASE_10_FORCE,
  686. .cb = cb_not_support,
  687. },
  688. {
  689. .cmd = K_FW_GET_VERSION,
  690. .cb = cb_not_support,
  691. },
  692. {
  693. .cmd = K_FW_LBA_READ_10,
  694. .cb = cb_read_lba,
  695. },
  696. {
  697. .cmd = K_FW_LBA_WRITE_10,
  698. .cb = cb_write_lba,
  699. },
  700. {
  701. .cmd = K_FW_ERASE_SYS_DISK,
  702. .cb = cb_not_support,
  703. },
  704. {
  705. .cmd = K_FW_SDRAM_READ_10,
  706. .cb = cb_not_support,
  707. },
  708. {
  709. .cmd = K_FW_SDRAM_WRITE_10,
  710. .cb = cb_not_support,
  711. },
  712. {
  713. .cmd = K_FW_SDRAM_EXECUTE,
  714. .cb = cb_not_support,
  715. },
  716. {
  717. .cmd = K_FW_READ_FLASH_INFO,
  718. .cb = cb_not_support,
  719. },
  720. {
  721. .cmd = K_FW_GET_CHIP_VER,
  722. .cb = cb_get_chip_version,
  723. },
  724. {
  725. .cmd = K_FW_LOW_FORMAT,
  726. .cb = cb_not_support,
  727. },
  728. {
  729. .cmd = K_FW_SET_RESET_FLAG,
  730. .cb = cb_not_support,
  731. },
  732. {
  733. .cmd = K_FW_SPI_READ_10,
  734. .cb = cb_not_support,
  735. },
  736. {
  737. .cmd = K_FW_SPI_WRITE_10,
  738. .cb = cb_not_support,
  739. },
  740. {
  741. .cmd = K_FW_LBA_ERASE_10,
  742. .cb = cb_erase_lba,
  743. },
  744. {
  745. .cmd = K_FW_SESSION,
  746. .cb = cb_not_support,
  747. },
  748. {
  749. .cmd = K_FW_RESET,
  750. .cb = cb_reboot,
  751. },
  752. };
  753. static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
  754. {
  755. void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
  756. ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
  757. sizeof(struct fsg_bulk_cb_wrap));
  758. char *cmdbuf = req->buf;
  759. int i;
  760. if (req->status || req->length == 0)
  761. return;
  762. memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
  763. #ifdef DEBUG
  764. printcbw(req->buf);
  765. #endif
  766. for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
  767. if (cmd_dispatch_info[i].cmd == cbw->CDB[0]) {
  768. func_cb = cmd_dispatch_info[i].cb;
  769. break;
  770. }
  771. }
  772. if (!func_cb) {
  773. printf("unknown command: %s\n", (char *)req->buf);
  774. rockusb_tx_write_str("FAILunknown command");
  775. } else {
  776. if (req->actual < req->length) {
  777. u8 *buf = (u8 *)req->buf;
  778. buf[req->actual] = 0;
  779. func_cb(ep, req);
  780. } else {
  781. puts("buffer overflow\n");
  782. rockusb_tx_write_str("FAILbuffer overflow");
  783. }
  784. }
  785. *cmdbuf = '\0';
  786. req->actual = 0;
  787. usb_ep_queue(ep, req, 0);
  788. }