cdnsp-ep0.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cadence CDNSP DRD Driver.
  4. *
  5. * Copyright (C) 2020 Cadence.
  6. *
  7. * Author: Pawel Laszczak <[email protected]>
  8. *
  9. */
  10. #include <linux/usb/composite.h>
  11. #include <linux/usb/gadget.h>
  12. #include <linux/list.h>
  13. #include "cdnsp-gadget.h"
  14. #include "cdnsp-trace.h"
  15. static void cdnsp_ep0_stall(struct cdnsp_device *pdev)
  16. {
  17. struct cdnsp_request *preq;
  18. struct cdnsp_ep *pep;
  19. pep = &pdev->eps[0];
  20. preq = next_request(&pep->pending_list);
  21. if (pdev->three_stage_setup) {
  22. cdnsp_halt_endpoint(pdev, pep, true);
  23. if (preq)
  24. cdnsp_gadget_giveback(pep, preq, -ECONNRESET);
  25. } else {
  26. pep->ep_state |= EP0_HALTED_STATUS;
  27. if (preq)
  28. list_del(&preq->list);
  29. cdnsp_status_stage(pdev);
  30. }
  31. }
  32. static int cdnsp_ep0_delegate_req(struct cdnsp_device *pdev,
  33. struct usb_ctrlrequest *ctrl)
  34. {
  35. int ret;
  36. spin_unlock(&pdev->lock);
  37. ret = pdev->gadget_driver->setup(&pdev->gadget, ctrl);
  38. spin_lock(&pdev->lock);
  39. return ret;
  40. }
  41. static int cdnsp_ep0_set_config(struct cdnsp_device *pdev,
  42. struct usb_ctrlrequest *ctrl)
  43. {
  44. enum usb_device_state state = pdev->gadget.state;
  45. u32 cfg;
  46. int ret;
  47. cfg = le16_to_cpu(ctrl->wValue);
  48. switch (state) {
  49. case USB_STATE_ADDRESS:
  50. trace_cdnsp_ep0_set_config("from Address state");
  51. break;
  52. case USB_STATE_CONFIGURED:
  53. trace_cdnsp_ep0_set_config("from Configured state");
  54. break;
  55. default:
  56. dev_err(pdev->dev, "Set Configuration - bad device state\n");
  57. return -EINVAL;
  58. }
  59. ret = cdnsp_ep0_delegate_req(pdev, ctrl);
  60. if (ret)
  61. return ret;
  62. if (!cfg)
  63. usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS);
  64. return 0;
  65. }
  66. static int cdnsp_ep0_set_address(struct cdnsp_device *pdev,
  67. struct usb_ctrlrequest *ctrl)
  68. {
  69. enum usb_device_state state = pdev->gadget.state;
  70. struct cdnsp_slot_ctx *slot_ctx;
  71. unsigned int slot_state;
  72. int ret;
  73. u32 addr;
  74. addr = le16_to_cpu(ctrl->wValue);
  75. if (addr > 127) {
  76. dev_err(pdev->dev, "Invalid device address %d\n", addr);
  77. return -EINVAL;
  78. }
  79. slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
  80. if (state == USB_STATE_CONFIGURED) {
  81. dev_err(pdev->dev, "Can't Set Address from Configured State\n");
  82. return -EINVAL;
  83. }
  84. pdev->device_address = le16_to_cpu(ctrl->wValue);
  85. slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
  86. slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
  87. if (slot_state == SLOT_STATE_ADDRESSED)
  88. cdnsp_reset_device(pdev);
  89. /*set device address*/
  90. ret = cdnsp_setup_device(pdev, SETUP_CONTEXT_ADDRESS);
  91. if (ret)
  92. return ret;
  93. if (addr)
  94. usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS);
  95. else
  96. usb_gadget_set_state(&pdev->gadget, USB_STATE_DEFAULT);
  97. return 0;
  98. }
  99. int cdnsp_status_stage(struct cdnsp_device *pdev)
  100. {
  101. pdev->ep0_stage = CDNSP_STATUS_STAGE;
  102. pdev->ep0_preq.request.length = 0;
  103. return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq);
  104. }
  105. static int cdnsp_w_index_to_ep_index(u16 wIndex)
  106. {
  107. if (!(wIndex & USB_ENDPOINT_NUMBER_MASK))
  108. return 0;
  109. return ((wIndex & USB_ENDPOINT_NUMBER_MASK) * 2) +
  110. (wIndex & USB_ENDPOINT_DIR_MASK ? 1 : 0) - 1;
  111. }
  112. static int cdnsp_ep0_handle_status(struct cdnsp_device *pdev,
  113. struct usb_ctrlrequest *ctrl)
  114. {
  115. struct cdnsp_ep *pep;
  116. __le16 *response;
  117. int ep_sts = 0;
  118. u16 status = 0;
  119. u32 recipient;
  120. recipient = ctrl->bRequestType & USB_RECIP_MASK;
  121. switch (recipient) {
  122. case USB_RECIP_DEVICE:
  123. status = pdev->gadget.is_selfpowered;
  124. status |= pdev->may_wakeup << USB_DEVICE_REMOTE_WAKEUP;
  125. if (pdev->gadget.speed >= USB_SPEED_SUPER) {
  126. status |= pdev->u1_allowed << USB_DEV_STAT_U1_ENABLED;
  127. status |= pdev->u2_allowed << USB_DEV_STAT_U2_ENABLED;
  128. }
  129. break;
  130. case USB_RECIP_INTERFACE:
  131. /*
  132. * Function Remote Wake Capable D0
  133. * Function Remote Wakeup D1
  134. */
  135. return cdnsp_ep0_delegate_req(pdev, ctrl);
  136. case USB_RECIP_ENDPOINT:
  137. ep_sts = cdnsp_w_index_to_ep_index(le16_to_cpu(ctrl->wIndex));
  138. pep = &pdev->eps[ep_sts];
  139. ep_sts = GET_EP_CTX_STATE(pep->out_ctx);
  140. /* check if endpoint is stalled */
  141. if (ep_sts == EP_STATE_HALTED)
  142. status = BIT(USB_ENDPOINT_HALT);
  143. break;
  144. default:
  145. return -EINVAL;
  146. }
  147. response = (__le16 *)pdev->setup_buf;
  148. *response = cpu_to_le16(status);
  149. pdev->ep0_preq.request.length = sizeof(*response);
  150. pdev->ep0_preq.request.buf = pdev->setup_buf;
  151. return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq);
  152. }
  153. static void cdnsp_enter_test_mode(struct cdnsp_device *pdev)
  154. {
  155. u32 temp;
  156. temp = readl(&pdev->active_port->regs->portpmsc) & ~GENMASK(31, 28);
  157. temp |= PORT_TEST_MODE(pdev->test_mode);
  158. writel(temp, &pdev->active_port->regs->portpmsc);
  159. }
  160. static int cdnsp_ep0_handle_feature_device(struct cdnsp_device *pdev,
  161. struct usb_ctrlrequest *ctrl,
  162. int set)
  163. {
  164. enum usb_device_state state;
  165. enum usb_device_speed speed;
  166. u16 tmode;
  167. state = pdev->gadget.state;
  168. speed = pdev->gadget.speed;
  169. switch (le16_to_cpu(ctrl->wValue)) {
  170. case USB_DEVICE_REMOTE_WAKEUP:
  171. pdev->may_wakeup = !!set;
  172. trace_cdnsp_may_wakeup(set);
  173. break;
  174. case USB_DEVICE_U1_ENABLE:
  175. if (state != USB_STATE_CONFIGURED || speed < USB_SPEED_SUPER)
  176. return -EINVAL;
  177. pdev->u1_allowed = !!set;
  178. trace_cdnsp_u1(set);
  179. break;
  180. case USB_DEVICE_U2_ENABLE:
  181. if (state != USB_STATE_CONFIGURED || speed < USB_SPEED_SUPER)
  182. return -EINVAL;
  183. pdev->u2_allowed = !!set;
  184. trace_cdnsp_u2(set);
  185. break;
  186. case USB_DEVICE_LTM_ENABLE:
  187. return -EINVAL;
  188. case USB_DEVICE_TEST_MODE:
  189. if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH)
  190. return -EINVAL;
  191. tmode = le16_to_cpu(ctrl->wIndex);
  192. if (!set || (tmode & 0xff) != 0)
  193. return -EINVAL;
  194. tmode = tmode >> 8;
  195. if (tmode > USB_TEST_FORCE_ENABLE || tmode < USB_TEST_J)
  196. return -EINVAL;
  197. pdev->test_mode = tmode;
  198. /*
  199. * Test mode must be set before Status Stage but controller
  200. * will start testing sequence after Status Stage.
  201. */
  202. cdnsp_enter_test_mode(pdev);
  203. break;
  204. default:
  205. return -EINVAL;
  206. }
  207. return 0;
  208. }
  209. static int cdnsp_ep0_handle_feature_intf(struct cdnsp_device *pdev,
  210. struct usb_ctrlrequest *ctrl,
  211. int set)
  212. {
  213. u16 wValue, wIndex;
  214. int ret;
  215. wValue = le16_to_cpu(ctrl->wValue);
  216. wIndex = le16_to_cpu(ctrl->wIndex);
  217. switch (wValue) {
  218. case USB_INTRF_FUNC_SUSPEND:
  219. ret = cdnsp_ep0_delegate_req(pdev, ctrl);
  220. if (ret)
  221. return ret;
  222. /*
  223. * Remote wakeup is enabled when any function within a device
  224. * is enabled for function remote wakeup.
  225. */
  226. if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
  227. pdev->may_wakeup++;
  228. else
  229. if (pdev->may_wakeup > 0)
  230. pdev->may_wakeup--;
  231. return 0;
  232. default:
  233. return -EINVAL;
  234. }
  235. return 0;
  236. }
  237. static int cdnsp_ep0_handle_feature_endpoint(struct cdnsp_device *pdev,
  238. struct usb_ctrlrequest *ctrl,
  239. int set)
  240. {
  241. struct cdnsp_ep *pep;
  242. u16 wValue;
  243. wValue = le16_to_cpu(ctrl->wValue);
  244. pep = &pdev->eps[cdnsp_w_index_to_ep_index(le16_to_cpu(ctrl->wIndex))];
  245. switch (wValue) {
  246. case USB_ENDPOINT_HALT:
  247. if (!set && (pep->ep_state & EP_WEDGE)) {
  248. /* Resets Sequence Number */
  249. cdnsp_halt_endpoint(pdev, pep, 0);
  250. cdnsp_halt_endpoint(pdev, pep, 1);
  251. break;
  252. }
  253. return cdnsp_halt_endpoint(pdev, pep, set);
  254. default:
  255. dev_warn(pdev->dev, "WARN Incorrect wValue %04x\n", wValue);
  256. return -EINVAL;
  257. }
  258. return 0;
  259. }
  260. static int cdnsp_ep0_handle_feature(struct cdnsp_device *pdev,
  261. struct usb_ctrlrequest *ctrl,
  262. int set)
  263. {
  264. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  265. case USB_RECIP_DEVICE:
  266. return cdnsp_ep0_handle_feature_device(pdev, ctrl, set);
  267. case USB_RECIP_INTERFACE:
  268. return cdnsp_ep0_handle_feature_intf(pdev, ctrl, set);
  269. case USB_RECIP_ENDPOINT:
  270. return cdnsp_ep0_handle_feature_endpoint(pdev, ctrl, set);
  271. default:
  272. return -EINVAL;
  273. }
  274. }
  275. static int cdnsp_ep0_set_sel(struct cdnsp_device *pdev,
  276. struct usb_ctrlrequest *ctrl)
  277. {
  278. enum usb_device_state state = pdev->gadget.state;
  279. u16 wLength;
  280. if (state == USB_STATE_DEFAULT)
  281. return -EINVAL;
  282. wLength = le16_to_cpu(ctrl->wLength);
  283. if (wLength != 6) {
  284. dev_err(pdev->dev, "Set SEL should be 6 bytes, got %d\n",
  285. wLength);
  286. return -EINVAL;
  287. }
  288. /*
  289. * To handle Set SEL we need to receive 6 bytes from Host. So let's
  290. * queue a usb_request for 6 bytes.
  291. */
  292. pdev->ep0_preq.request.length = 6;
  293. pdev->ep0_preq.request.buf = pdev->setup_buf;
  294. return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq);
  295. }
  296. static int cdnsp_ep0_set_isoch_delay(struct cdnsp_device *pdev,
  297. struct usb_ctrlrequest *ctrl)
  298. {
  299. if (le16_to_cpu(ctrl->wIndex) || le16_to_cpu(ctrl->wLength))
  300. return -EINVAL;
  301. pdev->gadget.isoch_delay = le16_to_cpu(ctrl->wValue);
  302. return 0;
  303. }
  304. static int cdnsp_ep0_std_request(struct cdnsp_device *pdev,
  305. struct usb_ctrlrequest *ctrl)
  306. {
  307. int ret;
  308. switch (ctrl->bRequest) {
  309. case USB_REQ_GET_STATUS:
  310. ret = cdnsp_ep0_handle_status(pdev, ctrl);
  311. break;
  312. case USB_REQ_CLEAR_FEATURE:
  313. ret = cdnsp_ep0_handle_feature(pdev, ctrl, 0);
  314. break;
  315. case USB_REQ_SET_FEATURE:
  316. ret = cdnsp_ep0_handle_feature(pdev, ctrl, 1);
  317. break;
  318. case USB_REQ_SET_ADDRESS:
  319. ret = cdnsp_ep0_set_address(pdev, ctrl);
  320. break;
  321. case USB_REQ_SET_CONFIGURATION:
  322. ret = cdnsp_ep0_set_config(pdev, ctrl);
  323. break;
  324. case USB_REQ_SET_SEL:
  325. ret = cdnsp_ep0_set_sel(pdev, ctrl);
  326. break;
  327. case USB_REQ_SET_ISOCH_DELAY:
  328. ret = cdnsp_ep0_set_isoch_delay(pdev, ctrl);
  329. break;
  330. default:
  331. ret = cdnsp_ep0_delegate_req(pdev, ctrl);
  332. break;
  333. }
  334. return ret;
  335. }
  336. void cdnsp_setup_analyze(struct cdnsp_device *pdev)
  337. {
  338. struct usb_ctrlrequest *ctrl = &pdev->setup;
  339. int ret = -EINVAL;
  340. u16 len;
  341. trace_cdnsp_ctrl_req(ctrl);
  342. if (!pdev->gadget_driver)
  343. goto out;
  344. if (pdev->gadget.state == USB_STATE_NOTATTACHED) {
  345. dev_err(pdev->dev, "ERR: Setup detected in unattached state\n");
  346. goto out;
  347. }
  348. /* Restore the ep0 to Stopped/Running state. */
  349. if (pdev->eps[0].ep_state & EP_HALTED) {
  350. trace_cdnsp_ep0_halted("Restore to normal state");
  351. cdnsp_halt_endpoint(pdev, &pdev->eps[0], 0);
  352. }
  353. /*
  354. * Finishing previous SETUP transfer by removing request from
  355. * list and informing upper layer
  356. */
  357. if (!list_empty(&pdev->eps[0].pending_list)) {
  358. struct cdnsp_request *req;
  359. trace_cdnsp_ep0_request("Remove previous");
  360. req = next_request(&pdev->eps[0].pending_list);
  361. cdnsp_ep_dequeue(&pdev->eps[0], req);
  362. }
  363. len = le16_to_cpu(ctrl->wLength);
  364. if (!len) {
  365. pdev->three_stage_setup = false;
  366. pdev->ep0_expect_in = false;
  367. } else {
  368. pdev->three_stage_setup = true;
  369. pdev->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
  370. }
  371. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  372. ret = cdnsp_ep0_std_request(pdev, ctrl);
  373. else
  374. ret = cdnsp_ep0_delegate_req(pdev, ctrl);
  375. if (ret == USB_GADGET_DELAYED_STATUS) {
  376. trace_cdnsp_ep0_status_stage("delayed");
  377. return;
  378. }
  379. out:
  380. if (ret < 0)
  381. cdnsp_ep0_stall(pdev);
  382. else if (!len && pdev->ep0_stage != CDNSP_STATUS_STAGE)
  383. cdnsp_status_stage(pdev);
  384. }