hcd_ddma.c 39 KB

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