f_dfu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * f_dfu.c -- Device Firmware Update USB function
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  6. * Lukasz Majewski <l.majewski@samsung.com>
  7. *
  8. * Based on OpenMoko u-boot: drivers/usb/usbdfu.c
  9. * (C) 2007 by OpenMoko, Inc.
  10. * Author: Harald Welte <laforge@openmoko.org>
  11. *
  12. * based on existing SAM7DFU code from OpenPCD:
  13. * (C) Copyright 2006 by Harald Welte <hwelte at hmw-consulting.de>
  14. *
  15. * SPDX-License-Identifier: GPL-2.0+
  16. */
  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. 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->length, 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. int ret;
  144. ret = dfu_flush(dfu_get_entity(f_dfu->altsetting), req->buf,
  145. req->length, f_dfu->blk_seq_num);
  146. if (ret) {
  147. f_dfu->dfu_status = DFU_STATUS_errUNKNOWN;
  148. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  149. }
  150. }
  151. static inline int dfu_get_manifest_timeout(struct dfu_entity *dfu)
  152. {
  153. return dfu->poll_timeout ? dfu->poll_timeout(dfu) :
  154. DFU_MANIFEST_POLL_TIMEOUT;
  155. }
  156. static void handle_getstatus(struct usb_request *req)
  157. {
  158. struct dfu_status *dstat = (struct dfu_status *)req->buf;
  159. struct f_dfu *f_dfu = req->context;
  160. struct dfu_entity *dfu = dfu_get_entity(f_dfu->altsetting);
  161. dfu_set_poll_timeout(dstat, 0);
  162. switch (f_dfu->dfu_state) {
  163. case DFU_STATE_dfuDNLOAD_SYNC:
  164. case DFU_STATE_dfuDNBUSY:
  165. f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_IDLE;
  166. break;
  167. case DFU_STATE_dfuMANIFEST_SYNC:
  168. f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
  169. break;
  170. case DFU_STATE_dfuMANIFEST:
  171. dfu_set_poll_timeout(dstat, dfu_get_manifest_timeout(dfu));
  172. break;
  173. default:
  174. break;
  175. }
  176. if (f_dfu->poll_timeout)
  177. if (!(f_dfu->blk_seq_num %
  178. (dfu_get_buf_size() / DFU_USB_BUFSIZ)))
  179. dfu_set_poll_timeout(dstat, f_dfu->poll_timeout);
  180. /* send status response */
  181. dstat->bStatus = f_dfu->dfu_status;
  182. dstat->bState = f_dfu->dfu_state;
  183. dstat->iString = 0;
  184. }
  185. static void handle_getstate(struct usb_request *req)
  186. {
  187. struct f_dfu *f_dfu = req->context;
  188. ((u8 *)req->buf)[0] = f_dfu->dfu_state;
  189. req->actual = sizeof(u8);
  190. }
  191. static inline void to_dfu_mode(struct f_dfu *f_dfu)
  192. {
  193. f_dfu->usb_function.strings = dfu_strings;
  194. f_dfu->usb_function.hs_descriptors = f_dfu->function;
  195. f_dfu->usb_function.descriptors = f_dfu->function;
  196. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  197. }
  198. static inline void to_runtime_mode(struct f_dfu *f_dfu)
  199. {
  200. f_dfu->usb_function.strings = NULL;
  201. f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
  202. f_dfu->usb_function.descriptors = dfu_runtime_descs;
  203. }
  204. static int handle_upload(struct usb_request *req, u16 len)
  205. {
  206. struct f_dfu *f_dfu = req->context;
  207. return dfu_read(dfu_get_entity(f_dfu->altsetting), req->buf,
  208. req->length, f_dfu->blk_seq_num);
  209. }
  210. static int handle_dnload(struct usb_gadget *gadget, u16 len)
  211. {
  212. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  213. struct usb_request *req = cdev->req;
  214. struct f_dfu *f_dfu = req->context;
  215. if (len == 0)
  216. f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;
  217. req->complete = dnload_request_complete;
  218. return len;
  219. }
  220. /*-------------------------------------------------------------------------*/
  221. /* DFU state machine */
  222. static int state_app_idle(struct f_dfu *f_dfu,
  223. const struct usb_ctrlrequest *ctrl,
  224. struct usb_gadget *gadget,
  225. struct usb_request *req)
  226. {
  227. int value = 0;
  228. switch (ctrl->bRequest) {
  229. case USB_REQ_DFU_GETSTATUS:
  230. handle_getstatus(req);
  231. value = RET_STAT_LEN;
  232. break;
  233. case USB_REQ_DFU_GETSTATE:
  234. handle_getstate(req);
  235. break;
  236. case USB_REQ_DFU_DETACH:
  237. f_dfu->dfu_state = DFU_STATE_appDETACH;
  238. to_dfu_mode(f_dfu);
  239. value = RET_ZLP;
  240. break;
  241. default:
  242. value = RET_STALL;
  243. break;
  244. }
  245. return value;
  246. }
  247. static int state_app_detach(struct f_dfu *f_dfu,
  248. const struct usb_ctrlrequest *ctrl,
  249. struct usb_gadget *gadget,
  250. struct usb_request *req)
  251. {
  252. int value = 0;
  253. switch (ctrl->bRequest) {
  254. case USB_REQ_DFU_GETSTATUS:
  255. handle_getstatus(req);
  256. value = RET_STAT_LEN;
  257. break;
  258. case USB_REQ_DFU_GETSTATE:
  259. handle_getstate(req);
  260. break;
  261. default:
  262. f_dfu->dfu_state = DFU_STATE_appIDLE;
  263. value = RET_STALL;
  264. break;
  265. }
  266. return value;
  267. }
  268. static int state_dfu_idle(struct f_dfu *f_dfu,
  269. const struct usb_ctrlrequest *ctrl,
  270. struct usb_gadget *gadget,
  271. struct usb_request *req)
  272. {
  273. u16 w_value = le16_to_cpu(ctrl->wValue);
  274. u16 len = le16_to_cpu(ctrl->wLength);
  275. int value = 0;
  276. switch (ctrl->bRequest) {
  277. case USB_REQ_DFU_DNLOAD:
  278. if (len == 0) {
  279. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  280. value = RET_STALL;
  281. break;
  282. }
  283. f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
  284. f_dfu->blk_seq_num = w_value;
  285. value = handle_dnload(gadget, len);
  286. break;
  287. case USB_REQ_DFU_UPLOAD:
  288. f_dfu->dfu_state = DFU_STATE_dfuUPLOAD_IDLE;
  289. f_dfu->blk_seq_num = 0;
  290. value = handle_upload(req, len);
  291. break;
  292. case USB_REQ_DFU_ABORT:
  293. /* no zlp? */
  294. value = RET_ZLP;
  295. break;
  296. case USB_REQ_DFU_GETSTATUS:
  297. handle_getstatus(req);
  298. value = RET_STAT_LEN;
  299. break;
  300. case USB_REQ_DFU_GETSTATE:
  301. handle_getstate(req);
  302. break;
  303. case USB_REQ_DFU_DETACH:
  304. /*
  305. * Proprietary extension: 'detach' from idle mode and
  306. * get back to runtime mode in case of USB Reset. As
  307. * much as I dislike this, we just can't use every USB
  308. * bus reset to switch back to runtime mode, since at
  309. * least the Linux USB stack likes to send a number of
  310. * resets in a row :(
  311. */
  312. f_dfu->dfu_state =
  313. DFU_STATE_dfuMANIFEST_WAIT_RST;
  314. to_runtime_mode(f_dfu);
  315. f_dfu->dfu_state = DFU_STATE_appIDLE;
  316. g_dnl_trigger_detach();
  317. break;
  318. default:
  319. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  320. value = RET_STALL;
  321. break;
  322. }
  323. return value;
  324. }
  325. static int state_dfu_dnload_sync(struct f_dfu *f_dfu,
  326. const struct usb_ctrlrequest *ctrl,
  327. struct usb_gadget *gadget,
  328. struct usb_request *req)
  329. {
  330. int value = 0;
  331. switch (ctrl->bRequest) {
  332. case USB_REQ_DFU_GETSTATUS:
  333. handle_getstatus(req);
  334. value = RET_STAT_LEN;
  335. break;
  336. case USB_REQ_DFU_GETSTATE:
  337. handle_getstate(req);
  338. break;
  339. default:
  340. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  341. value = RET_STALL;
  342. break;
  343. }
  344. return value;
  345. }
  346. static int state_dfu_dnbusy(struct f_dfu *f_dfu,
  347. const struct usb_ctrlrequest *ctrl,
  348. struct usb_gadget *gadget,
  349. struct usb_request *req)
  350. {
  351. int value = 0;
  352. switch (ctrl->bRequest) {
  353. case USB_REQ_DFU_GETSTATUS:
  354. handle_getstatus(req);
  355. value = RET_STAT_LEN;
  356. break;
  357. default:
  358. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  359. value = RET_STALL;
  360. break;
  361. }
  362. return value;
  363. }
  364. static int state_dfu_dnload_idle(struct f_dfu *f_dfu,
  365. const struct usb_ctrlrequest *ctrl,
  366. struct usb_gadget *gadget,
  367. struct usb_request *req)
  368. {
  369. u16 w_value = le16_to_cpu(ctrl->wValue);
  370. u16 len = le16_to_cpu(ctrl->wLength);
  371. int value = 0;
  372. switch (ctrl->bRequest) {
  373. case USB_REQ_DFU_DNLOAD:
  374. f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
  375. f_dfu->blk_seq_num = w_value;
  376. value = handle_dnload(gadget, len);
  377. break;
  378. case USB_REQ_DFU_ABORT:
  379. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  380. value = RET_ZLP;
  381. break;
  382. case USB_REQ_DFU_GETSTATUS:
  383. handle_getstatus(req);
  384. value = RET_STAT_LEN;
  385. break;
  386. case USB_REQ_DFU_GETSTATE:
  387. handle_getstate(req);
  388. break;
  389. default:
  390. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  391. value = RET_STALL;
  392. break;
  393. }
  394. return value;
  395. }
  396. static int state_dfu_manifest_sync(struct f_dfu *f_dfu,
  397. const struct usb_ctrlrequest *ctrl,
  398. struct usb_gadget *gadget,
  399. struct usb_request *req)
  400. {
  401. int value = 0;
  402. switch (ctrl->bRequest) {
  403. case USB_REQ_DFU_GETSTATUS:
  404. /* We're MainfestationTolerant */
  405. f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
  406. handle_getstatus(req);
  407. f_dfu->blk_seq_num = 0;
  408. value = RET_STAT_LEN;
  409. req->complete = dnload_request_flush;
  410. break;
  411. case USB_REQ_DFU_GETSTATE:
  412. handle_getstate(req);
  413. break;
  414. default:
  415. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  416. value = RET_STALL;
  417. break;
  418. }
  419. return value;
  420. }
  421. static int state_dfu_manifest(struct f_dfu *f_dfu,
  422. const struct usb_ctrlrequest *ctrl,
  423. struct usb_gadget *gadget,
  424. struct usb_request *req)
  425. {
  426. int value = 0;
  427. switch (ctrl->bRequest) {
  428. case USB_REQ_DFU_GETSTATUS:
  429. /* We're MainfestationTolerant */
  430. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  431. handle_getstatus(req);
  432. f_dfu->blk_seq_num = 0;
  433. value = RET_STAT_LEN;
  434. puts("DOWNLOAD ... OK\nCtrl+C to exit ...\n");
  435. break;
  436. case USB_REQ_DFU_GETSTATE:
  437. handle_getstate(req);
  438. break;
  439. default:
  440. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  441. value = RET_STALL;
  442. break;
  443. }
  444. return value;
  445. }
  446. static int state_dfu_upload_idle(struct f_dfu *f_dfu,
  447. const struct usb_ctrlrequest *ctrl,
  448. struct usb_gadget *gadget,
  449. struct usb_request *req)
  450. {
  451. u16 w_value = le16_to_cpu(ctrl->wValue);
  452. u16 len = le16_to_cpu(ctrl->wLength);
  453. int value = 0;
  454. switch (ctrl->bRequest) {
  455. case USB_REQ_DFU_UPLOAD:
  456. /* state transition if less data then requested */
  457. f_dfu->blk_seq_num = w_value;
  458. value = handle_upload(req, len);
  459. if (value >= 0 && value < len)
  460. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  461. break;
  462. case USB_REQ_DFU_ABORT:
  463. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  464. /* no zlp? */
  465. value = RET_ZLP;
  466. break;
  467. case USB_REQ_DFU_GETSTATUS:
  468. handle_getstatus(req);
  469. value = RET_STAT_LEN;
  470. break;
  471. case USB_REQ_DFU_GETSTATE:
  472. handle_getstate(req);
  473. break;
  474. default:
  475. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  476. value = RET_STALL;
  477. break;
  478. }
  479. return value;
  480. }
  481. static int state_dfu_error(struct f_dfu *f_dfu,
  482. const struct usb_ctrlrequest *ctrl,
  483. struct usb_gadget *gadget,
  484. struct usb_request *req)
  485. {
  486. int value = 0;
  487. switch (ctrl->bRequest) {
  488. case USB_REQ_DFU_GETSTATUS:
  489. handle_getstatus(req);
  490. value = RET_STAT_LEN;
  491. break;
  492. case USB_REQ_DFU_GETSTATE:
  493. handle_getstate(req);
  494. break;
  495. case USB_REQ_DFU_CLRSTATUS:
  496. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  497. f_dfu->dfu_status = DFU_STATUS_OK;
  498. /* no zlp? */
  499. value = RET_ZLP;
  500. break;
  501. default:
  502. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  503. value = RET_STALL;
  504. break;
  505. }
  506. return value;
  507. }
  508. static dfu_state_fn dfu_state[] = {
  509. state_app_idle, /* DFU_STATE_appIDLE */
  510. state_app_detach, /* DFU_STATE_appDETACH */
  511. state_dfu_idle, /* DFU_STATE_dfuIDLE */
  512. state_dfu_dnload_sync, /* DFU_STATE_dfuDNLOAD_SYNC */
  513. state_dfu_dnbusy, /* DFU_STATE_dfuDNBUSY */
  514. state_dfu_dnload_idle, /* DFU_STATE_dfuDNLOAD_IDLE */
  515. state_dfu_manifest_sync, /* DFU_STATE_dfuMANIFEST_SYNC */
  516. state_dfu_manifest, /* DFU_STATE_dfuMANIFEST */
  517. NULL, /* DFU_STATE_dfuMANIFEST_WAIT_RST */
  518. state_dfu_upload_idle, /* DFU_STATE_dfuUPLOAD_IDLE */
  519. state_dfu_error /* DFU_STATE_dfuERROR */
  520. };
  521. static int
  522. dfu_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  523. {
  524. struct usb_gadget *gadget = f->config->cdev->gadget;
  525. struct usb_request *req = f->config->cdev->req;
  526. struct f_dfu *f_dfu = f->config->cdev->req->context;
  527. u16 len = le16_to_cpu(ctrl->wLength);
  528. u16 w_value = le16_to_cpu(ctrl->wValue);
  529. int value = 0;
  530. u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
  531. debug("w_value: 0x%x len: 0x%x\n", w_value, len);
  532. debug("req_type: 0x%x ctrl->bRequest: 0x%x f_dfu->dfu_state: 0x%x\n",
  533. req_type, ctrl->bRequest, f_dfu->dfu_state);
  534. if (req_type == USB_TYPE_STANDARD) {
  535. if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR &&
  536. (w_value >> 8) == DFU_DT_FUNC) {
  537. value = min(len, (u16) sizeof(dfu_func));
  538. memcpy(req->buf, &dfu_func, value);
  539. }
  540. } else /* DFU specific request */
  541. value = dfu_state[f_dfu->dfu_state] (f_dfu, ctrl, gadget, req);
  542. if (value >= 0) {
  543. req->length = value;
  544. req->zero = value < len;
  545. value = usb_ep_queue(gadget->ep0, req, 0);
  546. if (value < 0) {
  547. debug("ep_queue --> %d\n", value);
  548. req->status = 0;
  549. }
  550. }
  551. return value;
  552. }
  553. /*-------------------------------------------------------------------------*/
  554. static int
  555. dfu_prepare_strings(struct f_dfu *f_dfu, int n)
  556. {
  557. struct dfu_entity *de = NULL;
  558. int i = 0;
  559. f_dfu->strings = calloc(sizeof(struct usb_string), n + 1);
  560. if (!f_dfu->strings)
  561. goto enomem;
  562. for (i = 0; i < n; ++i) {
  563. de = dfu_get_entity(i);
  564. f_dfu->strings[i].s = de->name;
  565. }
  566. f_dfu->strings[i].id = 0;
  567. f_dfu->strings[i].s = NULL;
  568. return 0;
  569. enomem:
  570. while (i)
  571. f_dfu->strings[--i].s = NULL;
  572. free(f_dfu->strings);
  573. return -ENOMEM;
  574. }
  575. static int dfu_prepare_function(struct f_dfu *f_dfu, int n)
  576. {
  577. struct usb_interface_descriptor *d;
  578. int i = 0;
  579. f_dfu->function = calloc(sizeof(struct usb_descriptor_header *), n + 1);
  580. if (!f_dfu->function)
  581. goto enomem;
  582. for (i = 0; i < n; ++i) {
  583. d = calloc(sizeof(*d), 1);
  584. if (!d)
  585. goto enomem;
  586. d->bLength = sizeof(*d);
  587. d->bDescriptorType = USB_DT_INTERFACE;
  588. d->bAlternateSetting = i;
  589. d->bNumEndpoints = 0;
  590. d->bInterfaceClass = USB_CLASS_APP_SPEC;
  591. d->bInterfaceSubClass = 1;
  592. d->bInterfaceProtocol = 2;
  593. f_dfu->function[i] = (struct usb_descriptor_header *)d;
  594. }
  595. f_dfu->function[i] = NULL;
  596. return 0;
  597. enomem:
  598. while (i) {
  599. free(f_dfu->function[--i]);
  600. f_dfu->function[i] = NULL;
  601. }
  602. free(f_dfu->function);
  603. return -ENOMEM;
  604. }
  605. static int dfu_bind(struct usb_configuration *c, struct usb_function *f)
  606. {
  607. struct usb_composite_dev *cdev = c->cdev;
  608. struct f_dfu *f_dfu = func_to_dfu(f);
  609. int alt_num = dfu_get_alt_number();
  610. int rv, id, i;
  611. id = usb_interface_id(c, f);
  612. if (id < 0)
  613. return id;
  614. dfu_intf_runtime.bInterfaceNumber = id;
  615. f_dfu->dfu_state = DFU_STATE_appIDLE;
  616. f_dfu->dfu_status = DFU_STATUS_OK;
  617. rv = dfu_prepare_function(f_dfu, alt_num);
  618. if (rv)
  619. goto error;
  620. rv = dfu_prepare_strings(f_dfu, alt_num);
  621. if (rv)
  622. goto error;
  623. for (i = 0; i < alt_num; i++) {
  624. id = usb_string_id(cdev);
  625. if (id < 0)
  626. return id;
  627. f_dfu->strings[i].id = id;
  628. ((struct usb_interface_descriptor *)f_dfu->function[i])
  629. ->iInterface = id;
  630. }
  631. to_dfu_mode(f_dfu);
  632. stringtab_dfu.strings = f_dfu->strings;
  633. cdev->req->context = f_dfu;
  634. error:
  635. return rv;
  636. }
  637. static void dfu_unbind(struct usb_configuration *c, struct usb_function *f)
  638. {
  639. struct f_dfu *f_dfu = func_to_dfu(f);
  640. int alt_num = dfu_get_alt_number();
  641. int i;
  642. if (f_dfu->strings) {
  643. i = alt_num;
  644. while (i)
  645. f_dfu->strings[--i].s = NULL;
  646. free(f_dfu->strings);
  647. }
  648. if (f_dfu->function) {
  649. i = alt_num;
  650. while (i) {
  651. free(f_dfu->function[--i]);
  652. f_dfu->function[i] = NULL;
  653. }
  654. free(f_dfu->function);
  655. }
  656. free(f_dfu);
  657. }
  658. static int dfu_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  659. {
  660. struct f_dfu *f_dfu = func_to_dfu(f);
  661. debug("%s: intf:%d alt:%d\n", __func__, intf, alt);
  662. f_dfu->altsetting = alt;
  663. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  664. f_dfu->dfu_status = DFU_STATUS_OK;
  665. return 0;
  666. }
  667. static int __dfu_get_alt(struct usb_function *f, unsigned intf)
  668. {
  669. struct f_dfu *f_dfu = func_to_dfu(f);
  670. return f_dfu->altsetting;
  671. }
  672. /* TODO: is this really what we need here? */
  673. static void dfu_disable(struct usb_function *f)
  674. {
  675. struct f_dfu *f_dfu = func_to_dfu(f);
  676. if (f_dfu->config == 0)
  677. return;
  678. debug("%s: reset config\n", __func__);
  679. f_dfu->config = 0;
  680. }
  681. static int dfu_bind_config(struct usb_configuration *c)
  682. {
  683. struct f_dfu *f_dfu;
  684. int status;
  685. f_dfu = calloc(sizeof(*f_dfu), 1);
  686. if (!f_dfu)
  687. return -ENOMEM;
  688. f_dfu->usb_function.name = "dfu";
  689. f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
  690. f_dfu->usb_function.descriptors = dfu_runtime_descs;
  691. f_dfu->usb_function.bind = dfu_bind;
  692. f_dfu->usb_function.unbind = dfu_unbind;
  693. f_dfu->usb_function.set_alt = dfu_set_alt;
  694. f_dfu->usb_function.get_alt = __dfu_get_alt;
  695. f_dfu->usb_function.disable = dfu_disable;
  696. f_dfu->usb_function.strings = dfu_generic_strings;
  697. f_dfu->usb_function.setup = dfu_handle;
  698. f_dfu->poll_timeout = DFU_DEFAULT_POLL_TIMEOUT;
  699. status = usb_add_function(c, &f_dfu->usb_function);
  700. if (status)
  701. free(f_dfu);
  702. return status;
  703. }
  704. int dfu_add(struct usb_configuration *c)
  705. {
  706. int id;
  707. id = usb_string_id(c->cdev);
  708. if (id < 0)
  709. return id;
  710. strings_dfu_generic[0].id = id;
  711. dfu_intf_runtime.iInterface = id;
  712. debug("%s: cdev: 0x%p gadget:0x%p gadget->ep0: 0x%p\n", __func__,
  713. c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
  714. return dfu_bind_config(c);
  715. }
  716. DECLARE_GADGET_BIND_CALLBACK(usb_dnl_dfu, dfu_add);