f_thor.c 24 KB

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