hcd_ddma.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. /*
  2. * hcd_ddma.c - DesignWare HS OTG Controller descriptor DMA 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 Descriptor DMA implementation for Host mode
  38. */
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/io.h>
  45. #include <linux/slab.h>
  46. #include <linux/usb.h>
  47. #include <linux/usb/hcd.h>
  48. #include <linux/usb/ch11.h>
  49. #include "core.h"
  50. #include "hcd.h"
  51. static u16 dwc2_frame_list_idx(u16 frame)
  52. {
  53. return frame & (FRLISTEN_64_SIZE - 1);
  54. }
  55. static u16 dwc2_desclist_idx_inc(u16 idx, u16 inc, u8 speed)
  56. {
  57. return (idx + inc) &
  58. ((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
  59. MAX_DMA_DESC_NUM_GENERIC) - 1);
  60. }
  61. static u16 dwc2_desclist_idx_dec(u16 idx, u16 inc, u8 speed)
  62. {
  63. return (idx - inc) &
  64. ((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
  65. MAX_DMA_DESC_NUM_GENERIC) - 1);
  66. }
  67. static u16 dwc2_max_desc_num(struct dwc2_qh *qh)
  68. {
  69. return (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
  70. qh->dev_speed == USB_SPEED_HIGH) ?
  71. MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC;
  72. }
  73. static u16 dwc2_frame_incr_val(struct dwc2_qh *qh)
  74. {
  75. return qh->dev_speed == USB_SPEED_HIGH ?
  76. (qh->interval + 8 - 1) / 8 : qh->interval;
  77. }
  78. static int dwc2_desc_list_alloc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  79. gfp_t flags)
  80. {
  81. qh->desc_list = dma_alloc_coherent(hsotg->dev,
  82. sizeof(struct dwc2_hcd_dma_desc) *
  83. dwc2_max_desc_num(qh), &qh->desc_list_dma,
  84. flags);
  85. if (!qh->desc_list)
  86. return -ENOMEM;
  87. memset(qh->desc_list, 0,
  88. sizeof(struct dwc2_hcd_dma_desc) * dwc2_max_desc_num(qh));
  89. qh->n_bytes = kzalloc(sizeof(u32) * dwc2_max_desc_num(qh), flags);
  90. if (!qh->n_bytes) {
  91. dma_free_coherent(hsotg->dev, sizeof(struct dwc2_hcd_dma_desc)
  92. * dwc2_max_desc_num(qh), qh->desc_list,
  93. qh->desc_list_dma);
  94. qh->desc_list = NULL;
  95. return -ENOMEM;
  96. }
  97. return 0;
  98. }
  99. static void dwc2_desc_list_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  100. {
  101. if (qh->desc_list) {
  102. dma_free_coherent(hsotg->dev, sizeof(struct dwc2_hcd_dma_desc)
  103. * dwc2_max_desc_num(qh), qh->desc_list,
  104. qh->desc_list_dma);
  105. qh->desc_list = NULL;
  106. }
  107. kfree(qh->n_bytes);
  108. qh->n_bytes = NULL;
  109. }
  110. static int dwc2_frame_list_alloc(struct dwc2_hsotg *hsotg, gfp_t mem_flags)
  111. {
  112. if (hsotg->frame_list)
  113. return 0;
  114. hsotg->frame_list = dma_alloc_coherent(hsotg->dev,
  115. 4 * FRLISTEN_64_SIZE,
  116. &hsotg->frame_list_dma,
  117. mem_flags);
  118. if (!hsotg->frame_list)
  119. return -ENOMEM;
  120. memset(hsotg->frame_list, 0, 4 * FRLISTEN_64_SIZE);
  121. return 0;
  122. }
  123. static void dwc2_frame_list_free(struct dwc2_hsotg *hsotg)
  124. {
  125. u32 *frame_list;
  126. dma_addr_t frame_list_dma;
  127. unsigned long flags;
  128. spin_lock_irqsave(&hsotg->lock, flags);
  129. if (!hsotg->frame_list) {
  130. spin_unlock_irqrestore(&hsotg->lock, flags);
  131. return;
  132. }
  133. frame_list = hsotg->frame_list;
  134. frame_list_dma = hsotg->frame_list_dma;
  135. hsotg->frame_list = NULL;
  136. spin_unlock_irqrestore(&hsotg->lock, flags);
  137. dma_free_coherent(hsotg->dev, 4 * FRLISTEN_64_SIZE, frame_list,
  138. frame_list_dma);
  139. }
  140. static void dwc2_per_sched_enable(struct dwc2_hsotg *hsotg, u32 fr_list_en)
  141. {
  142. u32 hcfg;
  143. unsigned long flags;
  144. spin_lock_irqsave(&hsotg->lock, flags);
  145. hcfg = dwc2_readl(hsotg->regs + HCFG);
  146. if (hcfg & HCFG_PERSCHEDENA) {
  147. /* already enabled */
  148. spin_unlock_irqrestore(&hsotg->lock, flags);
  149. return;
  150. }
  151. dwc2_writel(hsotg->frame_list_dma, hsotg->regs + HFLBADDR);
  152. hcfg &= ~HCFG_FRLISTEN_MASK;
  153. hcfg |= fr_list_en | HCFG_PERSCHEDENA;
  154. dev_vdbg(hsotg->dev, "Enabling Periodic schedule\n");
  155. dwc2_writel(hcfg, hsotg->regs + HCFG);
  156. spin_unlock_irqrestore(&hsotg->lock, flags);
  157. }
  158. static void dwc2_per_sched_disable(struct dwc2_hsotg *hsotg)
  159. {
  160. u32 hcfg;
  161. unsigned long flags;
  162. spin_lock_irqsave(&hsotg->lock, flags);
  163. hcfg = dwc2_readl(hsotg->regs + HCFG);
  164. if (!(hcfg & HCFG_PERSCHEDENA)) {
  165. /* already disabled */
  166. spin_unlock_irqrestore(&hsotg->lock, flags);
  167. return;
  168. }
  169. hcfg &= ~HCFG_PERSCHEDENA;
  170. dev_vdbg(hsotg->dev, "Disabling Periodic schedule\n");
  171. dwc2_writel(hcfg, hsotg->regs + HCFG);
  172. spin_unlock_irqrestore(&hsotg->lock, flags);
  173. }
  174. /*
  175. * Activates/Deactivates FrameList entries for the channel based on endpoint
  176. * servicing period
  177. */
  178. static void dwc2_update_frame_list(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  179. int enable)
  180. {
  181. struct dwc2_host_chan *chan;
  182. u16 i, j, inc;
  183. if (!hsotg) {
  184. pr_err("hsotg = %p\n", hsotg);
  185. return;
  186. }
  187. if (!qh->channel) {
  188. dev_err(hsotg->dev, "qh->channel = %p\n", qh->channel);
  189. return;
  190. }
  191. if (!hsotg->frame_list) {
  192. dev_err(hsotg->dev, "hsotg->frame_list = %p\n",
  193. hsotg->frame_list);
  194. return;
  195. }
  196. chan = qh->channel;
  197. inc = dwc2_frame_incr_val(qh);
  198. if (qh->ep_type == USB_ENDPOINT_XFER_ISOC)
  199. i = dwc2_frame_list_idx(qh->sched_frame);
  200. else
  201. i = 0;
  202. j = i;
  203. do {
  204. if (enable)
  205. hsotg->frame_list[j] |= 1 << chan->hc_num;
  206. else
  207. hsotg->frame_list[j] &= ~(1 << chan->hc_num);
  208. j = (j + inc) & (FRLISTEN_64_SIZE - 1);
  209. } while (j != i);
  210. if (!enable)
  211. return;
  212. chan->schinfo = 0;
  213. if (chan->speed == USB_SPEED_HIGH && qh->interval) {
  214. j = 1;
  215. /* TODO - check this */
  216. inc = (8 + qh->interval - 1) / qh->interval;
  217. for (i = 0; i < inc; i++) {
  218. chan->schinfo |= j;
  219. j = j << qh->interval;
  220. }
  221. } else {
  222. chan->schinfo = 0xff;
  223. }
  224. }
  225. static void dwc2_release_channel_ddma(struct dwc2_hsotg *hsotg,
  226. struct dwc2_qh *qh)
  227. {
  228. struct dwc2_host_chan *chan = qh->channel;
  229. if (dwc2_qh_is_non_per(qh)) {
  230. if (hsotg->core_params->uframe_sched > 0)
  231. hsotg->available_host_channels++;
  232. else
  233. hsotg->non_periodic_channels--;
  234. } else {
  235. dwc2_update_frame_list(hsotg, qh, 0);
  236. }
  237. /*
  238. * The condition is added to prevent double cleanup try in case of
  239. * device disconnect. See channel cleanup in dwc2_hcd_disconnect().
  240. */
  241. if (chan->qh) {
  242. if (!list_empty(&chan->hc_list_entry))
  243. list_del(&chan->hc_list_entry);
  244. dwc2_hc_cleanup(hsotg, chan);
  245. list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
  246. chan->qh = NULL;
  247. }
  248. qh->channel = NULL;
  249. qh->ntd = 0;
  250. if (qh->desc_list)
  251. memset(qh->desc_list, 0, sizeof(struct dwc2_hcd_dma_desc) *
  252. dwc2_max_desc_num(qh));
  253. }
  254. /**
  255. * dwc2_hcd_qh_init_ddma() - Initializes a QH structure's Descriptor DMA
  256. * related members
  257. *
  258. * @hsotg: The HCD state structure for the DWC OTG controller
  259. * @qh: The QH to init
  260. *
  261. * Return: 0 if successful, negative error code otherwise
  262. *
  263. * Allocates memory for the descriptor list. For the first periodic QH,
  264. * allocates memory for the FrameList and enables periodic scheduling.
  265. */
  266. int dwc2_hcd_qh_init_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  267. gfp_t mem_flags)
  268. {
  269. int retval;
  270. if (qh->do_split) {
  271. dev_err(hsotg->dev,
  272. "SPLIT Transfers are not supported in Descriptor DMA mode.\n");
  273. retval = -EINVAL;
  274. goto err0;
  275. }
  276. retval = dwc2_desc_list_alloc(hsotg, qh, mem_flags);
  277. if (retval)
  278. goto err0;
  279. if (qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
  280. qh->ep_type == USB_ENDPOINT_XFER_INT) {
  281. if (!hsotg->frame_list) {
  282. retval = dwc2_frame_list_alloc(hsotg, mem_flags);
  283. if (retval)
  284. goto err1;
  285. /* Enable periodic schedule on first periodic QH */
  286. dwc2_per_sched_enable(hsotg, HCFG_FRLISTEN_64);
  287. }
  288. }
  289. qh->ntd = 0;
  290. return 0;
  291. err1:
  292. dwc2_desc_list_free(hsotg, qh);
  293. err0:
  294. return retval;
  295. }
  296. /**
  297. * dwc2_hcd_qh_free_ddma() - Frees a QH structure's Descriptor DMA related
  298. * members
  299. *
  300. * @hsotg: The HCD state structure for the DWC OTG controller
  301. * @qh: The QH to free
  302. *
  303. * Frees descriptor list memory associated with the QH. If QH is periodic and
  304. * the last, frees FrameList memory and disables periodic scheduling.
  305. */
  306. void dwc2_hcd_qh_free_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  307. {
  308. dwc2_desc_list_free(hsotg, qh);
  309. /*
  310. * Channel still assigned due to some reasons.
  311. * Seen on Isoc URB dequeue. Channel halted but no subsequent
  312. * ChHalted interrupt to release the channel. Afterwards
  313. * when it comes here from endpoint disable routine
  314. * channel remains assigned.
  315. */
  316. if (qh->channel)
  317. dwc2_release_channel_ddma(hsotg, qh);
  318. if ((qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
  319. qh->ep_type == USB_ENDPOINT_XFER_INT) &&
  320. (hsotg->core_params->uframe_sched > 0 ||
  321. !hsotg->periodic_channels) && hsotg->frame_list) {
  322. dwc2_per_sched_disable(hsotg);
  323. dwc2_frame_list_free(hsotg);
  324. }
  325. }
  326. static u8 dwc2_frame_to_desc_idx(struct dwc2_qh *qh, u16 frame_idx)
  327. {
  328. if (qh->dev_speed == USB_SPEED_HIGH)
  329. /* Descriptor set (8 descriptors) index which is 8-aligned */
  330. return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
  331. else
  332. return frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1);
  333. }
  334. /*
  335. * Determine starting frame for Isochronous transfer.
  336. * Few frames skipped to prevent race condition with HC.
  337. */
  338. static u16 dwc2_calc_starting_frame(struct dwc2_hsotg *hsotg,
  339. struct dwc2_qh *qh, u16 *skip_frames)
  340. {
  341. u16 frame;
  342. hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
  343. /* sched_frame is always frame number (not uFrame) both in FS and HS! */
  344. /*
  345. * skip_frames is used to limit activated descriptors number
  346. * to avoid the situation when HC services the last activated
  347. * descriptor firstly.
  348. * Example for FS:
  349. * Current frame is 1, scheduled frame is 3. Since HC always fetches
  350. * the descriptor corresponding to curr_frame+1, the descriptor
  351. * corresponding to frame 2 will be fetched. If the number of
  352. * descriptors is max=64 (or greather) the list will be fully programmed
  353. * with Active descriptors and it is possible case (rare) that the
  354. * latest descriptor(considering rollback) corresponding to frame 2 will
  355. * be serviced first. HS case is more probable because, in fact, up to
  356. * 11 uframes (16 in the code) may be skipped.
  357. */
  358. if (qh->dev_speed == USB_SPEED_HIGH) {
  359. /*
  360. * Consider uframe counter also, to start xfer asap. If half of
  361. * the frame elapsed skip 2 frames otherwise just 1 frame.
  362. * Starting descriptor index must be 8-aligned, so if the
  363. * current frame is near to complete the next one is skipped as
  364. * well.
  365. */
  366. if (dwc2_micro_frame_num(hsotg->frame_number) >= 5) {
  367. *skip_frames = 2 * 8;
  368. frame = dwc2_frame_num_inc(hsotg->frame_number,
  369. *skip_frames);
  370. } else {
  371. *skip_frames = 1 * 8;
  372. frame = dwc2_frame_num_inc(hsotg->frame_number,
  373. *skip_frames);
  374. }
  375. frame = dwc2_full_frame_num(frame);
  376. } else {
  377. /*
  378. * Two frames are skipped for FS - the current and the next.
  379. * But for descriptor programming, 1 frame (descriptor) is
  380. * enough, see example above.
  381. */
  382. *skip_frames = 1;
  383. frame = dwc2_frame_num_inc(hsotg->frame_number, 2);
  384. }
  385. return frame;
  386. }
  387. /*
  388. * Calculate initial descriptor index for isochronous transfer based on
  389. * scheduled frame
  390. */
  391. static u16 dwc2_recalc_initial_desc_idx(struct dwc2_hsotg *hsotg,
  392. struct dwc2_qh *qh)
  393. {
  394. u16 frame, fr_idx, fr_idx_tmp, skip_frames;
  395. /*
  396. * With current ISOC processing algorithm the channel is being released
  397. * when no more QTDs in the list (qh->ntd == 0). Thus this function is
  398. * called only when qh->ntd == 0 and qh->channel == 0.
  399. *
  400. * So qh->channel != NULL branch is not used and just not removed from
  401. * the source file. It is required for another possible approach which
  402. * is, do not disable and release the channel when ISOC session
  403. * completed, just move QH to inactive schedule until new QTD arrives.
  404. * On new QTD, the QH moved back to 'ready' schedule, starting frame and
  405. * therefore starting desc_index are recalculated. In this case channel
  406. * is released only on ep_disable.
  407. */
  408. /*
  409. * Calculate starting descriptor index. For INTERRUPT endpoint it is
  410. * always 0.
  411. */
  412. if (qh->channel) {
  413. frame = dwc2_calc_starting_frame(hsotg, qh, &skip_frames);
  414. /*
  415. * Calculate initial descriptor index based on FrameList current
  416. * bitmap and servicing period
  417. */
  418. fr_idx_tmp = dwc2_frame_list_idx(frame);
  419. fr_idx = (FRLISTEN_64_SIZE +
  420. dwc2_frame_list_idx(qh->sched_frame) - fr_idx_tmp)
  421. % dwc2_frame_incr_val(qh);
  422. fr_idx = (fr_idx + fr_idx_tmp) % FRLISTEN_64_SIZE;
  423. } else {
  424. qh->sched_frame = dwc2_calc_starting_frame(hsotg, qh,
  425. &skip_frames);
  426. fr_idx = dwc2_frame_list_idx(qh->sched_frame);
  427. }
  428. qh->td_first = qh->td_last = dwc2_frame_to_desc_idx(qh, fr_idx);
  429. return skip_frames;
  430. }
  431. #define ISOC_URB_GIVEBACK_ASAP
  432. #define MAX_ISOC_XFER_SIZE_FS 1023
  433. #define MAX_ISOC_XFER_SIZE_HS 3072
  434. #define DESCNUM_THRESHOLD 4
  435. static void dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
  436. struct dwc2_qtd *qtd,
  437. struct dwc2_qh *qh, u32 max_xfer_size,
  438. u16 idx)
  439. {
  440. struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[idx];
  441. struct dwc2_hcd_iso_packet_desc *frame_desc;
  442. memset(dma_desc, 0, sizeof(*dma_desc));
  443. frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  444. if (frame_desc->length > max_xfer_size)
  445. qh->n_bytes[idx] = max_xfer_size;
  446. else
  447. qh->n_bytes[idx] = frame_desc->length;
  448. dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
  449. dma_desc->status = qh->n_bytes[idx] << HOST_DMA_ISOC_NBYTES_SHIFT &
  450. HOST_DMA_ISOC_NBYTES_MASK;
  451. #ifdef ISOC_URB_GIVEBACK_ASAP
  452. /* Set IOC for each descriptor corresponding to last frame of URB */
  453. if (qtd->isoc_frame_index_last == qtd->urb->packet_count)
  454. dma_desc->status |= HOST_DMA_IOC;
  455. #endif
  456. qh->ntd++;
  457. qtd->isoc_frame_index_last++;
  458. }
  459. static void dwc2_init_isoc_dma_desc(struct dwc2_hsotg *hsotg,
  460. struct dwc2_qh *qh, u16 skip_frames)
  461. {
  462. struct dwc2_qtd *qtd;
  463. u32 max_xfer_size;
  464. u16 idx, inc, n_desc, ntd_max = 0;
  465. idx = qh->td_last;
  466. inc = qh->interval;
  467. n_desc = 0;
  468. if (qh->interval) {
  469. ntd_max = (dwc2_max_desc_num(qh) + qh->interval - 1) /
  470. qh->interval;
  471. if (skip_frames && !qh->channel)
  472. ntd_max -= skip_frames / qh->interval;
  473. }
  474. max_xfer_size = qh->dev_speed == USB_SPEED_HIGH ?
  475. MAX_ISOC_XFER_SIZE_HS : MAX_ISOC_XFER_SIZE_FS;
  476. list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
  477. while (qh->ntd < ntd_max && qtd->isoc_frame_index_last <
  478. qtd->urb->packet_count) {
  479. if (n_desc > 1)
  480. qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
  481. dwc2_fill_host_isoc_dma_desc(hsotg, qtd, qh,
  482. max_xfer_size, idx);
  483. idx = dwc2_desclist_idx_inc(idx, inc, qh->dev_speed);
  484. n_desc++;
  485. }
  486. qtd->in_process = 1;
  487. }
  488. qh->td_last = idx;
  489. #ifdef ISOC_URB_GIVEBACK_ASAP
  490. /* Set IOC for last descriptor if descriptor list is full */
  491. if (qh->ntd == ntd_max) {
  492. idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  493. qh->desc_list[idx].status |= HOST_DMA_IOC;
  494. }
  495. #else
  496. /*
  497. * Set IOC bit only for one descriptor. Always try to be ahead of HW
  498. * processing, i.e. on IOC generation driver activates next descriptor
  499. * but core continues to process descriptors following the one with IOC
  500. * set.
  501. */
  502. if (n_desc > DESCNUM_THRESHOLD)
  503. /*
  504. * Move IOC "up". Required even if there is only one QTD
  505. * in the list, because QTDs might continue to be queued,
  506. * but during the activation it was only one queued.
  507. * Actually more than one QTD might be in the list if this
  508. * function called from XferCompletion - QTDs was queued during
  509. * HW processing of the previous descriptor chunk.
  510. */
  511. idx = dwc2_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2),
  512. qh->dev_speed);
  513. else
  514. /*
  515. * Set the IOC for the latest descriptor if either number of
  516. * descriptors is not greater than threshold or no more new
  517. * descriptors activated
  518. */
  519. idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  520. qh->desc_list[idx].status |= HOST_DMA_IOC;
  521. #endif
  522. if (n_desc) {
  523. qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
  524. if (n_desc > 1)
  525. qh->desc_list[0].status |= HOST_DMA_A;
  526. }
  527. }
  528. static void dwc2_fill_host_dma_desc(struct dwc2_hsotg *hsotg,
  529. struct dwc2_host_chan *chan,
  530. struct dwc2_qtd *qtd, struct dwc2_qh *qh,
  531. int n_desc)
  532. {
  533. struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[n_desc];
  534. int len = chan->xfer_len;
  535. if (len > MAX_DMA_DESC_SIZE - (chan->max_packet - 1))
  536. len = MAX_DMA_DESC_SIZE - (chan->max_packet - 1);
  537. if (chan->ep_is_in) {
  538. int num_packets;
  539. if (len > 0 && chan->max_packet)
  540. num_packets = (len + chan->max_packet - 1)
  541. / chan->max_packet;
  542. else
  543. /* Need 1 packet for transfer length of 0 */
  544. num_packets = 1;
  545. /* Always program an integral # of packets for IN transfers */
  546. len = num_packets * chan->max_packet;
  547. }
  548. dma_desc->status = len << HOST_DMA_NBYTES_SHIFT & HOST_DMA_NBYTES_MASK;
  549. qh->n_bytes[n_desc] = len;
  550. if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL &&
  551. qtd->control_phase == DWC2_CONTROL_SETUP)
  552. dma_desc->status |= HOST_DMA_SUP;
  553. dma_desc->buf = (u32)chan->xfer_dma;
  554. /*
  555. * Last (or only) descriptor of IN transfer with actual size less
  556. * than MaxPacket
  557. */
  558. if (len > chan->xfer_len) {
  559. chan->xfer_len = 0;
  560. } else {
  561. chan->xfer_dma += len;
  562. chan->xfer_len -= len;
  563. }
  564. }
  565. static void dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg *hsotg,
  566. struct dwc2_qh *qh)
  567. {
  568. struct dwc2_qtd *qtd;
  569. struct dwc2_host_chan *chan = qh->channel;
  570. int n_desc = 0;
  571. dev_vdbg(hsotg->dev, "%s(): qh=%p dma=%08lx len=%d\n", __func__, qh,
  572. (unsigned long)chan->xfer_dma, chan->xfer_len);
  573. /*
  574. * Start with chan->xfer_dma initialized in assign_and_init_hc(), then
  575. * if SG transfer consists of multiple URBs, this pointer is re-assigned
  576. * to the buffer of the currently processed QTD. For non-SG request
  577. * there is always one QTD active.
  578. */
  579. list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
  580. dev_vdbg(hsotg->dev, "qtd=%p\n", qtd);
  581. if (n_desc) {
  582. /* SG request - more than 1 QTD */
  583. chan->xfer_dma = qtd->urb->dma +
  584. qtd->urb->actual_length;
  585. chan->xfer_len = qtd->urb->length -
  586. qtd->urb->actual_length;
  587. dev_vdbg(hsotg->dev, "buf=%08lx len=%d\n",
  588. (unsigned long)chan->xfer_dma, chan->xfer_len);
  589. }
  590. qtd->n_desc = 0;
  591. do {
  592. if (n_desc > 1) {
  593. qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
  594. dev_vdbg(hsotg->dev,
  595. "set A bit in desc %d (%p)\n",
  596. n_desc - 1,
  597. &qh->desc_list[n_desc - 1]);
  598. }
  599. dwc2_fill_host_dma_desc(hsotg, chan, qtd, qh, n_desc);
  600. dev_vdbg(hsotg->dev,
  601. "desc %d (%p) buf=%08x status=%08x\n",
  602. n_desc, &qh->desc_list[n_desc],
  603. qh->desc_list[n_desc].buf,
  604. qh->desc_list[n_desc].status);
  605. qtd->n_desc++;
  606. n_desc++;
  607. } while (chan->xfer_len > 0 &&
  608. n_desc != MAX_DMA_DESC_NUM_GENERIC);
  609. dev_vdbg(hsotg->dev, "n_desc=%d\n", n_desc);
  610. qtd->in_process = 1;
  611. if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL)
  612. break;
  613. if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
  614. break;
  615. }
  616. if (n_desc) {
  617. qh->desc_list[n_desc - 1].status |=
  618. HOST_DMA_IOC | HOST_DMA_EOL | HOST_DMA_A;
  619. dev_vdbg(hsotg->dev, "set IOC/EOL/A bits in desc %d (%p)\n",
  620. n_desc - 1, &qh->desc_list[n_desc - 1]);
  621. if (n_desc > 1) {
  622. qh->desc_list[0].status |= HOST_DMA_A;
  623. dev_vdbg(hsotg->dev, "set A bit in desc 0 (%p)\n",
  624. &qh->desc_list[0]);
  625. }
  626. chan->ntd = n_desc;
  627. }
  628. }
  629. /**
  630. * dwc2_hcd_start_xfer_ddma() - Starts a transfer in Descriptor DMA mode
  631. *
  632. * @hsotg: The HCD state structure for the DWC OTG controller
  633. * @qh: The QH to init
  634. *
  635. * Return: 0 if successful, negative error code otherwise
  636. *
  637. * For Control and Bulk endpoints, initializes descriptor list and starts the
  638. * transfer. For Interrupt and Isochronous endpoints, initializes descriptor
  639. * list then updates FrameList, marking appropriate entries as active.
  640. *
  641. * For Isochronous endpoints the starting descriptor index is calculated based
  642. * on the scheduled frame, but only on the first transfer descriptor within a
  643. * session. Then the transfer is started via enabling the channel.
  644. *
  645. * For Isochronous endpoints the channel is not halted on XferComplete
  646. * interrupt so remains assigned to the endpoint(QH) until session is done.
  647. */
  648. void dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  649. {
  650. /* Channel is already assigned */
  651. struct dwc2_host_chan *chan = qh->channel;
  652. u16 skip_frames = 0;
  653. switch (chan->ep_type) {
  654. case USB_ENDPOINT_XFER_CONTROL:
  655. case USB_ENDPOINT_XFER_BULK:
  656. dwc2_init_non_isoc_dma_desc(hsotg, qh);
  657. dwc2_hc_start_transfer_ddma(hsotg, chan);
  658. break;
  659. case USB_ENDPOINT_XFER_INT:
  660. dwc2_init_non_isoc_dma_desc(hsotg, qh);
  661. dwc2_update_frame_list(hsotg, qh, 1);
  662. dwc2_hc_start_transfer_ddma(hsotg, chan);
  663. break;
  664. case USB_ENDPOINT_XFER_ISOC:
  665. if (!qh->ntd)
  666. skip_frames = dwc2_recalc_initial_desc_idx(hsotg, qh);
  667. dwc2_init_isoc_dma_desc(hsotg, qh, skip_frames);
  668. if (!chan->xfer_started) {
  669. dwc2_update_frame_list(hsotg, qh, 1);
  670. /*
  671. * Always set to max, instead of actual size. Otherwise
  672. * ntd will be changed with channel being enabled. Not
  673. * recommended.
  674. */
  675. chan->ntd = dwc2_max_desc_num(qh);
  676. /* Enable channel only once for ISOC */
  677. dwc2_hc_start_transfer_ddma(hsotg, chan);
  678. }
  679. break;
  680. default:
  681. break;
  682. }
  683. }
  684. #define DWC2_CMPL_DONE 1
  685. #define DWC2_CMPL_STOP 2
  686. static int dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
  687. struct dwc2_host_chan *chan,
  688. struct dwc2_qtd *qtd,
  689. struct dwc2_qh *qh, u16 idx)
  690. {
  691. struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[idx];
  692. struct dwc2_hcd_iso_packet_desc *frame_desc;
  693. u16 remain = 0;
  694. int rc = 0;
  695. if (!qtd->urb)
  696. return -EINVAL;
  697. frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  698. dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
  699. if (chan->ep_is_in)
  700. remain = (dma_desc->status & HOST_DMA_ISOC_NBYTES_MASK) >>
  701. HOST_DMA_ISOC_NBYTES_SHIFT;
  702. if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
  703. /*
  704. * XactError, or unable to complete all the transactions
  705. * in the scheduled micro-frame/frame, both indicated by
  706. * HOST_DMA_STS_PKTERR
  707. */
  708. qtd->urb->error_count++;
  709. frame_desc->actual_length = qh->n_bytes[idx] - remain;
  710. frame_desc->status = -EPROTO;
  711. } else {
  712. /* Success */
  713. frame_desc->actual_length = qh->n_bytes[idx] - remain;
  714. frame_desc->status = 0;
  715. }
  716. if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  717. /*
  718. * urb->status is not used for isoc transfers here. The
  719. * individual frame_desc status are used instead.
  720. */
  721. dwc2_host_complete(hsotg, qtd, 0);
  722. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  723. /*
  724. * This check is necessary because urb_dequeue can be called
  725. * from urb complete callback (sound driver for example). All
  726. * pending URBs are dequeued there, so no need for further
  727. * processing.
  728. */
  729. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE)
  730. return -1;
  731. rc = DWC2_CMPL_DONE;
  732. }
  733. qh->ntd--;
  734. /* Stop if IOC requested descriptor reached */
  735. if (dma_desc->status & HOST_DMA_IOC)
  736. rc = DWC2_CMPL_STOP;
  737. return rc;
  738. }
  739. static void dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
  740. struct dwc2_host_chan *chan,
  741. enum dwc2_halt_status halt_status)
  742. {
  743. struct dwc2_hcd_iso_packet_desc *frame_desc;
  744. struct dwc2_qtd *qtd, *qtd_tmp;
  745. struct dwc2_qh *qh;
  746. u16 idx;
  747. int rc;
  748. qh = chan->qh;
  749. idx = qh->td_first;
  750. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
  751. list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
  752. qtd->in_process = 0;
  753. return;
  754. }
  755. if (halt_status == DWC2_HC_XFER_AHB_ERR ||
  756. halt_status == DWC2_HC_XFER_BABBLE_ERR) {
  757. /*
  758. * Channel is halted in these error cases, considered as serious
  759. * issues.
  760. * Complete all URBs marking all frames as failed, irrespective
  761. * whether some of the descriptors (frames) succeeded or not.
  762. * Pass error code to completion routine as well, to update
  763. * urb->status, some of class drivers might use it to stop
  764. * queing transfer requests.
  765. */
  766. int err = halt_status == DWC2_HC_XFER_AHB_ERR ?
  767. -EIO : -EOVERFLOW;
  768. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  769. qtd_list_entry) {
  770. if (qtd->urb) {
  771. for (idx = 0; idx < qtd->urb->packet_count;
  772. idx++) {
  773. frame_desc = &qtd->urb->iso_descs[idx];
  774. frame_desc->status = err;
  775. }
  776. dwc2_host_complete(hsotg, qtd, err);
  777. }
  778. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  779. }
  780. return;
  781. }
  782. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  783. if (!qtd->in_process)
  784. break;
  785. do {
  786. rc = dwc2_cmpl_host_isoc_dma_desc(hsotg, chan, qtd, qh,
  787. idx);
  788. if (rc < 0)
  789. return;
  790. idx = dwc2_desclist_idx_inc(idx, qh->interval,
  791. chan->speed);
  792. if (rc == DWC2_CMPL_STOP)
  793. goto stop_scan;
  794. if (rc == DWC2_CMPL_DONE)
  795. break;
  796. } while (idx != qh->td_first);
  797. }
  798. stop_scan:
  799. qh->td_first = idx;
  800. }
  801. static int dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg *hsotg,
  802. struct dwc2_host_chan *chan,
  803. struct dwc2_qtd *qtd,
  804. struct dwc2_hcd_dma_desc *dma_desc,
  805. enum dwc2_halt_status halt_status,
  806. u32 n_bytes, int *xfer_done)
  807. {
  808. struct dwc2_hcd_urb *urb = qtd->urb;
  809. u16 remain = 0;
  810. if (chan->ep_is_in)
  811. remain = (dma_desc->status & HOST_DMA_NBYTES_MASK) >>
  812. HOST_DMA_NBYTES_SHIFT;
  813. dev_vdbg(hsotg->dev, "remain=%d dwc2_urb=%p\n", remain, urb);
  814. if (halt_status == DWC2_HC_XFER_AHB_ERR) {
  815. dev_err(hsotg->dev, "EIO\n");
  816. urb->status = -EIO;
  817. return 1;
  818. }
  819. if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
  820. switch (halt_status) {
  821. case DWC2_HC_XFER_STALL:
  822. dev_vdbg(hsotg->dev, "Stall\n");
  823. urb->status = -EPIPE;
  824. break;
  825. case DWC2_HC_XFER_BABBLE_ERR:
  826. dev_err(hsotg->dev, "Babble\n");
  827. urb->status = -EOVERFLOW;
  828. break;
  829. case DWC2_HC_XFER_XACT_ERR:
  830. dev_err(hsotg->dev, "XactErr\n");
  831. urb->status = -EPROTO;
  832. break;
  833. default:
  834. dev_err(hsotg->dev,
  835. "%s: Unhandled descriptor error status (%d)\n",
  836. __func__, halt_status);
  837. break;
  838. }
  839. return 1;
  840. }
  841. if (dma_desc->status & HOST_DMA_A) {
  842. dev_vdbg(hsotg->dev,
  843. "Active descriptor encountered on channel %d\n",
  844. chan->hc_num);
  845. return 0;
  846. }
  847. if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL) {
  848. if (qtd->control_phase == DWC2_CONTROL_DATA) {
  849. urb->actual_length += n_bytes - remain;
  850. if (remain || urb->actual_length >= urb->length) {
  851. /*
  852. * For Control Data stage do not set urb->status
  853. * to 0, to prevent URB callback. Set it when
  854. * Status phase is done. See below.
  855. */
  856. *xfer_done = 1;
  857. }
  858. } else if (qtd->control_phase == DWC2_CONTROL_STATUS) {
  859. urb->status = 0;
  860. *xfer_done = 1;
  861. }
  862. /* No handling for SETUP stage */
  863. } else {
  864. /* BULK and INTR */
  865. urb->actual_length += n_bytes - remain;
  866. dev_vdbg(hsotg->dev, "length=%d actual=%d\n", urb->length,
  867. urb->actual_length);
  868. if (remain || urb->actual_length >= urb->length) {
  869. urb->status = 0;
  870. *xfer_done = 1;
  871. }
  872. }
  873. return 0;
  874. }
  875. static int dwc2_process_non_isoc_desc(struct dwc2_hsotg *hsotg,
  876. struct dwc2_host_chan *chan,
  877. int chnum, struct dwc2_qtd *qtd,
  878. int desc_num,
  879. enum dwc2_halt_status halt_status,
  880. int *xfer_done)
  881. {
  882. struct dwc2_qh *qh = chan->qh;
  883. struct dwc2_hcd_urb *urb = qtd->urb;
  884. struct dwc2_hcd_dma_desc *dma_desc;
  885. u32 n_bytes;
  886. int failed;
  887. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  888. if (!urb)
  889. return -EINVAL;
  890. dma_desc = &qh->desc_list[desc_num];
  891. n_bytes = qh->n_bytes[desc_num];
  892. dev_vdbg(hsotg->dev,
  893. "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
  894. qtd, urb, desc_num, dma_desc, n_bytes);
  895. failed = dwc2_update_non_isoc_urb_state_ddma(hsotg, chan, qtd, dma_desc,
  896. halt_status, n_bytes,
  897. xfer_done);
  898. if (failed || (*xfer_done && urb->status != -EINPROGRESS)) {
  899. dwc2_host_complete(hsotg, qtd, urb->status);
  900. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  901. dev_vdbg(hsotg->dev, "failed=%1x xfer_done=%1x status=%08x\n",
  902. failed, *xfer_done, urb->status);
  903. return failed;
  904. }
  905. if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL) {
  906. switch (qtd->control_phase) {
  907. case DWC2_CONTROL_SETUP:
  908. if (urb->length > 0)
  909. qtd->control_phase = DWC2_CONTROL_DATA;
  910. else
  911. qtd->control_phase = DWC2_CONTROL_STATUS;
  912. dev_vdbg(hsotg->dev,
  913. " Control setup transaction done\n");
  914. break;
  915. case DWC2_CONTROL_DATA:
  916. if (*xfer_done) {
  917. qtd->control_phase = DWC2_CONTROL_STATUS;
  918. dev_vdbg(hsotg->dev,
  919. " Control data transfer done\n");
  920. } else if (desc_num + 1 == qtd->n_desc) {
  921. /*
  922. * Last descriptor for Control data stage which
  923. * is not completed yet
  924. */
  925. dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
  926. qtd);
  927. }
  928. break;
  929. default:
  930. break;
  931. }
  932. }
  933. return 0;
  934. }
  935. static void dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
  936. struct dwc2_host_chan *chan,
  937. int chnum,
  938. enum dwc2_halt_status halt_status)
  939. {
  940. struct list_head *qtd_item, *qtd_tmp;
  941. struct dwc2_qh *qh = chan->qh;
  942. struct dwc2_qtd *qtd = NULL;
  943. int xfer_done;
  944. int desc_num = 0;
  945. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
  946. list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
  947. qtd->in_process = 0;
  948. return;
  949. }
  950. list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
  951. int i;
  952. qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
  953. xfer_done = 0;
  954. for (i = 0; i < qtd->n_desc; i++) {
  955. if (dwc2_process_non_isoc_desc(hsotg, chan, chnum, qtd,
  956. desc_num, halt_status,
  957. &xfer_done)) {
  958. qtd = NULL;
  959. break;
  960. }
  961. desc_num++;
  962. }
  963. }
  964. if (qh->ep_type != USB_ENDPOINT_XFER_CONTROL) {
  965. /*
  966. * Resetting the data toggle for bulk and interrupt endpoints
  967. * in case of stall. See handle_hc_stall_intr().
  968. */
  969. if (halt_status == DWC2_HC_XFER_STALL)
  970. qh->data_toggle = DWC2_HC_PID_DATA0;
  971. else if (qtd)
  972. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  973. }
  974. if (halt_status == DWC2_HC_XFER_COMPLETE) {
  975. if (chan->hcint & HCINTMSK_NYET) {
  976. /*
  977. * Got a NYET on the last transaction of the transfer.
  978. * It means that the endpoint should be in the PING
  979. * state at the beginning of the next transfer.
  980. */
  981. qh->ping_state = 1;
  982. }
  983. }
  984. }
  985. /**
  986. * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
  987. * status and calls completion routine for the URB if it's done. Called from
  988. * interrupt handlers.
  989. *
  990. * @hsotg: The HCD state structure for the DWC OTG controller
  991. * @chan: Host channel the transfer is completed on
  992. * @chnum: Index of Host channel registers
  993. * @halt_status: Reason the channel is being halted or just XferComplete
  994. * for isochronous transfers
  995. *
  996. * Releases the channel to be used by other transfers.
  997. * In case of Isochronous endpoint the channel is not halted until the end of
  998. * the session, i.e. QTD list is empty.
  999. * If periodic channel released the FrameList is updated accordingly.
  1000. * Calls transaction selection routines to activate pending transfers.
  1001. */
  1002. void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg *hsotg,
  1003. struct dwc2_host_chan *chan, int chnum,
  1004. enum dwc2_halt_status halt_status)
  1005. {
  1006. struct dwc2_qh *qh = chan->qh;
  1007. int continue_isoc_xfer = 0;
  1008. enum dwc2_transaction_type tr_type;
  1009. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1010. dwc2_complete_isoc_xfer_ddma(hsotg, chan, halt_status);
  1011. /* Release the channel if halted or session completed */
  1012. if (halt_status != DWC2_HC_XFER_COMPLETE ||
  1013. list_empty(&qh->qtd_list)) {
  1014. /* Halt the channel if session completed */
  1015. if (halt_status == DWC2_HC_XFER_COMPLETE)
  1016. dwc2_hc_halt(hsotg, chan, halt_status);
  1017. dwc2_release_channel_ddma(hsotg, qh);
  1018. dwc2_hcd_qh_unlink(hsotg, qh);
  1019. } else {
  1020. /* Keep in assigned schedule to continue transfer */
  1021. list_move(&qh->qh_list_entry,
  1022. &hsotg->periodic_sched_assigned);
  1023. continue_isoc_xfer = 1;
  1024. }
  1025. /*
  1026. * Todo: Consider the case when period exceeds FrameList size.
  1027. * Frame Rollover interrupt should be used.
  1028. */
  1029. } else {
  1030. /*
  1031. * Scan descriptor list to complete the URB(s), then release
  1032. * the channel
  1033. */
  1034. dwc2_complete_non_isoc_xfer_ddma(hsotg, chan, chnum,
  1035. halt_status);
  1036. dwc2_release_channel_ddma(hsotg, qh);
  1037. dwc2_hcd_qh_unlink(hsotg, qh);
  1038. if (!list_empty(&qh->qtd_list)) {
  1039. /*
  1040. * Add back to inactive non-periodic schedule on normal
  1041. * completion
  1042. */
  1043. dwc2_hcd_qh_add(hsotg, qh);
  1044. }
  1045. }
  1046. tr_type = dwc2_hcd_select_transactions(hsotg);
  1047. if (tr_type != DWC2_TRANSACTION_NONE || continue_isoc_xfer) {
  1048. if (continue_isoc_xfer) {
  1049. if (tr_type == DWC2_TRANSACTION_NONE)
  1050. tr_type = DWC2_TRANSACTION_PERIODIC;
  1051. else if (tr_type == DWC2_TRANSACTION_NON_PERIODIC)
  1052. tr_type = DWC2_TRANSACTION_ALL;
  1053. }
  1054. dwc2_hcd_queue_transactions(hsotg, tr_type);
  1055. }
  1056. }