hcd_queue.c 22 KB

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