f_thor.c 24 KB

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