hcd_queue.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. 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. if (hsotg->core_params->dma_desc_enable > 0) {
  207. dwc2_hcd_qh_free_ddma(hsotg, qh);
  208. } else {
  209. /* kfree(NULL) is safe */
  210. kfree(qh->dw_align_buf);
  211. qh->dw_align_buf_dma = (dma_addr_t)0;
  212. }
  213. kfree(qh);
  214. }
  215. /**
  216. * dwc2_periodic_channel_available() - Checks that a channel is available for a
  217. * periodic transfer
  218. *
  219. * @hsotg: The HCD state structure for the DWC OTG controller
  220. *
  221. * Return: 0 if successful, negative error code otherwise
  222. */
  223. static int dwc2_periodic_channel_available(struct dwc2_hsotg *hsotg)
  224. {
  225. /*
  226. * Currently assuming that there is a dedicated host channel for
  227. * each periodic transaction plus at least one host channel for
  228. * non-periodic transactions
  229. */
  230. int status;
  231. int num_channels;
  232. num_channels = hsotg->core_params->host_channels;
  233. if (hsotg->periodic_channels + hsotg->non_periodic_channels <
  234. num_channels
  235. && hsotg->periodic_channels < num_channels - 1) {
  236. status = 0;
  237. } else {
  238. dev_dbg(hsotg->dev,
  239. "%s: Total channels: %d, Periodic: %d, "
  240. "Non-periodic: %d\n", __func__, num_channels,
  241. hsotg->periodic_channels, hsotg->non_periodic_channels);
  242. status = -ENOSPC;
  243. }
  244. return status;
  245. }
  246. /**
  247. * dwc2_check_periodic_bandwidth() - Checks that there is sufficient bandwidth
  248. * for the specified QH in the periodic schedule
  249. *
  250. * @hsotg: The HCD state structure for the DWC OTG controller
  251. * @qh: QH containing periodic bandwidth required
  252. *
  253. * Return: 0 if successful, negative error code otherwise
  254. *
  255. * For simplicity, this calculation assumes that all the transfers in the
  256. * periodic schedule may occur in the same (micro)frame
  257. */
  258. static int dwc2_check_periodic_bandwidth(struct dwc2_hsotg *hsotg,
  259. struct dwc2_qh *qh)
  260. {
  261. int status;
  262. s16 max_claimed_usecs;
  263. status = 0;
  264. if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
  265. /*
  266. * High speed mode
  267. * Max periodic usecs is 80% x 125 usec = 100 usec
  268. */
  269. max_claimed_usecs = 100 - qh->usecs;
  270. } else {
  271. /*
  272. * Full speed mode
  273. * Max periodic usecs is 90% x 1000 usec = 900 usec
  274. */
  275. max_claimed_usecs = 900 - qh->usecs;
  276. }
  277. if (hsotg->periodic_usecs > max_claimed_usecs) {
  278. dev_err(hsotg->dev,
  279. "%s: already claimed usecs %d, required usecs %d\n",
  280. __func__, hsotg->periodic_usecs, qh->usecs);
  281. status = -ENOSPC;
  282. }
  283. return status;
  284. }
  285. /**
  286. * Microframe scheduler
  287. * track the total use in hsotg->frame_usecs
  288. * keep each qh use in qh->frame_usecs
  289. * when surrendering the qh then donate the time back
  290. */
  291. static const unsigned short max_uframe_usecs[] = {
  292. 100, 100, 100, 100, 100, 100, 30, 0
  293. };
  294. void dwc2_hcd_init_usecs(struct dwc2_hsotg *hsotg)
  295. {
  296. int i;
  297. for (i = 0; i < 8; i++)
  298. hsotg->frame_usecs[i] = max_uframe_usecs[i];
  299. }
  300. static int dwc2_find_single_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  301. {
  302. unsigned short utime = qh->usecs;
  303. int i;
  304. for (i = 0; i < 8; i++) {
  305. /* At the start hsotg->frame_usecs[i] = max_uframe_usecs[i] */
  306. if (utime <= hsotg->frame_usecs[i]) {
  307. hsotg->frame_usecs[i] -= utime;
  308. qh->frame_usecs[i] += utime;
  309. return i;
  310. }
  311. }
  312. return -ENOSPC;
  313. }
  314. /*
  315. * use this for FS apps that can span multiple uframes
  316. */
  317. static int dwc2_find_multi_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  318. {
  319. unsigned short utime = qh->usecs;
  320. unsigned short xtime;
  321. int t_left;
  322. int i;
  323. int j;
  324. int k;
  325. for (i = 0; i < 8; i++) {
  326. if (hsotg->frame_usecs[i] <= 0)
  327. continue;
  328. /*
  329. * we need n consecutive slots so use j as a start slot
  330. * j plus j+1 must be enough time (for now)
  331. */
  332. xtime = hsotg->frame_usecs[i];
  333. for (j = i + 1; j < 8; j++) {
  334. /*
  335. * if we add this frame remaining time to xtime we may
  336. * be OK, if not we need to test j for a complete frame
  337. */
  338. if (xtime + hsotg->frame_usecs[j] < utime) {
  339. if (hsotg->frame_usecs[j] <
  340. max_uframe_usecs[j])
  341. continue;
  342. }
  343. if (xtime >= utime) {
  344. t_left = utime;
  345. for (k = i; k < 8; k++) {
  346. t_left -= hsotg->frame_usecs[k];
  347. if (t_left <= 0) {
  348. qh->frame_usecs[k] +=
  349. hsotg->frame_usecs[k]
  350. + t_left;
  351. hsotg->frame_usecs[k] = -t_left;
  352. return i;
  353. } else {
  354. qh->frame_usecs[k] +=
  355. hsotg->frame_usecs[k];
  356. hsotg->frame_usecs[k] = 0;
  357. }
  358. }
  359. }
  360. /* add the frame time to x time */
  361. xtime += hsotg->frame_usecs[j];
  362. /* we must have a fully available next frame or break */
  363. if (xtime < utime &&
  364. hsotg->frame_usecs[j] == max_uframe_usecs[j])
  365. continue;
  366. }
  367. }
  368. return -ENOSPC;
  369. }
  370. static int dwc2_find_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  371. {
  372. int ret;
  373. if (qh->dev_speed == USB_SPEED_HIGH) {
  374. /* if this is a hs transaction we need a full frame */
  375. ret = dwc2_find_single_uframe(hsotg, qh);
  376. } else {
  377. /*
  378. * if this is a fs transaction we may need a sequence
  379. * of frames
  380. */
  381. ret = dwc2_find_multi_uframe(hsotg, qh);
  382. }
  383. return ret;
  384. }
  385. /**
  386. * dwc2_check_max_xfer_size() - Checks that the max transfer size allowed in a
  387. * host channel is large enough to handle the maximum data transfer in a single
  388. * (micro)frame for a periodic transfer
  389. *
  390. * @hsotg: The HCD state structure for the DWC OTG controller
  391. * @qh: QH for a periodic endpoint
  392. *
  393. * Return: 0 if successful, negative error code otherwise
  394. */
  395. static int dwc2_check_max_xfer_size(struct dwc2_hsotg *hsotg,
  396. struct dwc2_qh *qh)
  397. {
  398. u32 max_xfer_size;
  399. u32 max_channel_xfer_size;
  400. int status = 0;
  401. max_xfer_size = dwc2_max_packet(qh->maxp) * dwc2_hb_mult(qh->maxp);
  402. max_channel_xfer_size = hsotg->core_params->max_transfer_size;
  403. if (max_xfer_size > max_channel_xfer_size) {
  404. dev_err(hsotg->dev,
  405. "%s: Periodic xfer length %d > max xfer length for channel %d\n",
  406. __func__, max_xfer_size, max_channel_xfer_size);
  407. status = -ENOSPC;
  408. }
  409. return status;
  410. }
  411. /**
  412. * dwc2_schedule_periodic() - Schedules an interrupt or isochronous transfer in
  413. * the periodic schedule
  414. *
  415. * @hsotg: The HCD state structure for the DWC OTG controller
  416. * @qh: QH for the periodic transfer. The QH should already contain the
  417. * scheduling information.
  418. *
  419. * Return: 0 if successful, negative error code otherwise
  420. */
  421. static int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  422. {
  423. int status;
  424. if (hsotg->core_params->uframe_sched > 0) {
  425. int frame = -1;
  426. status = dwc2_find_uframe(hsotg, qh);
  427. if (status == 0)
  428. frame = 7;
  429. else if (status > 0)
  430. frame = status - 1;
  431. /* Set the new frame up */
  432. if (frame >= 0) {
  433. qh->sched_frame &= ~0x7;
  434. qh->sched_frame |= (frame & 7);
  435. }
  436. if (status > 0)
  437. status = 0;
  438. } else {
  439. status = dwc2_periodic_channel_available(hsotg);
  440. if (status) {
  441. dev_info(hsotg->dev,
  442. "%s: No host channel available for periodic transfer\n",
  443. __func__);
  444. return status;
  445. }
  446. status = dwc2_check_periodic_bandwidth(hsotg, qh);
  447. }
  448. if (status) {
  449. dev_dbg(hsotg->dev,
  450. "%s: Insufficient periodic bandwidth for periodic transfer\n",
  451. __func__);
  452. return status;
  453. }
  454. status = dwc2_check_max_xfer_size(hsotg, qh);
  455. if (status) {
  456. dev_dbg(hsotg->dev,
  457. "%s: Channel max transfer size too small for periodic transfer\n",
  458. __func__);
  459. return status;
  460. }
  461. if (hsotg->core_params->dma_desc_enable > 0)
  462. /* Don't rely on SOF and start in ready schedule */
  463. list_add_tail(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
  464. else
  465. /* Always start in inactive schedule */
  466. list_add_tail(&qh->qh_list_entry,
  467. &hsotg->periodic_sched_inactive);
  468. if (hsotg->core_params->uframe_sched <= 0)
  469. /* Reserve periodic channel */
  470. hsotg->periodic_channels++;
  471. /* Update claimed usecs per (micro)frame */
  472. hsotg->periodic_usecs += qh->usecs;
  473. return status;
  474. }
  475. /**
  476. * dwc2_deschedule_periodic() - Removes an interrupt or isochronous transfer
  477. * from the periodic schedule
  478. *
  479. * @hsotg: The HCD state structure for the DWC OTG controller
  480. * @qh: QH for the periodic transfer
  481. */
  482. static void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg,
  483. struct dwc2_qh *qh)
  484. {
  485. int i;
  486. list_del_init(&qh->qh_list_entry);
  487. /* Update claimed usecs per (micro)frame */
  488. hsotg->periodic_usecs -= qh->usecs;
  489. if (hsotg->core_params->uframe_sched > 0) {
  490. for (i = 0; i < 8; i++) {
  491. hsotg->frame_usecs[i] += qh->frame_usecs[i];
  492. qh->frame_usecs[i] = 0;
  493. }
  494. } else {
  495. /* Release periodic channel reservation */
  496. hsotg->periodic_channels--;
  497. }
  498. }
  499. /**
  500. * dwc2_hcd_qh_add() - Adds a QH to either the non periodic or periodic
  501. * schedule if it is not already in the schedule. If the QH is already in
  502. * the schedule, no action is taken.
  503. *
  504. * @hsotg: The HCD state structure for the DWC OTG controller
  505. * @qh: The QH to add
  506. *
  507. * Return: 0 if successful, negative error code otherwise
  508. */
  509. int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  510. {
  511. int status;
  512. u32 intr_mask;
  513. if (dbg_qh(qh))
  514. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  515. if (!list_empty(&qh->qh_list_entry))
  516. /* QH already in a schedule */
  517. return 0;
  518. /* Add the new QH to the appropriate schedule */
  519. if (dwc2_qh_is_non_per(qh)) {
  520. /* Always start in inactive schedule */
  521. list_add_tail(&qh->qh_list_entry,
  522. &hsotg->non_periodic_sched_inactive);
  523. return 0;
  524. }
  525. status = dwc2_schedule_periodic(hsotg, qh);
  526. if (status)
  527. return status;
  528. if (!hsotg->periodic_qh_count) {
  529. intr_mask = readl(hsotg->regs + GINTMSK);
  530. intr_mask |= GINTSTS_SOF;
  531. writel(intr_mask, hsotg->regs + GINTMSK);
  532. }
  533. hsotg->periodic_qh_count++;
  534. return 0;
  535. }
  536. /**
  537. * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic
  538. * schedule. Memory is not freed.
  539. *
  540. * @hsotg: The HCD state structure
  541. * @qh: QH to remove from schedule
  542. */
  543. void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  544. {
  545. u32 intr_mask;
  546. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  547. if (list_empty(&qh->qh_list_entry))
  548. /* QH is not in a schedule */
  549. return;
  550. if (dwc2_qh_is_non_per(qh)) {
  551. if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry)
  552. hsotg->non_periodic_qh_ptr =
  553. hsotg->non_periodic_qh_ptr->next;
  554. list_del_init(&qh->qh_list_entry);
  555. return;
  556. }
  557. dwc2_deschedule_periodic(hsotg, qh);
  558. hsotg->periodic_qh_count--;
  559. if (!hsotg->periodic_qh_count) {
  560. intr_mask = readl(hsotg->regs + GINTMSK);
  561. intr_mask &= ~GINTSTS_SOF;
  562. writel(intr_mask, hsotg->regs + GINTMSK);
  563. }
  564. }
  565. /*
  566. * Schedule the next continuing periodic split transfer
  567. */
  568. static void dwc2_sched_periodic_split(struct dwc2_hsotg *hsotg,
  569. struct dwc2_qh *qh, u16 frame_number,
  570. int sched_next_periodic_split)
  571. {
  572. u16 incr;
  573. if (sched_next_periodic_split) {
  574. qh->sched_frame = frame_number;
  575. incr = dwc2_frame_num_inc(qh->start_split_frame, 1);
  576. if (dwc2_frame_num_le(frame_number, incr)) {
  577. /*
  578. * Allow one frame to elapse after start split
  579. * microframe before scheduling complete split, but
  580. * DON'T if we are doing the next start split in the
  581. * same frame for an ISOC out
  582. */
  583. if (qh->ep_type != USB_ENDPOINT_XFER_ISOC ||
  584. qh->ep_is_in != 0) {
  585. qh->sched_frame =
  586. dwc2_frame_num_inc(qh->sched_frame, 1);
  587. }
  588. }
  589. } else {
  590. qh->sched_frame = dwc2_frame_num_inc(qh->start_split_frame,
  591. qh->interval);
  592. if (dwc2_frame_num_le(qh->sched_frame, frame_number))
  593. qh->sched_frame = frame_number;
  594. qh->sched_frame |= 0x7;
  595. qh->start_split_frame = qh->sched_frame;
  596. }
  597. }
  598. /*
  599. * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  600. * non-periodic schedule. The QH is added to the inactive non-periodic
  601. * schedule if any QTDs are still attached to the QH.
  602. *
  603. * For periodic QHs, the QH is removed from the periodic queued schedule. If
  604. * there are any QTDs still attached to the QH, the QH is added to either the
  605. * periodic inactive schedule or the periodic ready schedule and its next
  606. * scheduled frame is calculated. The QH is placed in the ready schedule if
  607. * the scheduled frame has been reached already. Otherwise it's placed in the
  608. * inactive schedule. If there are no QTDs attached to the QH, the QH is
  609. * completely removed from the periodic schedule.
  610. */
  611. void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  612. int sched_next_periodic_split)
  613. {
  614. u16 frame_number;
  615. if (dbg_qh(qh))
  616. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  617. if (dwc2_qh_is_non_per(qh)) {
  618. dwc2_hcd_qh_unlink(hsotg, qh);
  619. if (!list_empty(&qh->qtd_list))
  620. /* Add back to inactive non-periodic schedule */
  621. dwc2_hcd_qh_add(hsotg, qh);
  622. return;
  623. }
  624. frame_number = dwc2_hcd_get_frame_number(hsotg);
  625. if (qh->do_split) {
  626. dwc2_sched_periodic_split(hsotg, qh, frame_number,
  627. sched_next_periodic_split);
  628. } else {
  629. qh->sched_frame = dwc2_frame_num_inc(qh->sched_frame,
  630. qh->interval);
  631. if (dwc2_frame_num_le(qh->sched_frame, frame_number))
  632. qh->sched_frame = frame_number;
  633. }
  634. if (list_empty(&qh->qtd_list)) {
  635. dwc2_hcd_qh_unlink(hsotg, qh);
  636. return;
  637. }
  638. /*
  639. * Remove from periodic_sched_queued and move to
  640. * appropriate queue
  641. */
  642. if ((hsotg->core_params->uframe_sched > 0 &&
  643. dwc2_frame_num_le(qh->sched_frame, frame_number)) ||
  644. (hsotg->core_params->uframe_sched <= 0 &&
  645. qh->sched_frame == frame_number))
  646. list_move(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
  647. else
  648. list_move(&qh->qh_list_entry, &hsotg->periodic_sched_inactive);
  649. }
  650. /**
  651. * dwc2_hcd_qtd_init() - Initializes a QTD structure
  652. *
  653. * @qtd: The QTD to initialize
  654. * @urb: The associated URB
  655. */
  656. void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
  657. {
  658. qtd->urb = urb;
  659. if (dwc2_hcd_get_pipe_type(&urb->pipe_info) ==
  660. USB_ENDPOINT_XFER_CONTROL) {
  661. /*
  662. * The only time the QTD data toggle is used is on the data
  663. * phase of control transfers. This phase always starts with
  664. * DATA1.
  665. */
  666. qtd->data_toggle = DWC2_HC_PID_DATA1;
  667. qtd->control_phase = DWC2_CONTROL_SETUP;
  668. }
  669. /* Start split */
  670. qtd->complete_split = 0;
  671. qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
  672. qtd->isoc_split_offset = 0;
  673. qtd->in_process = 0;
  674. /* Store the qtd ptr in the urb to reference the QTD */
  675. urb->qtd = qtd;
  676. }
  677. /**
  678. * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH
  679. * Caller must hold driver lock.
  680. *
  681. * @hsotg: The DWC HCD structure
  682. * @qtd: The QTD to add
  683. * @qh: Queue head to add qtd to
  684. *
  685. * Return: 0 if successful, negative error code otherwise
  686. *
  687. * If the QH to which the QTD is added is not currently scheduled, it is placed
  688. * into the proper schedule based on its EP type.
  689. */
  690. int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
  691. struct dwc2_qh *qh)
  692. {
  693. int retval;
  694. if (unlikely(!qh)) {
  695. dev_err(hsotg->dev, "%s: Invalid QH\n", __func__);
  696. retval = -EINVAL;
  697. goto fail;
  698. }
  699. retval = dwc2_hcd_qh_add(hsotg, qh);
  700. if (retval)
  701. goto fail;
  702. qtd->qh = qh;
  703. list_add_tail(&qtd->qtd_list_entry, &qh->qtd_list);
  704. return 0;
  705. fail:
  706. return retval;
  707. }