hcd_queue.c 22 KB

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