f_thor.c 24 KB

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