f_thor.c 25 KB

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