f_dfu.c 19 KB

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