f_thor.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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 <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);
  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. error("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. error("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. error("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. error("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. error("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 = rqt->int_data[0];
  228. debug("INIT: total %d bytes\n", rqt->int_data[0]);
  229. break;
  230. case RQT_DL_FILE_INFO:
  231. file_type = rqt->int_data[0];
  232. if (file_type == FILE_TYPE_PIT) {
  233. puts("PIT table file - not supported\n");
  234. rsp->ack = -ENOTSUPP;
  235. ret = rsp->ack;
  236. break;
  237. }
  238. thor_file_size = rqt->int_data[1];
  239. memcpy(f_name, rqt->str_data[0], F_NAME_BUF_SIZE);
  240. debug("INFO: name(%s, %d), size(%llu), type(%d)\n",
  241. f_name, 0, thor_file_size, file_type);
  242. rsp->int_data[0] = THOR_PACKET_SIZE;
  243. alt_setting_num = dfu_get_alt(f_name);
  244. if (alt_setting_num < 0) {
  245. error("Alt setting [%d] to write not found!",
  246. alt_setting_num);
  247. rsp->ack = -ENODEV;
  248. ret = rsp->ack;
  249. }
  250. break;
  251. case RQT_DL_FILE_START:
  252. send_rsp(rsp);
  253. ret_head = download_head(thor_file_size, THOR_PACKET_SIZE,
  254. &left, &cnt);
  255. if (ret_head < 0) {
  256. left = 0;
  257. cnt = 0;
  258. }
  259. return ret_head;
  260. case RQT_DL_FILE_END:
  261. debug("DL FILE_END\n");
  262. rsp->ack = download_tail(left, cnt);
  263. ret = rsp->ack;
  264. left = 0;
  265. cnt = 0;
  266. break;
  267. case RQT_DL_EXIT:
  268. debug("DL EXIT\n");
  269. break;
  270. default:
  271. error("Operation not supported: %d", rqt->rqt_data);
  272. ret = -ENOTSUPP;
  273. }
  274. send_rsp(rsp);
  275. return ret;
  276. }
  277. static int process_data(void)
  278. {
  279. ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box));
  280. int ret = -EINVAL;
  281. memcpy(rqt, thor_rx_data_buf, sizeof(struct rqt_box));
  282. debug("+RQT: %d, %d\n", rqt->rqt, rqt->rqt_data);
  283. switch (rqt->rqt) {
  284. case RQT_INFO:
  285. ret = process_rqt_info(rqt);
  286. break;
  287. case RQT_CMD:
  288. ret = process_rqt_cmd(rqt);
  289. break;
  290. case RQT_DL:
  291. ret = (int) process_rqt_download(rqt);
  292. break;
  293. case RQT_UL:
  294. puts("RQT: UPLOAD not supported!\n");
  295. break;
  296. default:
  297. error("unknown request (%d)", rqt->rqt);
  298. }
  299. return ret;
  300. }
  301. /* ********************************************************** */
  302. /* THOR USB Function */
  303. /* ********************************************************** */
  304. static inline struct usb_endpoint_descriptor *
  305. ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
  306. struct usb_endpoint_descriptor *fs)
  307. {
  308. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  309. return hs;
  310. return fs;
  311. }
  312. static struct usb_interface_descriptor thor_downloader_intf_data = {
  313. .bLength = sizeof(thor_downloader_intf_data),
  314. .bDescriptorType = USB_DT_INTERFACE,
  315. .bNumEndpoints = 2,
  316. .bInterfaceClass = USB_CLASS_CDC_DATA,
  317. };
  318. static struct usb_endpoint_descriptor fs_in_desc = {
  319. .bLength = USB_DT_ENDPOINT_SIZE,
  320. .bDescriptorType = USB_DT_ENDPOINT,
  321. .bEndpointAddress = USB_DIR_IN,
  322. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  323. };
  324. static struct usb_endpoint_descriptor fs_out_desc = {
  325. .bLength = USB_DT_ENDPOINT_SIZE,
  326. .bDescriptorType = USB_DT_ENDPOINT,
  327. .bEndpointAddress = USB_DIR_OUT,
  328. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  329. };
  330. /* CDC configuration */
  331. static struct usb_interface_descriptor thor_downloader_intf_int = {
  332. .bLength = sizeof(thor_downloader_intf_int),
  333. .bDescriptorType = USB_DT_INTERFACE,
  334. .bNumEndpoints = 1,
  335. .bInterfaceClass = USB_CLASS_COMM,
  336. /* 0x02 Abstract Line Control Model */
  337. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  338. /* 0x01 Common AT commands */
  339. .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
  340. };
  341. static struct usb_cdc_header_desc thor_downloader_cdc_header = {
  342. .bLength = sizeof(thor_downloader_cdc_header),
  343. .bDescriptorType = 0x24, /* CS_INTERFACE */
  344. .bDescriptorSubType = 0x00,
  345. .bcdCDC = 0x0110,
  346. };
  347. static struct usb_cdc_call_mgmt_descriptor thor_downloader_cdc_call = {
  348. .bLength = sizeof(thor_downloader_cdc_call),
  349. .bDescriptorType = 0x24, /* CS_INTERFACE */
  350. .bDescriptorSubType = 0x01,
  351. .bmCapabilities = 0x00,
  352. .bDataInterface = 0x01,
  353. };
  354. static struct usb_cdc_acm_descriptor thor_downloader_cdc_abstract = {
  355. .bLength = sizeof(thor_downloader_cdc_abstract),
  356. .bDescriptorType = 0x24, /* CS_INTERFACE */
  357. .bDescriptorSubType = 0x02,
  358. .bmCapabilities = 0x00,
  359. };
  360. static struct usb_cdc_union_desc thor_downloader_cdc_union = {
  361. .bLength = sizeof(thor_downloader_cdc_union),
  362. .bDescriptorType = 0x24, /* CS_INTERFACE */
  363. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  364. };
  365. static struct usb_endpoint_descriptor fs_int_desc = {
  366. .bLength = USB_DT_ENDPOINT_SIZE,
  367. .bDescriptorType = USB_DT_ENDPOINT,
  368. .bEndpointAddress = 3 | USB_DIR_IN,
  369. .bmAttributes = USB_ENDPOINT_XFER_INT,
  370. .wMaxPacketSize = __constant_cpu_to_le16(16),
  371. .bInterval = 0x9,
  372. };
  373. static struct usb_interface_assoc_descriptor
  374. thor_iad_descriptor = {
  375. .bLength = sizeof(thor_iad_descriptor),
  376. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  377. .bFirstInterface = 0,
  378. .bInterfaceCount = 2, /* control + data */
  379. .bFunctionClass = USB_CLASS_COMM,
  380. .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
  381. .bFunctionProtocol = USB_CDC_PROTO_NONE,
  382. };
  383. static struct usb_endpoint_descriptor hs_in_desc = {
  384. .bLength = USB_DT_ENDPOINT_SIZE,
  385. .bDescriptorType = USB_DT_ENDPOINT,
  386. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  387. .wMaxPacketSize = __constant_cpu_to_le16(512),
  388. };
  389. static struct usb_endpoint_descriptor hs_out_desc = {
  390. .bLength = USB_DT_ENDPOINT_SIZE,
  391. .bDescriptorType = USB_DT_ENDPOINT,
  392. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  393. .wMaxPacketSize = __constant_cpu_to_le16(512),
  394. };
  395. static struct usb_endpoint_descriptor hs_int_desc = {
  396. .bLength = USB_DT_ENDPOINT_SIZE,
  397. .bDescriptorType = USB_DT_ENDPOINT,
  398. .bmAttributes = USB_ENDPOINT_XFER_INT,
  399. .wMaxPacketSize = __constant_cpu_to_le16(16),
  400. .bInterval = 0x9,
  401. };
  402. /*
  403. * This attribute vendor descriptor is necessary for correct operation with
  404. * Windows version of THOR download program
  405. *
  406. * It prevents windows driver from sending zero lenght packet (ZLP) after
  407. * each THOR_PACKET_SIZE. This assures consistent behaviour with libusb
  408. */
  409. static struct usb_cdc_attribute_vendor_descriptor thor_downloader_cdc_av = {
  410. .bLength = sizeof(thor_downloader_cdc_av),
  411. .bDescriptorType = 0x24,
  412. .bDescriptorSubType = 0x80,
  413. .DAUType = 0x0002,
  414. .DAULength = 0x0001,
  415. .DAUValue = 0x00,
  416. };
  417. static const struct usb_descriptor_header *hs_thor_downloader_function[] = {
  418. (struct usb_descriptor_header *)&thor_iad_descriptor,
  419. (struct usb_descriptor_header *)&thor_downloader_intf_int,
  420. (struct usb_descriptor_header *)&thor_downloader_cdc_header,
  421. (struct usb_descriptor_header *)&thor_downloader_cdc_call,
  422. (struct usb_descriptor_header *)&thor_downloader_cdc_abstract,
  423. (struct usb_descriptor_header *)&thor_downloader_cdc_union,
  424. (struct usb_descriptor_header *)&hs_int_desc,
  425. (struct usb_descriptor_header *)&thor_downloader_intf_data,
  426. (struct usb_descriptor_header *)&thor_downloader_cdc_av,
  427. (struct usb_descriptor_header *)&hs_in_desc,
  428. (struct usb_descriptor_header *)&hs_out_desc,
  429. NULL,
  430. };
  431. /*-------------------------------------------------------------------------*/
  432. static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
  433. {
  434. struct usb_request *req;
  435. req = usb_ep_alloc_request(ep, 0);
  436. if (!req)
  437. return req;
  438. req->length = length;
  439. req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
  440. if (!req->buf) {
  441. usb_ep_free_request(ep, req);
  442. req = NULL;
  443. }
  444. return req;
  445. }
  446. static int thor_rx_data(void)
  447. {
  448. struct thor_dev *dev = thor_func->dev;
  449. int data_to_rx, tmp, status;
  450. data_to_rx = dev->out_req->length;
  451. tmp = data_to_rx;
  452. do {
  453. dev->out_req->length = data_to_rx;
  454. debug("dev->out_req->length:%d dev->rxdata:%d\n",
  455. dev->out_req->length, dev->rxdata);
  456. status = usb_ep_queue(dev->out_ep, dev->out_req, 0);
  457. if (status) {
  458. error("kill %s: resubmit %d bytes --> %d",
  459. dev->out_ep->name, dev->out_req->length, status);
  460. usb_ep_set_halt(dev->out_ep);
  461. return -EAGAIN;
  462. }
  463. while (!dev->rxdata) {
  464. usb_gadget_handle_interrupts(0);
  465. if (ctrlc())
  466. return -1;
  467. }
  468. dev->rxdata = 0;
  469. data_to_rx -= dev->out_req->actual;
  470. } while (data_to_rx);
  471. return tmp;
  472. }
  473. static void thor_tx_data(unsigned char *data, int len)
  474. {
  475. struct thor_dev *dev = thor_func->dev;
  476. unsigned char *ptr = dev->in_req->buf;
  477. int status;
  478. memset(ptr, 0, len);
  479. memcpy(ptr, data, len);
  480. dev->in_req->length = len;
  481. debug("%s: dev->in_req->length:%d to_cpy:%zd\n", __func__,
  482. dev->in_req->length, sizeof(data));
  483. status = usb_ep_queue(dev->in_ep, dev->in_req, 0);
  484. if (status) {
  485. error("kill %s: resubmit %d bytes --> %d",
  486. dev->in_ep->name, dev->in_req->length, status);
  487. usb_ep_set_halt(dev->in_ep);
  488. }
  489. /* Wait until tx interrupt received */
  490. while (!dev->txdata)
  491. usb_gadget_handle_interrupts(0);
  492. dev->txdata = 0;
  493. }
  494. static void thor_rx_tx_complete(struct usb_ep *ep, struct usb_request *req)
  495. {
  496. struct thor_dev *dev = thor_func->dev;
  497. int status = req->status;
  498. debug("%s: ep_ptr:%p, req_ptr:%p\n", __func__, ep, req);
  499. switch (status) {
  500. case 0:
  501. if (ep == dev->out_ep)
  502. dev->rxdata = 1;
  503. else
  504. dev->txdata = 1;
  505. break;
  506. /* this endpoint is normally active while we're configured */
  507. case -ECONNABORTED: /* hardware forced ep reset */
  508. case -ECONNRESET: /* request dequeued */
  509. case -ESHUTDOWN: /* disconnect from host */
  510. case -EREMOTEIO: /* short read */
  511. case -EOVERFLOW:
  512. error("ERROR:%d", status);
  513. break;
  514. }
  515. debug("%s complete --> %d, %d/%d\n", ep->name,
  516. status, req->actual, req->length);
  517. }
  518. static struct usb_request *thor_start_ep(struct usb_ep *ep)
  519. {
  520. struct usb_request *req;
  521. req = alloc_ep_req(ep, THOR_PACKET_SIZE);
  522. debug("%s: ep:%p req:%p\n", __func__, ep, req);
  523. if (!req)
  524. return NULL;
  525. memset(req->buf, 0, req->length);
  526. req->complete = thor_rx_tx_complete;
  527. return req;
  528. }
  529. static void thor_setup_complete(struct usb_ep *ep, struct usb_request *req)
  530. {
  531. if (req->status || req->actual != req->length)
  532. debug("setup complete --> %d, %d/%d\n",
  533. req->status, req->actual, req->length);
  534. }
  535. static int
  536. thor_func_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  537. {
  538. struct thor_dev *dev = thor_func->dev;
  539. struct usb_request *req = dev->req;
  540. struct usb_gadget *gadget = dev->gadget;
  541. int value = 0;
  542. u16 len = le16_to_cpu(ctrl->wLength);
  543. debug("Req_Type: 0x%x Req: 0x%x wValue: 0x%x wIndex: 0x%x wLen: 0x%x\n",
  544. ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, ctrl->wIndex,
  545. ctrl->wLength);
  546. switch (ctrl->bRequest) {
  547. case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
  548. value = 0;
  549. break;
  550. case USB_CDC_REQ_SET_LINE_CODING:
  551. value = len;
  552. /* Line Coding set done = configuration done */
  553. thor_func->dev->configuration_done = 1;
  554. break;
  555. default:
  556. error("thor_setup: unknown request: %d", ctrl->bRequest);
  557. }
  558. if (value >= 0) {
  559. req->length = value;
  560. req->zero = value < len;
  561. value = usb_ep_queue(gadget->ep0, req, 0);
  562. if (value < 0) {
  563. debug("%s: ep_queue: %d\n", __func__, value);
  564. req->status = 0;
  565. }
  566. }
  567. return value;
  568. }
  569. /* Specific to the THOR protocol */
  570. static void thor_set_dma(void *addr, int len)
  571. {
  572. struct thor_dev *dev = thor_func->dev;
  573. debug("in_req:%p, out_req:%p\n", dev->in_req, dev->out_req);
  574. debug("addr:%p, len:%d\n", addr, len);
  575. dev->out_req->buf = addr;
  576. dev->out_req->length = len;
  577. }
  578. int thor_init(void)
  579. {
  580. struct thor_dev *dev = thor_func->dev;
  581. /* Wait for a device enumeration and configuration settings */
  582. debug("THOR enumeration/configuration setting....\n");
  583. while (!dev->configuration_done)
  584. usb_gadget_handle_interrupts(0);
  585. thor_set_dma(thor_rx_data_buf, strlen("THOR"));
  586. /* detect the download request from Host PC */
  587. if (thor_rx_data() < 0) {
  588. printf("%s: Data not received!\n", __func__);
  589. return -1;
  590. }
  591. if (!strncmp((char *)thor_rx_data_buf, "THOR", strlen("THOR"))) {
  592. puts("Download request from the Host PC\n");
  593. udelay(30 * 1000); /* 30 ms */
  594. strcpy((char *)thor_tx_data_buf, "ROHT");
  595. thor_tx_data(thor_tx_data_buf, strlen("ROHT"));
  596. } else {
  597. puts("Wrong reply information\n");
  598. return -1;
  599. }
  600. return 0;
  601. }
  602. int thor_handle(void)
  603. {
  604. int ret;
  605. /* receive the data from Host PC */
  606. while (1) {
  607. thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box));
  608. ret = thor_rx_data();
  609. if (ret > 0) {
  610. ret = process_data();
  611. #ifdef CONFIG_THOR_RESET_OFF
  612. if (ret == RESET_DONE)
  613. break;
  614. #endif
  615. if (ret < 0)
  616. return ret;
  617. } else {
  618. printf("%s: No data received!\n", __func__);
  619. break;
  620. }
  621. }
  622. return 0;
  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. free(dev);
  708. return status;
  709. }
  710. static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  711. {
  712. free(req->buf);
  713. usb_ep_free_request(ep, req);
  714. }
  715. static void thor_unbind(struct usb_configuration *c, struct usb_function *f)
  716. {
  717. struct f_thor *f_thor = func_to_thor(f);
  718. struct thor_dev *dev = f_thor->dev;
  719. free(dev);
  720. memset(thor_func, 0, sizeof(*thor_func));
  721. thor_func = NULL;
  722. }
  723. static void thor_func_disable(struct usb_function *f)
  724. {
  725. struct f_thor *f_thor = func_to_thor(f);
  726. struct thor_dev *dev = f_thor->dev;
  727. debug("%s:\n", __func__);
  728. /* Avoid freeing memory when ep is still claimed */
  729. if (dev->in_ep->driver_data) {
  730. free_ep_req(dev->in_ep, dev->in_req);
  731. usb_ep_disable(dev->in_ep);
  732. dev->in_ep->driver_data = NULL;
  733. }
  734. if (dev->out_ep->driver_data) {
  735. dev->out_req->buf = NULL;
  736. usb_ep_free_request(dev->out_ep, dev->out_req);
  737. usb_ep_disable(dev->out_ep);
  738. dev->out_ep->driver_data = NULL;
  739. }
  740. if (dev->int_ep->driver_data) {
  741. usb_ep_disable(dev->int_ep);
  742. dev->int_ep->driver_data = NULL;
  743. }
  744. }
  745. static int thor_eps_setup(struct usb_function *f)
  746. {
  747. struct usb_composite_dev *cdev = f->config->cdev;
  748. struct usb_gadget *gadget = cdev->gadget;
  749. struct thor_dev *dev = thor_func->dev;
  750. struct usb_endpoint_descriptor *d;
  751. struct usb_request *req;
  752. struct usb_ep *ep;
  753. int result;
  754. ep = dev->in_ep;
  755. d = ep_desc(gadget, &hs_in_desc, &fs_in_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->in_req = req;
  768. ep = dev->out_ep;
  769. d = ep_desc(gadget, &hs_out_desc, &fs_out_desc);
  770. debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
  771. result = usb_ep_enable(ep, d);
  772. if (result)
  773. goto exit;
  774. ep->driver_data = cdev; /* claim */
  775. req = thor_start_ep(ep);
  776. if (!req) {
  777. usb_ep_disable(ep);
  778. result = -EIO;
  779. goto exit;
  780. }
  781. dev->out_req = req;
  782. /* ACM control EP */
  783. ep = dev->int_ep;
  784. ep->driver_data = cdev; /* claim */
  785. exit:
  786. return result;
  787. }
  788. static int thor_func_set_alt(struct usb_function *f,
  789. unsigned intf, unsigned alt)
  790. {
  791. struct thor_dev *dev = thor_func->dev;
  792. int result;
  793. debug("%s: func: %s intf: %d alt: %d\n",
  794. __func__, f->name, intf, alt);
  795. switch (intf) {
  796. case 0:
  797. debug("ACM INTR interface\n");
  798. break;
  799. case 1:
  800. debug("Communication Data interface\n");
  801. result = thor_eps_setup(f);
  802. if (result)
  803. error("%s: EPs setup failed!", __func__);
  804. dev->configuration_done = 1;
  805. break;
  806. }
  807. return 0;
  808. }
  809. static int thor_func_init(struct usb_configuration *c)
  810. {
  811. struct f_thor *f_thor;
  812. int status;
  813. debug("%s: cdev: 0x%p\n", __func__, c->cdev);
  814. f_thor = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_thor));
  815. if (!f_thor)
  816. return -ENOMEM;
  817. memset(f_thor, 0, sizeof(*f_thor));
  818. f_thor->usb_function.name = "f_thor";
  819. f_thor->usb_function.bind = thor_func_bind;
  820. f_thor->usb_function.unbind = thor_unbind;
  821. f_thor->usb_function.setup = thor_func_setup;
  822. f_thor->usb_function.set_alt = thor_func_set_alt;
  823. f_thor->usb_function.disable = thor_func_disable;
  824. status = usb_add_function(c, &f_thor->usb_function);
  825. if (status)
  826. free(f_thor);
  827. return status;
  828. }
  829. int thor_add(struct usb_configuration *c)
  830. {
  831. debug("%s:\n", __func__);
  832. return thor_func_init(c);
  833. }
  834. DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor, thor_add);