f_dfu.c 18 KB

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