adm5120-q.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /*
  2. * ADM5120 HCD (Host Controller Driver) for USB
  3. *
  4. * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This file was derived from: drivers/usb/host/ohci-q.c
  7. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  8. * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/irq.h>
  16. #include <linux/slab.h>
  17. /*-------------------------------------------------------------------------*/
  18. /*
  19. * URB goes back to driver, and isn't reissued.
  20. * It's completely gone from HC data structures.
  21. * PRECONDITION: ahcd lock held, irqs blocked.
  22. */
  23. static void
  24. finish_urb(struct admhcd *ahcd, struct urb *urb, int status)
  25. __releases(ahcd->lock)
  26. __acquires(ahcd->lock)
  27. {
  28. urb_priv_free(ahcd, urb->hcpriv);
  29. if (likely(status == -EINPROGRESS))
  30. status = 0;
  31. switch (usb_pipetype(urb->pipe)) {
  32. case PIPE_ISOCHRONOUS:
  33. admhcd_to_hcd(ahcd)->self.bandwidth_isoc_reqs--;
  34. break;
  35. case PIPE_INTERRUPT:
  36. admhcd_to_hcd(ahcd)->self.bandwidth_int_reqs--;
  37. break;
  38. }
  39. #ifdef ADMHC_VERBOSE_DEBUG
  40. urb_print(ahcd, urb, "RET", usb_pipeout(urb->pipe), status);
  41. #endif
  42. /* urb->complete() can reenter this HCD */
  43. usb_hcd_unlink_urb_from_ep(admhcd_to_hcd(ahcd), urb);
  44. spin_unlock(&ahcd->lock);
  45. usb_hcd_giveback_urb(admhcd_to_hcd(ahcd), urb, status);
  46. spin_lock(&ahcd->lock);
  47. }
  48. /*-------------------------------------------------------------------------*
  49. * ED handling functions
  50. *-------------------------------------------------------------------------*/
  51. #if 0 /* FIXME */
  52. /* search for the right schedule branch to use for a periodic ed.
  53. * does some load balancing; returns the branch, or negative errno.
  54. */
  55. static int balance(struct admhcd *ahcd, int interval, int load)
  56. {
  57. int i, branch = -ENOSPC;
  58. /* iso periods can be huge; iso tds specify frame numbers */
  59. if (interval > NUM_INTS)
  60. interval = NUM_INTS;
  61. /* search for the least loaded schedule branch of that period
  62. * that has enough bandwidth left unreserved.
  63. */
  64. for (i = 0; i < interval ; i++) {
  65. if (branch < 0 || ahcd->load[branch] > ahcd->load[i]) {
  66. int j;
  67. /* usb 1.1 says 90% of one frame */
  68. for (j = i; j < NUM_INTS; j += interval) {
  69. if ((ahcd->load[j] + load) > 900)
  70. break;
  71. }
  72. if (j < NUM_INTS)
  73. continue;
  74. branch = i;
  75. }
  76. }
  77. return branch;
  78. }
  79. #endif
  80. /*-------------------------------------------------------------------------*/
  81. #if 0 /* FIXME */
  82. /* both iso and interrupt requests have periods; this routine puts them
  83. * into the schedule tree in the apppropriate place. most iso devices use
  84. * 1msec periods, but that's not required.
  85. */
  86. static void periodic_link(struct admhcd *ahcd, struct ed *ed)
  87. {
  88. unsigned i;
  89. admhc_vdbg(ahcd, "link %sed %p branch %d [%dus.], interval %d\n",
  90. (ed->hwINFO & cpu_to_hc32(ahcd, ED_ISO)) ? "iso " : "",
  91. ed, ed->branch, ed->load, ed->interval);
  92. for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
  93. struct ed **prev = &ahcd->periodic[i];
  94. __hc32 *prev_p = &ahcd->hcca->int_table[i];
  95. struct ed *here = *prev;
  96. /* sorting each branch by period (slow before fast)
  97. * lets us share the faster parts of the tree.
  98. * (plus maybe: put interrupt eds before iso)
  99. */
  100. while (here && ed != here) {
  101. if (ed->interval > here->interval)
  102. break;
  103. prev = &here->ed_next;
  104. prev_p = &here->hwNextED;
  105. here = *prev;
  106. }
  107. if (ed != here) {
  108. ed->ed_next = here;
  109. if (here)
  110. ed->hwNextED = *prev_p;
  111. wmb();
  112. *prev = ed;
  113. *prev_p = cpu_to_hc32(ahcd, ed->dma);
  114. wmb();
  115. }
  116. ahcd->load[i] += ed->load;
  117. }
  118. admhcd_to_hcd(ahcd)->self.bandwidth_allocated += ed->load / ed->interval;
  119. }
  120. #endif
  121. /* link an ed into the HC chain */
  122. static int ed_schedule(struct admhcd *ahcd, struct ed *ed)
  123. {
  124. struct ed *old_tail;
  125. if (admhcd_to_hcd(ahcd)->state == HC_STATE_QUIESCING)
  126. return -EAGAIN;
  127. ed->state = ED_OPER;
  128. old_tail = ahcd->ed_tails[ed->type];
  129. ed->ed_next = old_tail->ed_next;
  130. if (ed->ed_next) {
  131. ed->ed_next->ed_prev = ed;
  132. ed->hwNextED = cpu_to_hc32(ahcd, ed->ed_next->dma);
  133. }
  134. ed->ed_prev = old_tail;
  135. old_tail->ed_next = ed;
  136. old_tail->hwNextED = cpu_to_hc32(ahcd, ed->dma);
  137. ahcd->ed_tails[ed->type] = ed;
  138. admhc_dma_enable(ahcd);
  139. return 0;
  140. }
  141. /*-------------------------------------------------------------------------*/
  142. #if 0 /* FIXME */
  143. /* scan the periodic table to find and unlink this ED */
  144. static void periodic_unlink(struct admhcd *ahcd, struct ed *ed)
  145. {
  146. int i;
  147. for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
  148. struct ed *temp;
  149. struct ed **prev = &ahcd->periodic[i];
  150. __hc32 *prev_p = &ahcd->hcca->int_table[i];
  151. while (*prev && (temp = *prev) != ed) {
  152. prev_p = &temp->hwNextED;
  153. prev = &temp->ed_next;
  154. }
  155. if (*prev) {
  156. *prev_p = ed->hwNextED;
  157. *prev = ed->ed_next;
  158. }
  159. ahcd->load[i] -= ed->load;
  160. }
  161. admhcd_to_hcd(ahcd)->self.bandwidth_allocated -= ed->load / ed->interval;
  162. admhc_vdbg(ahcd, "unlink %sed %p branch %d [%dus.], interval %d\n",
  163. (ed->hwINFO & cpu_to_hc32(ahcd, ED_ISO)) ? "iso " : "",
  164. ed, ed->branch, ed->load, ed->interval);
  165. }
  166. #endif
  167. /* unlink an ed from the HC chain.
  168. * just the link to the ed is unlinked.
  169. * the link from the ed still points to another operational ed or 0
  170. * so the HC can eventually finish the processing of the unlinked ed
  171. * (assuming it already started that, which needn't be true).
  172. *
  173. * ED_UNLINK is a transient state: the HC may still see this ED, but soon
  174. * it won't. ED_SKIP means the HC will finish its current transaction,
  175. * but won't start anything new. The TD queue may still grow; device
  176. * drivers don't know about this HCD-internal state.
  177. *
  178. * When the HC can't see the ED, something changes ED_UNLINK to one of:
  179. *
  180. * - ED_OPER: when there's any request queued, the ED gets rescheduled
  181. * immediately. HC should be working on them.
  182. *
  183. * - ED_IDLE: when there's no TD queue. there's no reason for the HC
  184. * to care about this ED; safe to disable the endpoint.
  185. *
  186. * When finish_unlinks() runs later, after SOF interrupt, it will often
  187. * complete one or more URB unlinks before making that state change.
  188. */
  189. static void ed_deschedule(struct admhcd *ahcd, struct ed *ed)
  190. {
  191. #ifdef ADMHC_VERBOSE_DEBUG
  192. admhc_dump_ed(ahcd, "ED-DESCHED", ed, 1);
  193. #endif
  194. ed->hwINFO |= cpu_to_hc32(ahcd, ED_SKIP);
  195. wmb();
  196. ed->state = ED_UNLINK;
  197. /* remove this ED from the HC list */
  198. ed->ed_prev->hwNextED = ed->hwNextED;
  199. /* and remove it from our list also */
  200. ed->ed_prev->ed_next = ed->ed_next;
  201. if (ed->ed_next)
  202. ed->ed_next->ed_prev = ed->ed_prev;
  203. if (ahcd->ed_tails[ed->type] == ed)
  204. ahcd->ed_tails[ed->type] = ed->ed_prev;
  205. }
  206. /*-------------------------------------------------------------------------*/
  207. static struct ed *ed_create(struct admhcd *ahcd, unsigned int type, u32 info)
  208. {
  209. struct ed *ed;
  210. struct td *td;
  211. ed = ed_alloc(ahcd, GFP_ATOMIC);
  212. if (!ed)
  213. goto err;
  214. /* dummy td; end of td list for this ed */
  215. td = td_alloc(ahcd, GFP_ATOMIC);
  216. if (!td)
  217. goto err_free_ed;
  218. switch (type) {
  219. case PIPE_INTERRUPT:
  220. info |= ED_INT;
  221. break;
  222. case PIPE_ISOCHRONOUS:
  223. info |= ED_ISO;
  224. break;
  225. }
  226. ed->dummy = td;
  227. ed->state = ED_IDLE;
  228. ed->type = type;
  229. ed->hwINFO = cpu_to_hc32(ahcd, info);
  230. ed->hwTailP = cpu_to_hc32(ahcd, td->td_dma);
  231. ed->hwHeadP = ed->hwTailP; /* ED_C, ED_H zeroed */
  232. return ed;
  233. err_free_ed:
  234. ed_free(ahcd, ed);
  235. err:
  236. return NULL;
  237. }
  238. /* get and maybe (re)init an endpoint. init _should_ be done only as part
  239. * of enumeration, usb_set_configuration() or usb_set_interface().
  240. */
  241. static struct ed *ed_get(struct admhcd *ahcd, struct usb_host_endpoint *ep,
  242. struct usb_device *udev, unsigned int pipe, int interval)
  243. {
  244. struct ed *ed;
  245. unsigned long flags;
  246. spin_lock_irqsave(&ahcd->lock, flags);
  247. ed = ep->hcpriv;
  248. if (!ed) {
  249. u32 info;
  250. /* FIXME: usbcore changes dev->devnum before SET_ADDRESS
  251. * succeeds ... otherwise we wouldn't need "pipe".
  252. */
  253. info = usb_pipedevice(pipe);
  254. info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << ED_EN_SHIFT;
  255. info |= le16_to_cpu(ep->desc.wMaxPacketSize) << ED_MPS_SHIFT;
  256. if (udev->speed == USB_SPEED_FULL)
  257. info |= ED_SPEED_FULL;
  258. ed = ed_create(ahcd, usb_pipetype(pipe), info);
  259. if (ed)
  260. ep->hcpriv = ed;
  261. }
  262. spin_unlock_irqrestore(&ahcd->lock, flags);
  263. return ed;
  264. }
  265. /*-------------------------------------------------------------------------*/
  266. /* request unlinking of an endpoint from an operational HC.
  267. * put the ep on the rm_list
  268. * real work is done at the next start frame (SOFI) hardware interrupt
  269. * caller guarantees HCD is running, so hardware access is safe,
  270. * and that ed->state is ED_OPER
  271. */
  272. static void start_ed_unlink(struct admhcd *ahcd, struct ed *ed)
  273. {
  274. #ifdef ADMHC_VERBOSE_DEBUG
  275. admhc_dump_ed(ahcd, "ED-UNLINK", ed, 1);
  276. #endif
  277. ed->hwINFO |= cpu_to_hc32(ahcd, ED_DEQUEUE);
  278. ed_deschedule(ahcd, ed);
  279. /* add this ED into the remove list */
  280. ed->ed_rm_next = ahcd->ed_rm_list;
  281. ahcd->ed_rm_list = ed;
  282. /* enable SOF interrupt */
  283. admhc_intr_ack(ahcd, ADMHC_INTR_SOFI);
  284. admhc_intr_enable(ahcd, ADMHC_INTR_SOFI);
  285. /* flush those writes */
  286. admhc_writel_flush(ahcd);
  287. /* SOF interrupt might get delayed; record the frame counter value that
  288. * indicates when the HC isn't looking at it, so concurrent unlinks
  289. * behave. frame_no wraps every 2^16 msec, and changes right before
  290. * SOF is triggered.
  291. */
  292. ed->tick = admhc_frame_no(ahcd) + 1;
  293. }
  294. /*-------------------------------------------------------------------------*
  295. * TD handling functions
  296. *-------------------------------------------------------------------------*/
  297. /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
  298. static void
  299. td_fill(struct admhcd *ahcd, u32 info, dma_addr_t data, int len,
  300. struct urb *urb, int index)
  301. {
  302. struct td *td, *td_pt;
  303. struct urb_priv *urb_priv = urb->hcpriv;
  304. int hash;
  305. u32 cbl = 0;
  306. #if 1
  307. if (index == (urb_priv->td_cnt - 1) &&
  308. ((urb->transfer_flags & URB_NO_INTERRUPT) == 0))
  309. cbl |= TD_IE;
  310. #else
  311. if (index == (urb_priv->td_cnt - 1))
  312. cbl |= TD_IE;
  313. #endif
  314. /* use this td as the next dummy */
  315. td_pt = urb_priv->td[index];
  316. /* fill the old dummy TD */
  317. td = urb_priv->td[index] = urb_priv->ed->dummy;
  318. urb_priv->ed->dummy = td_pt;
  319. td->ed = urb_priv->ed;
  320. td->next_dl_td = NULL;
  321. td->index = index;
  322. td->urb = urb;
  323. td->data_dma = data;
  324. if (!len)
  325. data = 0;
  326. if (data)
  327. cbl |= (len & TD_BL_MASK);
  328. info |= TD_OWN;
  329. /* setup hardware specific fields */
  330. td->hwINFO = cpu_to_hc32(ahcd, info);
  331. td->hwDBP = cpu_to_hc32(ahcd, data);
  332. td->hwCBL = cpu_to_hc32(ahcd, cbl);
  333. td->hwNextTD = cpu_to_hc32(ahcd, td_pt->td_dma);
  334. /* append to queue */
  335. list_add_tail(&td->td_list, &td->ed->td_list);
  336. /* hash it for later reverse mapping */
  337. hash = TD_HASH_FUNC(td->td_dma);
  338. td->td_hash = ahcd->td_hash[hash];
  339. ahcd->td_hash[hash] = td;
  340. /* HC might read the TD (or cachelines) right away ... */
  341. wmb();
  342. td->ed->hwTailP = td->hwNextTD;
  343. }
  344. /*-------------------------------------------------------------------------*/
  345. /* Prepare all TDs of a transfer, and queue them onto the ED.
  346. * Caller guarantees HC is active.
  347. * Usually the ED is already on the schedule, so TDs might be
  348. * processed as soon as they're queued.
  349. */
  350. static void td_submit_urb(struct admhcd *ahcd, struct urb *urb)
  351. {
  352. struct urb_priv *urb_priv = urb->hcpriv;
  353. dma_addr_t data;
  354. int data_len = urb->transfer_buffer_length;
  355. int cnt = 0;
  356. u32 info = 0;
  357. int is_out = usb_pipeout(urb->pipe);
  358. u32 toggle = 0;
  359. /* OHCI handles the bulk/interrupt data toggles itself. We just
  360. * use the device toggle bits for resetting, and rely on the fact
  361. * that resetting toggle is meaningless if the endpoint is active.
  362. */
  363. if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), is_out)) {
  364. toggle = TD_T_CARRY;
  365. } else {
  366. toggle = TD_T_DATA0;
  367. usb_settoggle(urb->dev, usb_pipeendpoint (urb->pipe),
  368. is_out, 1);
  369. }
  370. urb_priv->td_idx = 0;
  371. list_add(&urb_priv->pending, &ahcd->pending);
  372. if (data_len)
  373. data = urb->transfer_dma;
  374. else
  375. data = 0;
  376. /* NOTE: TD_CC is set so we can tell which TDs the HC processed by
  377. * using TD_CC_GET, as well as by seeing them on the done list.
  378. * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
  379. */
  380. switch (urb_priv->ed->type) {
  381. case PIPE_INTERRUPT:
  382. info = is_out
  383. ? TD_T_CARRY | TD_SCC_NOTACCESSED | TD_DP_OUT
  384. : TD_T_CARRY | TD_SCC_NOTACCESSED | TD_DP_IN;
  385. /* setup service interval and starting frame number */
  386. info |= (urb->start_frame & TD_FN_MASK);
  387. info |= (urb->interval & TD_ISI_MASK) << TD_ISI_SHIFT;
  388. td_fill(ahcd, info, data, data_len, urb, cnt);
  389. cnt++;
  390. admhcd_to_hcd(ahcd)->self.bandwidth_int_reqs++;
  391. break;
  392. case PIPE_BULK:
  393. info = is_out
  394. ? TD_SCC_NOTACCESSED | TD_DP_OUT
  395. : TD_SCC_NOTACCESSED | TD_DP_IN;
  396. /* TDs _could_ transfer up to 8K each */
  397. while (data_len > TD_DATALEN_MAX) {
  398. td_fill(ahcd, info | ((cnt) ? TD_T_CARRY : toggle),
  399. data, TD_DATALEN_MAX, urb, cnt);
  400. data += TD_DATALEN_MAX;
  401. data_len -= TD_DATALEN_MAX;
  402. cnt++;
  403. }
  404. td_fill(ahcd, info | ((cnt) ? TD_T_CARRY : toggle), data,
  405. data_len, urb, cnt);
  406. cnt++;
  407. if ((urb->transfer_flags & URB_ZERO_PACKET)
  408. && (cnt < urb_priv->td_cnt)) {
  409. td_fill(ahcd, info | ((cnt) ? TD_T_CARRY : toggle),
  410. 0, 0, urb, cnt);
  411. cnt++;
  412. }
  413. break;
  414. /* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
  415. * any DATA phase works normally, and the STATUS ack is special.
  416. */
  417. case PIPE_CONTROL:
  418. /* fill a TD for the setup */
  419. info = TD_SCC_NOTACCESSED | TD_DP_SETUP | TD_T_DATA0;
  420. td_fill(ahcd, info, urb->setup_dma, 8, urb, cnt++);
  421. if (data_len > 0) {
  422. /* fill a TD for the data */
  423. info = TD_SCC_NOTACCESSED | TD_T_DATA1;
  424. info |= is_out ? TD_DP_OUT : TD_DP_IN;
  425. /* NOTE: mishandles transfers >8K, some >4K */
  426. td_fill(ahcd, info, data, data_len, urb, cnt++);
  427. }
  428. /* fill a TD for the ACK */
  429. info = (is_out || data_len == 0)
  430. ? TD_SCC_NOTACCESSED | TD_DP_IN | TD_T_DATA1
  431. : TD_SCC_NOTACCESSED | TD_DP_OUT | TD_T_DATA1;
  432. td_fill(ahcd, info, data, 0, urb, cnt++);
  433. break;
  434. /* ISO has no retransmit, so no toggle;
  435. * Each TD could handle multiple consecutive frames (interval 1);
  436. * we could often reduce the number of TDs here.
  437. */
  438. case PIPE_ISOCHRONOUS:
  439. info = is_out
  440. ? TD_T_CARRY | TD_SCC_NOTACCESSED | TD_DP_OUT
  441. : TD_T_CARRY | TD_SCC_NOTACCESSED | TD_DP_IN;
  442. for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
  443. int frame = urb->start_frame;
  444. frame += cnt * urb->interval;
  445. frame &= TD_FN_MASK;
  446. td_fill(ahcd, info | frame,
  447. data + urb->iso_frame_desc[cnt].offset,
  448. urb->iso_frame_desc[cnt].length, urb, cnt);
  449. }
  450. admhcd_to_hcd(ahcd)->self.bandwidth_isoc_reqs++;
  451. break;
  452. }
  453. if (urb_priv->td_cnt != cnt)
  454. admhc_err(ahcd, "bad number of tds created for urb %p\n", urb);
  455. }
  456. /*-------------------------------------------------------------------------*
  457. * Done List handling functions
  458. *-------------------------------------------------------------------------*/
  459. /* calculate transfer length/status and update the urb */
  460. static int td_done(struct admhcd *ahcd, struct urb *urb, struct td *td)
  461. {
  462. struct urb_priv *urb_priv = urb->hcpriv;
  463. u32 info;
  464. u32 bl;
  465. u32 tdDBP;
  466. int type = usb_pipetype(urb->pipe);
  467. int cc;
  468. int status = -EINPROGRESS;
  469. info = hc32_to_cpup(ahcd, &td->hwINFO);
  470. tdDBP = hc32_to_cpup(ahcd, &td->hwDBP);
  471. bl = TD_BL_GET(hc32_to_cpup(ahcd, &td->hwCBL));
  472. cc = TD_CC_GET(info);
  473. /* ISO ... drivers see per-TD length/status */
  474. if (type == PIPE_ISOCHRONOUS) {
  475. /* TODO */
  476. int dlen = 0;
  477. /* NOTE: assumes FC in tdINFO == 0, and that
  478. * only the first of 0..MAXPSW psws is used.
  479. */
  480. if (info & TD_CC) /* hc didn't touch? */
  481. return status;
  482. if (usb_pipeout(urb->pipe))
  483. dlen = urb->iso_frame_desc[td->index].length;
  484. else {
  485. /* short reads are always OK for ISO */
  486. if (cc == TD_CC_DATAUNDERRUN)
  487. cc = TD_CC_NOERROR;
  488. dlen = tdDBP - td->data_dma + bl;
  489. }
  490. urb->actual_length += dlen;
  491. urb->iso_frame_desc[td->index].actual_length = dlen;
  492. urb->iso_frame_desc[td->index].status = cc_to_error[cc];
  493. if (cc != TD_CC_NOERROR)
  494. admhc_vdbg(ahcd,
  495. "urb %p iso td %p (%d) len %d cc %d\n",
  496. urb, td, 1 + td->index, dlen, cc);
  497. /* BULK, INT, CONTROL ... drivers see aggregate length/status,
  498. * except that "setup" bytes aren't counted and "short" transfers
  499. * might not be reported as errors.
  500. */
  501. } else {
  502. /* update packet status if needed (short is normally ok) */
  503. if (cc == TD_CC_DATAUNDERRUN
  504. && !(urb->transfer_flags & URB_SHORT_NOT_OK))
  505. cc = TD_CC_NOERROR;
  506. if (cc != TD_CC_NOERROR && cc < TD_CC_HCD0)
  507. status = cc_to_error[cc];
  508. /* count all non-empty packets except control SETUP packet */
  509. if ((type != PIPE_CONTROL || td->index != 0) && tdDBP != 0)
  510. urb->actual_length += tdDBP - td->data_dma + bl;
  511. if (cc != TD_CC_NOERROR && cc < TD_CC_HCD0)
  512. admhc_vdbg(ahcd,
  513. "urb %p td %p (%d) cc %d, len=%d/%d\n",
  514. urb, td, td->index, cc,
  515. urb->actual_length,
  516. urb->transfer_buffer_length);
  517. }
  518. list_del(&td->td_list);
  519. urb_priv->td_idx++;
  520. return status;
  521. }
  522. /*-------------------------------------------------------------------------*/
  523. static void ed_halted(struct admhcd *ahcd, struct td *td, int cc)
  524. {
  525. struct urb *urb = td->urb;
  526. struct urb_priv *urb_priv = urb->hcpriv;
  527. struct ed *ed = td->ed;
  528. struct list_head *tmp = td->td_list.next;
  529. __hc32 toggle = ed->hwHeadP & cpu_to_hc32(ahcd, ED_C);
  530. admhc_dump_ed(ahcd, "ed halted", td->ed, 1);
  531. /* clear ed halt; this is the td that caused it, but keep it inactive
  532. * until its urb->complete() has a chance to clean up.
  533. */
  534. ed->hwINFO |= cpu_to_hc32(ahcd, ED_SKIP);
  535. wmb();
  536. ed->hwHeadP &= ~cpu_to_hc32(ahcd, ED_H);
  537. /* Get rid of all later tds from this urb. We don't have
  538. * to be careful: no errors and nothing was transferred.
  539. * Also patch the ed so it looks as if those tds completed normally.
  540. */
  541. while (tmp != &ed->td_list) {
  542. struct td *next;
  543. next = list_entry(tmp, struct td, td_list);
  544. tmp = next->td_list.next;
  545. if (next->urb != urb)
  546. break;
  547. /* NOTE: if multi-td control DATA segments get supported,
  548. * this urb had one of them, this td wasn't the last td
  549. * in that segment (TD_R clear), this ed halted because
  550. * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
  551. * then we need to leave the control STATUS packet queued
  552. * and clear ED_SKIP.
  553. */
  554. list_del(&next->td_list);
  555. urb_priv->td_cnt++;
  556. ed->hwHeadP = next->hwNextTD | toggle;
  557. }
  558. /* help for troubleshooting: report anything that
  559. * looks odd ... that doesn't include protocol stalls
  560. * (or maybe some other things)
  561. */
  562. switch (cc) {
  563. case TD_CC_DATAUNDERRUN:
  564. if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)
  565. break;
  566. /* fallthrough */
  567. case TD_CC_STALL:
  568. if (usb_pipecontrol(urb->pipe))
  569. break;
  570. /* fallthrough */
  571. default:
  572. admhc_dbg(ahcd,
  573. "urb %p path %s ep%d%s %08x cc %d --> status %d\n",
  574. urb, urb->dev->devpath,
  575. usb_pipeendpoint (urb->pipe),
  576. usb_pipein(urb->pipe) ? "in" : "out",
  577. hc32_to_cpu(ahcd, td->hwINFO),
  578. cc, cc_to_error[cc]);
  579. }
  580. }
  581. /*-------------------------------------------------------------------------*/
  582. /* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
  583. static void
  584. finish_unlinks(struct admhcd *ahcd, u16 tick)
  585. {
  586. struct ed *ed, **last;
  587. rescan_all:
  588. for (last = &ahcd->ed_rm_list, ed = *last; ed != NULL; ed = *last) {
  589. struct list_head *entry, *tmp;
  590. int completed, modified;
  591. __hc32 *prev;
  592. /* only take off EDs that the HC isn't using, accounting for
  593. * frame counter wraps and EDs with partially retired TDs
  594. */
  595. if (likely(HC_IS_RUNNING(admhcd_to_hcd(ahcd)->state))) {
  596. if (tick_before(tick, ed->tick)) {
  597. skip_ed:
  598. last = &ed->ed_rm_next;
  599. continue;
  600. }
  601. #if 0
  602. if (!list_empty(&ed->td_list)) {
  603. struct td *td;
  604. u32 head;
  605. td = list_entry(ed->td_list.next, struct td,
  606. td_list);
  607. head = hc32_to_cpu(ahcd, ed->hwHeadP) &
  608. TD_MASK;
  609. /* INTR_WDH may need to clean up first */
  610. if (td->td_dma != head)
  611. goto skip_ed;
  612. }
  613. #endif
  614. }
  615. /* reentrancy: if we drop the schedule lock, someone might
  616. * have modified this list. normally it's just prepending
  617. * entries (which we'd ignore), but paranoia won't hurt.
  618. */
  619. *last = ed->ed_rm_next;
  620. ed->ed_rm_next = NULL;
  621. modified = 0;
  622. /* unlink urbs as requested, but rescan the list after
  623. * we call a completion since it might have unlinked
  624. * another (earlier) urb
  625. *
  626. * When we get here, the HC doesn't see this ed. But it
  627. * must not be rescheduled until all completed URBs have
  628. * been given back to the driver.
  629. */
  630. rescan_this:
  631. completed = 0;
  632. prev = &ed->hwHeadP;
  633. list_for_each_safe(entry, tmp, &ed->td_list) {
  634. struct td *td;
  635. struct urb *urb;
  636. struct urb_priv *urb_priv;
  637. __hc32 savebits;
  638. u32 tdINFO;
  639. int status;
  640. td = list_entry(entry, struct td, td_list);
  641. urb = td->urb;
  642. urb_priv = td->urb->hcpriv;
  643. if (!urb->unlinked) {
  644. prev = &td->hwNextTD;
  645. continue;
  646. }
  647. if ((urb_priv) == NULL)
  648. continue;
  649. /* patch pointer hc uses */
  650. savebits = *prev & ~cpu_to_hc32(ahcd, TD_MASK);
  651. *prev = td->hwNextTD | savebits;
  652. /* If this was unlinked, the TD may not have been
  653. * retired ... so manually save dhe data toggle.
  654. * The controller ignores the value we save for
  655. * control and ISO endpoints.
  656. */
  657. tdINFO = hc32_to_cpup(ahcd, &td->hwINFO);
  658. if ((tdINFO & TD_T) == TD_T_DATA0)
  659. ed->hwHeadP &= ~cpu_to_hc32(ahcd, ED_C);
  660. else if ((tdINFO & TD_T) == TD_T_DATA1)
  661. ed->hwHeadP |= cpu_to_hc32(ahcd, ED_C);
  662. /* HC may have partly processed this TD */
  663. #ifdef ADMHC_VERBOSE_DEBUG
  664. urb_print(ahcd, urb, "PARTIAL", 0);
  665. #endif
  666. status = td_done(ahcd, urb, td);
  667. /* if URB is done, clean up */
  668. if (urb_priv->td_idx == urb_priv->td_cnt) {
  669. modified = completed = 1;
  670. finish_urb(ahcd, urb, status);
  671. }
  672. }
  673. if (completed && !list_empty(&ed->td_list))
  674. goto rescan_this;
  675. /* ED's now officially unlinked, hc doesn't see */
  676. ed->state = ED_IDLE;
  677. ed->hwHeadP &= ~cpu_to_hc32(ahcd, ED_H);
  678. ed->hwNextED = 0;
  679. wmb();
  680. ed->hwINFO &= ~cpu_to_hc32(ahcd, ED_SKIP | ED_DEQUEUE);
  681. /* but if there's work queued, reschedule */
  682. if (!list_empty(&ed->td_list)) {
  683. if (HC_IS_RUNNING(admhcd_to_hcd(ahcd)->state))
  684. ed_schedule(ahcd, ed);
  685. }
  686. if (modified)
  687. goto rescan_all;
  688. }
  689. }
  690. /*-------------------------------------------------------------------------*/
  691. /*
  692. * Process normal completions (error or success) and clean the schedules.
  693. *
  694. * This is the main path for handing urbs back to drivers. The only other
  695. * normal path is finish_unlinks(), which unlinks URBs using ed_rm_list,
  696. * instead of scanning the (re-reversed) donelist as this does.
  697. */
  698. static void ed_unhalt(struct admhcd *ahcd, struct ed *ed, struct urb *urb)
  699. {
  700. struct list_head *entry, *tmp;
  701. __hc32 toggle = ed->hwHeadP & cpu_to_hc32(ahcd, ED_C);
  702. #ifdef ADMHC_VERBOSE_DEBUG
  703. admhc_dump_ed(ahcd, "UNHALT", ed, 0);
  704. #endif
  705. /* clear ed halt; this is the td that caused it, but keep it inactive
  706. * until its urb->complete() has a chance to clean up.
  707. */
  708. ed->hwINFO |= cpu_to_hc32(ahcd, ED_SKIP);
  709. wmb();
  710. ed->hwHeadP &= ~cpu_to_hc32(ahcd, ED_H);
  711. list_for_each_safe(entry, tmp, &ed->td_list) {
  712. struct td *td = list_entry(entry, struct td, td_list);
  713. __hc32 info;
  714. if (td->urb != urb)
  715. break;
  716. info = td->hwINFO;
  717. info &= ~cpu_to_hc32(ahcd, TD_CC | TD_OWN);
  718. td->hwINFO = info;
  719. ed->hwHeadP = td->hwNextTD | toggle;
  720. wmb();
  721. }
  722. }
  723. static void ed_intr_refill(struct admhcd *ahcd, struct ed *ed)
  724. {
  725. __hc32 toggle = ed->hwHeadP & cpu_to_hc32(ahcd, ED_C);
  726. ed->hwHeadP = ed->hwTailP | toggle;
  727. }
  728. static inline int is_ed_halted(struct admhcd *ahcd, struct ed *ed)
  729. {
  730. return ((hc32_to_cpup(ahcd, &ed->hwHeadP) & ED_H) == ED_H);
  731. }
  732. static inline int is_td_halted(struct admhcd *ahcd, struct ed *ed,
  733. struct td *td)
  734. {
  735. return ((hc32_to_cpup(ahcd, &ed->hwHeadP) & TD_MASK) ==
  736. (hc32_to_cpup(ahcd, &td->hwNextTD) & TD_MASK));
  737. }
  738. static void ed_update(struct admhcd *ahcd, struct ed *ed)
  739. {
  740. struct list_head *entry, *tmp;
  741. #ifdef ADMHC_VERBOSE_DEBUG
  742. admhc_dump_ed(ahcd, "UPDATE", ed, 1);
  743. #endif
  744. list_for_each_safe(entry, tmp, &ed->td_list) {
  745. struct td *td = list_entry(entry, struct td, td_list);
  746. struct urb *urb = td->urb;
  747. struct urb_priv *urb_priv = urb->hcpriv;
  748. int status;
  749. if (hc32_to_cpup(ahcd, &td->hwINFO) & TD_OWN)
  750. break;
  751. /* update URB's length and status from TD */
  752. status = td_done(ahcd, urb, td);
  753. if (is_ed_halted(ahcd, ed) && is_td_halted(ahcd, ed, td))
  754. ed_unhalt(ahcd, ed, urb);
  755. if (ed->type == PIPE_INTERRUPT)
  756. ed_intr_refill(ahcd, ed);
  757. /* If all this urb's TDs are done, call complete() */
  758. if (urb_priv->td_idx == urb_priv->td_cnt)
  759. finish_urb(ahcd, urb, status);
  760. /* clean schedule: unlink EDs that are no longer busy */
  761. if (list_empty(&ed->td_list)) {
  762. if (ed->state == ED_OPER)
  763. start_ed_unlink(ahcd, ed);
  764. /* ... reenabling halted EDs only after fault cleanup */
  765. } else if ((ed->hwINFO & cpu_to_hc32(ahcd,
  766. ED_SKIP | ED_DEQUEUE))
  767. == cpu_to_hc32(ahcd, ED_SKIP)) {
  768. td = list_entry(ed->td_list.next, struct td, td_list);
  769. #if 0
  770. if (!(td->hwINFO & cpu_to_hc32(ahcd, TD_DONE))) {
  771. ed->hwINFO &= ~cpu_to_hc32(ahcd, ED_SKIP);
  772. /* ... hc may need waking-up */
  773. switch (ed->type) {
  774. case PIPE_CONTROL:
  775. admhc_writel(ahcd, OHCI_CLF,
  776. &ahcd->regs->cmdstatus);
  777. break;
  778. case PIPE_BULK:
  779. admhc_writel(ahcd, OHCI_BLF,
  780. &ahcd->regs->cmdstatus);
  781. break;
  782. }
  783. }
  784. #else
  785. if ((td->hwINFO & cpu_to_hc32(ahcd, TD_OWN)))
  786. ed->hwINFO &= ~cpu_to_hc32(ahcd, ED_SKIP);
  787. #endif
  788. }
  789. }
  790. }
  791. /* there are some tds completed; called in_irq(), with HCD locked */
  792. static void admhc_td_complete(struct admhcd *ahcd)
  793. {
  794. struct ed *ed;
  795. for (ed = ahcd->ed_head; ed; ed = ed->ed_next) {
  796. if (ed->state != ED_OPER)
  797. continue;
  798. ed_update(ahcd, ed);
  799. }
  800. }