f_thor.c 25 KB

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