f_thor.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * f_thor.c -- USB TIZEN THOR Downloader gadget function
  4. *
  5. * Copyright (C) 2013 Samsung Electronics
  6. * Lukasz Majewski <l.majewski@samsung.com>
  7. *
  8. * Based on code from:
  9. * git://review.tizen.org/kernel/u-boot
  10. *
  11. * Developed by:
  12. * Copyright (C) 2009 Samsung Electronics
  13. * Minkyu Kang <mk7.kang@samsung.com>
  14. * Sanghee Kim <sh0130.kim@samsung.com>
  15. */
  16. #include <errno.h>
  17. #include <common.h>
  18. #include <console.h>
  19. #include <malloc.h>
  20. #include <memalign.h>
  21. #include <version.h>
  22. #include <linux/usb/ch9.h>
  23. #include <linux/usb/gadget.h>
  24. #include <linux/usb/composite.h>
  25. #include <linux/usb/cdc.h>
  26. #include <g_dnl.h>
  27. #include <dfu.h>
  28. #include "f_thor.h"
  29. static void thor_tx_data(unsigned char *data, int len);
  30. static void thor_set_dma(void *addr, int len);
  31. static int thor_rx_data(void);
  32. static struct f_thor *thor_func;
  33. static inline struct f_thor *func_to_thor(struct usb_function *f)
  34. {
  35. return container_of(f, struct f_thor, usb_function);
  36. }
  37. DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_tx_data_buf,
  38. sizeof(struct rsp_box));
  39. DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_rx_data_buf,
  40. sizeof(struct rqt_box));
  41. /* ********************************************************** */
  42. /* THOR protocol - transmission handling */
  43. /* ********************************************************** */
  44. DEFINE_CACHE_ALIGN_BUFFER(char, f_name, F_NAME_BUF_SIZE + 1);
  45. static unsigned long long int thor_file_size;
  46. static int alt_setting_num;
  47. static void send_rsp(const struct rsp_box *rsp)
  48. {
  49. memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box));
  50. thor_tx_data(thor_tx_data_buf, sizeof(struct rsp_box));
  51. debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data);
  52. }
  53. static void send_data_rsp(s32 ack, s32 count)
  54. {
  55. ALLOC_CACHE_ALIGN_BUFFER(struct data_rsp_box, rsp,
  56. sizeof(struct data_rsp_box));
  57. rsp->ack = ack;
  58. rsp->count = count;
  59. memcpy(thor_tx_data_buf, rsp, sizeof(struct data_rsp_box));
  60. thor_tx_data(thor_tx_data_buf, sizeof(struct data_rsp_box));
  61. debug("-DATA RSP: %d, %d\n", ack, count);
  62. }
  63. static int process_rqt_info(const struct rqt_box *rqt)
  64. {
  65. ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
  66. memset(rsp, 0, sizeof(struct rsp_box));
  67. rsp->rsp = rqt->rqt;
  68. rsp->rsp_data = rqt->rqt_data;
  69. switch (rqt->rqt_data) {
  70. case RQT_INFO_VER_PROTOCOL:
  71. rsp->int_data[0] = VER_PROTOCOL_MAJOR;
  72. rsp->int_data[1] = VER_PROTOCOL_MINOR;
  73. break;
  74. case RQT_INIT_VER_HW:
  75. snprintf(rsp->str_data[0], sizeof(rsp->str_data[0]),
  76. "%x", checkboard());
  77. break;
  78. case RQT_INIT_VER_BOOT:
  79. sprintf(rsp->str_data[0], "%s", U_BOOT_VERSION);
  80. break;
  81. case RQT_INIT_VER_KERNEL:
  82. sprintf(rsp->str_data[0], "%s", "k unknown");
  83. break;
  84. case RQT_INIT_VER_PLATFORM:
  85. sprintf(rsp->str_data[0], "%s", "p unknown");
  86. break;
  87. case RQT_INIT_VER_CSC:
  88. sprintf(rsp->str_data[0], "%s", "c unknown");
  89. break;
  90. default:
  91. return -EINVAL;
  92. }
  93. send_rsp(rsp);
  94. return true;
  95. }
  96. static int process_rqt_cmd(const struct rqt_box *rqt)
  97. {
  98. ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
  99. memset(rsp, 0, sizeof(struct rsp_box));
  100. rsp->rsp = rqt->rqt;
  101. rsp->rsp_data = rqt->rqt_data;
  102. switch (rqt->rqt_data) {
  103. case RQT_CMD_REBOOT:
  104. debug("TARGET RESET\n");
  105. send_rsp(rsp);
  106. g_dnl_unregister();
  107. dfu_free_entities();
  108. #ifdef CONFIG_THOR_RESET_OFF
  109. return RESET_DONE;
  110. #endif
  111. run_command("reset", 0);
  112. break;
  113. case RQT_CMD_POWEROFF:
  114. case RQT_CMD_EFSCLEAR:
  115. send_rsp(rsp);
  116. default:
  117. printf("Command not supported -> cmd: %d\n", rqt->rqt_data);
  118. return -EINVAL;
  119. }
  120. return true;
  121. }
  122. static long long int download_head(unsigned long long total,
  123. unsigned int packet_size,
  124. long long int *left,
  125. int *cnt)
  126. {
  127. long long int rcv_cnt = 0, left_to_rcv, ret_rcv;
  128. struct dfu_entity *dfu_entity = dfu_get_entity(alt_setting_num);
  129. void *transfer_buffer = dfu_get_buf(dfu_entity);
  130. void *buf = transfer_buffer;
  131. int usb_pkt_cnt = 0, ret;
  132. /*
  133. * Files smaller than THOR_STORE_UNIT_SIZE (now 32 MiB) are stored on
  134. * the medium.
  135. * The packet response is sent on the purpose after successful data
  136. * chunk write. There is a room for improvement when asynchronous write
  137. * is performed.
  138. */
  139. while (total - rcv_cnt >= packet_size) {
  140. thor_set_dma(buf, packet_size);
  141. buf += packet_size;
  142. ret_rcv = thor_rx_data();
  143. if (ret_rcv < 0)
  144. return ret_rcv;
  145. rcv_cnt += ret_rcv;
  146. debug("%d: RCV data count: %llu cnt: %d\n", usb_pkt_cnt,
  147. rcv_cnt, *cnt);
  148. if ((rcv_cnt % THOR_STORE_UNIT_SIZE) == 0) {
  149. ret = dfu_write(dfu_get_entity(alt_setting_num),
  150. transfer_buffer, THOR_STORE_UNIT_SIZE,
  151. (*cnt)++);
  152. if (ret) {
  153. pr_err("DFU write failed [%d] cnt: %d",
  154. ret, *cnt);
  155. return ret;
  156. }
  157. buf = transfer_buffer;
  158. }
  159. send_data_rsp(0, ++usb_pkt_cnt);
  160. }
  161. /* Calculate the amount of data to arrive from PC (in bytes) */
  162. left_to_rcv = total - rcv_cnt;
  163. /*
  164. * Calculate number of data already received. but not yet stored
  165. * on the medium (they are smaller than THOR_STORE_UNIT_SIZE)
  166. */
  167. *left = left_to_rcv + buf - transfer_buffer;
  168. debug("%s: left: %llu left_to_rcv: %llu buf: 0x%p\n", __func__,
  169. *left, left_to_rcv, buf);
  170. if (left_to_rcv) {
  171. thor_set_dma(buf, packet_size);
  172. ret_rcv = thor_rx_data();
  173. if (ret_rcv < 0)
  174. return ret_rcv;
  175. rcv_cnt += ret_rcv;
  176. send_data_rsp(0, ++usb_pkt_cnt);
  177. }
  178. debug("%s: %llu total: %llu cnt: %d\n", __func__, rcv_cnt, total, *cnt);
  179. return rcv_cnt;
  180. }
  181. static int download_tail(long long int left, int cnt)
  182. {
  183. struct dfu_entity *dfu_entity;
  184. void *transfer_buffer;
  185. int ret;
  186. debug("%s: left: %llu cnt: %d\n", __func__, left, cnt);
  187. dfu_entity = dfu_get_entity(alt_setting_num);
  188. if (!dfu_entity) {
  189. pr_err("Alt setting: %d entity not found!\n", alt_setting_num);
  190. return -ENOENT;
  191. }
  192. transfer_buffer = dfu_get_buf(dfu_entity);
  193. if (!transfer_buffer) {
  194. pr_err("Transfer buffer not allocated!");
  195. return -ENXIO;
  196. }
  197. if (left) {
  198. ret = dfu_write(dfu_entity, transfer_buffer, left, cnt++);
  199. if (ret) {
  200. pr_err("DFU write failed [%d]: left: %llu", ret, left);
  201. return ret;
  202. }
  203. }
  204. /*
  205. * To store last "packet" or write file from buffer to filesystem
  206. * DFU storage backend requires dfu_flush
  207. *
  208. * This also frees memory malloc'ed by dfu_get_buf(), so no explicit
  209. * need fo call dfu_free_buf() is needed.
  210. */
  211. ret = dfu_flush(dfu_entity, transfer_buffer, 0, cnt);
  212. if (ret)
  213. pr_err("DFU flush failed!");
  214. return ret;
  215. }
  216. static long long int process_rqt_download(const struct rqt_box *rqt)
  217. {
  218. ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
  219. static long long int left, ret_head;
  220. int file_type, ret = 0;
  221. static int cnt;
  222. memset(rsp, 0, sizeof(struct rsp_box));
  223. rsp->rsp = rqt->rqt;
  224. rsp->rsp_data = rqt->rqt_data;
  225. switch (rqt->rqt_data) {
  226. case RQT_DL_INIT:
  227. thor_file_size = (unsigned long long int)rqt->int_data[0] +
  228. (((unsigned long long int)rqt->int_data[1])
  229. << 32);
  230. debug("INIT: total %llu bytes\n", thor_file_size);
  231. break;
  232. case RQT_DL_FILE_INFO:
  233. file_type = rqt->int_data[0];
  234. if (file_type == FILE_TYPE_PIT) {
  235. puts("PIT table file - not supported\n");
  236. rsp->ack = -ENOTSUPP;
  237. ret = rsp->ack;
  238. break;
  239. }
  240. thor_file_size = (unsigned long long int)rqt->int_data[1] +
  241. (((unsigned long long int)rqt->int_data[2])
  242. << 32);
  243. memcpy(f_name, rqt->str_data[0], F_NAME_BUF_SIZE);
  244. f_name[F_NAME_BUF_SIZE] = '\0';
  245. debug("INFO: name(%s, %d), size(%llu), type(%d)\n",
  246. f_name, 0, thor_file_size, file_type);
  247. rsp->int_data[0] = THOR_PACKET_SIZE;
  248. alt_setting_num = dfu_get_alt(f_name);
  249. if (alt_setting_num < 0) {
  250. pr_err("Alt setting [%d] to write not found!",
  251. alt_setting_num);
  252. rsp->ack = -ENODEV;
  253. ret = rsp->ack;
  254. }
  255. break;
  256. case RQT_DL_FILE_START:
  257. send_rsp(rsp);
  258. ret_head = download_head(thor_file_size, THOR_PACKET_SIZE,
  259. &left, &cnt);
  260. if (ret_head < 0) {
  261. left = 0;
  262. cnt = 0;
  263. }
  264. return ret_head;
  265. case RQT_DL_FILE_END:
  266. debug("DL FILE_END\n");
  267. rsp->ack = download_tail(left, cnt);
  268. ret = rsp->ack;
  269. left = 0;
  270. cnt = 0;
  271. break;
  272. case RQT_DL_EXIT:
  273. debug("DL EXIT\n");
  274. break;
  275. default:
  276. pr_err("Operation not supported: %d", rqt->rqt_data);
  277. ret = -ENOTSUPP;
  278. }
  279. send_rsp(rsp);
  280. return ret;
  281. }
  282. static int process_data(void)
  283. {
  284. ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box));
  285. int ret = -EINVAL;
  286. memcpy(rqt, thor_rx_data_buf, sizeof(struct rqt_box));
  287. debug("+RQT: %d, %d\n", rqt->rqt, rqt->rqt_data);
  288. switch (rqt->rqt) {
  289. case RQT_INFO:
  290. ret = process_rqt_info(rqt);
  291. break;
  292. case RQT_CMD:
  293. ret = process_rqt_cmd(rqt);
  294. break;
  295. case RQT_DL:
  296. ret = (int) process_rqt_download(rqt);
  297. break;
  298. case RQT_UL:
  299. puts("RQT: UPLOAD not supported!\n");
  300. break;
  301. default:
  302. pr_err("unknown request (%d)", rqt->rqt);
  303. }
  304. return ret;
  305. }
  306. /* ********************************************************** */
  307. /* THOR USB Function */
  308. /* ********************************************************** */
  309. static inline struct usb_endpoint_descriptor *
  310. ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
  311. struct usb_endpoint_descriptor *fs)
  312. {
  313. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  314. return hs;
  315. return fs;
  316. }
  317. static struct usb_interface_descriptor thor_downloader_intf_data = {
  318. .bLength = sizeof(thor_downloader_intf_data),
  319. .bDescriptorType = USB_DT_INTERFACE,
  320. .bNumEndpoints = 2,
  321. .bInterfaceClass = USB_CLASS_CDC_DATA,
  322. };
  323. static struct usb_endpoint_descriptor fs_in_desc = {
  324. .bLength = USB_DT_ENDPOINT_SIZE,
  325. .bDescriptorType = USB_DT_ENDPOINT,
  326. .bEndpointAddress = USB_DIR_IN,
  327. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  328. };
  329. static struct usb_endpoint_descriptor fs_out_desc = {
  330. .bLength = USB_DT_ENDPOINT_SIZE,
  331. .bDescriptorType = USB_DT_ENDPOINT,
  332. .bEndpointAddress = USB_DIR_OUT,
  333. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  334. };
  335. /* CDC configuration */
  336. static struct usb_interface_descriptor thor_downloader_intf_int = {
  337. .bLength = sizeof(thor_downloader_intf_int),
  338. .bDescriptorType = USB_DT_INTERFACE,
  339. .bNumEndpoints = 1,
  340. .bInterfaceClass = USB_CLASS_COMM,
  341. /* 0x02 Abstract Line Control Model */
  342. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  343. /* 0x01 Common AT commands */
  344. .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
  345. };
  346. static struct usb_cdc_header_desc thor_downloader_cdc_header = {
  347. .bLength = sizeof(thor_downloader_cdc_header),
  348. .bDescriptorType = 0x24, /* CS_INTERFACE */
  349. .bDescriptorSubType = 0x00,
  350. .bcdCDC = 0x0110,
  351. };
  352. static struct usb_cdc_call_mgmt_descriptor thor_downloader_cdc_call = {
  353. .bLength = sizeof(thor_downloader_cdc_call),
  354. .bDescriptorType = 0x24, /* CS_INTERFACE */
  355. .bDescriptorSubType = 0x01,
  356. .bmCapabilities = 0x00,
  357. .bDataInterface = 0x01,
  358. };
  359. static struct usb_cdc_acm_descriptor thor_downloader_cdc_abstract = {
  360. .bLength = sizeof(thor_downloader_cdc_abstract),
  361. .bDescriptorType = 0x24, /* CS_INTERFACE */
  362. .bDescriptorSubType = 0x02,
  363. .bmCapabilities = 0x00,
  364. };
  365. static struct usb_cdc_union_desc thor_downloader_cdc_union = {
  366. .bLength = sizeof(thor_downloader_cdc_union),
  367. .bDescriptorType = 0x24, /* CS_INTERFACE */
  368. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  369. };
  370. static struct usb_endpoint_descriptor fs_int_desc = {
  371. .bLength = USB_DT_ENDPOINT_SIZE,
  372. .bDescriptorType = USB_DT_ENDPOINT,
  373. .bEndpointAddress = 3 | USB_DIR_IN,
  374. .bmAttributes = USB_ENDPOINT_XFER_INT,
  375. .wMaxPacketSize = __constant_cpu_to_le16(16),
  376. .bInterval = 0x9,
  377. };
  378. static struct usb_interface_assoc_descriptor
  379. thor_iad_descriptor = {
  380. .bLength = sizeof(thor_iad_descriptor),
  381. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  382. .bFirstInterface = 0,
  383. .bInterfaceCount = 2, /* control + data */
  384. .bFunctionClass = USB_CLASS_COMM,
  385. .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
  386. .bFunctionProtocol = USB_CDC_PROTO_NONE,
  387. };
  388. static struct usb_endpoint_descriptor hs_in_desc = {
  389. .bLength = USB_DT_ENDPOINT_SIZE,
  390. .bDescriptorType = USB_DT_ENDPOINT,
  391. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  392. .wMaxPacketSize = __constant_cpu_to_le16(512),
  393. };
  394. static struct usb_endpoint_descriptor hs_out_desc = {
  395. .bLength = USB_DT_ENDPOINT_SIZE,
  396. .bDescriptorType = USB_DT_ENDPOINT,
  397. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  398. .wMaxPacketSize = __constant_cpu_to_le16(512),
  399. };
  400. static struct usb_endpoint_descriptor hs_int_desc = {
  401. .bLength = USB_DT_ENDPOINT_SIZE,
  402. .bDescriptorType = USB_DT_ENDPOINT,
  403. .bmAttributes = USB_ENDPOINT_XFER_INT,
  404. .wMaxPacketSize = __constant_cpu_to_le16(16),
  405. .bInterval = 0x9,
  406. };
  407. /*
  408. * This attribute vendor descriptor is necessary for correct operation with
  409. * Windows version of THOR download program
  410. *
  411. * It prevents windows driver from sending zero lenght packet (ZLP) after
  412. * each THOR_PACKET_SIZE. This assures consistent behaviour with libusb
  413. */
  414. static struct usb_cdc_attribute_vendor_descriptor thor_downloader_cdc_av = {
  415. .bLength = sizeof(thor_downloader_cdc_av),
  416. .bDescriptorType = 0x24,
  417. .bDescriptorSubType = 0x80,
  418. .DAUType = 0x0002,
  419. .DAULength = 0x0001,
  420. .DAUValue = 0x00,
  421. };
  422. static const struct usb_descriptor_header *hs_thor_downloader_function[] = {
  423. (struct usb_descriptor_header *)&thor_iad_descriptor,
  424. (struct usb_descriptor_header *)&thor_downloader_intf_int,
  425. (struct usb_descriptor_header *)&thor_downloader_cdc_header,
  426. (struct usb_descriptor_header *)&thor_downloader_cdc_call,
  427. (struct usb_descriptor_header *)&thor_downloader_cdc_abstract,
  428. (struct usb_descriptor_header *)&thor_downloader_cdc_union,
  429. (struct usb_descriptor_header *)&hs_int_desc,
  430. (struct usb_descriptor_header *)&thor_downloader_intf_data,
  431. (struct usb_descriptor_header *)&thor_downloader_cdc_av,
  432. (struct usb_descriptor_header *)&hs_in_desc,
  433. (struct usb_descriptor_header *)&hs_out_desc,
  434. NULL,
  435. };
  436. /*-------------------------------------------------------------------------*/
  437. static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
  438. {
  439. struct usb_request *req;
  440. req = usb_ep_alloc_request(ep, 0);
  441. if (!req)
  442. return req;
  443. req->length = length;
  444. req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
  445. if (!req->buf) {
  446. usb_ep_free_request(ep, req);
  447. req = NULL;
  448. }
  449. return req;
  450. }
  451. static int thor_rx_data(void)
  452. {
  453. struct thor_dev *dev = thor_func->dev;
  454. int data_to_rx, tmp, status;
  455. data_to_rx = dev->out_req->length;
  456. tmp = data_to_rx;
  457. do {
  458. dev->out_req->length = data_to_rx;
  459. debug("dev->out_req->length:%d dev->rxdata:%d\n",
  460. dev->out_req->length, dev->rxdata);
  461. status = usb_ep_queue(dev->out_ep, dev->out_req, 0);
  462. if (status) {
  463. pr_err("kill %s: resubmit %d bytes --> %d",
  464. dev->out_ep->name, dev->out_req->length, status);
  465. usb_ep_set_halt(dev->out_ep);
  466. return -EAGAIN;
  467. }
  468. while (!dev->rxdata) {
  469. usb_gadget_handle_interrupts(0);
  470. if (ctrlc())
  471. return -1;
  472. }
  473. dev->rxdata = 0;
  474. data_to_rx -= dev->out_req->actual;
  475. } while (data_to_rx);
  476. return tmp;
  477. }
  478. static void thor_tx_data(unsigned char *data, int len)
  479. {
  480. struct thor_dev *dev = thor_func->dev;
  481. unsigned char *ptr = dev->in_req->buf;
  482. int status;
  483. memset(ptr, 0, len);
  484. memcpy(ptr, data, len);
  485. dev->in_req->length = len;
  486. debug("%s: dev->in_req->length:%d to_cpy:%zd\n", __func__,
  487. dev->in_req->length, sizeof(data));
  488. status = usb_ep_queue(dev->in_ep, dev->in_req, 0);
  489. if (status) {
  490. pr_err("kill %s: resubmit %d bytes --> %d",
  491. dev->in_ep->name, dev->in_req->length, status);
  492. usb_ep_set_halt(dev->in_ep);
  493. }
  494. /* Wait until tx interrupt received */
  495. while (!dev->txdata)
  496. usb_gadget_handle_interrupts(0);
  497. dev->txdata = 0;
  498. }
  499. static void thor_rx_tx_complete(struct usb_ep *ep, struct usb_request *req)
  500. {
  501. struct thor_dev *dev = thor_func->dev;
  502. int status = req->status;
  503. debug("%s: ep_ptr:%p, req_ptr:%p\n", __func__, ep, req);
  504. switch (status) {
  505. case 0:
  506. if (ep == dev->out_ep)
  507. dev->rxdata = 1;
  508. else
  509. dev->txdata = 1;
  510. break;
  511. /* this endpoint is normally active while we're configured */
  512. case -ECONNABORTED: /* hardware forced ep reset */
  513. case -ECONNRESET: /* request dequeued */
  514. case -ESHUTDOWN: /* disconnect from host */
  515. case -EREMOTEIO: /* short read */
  516. case -EOVERFLOW:
  517. pr_err("ERROR:%d", status);
  518. break;
  519. }
  520. debug("%s complete --> %d, %d/%d\n", ep->name,
  521. status, req->actual, req->length);
  522. }
  523. static void thor_setup_complete(struct usb_ep *ep, struct usb_request *req)
  524. {
  525. if (req->status || req->actual != req->length)
  526. debug("setup complete --> %d, %d/%d\n",
  527. req->status, req->actual, req->length);
  528. }
  529. static int
  530. thor_func_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  531. {
  532. struct thor_dev *dev = thor_func->dev;
  533. struct usb_request *req = dev->req;
  534. struct usb_gadget *gadget = dev->gadget;
  535. int value = 0;
  536. u16 len = le16_to_cpu(ctrl->wLength);
  537. debug("Req_Type: 0x%x Req: 0x%x wValue: 0x%x wIndex: 0x%x wLen: 0x%x\n",
  538. ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, ctrl->wIndex,
  539. ctrl->wLength);
  540. switch (ctrl->bRequest) {
  541. case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
  542. value = 0;
  543. break;
  544. case USB_CDC_REQ_SET_LINE_CODING:
  545. value = len;
  546. /* Line Coding set done = configuration done */
  547. thor_func->dev->configuration_done = 1;
  548. break;
  549. default:
  550. pr_err("thor_setup: unknown request: %d", ctrl->bRequest);
  551. }
  552. if (value >= 0) {
  553. req->length = value;
  554. req->zero = value < len;
  555. value = usb_ep_queue(gadget->ep0, req, 0);
  556. if (value < 0) {
  557. debug("%s: ep_queue: %d\n", __func__, value);
  558. req->status = 0;
  559. }
  560. }
  561. return value;
  562. }
  563. /* Specific to the THOR protocol */
  564. static void thor_set_dma(void *addr, int len)
  565. {
  566. struct thor_dev *dev = thor_func->dev;
  567. debug("in_req:%p, out_req:%p\n", dev->in_req, dev->out_req);
  568. debug("addr:%p, len:%d\n", addr, len);
  569. dev->out_req->buf = addr;
  570. dev->out_req->length = len;
  571. }
  572. int thor_init(void)
  573. {
  574. struct thor_dev *dev = thor_func->dev;
  575. /* Wait for a device enumeration and configuration settings */
  576. debug("THOR enumeration/configuration setting....\n");
  577. while (!dev->configuration_done)
  578. usb_gadget_handle_interrupts(0);
  579. thor_set_dma(thor_rx_data_buf, strlen("THOR"));
  580. /* detect the download request from Host PC */
  581. if (thor_rx_data() < 0) {
  582. printf("%s: Data not received!\n", __func__);
  583. return -1;
  584. }
  585. if (!strncmp((char *)thor_rx_data_buf, "THOR", strlen("THOR"))) {
  586. puts("Download request from the Host PC\n");
  587. udelay(30 * 1000); /* 30 ms */
  588. strcpy((char *)thor_tx_data_buf, "ROHT");
  589. thor_tx_data(thor_tx_data_buf, strlen("ROHT"));
  590. } else {
  591. puts("Wrong reply information\n");
  592. return -1;
  593. }
  594. return 0;
  595. }
  596. int thor_handle(void)
  597. {
  598. int ret;
  599. /* receive the data from Host PC */
  600. while (1) {
  601. thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box));
  602. ret = thor_rx_data();
  603. if (ret > 0) {
  604. ret = process_data();
  605. #ifdef CONFIG_THOR_RESET_OFF
  606. if (ret == RESET_DONE)
  607. break;
  608. #endif
  609. if (ret < 0)
  610. return ret;
  611. } else {
  612. printf("%s: No data received!\n", __func__);
  613. break;
  614. }
  615. }
  616. return 0;
  617. }
  618. static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  619. {
  620. if (req->buf)
  621. free(req->buf);
  622. usb_ep_free_request(ep, req);
  623. }
  624. static int thor_func_bind(struct usb_configuration *c, struct usb_function *f)
  625. {
  626. struct usb_gadget *gadget = c->cdev->gadget;
  627. struct f_thor *f_thor = func_to_thor(f);
  628. struct thor_dev *dev;
  629. struct usb_ep *ep;
  630. int status;
  631. thor_func = f_thor;
  632. dev = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*dev));
  633. if (!dev)
  634. return -ENOMEM;
  635. memset(dev, 0, sizeof(*dev));
  636. dev->gadget = gadget;
  637. f_thor->dev = dev;
  638. debug("%s: usb_configuration: 0x%p usb_function: 0x%p\n",
  639. __func__, c, f);
  640. debug("f_thor: 0x%p thor: 0x%p\n", f_thor, dev);
  641. /* EP0 */
  642. /* preallocate control response and buffer */
  643. dev->req = usb_ep_alloc_request(gadget->ep0, 0);
  644. if (!dev->req) {
  645. status = -ENOMEM;
  646. goto fail;
  647. }
  648. dev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
  649. THOR_PACKET_SIZE);
  650. if (!dev->req->buf) {
  651. status = -ENOMEM;
  652. goto fail;
  653. }
  654. dev->req->complete = thor_setup_complete;
  655. /* DYNAMIC interface numbers assignments */
  656. status = usb_interface_id(c, f);
  657. if (status < 0)
  658. goto fail;
  659. thor_downloader_intf_int.bInterfaceNumber = status;
  660. thor_downloader_cdc_union.bMasterInterface0 = status;
  661. status = usb_interface_id(c, f);
  662. if (status < 0)
  663. goto fail;
  664. thor_downloader_intf_data.bInterfaceNumber = status;
  665. thor_downloader_cdc_union.bSlaveInterface0 = status;
  666. /* allocate instance-specific endpoints */
  667. ep = usb_ep_autoconfig(gadget, &fs_in_desc);
  668. if (!ep) {
  669. status = -ENODEV;
  670. goto fail;
  671. }
  672. if (gadget_is_dualspeed(gadget)) {
  673. hs_in_desc.bEndpointAddress =
  674. fs_in_desc.bEndpointAddress;
  675. }
  676. dev->in_ep = ep; /* Store IN EP for enabling @ setup */
  677. ep->driver_data = dev;
  678. ep = usb_ep_autoconfig(gadget, &fs_out_desc);
  679. if (!ep) {
  680. status = -ENODEV;
  681. goto fail;
  682. }
  683. if (gadget_is_dualspeed(gadget))
  684. hs_out_desc.bEndpointAddress =
  685. fs_out_desc.bEndpointAddress;
  686. dev->out_ep = ep; /* Store OUT EP for enabling @ setup */
  687. ep->driver_data = dev;
  688. ep = usb_ep_autoconfig(gadget, &fs_int_desc);
  689. if (!ep) {
  690. status = -ENODEV;
  691. goto fail;
  692. }
  693. dev->int_ep = ep;
  694. ep->driver_data = dev;
  695. if (gadget_is_dualspeed(gadget)) {
  696. hs_int_desc.bEndpointAddress =
  697. fs_int_desc.bEndpointAddress;
  698. f->hs_descriptors = (struct usb_descriptor_header **)
  699. &hs_thor_downloader_function;
  700. if (!f->hs_descriptors)
  701. goto fail;
  702. }
  703. debug("%s: out_ep:%p out_req:%p\n", __func__,
  704. dev->out_ep, dev->out_req);
  705. return 0;
  706. fail:
  707. if (dev->req)
  708. free_ep_req(gadget->ep0, dev->req);
  709. free(dev);
  710. return status;
  711. }
  712. static void thor_unbind(struct usb_configuration *c, struct usb_function *f)
  713. {
  714. struct f_thor *f_thor = func_to_thor(f);
  715. struct thor_dev *dev = f_thor->dev;
  716. free_ep_req(dev->gadget->ep0, dev->req);
  717. free(dev);
  718. memset(thor_func, 0, sizeof(*thor_func));
  719. thor_func = NULL;
  720. }
  721. static void thor_func_disable(struct usb_function *f)
  722. {
  723. struct f_thor *f_thor = func_to_thor(f);
  724. struct thor_dev *dev = f_thor->dev;
  725. debug("%s:\n", __func__);
  726. /* Avoid freeing memory when ep is still claimed */
  727. if (dev->in_ep->driver_data) {
  728. free_ep_req(dev->in_ep, dev->in_req);
  729. usb_ep_disable(dev->in_ep);
  730. dev->in_ep->driver_data = NULL;
  731. }
  732. if (dev->out_ep->driver_data) {
  733. usb_ep_free_request(dev->out_ep, dev->out_req);
  734. usb_ep_disable(dev->out_ep);
  735. dev->out_ep->driver_data = NULL;
  736. }
  737. if (dev->int_ep->driver_data) {
  738. usb_ep_disable(dev->int_ep);
  739. dev->int_ep->driver_data = NULL;
  740. }
  741. }
  742. static int thor_eps_setup(struct usb_function *f)
  743. {
  744. struct usb_composite_dev *cdev = f->config->cdev;
  745. struct usb_gadget *gadget = cdev->gadget;
  746. struct thor_dev *dev = thor_func->dev;
  747. struct usb_endpoint_descriptor *d;
  748. struct usb_request *req;
  749. struct usb_ep *ep;
  750. int result;
  751. ep = dev->in_ep;
  752. d = ep_desc(gadget, &hs_in_desc, &fs_in_desc);
  753. debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
  754. result = usb_ep_enable(ep, d);
  755. if (result)
  756. goto err;
  757. ep->driver_data = cdev; /* claim */
  758. req = alloc_ep_req(ep, THOR_PACKET_SIZE);
  759. if (!req) {
  760. result = -EIO;
  761. goto err_disable_in_ep;
  762. }
  763. memset(req->buf, 0, req->length);
  764. req->complete = thor_rx_tx_complete;
  765. dev->in_req = req;
  766. ep = dev->out_ep;
  767. d = ep_desc(gadget, &hs_out_desc, &fs_out_desc);
  768. debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
  769. result = usb_ep_enable(ep, d);
  770. if (result)
  771. goto err_free_in_req;
  772. ep->driver_data = cdev; /* claim */
  773. req = usb_ep_alloc_request(ep, 0);
  774. if (!req) {
  775. result = -EIO;
  776. goto err_disable_out_ep;
  777. }
  778. req->complete = thor_rx_tx_complete;
  779. dev->out_req = req;
  780. /* ACM control EP */
  781. ep = dev->int_ep;
  782. ep->driver_data = cdev; /* claim */
  783. return 0;
  784. err_disable_out_ep:
  785. usb_ep_disable(dev->out_ep);
  786. err_free_in_req:
  787. free_ep_req(dev->in_ep, dev->in_req);
  788. dev->in_req = NULL;
  789. err_disable_in_ep:
  790. usb_ep_disable(dev->in_ep);
  791. err:
  792. return result;
  793. }
  794. static int thor_func_set_alt(struct usb_function *f,
  795. unsigned intf, unsigned alt)
  796. {
  797. struct thor_dev *dev = thor_func->dev;
  798. int result;
  799. debug("%s: func: %s intf: %d alt: %d\n",
  800. __func__, f->name, intf, alt);
  801. switch (intf) {
  802. case 0:
  803. debug("ACM INTR interface\n");
  804. break;
  805. case 1:
  806. debug("Communication Data interface\n");
  807. result = thor_eps_setup(f);
  808. if (result)
  809. pr_err("%s: EPs setup failed!", __func__);
  810. dev->configuration_done = 1;
  811. break;
  812. }
  813. return 0;
  814. }
  815. static int thor_func_init(struct usb_configuration *c)
  816. {
  817. struct f_thor *f_thor;
  818. int status;
  819. debug("%s: cdev: 0x%p\n", __func__, c->cdev);
  820. f_thor = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_thor));
  821. if (!f_thor)
  822. return -ENOMEM;
  823. memset(f_thor, 0, sizeof(*f_thor));
  824. f_thor->usb_function.name = "f_thor";
  825. f_thor->usb_function.bind = thor_func_bind;
  826. f_thor->usb_function.unbind = thor_unbind;
  827. f_thor->usb_function.setup = thor_func_setup;
  828. f_thor->usb_function.set_alt = thor_func_set_alt;
  829. f_thor->usb_function.disable = thor_func_disable;
  830. status = usb_add_function(c, &f_thor->usb_function);
  831. if (status)
  832. free(f_thor);
  833. return status;
  834. }
  835. int thor_add(struct usb_configuration *c)
  836. {
  837. debug("%s:\n", __func__);
  838. return thor_func_init(c);
  839. }
  840. DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor, thor_add);