dev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget
  4. *
  5. * dev.c - Individual device/gadget management (ie, a port = a gadget)
  6. *
  7. * Copyright 2017 IBM Corporation
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/delay.h>
  13. #include <linux/ioport.h>
  14. #include <linux/slab.h>
  15. #include <linux/errno.h>
  16. #include <linux/list.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/prefetch.h>
  20. #include <linux/clk.h>
  21. #include <linux/usb/gadget.h>
  22. #include <linux/of.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/regmap.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/hcd.h>
  28. #include "vhub.h"
  29. void ast_vhub_dev_irq(struct ast_vhub_dev *d)
  30. {
  31. u32 istat = readl(d->regs + AST_VHUB_DEV_ISR);
  32. writel(istat, d->regs + AST_VHUB_DEV_ISR);
  33. if (istat & VHUV_DEV_IRQ_EP0_IN_ACK_STALL)
  34. ast_vhub_ep0_handle_ack(&d->ep0, true);
  35. if (istat & VHUV_DEV_IRQ_EP0_OUT_ACK_STALL)
  36. ast_vhub_ep0_handle_ack(&d->ep0, false);
  37. if (istat & VHUV_DEV_IRQ_EP0_SETUP)
  38. ast_vhub_ep0_handle_setup(&d->ep0);
  39. }
  40. static void ast_vhub_dev_enable(struct ast_vhub_dev *d)
  41. {
  42. u32 reg, hmsk, i;
  43. if (d->enabled)
  44. return;
  45. /* Cleanup EP0 state */
  46. ast_vhub_reset_ep0(d);
  47. /* Enable device and its EP0 interrupts */
  48. reg = VHUB_DEV_EN_ENABLE_PORT |
  49. VHUB_DEV_EN_EP0_IN_ACK_IRQEN |
  50. VHUB_DEV_EN_EP0_OUT_ACK_IRQEN |
  51. VHUB_DEV_EN_EP0_SETUP_IRQEN;
  52. if (d->gadget.speed == USB_SPEED_HIGH)
  53. reg |= VHUB_DEV_EN_SPEED_SEL_HIGH;
  54. writel(reg, d->regs + AST_VHUB_DEV_EN_CTRL);
  55. /* Enable device interrupt in the hub as well */
  56. hmsk = VHUB_IRQ_DEVICE1 << d->index;
  57. reg = readl(d->vhub->regs + AST_VHUB_IER);
  58. reg |= hmsk;
  59. writel(reg, d->vhub->regs + AST_VHUB_IER);
  60. /* Set EP0 DMA buffer address */
  61. writel(d->ep0.buf_dma, d->regs + AST_VHUB_DEV_EP0_DATA);
  62. /* Clear stall on all EPs */
  63. for (i = 0; i < d->max_epns; i++) {
  64. struct ast_vhub_ep *ep = d->epns[i];
  65. if (ep && (ep->epn.stalled || ep->epn.wedged)) {
  66. ep->epn.stalled = false;
  67. ep->epn.wedged = false;
  68. ast_vhub_update_epn_stall(ep);
  69. }
  70. }
  71. /* Additional cleanups */
  72. d->wakeup_en = false;
  73. d->enabled = true;
  74. }
  75. static void ast_vhub_dev_disable(struct ast_vhub_dev *d)
  76. {
  77. u32 reg, hmsk;
  78. if (!d->enabled)
  79. return;
  80. /* Disable device interrupt in the hub */
  81. hmsk = VHUB_IRQ_DEVICE1 << d->index;
  82. reg = readl(d->vhub->regs + AST_VHUB_IER);
  83. reg &= ~hmsk;
  84. writel(reg, d->vhub->regs + AST_VHUB_IER);
  85. /* Then disable device */
  86. writel(0, d->regs + AST_VHUB_DEV_EN_CTRL);
  87. d->gadget.speed = USB_SPEED_UNKNOWN;
  88. d->enabled = false;
  89. }
  90. static int ast_vhub_dev_feature(struct ast_vhub_dev *d,
  91. u16 wIndex, u16 wValue,
  92. bool is_set)
  93. {
  94. u32 val;
  95. DDBG(d, "%s_FEATURE(dev val=%02x)\n",
  96. is_set ? "SET" : "CLEAR", wValue);
  97. if (wValue == USB_DEVICE_REMOTE_WAKEUP) {
  98. d->wakeup_en = is_set;
  99. return std_req_complete;
  100. }
  101. if (wValue == USB_DEVICE_TEST_MODE) {
  102. val = readl(d->vhub->regs + AST_VHUB_CTRL);
  103. val &= ~GENMASK(10, 8);
  104. val |= VHUB_CTRL_SET_TEST_MODE((wIndex >> 8) & 0x7);
  105. writel(val, d->vhub->regs + AST_VHUB_CTRL);
  106. return std_req_complete;
  107. }
  108. return std_req_driver;
  109. }
  110. static int ast_vhub_ep_feature(struct ast_vhub_dev *d,
  111. u16 wIndex, u16 wValue, bool is_set)
  112. {
  113. struct ast_vhub_ep *ep;
  114. int ep_num;
  115. ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
  116. DDBG(d, "%s_FEATURE(ep%d val=%02x)\n",
  117. is_set ? "SET" : "CLEAR", ep_num, wValue);
  118. if (ep_num == 0)
  119. return std_req_complete;
  120. if (ep_num >= d->max_epns || !d->epns[ep_num - 1])
  121. return std_req_stall;
  122. if (wValue != USB_ENDPOINT_HALT)
  123. return std_req_driver;
  124. ep = d->epns[ep_num - 1];
  125. if (WARN_ON(!ep))
  126. return std_req_stall;
  127. if (!ep->epn.enabled || !ep->ep.desc || ep->epn.is_iso ||
  128. ep->epn.is_in != !!(wIndex & USB_DIR_IN))
  129. return std_req_stall;
  130. DDBG(d, "%s stall on EP %d\n",
  131. is_set ? "setting" : "clearing", ep_num);
  132. ep->epn.stalled = is_set;
  133. ast_vhub_update_epn_stall(ep);
  134. return std_req_complete;
  135. }
  136. static int ast_vhub_dev_status(struct ast_vhub_dev *d,
  137. u16 wIndex, u16 wValue)
  138. {
  139. u8 st0;
  140. DDBG(d, "GET_STATUS(dev)\n");
  141. st0 = d->gadget.is_selfpowered << USB_DEVICE_SELF_POWERED;
  142. if (d->wakeup_en)
  143. st0 |= 1 << USB_DEVICE_REMOTE_WAKEUP;
  144. return ast_vhub_simple_reply(&d->ep0, st0, 0);
  145. }
  146. static int ast_vhub_ep_status(struct ast_vhub_dev *d,
  147. u16 wIndex, u16 wValue)
  148. {
  149. int ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
  150. struct ast_vhub_ep *ep;
  151. u8 st0 = 0;
  152. DDBG(d, "GET_STATUS(ep%d)\n", ep_num);
  153. if (ep_num >= d->max_epns)
  154. return std_req_stall;
  155. if (ep_num != 0) {
  156. ep = d->epns[ep_num - 1];
  157. if (!ep)
  158. return std_req_stall;
  159. if (!ep->epn.enabled || !ep->ep.desc || ep->epn.is_iso ||
  160. ep->epn.is_in != !!(wIndex & USB_DIR_IN))
  161. return std_req_stall;
  162. if (ep->epn.stalled)
  163. st0 |= 1 << USB_ENDPOINT_HALT;
  164. }
  165. return ast_vhub_simple_reply(&d->ep0, st0, 0);
  166. }
  167. static void ast_vhub_dev_set_address(struct ast_vhub_dev *d, u8 addr)
  168. {
  169. u32 reg;
  170. DDBG(d, "SET_ADDRESS: Got address %x\n", addr);
  171. reg = readl(d->regs + AST_VHUB_DEV_EN_CTRL);
  172. reg &= ~VHUB_DEV_EN_ADDR_MASK;
  173. reg |= VHUB_DEV_EN_SET_ADDR(addr);
  174. writel(reg, d->regs + AST_VHUB_DEV_EN_CTRL);
  175. }
  176. int ast_vhub_std_dev_request(struct ast_vhub_ep *ep,
  177. struct usb_ctrlrequest *crq)
  178. {
  179. struct ast_vhub_dev *d = ep->dev;
  180. u16 wValue, wIndex;
  181. /* No driver, we shouldn't be enabled ... */
  182. if (!d->driver || !d->enabled) {
  183. EPDBG(ep,
  184. "Device is wrong state driver=%p enabled=%d\n",
  185. d->driver, d->enabled);
  186. return std_req_stall;
  187. }
  188. /*
  189. * Note: we used to reject/stall requests while suspended,
  190. * we don't do that anymore as we seem to have cases of
  191. * mass storage getting very upset.
  192. */
  193. /* First packet, grab speed */
  194. if (d->gadget.speed == USB_SPEED_UNKNOWN) {
  195. d->gadget.speed = ep->vhub->speed;
  196. if (d->gadget.speed > d->driver->max_speed)
  197. d->gadget.speed = d->driver->max_speed;
  198. DDBG(d, "fist packet, captured speed %d\n",
  199. d->gadget.speed);
  200. }
  201. wValue = le16_to_cpu(crq->wValue);
  202. wIndex = le16_to_cpu(crq->wIndex);
  203. switch ((crq->bRequestType << 8) | crq->bRequest) {
  204. /* SET_ADDRESS */
  205. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  206. ast_vhub_dev_set_address(d, wValue);
  207. return std_req_complete;
  208. /* GET_STATUS */
  209. case DeviceRequest | USB_REQ_GET_STATUS:
  210. return ast_vhub_dev_status(d, wIndex, wValue);
  211. case InterfaceRequest | USB_REQ_GET_STATUS:
  212. return ast_vhub_simple_reply(ep, 0, 0);
  213. case EndpointRequest | USB_REQ_GET_STATUS:
  214. return ast_vhub_ep_status(d, wIndex, wValue);
  215. /* SET/CLEAR_FEATURE */
  216. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  217. return ast_vhub_dev_feature(d, wIndex, wValue, true);
  218. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  219. return ast_vhub_dev_feature(d, wIndex, wValue, false);
  220. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  221. return ast_vhub_ep_feature(d, wIndex, wValue, true);
  222. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  223. return ast_vhub_ep_feature(d, wIndex, wValue, false);
  224. }
  225. return std_req_driver;
  226. }
  227. static int ast_vhub_udc_wakeup(struct usb_gadget* gadget)
  228. {
  229. struct ast_vhub_dev *d = to_ast_dev(gadget);
  230. unsigned long flags;
  231. int rc = -EINVAL;
  232. spin_lock_irqsave(&d->vhub->lock, flags);
  233. if (!d->wakeup_en)
  234. goto err;
  235. DDBG(d, "Device initiated wakeup\n");
  236. /* Wakeup the host */
  237. ast_vhub_hub_wake_all(d->vhub);
  238. rc = 0;
  239. err:
  240. spin_unlock_irqrestore(&d->vhub->lock, flags);
  241. return rc;
  242. }
  243. static int ast_vhub_udc_get_frame(struct usb_gadget* gadget)
  244. {
  245. struct ast_vhub_dev *d = to_ast_dev(gadget);
  246. return (readl(d->vhub->regs + AST_VHUB_USBSTS) >> 16) & 0x7ff;
  247. }
  248. static void ast_vhub_dev_nuke(struct ast_vhub_dev *d)
  249. {
  250. unsigned int i;
  251. for (i = 0; i < d->max_epns; i++) {
  252. if (!d->epns[i])
  253. continue;
  254. ast_vhub_nuke(d->epns[i], -ESHUTDOWN);
  255. }
  256. }
  257. static int ast_vhub_udc_pullup(struct usb_gadget* gadget, int on)
  258. {
  259. struct ast_vhub_dev *d = to_ast_dev(gadget);
  260. unsigned long flags;
  261. spin_lock_irqsave(&d->vhub->lock, flags);
  262. DDBG(d, "pullup(%d)\n", on);
  263. /* Mark disconnected in the hub */
  264. ast_vhub_device_connect(d->vhub, d->index, on);
  265. /*
  266. * If enabled, nuke all requests if any (there shouldn't be)
  267. * and disable the port. This will clear the address too.
  268. */
  269. if (d->enabled) {
  270. ast_vhub_dev_nuke(d);
  271. ast_vhub_dev_disable(d);
  272. }
  273. spin_unlock_irqrestore(&d->vhub->lock, flags);
  274. return 0;
  275. }
  276. static int ast_vhub_udc_start(struct usb_gadget *gadget,
  277. struct usb_gadget_driver *driver)
  278. {
  279. struct ast_vhub_dev *d = to_ast_dev(gadget);
  280. unsigned long flags;
  281. spin_lock_irqsave(&d->vhub->lock, flags);
  282. DDBG(d, "start\n");
  283. /* We don't do much more until the hub enables us */
  284. d->driver = driver;
  285. d->gadget.is_selfpowered = 1;
  286. spin_unlock_irqrestore(&d->vhub->lock, flags);
  287. return 0;
  288. }
  289. static struct usb_ep *ast_vhub_udc_match_ep(struct usb_gadget *gadget,
  290. struct usb_endpoint_descriptor *desc,
  291. struct usb_ss_ep_comp_descriptor *ss)
  292. {
  293. struct ast_vhub_dev *d = to_ast_dev(gadget);
  294. struct ast_vhub_ep *ep;
  295. struct usb_ep *u_ep;
  296. unsigned int max, addr, i;
  297. DDBG(d, "Match EP type %d\n", usb_endpoint_type(desc));
  298. /*
  299. * First we need to look for an existing unclaimed EP as another
  300. * configuration may have already associated a bunch of EPs with
  301. * this gadget. This duplicates the code in usb_ep_autoconfig_ss()
  302. * unfortunately.
  303. */
  304. list_for_each_entry(u_ep, &gadget->ep_list, ep_list) {
  305. if (usb_gadget_ep_match_desc(gadget, u_ep, desc, ss)) {
  306. DDBG(d, " -> using existing EP%d\n",
  307. to_ast_ep(u_ep)->d_idx);
  308. return u_ep;
  309. }
  310. }
  311. /*
  312. * We didn't find one, we need to grab one from the pool.
  313. *
  314. * First let's do some sanity checking
  315. */
  316. switch(usb_endpoint_type(desc)) {
  317. case USB_ENDPOINT_XFER_CONTROL:
  318. /* Only EP0 can be a control endpoint */
  319. return NULL;
  320. case USB_ENDPOINT_XFER_ISOC:
  321. /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
  322. if (gadget_is_dualspeed(gadget))
  323. max = 1024;
  324. else
  325. max = 1023;
  326. break;
  327. case USB_ENDPOINT_XFER_BULK:
  328. if (gadget_is_dualspeed(gadget))
  329. max = 512;
  330. else
  331. max = 64;
  332. break;
  333. case USB_ENDPOINT_XFER_INT:
  334. if (gadget_is_dualspeed(gadget))
  335. max = 1024;
  336. else
  337. max = 64;
  338. break;
  339. }
  340. if (usb_endpoint_maxp(desc) > max)
  341. return NULL;
  342. /*
  343. * Find a free EP address for that device. We can't
  344. * let the generic code assign these as it would
  345. * create overlapping numbers for IN and OUT which
  346. * we don't support, so also create a suitable name
  347. * that will allow the generic code to use our
  348. * assigned address.
  349. */
  350. for (i = 0; i < d->max_epns; i++)
  351. if (d->epns[i] == NULL)
  352. break;
  353. if (i >= d->max_epns)
  354. return NULL;
  355. addr = i + 1;
  356. /*
  357. * Now grab an EP from the shared pool and associate
  358. * it with our device
  359. */
  360. ep = ast_vhub_alloc_epn(d, addr);
  361. if (!ep)
  362. return NULL;
  363. DDBG(d, "Allocated epn#%d for port EP%d\n",
  364. ep->epn.g_idx, addr);
  365. return &ep->ep;
  366. }
  367. static int ast_vhub_udc_stop(struct usb_gadget *gadget)
  368. {
  369. struct ast_vhub_dev *d = to_ast_dev(gadget);
  370. unsigned long flags;
  371. spin_lock_irqsave(&d->vhub->lock, flags);
  372. DDBG(d, "stop\n");
  373. d->driver = NULL;
  374. d->gadget.speed = USB_SPEED_UNKNOWN;
  375. ast_vhub_dev_nuke(d);
  376. if (d->enabled)
  377. ast_vhub_dev_disable(d);
  378. spin_unlock_irqrestore(&d->vhub->lock, flags);
  379. return 0;
  380. }
  381. static const struct usb_gadget_ops ast_vhub_udc_ops = {
  382. .get_frame = ast_vhub_udc_get_frame,
  383. .wakeup = ast_vhub_udc_wakeup,
  384. .pullup = ast_vhub_udc_pullup,
  385. .udc_start = ast_vhub_udc_start,
  386. .udc_stop = ast_vhub_udc_stop,
  387. .match_ep = ast_vhub_udc_match_ep,
  388. };
  389. void ast_vhub_dev_suspend(struct ast_vhub_dev *d)
  390. {
  391. if (d->driver && d->driver->suspend) {
  392. spin_unlock(&d->vhub->lock);
  393. d->driver->suspend(&d->gadget);
  394. spin_lock(&d->vhub->lock);
  395. }
  396. }
  397. void ast_vhub_dev_resume(struct ast_vhub_dev *d)
  398. {
  399. if (d->driver && d->driver->resume) {
  400. spin_unlock(&d->vhub->lock);
  401. d->driver->resume(&d->gadget);
  402. spin_lock(&d->vhub->lock);
  403. }
  404. }
  405. void ast_vhub_dev_reset(struct ast_vhub_dev *d)
  406. {
  407. /* No driver, just disable the device and return */
  408. if (!d->driver) {
  409. ast_vhub_dev_disable(d);
  410. return;
  411. }
  412. /* If the port isn't enabled, just enable it */
  413. if (!d->enabled) {
  414. DDBG(d, "Reset of disabled device, enabling...\n");
  415. ast_vhub_dev_enable(d);
  416. } else {
  417. DDBG(d, "Reset of enabled device, resetting...\n");
  418. spin_unlock(&d->vhub->lock);
  419. usb_gadget_udc_reset(&d->gadget, d->driver);
  420. spin_lock(&d->vhub->lock);
  421. /*
  422. * Disable and maybe re-enable HW, this will clear the address
  423. * and speed setting.
  424. */
  425. ast_vhub_dev_disable(d);
  426. ast_vhub_dev_enable(d);
  427. }
  428. }
  429. void ast_vhub_del_dev(struct ast_vhub_dev *d)
  430. {
  431. unsigned long flags;
  432. spin_lock_irqsave(&d->vhub->lock, flags);
  433. if (!d->registered) {
  434. spin_unlock_irqrestore(&d->vhub->lock, flags);
  435. return;
  436. }
  437. d->registered = false;
  438. spin_unlock_irqrestore(&d->vhub->lock, flags);
  439. usb_del_gadget_udc(&d->gadget);
  440. device_unregister(d->port_dev);
  441. kfree(d->epns);
  442. }
  443. static void ast_vhub_dev_release(struct device *dev)
  444. {
  445. kfree(dev);
  446. }
  447. int ast_vhub_init_dev(struct ast_vhub *vhub, unsigned int idx)
  448. {
  449. struct ast_vhub_dev *d = &vhub->ports[idx].dev;
  450. struct device *parent = &vhub->pdev->dev;
  451. int rc;
  452. d->vhub = vhub;
  453. d->index = idx;
  454. d->name = devm_kasprintf(parent, GFP_KERNEL, "port%d", idx+1);
  455. d->regs = vhub->regs + 0x100 + 0x10 * idx;
  456. ast_vhub_init_ep0(vhub, &d->ep0, d);
  457. /*
  458. * A USB device can have up to 30 endpoints besides control
  459. * endpoint 0.
  460. */
  461. d->max_epns = min_t(u32, vhub->max_epns, 30);
  462. d->epns = kcalloc(d->max_epns, sizeof(*d->epns), GFP_KERNEL);
  463. if (!d->epns)
  464. return -ENOMEM;
  465. /*
  466. * The UDC core really needs us to have separate and uniquely
  467. * named "parent" devices for each port so we create a sub device
  468. * here for that purpose
  469. */
  470. d->port_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  471. if (!d->port_dev) {
  472. rc = -ENOMEM;
  473. goto fail_alloc;
  474. }
  475. device_initialize(d->port_dev);
  476. d->port_dev->release = ast_vhub_dev_release;
  477. d->port_dev->parent = parent;
  478. dev_set_name(d->port_dev, "%s:p%d", dev_name(parent), idx + 1);
  479. rc = device_add(d->port_dev);
  480. if (rc)
  481. goto fail_add;
  482. /* Populate gadget */
  483. INIT_LIST_HEAD(&d->gadget.ep_list);
  484. d->gadget.ops = &ast_vhub_udc_ops;
  485. d->gadget.ep0 = &d->ep0.ep;
  486. d->gadget.name = KBUILD_MODNAME;
  487. if (vhub->force_usb1)
  488. d->gadget.max_speed = USB_SPEED_FULL;
  489. else
  490. d->gadget.max_speed = USB_SPEED_HIGH;
  491. d->gadget.speed = USB_SPEED_UNKNOWN;
  492. d->gadget.dev.of_node = vhub->pdev->dev.of_node;
  493. d->gadget.dev.of_node_reused = true;
  494. rc = usb_add_gadget_udc(d->port_dev, &d->gadget);
  495. if (rc != 0)
  496. goto fail_udc;
  497. d->registered = true;
  498. return 0;
  499. fail_udc:
  500. device_del(d->port_dev);
  501. fail_add:
  502. put_device(d->port_dev);
  503. fail_alloc:
  504. kfree(d->epns);
  505. return rc;
  506. }