f_dfu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * f_dfu.c -- Device Firmware Update USB function
  4. *
  5. * Copyright (C) 2012 Samsung Electronics
  6. * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  7. * Lukasz Majewski <l.majewski@samsung.com>
  8. *
  9. * Based on OpenMoko u-boot: drivers/usb/usbdfu.c
  10. * (C) 2007 by OpenMoko, Inc.
  11. * Author: Harald Welte <laforge@openmoko.org>
  12. *
  13. * based on existing SAM7DFU code from OpenPCD:
  14. * (C) Copyright 2006 by Harald Welte <hwelte at hmw-consulting.de>
  15. */
  16. #include <env.h>
  17. #include <errno.h>
  18. #include <common.h>
  19. #include <malloc.h>
  20. #include <linux/usb/ch9.h>
  21. #include <linux/usb/gadget.h>
  22. #include <linux/usb/composite.h>
  23. #include <dfu.h>
  24. #include <g_dnl.h>
  25. #include "f_dfu.h"
  26. struct f_dfu {
  27. struct usb_function usb_function;
  28. struct usb_descriptor_header **function;
  29. struct usb_string *strings;
  30. /* when configured, we have one config */
  31. u8 config;
  32. u8 altsetting;
  33. enum dfu_state dfu_state;
  34. unsigned int dfu_status;
  35. /* Send/received block number is handy for data integrity check */
  36. int blk_seq_num;
  37. unsigned int poll_timeout;
  38. };
  39. struct dfu_entity *dfu_defer_flush;
  40. typedef int (*dfu_state_fn) (struct f_dfu *,
  41. const struct usb_ctrlrequest *,
  42. struct usb_gadget *,
  43. struct usb_request *);
  44. static inline struct f_dfu *func_to_dfu(struct usb_function *f)
  45. {
  46. return container_of(f, struct f_dfu, usb_function);
  47. }
  48. static const struct dfu_function_descriptor dfu_func = {
  49. .bLength = sizeof dfu_func,
  50. .bDescriptorType = DFU_DT_FUNC,
  51. .bmAttributes = DFU_BIT_WILL_DETACH |
  52. DFU_BIT_MANIFESTATION_TOLERANT |
  53. DFU_BIT_CAN_UPLOAD |
  54. DFU_BIT_CAN_DNLOAD,
  55. .wDetachTimeOut = 0,
  56. .wTransferSize = DFU_USB_BUFSIZ,
  57. .bcdDFUVersion = __constant_cpu_to_le16(0x0110),
  58. };
  59. static struct usb_interface_descriptor dfu_intf_runtime = {
  60. .bLength = sizeof dfu_intf_runtime,
  61. .bDescriptorType = USB_DT_INTERFACE,
  62. .bNumEndpoints = 0,
  63. .bInterfaceClass = USB_CLASS_APP_SPEC,
  64. .bInterfaceSubClass = 1,
  65. .bInterfaceProtocol = 1,
  66. /* .iInterface = DYNAMIC */
  67. };
  68. static struct usb_descriptor_header *dfu_runtime_descs[] = {
  69. (struct usb_descriptor_header *) &dfu_intf_runtime,
  70. NULL,
  71. };
  72. static const char dfu_name[] = "Device Firmware Upgrade";
  73. /*
  74. * static strings, in UTF-8
  75. *
  76. * dfu_generic configuration
  77. */
  78. static struct usb_string strings_dfu_generic[] = {
  79. [0].s = dfu_name,
  80. { } /* end of list */
  81. };
  82. static struct usb_gadget_strings stringtab_dfu_generic = {
  83. .language = 0x0409, /* en-us */
  84. .strings = strings_dfu_generic,
  85. };
  86. static struct usb_gadget_strings *dfu_generic_strings[] = {
  87. &stringtab_dfu_generic,
  88. NULL,
  89. };
  90. /*
  91. * usb_function specific
  92. */
  93. static struct usb_gadget_strings stringtab_dfu = {
  94. .language = 0x0409, /* en-us */
  95. /*
  96. * .strings
  97. *
  98. * assigned during initialization,
  99. * depends on number of flash entities
  100. *
  101. */
  102. };
  103. static struct usb_gadget_strings *dfu_strings[] = {
  104. &stringtab_dfu,
  105. NULL,
  106. };
  107. static void dfu_set_poll_timeout(struct dfu_status *dstat, unsigned int ms)
  108. {
  109. /*
  110. * The bwPollTimeout DFU_GETSTATUS request payload provides information
  111. * about minimum time, in milliseconds, that the host should wait before
  112. * sending a subsequent DFU_GETSTATUS request
  113. *
  114. * This permits the device to vary the delay depending on its need to
  115. * erase or program the memory
  116. *
  117. */
  118. unsigned char *p = (unsigned char *)&ms;
  119. if (!ms || (ms & ~DFU_POLL_TIMEOUT_MASK)) {
  120. dstat->bwPollTimeout[0] = 0;
  121. dstat->bwPollTimeout[1] = 0;
  122. dstat->bwPollTimeout[2] = 0;
  123. return;
  124. }
  125. dstat->bwPollTimeout[0] = *p++;
  126. dstat->bwPollTimeout[1] = *p++;
  127. dstat->bwPollTimeout[2] = *p;
  128. }
  129. /*-------------------------------------------------------------------------*/
  130. static void dnload_request_complete(struct usb_ep *ep, struct usb_request *req)
  131. {
  132. struct f_dfu *f_dfu = req->context;
  133. int ret;
  134. ret = dfu_write(dfu_get_entity(f_dfu->altsetting), req->buf,
  135. req->actual, f_dfu->blk_seq_num);
  136. if (ret) {
  137. f_dfu->dfu_status = DFU_STATUS_errUNKNOWN;
  138. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  139. }
  140. }
  141. static void dnload_request_flush(struct usb_ep *ep, struct usb_request *req)
  142. {
  143. struct f_dfu *f_dfu = req->context;
  144. dfu_set_defer_flush(dfu_get_entity(f_dfu->altsetting));
  145. }
  146. static inline int dfu_get_manifest_timeout(struct dfu_entity *dfu)
  147. {
  148. return dfu->poll_timeout ? dfu->poll_timeout(dfu) :
  149. DFU_MANIFEST_POLL_TIMEOUT;
  150. }
  151. static int handle_getstatus(struct usb_request *req)
  152. {
  153. struct dfu_status *dstat = (struct dfu_status *)req->buf;
  154. struct f_dfu *f_dfu = req->context;
  155. struct dfu_entity *dfu = dfu_get_entity(f_dfu->altsetting);
  156. dfu_set_poll_timeout(dstat, 0);
  157. switch (f_dfu->dfu_state) {
  158. case DFU_STATE_dfuDNLOAD_SYNC:
  159. case DFU_STATE_dfuDNBUSY:
  160. f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_IDLE;
  161. break;
  162. case DFU_STATE_dfuMANIFEST_SYNC:
  163. f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
  164. break;
  165. case DFU_STATE_dfuMANIFEST:
  166. dfu_set_poll_timeout(dstat, dfu_get_manifest_timeout(dfu));
  167. break;
  168. default:
  169. break;
  170. }
  171. if (f_dfu->poll_timeout)
  172. if (!(f_dfu->blk_seq_num %
  173. (dfu_get_buf_size() / DFU_USB_BUFSIZ)))
  174. dfu_set_poll_timeout(dstat, f_dfu->poll_timeout);
  175. /* send status response */
  176. dstat->bStatus = f_dfu->dfu_status;
  177. dstat->bState = f_dfu->dfu_state;
  178. dstat->iString = 0;
  179. return sizeof(struct dfu_status);
  180. }
  181. static int handle_getstate(struct usb_request *req)
  182. {
  183. struct f_dfu *f_dfu = req->context;
  184. ((u8 *)req->buf)[0] = f_dfu->dfu_state;
  185. return sizeof(u8);
  186. }
  187. static inline void to_dfu_mode(struct f_dfu *f_dfu)
  188. {
  189. f_dfu->usb_function.strings = dfu_strings;
  190. f_dfu->usb_function.hs_descriptors = f_dfu->function;
  191. f_dfu->usb_function.descriptors = f_dfu->function;
  192. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  193. }
  194. static inline void to_runtime_mode(struct f_dfu *f_dfu)
  195. {
  196. f_dfu->usb_function.strings = NULL;
  197. f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
  198. f_dfu->usb_function.descriptors = dfu_runtime_descs;
  199. }
  200. static int handle_upload(struct usb_request *req, u16 len)
  201. {
  202. struct f_dfu *f_dfu = req->context;
  203. return dfu_read(dfu_get_entity(f_dfu->altsetting), req->buf,
  204. req->length, f_dfu->blk_seq_num);
  205. }
  206. static int handle_dnload(struct usb_gadget *gadget, u16 len)
  207. {
  208. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  209. struct usb_request *req = cdev->req;
  210. struct f_dfu *f_dfu = req->context;
  211. if (len == 0)
  212. f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;
  213. req->complete = dnload_request_complete;
  214. return len;
  215. }
  216. /*-------------------------------------------------------------------------*/
  217. /* DFU state machine */
  218. static int state_app_idle(struct f_dfu *f_dfu,
  219. const struct usb_ctrlrequest *ctrl,
  220. struct usb_gadget *gadget,
  221. struct usb_request *req)
  222. {
  223. int value = 0;
  224. switch (ctrl->bRequest) {
  225. case USB_REQ_DFU_GETSTATUS:
  226. value = handle_getstatus(req);
  227. break;
  228. case USB_REQ_DFU_GETSTATE:
  229. value = handle_getstate(req);
  230. break;
  231. case USB_REQ_DFU_DETACH:
  232. f_dfu->dfu_state = DFU_STATE_appDETACH;
  233. to_dfu_mode(f_dfu);
  234. value = RET_ZLP;
  235. break;
  236. default:
  237. value = RET_STALL;
  238. break;
  239. }
  240. return value;
  241. }
  242. static int state_app_detach(struct f_dfu *f_dfu,
  243. const struct usb_ctrlrequest *ctrl,
  244. struct usb_gadget *gadget,
  245. struct usb_request *req)
  246. {
  247. int value = 0;
  248. switch (ctrl->bRequest) {
  249. case USB_REQ_DFU_GETSTATUS:
  250. value = handle_getstatus(req);
  251. break;
  252. case USB_REQ_DFU_GETSTATE:
  253. value = handle_getstate(req);
  254. break;
  255. default:
  256. f_dfu->dfu_state = DFU_STATE_appIDLE;
  257. value = RET_STALL;
  258. break;
  259. }
  260. return value;
  261. }
  262. static int state_dfu_idle(struct f_dfu *f_dfu,
  263. const struct usb_ctrlrequest *ctrl,
  264. struct usb_gadget *gadget,
  265. struct usb_request *req)
  266. {
  267. u16 w_value = le16_to_cpu(ctrl->wValue);
  268. u16 len = le16_to_cpu(ctrl->wLength);
  269. int value = 0;
  270. switch (ctrl->bRequest) {
  271. case USB_REQ_DFU_DNLOAD:
  272. if (len == 0) {
  273. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  274. value = RET_STALL;
  275. break;
  276. }
  277. f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
  278. f_dfu->blk_seq_num = w_value;
  279. value = handle_dnload(gadget, len);
  280. break;
  281. case USB_REQ_DFU_UPLOAD:
  282. f_dfu->dfu_state = DFU_STATE_dfuUPLOAD_IDLE;
  283. f_dfu->blk_seq_num = 0;
  284. value = handle_upload(req, len);
  285. break;
  286. case USB_REQ_DFU_ABORT:
  287. /* no zlp? */
  288. value = RET_ZLP;
  289. break;
  290. case USB_REQ_DFU_GETSTATUS:
  291. value = handle_getstatus(req);
  292. break;
  293. case USB_REQ_DFU_GETSTATE:
  294. value = handle_getstate(req);
  295. break;
  296. case USB_REQ_DFU_DETACH:
  297. /*
  298. * Proprietary extension: 'detach' from idle mode and
  299. * get back to runtime mode in case of USB Reset. As
  300. * much as I dislike this, we just can't use every USB
  301. * bus reset to switch back to runtime mode, since at
  302. * least the Linux USB stack likes to send a number of
  303. * resets in a row :(
  304. */
  305. f_dfu->dfu_state =
  306. DFU_STATE_dfuMANIFEST_WAIT_RST;
  307. to_runtime_mode(f_dfu);
  308. f_dfu->dfu_state = DFU_STATE_appIDLE;
  309. g_dnl_trigger_detach();
  310. break;
  311. default:
  312. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  313. value = RET_STALL;
  314. break;
  315. }
  316. return value;
  317. }
  318. static int state_dfu_dnload_sync(struct f_dfu *f_dfu,
  319. const struct usb_ctrlrequest *ctrl,
  320. struct usb_gadget *gadget,
  321. struct usb_request *req)
  322. {
  323. int value = 0;
  324. switch (ctrl->bRequest) {
  325. case USB_REQ_DFU_GETSTATUS:
  326. value = handle_getstatus(req);
  327. break;
  328. case USB_REQ_DFU_GETSTATE:
  329. value = handle_getstate(req);
  330. break;
  331. default:
  332. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  333. value = RET_STALL;
  334. break;
  335. }
  336. return value;
  337. }
  338. static int state_dfu_dnbusy(struct f_dfu *f_dfu,
  339. const struct usb_ctrlrequest *ctrl,
  340. struct usb_gadget *gadget,
  341. struct usb_request *req)
  342. {
  343. int value = 0;
  344. switch (ctrl->bRequest) {
  345. case USB_REQ_DFU_GETSTATUS:
  346. value = handle_getstatus(req);
  347. break;
  348. default:
  349. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  350. value = RET_STALL;
  351. break;
  352. }
  353. return value;
  354. }
  355. static int state_dfu_dnload_idle(struct f_dfu *f_dfu,
  356. const struct usb_ctrlrequest *ctrl,
  357. struct usb_gadget *gadget,
  358. struct usb_request *req)
  359. {
  360. u16 w_value = le16_to_cpu(ctrl->wValue);
  361. u16 len = le16_to_cpu(ctrl->wLength);
  362. int value = 0;
  363. switch (ctrl->bRequest) {
  364. case USB_REQ_DFU_DNLOAD:
  365. f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
  366. f_dfu->blk_seq_num = w_value;
  367. value = handle_dnload(gadget, len);
  368. break;
  369. case USB_REQ_DFU_ABORT:
  370. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  371. value = RET_ZLP;
  372. break;
  373. case USB_REQ_DFU_GETSTATUS:
  374. value = handle_getstatus(req);
  375. break;
  376. case USB_REQ_DFU_GETSTATE:
  377. value = handle_getstate(req);
  378. break;
  379. default:
  380. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  381. value = RET_STALL;
  382. break;
  383. }
  384. return value;
  385. }
  386. static int state_dfu_manifest_sync(struct f_dfu *f_dfu,
  387. const struct usb_ctrlrequest *ctrl,
  388. struct usb_gadget *gadget,
  389. struct usb_request *req)
  390. {
  391. int value = 0;
  392. switch (ctrl->bRequest) {
  393. case USB_REQ_DFU_GETSTATUS:
  394. /* We're MainfestationTolerant */
  395. f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
  396. value = handle_getstatus(req);
  397. f_dfu->blk_seq_num = 0;
  398. req->complete = dnload_request_flush;
  399. break;
  400. case USB_REQ_DFU_GETSTATE:
  401. value = handle_getstate(req);
  402. break;
  403. default:
  404. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  405. value = RET_STALL;
  406. break;
  407. }
  408. return value;
  409. }
  410. static int state_dfu_manifest(struct f_dfu *f_dfu,
  411. const struct usb_ctrlrequest *ctrl,
  412. struct usb_gadget *gadget,
  413. struct usb_request *req)
  414. {
  415. int value = 0;
  416. switch (ctrl->bRequest) {
  417. case USB_REQ_DFU_GETSTATUS:
  418. /* We're MainfestationTolerant */
  419. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  420. value = handle_getstatus(req);
  421. f_dfu->blk_seq_num = 0;
  422. puts("DOWNLOAD ... OK\nCtrl+C to exit ...\n");
  423. break;
  424. case USB_REQ_DFU_GETSTATE:
  425. value = handle_getstate(req);
  426. break;
  427. default:
  428. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  429. value = RET_STALL;
  430. break;
  431. }
  432. return value;
  433. }
  434. static int state_dfu_upload_idle(struct f_dfu *f_dfu,
  435. const struct usb_ctrlrequest *ctrl,
  436. struct usb_gadget *gadget,
  437. struct usb_request *req)
  438. {
  439. u16 w_value = le16_to_cpu(ctrl->wValue);
  440. u16 len = le16_to_cpu(ctrl->wLength);
  441. int value = 0;
  442. switch (ctrl->bRequest) {
  443. case USB_REQ_DFU_UPLOAD:
  444. /* state transition if less data then requested */
  445. f_dfu->blk_seq_num = w_value;
  446. value = handle_upload(req, len);
  447. if (value >= 0 && value < len)
  448. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  449. break;
  450. case USB_REQ_DFU_ABORT:
  451. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  452. /* no zlp? */
  453. value = RET_ZLP;
  454. break;
  455. case USB_REQ_DFU_GETSTATUS:
  456. value = handle_getstatus(req);
  457. break;
  458. case USB_REQ_DFU_GETSTATE:
  459. value = handle_getstate(req);
  460. break;
  461. default:
  462. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  463. value = RET_STALL;
  464. break;
  465. }
  466. return value;
  467. }
  468. static int state_dfu_error(struct f_dfu *f_dfu,
  469. const struct usb_ctrlrequest *ctrl,
  470. struct usb_gadget *gadget,
  471. struct usb_request *req)
  472. {
  473. int value = 0;
  474. switch (ctrl->bRequest) {
  475. case USB_REQ_DFU_GETSTATUS:
  476. value = handle_getstatus(req);
  477. break;
  478. case USB_REQ_DFU_GETSTATE:
  479. value = handle_getstate(req);
  480. break;
  481. case USB_REQ_DFU_CLRSTATUS:
  482. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  483. f_dfu->dfu_status = DFU_STATUS_OK;
  484. /* no zlp? */
  485. value = RET_ZLP;
  486. break;
  487. default:
  488. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  489. value = RET_STALL;
  490. break;
  491. }
  492. return value;
  493. }
  494. static dfu_state_fn dfu_state[] = {
  495. state_app_idle, /* DFU_STATE_appIDLE */
  496. state_app_detach, /* DFU_STATE_appDETACH */
  497. state_dfu_idle, /* DFU_STATE_dfuIDLE */
  498. state_dfu_dnload_sync, /* DFU_STATE_dfuDNLOAD_SYNC */
  499. state_dfu_dnbusy, /* DFU_STATE_dfuDNBUSY */
  500. state_dfu_dnload_idle, /* DFU_STATE_dfuDNLOAD_IDLE */
  501. state_dfu_manifest_sync, /* DFU_STATE_dfuMANIFEST_SYNC */
  502. state_dfu_manifest, /* DFU_STATE_dfuMANIFEST */
  503. NULL, /* DFU_STATE_dfuMANIFEST_WAIT_RST */
  504. state_dfu_upload_idle, /* DFU_STATE_dfuUPLOAD_IDLE */
  505. state_dfu_error /* DFU_STATE_dfuERROR */
  506. };
  507. static int
  508. dfu_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  509. {
  510. struct usb_gadget *gadget = f->config->cdev->gadget;
  511. struct usb_request *req = f->config->cdev->req;
  512. struct f_dfu *f_dfu = f->config->cdev->req->context;
  513. u16 len = le16_to_cpu(ctrl->wLength);
  514. u16 w_value = le16_to_cpu(ctrl->wValue);
  515. int value = 0;
  516. u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
  517. debug("w_value: 0x%x len: 0x%x\n", w_value, len);
  518. debug("req_type: 0x%x ctrl->bRequest: 0x%x f_dfu->dfu_state: 0x%x\n",
  519. req_type, ctrl->bRequest, f_dfu->dfu_state);
  520. #ifdef CONFIG_DFU_TIMEOUT
  521. /* Forbid aborting by timeout. Next dfu command may update this */
  522. dfu_set_timeout(0);
  523. #endif
  524. if (req_type == USB_TYPE_STANDARD) {
  525. if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR &&
  526. (w_value >> 8) == DFU_DT_FUNC) {
  527. value = min(len, (u16) sizeof(dfu_func));
  528. memcpy(req->buf, &dfu_func, value);
  529. }
  530. } else /* DFU specific request */
  531. value = dfu_state[f_dfu->dfu_state] (f_dfu, ctrl, gadget, req);
  532. if (value >= 0) {
  533. req->length = value;
  534. req->zero = value < len;
  535. value = usb_ep_queue(gadget->ep0, req, 0);
  536. if (value < 0) {
  537. debug("ep_queue --> %d\n", value);
  538. req->status = 0;
  539. }
  540. }
  541. return value;
  542. }
  543. /*-------------------------------------------------------------------------*/
  544. static int
  545. dfu_prepare_strings(struct f_dfu *f_dfu, int n)
  546. {
  547. struct dfu_entity *de = NULL;
  548. int i = 0;
  549. f_dfu->strings = calloc(sizeof(struct usb_string), n + 1);
  550. if (!f_dfu->strings)
  551. return -ENOMEM;
  552. for (i = 0; i < n; ++i) {
  553. de = dfu_get_entity(i);
  554. f_dfu->strings[i].s = de->name;
  555. }
  556. f_dfu->strings[i].id = 0;
  557. f_dfu->strings[i].s = NULL;
  558. return 0;
  559. }
  560. static int dfu_prepare_function(struct f_dfu *f_dfu, int n)
  561. {
  562. struct usb_interface_descriptor *d;
  563. int i = 0;
  564. f_dfu->function = calloc(sizeof(struct usb_descriptor_header *), n + 2);
  565. if (!f_dfu->function)
  566. goto enomem;
  567. for (i = 0; i < n; ++i) {
  568. d = calloc(sizeof(*d), 1);
  569. if (!d)
  570. goto enomem;
  571. d->bLength = sizeof(*d);
  572. d->bDescriptorType = USB_DT_INTERFACE;
  573. d->bAlternateSetting = i;
  574. d->bNumEndpoints = 0;
  575. d->bInterfaceClass = USB_CLASS_APP_SPEC;
  576. d->bInterfaceSubClass = 1;
  577. d->bInterfaceProtocol = 2;
  578. f_dfu->function[i] = (struct usb_descriptor_header *)d;
  579. }
  580. /* add DFU Functional Descriptor */
  581. f_dfu->function[i] = calloc(sizeof(dfu_func), 1);
  582. if (!f_dfu->function[i])
  583. goto enomem;
  584. memcpy(f_dfu->function[i], &dfu_func, sizeof(dfu_func));
  585. i++;
  586. f_dfu->function[i] = NULL;
  587. return 0;
  588. enomem:
  589. while (i) {
  590. free(f_dfu->function[--i]);
  591. f_dfu->function[i] = NULL;
  592. }
  593. free(f_dfu->function);
  594. return -ENOMEM;
  595. }
  596. static int dfu_bind(struct usb_configuration *c, struct usb_function *f)
  597. {
  598. struct usb_composite_dev *cdev = c->cdev;
  599. struct f_dfu *f_dfu = func_to_dfu(f);
  600. const char *s;
  601. int alt_num = dfu_get_alt_number();
  602. int rv, id, i;
  603. id = usb_interface_id(c, f);
  604. if (id < 0)
  605. return id;
  606. dfu_intf_runtime.bInterfaceNumber = id;
  607. f_dfu->dfu_state = DFU_STATE_appIDLE;
  608. f_dfu->dfu_status = DFU_STATUS_OK;
  609. rv = dfu_prepare_function(f_dfu, alt_num);
  610. if (rv)
  611. goto error;
  612. rv = dfu_prepare_strings(f_dfu, alt_num);
  613. if (rv)
  614. goto error;
  615. for (i = 0; i < alt_num; i++) {
  616. id = usb_string_id(cdev);
  617. if (id < 0)
  618. return id;
  619. f_dfu->strings[i].id = id;
  620. ((struct usb_interface_descriptor *)f_dfu->function[i])
  621. ->iInterface = id;
  622. }
  623. to_dfu_mode(f_dfu);
  624. stringtab_dfu.strings = f_dfu->strings;
  625. cdev->req->context = f_dfu;
  626. s = env_get("serial#");
  627. if (s)
  628. g_dnl_set_serialnumber((char *)s);
  629. error:
  630. return rv;
  631. }
  632. static void dfu_unbind(struct usb_configuration *c, struct usb_function *f)
  633. {
  634. struct f_dfu *f_dfu = func_to_dfu(f);
  635. int alt_num = dfu_get_alt_number();
  636. int i;
  637. if (f_dfu->strings) {
  638. i = alt_num;
  639. while (i)
  640. f_dfu->strings[--i].s = NULL;
  641. free(f_dfu->strings);
  642. }
  643. if (f_dfu->function) {
  644. i = alt_num;
  645. i++; /* free DFU Functional Descriptor */
  646. while (i) {
  647. free(f_dfu->function[--i]);
  648. f_dfu->function[i] = NULL;
  649. }
  650. free(f_dfu->function);
  651. }
  652. free(f_dfu);
  653. }
  654. static int dfu_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  655. {
  656. struct f_dfu *f_dfu = func_to_dfu(f);
  657. debug("%s: intf:%d alt:%d\n", __func__, intf, alt);
  658. f_dfu->altsetting = alt;
  659. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  660. f_dfu->dfu_status = DFU_STATUS_OK;
  661. return 0;
  662. }
  663. static int __dfu_get_alt(struct usb_function *f, unsigned intf)
  664. {
  665. struct f_dfu *f_dfu = func_to_dfu(f);
  666. return f_dfu->altsetting;
  667. }
  668. /* TODO: is this really what we need here? */
  669. static void dfu_disable(struct usb_function *f)
  670. {
  671. struct f_dfu *f_dfu = func_to_dfu(f);
  672. if (f_dfu->config == 0)
  673. return;
  674. debug("%s: reset config\n", __func__);
  675. f_dfu->config = 0;
  676. }
  677. static int dfu_bind_config(struct usb_configuration *c)
  678. {
  679. struct f_dfu *f_dfu;
  680. int status;
  681. f_dfu = calloc(sizeof(*f_dfu), 1);
  682. if (!f_dfu)
  683. return -ENOMEM;
  684. f_dfu->usb_function.name = "dfu";
  685. f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
  686. f_dfu->usb_function.descriptors = dfu_runtime_descs;
  687. f_dfu->usb_function.bind = dfu_bind;
  688. f_dfu->usb_function.unbind = dfu_unbind;
  689. f_dfu->usb_function.set_alt = dfu_set_alt;
  690. f_dfu->usb_function.get_alt = __dfu_get_alt;
  691. f_dfu->usb_function.disable = dfu_disable;
  692. f_dfu->usb_function.strings = dfu_generic_strings;
  693. f_dfu->usb_function.setup = dfu_handle;
  694. f_dfu->poll_timeout = DFU_DEFAULT_POLL_TIMEOUT;
  695. status = usb_add_function(c, &f_dfu->usb_function);
  696. if (status)
  697. free(f_dfu);
  698. return status;
  699. }
  700. int dfu_add(struct usb_configuration *c)
  701. {
  702. int id;
  703. id = usb_string_id(c->cdev);
  704. if (id < 0)
  705. return id;
  706. strings_dfu_generic[0].id = id;
  707. dfu_intf_runtime.iInterface = id;
  708. debug("%s: cdev: 0x%p gadget:0x%p gadget->ep0: 0x%p\n", __func__,
  709. c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
  710. return dfu_bind_config(c);
  711. }
  712. DECLARE_GADGET_BIND_CALLBACK(usb_dnl_dfu, dfu_add);