f_dfu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. 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->length, 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 void 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. }
  180. static void 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. req->actual = 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. handle_getstatus(req);
  226. value = RET_STAT_LEN;
  227. break;
  228. case USB_REQ_DFU_GETSTATE:
  229. 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. handle_getstatus(req);
  251. value = RET_STAT_LEN;
  252. break;
  253. case USB_REQ_DFU_GETSTATE:
  254. handle_getstate(req);
  255. break;
  256. default:
  257. f_dfu->dfu_state = DFU_STATE_appIDLE;
  258. value = RET_STALL;
  259. break;
  260. }
  261. return value;
  262. }
  263. static int state_dfu_idle(struct f_dfu *f_dfu,
  264. const struct usb_ctrlrequest *ctrl,
  265. struct usb_gadget *gadget,
  266. struct usb_request *req)
  267. {
  268. u16 w_value = le16_to_cpu(ctrl->wValue);
  269. u16 len = le16_to_cpu(ctrl->wLength);
  270. int value = 0;
  271. switch (ctrl->bRequest) {
  272. case USB_REQ_DFU_DNLOAD:
  273. if (len == 0) {
  274. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  275. value = RET_STALL;
  276. break;
  277. }
  278. f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
  279. f_dfu->blk_seq_num = w_value;
  280. value = handle_dnload(gadget, len);
  281. break;
  282. case USB_REQ_DFU_UPLOAD:
  283. f_dfu->dfu_state = DFU_STATE_dfuUPLOAD_IDLE;
  284. f_dfu->blk_seq_num = 0;
  285. value = handle_upload(req, len);
  286. break;
  287. case USB_REQ_DFU_ABORT:
  288. /* no zlp? */
  289. value = RET_ZLP;
  290. break;
  291. case USB_REQ_DFU_GETSTATUS:
  292. handle_getstatus(req);
  293. value = RET_STAT_LEN;
  294. break;
  295. case USB_REQ_DFU_GETSTATE:
  296. handle_getstate(req);
  297. break;
  298. case USB_REQ_DFU_DETACH:
  299. /*
  300. * Proprietary extension: 'detach' from idle mode and
  301. * get back to runtime mode in case of USB Reset. As
  302. * much as I dislike this, we just can't use every USB
  303. * bus reset to switch back to runtime mode, since at
  304. * least the Linux USB stack likes to send a number of
  305. * resets in a row :(
  306. */
  307. f_dfu->dfu_state =
  308. DFU_STATE_dfuMANIFEST_WAIT_RST;
  309. to_runtime_mode(f_dfu);
  310. f_dfu->dfu_state = DFU_STATE_appIDLE;
  311. g_dnl_trigger_detach();
  312. break;
  313. default:
  314. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  315. value = RET_STALL;
  316. break;
  317. }
  318. return value;
  319. }
  320. static int state_dfu_dnload_sync(struct f_dfu *f_dfu,
  321. const struct usb_ctrlrequest *ctrl,
  322. struct usb_gadget *gadget,
  323. struct usb_request *req)
  324. {
  325. int value = 0;
  326. switch (ctrl->bRequest) {
  327. case USB_REQ_DFU_GETSTATUS:
  328. handle_getstatus(req);
  329. value = RET_STAT_LEN;
  330. break;
  331. case USB_REQ_DFU_GETSTATE:
  332. handle_getstate(req);
  333. break;
  334. default:
  335. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  336. value = RET_STALL;
  337. break;
  338. }
  339. return value;
  340. }
  341. static int state_dfu_dnbusy(struct f_dfu *f_dfu,
  342. const struct usb_ctrlrequest *ctrl,
  343. struct usb_gadget *gadget,
  344. struct usb_request *req)
  345. {
  346. int value = 0;
  347. switch (ctrl->bRequest) {
  348. case USB_REQ_DFU_GETSTATUS:
  349. handle_getstatus(req);
  350. value = RET_STAT_LEN;
  351. break;
  352. default:
  353. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  354. value = RET_STALL;
  355. break;
  356. }
  357. return value;
  358. }
  359. static int state_dfu_dnload_idle(struct f_dfu *f_dfu,
  360. const struct usb_ctrlrequest *ctrl,
  361. struct usb_gadget *gadget,
  362. struct usb_request *req)
  363. {
  364. u16 w_value = le16_to_cpu(ctrl->wValue);
  365. u16 len = le16_to_cpu(ctrl->wLength);
  366. int value = 0;
  367. switch (ctrl->bRequest) {
  368. case USB_REQ_DFU_DNLOAD:
  369. f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
  370. f_dfu->blk_seq_num = w_value;
  371. value = handle_dnload(gadget, len);
  372. break;
  373. case USB_REQ_DFU_ABORT:
  374. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  375. value = RET_ZLP;
  376. break;
  377. case USB_REQ_DFU_GETSTATUS:
  378. handle_getstatus(req);
  379. value = RET_STAT_LEN;
  380. break;
  381. case USB_REQ_DFU_GETSTATE:
  382. handle_getstate(req);
  383. break;
  384. default:
  385. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  386. value = RET_STALL;
  387. break;
  388. }
  389. return value;
  390. }
  391. static int state_dfu_manifest_sync(struct f_dfu *f_dfu,
  392. const struct usb_ctrlrequest *ctrl,
  393. struct usb_gadget *gadget,
  394. struct usb_request *req)
  395. {
  396. int value = 0;
  397. switch (ctrl->bRequest) {
  398. case USB_REQ_DFU_GETSTATUS:
  399. /* We're MainfestationTolerant */
  400. f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
  401. handle_getstatus(req);
  402. f_dfu->blk_seq_num = 0;
  403. value = RET_STAT_LEN;
  404. req->complete = dnload_request_flush;
  405. break;
  406. case USB_REQ_DFU_GETSTATE:
  407. handle_getstate(req);
  408. break;
  409. default:
  410. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  411. value = RET_STALL;
  412. break;
  413. }
  414. return value;
  415. }
  416. static int state_dfu_manifest(struct f_dfu *f_dfu,
  417. const struct usb_ctrlrequest *ctrl,
  418. struct usb_gadget *gadget,
  419. struct usb_request *req)
  420. {
  421. int value = 0;
  422. switch (ctrl->bRequest) {
  423. case USB_REQ_DFU_GETSTATUS:
  424. /* We're MainfestationTolerant */
  425. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  426. handle_getstatus(req);
  427. f_dfu->blk_seq_num = 0;
  428. value = RET_STAT_LEN;
  429. puts("DOWNLOAD ... OK\nCtrl+C to exit ...\n");
  430. break;
  431. case USB_REQ_DFU_GETSTATE:
  432. handle_getstate(req);
  433. break;
  434. default:
  435. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  436. value = RET_STALL;
  437. break;
  438. }
  439. return value;
  440. }
  441. static int state_dfu_upload_idle(struct f_dfu *f_dfu,
  442. const struct usb_ctrlrequest *ctrl,
  443. struct usb_gadget *gadget,
  444. struct usb_request *req)
  445. {
  446. u16 w_value = le16_to_cpu(ctrl->wValue);
  447. u16 len = le16_to_cpu(ctrl->wLength);
  448. int value = 0;
  449. switch (ctrl->bRequest) {
  450. case USB_REQ_DFU_UPLOAD:
  451. /* state transition if less data then requested */
  452. f_dfu->blk_seq_num = w_value;
  453. value = handle_upload(req, len);
  454. if (value >= 0 && value < len)
  455. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  456. break;
  457. case USB_REQ_DFU_ABORT:
  458. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  459. /* no zlp? */
  460. value = RET_ZLP;
  461. break;
  462. case USB_REQ_DFU_GETSTATUS:
  463. handle_getstatus(req);
  464. value = RET_STAT_LEN;
  465. break;
  466. case USB_REQ_DFU_GETSTATE:
  467. handle_getstate(req);
  468. break;
  469. default:
  470. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  471. value = RET_STALL;
  472. break;
  473. }
  474. return value;
  475. }
  476. static int state_dfu_error(struct f_dfu *f_dfu,
  477. const struct usb_ctrlrequest *ctrl,
  478. struct usb_gadget *gadget,
  479. struct usb_request *req)
  480. {
  481. int value = 0;
  482. switch (ctrl->bRequest) {
  483. case USB_REQ_DFU_GETSTATUS:
  484. handle_getstatus(req);
  485. value = RET_STAT_LEN;
  486. break;
  487. case USB_REQ_DFU_GETSTATE:
  488. handle_getstate(req);
  489. break;
  490. case USB_REQ_DFU_CLRSTATUS:
  491. f_dfu->dfu_state = DFU_STATE_dfuIDLE;
  492. f_dfu->dfu_status = DFU_STATUS_OK;
  493. /* no zlp? */
  494. value = RET_ZLP;
  495. break;
  496. default:
  497. f_dfu->dfu_state = DFU_STATE_dfuERROR;
  498. value = RET_STALL;
  499. break;
  500. }
  501. return value;
  502. }
  503. static dfu_state_fn dfu_state[] = {
  504. state_app_idle, /* DFU_STATE_appIDLE */
  505. state_app_detach, /* DFU_STATE_appDETACH */
  506. state_dfu_idle, /* DFU_STATE_dfuIDLE */
  507. state_dfu_dnload_sync, /* DFU_STATE_dfuDNLOAD_SYNC */
  508. state_dfu_dnbusy, /* DFU_STATE_dfuDNBUSY */
  509. state_dfu_dnload_idle, /* DFU_STATE_dfuDNLOAD_IDLE */
  510. state_dfu_manifest_sync, /* DFU_STATE_dfuMANIFEST_SYNC */
  511. state_dfu_manifest, /* DFU_STATE_dfuMANIFEST */
  512. NULL, /* DFU_STATE_dfuMANIFEST_WAIT_RST */
  513. state_dfu_upload_idle, /* DFU_STATE_dfuUPLOAD_IDLE */
  514. state_dfu_error /* DFU_STATE_dfuERROR */
  515. };
  516. static int
  517. dfu_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  518. {
  519. struct usb_gadget *gadget = f->config->cdev->gadget;
  520. struct usb_request *req = f->config->cdev->req;
  521. struct f_dfu *f_dfu = f->config->cdev->req->context;
  522. u16 len = le16_to_cpu(ctrl->wLength);
  523. u16 w_value = le16_to_cpu(ctrl->wValue);
  524. int value = 0;
  525. u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
  526. debug("w_value: 0x%x len: 0x%x\n", w_value, len);
  527. debug("req_type: 0x%x ctrl->bRequest: 0x%x f_dfu->dfu_state: 0x%x\n",
  528. req_type, ctrl->bRequest, f_dfu->dfu_state);
  529. if (req_type == USB_TYPE_STANDARD) {
  530. if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR &&
  531. (w_value >> 8) == DFU_DT_FUNC) {
  532. value = min(len, (u16) sizeof(dfu_func));
  533. memcpy(req->buf, &dfu_func, value);
  534. }
  535. } else /* DFU specific request */
  536. value = dfu_state[f_dfu->dfu_state] (f_dfu, ctrl, gadget, req);
  537. if (value >= 0) {
  538. req->length = value;
  539. req->zero = value < len;
  540. value = usb_ep_queue(gadget->ep0, req, 0);
  541. if (value < 0) {
  542. debug("ep_queue --> %d\n", value);
  543. req->status = 0;
  544. }
  545. }
  546. return value;
  547. }
  548. /*-------------------------------------------------------------------------*/
  549. static int
  550. dfu_prepare_strings(struct f_dfu *f_dfu, int n)
  551. {
  552. struct dfu_entity *de = NULL;
  553. int i = 0;
  554. f_dfu->strings = calloc(sizeof(struct usb_string), n + 1);
  555. if (!f_dfu->strings)
  556. return -ENOMEM;
  557. for (i = 0; i < n; ++i) {
  558. de = dfu_get_entity(i);
  559. f_dfu->strings[i].s = de->name;
  560. }
  561. f_dfu->strings[i].id = 0;
  562. f_dfu->strings[i].s = NULL;
  563. return 0;
  564. }
  565. static int dfu_prepare_function(struct f_dfu *f_dfu, int n)
  566. {
  567. struct usb_interface_descriptor *d;
  568. int i = 0;
  569. f_dfu->function = calloc(sizeof(struct usb_descriptor_header *), n + 1);
  570. if (!f_dfu->function)
  571. goto enomem;
  572. for (i = 0; i < n; ++i) {
  573. d = calloc(sizeof(*d), 1);
  574. if (!d)
  575. goto enomem;
  576. d->bLength = sizeof(*d);
  577. d->bDescriptorType = USB_DT_INTERFACE;
  578. d->bAlternateSetting = i;
  579. d->bNumEndpoints = 0;
  580. d->bInterfaceClass = USB_CLASS_APP_SPEC;
  581. d->bInterfaceSubClass = 1;
  582. d->bInterfaceProtocol = 2;
  583. f_dfu->function[i] = (struct usb_descriptor_header *)d;
  584. }
  585. f_dfu->function[i] = NULL;
  586. return 0;
  587. enomem:
  588. while (i) {
  589. free(f_dfu->function[--i]);
  590. f_dfu->function[i] = NULL;
  591. }
  592. free(f_dfu->function);
  593. return -ENOMEM;
  594. }
  595. static int dfu_bind(struct usb_configuration *c, struct usb_function *f)
  596. {
  597. struct usb_composite_dev *cdev = c->cdev;
  598. struct f_dfu *f_dfu = func_to_dfu(f);
  599. int alt_num = dfu_get_alt_number();
  600. int rv, id, i;
  601. id = usb_interface_id(c, f);
  602. if (id < 0)
  603. return id;
  604. dfu_intf_runtime.bInterfaceNumber = id;
  605. f_dfu->dfu_state = DFU_STATE_appIDLE;
  606. f_dfu->dfu_status = DFU_STATUS_OK;
  607. rv = dfu_prepare_function(f_dfu, alt_num);
  608. if (rv)
  609. goto error;
  610. rv = dfu_prepare_strings(f_dfu, alt_num);
  611. if (rv)
  612. goto error;
  613. for (i = 0; i < alt_num; i++) {
  614. id = usb_string_id(cdev);
  615. if (id < 0)
  616. return id;
  617. f_dfu->strings[i].id = id;
  618. ((struct usb_interface_descriptor *)f_dfu->function[i])
  619. ->iInterface = id;
  620. }
  621. to_dfu_mode(f_dfu);
  622. stringtab_dfu.strings = f_dfu->strings;
  623. cdev->req->context = f_dfu;
  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);