f_thor.c 24 KB

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