hcd_queue.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * hcd_queue.c - DesignWare HS OTG Controller host queuing routines
  3. *
  4. * Copyright (C) 2004-2013 Synopsys, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions, and the following disclaimer,
  11. * without modification.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The names of the above-listed copyright holders may not be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * ALTERNATIVELY, this software may be distributed under the terms of the
  20. * GNU General Public License ("GPL") as published by the Free Software
  21. * Foundation; either version 2 of the License, or (at your option) any
  22. * later version.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * This file contains the functions to manage Queue Heads and Queue
  38. * Transfer Descriptors for Host mode
  39. */
  40. #include "usb_os_adapter.h"
  41. #include "trace.h"
  42. #include <asm/dma-mapping.h>
  43. #include <linux/usb/ch9.h>
  44. #include <linux/usb/gadget.h>
  45. #include "core.h"
  46. #include "hcd.h"
  47. /* Wait this long before releasing periodic reservation */
  48. #define DWC2_UNRESERVE_DELAY (5)
  49. #define swap(a, b) do {unsigned long tmp = (a); (a) = (b); (b) = tmp;} while(0)
  50. unsigned long gcd(unsigned long a, unsigned long b)
  51. {
  52. unsigned long r = a | b;
  53. if (!a || !b)
  54. return r;
  55. /* Isolate lsbit of r */
  56. r &= -r;
  57. while (!(b & r))
  58. b >>= 1;
  59. if (b == r)
  60. return r;
  61. for (;;) {
  62. while (!(a & r))
  63. a >>= 1;
  64. if (a == r)
  65. return r;
  66. if (a == b)
  67. return a;
  68. if (a < b)
  69. swap(a, b);
  70. a -= b;
  71. a >>= 1;
  72. if (a & r)
  73. a += b;
  74. a >>= 1;
  75. }
  76. }
  77. /**
  78. * dwc2_do_unreserve() - Actually release the periodic reservation
  79. *
  80. * This function actually releases the periodic bandwidth that was reserved
  81. * by the given qh.
  82. *
  83. * @hsotg: The HCD state structure for the DWC OTG controller
  84. * @qh: QH for the periodic transfer.
  85. */
  86. static void dwc2_do_unreserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  87. {
  88. assert_spin_locked(&hsotg->lock);
  89. WARN_ON(!qh->unreserve_pending);
  90. /* No more unreserve pending--we're doing it */
  91. qh->unreserve_pending = false;
  92. if (!list_item_empty(&qh->qh_list_entry)) {
  93. WARN_ON(!list_item_empty(&qh->qh_list_entry));
  94. list_del_init(&qh->qh_list_entry);
  95. }
  96. }
  97. /**
  98. * dwc2_unreserve_timer_fn() - Timer function to release periodic reservation
  99. *
  100. * According to the kernel doc for usb_submit_urb() (specifically the part about
  101. * "Reserved Bandwidth Transfers"), we need to keep a reservation active as
  102. * long as a device driver keeps submitting. Since we're using HCD_BH to give
  103. * back the URB we need to give the driver a little bit of time before we
  104. * release the reservation. This worker is called after the appropriate
  105. * delay.
  106. *
  107. * @work: Pointer to a qh unreserve_work.
  108. */
  109. /* static void dwc2_unreserve_timer_fn(unsigned long data)
  110. {
  111. } */
  112. /**
  113. * dwc2_qh_init() - Initializes a QH structure
  114. *
  115. * @hsotg: The HCD state structure for the DWC OTG controller
  116. * @qh: The QH to init
  117. * @urb: Holds the information about the device/endpoint needed to initialize
  118. * the QH
  119. * @mem_flags: Flags for allocating memory.
  120. */
  121. static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  122. struct dwc2_hcd_urb *urb, gfp_t mem_flags)
  123. {
  124. int dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
  125. u8 ep_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
  126. bool ep_is_in = !!dwc2_hcd_is_pipe_in(&urb->pipe_info);
  127. u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
  128. u32 prtspd = (hprt & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  129. bool do_split = (prtspd == HPRT0_SPD_HIGH_SPEED &&
  130. dev_speed != USB_SPEED_HIGH);
  131. int maxp = dwc2_hcd_get_mps(&urb->pipe_info);
  132. int bytecount = dwc2_hb_mult(maxp) * dwc2_max_packet(maxp);
  133. char *speed, *type;
  134. /* Initialize QH */
  135. qh->hsotg = hsotg;
  136. qh->ep_type = ep_type;
  137. qh->ep_is_in = ep_is_in;
  138. qh->data_toggle = DWC2_HC_PID_DATA0;
  139. qh->maxp = maxp;
  140. INIT_LIST_HEAD(&qh->qtd_list);
  141. INIT_LIST_ITEM(&qh->qh_list_entry);
  142. qh->qh_list_entry.pvOwner = (void *)qh;
  143. qh->do_split = do_split;
  144. qh->dev_speed = dev_speed;
  145. switch (dev_speed) {
  146. case USB_SPEED_LOW:
  147. speed = "low";
  148. break;
  149. case USB_SPEED_FULL:
  150. speed = "full";
  151. break;
  152. case USB_SPEED_HIGH:
  153. speed = "high";
  154. break;
  155. default:
  156. speed = "?";
  157. break;
  158. }
  159. switch (qh->ep_type) {
  160. case USB_ENDPOINT_XFER_CONTROL:
  161. type = "control";
  162. break;
  163. case USB_ENDPOINT_XFER_BULK:
  164. type = "bulk";
  165. break;
  166. default:
  167. type = "?";
  168. break;
  169. }
  170. USB_UNUSED(bytecount);
  171. USB_UNUSED(speed);
  172. USB_UNUSED(type);
  173. dwc2_sch_dbg(hsotg, "QH=%p Init %s, %s speed, %d bytes:\n", qh, type,
  174. speed, bytecount);
  175. dwc2_sch_dbg(hsotg, "QH=%p ...addr=%d, ep=%d, %s\n", qh,
  176. dwc2_hcd_get_dev_addr(&urb->pipe_info),
  177. dwc2_hcd_get_ep_num(&urb->pipe_info),
  178. ep_is_in ? "IN" : "OUT");
  179. }
  180. /**
  181. * dwc2_hcd_qh_create() - Allocates and initializes a QH
  182. *
  183. * @hsotg: The HCD state structure for the DWC OTG controller
  184. * @urb: Holds the information about the device/endpoint needed
  185. * to initialize the QH
  186. * @atomic_alloc: Flag to do atomic allocation if needed
  187. *
  188. * Return: Pointer to the newly allocated QH, or NULL on error
  189. */
  190. struct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg,
  191. struct dwc2_hcd_urb *urb,
  192. gfp_t mem_flags)
  193. {
  194. struct dwc2_qh *qh;
  195. if (!urb->priv)
  196. return NULL;
  197. /* Allocate memory */
  198. qh = (struct dwc2_qh *)kzalloc(sizeof(*qh), mem_flags);
  199. if (!qh)
  200. return NULL;
  201. dwc2_qh_init(hsotg, qh, urb, mem_flags);
  202. /* if (hsotg->params.dma_desc_enable &&
  203. dwc2_hcd_qh_init_ddma(hsotg, qh, mem_flags) < 0) {
  204. dwc2_hcd_qh_free(hsotg, qh);
  205. return NULL;
  206. } */
  207. return qh;
  208. }
  209. /**
  210. * dwc2_hcd_qh_free() - Frees the QH
  211. *
  212. * @hsotg: HCD instance
  213. * @qh: The QH to free
  214. *
  215. * QH should already be removed from the list. QTD list should already be empty
  216. * if called from URB Dequeue.
  217. *
  218. * Must NOT be called with interrupt disabled or spinlock held
  219. */
  220. void dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  221. {
  222. /* Make sure any unreserve work is finished. */
  223. if (0) {
  224. unsigned long flags;
  225. spin_lock_irqsave(&hsotg->lock, flags);
  226. dwc2_do_unreserve(hsotg, qh);
  227. spin_unlock_irqrestore(&hsotg->lock, flags);
  228. }
  229. dwc2_host_put_tt_info(hsotg, qh->dwc_tt);
  230. kfree(qh);
  231. }
  232. /**
  233. * dwc2_hcd_qh_add() - Adds a QH to either the non periodic or periodic
  234. * schedule if it is not already in the schedule. If the QH is already in
  235. * the schedule, no action is taken.
  236. *
  237. * @hsotg: The HCD state structure for the DWC OTG controller
  238. * @qh: The QH to add
  239. *
  240. * Return: 0 if successful, negative error code otherwise
  241. */
  242. int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  243. {
  244. u32 intr_mask;
  245. if (dbg_qh(qh))
  246. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  247. if (!list_item_empty(&qh->qh_list_entry))
  248. /* QH already in a schedule */
  249. return 0;
  250. /* Add the new QH to the appropriate schedule */
  251. if (dwc2_qh_is_non_per(qh)) {
  252. /* Schedule right away */
  253. qh->start_active_frame = hsotg->frame_number;
  254. qh->next_active_frame = qh->start_active_frame;
  255. /* Always start in inactive schedule */
  256. list_add_tail(&qh->qh_list_entry,
  257. &hsotg->non_periodic_sched_inactive);
  258. return 0;
  259. }
  260. intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
  261. intr_mask |= GINTSTS_SOF;
  262. dwc2_writel(intr_mask, hsotg->regs + GINTMSK);
  263. return 0;
  264. }
  265. /**
  266. * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic
  267. * schedule. Memory is not freed.
  268. *
  269. * @hsotg: The HCD state structure
  270. * @qh: QH to remove from schedule
  271. */
  272. void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  273. {
  274. u32 intr_mask;
  275. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  276. if (list_item_empty(&qh->qh_list_entry))
  277. /* QH is not in a schedule */
  278. return;
  279. if (dwc2_qh_is_non_per(qh)) {
  280. if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry) {
  281. hsotg->non_periodic_qh_ptr = listGET_NEXT(hsotg->non_periodic_qh_ptr);
  282. }
  283. list_del_init(&qh->qh_list_entry);
  284. return;
  285. }
  286. intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
  287. intr_mask &= ~GINTSTS_SOF;
  288. dwc2_writel(intr_mask, hsotg->regs + GINTMSK);
  289. }
  290. /*
  291. * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  292. * non-periodic schedule. The QH is added to the inactive non-periodic
  293. * schedule if any QTDs are still attached to the QH.
  294. *
  295. * For periodic QHs, the QH is removed from the periodic queued schedule. If
  296. * there are any QTDs still attached to the QH, the QH is added to either the
  297. * periodic inactive schedule or the periodic ready schedule and its next
  298. * scheduled frame is calculated. The QH is placed in the ready schedule if
  299. * the scheduled frame has been reached already. Otherwise it's placed in the
  300. * inactive schedule. If there are no QTDs attached to the QH, the QH is
  301. * completely removed from the periodic schedule.
  302. */
  303. void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  304. int sched_next_periodic_split)
  305. {
  306. u16 old_frame = qh->next_active_frame;
  307. dev_vdbg(hsotg->dev, "%s() ep_type:%d\n", __func__, qh->ep_type);
  308. USB_UNUSED(old_frame);
  309. if (dbg_qh(qh))
  310. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  311. if (dwc2_qh_is_non_per(qh)) {
  312. dwc2_hcd_qh_unlink(hsotg, qh);
  313. if (!list_empty(&qh->qtd_list))
  314. /* Add back to inactive non-periodic schedule */
  315. dwc2_hcd_qh_add(hsotg, qh);
  316. return;
  317. }
  318. }
  319. /**
  320. * dwc2_hcd_qtd_init() - Initializes a QTD structure
  321. *
  322. * @qtd: The QTD to initialize
  323. * @urb: The associated URB
  324. */
  325. void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
  326. {
  327. qtd->urb = urb;
  328. if (dwc2_hcd_get_pipe_type(&urb->pipe_info) ==
  329. USB_ENDPOINT_XFER_CONTROL) {
  330. /*
  331. * The only time the QTD data toggle is used is on the data
  332. * phase of control transfers. This phase always starts with
  333. * DATA1.
  334. */
  335. qtd->data_toggle = DWC2_HC_PID_DATA1;
  336. qtd->control_phase = DWC2_CONTROL_SETUP;
  337. }
  338. /* Start split */
  339. qtd->complete_split = 0;
  340. qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
  341. qtd->in_process = 0;
  342. /* Store the qtd ptr in the urb to reference the QTD */
  343. urb->qtd = qtd;
  344. }
  345. /**
  346. * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH
  347. * Caller must hold driver lock.
  348. *
  349. * @hsotg: The DWC HCD structure
  350. * @qtd: The QTD to add
  351. * @qh: Queue head to add qtd to
  352. *
  353. * Return: 0 if successful, negative error code otherwise
  354. *
  355. * If the QH to which the QTD is added is not currently scheduled, it is placed
  356. * into the proper schedule based on its EP type.
  357. */
  358. int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
  359. struct dwc2_qh *qh)
  360. {
  361. int retval;
  362. if (unlikely(!qh)) {
  363. dev_err(hsotg->dev, "%s: Invalid QH\n", __func__);
  364. retval = -EINVAL;
  365. goto fail;
  366. }
  367. retval = dwc2_hcd_qh_add(hsotg, qh);
  368. if (retval)
  369. goto fail;
  370. qtd->qh = qh;
  371. list_add_tail(&qtd->qtd_list_entry, &qh->qtd_list);
  372. return 0;
  373. fail:
  374. return retval;
  375. }