dev.c 14 KB

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