f_dfu.c 17 KB

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