f_sdp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * f_sdp.c -- USB HID Serial Download Protocol
  3. *
  4. * Copyright (C) 2017 Toradex
  5. * Author: Stefan Agner <stefan.agner@toradex.com>
  6. *
  7. * This file implements the Serial Download Protocol (SDP) as specified in
  8. * the i.MX 6 Reference Manual. The SDP is a USB HID based protocol and
  9. * allows to download images directly to memory. The implementation
  10. * works with the imx_loader (imx_usb) USB client software on host side.
  11. *
  12. * Not all commands are implemented, e.g. WRITE_REGISTER, DCD_WRITE and
  13. * SKIP_DCD_HEADER are only stubs.
  14. *
  15. * Parts of the implementation are based on f_dfu and f_thor.
  16. *
  17. * SPDX-License-Identifier: GPL-2.0+
  18. */
  19. #include <errno.h>
  20. #include <common.h>
  21. #include <console.h>
  22. #include <malloc.h>
  23. #include <linux/usb/ch9.h>
  24. #include <linux/usb/gadget.h>
  25. #include <linux/usb/composite.h>
  26. #include <asm/io.h>
  27. #include <g_dnl.h>
  28. #include <sdp.h>
  29. #include <spl.h>
  30. #include <image.h>
  31. #include <imximage.h>
  32. #define HID_REPORT_ID_MASK 0x000000ff
  33. /*
  34. * HID class requests
  35. */
  36. #define HID_REQ_GET_REPORT 0x01
  37. #define HID_REQ_GET_IDLE 0x02
  38. #define HID_REQ_GET_PROTOCOL 0x03
  39. #define HID_REQ_SET_REPORT 0x09
  40. #define HID_REQ_SET_IDLE 0x0A
  41. #define HID_REQ_SET_PROTOCOL 0x0B
  42. #define HID_USAGE_PAGE_LEN 76
  43. struct hid_report {
  44. u8 usage_page[HID_USAGE_PAGE_LEN];
  45. } __packed;
  46. #define SDP_READ_REGISTER 0x0101
  47. #define SDP_WRITE_REGISTER 0x0202
  48. #define SDP_WRITE_FILE 0x0404
  49. #define SDP_ERROR_STATUS 0x0505
  50. #define SDP_DCD_WRITE 0x0a0a
  51. #define SDP_JUMP_ADDRESS 0x0b0b
  52. #define SDP_SKIP_DCD_HEADER 0x0c0c
  53. #define SDP_SECURITY_CLOSED 0x12343412
  54. #define SDP_SECURITY_OPEN 0x56787856
  55. #define SDP_WRITE_FILE_COMPLETE 0x88888888
  56. #define SDP_WRITE_REGISTER_COMPLETE 0x128A8A12
  57. #define SDP_SKIP_DCD_HEADER_COMPLETE 0x900DD009
  58. #define SDP_ERROR_IMXHEADER 0x000a0533
  59. #define SDP_COMMAND_LEN 16
  60. struct sdp_command {
  61. u16 cmd;
  62. u32 addr;
  63. u8 format;
  64. u32 cnt;
  65. u32 data;
  66. u8 rsvd;
  67. } __packed;
  68. enum sdp_state {
  69. SDP_STATE_IDLE,
  70. SDP_STATE_RX_DCD_DATA,
  71. SDP_STATE_RX_FILE_DATA,
  72. SDP_STATE_TX_SEC_CONF,
  73. SDP_STATE_TX_SEC_CONF_BUSY,
  74. SDP_STATE_TX_REGISTER,
  75. SDP_STATE_TX_REGISTER_BUSY,
  76. SDP_STATE_TX_STATUS,
  77. SDP_STATE_TX_STATUS_BUSY,
  78. SDP_STATE_JUMP,
  79. };
  80. struct f_sdp {
  81. struct usb_function usb_function;
  82. struct usb_descriptor_header **function;
  83. u8 altsetting;
  84. enum sdp_state state;
  85. enum sdp_state next_state;
  86. u32 dnl_address;
  87. u32 dnl_bytes_remaining;
  88. u32 jmp_address;
  89. bool always_send_status;
  90. u32 error_status;
  91. /* EP0 request */
  92. struct usb_request *req;
  93. /* EP1 IN */
  94. struct usb_ep *in_ep;
  95. struct usb_request *in_req;
  96. bool configuration_done;
  97. };
  98. static struct f_sdp *sdp_func;
  99. static inline struct f_sdp *func_to_sdp(struct usb_function *f)
  100. {
  101. return container_of(f, struct f_sdp, usb_function);
  102. }
  103. static struct usb_interface_descriptor sdp_intf_runtime = {
  104. .bLength = sizeof(sdp_intf_runtime),
  105. .bDescriptorType = USB_DT_INTERFACE,
  106. .bAlternateSetting = 0,
  107. .bNumEndpoints = 1,
  108. .bInterfaceClass = USB_CLASS_HID,
  109. .bInterfaceSubClass = 0,
  110. .bInterfaceProtocol = 0,
  111. /* .iInterface = DYNAMIC */
  112. };
  113. /* HID configuration */
  114. static struct usb_class_hid_descriptor sdp_hid_desc = {
  115. .bLength = sizeof(sdp_hid_desc),
  116. .bDescriptorType = USB_DT_CS_DEVICE,
  117. .bcdCDC = __constant_cpu_to_le16(0x0110),
  118. .bCountryCode = 0,
  119. .bNumDescriptors = 1,
  120. .bDescriptorType0 = USB_DT_HID_REPORT,
  121. .wDescriptorLength0 = HID_USAGE_PAGE_LEN,
  122. };
  123. static struct usb_endpoint_descriptor in_desc = {
  124. .bLength = USB_DT_ENDPOINT_SIZE,
  125. .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
  126. .bEndpointAddress = 1 | USB_DIR_IN,
  127. .bmAttributes = USB_ENDPOINT_XFER_INT,
  128. .wMaxPacketSize = 64,
  129. .bInterval = 1,
  130. };
  131. static struct usb_descriptor_header *sdp_runtime_descs[] = {
  132. (struct usb_descriptor_header *)&sdp_intf_runtime,
  133. (struct usb_descriptor_header *)&sdp_hid_desc,
  134. (struct usb_descriptor_header *)&in_desc,
  135. NULL,
  136. };
  137. /* This is synchronized with what the SoC implementation reports */
  138. static struct hid_report sdp_hid_report = {
  139. .usage_page = {
  140. 0x06, 0x00, 0xff, /* Usage Page */
  141. 0x09, 0x01, /* Usage (Pointer?) */
  142. 0xa1, 0x01, /* Collection */
  143. 0x85, 0x01, /* Report ID */
  144. 0x19, 0x01, /* Usage Minimum */
  145. 0x29, 0x01, /* Usage Maximum */
  146. 0x15, 0x00, /* Local Minimum */
  147. 0x26, 0xFF, 0x00, /* Local Maximum? */
  148. 0x75, 0x08, /* Report Size */
  149. 0x95, 0x10, /* Report Count */
  150. 0x91, 0x02, /* Output Data */
  151. 0x85, 0x02, /* Report ID */
  152. 0x19, 0x01, /* Usage Minimum */
  153. 0x29, 0x01, /* Usage Maximum */
  154. 0x15, 0x00, /* Local Minimum */
  155. 0x26, 0xFF, 0x00, /* Local Maximum? */
  156. 0x75, 0x80, /* Report Size 128 */
  157. 0x95, 0x40, /* Report Count */
  158. 0x91, 0x02, /* Output Data */
  159. 0x85, 0x03, /* Report ID */
  160. 0x19, 0x01, /* Usage Minimum */
  161. 0x29, 0x01, /* Usage Maximum */
  162. 0x15, 0x00, /* Local Minimum */
  163. 0x26, 0xFF, 0x00, /* Local Maximum? */
  164. 0x75, 0x08, /* Report Size 8 */
  165. 0x95, 0x04, /* Report Count */
  166. 0x81, 0x02, /* Input Data */
  167. 0x85, 0x04, /* Report ID */
  168. 0x19, 0x01, /* Usage Minimum */
  169. 0x29, 0x01, /* Usage Maximum */
  170. 0x15, 0x00, /* Local Minimum */
  171. 0x26, 0xFF, 0x00, /* Local Maximum? */
  172. 0x75, 0x08, /* Report Size 8 */
  173. 0x95, 0x40, /* Report Count */
  174. 0x81, 0x02, /* Input Data */
  175. 0xc0
  176. },
  177. };
  178. static const char sdp_name[] = "Serial Downloader Protocol";
  179. /*
  180. * static strings, in UTF-8
  181. */
  182. static struct usb_string strings_sdp_generic[] = {
  183. [0].s = sdp_name,
  184. { } /* end of list */
  185. };
  186. static struct usb_gadget_strings stringtab_sdp_generic = {
  187. .language = 0x0409, /* en-us */
  188. .strings = strings_sdp_generic,
  189. };
  190. static struct usb_gadget_strings *sdp_generic_strings[] = {
  191. &stringtab_sdp_generic,
  192. NULL,
  193. };
  194. static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req)
  195. {
  196. struct f_sdp *sdp = req->context;
  197. int status = req->status;
  198. u8 *data = req->buf;
  199. u8 report = data[0];
  200. if (status != 0) {
  201. error("Status: %d", status);
  202. return;
  203. }
  204. if (report != 1) {
  205. error("Unexpected report %d", report);
  206. return;
  207. }
  208. struct sdp_command *cmd = req->buf + 1;
  209. debug("%s: command: %04x, addr: %08x, cnt: %u\n",
  210. __func__, be16_to_cpu(cmd->cmd),
  211. be32_to_cpu(cmd->addr), be32_to_cpu(cmd->cnt));
  212. switch (be16_to_cpu(cmd->cmd)) {
  213. case SDP_READ_REGISTER:
  214. sdp->always_send_status = false;
  215. sdp->error_status = 0x0;
  216. sdp->state = SDP_STATE_TX_SEC_CONF;
  217. sdp->dnl_address = be32_to_cpu(cmd->addr);
  218. sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
  219. sdp->next_state = SDP_STATE_TX_REGISTER;
  220. printf("Reading %d registers at 0x%08x... ",
  221. sdp->dnl_bytes_remaining, sdp->dnl_address);
  222. break;
  223. case SDP_WRITE_FILE:
  224. sdp->always_send_status = true;
  225. sdp->error_status = SDP_WRITE_FILE_COMPLETE;
  226. sdp->state = SDP_STATE_RX_FILE_DATA;
  227. sdp->dnl_address = be32_to_cpu(cmd->addr);
  228. sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
  229. sdp->next_state = SDP_STATE_IDLE;
  230. printf("Downloading file of size %d to 0x%08x... ",
  231. sdp->dnl_bytes_remaining, sdp->dnl_address);
  232. break;
  233. case SDP_ERROR_STATUS:
  234. sdp->always_send_status = true;
  235. sdp->error_status = 0;
  236. sdp->state = SDP_STATE_TX_SEC_CONF;
  237. sdp->next_state = SDP_STATE_IDLE;
  238. break;
  239. case SDP_DCD_WRITE:
  240. sdp->always_send_status = true;
  241. sdp->error_status = SDP_WRITE_REGISTER_COMPLETE;
  242. sdp->state = SDP_STATE_RX_DCD_DATA;
  243. sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
  244. sdp->next_state = SDP_STATE_IDLE;
  245. break;
  246. case SDP_JUMP_ADDRESS:
  247. sdp->always_send_status = false;
  248. sdp->error_status = 0;
  249. sdp->jmp_address = be32_to_cpu(cmd->addr);
  250. sdp->state = SDP_STATE_TX_SEC_CONF;
  251. sdp->next_state = SDP_STATE_JUMP;
  252. break;
  253. case SDP_SKIP_DCD_HEADER:
  254. sdp->always_send_status = true;
  255. sdp->error_status = SDP_SKIP_DCD_HEADER_COMPLETE;
  256. /* Ignore command, DCD not supported anyway */
  257. sdp->state = SDP_STATE_TX_SEC_CONF;
  258. sdp->next_state = SDP_STATE_IDLE;
  259. break;
  260. default:
  261. error("Unknown command: %04x\n", be16_to_cpu(cmd->cmd));
  262. }
  263. }
  264. static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req)
  265. {
  266. struct f_sdp *sdp = req->context;
  267. int status = req->status;
  268. u8 *data = req->buf;
  269. u8 report = data[0];
  270. int datalen = req->length - 1;
  271. if (status != 0) {
  272. error("Status: %d", status);
  273. return;
  274. }
  275. if (report != 2) {
  276. error("Unexpected report %d", report);
  277. return;
  278. }
  279. if (sdp->dnl_bytes_remaining < datalen) {
  280. /*
  281. * Some USB stacks require to send a complete buffer as
  282. * specified in the HID descriptor. This leads to longer
  283. * transfers than the file length, no problem for us.
  284. */
  285. sdp->dnl_bytes_remaining = 0;
  286. } else {
  287. sdp->dnl_bytes_remaining -= datalen;
  288. }
  289. if (sdp->state == SDP_STATE_RX_FILE_DATA) {
  290. memcpy((void *)sdp->dnl_address, req->buf + 1, datalen);
  291. sdp->dnl_address += datalen;
  292. }
  293. if (sdp->dnl_bytes_remaining)
  294. return;
  295. printf("done\n");
  296. switch (sdp->state) {
  297. case SDP_STATE_RX_FILE_DATA:
  298. sdp->state = SDP_STATE_TX_SEC_CONF;
  299. break;
  300. case SDP_STATE_RX_DCD_DATA:
  301. sdp->state = SDP_STATE_TX_SEC_CONF;
  302. break;
  303. default:
  304. error("Invalid state: %d", sdp->state);
  305. }
  306. }
  307. static void sdp_tx_complete(struct usb_ep *ep, struct usb_request *req)
  308. {
  309. struct f_sdp *sdp = req->context;
  310. int status = req->status;
  311. if (status != 0) {
  312. error("Status: %d", status);
  313. return;
  314. }
  315. switch (sdp->state) {
  316. case SDP_STATE_TX_SEC_CONF_BUSY:
  317. /* Not all commands require status report */
  318. if (sdp->always_send_status || sdp->error_status)
  319. sdp->state = SDP_STATE_TX_STATUS;
  320. else
  321. sdp->state = sdp->next_state;
  322. break;
  323. case SDP_STATE_TX_STATUS_BUSY:
  324. sdp->state = sdp->next_state;
  325. break;
  326. case SDP_STATE_TX_REGISTER_BUSY:
  327. if (sdp->dnl_bytes_remaining)
  328. sdp->state = SDP_STATE_TX_REGISTER;
  329. else
  330. sdp->state = SDP_STATE_IDLE;
  331. break;
  332. default:
  333. error("Wrong State: %d", sdp->state);
  334. sdp->state = SDP_STATE_IDLE;
  335. break;
  336. }
  337. debug("%s complete --> %d, %d/%d\n", ep->name,
  338. status, req->actual, req->length);
  339. }
  340. static int sdp_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  341. {
  342. struct usb_gadget *gadget = f->config->cdev->gadget;
  343. struct usb_request *req = f->config->cdev->req;
  344. struct f_sdp *sdp = f->config->cdev->req->context;
  345. u16 len = le16_to_cpu(ctrl->wLength);
  346. u16 w_value = le16_to_cpu(ctrl->wValue);
  347. int value = 0;
  348. u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
  349. debug("w_value: 0x%04x len: 0x%04x\n", w_value, len);
  350. debug("req_type: 0x%02x ctrl->bRequest: 0x%02x sdp->state: %d\n",
  351. req_type, ctrl->bRequest, sdp->state);
  352. if (req_type == USB_TYPE_STANDARD) {
  353. if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR) {
  354. /* Send HID report descriptor */
  355. value = min(len, (u16) sizeof(sdp_hid_report));
  356. memcpy(req->buf, &sdp_hid_report, value);
  357. sdp->configuration_done = true;
  358. }
  359. }
  360. if (req_type == USB_TYPE_CLASS) {
  361. int report = w_value & HID_REPORT_ID_MASK;
  362. /* HID (SDP) request */
  363. switch (ctrl->bRequest) {
  364. case HID_REQ_SET_REPORT:
  365. switch (report) {
  366. case 1:
  367. value = SDP_COMMAND_LEN + 1;
  368. req->complete = sdp_rx_command_complete;
  369. break;
  370. case 2:
  371. value = len;
  372. req->complete = sdp_rx_data_complete;
  373. break;
  374. }
  375. }
  376. }
  377. if (value >= 0) {
  378. req->length = value;
  379. req->zero = value < len;
  380. value = usb_ep_queue(gadget->ep0, req, 0);
  381. if (value < 0) {
  382. debug("ep_queue --> %d\n", value);
  383. req->status = 0;
  384. }
  385. }
  386. return value;
  387. }
  388. static int sdp_bind(struct usb_configuration *c, struct usb_function *f)
  389. {
  390. struct usb_gadget *gadget = c->cdev->gadget;
  391. struct usb_composite_dev *cdev = c->cdev;
  392. struct f_sdp *sdp = func_to_sdp(f);
  393. int rv = 0, id;
  394. id = usb_interface_id(c, f);
  395. if (id < 0)
  396. return id;
  397. sdp_intf_runtime.bInterfaceNumber = id;
  398. struct usb_ep *ep;
  399. /* allocate instance-specific endpoints */
  400. ep = usb_ep_autoconfig(gadget, &in_desc);
  401. if (!ep) {
  402. rv = -ENODEV;
  403. goto error;
  404. }
  405. sdp->in_ep = ep; /* Store IN EP for enabling @ setup */
  406. cdev->req->context = sdp;
  407. error:
  408. return rv;
  409. }
  410. static void sdp_unbind(struct usb_configuration *c, struct usb_function *f)
  411. {
  412. free(sdp_func);
  413. sdp_func = NULL;
  414. }
  415. static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
  416. {
  417. struct usb_request *req;
  418. req = usb_ep_alloc_request(ep, 0);
  419. if (!req)
  420. return req;
  421. req->length = length;
  422. req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
  423. if (!req->buf) {
  424. usb_ep_free_request(ep, req);
  425. req = NULL;
  426. }
  427. return req;
  428. }
  429. static struct usb_request *sdp_start_ep(struct usb_ep *ep)
  430. {
  431. struct usb_request *req;
  432. req = alloc_ep_req(ep, 64);
  433. debug("%s: ep:%p req:%p\n", __func__, ep, req);
  434. if (!req)
  435. return NULL;
  436. memset(req->buf, 0, req->length);
  437. req->complete = sdp_tx_complete;
  438. return req;
  439. }
  440. static int sdp_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  441. {
  442. struct f_sdp *sdp = func_to_sdp(f);
  443. struct usb_composite_dev *cdev = f->config->cdev;
  444. int result;
  445. debug("%s: intf: %d alt: %d\n", __func__, intf, alt);
  446. result = usb_ep_enable(sdp->in_ep, &in_desc);
  447. if (result)
  448. return result;
  449. sdp->in_req = sdp_start_ep(sdp->in_ep);
  450. sdp->in_req->context = sdp;
  451. sdp->in_ep->driver_data = cdev; /* claim */
  452. sdp->altsetting = alt;
  453. sdp->state = SDP_STATE_IDLE;
  454. return 0;
  455. }
  456. static int sdp_get_alt(struct usb_function *f, unsigned intf)
  457. {
  458. struct f_sdp *sdp = func_to_sdp(f);
  459. return sdp->altsetting;
  460. }
  461. static void sdp_disable(struct usb_function *f)
  462. {
  463. struct f_sdp *sdp = func_to_sdp(f);
  464. usb_ep_disable(sdp->in_ep);
  465. if (sdp->in_req) {
  466. free(sdp->in_req);
  467. sdp->in_req = NULL;
  468. }
  469. }
  470. static int sdp_bind_config(struct usb_configuration *c)
  471. {
  472. int status;
  473. if (!sdp_func) {
  474. sdp_func = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*sdp_func));
  475. if (!sdp_func)
  476. return -ENOMEM;
  477. }
  478. memset(sdp_func, 0, sizeof(*sdp_func));
  479. sdp_func->usb_function.name = "sdp";
  480. sdp_func->usb_function.hs_descriptors = sdp_runtime_descs;
  481. sdp_func->usb_function.descriptors = sdp_runtime_descs;
  482. sdp_func->usb_function.bind = sdp_bind;
  483. sdp_func->usb_function.unbind = sdp_unbind;
  484. sdp_func->usb_function.set_alt = sdp_set_alt;
  485. sdp_func->usb_function.get_alt = sdp_get_alt;
  486. sdp_func->usb_function.disable = sdp_disable;
  487. sdp_func->usb_function.strings = sdp_generic_strings;
  488. sdp_func->usb_function.setup = sdp_setup;
  489. status = usb_add_function(c, &sdp_func->usb_function);
  490. return status;
  491. }
  492. int sdp_init(int controller_index)
  493. {
  494. printf("SDP: initialize...\n");
  495. while (!sdp_func->configuration_done) {
  496. if (ctrlc()) {
  497. puts("\rCTRL+C - Operation aborted.\n");
  498. return 1;
  499. }
  500. usb_gadget_handle_interrupts(controller_index);
  501. }
  502. return 0;
  503. }
  504. static u32 sdp_jump_imxheader(void *address)
  505. {
  506. flash_header_v2_t *headerv2 = address;
  507. ulong (*entry)(void);
  508. if (headerv2->header.tag != IVT_HEADER_TAG) {
  509. printf("Header Tag is not an IMX image\n");
  510. return SDP_ERROR_IMXHEADER;
  511. }
  512. printf("Jumping to 0x%08x\n", headerv2->entry);
  513. entry = (void *)headerv2->entry;
  514. entry();
  515. /* The image probably never returns hence we won't reach that point */
  516. return 0;
  517. }
  518. static void sdp_handle_in_ep(void)
  519. {
  520. u8 *data = sdp_func->in_req->buf;
  521. u32 status;
  522. int datalen;
  523. switch (sdp_func->state) {
  524. case SDP_STATE_TX_SEC_CONF:
  525. debug("Report 3: HAB security\n");
  526. data[0] = 3;
  527. status = SDP_SECURITY_OPEN;
  528. memcpy(&data[1], &status, 4);
  529. sdp_func->in_req->length = 5;
  530. usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
  531. sdp_func->state = SDP_STATE_TX_SEC_CONF_BUSY;
  532. break;
  533. case SDP_STATE_TX_STATUS:
  534. debug("Report 4: Status\n");
  535. data[0] = 4;
  536. memcpy(&data[1], &sdp_func->error_status, 4);
  537. sdp_func->in_req->length = 65;
  538. usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
  539. sdp_func->state = SDP_STATE_TX_STATUS_BUSY;
  540. break;
  541. case SDP_STATE_TX_REGISTER:
  542. debug("Report 4: Register Values\n");
  543. data[0] = 4;
  544. datalen = sdp_func->dnl_bytes_remaining;
  545. if (datalen > 64)
  546. datalen = 64;
  547. memcpy(&data[1], (void *)sdp_func->dnl_address, datalen);
  548. sdp_func->in_req->length = 65;
  549. sdp_func->dnl_bytes_remaining -= datalen;
  550. sdp_func->dnl_address += datalen;
  551. usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
  552. sdp_func->state = SDP_STATE_TX_REGISTER_BUSY;
  553. break;
  554. case SDP_STATE_JUMP:
  555. printf("Jumping to header at 0x%08x\n", sdp_func->jmp_address);
  556. status = sdp_jump_imxheader((void *)sdp_func->jmp_address);
  557. /* If imx header fails, try some U-Boot specific headers */
  558. if (status) {
  559. #ifdef CONFIG_SPL_BUILD
  560. /* In SPL, allow jumps to U-Boot images */
  561. struct spl_image_info spl_image = {};
  562. spl_parse_image_header(&spl_image,
  563. (struct image_header *)sdp_func->jmp_address);
  564. jump_to_image_no_args(&spl_image);
  565. #else
  566. /* In U-Boot, allow jumps to scripts */
  567. source(sdp_func->jmp_address, "script@1");
  568. #endif
  569. }
  570. sdp_func->next_state = SDP_STATE_IDLE;
  571. sdp_func->error_status = status;
  572. /* Only send Report 4 if there was an error */
  573. if (status)
  574. sdp_func->state = SDP_STATE_TX_STATUS;
  575. else
  576. sdp_func->state = SDP_STATE_IDLE;
  577. break;
  578. default:
  579. break;
  580. };
  581. }
  582. void sdp_handle(int controller_index)
  583. {
  584. printf("SDP: handle requests...\n");
  585. while (1) {
  586. if (ctrlc()) {
  587. puts("\rCTRL+C - Operation aborted.\n");
  588. return;
  589. }
  590. usb_gadget_handle_interrupts(controller_index);
  591. sdp_handle_in_ep();
  592. }
  593. }
  594. int sdp_add(struct usb_configuration *c)
  595. {
  596. int id;
  597. id = usb_string_id(c->cdev);
  598. if (id < 0)
  599. return id;
  600. strings_sdp_generic[0].id = id;
  601. sdp_intf_runtime.iInterface = id;
  602. debug("%s: cdev: %p gadget: %p gadget->ep0: %p\n", __func__,
  603. c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
  604. return sdp_bind_config(c);
  605. }
  606. DECLARE_GADGET_BIND_CALLBACK(usb_dnl_sdp, sdp_add);