epn.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget
  4. *
  5. * epn.c - Generic endpoints management
  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 "vhub.h"
  27. #define EXTRA_CHECKS
  28. #ifdef EXTRA_CHECKS
  29. #define CHECK(ep, expr, fmt...) \
  30. do { \
  31. if (!(expr)) EPDBG(ep, "CHECK:" fmt); \
  32. } while(0)
  33. #else
  34. #define CHECK(ep, expr, fmt...) do { } while(0)
  35. #endif
  36. static void ast_vhub_epn_kick(struct ast_vhub_ep *ep, struct ast_vhub_req *req)
  37. {
  38. unsigned int act = req->req.actual;
  39. unsigned int len = req->req.length;
  40. unsigned int chunk;
  41. /* There should be no DMA ongoing */
  42. WARN_ON(req->active);
  43. /* Calculate next chunk size */
  44. chunk = len - act;
  45. if (chunk > ep->ep.maxpacket)
  46. chunk = ep->ep.maxpacket;
  47. else if ((chunk < ep->ep.maxpacket) || !req->req.zero)
  48. req->last_desc = 1;
  49. EPVDBG(ep, "kick req %p act=%d/%d chunk=%d last=%d\n",
  50. req, act, len, chunk, req->last_desc);
  51. /* If DMA unavailable, using staging EP buffer */
  52. if (!req->req.dma) {
  53. /* For IN transfers, copy data over first */
  54. if (ep->epn.is_in) {
  55. memcpy(ep->buf, req->req.buf + act, chunk);
  56. vhub_dma_workaround(ep->buf);
  57. }
  58. writel(ep->buf_dma, ep->epn.regs + AST_VHUB_EP_DESC_BASE);
  59. } else {
  60. if (ep->epn.is_in)
  61. vhub_dma_workaround(req->req.buf);
  62. writel(req->req.dma + act, ep->epn.regs + AST_VHUB_EP_DESC_BASE);
  63. }
  64. /* Start DMA */
  65. req->active = true;
  66. writel(VHUB_EP_DMA_SET_TX_SIZE(chunk),
  67. ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
  68. writel(VHUB_EP_DMA_SET_TX_SIZE(chunk) | VHUB_EP_DMA_SINGLE_KICK,
  69. ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
  70. }
  71. static void ast_vhub_epn_handle_ack(struct ast_vhub_ep *ep)
  72. {
  73. struct ast_vhub_req *req;
  74. unsigned int len;
  75. u32 stat;
  76. /* Read EP status */
  77. stat = readl(ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
  78. /* Grab current request if any */
  79. req = list_first_entry_or_null(&ep->queue, struct ast_vhub_req, queue);
  80. EPVDBG(ep, "ACK status=%08x is_in=%d, req=%p (active=%d)\n",
  81. stat, ep->epn.is_in, req, req ? req->active : 0);
  82. /* In absence of a request, bail out, must have been dequeued */
  83. if (!req)
  84. return;
  85. /*
  86. * Request not active, move on to processing queue, active request
  87. * was probably dequeued
  88. */
  89. if (!req->active)
  90. goto next_chunk;
  91. /* Check if HW has moved on */
  92. if (VHUB_EP_DMA_RPTR(stat) != 0) {
  93. EPDBG(ep, "DMA read pointer not 0 !\n");
  94. return;
  95. }
  96. /* No current DMA ongoing */
  97. req->active = false;
  98. /* Grab length out of HW */
  99. len = VHUB_EP_DMA_TX_SIZE(stat);
  100. /* If not using DMA, copy data out if needed */
  101. if (!req->req.dma && !ep->epn.is_in && len)
  102. memcpy(req->req.buf + req->req.actual, ep->buf, len);
  103. /* Adjust size */
  104. req->req.actual += len;
  105. /* Check for short packet */
  106. if (len < ep->ep.maxpacket)
  107. req->last_desc = 1;
  108. /* That's it ? complete the request and pick a new one */
  109. if (req->last_desc >= 0) {
  110. ast_vhub_done(ep, req, 0);
  111. req = list_first_entry_or_null(&ep->queue, struct ast_vhub_req,
  112. queue);
  113. /*
  114. * Due to lock dropping inside "done" the next request could
  115. * already be active, so check for that and bail if needed.
  116. */
  117. if (!req || req->active)
  118. return;
  119. }
  120. next_chunk:
  121. ast_vhub_epn_kick(ep, req);
  122. }
  123. static inline unsigned int ast_vhub_count_free_descs(struct ast_vhub_ep *ep)
  124. {
  125. /*
  126. * d_next == d_last means descriptor list empty to HW,
  127. * thus we can only have AST_VHUB_DESCS_COUNT-1 descriptors
  128. * in the list
  129. */
  130. return (ep->epn.d_last + AST_VHUB_DESCS_COUNT - ep->epn.d_next - 1) &
  131. (AST_VHUB_DESCS_COUNT - 1);
  132. }
  133. static void ast_vhub_epn_kick_desc(struct ast_vhub_ep *ep,
  134. struct ast_vhub_req *req)
  135. {
  136. struct ast_vhub_desc *desc = NULL;
  137. unsigned int act = req->act_count;
  138. unsigned int len = req->req.length;
  139. unsigned int chunk;
  140. /* Mark request active if not already */
  141. req->active = true;
  142. /* If the request was already completely written, do nothing */
  143. if (req->last_desc >= 0)
  144. return;
  145. EPVDBG(ep, "kick act=%d/%d chunk_max=%d free_descs=%d\n",
  146. act, len, ep->epn.chunk_max, ast_vhub_count_free_descs(ep));
  147. /* While we can create descriptors */
  148. while (ast_vhub_count_free_descs(ep) && req->last_desc < 0) {
  149. unsigned int d_num;
  150. /* Grab next free descriptor */
  151. d_num = ep->epn.d_next;
  152. desc = &ep->epn.descs[d_num];
  153. ep->epn.d_next = (d_num + 1) & (AST_VHUB_DESCS_COUNT - 1);
  154. /* Calculate next chunk size */
  155. chunk = len - act;
  156. if (chunk <= ep->epn.chunk_max) {
  157. /*
  158. * Is this the last packet ? Because of having up to 8
  159. * packets in a descriptor we can't just compare "chunk"
  160. * with ep.maxpacket. We have to see if it's a multiple
  161. * of it to know if we have to send a zero packet.
  162. * Sadly that involves a modulo which is a bit expensive
  163. * but probably still better than not doing it.
  164. */
  165. if (!chunk || !req->req.zero || (chunk % ep->ep.maxpacket) != 0)
  166. req->last_desc = d_num;
  167. } else {
  168. chunk = ep->epn.chunk_max;
  169. }
  170. EPVDBG(ep, " chunk: act=%d/%d chunk=%d last=%d desc=%d free=%d\n",
  171. act, len, chunk, req->last_desc, d_num,
  172. ast_vhub_count_free_descs(ep));
  173. /* Populate descriptor */
  174. desc->w0 = cpu_to_le32(req->req.dma + act);
  175. /* Interrupt if end of request or no more descriptors */
  176. /*
  177. * TODO: Be smarter about it, if we don't have enough
  178. * descriptors request an interrupt before queue empty
  179. * or so in order to be able to populate more before
  180. * the HW runs out. This isn't a problem at the moment
  181. * as we use 256 descriptors and only put at most one
  182. * request in the ring.
  183. */
  184. desc->w1 = cpu_to_le32(VHUB_DSC1_IN_SET_LEN(chunk));
  185. if (req->last_desc >= 0 || !ast_vhub_count_free_descs(ep))
  186. desc->w1 |= cpu_to_le32(VHUB_DSC1_IN_INTERRUPT);
  187. /* Account packet */
  188. req->act_count = act = act + chunk;
  189. }
  190. if (likely(desc))
  191. vhub_dma_workaround(desc);
  192. /* Tell HW about new descriptors */
  193. writel(VHUB_EP_DMA_SET_CPU_WPTR(ep->epn.d_next),
  194. ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
  195. EPVDBG(ep, "HW kicked, d_next=%d dstat=%08x\n",
  196. ep->epn.d_next, readl(ep->epn.regs + AST_VHUB_EP_DESC_STATUS));
  197. }
  198. static void ast_vhub_epn_handle_ack_desc(struct ast_vhub_ep *ep)
  199. {
  200. struct ast_vhub_req *req;
  201. unsigned int len, d_last;
  202. u32 stat, stat1;
  203. /* Read EP status, workaround HW race */
  204. do {
  205. stat = readl(ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
  206. stat1 = readl(ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
  207. } while(stat != stat1);
  208. /* Extract RPTR */
  209. d_last = VHUB_EP_DMA_RPTR(stat);
  210. /* Grab current request if any */
  211. req = list_first_entry_or_null(&ep->queue, struct ast_vhub_req, queue);
  212. EPVDBG(ep, "ACK status=%08x is_in=%d ep->d_last=%d..%d\n",
  213. stat, ep->epn.is_in, ep->epn.d_last, d_last);
  214. /* Check all completed descriptors */
  215. while (ep->epn.d_last != d_last) {
  216. struct ast_vhub_desc *desc;
  217. unsigned int d_num;
  218. bool is_last_desc;
  219. /* Grab next completed descriptor */
  220. d_num = ep->epn.d_last;
  221. desc = &ep->epn.descs[d_num];
  222. ep->epn.d_last = (d_num + 1) & (AST_VHUB_DESCS_COUNT - 1);
  223. /* Grab len out of descriptor */
  224. len = VHUB_DSC1_IN_LEN(le32_to_cpu(desc->w1));
  225. EPVDBG(ep, " desc %d len=%d req=%p (act=%d)\n",
  226. d_num, len, req, req ? req->active : 0);
  227. /* If no active request pending, move on */
  228. if (!req || !req->active)
  229. continue;
  230. /* Adjust size */
  231. req->req.actual += len;
  232. /* Is that the last chunk ? */
  233. is_last_desc = req->last_desc == d_num;
  234. CHECK(ep, is_last_desc == (len < ep->ep.maxpacket ||
  235. (req->req.actual >= req->req.length &&
  236. !req->req.zero)),
  237. "Last packet discrepancy: last_desc=%d len=%d r.act=%d "
  238. "r.len=%d r.zero=%d mp=%d\n",
  239. is_last_desc, len, req->req.actual, req->req.length,
  240. req->req.zero, ep->ep.maxpacket);
  241. if (is_last_desc) {
  242. /*
  243. * Because we can only have one request at a time
  244. * in our descriptor list in this implementation,
  245. * d_last and ep->d_last should now be equal
  246. */
  247. CHECK(ep, d_last == ep->epn.d_last,
  248. "DMA read ptr mismatch %d vs %d\n",
  249. d_last, ep->epn.d_last);
  250. /* Note: done will drop and re-acquire the lock */
  251. ast_vhub_done(ep, req, 0);
  252. req = list_first_entry_or_null(&ep->queue,
  253. struct ast_vhub_req,
  254. queue);
  255. break;
  256. }
  257. }
  258. /* More work ? */
  259. if (req)
  260. ast_vhub_epn_kick_desc(ep, req);
  261. }
  262. void ast_vhub_epn_ack_irq(struct ast_vhub_ep *ep)
  263. {
  264. if (ep->epn.desc_mode)
  265. ast_vhub_epn_handle_ack_desc(ep);
  266. else
  267. ast_vhub_epn_handle_ack(ep);
  268. }
  269. static int ast_vhub_epn_queue(struct usb_ep* u_ep, struct usb_request *u_req,
  270. gfp_t gfp_flags)
  271. {
  272. struct ast_vhub_req *req = to_ast_req(u_req);
  273. struct ast_vhub_ep *ep = to_ast_ep(u_ep);
  274. struct ast_vhub *vhub = ep->vhub;
  275. unsigned long flags;
  276. bool empty;
  277. int rc;
  278. /* Paranoid checks */
  279. if (!u_req || !u_req->complete || !u_req->buf) {
  280. dev_warn(&vhub->pdev->dev, "Bogus EPn request ! u_req=%p\n", u_req);
  281. if (u_req) {
  282. dev_warn(&vhub->pdev->dev, "complete=%p internal=%d\n",
  283. u_req->complete, req->internal);
  284. }
  285. return -EINVAL;
  286. }
  287. /* Endpoint enabled ? */
  288. if (!ep->epn.enabled || !u_ep->desc || !ep->dev || !ep->d_idx ||
  289. !ep->dev->enabled) {
  290. EPDBG(ep, "Enqueuing request on wrong or disabled EP\n");
  291. return -ESHUTDOWN;
  292. }
  293. /* Map request for DMA if possible. For now, the rule for DMA is
  294. * that:
  295. *
  296. * * For single stage mode (no descriptors):
  297. *
  298. * - The buffer is aligned to a 8 bytes boundary (HW requirement)
  299. * - For a OUT endpoint, the request size is a multiple of the EP
  300. * packet size (otherwise the controller will DMA past the end
  301. * of the buffer if the host is sending a too long packet).
  302. *
  303. * * For descriptor mode (tx only for now), always.
  304. *
  305. * We could relax the latter by making the decision to use the bounce
  306. * buffer based on the size of a given *segment* of the request rather
  307. * than the whole request.
  308. */
  309. if (ep->epn.desc_mode ||
  310. ((((unsigned long)u_req->buf & 7) == 0) &&
  311. (ep->epn.is_in || !(u_req->length & (u_ep->maxpacket - 1))))) {
  312. rc = usb_gadget_map_request_by_dev(&vhub->pdev->dev, u_req,
  313. ep->epn.is_in);
  314. if (rc) {
  315. dev_warn(&vhub->pdev->dev,
  316. "Request mapping failure %d\n", rc);
  317. return rc;
  318. }
  319. } else
  320. u_req->dma = 0;
  321. EPVDBG(ep, "enqueue req @%p\n", req);
  322. EPVDBG(ep, " l=%d dma=0x%x zero=%d noshort=%d noirq=%d is_in=%d\n",
  323. u_req->length, (u32)u_req->dma, u_req->zero,
  324. u_req->short_not_ok, u_req->no_interrupt,
  325. ep->epn.is_in);
  326. /* Initialize request progress fields */
  327. u_req->status = -EINPROGRESS;
  328. u_req->actual = 0;
  329. req->act_count = 0;
  330. req->active = false;
  331. req->last_desc = -1;
  332. spin_lock_irqsave(&vhub->lock, flags);
  333. empty = list_empty(&ep->queue);
  334. /* Add request to list and kick processing if empty */
  335. list_add_tail(&req->queue, &ep->queue);
  336. if (empty) {
  337. if (ep->epn.desc_mode)
  338. ast_vhub_epn_kick_desc(ep, req);
  339. else
  340. ast_vhub_epn_kick(ep, req);
  341. }
  342. spin_unlock_irqrestore(&vhub->lock, flags);
  343. return 0;
  344. }
  345. static void ast_vhub_stop_active_req(struct ast_vhub_ep *ep,
  346. bool restart_ep)
  347. {
  348. u32 state, reg, loops;
  349. /* Stop DMA activity */
  350. if (ep->epn.desc_mode)
  351. writel(VHUB_EP_DMA_CTRL_RESET, ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
  352. else
  353. writel(0, ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
  354. /* Wait for it to complete */
  355. for (loops = 0; loops < 1000; loops++) {
  356. state = readl(ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
  357. state = VHUB_EP_DMA_PROC_STATUS(state);
  358. if (state == EP_DMA_PROC_RX_IDLE ||
  359. state == EP_DMA_PROC_TX_IDLE)
  360. break;
  361. udelay(1);
  362. }
  363. if (loops >= 1000)
  364. dev_warn(&ep->vhub->pdev->dev, "Timeout waiting for DMA\n");
  365. /* If we don't have to restart the endpoint, that's it */
  366. if (!restart_ep)
  367. return;
  368. /* Restart the endpoint */
  369. if (ep->epn.desc_mode) {
  370. /*
  371. * Take out descriptors by resetting the DMA read
  372. * pointer to be equal to the CPU write pointer.
  373. *
  374. * Note: If we ever support creating descriptors for
  375. * requests that aren't the head of the queue, we
  376. * may have to do something more complex here,
  377. * especially if the request being taken out is
  378. * not the current head descriptors.
  379. */
  380. reg = VHUB_EP_DMA_SET_RPTR(ep->epn.d_next) |
  381. VHUB_EP_DMA_SET_CPU_WPTR(ep->epn.d_next);
  382. writel(reg, ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
  383. /* Then turn it back on */
  384. writel(ep->epn.dma_conf,
  385. ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
  386. } else {
  387. /* Single mode: just turn it back on */
  388. writel(ep->epn.dma_conf,
  389. ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
  390. }
  391. }
  392. static int ast_vhub_epn_dequeue(struct usb_ep* u_ep, struct usb_request *u_req)
  393. {
  394. struct ast_vhub_ep *ep = to_ast_ep(u_ep);
  395. struct ast_vhub *vhub = ep->vhub;
  396. struct ast_vhub_req *req = NULL, *iter;
  397. unsigned long flags;
  398. int rc = -EINVAL;
  399. spin_lock_irqsave(&vhub->lock, flags);
  400. /* Make sure it's actually queued on this endpoint */
  401. list_for_each_entry(iter, &ep->queue, queue) {
  402. if (&iter->req != u_req)
  403. continue;
  404. req = iter;
  405. break;
  406. }
  407. if (req) {
  408. EPVDBG(ep, "dequeue req @%p active=%d\n",
  409. req, req->active);
  410. if (req->active)
  411. ast_vhub_stop_active_req(ep, true);
  412. ast_vhub_done(ep, req, -ECONNRESET);
  413. rc = 0;
  414. }
  415. spin_unlock_irqrestore(&vhub->lock, flags);
  416. return rc;
  417. }
  418. void ast_vhub_update_epn_stall(struct ast_vhub_ep *ep)
  419. {
  420. u32 reg;
  421. if (WARN_ON(ep->d_idx == 0))
  422. return;
  423. reg = readl(ep->epn.regs + AST_VHUB_EP_CONFIG);
  424. if (ep->epn.stalled || ep->epn.wedged)
  425. reg |= VHUB_EP_CFG_STALL_CTRL;
  426. else
  427. reg &= ~VHUB_EP_CFG_STALL_CTRL;
  428. writel(reg, ep->epn.regs + AST_VHUB_EP_CONFIG);
  429. if (!ep->epn.stalled && !ep->epn.wedged)
  430. writel(VHUB_EP_TOGGLE_SET_EPNUM(ep->epn.g_idx),
  431. ep->vhub->regs + AST_VHUB_EP_TOGGLE);
  432. }
  433. static int ast_vhub_set_halt_and_wedge(struct usb_ep* u_ep, bool halt,
  434. bool wedge)
  435. {
  436. struct ast_vhub_ep *ep = to_ast_ep(u_ep);
  437. struct ast_vhub *vhub = ep->vhub;
  438. unsigned long flags;
  439. EPDBG(ep, "Set halt (%d) & wedge (%d)\n", halt, wedge);
  440. if (!u_ep || !u_ep->desc)
  441. return -EINVAL;
  442. if (ep->d_idx == 0)
  443. return 0;
  444. if (ep->epn.is_iso)
  445. return -EOPNOTSUPP;
  446. spin_lock_irqsave(&vhub->lock, flags);
  447. /* Fail with still-busy IN endpoints */
  448. if (halt && ep->epn.is_in && !list_empty(&ep->queue)) {
  449. spin_unlock_irqrestore(&vhub->lock, flags);
  450. return -EAGAIN;
  451. }
  452. ep->epn.stalled = halt;
  453. ep->epn.wedged = wedge;
  454. ast_vhub_update_epn_stall(ep);
  455. spin_unlock_irqrestore(&vhub->lock, flags);
  456. return 0;
  457. }
  458. static int ast_vhub_epn_set_halt(struct usb_ep *u_ep, int value)
  459. {
  460. return ast_vhub_set_halt_and_wedge(u_ep, value != 0, false);
  461. }
  462. static int ast_vhub_epn_set_wedge(struct usb_ep *u_ep)
  463. {
  464. return ast_vhub_set_halt_and_wedge(u_ep, true, true);
  465. }
  466. static int ast_vhub_epn_disable(struct usb_ep* u_ep)
  467. {
  468. struct ast_vhub_ep *ep = to_ast_ep(u_ep);
  469. struct ast_vhub *vhub = ep->vhub;
  470. unsigned long flags;
  471. u32 imask, ep_ier;
  472. EPDBG(ep, "Disabling !\n");
  473. spin_lock_irqsave(&vhub->lock, flags);
  474. ep->epn.enabled = false;
  475. /* Stop active DMA if any */
  476. ast_vhub_stop_active_req(ep, false);
  477. /* Disable endpoint */
  478. writel(0, ep->epn.regs + AST_VHUB_EP_CONFIG);
  479. /* Disable ACK interrupt */
  480. imask = VHUB_EP_IRQ(ep->epn.g_idx);
  481. ep_ier = readl(vhub->regs + AST_VHUB_EP_ACK_IER);
  482. ep_ier &= ~imask;
  483. writel(ep_ier, vhub->regs + AST_VHUB_EP_ACK_IER);
  484. writel(imask, vhub->regs + AST_VHUB_EP_ACK_ISR);
  485. /* Nuke all pending requests */
  486. ast_vhub_nuke(ep, -ESHUTDOWN);
  487. /* No more descriptor associated with request */
  488. ep->ep.desc = NULL;
  489. spin_unlock_irqrestore(&vhub->lock, flags);
  490. return 0;
  491. }
  492. static int ast_vhub_epn_enable(struct usb_ep* u_ep,
  493. const struct usb_endpoint_descriptor *desc)
  494. {
  495. struct ast_vhub_ep *ep = to_ast_ep(u_ep);
  496. struct ast_vhub_dev *dev;
  497. struct ast_vhub *vhub;
  498. u16 maxpacket, type;
  499. unsigned long flags;
  500. u32 ep_conf, ep_ier, imask;
  501. /* Check arguments */
  502. if (!u_ep || !desc)
  503. return -EINVAL;
  504. maxpacket = usb_endpoint_maxp(desc);
  505. if (!ep->d_idx || !ep->dev ||
  506. desc->bDescriptorType != USB_DT_ENDPOINT ||
  507. maxpacket == 0 || maxpacket > ep->ep.maxpacket) {
  508. EPDBG(ep, "Invalid EP enable,d_idx=%d,dev=%p,type=%d,mp=%d/%d\n",
  509. ep->d_idx, ep->dev, desc->bDescriptorType,
  510. maxpacket, ep->ep.maxpacket);
  511. return -EINVAL;
  512. }
  513. if (ep->d_idx != usb_endpoint_num(desc)) {
  514. EPDBG(ep, "EP number mismatch !\n");
  515. return -EINVAL;
  516. }
  517. if (ep->epn.enabled) {
  518. EPDBG(ep, "Already enabled\n");
  519. return -EBUSY;
  520. }
  521. dev = ep->dev;
  522. vhub = ep->vhub;
  523. /* Check device state */
  524. if (!dev->driver) {
  525. EPDBG(ep, "Bogus device state: driver=%p speed=%d\n",
  526. dev->driver, dev->gadget.speed);
  527. return -ESHUTDOWN;
  528. }
  529. /* Grab some info from the descriptor */
  530. ep->epn.is_in = usb_endpoint_dir_in(desc);
  531. ep->ep.maxpacket = maxpacket;
  532. type = usb_endpoint_type(desc);
  533. ep->epn.d_next = ep->epn.d_last = 0;
  534. ep->epn.is_iso = false;
  535. ep->epn.stalled = false;
  536. ep->epn.wedged = false;
  537. EPDBG(ep, "Enabling [%s] %s num %d maxpacket=%d\n",
  538. ep->epn.is_in ? "in" : "out", usb_ep_type_string(type),
  539. usb_endpoint_num(desc), maxpacket);
  540. /* Can we use DMA descriptor mode ? */
  541. ep->epn.desc_mode = ep->epn.descs && ep->epn.is_in;
  542. if (ep->epn.desc_mode)
  543. memset(ep->epn.descs, 0, 8 * AST_VHUB_DESCS_COUNT);
  544. /*
  545. * Large send function can send up to 8 packets from
  546. * one descriptor with a limit of 4095 bytes.
  547. */
  548. ep->epn.chunk_max = ep->ep.maxpacket;
  549. if (ep->epn.is_in) {
  550. ep->epn.chunk_max <<= 3;
  551. while (ep->epn.chunk_max > 4095)
  552. ep->epn.chunk_max -= ep->ep.maxpacket;
  553. }
  554. switch(type) {
  555. case USB_ENDPOINT_XFER_CONTROL:
  556. EPDBG(ep, "Only one control endpoint\n");
  557. return -EINVAL;
  558. case USB_ENDPOINT_XFER_INT:
  559. ep_conf = VHUB_EP_CFG_SET_TYPE(EP_TYPE_INT);
  560. break;
  561. case USB_ENDPOINT_XFER_BULK:
  562. ep_conf = VHUB_EP_CFG_SET_TYPE(EP_TYPE_BULK);
  563. break;
  564. case USB_ENDPOINT_XFER_ISOC:
  565. ep_conf = VHUB_EP_CFG_SET_TYPE(EP_TYPE_ISO);
  566. ep->epn.is_iso = true;
  567. break;
  568. default:
  569. return -EINVAL;
  570. }
  571. /* Encode the rest of the EP config register */
  572. if (maxpacket < 1024)
  573. ep_conf |= VHUB_EP_CFG_SET_MAX_PKT(maxpacket);
  574. if (!ep->epn.is_in)
  575. ep_conf |= VHUB_EP_CFG_DIR_OUT;
  576. ep_conf |= VHUB_EP_CFG_SET_EP_NUM(usb_endpoint_num(desc));
  577. ep_conf |= VHUB_EP_CFG_ENABLE;
  578. ep_conf |= VHUB_EP_CFG_SET_DEV(dev->index + 1);
  579. EPVDBG(ep, "config=%08x\n", ep_conf);
  580. spin_lock_irqsave(&vhub->lock, flags);
  581. /* Disable HW and reset DMA */
  582. writel(0, ep->epn.regs + AST_VHUB_EP_CONFIG);
  583. writel(VHUB_EP_DMA_CTRL_RESET,
  584. ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
  585. /* Configure and enable */
  586. writel(ep_conf, ep->epn.regs + AST_VHUB_EP_CONFIG);
  587. if (ep->epn.desc_mode) {
  588. /* Clear DMA status, including the DMA read ptr */
  589. writel(0, ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
  590. /* Set descriptor base */
  591. writel(ep->epn.descs_dma,
  592. ep->epn.regs + AST_VHUB_EP_DESC_BASE);
  593. /* Set base DMA config value */
  594. ep->epn.dma_conf = VHUB_EP_DMA_DESC_MODE;
  595. if (ep->epn.is_in)
  596. ep->epn.dma_conf |= VHUB_EP_DMA_IN_LONG_MODE;
  597. /* First reset and disable all operations */
  598. writel(ep->epn.dma_conf | VHUB_EP_DMA_CTRL_RESET,
  599. ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
  600. /* Enable descriptor mode */
  601. writel(ep->epn.dma_conf,
  602. ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
  603. } else {
  604. /* Set base DMA config value */
  605. ep->epn.dma_conf = VHUB_EP_DMA_SINGLE_STAGE;
  606. /* Reset and switch to single stage mode */
  607. writel(ep->epn.dma_conf | VHUB_EP_DMA_CTRL_RESET,
  608. ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
  609. writel(ep->epn.dma_conf,
  610. ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
  611. writel(0, ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
  612. }
  613. /* Cleanup data toggle just in case */
  614. writel(VHUB_EP_TOGGLE_SET_EPNUM(ep->epn.g_idx),
  615. vhub->regs + AST_VHUB_EP_TOGGLE);
  616. /* Cleanup and enable ACK interrupt */
  617. imask = VHUB_EP_IRQ(ep->epn.g_idx);
  618. writel(imask, vhub->regs + AST_VHUB_EP_ACK_ISR);
  619. ep_ier = readl(vhub->regs + AST_VHUB_EP_ACK_IER);
  620. ep_ier |= imask;
  621. writel(ep_ier, vhub->regs + AST_VHUB_EP_ACK_IER);
  622. /* Woot, we are online ! */
  623. ep->epn.enabled = true;
  624. spin_unlock_irqrestore(&vhub->lock, flags);
  625. return 0;
  626. }
  627. static void ast_vhub_epn_dispose(struct usb_ep *u_ep)
  628. {
  629. struct ast_vhub_ep *ep = to_ast_ep(u_ep);
  630. if (WARN_ON(!ep->dev || !ep->d_idx))
  631. return;
  632. EPDBG(ep, "Releasing endpoint\n");
  633. /* Take it out of the EP list */
  634. list_del_init(&ep->ep.ep_list);
  635. /* Mark the address free in the device */
  636. ep->dev->epns[ep->d_idx - 1] = NULL;
  637. /* Free name & DMA buffers */
  638. kfree(ep->ep.name);
  639. ep->ep.name = NULL;
  640. dma_free_coherent(&ep->vhub->pdev->dev,
  641. AST_VHUB_EPn_MAX_PACKET +
  642. 8 * AST_VHUB_DESCS_COUNT,
  643. ep->buf, ep->buf_dma);
  644. ep->buf = NULL;
  645. ep->epn.descs = NULL;
  646. /* Mark free */
  647. ep->dev = NULL;
  648. }
  649. static const struct usb_ep_ops ast_vhub_epn_ops = {
  650. .enable = ast_vhub_epn_enable,
  651. .disable = ast_vhub_epn_disable,
  652. .dispose = ast_vhub_epn_dispose,
  653. .queue = ast_vhub_epn_queue,
  654. .dequeue = ast_vhub_epn_dequeue,
  655. .set_halt = ast_vhub_epn_set_halt,
  656. .set_wedge = ast_vhub_epn_set_wedge,
  657. .alloc_request = ast_vhub_alloc_request,
  658. .free_request = ast_vhub_free_request,
  659. };
  660. struct ast_vhub_ep *ast_vhub_alloc_epn(struct ast_vhub_dev *d, u8 addr)
  661. {
  662. struct ast_vhub *vhub = d->vhub;
  663. struct ast_vhub_ep *ep;
  664. unsigned long flags;
  665. int i;
  666. /* Find a free one (no device) */
  667. spin_lock_irqsave(&vhub->lock, flags);
  668. for (i = 0; i < vhub->max_epns; i++)
  669. if (vhub->epns[i].dev == NULL)
  670. break;
  671. if (i >= vhub->max_epns) {
  672. spin_unlock_irqrestore(&vhub->lock, flags);
  673. return NULL;
  674. }
  675. /* Set it up */
  676. ep = &vhub->epns[i];
  677. ep->dev = d;
  678. spin_unlock_irqrestore(&vhub->lock, flags);
  679. DDBG(d, "Allocating gen EP %d for addr %d\n", i, addr);
  680. INIT_LIST_HEAD(&ep->queue);
  681. ep->d_idx = addr;
  682. ep->vhub = vhub;
  683. ep->ep.ops = &ast_vhub_epn_ops;
  684. ep->ep.name = kasprintf(GFP_KERNEL, "ep%d", addr);
  685. d->epns[addr-1] = ep;
  686. ep->epn.g_idx = i;
  687. ep->epn.regs = vhub->regs + 0x200 + (i * 0x10);
  688. ep->buf = dma_alloc_coherent(&vhub->pdev->dev,
  689. AST_VHUB_EPn_MAX_PACKET +
  690. 8 * AST_VHUB_DESCS_COUNT,
  691. &ep->buf_dma, GFP_KERNEL);
  692. if (!ep->buf) {
  693. kfree(ep->ep.name);
  694. ep->ep.name = NULL;
  695. return NULL;
  696. }
  697. ep->epn.descs = ep->buf + AST_VHUB_EPn_MAX_PACKET;
  698. ep->epn.descs_dma = ep->buf_dma + AST_VHUB_EPn_MAX_PACKET;
  699. usb_ep_set_maxpacket_limit(&ep->ep, AST_VHUB_EPn_MAX_PACKET);
  700. list_add_tail(&ep->ep.ep_list, &d->gadget.ep_list);
  701. ep->ep.caps.type_iso = true;
  702. ep->ep.caps.type_bulk = true;
  703. ep->ep.caps.type_int = true;
  704. ep->ep.caps.dir_in = true;
  705. ep->ep.caps.dir_out = true;
  706. return ep;
  707. }