f_dfu.c 17 KB

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