f_dfu.c 18 KB

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