f_dfu.c 19 KB

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