f_sdp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * f_sdp.c -- USB HID Serial Download Protocol
  4. *
  5. * Copyright (C) 2017 Toradex
  6. * Author: Stefan Agner <stefan.agner@toradex.com>
  7. *
  8. * This file implements the Serial Download Protocol (SDP) as specified in
  9. * the i.MX 6 Reference Manual. The SDP is a USB HID based protocol and
  10. * allows to download images directly to memory. The implementation
  11. * works with the imx_loader (imx_usb) USB client software on host side.
  12. *
  13. * Not all commands are implemented, e.g. WRITE_REGISTER, DCD_WRITE and
  14. * SKIP_DCD_HEADER are only stubs.
  15. *
  16. * Parts of the implementation are based on f_dfu and f_thor.
  17. */
  18. #include <errno.h>
  19. #include <common.h>
  20. #include <console.h>
  21. #include <malloc.h>
  22. #include <linux/usb/ch9.h>
  23. #include <linux/usb/gadget.h>
  24. #include <linux/usb/composite.h>
  25. #include <asm/io.h>
  26. #include <g_dnl.h>
  27. #include <sdp.h>
  28. #include <spl.h>
  29. #include <image.h>
  30. #include <imximage.h>
  31. #include <watchdog.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 inline void *sdp_ptr(u32 val)
  195. {
  196. return (void *)(uintptr_t)val;
  197. }
  198. static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req)
  199. {
  200. struct f_sdp *sdp = req->context;
  201. int status = req->status;
  202. u8 *data = req->buf;
  203. u8 report = data[0];
  204. if (status != 0) {
  205. pr_err("Status: %d\n", status);
  206. return;
  207. }
  208. if (report != 1) {
  209. pr_err("Unexpected report %d\n", report);
  210. return;
  211. }
  212. struct sdp_command *cmd = req->buf + 1;
  213. debug("%s: command: %04x, addr: %08x, cnt: %u\n",
  214. __func__, be16_to_cpu(cmd->cmd),
  215. be32_to_cpu(cmd->addr), be32_to_cpu(cmd->cnt));
  216. switch (be16_to_cpu(cmd->cmd)) {
  217. case SDP_READ_REGISTER:
  218. sdp->always_send_status = false;
  219. sdp->error_status = 0x0;
  220. sdp->state = SDP_STATE_TX_SEC_CONF;
  221. sdp->dnl_address = be32_to_cpu(cmd->addr);
  222. sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
  223. sdp->next_state = SDP_STATE_TX_REGISTER;
  224. printf("Reading %d registers at 0x%08x... ",
  225. sdp->dnl_bytes_remaining, sdp->dnl_address);
  226. break;
  227. case SDP_WRITE_FILE:
  228. sdp->always_send_status = true;
  229. sdp->error_status = SDP_WRITE_FILE_COMPLETE;
  230. sdp->state = SDP_STATE_RX_FILE_DATA;
  231. sdp->dnl_address = be32_to_cpu(cmd->addr);
  232. sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
  233. sdp->next_state = SDP_STATE_IDLE;
  234. printf("Downloading file of size %d to 0x%08x... ",
  235. sdp->dnl_bytes_remaining, sdp->dnl_address);
  236. break;
  237. case SDP_ERROR_STATUS:
  238. sdp->always_send_status = true;
  239. sdp->error_status = 0;
  240. sdp->state = SDP_STATE_TX_SEC_CONF;
  241. sdp->next_state = SDP_STATE_IDLE;
  242. break;
  243. case SDP_DCD_WRITE:
  244. sdp->always_send_status = true;
  245. sdp->error_status = SDP_WRITE_REGISTER_COMPLETE;
  246. sdp->state = SDP_STATE_RX_DCD_DATA;
  247. sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
  248. sdp->next_state = SDP_STATE_IDLE;
  249. break;
  250. case SDP_JUMP_ADDRESS:
  251. sdp->always_send_status = false;
  252. sdp->error_status = 0;
  253. sdp->jmp_address = be32_to_cpu(cmd->addr);
  254. sdp->state = SDP_STATE_TX_SEC_CONF;
  255. sdp->next_state = SDP_STATE_JUMP;
  256. break;
  257. case SDP_SKIP_DCD_HEADER:
  258. sdp->always_send_status = true;
  259. sdp->error_status = SDP_SKIP_DCD_HEADER_COMPLETE;
  260. /* Ignore command, DCD not supported anyway */
  261. sdp->state = SDP_STATE_TX_SEC_CONF;
  262. sdp->next_state = SDP_STATE_IDLE;
  263. break;
  264. default:
  265. pr_err("Unknown command: %04x\n", be16_to_cpu(cmd->cmd));
  266. }
  267. }
  268. static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req)
  269. {
  270. struct f_sdp *sdp = req->context;
  271. int status = req->status;
  272. u8 *data = req->buf;
  273. u8 report = data[0];
  274. int datalen = req->length - 1;
  275. if (status != 0) {
  276. pr_err("Status: %d\n", status);
  277. return;
  278. }
  279. if (report != 2) {
  280. pr_err("Unexpected report %d\n", report);
  281. return;
  282. }
  283. if (sdp->dnl_bytes_remaining < datalen) {
  284. /*
  285. * Some USB stacks require to send a complete buffer as
  286. * specified in the HID descriptor. This leads to longer
  287. * transfers than the file length, no problem for us.
  288. */
  289. sdp->dnl_bytes_remaining = 0;
  290. } else {
  291. sdp->dnl_bytes_remaining -= datalen;
  292. }
  293. if (sdp->state == SDP_STATE_RX_FILE_DATA) {
  294. memcpy(sdp_ptr(sdp->dnl_address), req->buf + 1, datalen);
  295. sdp->dnl_address += datalen;
  296. }
  297. if (sdp->dnl_bytes_remaining)
  298. return;
  299. printf("done\n");
  300. switch (sdp->state) {
  301. case SDP_STATE_RX_FILE_DATA:
  302. sdp->state = SDP_STATE_TX_SEC_CONF;
  303. break;
  304. case SDP_STATE_RX_DCD_DATA:
  305. sdp->state = SDP_STATE_TX_SEC_CONF;
  306. break;
  307. default:
  308. pr_err("Invalid state: %d\n", sdp->state);
  309. }
  310. }
  311. static void sdp_tx_complete(struct usb_ep *ep, struct usb_request *req)
  312. {
  313. struct f_sdp *sdp = req->context;
  314. int status = req->status;
  315. if (status != 0) {
  316. pr_err("Status: %d\n", status);
  317. return;
  318. }
  319. switch (sdp->state) {
  320. case SDP_STATE_TX_SEC_CONF_BUSY:
  321. /* Not all commands require status report */
  322. if (sdp->always_send_status || sdp->error_status)
  323. sdp->state = SDP_STATE_TX_STATUS;
  324. else
  325. sdp->state = sdp->next_state;
  326. break;
  327. case SDP_STATE_TX_STATUS_BUSY:
  328. sdp->state = sdp->next_state;
  329. break;
  330. case SDP_STATE_TX_REGISTER_BUSY:
  331. if (sdp->dnl_bytes_remaining)
  332. sdp->state = SDP_STATE_TX_REGISTER;
  333. else
  334. sdp->state = SDP_STATE_IDLE;
  335. break;
  336. default:
  337. pr_err("Wrong State: %d\n", sdp->state);
  338. sdp->state = SDP_STATE_IDLE;
  339. break;
  340. }
  341. debug("%s complete --> %d, %d/%d\n", ep->name,
  342. status, req->actual, req->length);
  343. }
  344. static int sdp_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  345. {
  346. struct usb_gadget *gadget = f->config->cdev->gadget;
  347. struct usb_request *req = f->config->cdev->req;
  348. struct f_sdp *sdp = f->config->cdev->req->context;
  349. u16 len = le16_to_cpu(ctrl->wLength);
  350. u16 w_value = le16_to_cpu(ctrl->wValue);
  351. int value = 0;
  352. u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
  353. debug("w_value: 0x%04x len: 0x%04x\n", w_value, len);
  354. debug("req_type: 0x%02x ctrl->bRequest: 0x%02x sdp->state: %d\n",
  355. req_type, ctrl->bRequest, sdp->state);
  356. if (req_type == USB_TYPE_STANDARD) {
  357. if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR) {
  358. /* Send HID report descriptor */
  359. value = min(len, (u16) sizeof(sdp_hid_report));
  360. memcpy(req->buf, &sdp_hid_report, value);
  361. sdp->configuration_done = true;
  362. }
  363. }
  364. if (req_type == USB_TYPE_CLASS) {
  365. int report = w_value & HID_REPORT_ID_MASK;
  366. /* HID (SDP) request */
  367. switch (ctrl->bRequest) {
  368. case HID_REQ_SET_REPORT:
  369. switch (report) {
  370. case 1:
  371. value = SDP_COMMAND_LEN + 1;
  372. req->complete = sdp_rx_command_complete;
  373. break;
  374. case 2:
  375. value = len;
  376. req->complete = sdp_rx_data_complete;
  377. break;
  378. }
  379. }
  380. }
  381. if (value >= 0) {
  382. req->length = value;
  383. req->zero = value < len;
  384. value = usb_ep_queue(gadget->ep0, req, 0);
  385. if (value < 0) {
  386. debug("ep_queue --> %d\n", value);
  387. req->status = 0;
  388. }
  389. }
  390. return value;
  391. }
  392. static int sdp_bind(struct usb_configuration *c, struct usb_function *f)
  393. {
  394. struct usb_gadget *gadget = c->cdev->gadget;
  395. struct usb_composite_dev *cdev = c->cdev;
  396. struct f_sdp *sdp = func_to_sdp(f);
  397. int rv = 0, id;
  398. id = usb_interface_id(c, f);
  399. if (id < 0)
  400. return id;
  401. sdp_intf_runtime.bInterfaceNumber = id;
  402. struct usb_ep *ep;
  403. /* allocate instance-specific endpoints */
  404. ep = usb_ep_autoconfig(gadget, &in_desc);
  405. if (!ep) {
  406. rv = -ENODEV;
  407. goto error;
  408. }
  409. sdp->in_ep = ep; /* Store IN EP for enabling @ setup */
  410. cdev->req->context = sdp;
  411. error:
  412. return rv;
  413. }
  414. static void sdp_unbind(struct usb_configuration *c, struct usb_function *f)
  415. {
  416. free(sdp_func);
  417. sdp_func = NULL;
  418. }
  419. static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
  420. {
  421. struct usb_request *req;
  422. req = usb_ep_alloc_request(ep, 0);
  423. if (!req)
  424. return req;
  425. req->length = length;
  426. req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
  427. if (!req->buf) {
  428. usb_ep_free_request(ep, req);
  429. req = NULL;
  430. }
  431. return req;
  432. }
  433. static struct usb_request *sdp_start_ep(struct usb_ep *ep)
  434. {
  435. struct usb_request *req;
  436. req = alloc_ep_req(ep, 64);
  437. debug("%s: ep:%p req:%p\n", __func__, ep, req);
  438. if (!req)
  439. return NULL;
  440. memset(req->buf, 0, req->length);
  441. req->complete = sdp_tx_complete;
  442. return req;
  443. }
  444. static int sdp_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  445. {
  446. struct f_sdp *sdp = func_to_sdp(f);
  447. struct usb_composite_dev *cdev = f->config->cdev;
  448. int result;
  449. debug("%s: intf: %d alt: %d\n", __func__, intf, alt);
  450. result = usb_ep_enable(sdp->in_ep, &in_desc);
  451. if (result)
  452. return result;
  453. sdp->in_req = sdp_start_ep(sdp->in_ep);
  454. sdp->in_req->context = sdp;
  455. sdp->in_ep->driver_data = cdev; /* claim */
  456. sdp->altsetting = alt;
  457. sdp->state = SDP_STATE_IDLE;
  458. return 0;
  459. }
  460. static int sdp_get_alt(struct usb_function *f, unsigned intf)
  461. {
  462. struct f_sdp *sdp = func_to_sdp(f);
  463. return sdp->altsetting;
  464. }
  465. static void sdp_disable(struct usb_function *f)
  466. {
  467. struct f_sdp *sdp = func_to_sdp(f);
  468. usb_ep_disable(sdp->in_ep);
  469. if (sdp->in_req) {
  470. free(sdp->in_req);
  471. sdp->in_req = NULL;
  472. }
  473. }
  474. static int sdp_bind_config(struct usb_configuration *c)
  475. {
  476. int status;
  477. if (!sdp_func) {
  478. sdp_func = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*sdp_func));
  479. if (!sdp_func)
  480. return -ENOMEM;
  481. }
  482. memset(sdp_func, 0, sizeof(*sdp_func));
  483. sdp_func->usb_function.name = "sdp";
  484. sdp_func->usb_function.hs_descriptors = sdp_runtime_descs;
  485. sdp_func->usb_function.descriptors = sdp_runtime_descs;
  486. sdp_func->usb_function.bind = sdp_bind;
  487. sdp_func->usb_function.unbind = sdp_unbind;
  488. sdp_func->usb_function.set_alt = sdp_set_alt;
  489. sdp_func->usb_function.get_alt = sdp_get_alt;
  490. sdp_func->usb_function.disable = sdp_disable;
  491. sdp_func->usb_function.strings = sdp_generic_strings;
  492. sdp_func->usb_function.setup = sdp_setup;
  493. status = usb_add_function(c, &sdp_func->usb_function);
  494. return status;
  495. }
  496. int sdp_init(int controller_index)
  497. {
  498. printf("SDP: initialize...\n");
  499. while (!sdp_func->configuration_done) {
  500. if (ctrlc()) {
  501. puts("\rCTRL+C - Operation aborted.\n");
  502. return 1;
  503. }
  504. WATCHDOG_RESET();
  505. usb_gadget_handle_interrupts(controller_index);
  506. }
  507. return 0;
  508. }
  509. static u32 sdp_jump_imxheader(void *address)
  510. {
  511. flash_header_v2_t *headerv2 = address;
  512. ulong (*entry)(void);
  513. if (headerv2->header.tag != IVT_HEADER_TAG) {
  514. printf("Header Tag is not an IMX image\n");
  515. return SDP_ERROR_IMXHEADER;
  516. }
  517. printf("Jumping to 0x%08x\n", headerv2->entry);
  518. entry = sdp_ptr(headerv2->entry);
  519. entry();
  520. /* The image probably never returns hence we won't reach that point */
  521. return 0;
  522. }
  523. static void sdp_handle_in_ep(void)
  524. {
  525. u8 *data = sdp_func->in_req->buf;
  526. u32 status;
  527. int datalen;
  528. switch (sdp_func->state) {
  529. case SDP_STATE_TX_SEC_CONF:
  530. debug("Report 3: HAB security\n");
  531. data[0] = 3;
  532. status = SDP_SECURITY_OPEN;
  533. memcpy(&data[1], &status, 4);
  534. sdp_func->in_req->length = 5;
  535. usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
  536. sdp_func->state = SDP_STATE_TX_SEC_CONF_BUSY;
  537. break;
  538. case SDP_STATE_TX_STATUS:
  539. debug("Report 4: Status\n");
  540. data[0] = 4;
  541. memcpy(&data[1], &sdp_func->error_status, 4);
  542. sdp_func->in_req->length = 65;
  543. usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
  544. sdp_func->state = SDP_STATE_TX_STATUS_BUSY;
  545. break;
  546. case SDP_STATE_TX_REGISTER:
  547. debug("Report 4: Register Values\n");
  548. data[0] = 4;
  549. datalen = sdp_func->dnl_bytes_remaining;
  550. if (datalen > 64)
  551. datalen = 64;
  552. memcpy(&data[1], sdp_ptr(sdp_func->dnl_address), datalen);
  553. sdp_func->in_req->length = 65;
  554. sdp_func->dnl_bytes_remaining -= datalen;
  555. sdp_func->dnl_address += datalen;
  556. usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
  557. sdp_func->state = SDP_STATE_TX_REGISTER_BUSY;
  558. break;
  559. case SDP_STATE_JUMP:
  560. printf("Jumping to header at 0x%08x\n", sdp_func->jmp_address);
  561. status = sdp_jump_imxheader(sdp_ptr(sdp_func->jmp_address));
  562. /* If imx header fails, try some U-Boot specific headers */
  563. if (status) {
  564. #ifdef CONFIG_SPL_BUILD
  565. /* In SPL, allow jumps to U-Boot images */
  566. struct spl_image_info spl_image = {};
  567. spl_parse_image_header(&spl_image,
  568. (struct image_header *)sdp_func->jmp_address);
  569. jump_to_image_no_args(&spl_image);
  570. #else
  571. /* In U-Boot, allow jumps to scripts */
  572. source(sdp_func->jmp_address, "script@1");
  573. #endif
  574. }
  575. sdp_func->next_state = SDP_STATE_IDLE;
  576. sdp_func->error_status = status;
  577. /* Only send Report 4 if there was an error */
  578. if (status)
  579. sdp_func->state = SDP_STATE_TX_STATUS;
  580. else
  581. sdp_func->state = SDP_STATE_IDLE;
  582. break;
  583. default:
  584. break;
  585. };
  586. }
  587. void sdp_handle(int controller_index)
  588. {
  589. printf("SDP: handle requests...\n");
  590. while (1) {
  591. if (ctrlc()) {
  592. puts("\rCTRL+C - Operation aborted.\n");
  593. return;
  594. }
  595. WATCHDOG_RESET();
  596. usb_gadget_handle_interrupts(controller_index);
  597. sdp_handle_in_ep();
  598. }
  599. }
  600. int sdp_add(struct usb_configuration *c)
  601. {
  602. int id;
  603. id = usb_string_id(c->cdev);
  604. if (id < 0)
  605. return id;
  606. strings_sdp_generic[0].id = id;
  607. sdp_intf_runtime.iInterface = id;
  608. debug("%s: cdev: %p gadget: %p gadget->ep0: %p\n", __func__,
  609. c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
  610. return sdp_bind_config(c);
  611. }
  612. DECLARE_GADGET_BIND_CALLBACK(usb_dnl_sdp, sdp_add);