f_dfu.c 17 KB

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