f_thor.c 25 KB

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