hcd_queue.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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 <linux/kernel.h>
  41. #include <linux/module.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/dma-mapping.h>
  45. #include <linux/io.h>
  46. #include <linux/slab.h>
  47. #include <linux/usb.h>
  48. #include <linux/usb/hcd.h>
  49. #include <linux/usb/ch11.h>
  50. #include "core.h"
  51. #include "hcd.h"
  52. /**
  53. * dwc2_qh_init() - Initializes a QH structure
  54. *
  55. * @hsotg: The HCD state structure for the DWC OTG controller
  56. * @qh: The QH to init
  57. * @urb: Holds the information about the device/endpoint needed to initialize
  58. * the QH
  59. */
  60. #define SCHEDULE_SLOP 10
  61. static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  62. struct dwc2_hcd_urb *urb)
  63. {
  64. int dev_speed, hub_addr, hub_port;
  65. char *speed, *type;
  66. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  67. /* Initialize QH */
  68. qh->ep_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
  69. qh->ep_is_in = dwc2_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
  70. qh->data_toggle = DWC2_HC_PID_DATA0;
  71. qh->maxp = dwc2_hcd_get_mps(&urb->pipe_info);
  72. INIT_LIST_HEAD(&qh->qtd_list);
  73. INIT_LIST_HEAD(&qh->qh_list_entry);
  74. /* FS/LS Endpoint on HS Hub, NOT virtual root hub */
  75. dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
  76. dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
  77. if ((dev_speed == USB_SPEED_LOW || dev_speed == USB_SPEED_FULL) &&
  78. hub_addr != 0 && hub_addr != 1) {
  79. dev_vdbg(hsotg->dev,
  80. "QH init: EP %d: TT found at hub addr %d, for port %d\n",
  81. dwc2_hcd_get_ep_num(&urb->pipe_info), hub_addr,
  82. hub_port);
  83. qh->do_split = 1;
  84. }
  85. if (qh->ep_type == USB_ENDPOINT_XFER_INT ||
  86. qh->ep_type == USB_ENDPOINT_XFER_ISOC) {
  87. /* Compute scheduling parameters once and save them */
  88. u32 hprt, prtspd;
  89. /* Todo: Account for split transfers in the bus time */
  90. int bytecount =
  91. dwc2_hb_mult(qh->maxp) * dwc2_max_packet(qh->maxp);
  92. qh->usecs = NS_TO_US(usb_calc_bus_time(qh->do_split ?
  93. USB_SPEED_HIGH : dev_speed, qh->ep_is_in,
  94. qh->ep_type == USB_ENDPOINT_XFER_ISOC,
  95. bytecount));
  96. /* Start in a slightly future (micro)frame */
  97. qh->sched_frame = dwc2_frame_num_inc(hsotg->frame_number,
  98. SCHEDULE_SLOP);
  99. qh->interval = urb->interval;
  100. #if 0
  101. /* Increase interrupt polling rate for debugging */
  102. if (qh->ep_type == USB_ENDPOINT_XFER_INT)
  103. qh->interval = 8;
  104. #endif
  105. hprt = readl(hsotg->regs + HPRT0);
  106. prtspd = (hprt & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  107. if (prtspd == HPRT0_SPD_HIGH_SPEED &&
  108. (dev_speed == USB_SPEED_LOW ||
  109. dev_speed == USB_SPEED_FULL)) {
  110. qh->interval *= 8;
  111. qh->sched_frame |= 0x7;
  112. qh->start_split_frame = qh->sched_frame;
  113. }
  114. dev_dbg(hsotg->dev, "interval=%d\n", qh->interval);
  115. }
  116. dev_vdbg(hsotg->dev, "DWC OTG HCD QH Initialized\n");
  117. dev_vdbg(hsotg->dev, "DWC OTG HCD QH - qh = %p\n", qh);
  118. dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Device Address = %d\n",
  119. dwc2_hcd_get_dev_addr(&urb->pipe_info));
  120. dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Endpoint %d, %s\n",
  121. dwc2_hcd_get_ep_num(&urb->pipe_info),
  122. dwc2_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  123. qh->dev_speed = dev_speed;
  124. switch (dev_speed) {
  125. case USB_SPEED_LOW:
  126. speed = "low";
  127. break;
  128. case USB_SPEED_FULL:
  129. speed = "full";
  130. break;
  131. case USB_SPEED_HIGH:
  132. speed = "high";
  133. break;
  134. default:
  135. speed = "?";
  136. break;
  137. }
  138. dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Speed = %s\n", speed);
  139. switch (qh->ep_type) {
  140. case USB_ENDPOINT_XFER_ISOC:
  141. type = "isochronous";
  142. break;
  143. case USB_ENDPOINT_XFER_INT:
  144. type = "interrupt";
  145. break;
  146. case USB_ENDPOINT_XFER_CONTROL:
  147. type = "control";
  148. break;
  149. case USB_ENDPOINT_XFER_BULK:
  150. type = "bulk";
  151. break;
  152. default:
  153. type = "?";
  154. break;
  155. }
  156. dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Type = %s\n", type);
  157. if (qh->ep_type == USB_ENDPOINT_XFER_INT) {
  158. dev_vdbg(hsotg->dev, "DWC OTG HCD QH - usecs = %d\n",
  159. qh->usecs);
  160. dev_vdbg(hsotg->dev, "DWC OTG HCD QH - interval = %d\n",
  161. qh->interval);
  162. }
  163. }
  164. /**
  165. * dwc2_hcd_qh_create() - Allocates and initializes a QH
  166. *
  167. * @hsotg: The HCD state structure for the DWC OTG controller
  168. * @urb: Holds the information about the device/endpoint needed
  169. * to initialize the QH
  170. * @atomic_alloc: Flag to do atomic allocation if needed
  171. *
  172. * Return: Pointer to the newly allocated QH, or NULL on error
  173. */
  174. static struct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg,
  175. struct dwc2_hcd_urb *urb,
  176. gfp_t mem_flags)
  177. {
  178. struct dwc2_qh *qh;
  179. if (!urb->priv)
  180. return NULL;
  181. /* Allocate memory */
  182. qh = kzalloc(sizeof(*qh), mem_flags);
  183. if (!qh)
  184. return NULL;
  185. dwc2_qh_init(hsotg, qh, urb);
  186. if (hsotg->core_params->dma_desc_enable > 0 &&
  187. dwc2_hcd_qh_init_ddma(hsotg, qh, mem_flags) < 0) {
  188. dwc2_hcd_qh_free(hsotg, qh);
  189. return NULL;
  190. }
  191. return qh;
  192. }
  193. /**
  194. * dwc2_hcd_qh_free() - Frees the QH
  195. *
  196. * @hsotg: HCD instance
  197. * @qh: The QH to free
  198. *
  199. * QH should already be removed from the list. QTD list should already be empty
  200. * if called from URB Dequeue.
  201. *
  202. * Must NOT be called with interrupt disabled or spinlock held
  203. */
  204. void dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  205. {
  206. u32 buf_size;
  207. if (hsotg->core_params->dma_desc_enable > 0) {
  208. dwc2_hcd_qh_free_ddma(hsotg, qh);
  209. } else if (qh->dw_align_buf) {
  210. if (qh->ep_type == USB_ENDPOINT_XFER_ISOC)
  211. buf_size = 4096;
  212. else
  213. buf_size = hsotg->core_params->max_transfer_size;
  214. dma_free_coherent(hsotg->dev, buf_size, qh->dw_align_buf,
  215. qh->dw_align_buf_dma);
  216. }
  217. kfree(qh);
  218. }
  219. /**
  220. * dwc2_periodic_channel_available() - Checks that a channel is available for a
  221. * periodic transfer
  222. *
  223. * @hsotg: The HCD state structure for the DWC OTG controller
  224. *
  225. * Return: 0 if successful, negative error code otherwise
  226. */
  227. static int dwc2_periodic_channel_available(struct dwc2_hsotg *hsotg)
  228. {
  229. /*
  230. * Currently assuming that there is a dedicated host channel for
  231. * each periodic transaction plus at least one host channel for
  232. * non-periodic transactions
  233. */
  234. int status;
  235. int num_channels;
  236. num_channels = hsotg->core_params->host_channels;
  237. if (hsotg->periodic_channels + hsotg->non_periodic_channels <
  238. num_channels
  239. && hsotg->periodic_channels < num_channels - 1) {
  240. status = 0;
  241. } else {
  242. dev_dbg(hsotg->dev,
  243. "%s: Total channels: %d, Periodic: %d, "
  244. "Non-periodic: %d\n", __func__, num_channels,
  245. hsotg->periodic_channels, hsotg->non_periodic_channels);
  246. status = -ENOSPC;
  247. }
  248. return status;
  249. }
  250. /**
  251. * dwc2_check_periodic_bandwidth() - Checks that there is sufficient bandwidth
  252. * for the specified QH in the periodic schedule
  253. *
  254. * @hsotg: The HCD state structure for the DWC OTG controller
  255. * @qh: QH containing periodic bandwidth required
  256. *
  257. * Return: 0 if successful, negative error code otherwise
  258. *
  259. * For simplicity, this calculation assumes that all the transfers in the
  260. * periodic schedule may occur in the same (micro)frame
  261. */
  262. static int dwc2_check_periodic_bandwidth(struct dwc2_hsotg *hsotg,
  263. struct dwc2_qh *qh)
  264. {
  265. int status;
  266. s16 max_claimed_usecs;
  267. status = 0;
  268. if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
  269. /*
  270. * High speed mode
  271. * Max periodic usecs is 80% x 125 usec = 100 usec
  272. */
  273. max_claimed_usecs = 100 - qh->usecs;
  274. } else {
  275. /*
  276. * Full speed mode
  277. * Max periodic usecs is 90% x 1000 usec = 900 usec
  278. */
  279. max_claimed_usecs = 900 - qh->usecs;
  280. }
  281. if (hsotg->periodic_usecs > max_claimed_usecs) {
  282. dev_err(hsotg->dev,
  283. "%s: already claimed usecs %d, required usecs %d\n",
  284. __func__, hsotg->periodic_usecs, qh->usecs);
  285. status = -ENOSPC;
  286. }
  287. return status;
  288. }
  289. /**
  290. * Microframe scheduler
  291. * track the total use in hsotg->frame_usecs
  292. * keep each qh use in qh->frame_usecs
  293. * when surrendering the qh then donate the time back
  294. */
  295. static const unsigned short max_uframe_usecs[] = {
  296. 100, 100, 100, 100, 100, 100, 30, 0
  297. };
  298. void dwc2_hcd_init_usecs(struct dwc2_hsotg *hsotg)
  299. {
  300. int i;
  301. for (i = 0; i < 8; i++)
  302. hsotg->frame_usecs[i] = max_uframe_usecs[i];
  303. }
  304. static int dwc2_find_single_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  305. {
  306. unsigned short utime = qh->usecs;
  307. int i;
  308. for (i = 0; i < 8; i++) {
  309. /* At the start hsotg->frame_usecs[i] = max_uframe_usecs[i] */
  310. if (utime <= hsotg->frame_usecs[i]) {
  311. hsotg->frame_usecs[i] -= utime;
  312. qh->frame_usecs[i] += utime;
  313. return i;
  314. }
  315. }
  316. return -ENOSPC;
  317. }
  318. /*
  319. * use this for FS apps that can span multiple uframes
  320. */
  321. static int dwc2_find_multi_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  322. {
  323. unsigned short utime = qh->usecs;
  324. unsigned short xtime;
  325. int t_left;
  326. int i;
  327. int j;
  328. int k;
  329. for (i = 0; i < 8; i++) {
  330. if (hsotg->frame_usecs[i] <= 0)
  331. continue;
  332. /*
  333. * we need n consecutive slots so use j as a start slot
  334. * j plus j+1 must be enough time (for now)
  335. */
  336. xtime = hsotg->frame_usecs[i];
  337. for (j = i + 1; j < 8; j++) {
  338. /*
  339. * if we add this frame remaining time to xtime we may
  340. * be OK, if not we need to test j for a complete frame
  341. */
  342. if (xtime + hsotg->frame_usecs[j] < utime) {
  343. if (hsotg->frame_usecs[j] <
  344. max_uframe_usecs[j])
  345. continue;
  346. }
  347. if (xtime >= utime) {
  348. t_left = utime;
  349. for (k = i; k < 8; k++) {
  350. t_left -= hsotg->frame_usecs[k];
  351. if (t_left <= 0) {
  352. qh->frame_usecs[k] +=
  353. hsotg->frame_usecs[k]
  354. + t_left;
  355. hsotg->frame_usecs[k] = -t_left;
  356. return i;
  357. } else {
  358. qh->frame_usecs[k] +=
  359. hsotg->frame_usecs[k];
  360. hsotg->frame_usecs[k] = 0;
  361. }
  362. }
  363. }
  364. /* add the frame time to x time */
  365. xtime += hsotg->frame_usecs[j];
  366. /* we must have a fully available next frame or break */
  367. if (xtime < utime &&
  368. hsotg->frame_usecs[j] == max_uframe_usecs[j])
  369. continue;
  370. }
  371. }
  372. return -ENOSPC;
  373. }
  374. static int dwc2_find_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  375. {
  376. int ret;
  377. if (qh->dev_speed == USB_SPEED_HIGH) {
  378. /* if this is a hs transaction we need a full frame */
  379. ret = dwc2_find_single_uframe(hsotg, qh);
  380. } else {
  381. /*
  382. * if this is a fs transaction we may need a sequence
  383. * of frames
  384. */
  385. ret = dwc2_find_multi_uframe(hsotg, qh);
  386. }
  387. return ret;
  388. }
  389. /**
  390. * dwc2_check_max_xfer_size() - Checks that the max transfer size allowed in a
  391. * host channel is large enough to handle the maximum data transfer in a single
  392. * (micro)frame for a periodic transfer
  393. *
  394. * @hsotg: The HCD state structure for the DWC OTG controller
  395. * @qh: QH for a periodic endpoint
  396. *
  397. * Return: 0 if successful, negative error code otherwise
  398. */
  399. static int dwc2_check_max_xfer_size(struct dwc2_hsotg *hsotg,
  400. struct dwc2_qh *qh)
  401. {
  402. u32 max_xfer_size;
  403. u32 max_channel_xfer_size;
  404. int status = 0;
  405. max_xfer_size = dwc2_max_packet(qh->maxp) * dwc2_hb_mult(qh->maxp);
  406. max_channel_xfer_size = hsotg->core_params->max_transfer_size;
  407. if (max_xfer_size > max_channel_xfer_size) {
  408. dev_err(hsotg->dev,
  409. "%s: Periodic xfer length %d > max xfer length for channel %d\n",
  410. __func__, max_xfer_size, max_channel_xfer_size);
  411. status = -ENOSPC;
  412. }
  413. return status;
  414. }
  415. /**
  416. * dwc2_schedule_periodic() - Schedules an interrupt or isochronous transfer in
  417. * the periodic schedule
  418. *
  419. * @hsotg: The HCD state structure for the DWC OTG controller
  420. * @qh: QH for the periodic transfer. The QH should already contain the
  421. * scheduling information.
  422. *
  423. * Return: 0 if successful, negative error code otherwise
  424. */
  425. static int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  426. {
  427. int status;
  428. if (hsotg->core_params->uframe_sched > 0) {
  429. int frame = -1;
  430. status = dwc2_find_uframe(hsotg, qh);
  431. if (status == 0)
  432. frame = 7;
  433. else if (status > 0)
  434. frame = status - 1;
  435. /* Set the new frame up */
  436. if (frame >= 0) {
  437. qh->sched_frame &= ~0x7;
  438. qh->sched_frame |= (frame & 7);
  439. }
  440. if (status > 0)
  441. status = 0;
  442. } else {
  443. status = dwc2_periodic_channel_available(hsotg);
  444. if (status) {
  445. dev_info(hsotg->dev,
  446. "%s: No host channel available for periodic transfer\n",
  447. __func__);
  448. return status;
  449. }
  450. status = dwc2_check_periodic_bandwidth(hsotg, qh);
  451. }
  452. if (status) {
  453. dev_dbg(hsotg->dev,
  454. "%s: Insufficient periodic bandwidth for periodic transfer\n",
  455. __func__);
  456. return status;
  457. }
  458. status = dwc2_check_max_xfer_size(hsotg, qh);
  459. if (status) {
  460. dev_dbg(hsotg->dev,
  461. "%s: Channel max transfer size too small for periodic transfer\n",
  462. __func__);
  463. return status;
  464. }
  465. if (hsotg->core_params->dma_desc_enable > 0)
  466. /* Don't rely on SOF and start in ready schedule */
  467. list_add_tail(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
  468. else
  469. /* Always start in inactive schedule */
  470. list_add_tail(&qh->qh_list_entry,
  471. &hsotg->periodic_sched_inactive);
  472. if (hsotg->core_params->uframe_sched <= 0)
  473. /* Reserve periodic channel */
  474. hsotg->periodic_channels++;
  475. /* Update claimed usecs per (micro)frame */
  476. hsotg->periodic_usecs += qh->usecs;
  477. return status;
  478. }
  479. /**
  480. * dwc2_deschedule_periodic() - Removes an interrupt or isochronous transfer
  481. * from the periodic schedule
  482. *
  483. * @hsotg: The HCD state structure for the DWC OTG controller
  484. * @qh: QH for the periodic transfer
  485. */
  486. static void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg,
  487. struct dwc2_qh *qh)
  488. {
  489. int i;
  490. list_del_init(&qh->qh_list_entry);
  491. /* Update claimed usecs per (micro)frame */
  492. hsotg->periodic_usecs -= qh->usecs;
  493. if (hsotg->core_params->uframe_sched > 0) {
  494. for (i = 0; i < 8; i++) {
  495. hsotg->frame_usecs[i] += qh->frame_usecs[i];
  496. qh->frame_usecs[i] = 0;
  497. }
  498. } else {
  499. /* Release periodic channel reservation */
  500. hsotg->periodic_channels--;
  501. }
  502. }
  503. /**
  504. * dwc2_hcd_qh_add() - Adds a QH to either the non periodic or periodic
  505. * schedule if it is not already in the schedule. If the QH is already in
  506. * the schedule, no action is taken.
  507. *
  508. * @hsotg: The HCD state structure for the DWC OTG controller
  509. * @qh: The QH to add
  510. *
  511. * Return: 0 if successful, negative error code otherwise
  512. */
  513. int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  514. {
  515. int status;
  516. u32 intr_mask;
  517. if (dbg_qh(qh))
  518. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  519. if (!list_empty(&qh->qh_list_entry))
  520. /* QH already in a schedule */
  521. return 0;
  522. /* Add the new QH to the appropriate schedule */
  523. if (dwc2_qh_is_non_per(qh)) {
  524. /* Always start in inactive schedule */
  525. list_add_tail(&qh->qh_list_entry,
  526. &hsotg->non_periodic_sched_inactive);
  527. return 0;
  528. }
  529. status = dwc2_schedule_periodic(hsotg, qh);
  530. if (status)
  531. return status;
  532. if (!hsotg->periodic_qh_count) {
  533. intr_mask = readl(hsotg->regs + GINTMSK);
  534. intr_mask |= GINTSTS_SOF;
  535. writel(intr_mask, hsotg->regs + GINTMSK);
  536. }
  537. hsotg->periodic_qh_count++;
  538. return 0;
  539. }
  540. /**
  541. * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic
  542. * schedule. Memory is not freed.
  543. *
  544. * @hsotg: The HCD state structure
  545. * @qh: QH to remove from schedule
  546. */
  547. void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  548. {
  549. u32 intr_mask;
  550. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  551. if (list_empty(&qh->qh_list_entry))
  552. /* QH is not in a schedule */
  553. return;
  554. if (dwc2_qh_is_non_per(qh)) {
  555. if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry)
  556. hsotg->non_periodic_qh_ptr =
  557. hsotg->non_periodic_qh_ptr->next;
  558. list_del_init(&qh->qh_list_entry);
  559. return;
  560. }
  561. dwc2_deschedule_periodic(hsotg, qh);
  562. hsotg->periodic_qh_count--;
  563. if (!hsotg->periodic_qh_count) {
  564. intr_mask = readl(hsotg->regs + GINTMSK);
  565. intr_mask &= ~GINTSTS_SOF;
  566. writel(intr_mask, hsotg->regs + GINTMSK);
  567. }
  568. }
  569. /*
  570. * Schedule the next continuing periodic split transfer
  571. */
  572. static void dwc2_sched_periodic_split(struct dwc2_hsotg *hsotg,
  573. struct dwc2_qh *qh, u16 frame_number,
  574. int sched_next_periodic_split)
  575. {
  576. u16 incr;
  577. if (sched_next_periodic_split) {
  578. qh->sched_frame = frame_number;
  579. incr = dwc2_frame_num_inc(qh->start_split_frame, 1);
  580. if (dwc2_frame_num_le(frame_number, incr)) {
  581. /*
  582. * Allow one frame to elapse after start split
  583. * microframe before scheduling complete split, but
  584. * DON'T if we are doing the next start split in the
  585. * same frame for an ISOC out
  586. */
  587. if (qh->ep_type != USB_ENDPOINT_XFER_ISOC ||
  588. qh->ep_is_in != 0) {
  589. qh->sched_frame =
  590. dwc2_frame_num_inc(qh->sched_frame, 1);
  591. }
  592. }
  593. } else {
  594. qh->sched_frame = dwc2_frame_num_inc(qh->start_split_frame,
  595. qh->interval);
  596. if (dwc2_frame_num_le(qh->sched_frame, frame_number))
  597. qh->sched_frame = frame_number;
  598. qh->sched_frame |= 0x7;
  599. qh->start_split_frame = qh->sched_frame;
  600. }
  601. }
  602. /*
  603. * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  604. * non-periodic schedule. The QH is added to the inactive non-periodic
  605. * schedule if any QTDs are still attached to the QH.
  606. *
  607. * For periodic QHs, the QH is removed from the periodic queued schedule. If
  608. * there are any QTDs still attached to the QH, the QH is added to either the
  609. * periodic inactive schedule or the periodic ready schedule and its next
  610. * scheduled frame is calculated. The QH is placed in the ready schedule if
  611. * the scheduled frame has been reached already. Otherwise it's placed in the
  612. * inactive schedule. If there are no QTDs attached to the QH, the QH is
  613. * completely removed from the periodic schedule.
  614. */
  615. void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  616. int sched_next_periodic_split)
  617. {
  618. u16 frame_number;
  619. if (dbg_qh(qh))
  620. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  621. if (dwc2_qh_is_non_per(qh)) {
  622. dwc2_hcd_qh_unlink(hsotg, qh);
  623. if (!list_empty(&qh->qtd_list))
  624. /* Add back to inactive non-periodic schedule */
  625. dwc2_hcd_qh_add(hsotg, qh);
  626. return;
  627. }
  628. frame_number = dwc2_hcd_get_frame_number(hsotg);
  629. if (qh->do_split) {
  630. dwc2_sched_periodic_split(hsotg, qh, frame_number,
  631. sched_next_periodic_split);
  632. } else {
  633. qh->sched_frame = dwc2_frame_num_inc(qh->sched_frame,
  634. qh->interval);
  635. if (dwc2_frame_num_le(qh->sched_frame, frame_number))
  636. qh->sched_frame = frame_number;
  637. }
  638. if (list_empty(&qh->qtd_list)) {
  639. dwc2_hcd_qh_unlink(hsotg, qh);
  640. return;
  641. }
  642. /*
  643. * Remove from periodic_sched_queued and move to
  644. * appropriate queue
  645. */
  646. if ((hsotg->core_params->uframe_sched > 0 &&
  647. dwc2_frame_num_le(qh->sched_frame, frame_number)) ||
  648. (hsotg->core_params->uframe_sched <= 0 &&
  649. qh->sched_frame == frame_number))
  650. list_move(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
  651. else
  652. list_move(&qh->qh_list_entry, &hsotg->periodic_sched_inactive);
  653. }
  654. /**
  655. * dwc2_hcd_qtd_init() - Initializes a QTD structure
  656. *
  657. * @qtd: The QTD to initialize
  658. * @urb: The associated URB
  659. */
  660. void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
  661. {
  662. qtd->urb = urb;
  663. if (dwc2_hcd_get_pipe_type(&urb->pipe_info) ==
  664. USB_ENDPOINT_XFER_CONTROL) {
  665. /*
  666. * The only time the QTD data toggle is used is on the data
  667. * phase of control transfers. This phase always starts with
  668. * DATA1.
  669. */
  670. qtd->data_toggle = DWC2_HC_PID_DATA1;
  671. qtd->control_phase = DWC2_CONTROL_SETUP;
  672. }
  673. /* Start split */
  674. qtd->complete_split = 0;
  675. qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
  676. qtd->isoc_split_offset = 0;
  677. qtd->in_process = 0;
  678. /* Store the qtd ptr in the urb to reference the QTD */
  679. urb->qtd = qtd;
  680. }
  681. /**
  682. * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH
  683. *
  684. * @hsotg: The DWC HCD structure
  685. * @qtd: The QTD to add
  686. * @qh: Out parameter to return queue head
  687. * @atomic_alloc: Flag to do atomic alloc if needed
  688. *
  689. * Return: 0 if successful, negative error code otherwise
  690. *
  691. * Finds the correct QH to place the QTD into. If it does not find a QH, it
  692. * will create a new QH. If the QH to which the QTD is added is not currently
  693. * scheduled, it is placed into the proper schedule based on its EP type.
  694. */
  695. int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
  696. struct dwc2_qh **qh, gfp_t mem_flags)
  697. {
  698. struct dwc2_hcd_urb *urb = qtd->urb;
  699. unsigned long flags;
  700. int allocated = 0;
  701. int retval;
  702. /*
  703. * Get the QH which holds the QTD-list to insert to. Create QH if it
  704. * doesn't exist.
  705. */
  706. if (*qh == NULL) {
  707. *qh = dwc2_hcd_qh_create(hsotg, urb, mem_flags);
  708. if (*qh == NULL)
  709. return -ENOMEM;
  710. allocated = 1;
  711. }
  712. spin_lock_irqsave(&hsotg->lock, flags);
  713. retval = dwc2_hcd_qh_add(hsotg, *qh);
  714. if (retval)
  715. goto fail;
  716. qtd->qh = *qh;
  717. list_add_tail(&qtd->qtd_list_entry, &(*qh)->qtd_list);
  718. spin_unlock_irqrestore(&hsotg->lock, flags);
  719. return 0;
  720. fail:
  721. if (allocated) {
  722. struct dwc2_qtd *qtd2, *qtd2_tmp;
  723. struct dwc2_qh *qh_tmp = *qh;
  724. *qh = NULL;
  725. dwc2_hcd_qh_unlink(hsotg, qh_tmp);
  726. /* Free each QTD in the QH's QTD list */
  727. list_for_each_entry_safe(qtd2, qtd2_tmp, &qh_tmp->qtd_list,
  728. qtd_list_entry)
  729. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh_tmp);
  730. spin_unlock_irqrestore(&hsotg->lock, flags);
  731. dwc2_hcd_qh_free(hsotg, qh_tmp);
  732. } else {
  733. spin_unlock_irqrestore(&hsotg->lock, flags);
  734. }
  735. return retval;
  736. }