uhci-q.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. /*
  2. * Universal Host Controller Interface driver for USB.
  3. *
  4. * Maintainer: Alan Stern <stern@rowland.harvard.edu>
  5. *
  6. * (C) Copyright 1999 Linus Torvalds
  7. * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
  8. * (C) Copyright 1999 Randy Dunlap
  9. * (C) Copyright 1999 Georg Acher, acher@in.tum.de
  10. * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
  11. * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
  12. * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
  13. * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
  14. * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
  15. * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
  16. * (C) Copyright 2004-2006 Alan Stern, stern@rowland.harvard.edu
  17. */
  18. /*
  19. * Technically, updating td->status here is a race, but it's not really a
  20. * problem. The worst that can happen is that we set the IOC bit again
  21. * generating a spurious interrupt. We could fix this by creating another
  22. * QH and leaving the IOC bit always set, but then we would have to play
  23. * games with the FSBR code to make sure we get the correct order in all
  24. * the cases. I don't think it's worth the effort
  25. */
  26. static void uhci_set_next_interrupt(struct uhci_hcd *uhci)
  27. {
  28. if (uhci->is_stopped)
  29. mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
  30. uhci->term_td->status |= cpu_to_le32(TD_CTRL_IOC);
  31. }
  32. static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci)
  33. {
  34. uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);
  35. }
  36. /*
  37. * Full-Speed Bandwidth Reclamation (FSBR).
  38. * We turn on FSBR whenever a queue that wants it is advancing,
  39. * and leave it on for a short time thereafter.
  40. */
  41. static void uhci_fsbr_on(struct uhci_hcd *uhci)
  42. {
  43. uhci->fsbr_is_on = 1;
  44. uhci->skel_term_qh->link = cpu_to_le32(
  45. uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
  46. }
  47. static void uhci_fsbr_off(struct uhci_hcd *uhci)
  48. {
  49. uhci->fsbr_is_on = 0;
  50. uhci->skel_term_qh->link = UHCI_PTR_TERM;
  51. }
  52. static void uhci_add_fsbr(struct uhci_hcd *uhci, struct urb *urb)
  53. {
  54. struct urb_priv *urbp = urb->hcpriv;
  55. if (!(urb->transfer_flags & URB_NO_FSBR))
  56. urbp->fsbr = 1;
  57. }
  58. static void uhci_urbp_wants_fsbr(struct uhci_hcd *uhci, struct urb_priv *urbp)
  59. {
  60. if (urbp->fsbr) {
  61. uhci->fsbr_is_wanted = 1;
  62. if (!uhci->fsbr_is_on)
  63. uhci_fsbr_on(uhci);
  64. else if (uhci->fsbr_expiring) {
  65. uhci->fsbr_expiring = 0;
  66. del_timer(&uhci->fsbr_timer);
  67. }
  68. }
  69. }
  70. static void uhci_fsbr_timeout(unsigned long _uhci)
  71. {
  72. struct uhci_hcd *uhci = (struct uhci_hcd *) _uhci;
  73. unsigned long flags;
  74. spin_lock_irqsave(&uhci->lock, flags);
  75. if (uhci->fsbr_expiring) {
  76. uhci->fsbr_expiring = 0;
  77. uhci_fsbr_off(uhci);
  78. }
  79. spin_unlock_irqrestore(&uhci->lock, flags);
  80. }
  81. static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
  82. {
  83. dma_addr_t dma_handle;
  84. struct uhci_td *td;
  85. td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle);
  86. if (!td)
  87. return NULL;
  88. td->dma_handle = dma_handle;
  89. td->frame = -1;
  90. INIT_LIST_HEAD(&td->list);
  91. INIT_LIST_HEAD(&td->fl_list);
  92. return td;
  93. }
  94. static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
  95. {
  96. if (!list_empty(&td->list))
  97. dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
  98. if (!list_empty(&td->fl_list))
  99. dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
  100. dma_pool_free(uhci->td_pool, td, td->dma_handle);
  101. }
  102. static inline void uhci_fill_td(struct uhci_td *td, u32 status,
  103. u32 token, u32 buffer)
  104. {
  105. td->status = cpu_to_le32(status);
  106. td->token = cpu_to_le32(token);
  107. td->buffer = cpu_to_le32(buffer);
  108. }
  109. static void uhci_add_td_to_urbp(struct uhci_td *td, struct urb_priv *urbp)
  110. {
  111. list_add_tail(&td->list, &urbp->td_list);
  112. }
  113. static void uhci_remove_td_from_urbp(struct uhci_td *td)
  114. {
  115. list_del_init(&td->list);
  116. }
  117. /*
  118. * We insert Isochronous URBs directly into the frame list at the beginning
  119. */
  120. static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
  121. struct uhci_td *td, unsigned framenum)
  122. {
  123. framenum &= (UHCI_NUMFRAMES - 1);
  124. td->frame = framenum;
  125. /* Is there a TD already mapped there? */
  126. if (uhci->frame_cpu[framenum]) {
  127. struct uhci_td *ftd, *ltd;
  128. ftd = uhci->frame_cpu[framenum];
  129. ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
  130. list_add_tail(&td->fl_list, &ftd->fl_list);
  131. td->link = ltd->link;
  132. wmb();
  133. ltd->link = cpu_to_le32(td->dma_handle);
  134. } else {
  135. td->link = uhci->frame[framenum];
  136. wmb();
  137. uhci->frame[framenum] = cpu_to_le32(td->dma_handle);
  138. uhci->frame_cpu[framenum] = td;
  139. }
  140. }
  141. static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
  142. struct uhci_td *td)
  143. {
  144. /* If it's not inserted, don't remove it */
  145. if (td->frame == -1) {
  146. WARN_ON(!list_empty(&td->fl_list));
  147. return;
  148. }
  149. if (uhci->frame_cpu[td->frame] == td) {
  150. if (list_empty(&td->fl_list)) {
  151. uhci->frame[td->frame] = td->link;
  152. uhci->frame_cpu[td->frame] = NULL;
  153. } else {
  154. struct uhci_td *ntd;
  155. ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list);
  156. uhci->frame[td->frame] = cpu_to_le32(ntd->dma_handle);
  157. uhci->frame_cpu[td->frame] = ntd;
  158. }
  159. } else {
  160. struct uhci_td *ptd;
  161. ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list);
  162. ptd->link = td->link;
  163. }
  164. list_del_init(&td->fl_list);
  165. td->frame = -1;
  166. }
  167. static inline void uhci_remove_tds_from_frame(struct uhci_hcd *uhci,
  168. unsigned int framenum)
  169. {
  170. struct uhci_td *ftd, *ltd;
  171. framenum &= (UHCI_NUMFRAMES - 1);
  172. ftd = uhci->frame_cpu[framenum];
  173. if (ftd) {
  174. ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
  175. uhci->frame[framenum] = ltd->link;
  176. uhci->frame_cpu[framenum] = NULL;
  177. while (!list_empty(&ftd->fl_list))
  178. list_del_init(ftd->fl_list.prev);
  179. }
  180. }
  181. /*
  182. * Remove all the TDs for an Isochronous URB from the frame list
  183. */
  184. static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
  185. {
  186. struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
  187. struct uhci_td *td;
  188. list_for_each_entry(td, &urbp->td_list, list)
  189. uhci_remove_td_from_frame_list(uhci, td);
  190. }
  191. static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
  192. struct usb_device *udev, struct usb_host_endpoint *hep)
  193. {
  194. dma_addr_t dma_handle;
  195. struct uhci_qh *qh;
  196. qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle);
  197. if (!qh)
  198. return NULL;
  199. memset(qh, 0, sizeof(*qh));
  200. qh->dma_handle = dma_handle;
  201. qh->element = UHCI_PTR_TERM;
  202. qh->link = UHCI_PTR_TERM;
  203. INIT_LIST_HEAD(&qh->queue);
  204. INIT_LIST_HEAD(&qh->node);
  205. if (udev) { /* Normal QH */
  206. qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  207. if (qh->type != USB_ENDPOINT_XFER_ISOC) {
  208. qh->dummy_td = uhci_alloc_td(uhci);
  209. if (!qh->dummy_td) {
  210. dma_pool_free(uhci->qh_pool, qh, dma_handle);
  211. return NULL;
  212. }
  213. }
  214. qh->state = QH_STATE_IDLE;
  215. qh->hep = hep;
  216. qh->udev = udev;
  217. hep->hcpriv = qh;
  218. if (qh->type == USB_ENDPOINT_XFER_INT ||
  219. qh->type == USB_ENDPOINT_XFER_ISOC)
  220. qh->load = usb_calc_bus_time(udev->speed,
  221. usb_endpoint_dir_in(&hep->desc),
  222. qh->type == USB_ENDPOINT_XFER_ISOC,
  223. le16_to_cpu(hep->desc.wMaxPacketSize))
  224. / 1000 + 1;
  225. } else { /* Skeleton QH */
  226. qh->state = QH_STATE_ACTIVE;
  227. qh->type = -1;
  228. }
  229. return qh;
  230. }
  231. static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
  232. {
  233. WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
  234. if (!list_empty(&qh->queue))
  235. dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
  236. list_del(&qh->node);
  237. if (qh->udev) {
  238. qh->hep->hcpriv = NULL;
  239. if (qh->dummy_td)
  240. uhci_free_td(uhci, qh->dummy_td);
  241. }
  242. dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
  243. }
  244. /*
  245. * When a queue is stopped and a dequeued URB is given back, adjust
  246. * the previous TD link (if the URB isn't first on the queue) or
  247. * save its toggle value (if it is first and is currently executing).
  248. *
  249. * Returns 0 if the URB should not yet be given back, 1 otherwise.
  250. */
  251. static int uhci_cleanup_queue(struct uhci_hcd *uhci, struct uhci_qh *qh,
  252. struct urb *urb)
  253. {
  254. struct urb_priv *urbp = urb->hcpriv;
  255. struct uhci_td *td;
  256. int ret = 1;
  257. /* Isochronous pipes don't use toggles and their TD link pointers
  258. * get adjusted during uhci_urb_dequeue(). But since their queues
  259. * cannot truly be stopped, we have to watch out for dequeues
  260. * occurring after the nominal unlink frame. */
  261. if (qh->type == USB_ENDPOINT_XFER_ISOC) {
  262. ret = (uhci->frame_number + uhci->is_stopped !=
  263. qh->unlink_frame);
  264. goto done;
  265. }
  266. /* If the URB isn't first on its queue, adjust the link pointer
  267. * of the last TD in the previous URB. The toggle doesn't need
  268. * to be saved since this URB can't be executing yet. */
  269. if (qh->queue.next != &urbp->node) {
  270. struct urb_priv *purbp;
  271. struct uhci_td *ptd;
  272. purbp = list_entry(urbp->node.prev, struct urb_priv, node);
  273. WARN_ON(list_empty(&purbp->td_list));
  274. ptd = list_entry(purbp->td_list.prev, struct uhci_td,
  275. list);
  276. td = list_entry(urbp->td_list.prev, struct uhci_td,
  277. list);
  278. ptd->link = td->link;
  279. goto done;
  280. }
  281. /* If the QH element pointer is UHCI_PTR_TERM then then currently
  282. * executing URB has already been unlinked, so this one isn't it. */
  283. if (qh_element(qh) == UHCI_PTR_TERM)
  284. goto done;
  285. qh->element = UHCI_PTR_TERM;
  286. /* Control pipes don't have to worry about toggles */
  287. if (qh->type == USB_ENDPOINT_XFER_CONTROL)
  288. goto done;
  289. /* Save the next toggle value */
  290. WARN_ON(list_empty(&urbp->td_list));
  291. td = list_entry(urbp->td_list.next, struct uhci_td, list);
  292. qh->needs_fixup = 1;
  293. qh->initial_toggle = uhci_toggle(td_token(td));
  294. done:
  295. return ret;
  296. }
  297. /*
  298. * Fix up the data toggles for URBs in a queue, when one of them
  299. * terminates early (short transfer, error, or dequeued).
  300. */
  301. static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
  302. {
  303. struct urb_priv *urbp = NULL;
  304. struct uhci_td *td;
  305. unsigned int toggle = qh->initial_toggle;
  306. unsigned int pipe;
  307. /* Fixups for a short transfer start with the second URB in the
  308. * queue (the short URB is the first). */
  309. if (skip_first)
  310. urbp = list_entry(qh->queue.next, struct urb_priv, node);
  311. /* When starting with the first URB, if the QH element pointer is
  312. * still valid then we know the URB's toggles are okay. */
  313. else if (qh_element(qh) != UHCI_PTR_TERM)
  314. toggle = 2;
  315. /* Fix up the toggle for the URBs in the queue. Normally this
  316. * loop won't run more than once: When an error or short transfer
  317. * occurs, the queue usually gets emptied. */
  318. urbp = list_prepare_entry(urbp, &qh->queue, node);
  319. list_for_each_entry_continue(urbp, &qh->queue, node) {
  320. /* If the first TD has the right toggle value, we don't
  321. * need to change any toggles in this URB */
  322. td = list_entry(urbp->td_list.next, struct uhci_td, list);
  323. if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
  324. td = list_entry(urbp->td_list.prev, struct uhci_td,
  325. list);
  326. toggle = uhci_toggle(td_token(td)) ^ 1;
  327. /* Otherwise all the toggles in the URB have to be switched */
  328. } else {
  329. list_for_each_entry(td, &urbp->td_list, list) {
  330. td->token ^= __constant_cpu_to_le32(
  331. TD_TOKEN_TOGGLE);
  332. toggle ^= 1;
  333. }
  334. }
  335. }
  336. wmb();
  337. pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
  338. usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
  339. usb_pipeout(pipe), toggle);
  340. qh->needs_fixup = 0;
  341. }
  342. /*
  343. * Put a QH on the schedule in both hardware and software
  344. */
  345. static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
  346. {
  347. struct uhci_qh *pqh;
  348. WARN_ON(list_empty(&qh->queue));
  349. /* Set the element pointer if it isn't set already.
  350. * This isn't needed for Isochronous queues, but it doesn't hurt. */
  351. if (qh_element(qh) == UHCI_PTR_TERM) {
  352. struct urb_priv *urbp = list_entry(qh->queue.next,
  353. struct urb_priv, node);
  354. struct uhci_td *td = list_entry(urbp->td_list.next,
  355. struct uhci_td, list);
  356. qh->element = cpu_to_le32(td->dma_handle);
  357. }
  358. /* Treat the queue as if it has just advanced */
  359. qh->wait_expired = 0;
  360. qh->advance_jiffies = jiffies;
  361. if (qh->state == QH_STATE_ACTIVE)
  362. return;
  363. qh->state = QH_STATE_ACTIVE;
  364. /* Move the QH from its old list to the end of the appropriate
  365. * skeleton's list */
  366. if (qh == uhci->next_qh)
  367. uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
  368. node);
  369. list_move_tail(&qh->node, &qh->skel->node);
  370. /* Link it into the schedule */
  371. pqh = list_entry(qh->node.prev, struct uhci_qh, node);
  372. qh->link = pqh->link;
  373. wmb();
  374. pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
  375. }
  376. /*
  377. * Take a QH off the hardware schedule
  378. */
  379. static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
  380. {
  381. struct uhci_qh *pqh;
  382. if (qh->state == QH_STATE_UNLINKING)
  383. return;
  384. WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
  385. qh->state = QH_STATE_UNLINKING;
  386. /* Unlink the QH from the schedule and record when we did it */
  387. pqh = list_entry(qh->node.prev, struct uhci_qh, node);
  388. pqh->link = qh->link;
  389. mb();
  390. uhci_get_current_frame_number(uhci);
  391. qh->unlink_frame = uhci->frame_number;
  392. /* Force an interrupt so we know when the QH is fully unlinked */
  393. if (list_empty(&uhci->skel_unlink_qh->node))
  394. uhci_set_next_interrupt(uhci);
  395. /* Move the QH from its old list to the end of the unlinking list */
  396. if (qh == uhci->next_qh)
  397. uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
  398. node);
  399. list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
  400. }
  401. /*
  402. * When we and the controller are through with a QH, it becomes IDLE.
  403. * This happens when a QH has been off the schedule (on the unlinking
  404. * list) for more than one frame, or when an error occurs while adding
  405. * the first URB onto a new QH.
  406. */
  407. static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
  408. {
  409. WARN_ON(qh->state == QH_STATE_ACTIVE);
  410. if (qh == uhci->next_qh)
  411. uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
  412. node);
  413. list_move(&qh->node, &uhci->idle_qh_list);
  414. qh->state = QH_STATE_IDLE;
  415. /* Now that the QH is idle, its post_td isn't being used */
  416. if (qh->post_td) {
  417. uhci_free_td(uhci, qh->post_td);
  418. qh->post_td = NULL;
  419. }
  420. /* If anyone is waiting for a QH to become idle, wake them up */
  421. if (uhci->num_waiting)
  422. wake_up_all(&uhci->waitqh);
  423. }
  424. /*
  425. * Find the highest existing bandwidth load for a given phase and period.
  426. */
  427. static int uhci_highest_load(struct uhci_hcd *uhci, int phase, int period)
  428. {
  429. int highest_load = uhci->load[phase];
  430. for (phase += period; phase < MAX_PHASE; phase += period)
  431. highest_load = max_t(int, highest_load, uhci->load[phase]);
  432. return highest_load;
  433. }
  434. /*
  435. * Set qh->phase to the optimal phase for a periodic transfer and
  436. * check whether the bandwidth requirement is acceptable.
  437. */
  438. static int uhci_check_bandwidth(struct uhci_hcd *uhci, struct uhci_qh *qh)
  439. {
  440. int minimax_load;
  441. /* Find the optimal phase (unless it is already set) and get
  442. * its load value. */
  443. if (qh->phase >= 0)
  444. minimax_load = uhci_highest_load(uhci, qh->phase, qh->period);
  445. else {
  446. int phase, load;
  447. int max_phase = min_t(int, MAX_PHASE, qh->period);
  448. qh->phase = 0;
  449. minimax_load = uhci_highest_load(uhci, qh->phase, qh->period);
  450. for (phase = 1; phase < max_phase; ++phase) {
  451. load = uhci_highest_load(uhci, phase, qh->period);
  452. if (load < minimax_load) {
  453. minimax_load = load;
  454. qh->phase = phase;
  455. }
  456. }
  457. }
  458. /* Maximum allowable periodic bandwidth is 90%, or 900 us per frame */
  459. if (minimax_load + qh->load > 900) {
  460. dev_dbg(uhci_dev(uhci), "bandwidth allocation failed: "
  461. "period %d, phase %d, %d + %d us\n",
  462. qh->period, qh->phase, minimax_load, qh->load);
  463. return -ENOSPC;
  464. }
  465. return 0;
  466. }
  467. /*
  468. * Reserve a periodic QH's bandwidth in the schedule
  469. */
  470. static void uhci_reserve_bandwidth(struct uhci_hcd *uhci, struct uhci_qh *qh)
  471. {
  472. int i;
  473. int load = qh->load;
  474. char *p = "??";
  475. for (i = qh->phase; i < MAX_PHASE; i += qh->period) {
  476. uhci->load[i] += load;
  477. uhci->total_load += load;
  478. }
  479. uhci_to_hcd(uhci)->self.bandwidth_allocated =
  480. uhci->total_load / MAX_PHASE;
  481. switch (qh->type) {
  482. case USB_ENDPOINT_XFER_INT:
  483. ++uhci_to_hcd(uhci)->self.bandwidth_int_reqs;
  484. p = "INT";
  485. break;
  486. case USB_ENDPOINT_XFER_ISOC:
  487. ++uhci_to_hcd(uhci)->self.bandwidth_isoc_reqs;
  488. p = "ISO";
  489. break;
  490. }
  491. qh->bandwidth_reserved = 1;
  492. dev_dbg(uhci_dev(uhci),
  493. "%s dev %d ep%02x-%s, period %d, phase %d, %d us\n",
  494. "reserve", qh->udev->devnum,
  495. qh->hep->desc.bEndpointAddress, p,
  496. qh->period, qh->phase, load);
  497. }
  498. /*
  499. * Release a periodic QH's bandwidth reservation
  500. */
  501. static void uhci_release_bandwidth(struct uhci_hcd *uhci, struct uhci_qh *qh)
  502. {
  503. int i;
  504. int load = qh->load;
  505. char *p = "??";
  506. for (i = qh->phase; i < MAX_PHASE; i += qh->period) {
  507. uhci->load[i] -= load;
  508. uhci->total_load -= load;
  509. }
  510. uhci_to_hcd(uhci)->self.bandwidth_allocated =
  511. uhci->total_load / MAX_PHASE;
  512. switch (qh->type) {
  513. case USB_ENDPOINT_XFER_INT:
  514. --uhci_to_hcd(uhci)->self.bandwidth_int_reqs;
  515. p = "INT";
  516. break;
  517. case USB_ENDPOINT_XFER_ISOC:
  518. --uhci_to_hcd(uhci)->self.bandwidth_isoc_reqs;
  519. p = "ISO";
  520. break;
  521. }
  522. qh->bandwidth_reserved = 0;
  523. dev_dbg(uhci_dev(uhci),
  524. "%s dev %d ep%02x-%s, period %d, phase %d, %d us\n",
  525. "release", qh->udev->devnum,
  526. qh->hep->desc.bEndpointAddress, p,
  527. qh->period, qh->phase, load);
  528. }
  529. static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
  530. struct urb *urb)
  531. {
  532. struct urb_priv *urbp;
  533. urbp = kmem_cache_zalloc(uhci_up_cachep, GFP_ATOMIC);
  534. if (!urbp)
  535. return NULL;
  536. urbp->urb = urb;
  537. urb->hcpriv = urbp;
  538. INIT_LIST_HEAD(&urbp->node);
  539. INIT_LIST_HEAD(&urbp->td_list);
  540. return urbp;
  541. }
  542. static void uhci_free_urb_priv(struct uhci_hcd *uhci,
  543. struct urb_priv *urbp)
  544. {
  545. struct uhci_td *td, *tmp;
  546. if (!list_empty(&urbp->node))
  547. dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
  548. urbp->urb);
  549. list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
  550. uhci_remove_td_from_urbp(td);
  551. uhci_free_td(uhci, td);
  552. }
  553. urbp->urb->hcpriv = NULL;
  554. kmem_cache_free(uhci_up_cachep, urbp);
  555. }
  556. /*
  557. * Map status to standard result codes
  558. *
  559. * <status> is (td_status(td) & 0xF60000), a.k.a.
  560. * uhci_status_bits(td_status(td)).
  561. * Note: <status> does not include the TD_CTRL_NAK bit.
  562. * <dir_out> is True for output TDs and False for input TDs.
  563. */
  564. static int uhci_map_status(int status, int dir_out)
  565. {
  566. if (!status)
  567. return 0;
  568. if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
  569. return -EPROTO;
  570. if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
  571. if (dir_out)
  572. return -EPROTO;
  573. else
  574. return -EILSEQ;
  575. }
  576. if (status & TD_CTRL_BABBLE) /* Babble */
  577. return -EOVERFLOW;
  578. if (status & TD_CTRL_DBUFERR) /* Buffer error */
  579. return -ENOSR;
  580. if (status & TD_CTRL_STALLED) /* Stalled */
  581. return -EPIPE;
  582. return 0;
  583. }
  584. /*
  585. * Control transfers
  586. */
  587. static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
  588. struct uhci_qh *qh)
  589. {
  590. struct uhci_td *td;
  591. unsigned long destination, status;
  592. int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
  593. int len = urb->transfer_buffer_length;
  594. dma_addr_t data = urb->transfer_dma;
  595. __le32 *plink;
  596. struct urb_priv *urbp = urb->hcpriv;
  597. /* The "pipe" thing contains the destination in bits 8--18 */
  598. destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
  599. /* 3 errors, dummy TD remains inactive */
  600. status = uhci_maxerr(3);
  601. if (urb->dev->speed == USB_SPEED_LOW)
  602. status |= TD_CTRL_LS;
  603. /*
  604. * Build the TD for the control request setup packet
  605. */
  606. td = qh->dummy_td;
  607. uhci_add_td_to_urbp(td, urbp);
  608. uhci_fill_td(td, status, destination | uhci_explen(8),
  609. urb->setup_dma);
  610. plink = &td->link;
  611. status |= TD_CTRL_ACTIVE;
  612. /*
  613. * If direction is "send", change the packet ID from SETUP (0x2D)
  614. * to OUT (0xE1). Else change it from SETUP to IN (0x69) and
  615. * set Short Packet Detect (SPD) for all data packets.
  616. */
  617. if (usb_pipeout(urb->pipe))
  618. destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
  619. else {
  620. destination ^= (USB_PID_SETUP ^ USB_PID_IN);
  621. status |= TD_CTRL_SPD;
  622. }
  623. /*
  624. * Build the DATA TDs
  625. */
  626. while (len > 0) {
  627. int pktsze = min(len, maxsze);
  628. td = uhci_alloc_td(uhci);
  629. if (!td)
  630. goto nomem;
  631. *plink = cpu_to_le32(td->dma_handle);
  632. /* Alternate Data0/1 (start with Data1) */
  633. destination ^= TD_TOKEN_TOGGLE;
  634. uhci_add_td_to_urbp(td, urbp);
  635. uhci_fill_td(td, status, destination | uhci_explen(pktsze),
  636. data);
  637. plink = &td->link;
  638. data += pktsze;
  639. len -= pktsze;
  640. }
  641. /*
  642. * Build the final TD for control status
  643. */
  644. td = uhci_alloc_td(uhci);
  645. if (!td)
  646. goto nomem;
  647. *plink = cpu_to_le32(td->dma_handle);
  648. /*
  649. * It's IN if the pipe is an output pipe or we're not expecting
  650. * data back.
  651. */
  652. destination &= ~TD_TOKEN_PID_MASK;
  653. if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
  654. destination |= USB_PID_IN;
  655. else
  656. destination |= USB_PID_OUT;
  657. destination |= TD_TOKEN_TOGGLE; /* End in Data1 */
  658. status &= ~TD_CTRL_SPD;
  659. uhci_add_td_to_urbp(td, urbp);
  660. uhci_fill_td(td, status | TD_CTRL_IOC,
  661. destination | uhci_explen(0), 0);
  662. plink = &td->link;
  663. /*
  664. * Build the new dummy TD and activate the old one
  665. */
  666. td = uhci_alloc_td(uhci);
  667. if (!td)
  668. goto nomem;
  669. *plink = cpu_to_le32(td->dma_handle);
  670. uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
  671. wmb();
  672. qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
  673. qh->dummy_td = td;
  674. /* Low-speed transfers get a different queue, and won't hog the bus.
  675. * Also, some devices enumerate better without FSBR; the easiest way
  676. * to do that is to put URBs on the low-speed queue while the device
  677. * isn't in the CONFIGURED state. */
  678. if (urb->dev->speed == USB_SPEED_LOW ||
  679. urb->dev->state != USB_STATE_CONFIGURED)
  680. qh->skel = uhci->skel_ls_control_qh;
  681. else {
  682. qh->skel = uhci->skel_fs_control_qh;
  683. uhci_add_fsbr(uhci, urb);
  684. }
  685. urb->actual_length = -8; /* Account for the SETUP packet */
  686. return 0;
  687. nomem:
  688. /* Remove the dummy TD from the td_list so it doesn't get freed */
  689. uhci_remove_td_from_urbp(qh->dummy_td);
  690. return -ENOMEM;
  691. }
  692. /*
  693. * Common submit for bulk and interrupt
  694. */
  695. static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
  696. struct uhci_qh *qh)
  697. {
  698. struct uhci_td *td;
  699. unsigned long destination, status;
  700. int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
  701. int len = urb->transfer_buffer_length;
  702. dma_addr_t data = urb->transfer_dma;
  703. __le32 *plink;
  704. struct urb_priv *urbp = urb->hcpriv;
  705. unsigned int toggle;
  706. if (len < 0)
  707. return -EINVAL;
  708. /* The "pipe" thing contains the destination in bits 8--18 */
  709. destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
  710. toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  711. usb_pipeout(urb->pipe));
  712. /* 3 errors, dummy TD remains inactive */
  713. status = uhci_maxerr(3);
  714. if (urb->dev->speed == USB_SPEED_LOW)
  715. status |= TD_CTRL_LS;
  716. if (usb_pipein(urb->pipe))
  717. status |= TD_CTRL_SPD;
  718. /*
  719. * Build the DATA TDs
  720. */
  721. plink = NULL;
  722. td = qh->dummy_td;
  723. do { /* Allow zero length packets */
  724. int pktsze = maxsze;
  725. if (len <= pktsze) { /* The last packet */
  726. pktsze = len;
  727. if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
  728. status &= ~TD_CTRL_SPD;
  729. }
  730. if (plink) {
  731. td = uhci_alloc_td(uhci);
  732. if (!td)
  733. goto nomem;
  734. *plink = cpu_to_le32(td->dma_handle);
  735. }
  736. uhci_add_td_to_urbp(td, urbp);
  737. uhci_fill_td(td, status,
  738. destination | uhci_explen(pktsze) |
  739. (toggle << TD_TOKEN_TOGGLE_SHIFT),
  740. data);
  741. plink = &td->link;
  742. status |= TD_CTRL_ACTIVE;
  743. data += pktsze;
  744. len -= maxsze;
  745. toggle ^= 1;
  746. } while (len > 0);
  747. /*
  748. * URB_ZERO_PACKET means adding a 0-length packet, if direction
  749. * is OUT and the transfer_length was an exact multiple of maxsze,
  750. * hence (len = transfer_length - N * maxsze) == 0
  751. * however, if transfer_length == 0, the zero packet was already
  752. * prepared above.
  753. */
  754. if ((urb->transfer_flags & URB_ZERO_PACKET) &&
  755. usb_pipeout(urb->pipe) && len == 0 &&
  756. urb->transfer_buffer_length > 0) {
  757. td = uhci_alloc_td(uhci);
  758. if (!td)
  759. goto nomem;
  760. *plink = cpu_to_le32(td->dma_handle);
  761. uhci_add_td_to_urbp(td, urbp);
  762. uhci_fill_td(td, status,
  763. destination | uhci_explen(0) |
  764. (toggle << TD_TOKEN_TOGGLE_SHIFT),
  765. data);
  766. plink = &td->link;
  767. toggle ^= 1;
  768. }
  769. /* Set the interrupt-on-completion flag on the last packet.
  770. * A more-or-less typical 4 KB URB (= size of one memory page)
  771. * will require about 3 ms to transfer; that's a little on the
  772. * fast side but not enough to justify delaying an interrupt
  773. * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
  774. * flag setting. */
  775. td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
  776. /*
  777. * Build the new dummy TD and activate the old one
  778. */
  779. td = uhci_alloc_td(uhci);
  780. if (!td)
  781. goto nomem;
  782. *plink = cpu_to_le32(td->dma_handle);
  783. uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
  784. wmb();
  785. qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
  786. qh->dummy_td = td;
  787. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  788. usb_pipeout(urb->pipe), toggle);
  789. return 0;
  790. nomem:
  791. /* Remove the dummy TD from the td_list so it doesn't get freed */
  792. uhci_remove_td_from_urbp(qh->dummy_td);
  793. return -ENOMEM;
  794. }
  795. static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
  796. struct uhci_qh *qh)
  797. {
  798. int ret;
  799. /* Can't have low-speed bulk transfers */
  800. if (urb->dev->speed == USB_SPEED_LOW)
  801. return -EINVAL;
  802. qh->skel = uhci->skel_bulk_qh;
  803. ret = uhci_submit_common(uhci, urb, qh);
  804. if (ret == 0)
  805. uhci_add_fsbr(uhci, urb);
  806. return ret;
  807. }
  808. static int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
  809. struct uhci_qh *qh)
  810. {
  811. int ret;
  812. /* USB 1.1 interrupt transfers only involve one packet per interval.
  813. * Drivers can submit URBs of any length, but longer ones will need
  814. * multiple intervals to complete.
  815. */
  816. if (!qh->bandwidth_reserved) {
  817. int exponent;
  818. /* Figure out which power-of-two queue to use */
  819. for (exponent = 7; exponent >= 0; --exponent) {
  820. if ((1 << exponent) <= urb->interval)
  821. break;
  822. }
  823. if (exponent < 0)
  824. return -EINVAL;
  825. qh->period = 1 << exponent;
  826. qh->skel = uhci->skelqh[UHCI_SKEL_INDEX(exponent)];
  827. /* For now, interrupt phase is fixed by the layout
  828. * of the QH lists. */
  829. qh->phase = (qh->period / 2) & (MAX_PHASE - 1);
  830. ret = uhci_check_bandwidth(uhci, qh);
  831. if (ret)
  832. return ret;
  833. } else if (qh->period > urb->interval)
  834. return -EINVAL; /* Can't decrease the period */
  835. ret = uhci_submit_common(uhci, urb, qh);
  836. if (ret == 0) {
  837. urb->interval = qh->period;
  838. if (!qh->bandwidth_reserved)
  839. uhci_reserve_bandwidth(uhci, qh);
  840. }
  841. return ret;
  842. }
  843. /*
  844. * Fix up the data structures following a short transfer
  845. */
  846. static int uhci_fixup_short_transfer(struct uhci_hcd *uhci,
  847. struct uhci_qh *qh, struct urb_priv *urbp)
  848. {
  849. struct uhci_td *td;
  850. struct list_head *tmp;
  851. int ret;
  852. td = list_entry(urbp->td_list.prev, struct uhci_td, list);
  853. if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
  854. /* When a control transfer is short, we have to restart
  855. * the queue at the status stage transaction, which is
  856. * the last TD. */
  857. WARN_ON(list_empty(&urbp->td_list));
  858. qh->element = cpu_to_le32(td->dma_handle);
  859. tmp = td->list.prev;
  860. ret = -EINPROGRESS;
  861. } else {
  862. /* When a bulk/interrupt transfer is short, we have to
  863. * fix up the toggles of the following URBs on the queue
  864. * before restarting the queue at the next URB. */
  865. qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1;
  866. uhci_fixup_toggles(qh, 1);
  867. if (list_empty(&urbp->td_list))
  868. td = qh->post_td;
  869. qh->element = td->link;
  870. tmp = urbp->td_list.prev;
  871. ret = 0;
  872. }
  873. /* Remove all the TDs we skipped over, from tmp back to the start */
  874. while (tmp != &urbp->td_list) {
  875. td = list_entry(tmp, struct uhci_td, list);
  876. tmp = tmp->prev;
  877. uhci_remove_td_from_urbp(td);
  878. uhci_free_td(uhci, td);
  879. }
  880. return ret;
  881. }
  882. /*
  883. * Common result for control, bulk, and interrupt
  884. */
  885. static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
  886. {
  887. struct urb_priv *urbp = urb->hcpriv;
  888. struct uhci_qh *qh = urbp->qh;
  889. struct uhci_td *td, *tmp;
  890. unsigned status;
  891. int ret = 0;
  892. list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
  893. unsigned int ctrlstat;
  894. int len;
  895. ctrlstat = td_status(td);
  896. status = uhci_status_bits(ctrlstat);
  897. if (status & TD_CTRL_ACTIVE)
  898. return -EINPROGRESS;
  899. len = uhci_actual_length(ctrlstat);
  900. urb->actual_length += len;
  901. if (status) {
  902. ret = uhci_map_status(status,
  903. uhci_packetout(td_token(td)));
  904. if ((debug == 1 && ret != -EPIPE) || debug > 1) {
  905. /* Some debugging code */
  906. dev_dbg(&urb->dev->dev,
  907. "%s: failed with status %x\n",
  908. __FUNCTION__, status);
  909. if (debug > 1 && errbuf) {
  910. /* Print the chain for debugging */
  911. uhci_show_qh(urbp->qh, errbuf,
  912. ERRBUF_LEN, 0);
  913. lprintk(errbuf);
  914. }
  915. }
  916. } else if (len < uhci_expected_length(td_token(td))) {
  917. /* We received a short packet */
  918. if (urb->transfer_flags & URB_SHORT_NOT_OK)
  919. ret = -EREMOTEIO;
  920. /* Fixup needed only if this isn't the URB's last TD */
  921. else if (&td->list != urbp->td_list.prev)
  922. ret = 1;
  923. }
  924. uhci_remove_td_from_urbp(td);
  925. if (qh->post_td)
  926. uhci_free_td(uhci, qh->post_td);
  927. qh->post_td = td;
  928. if (ret != 0)
  929. goto err;
  930. }
  931. return ret;
  932. err:
  933. if (ret < 0) {
  934. /* In case a control transfer gets an error
  935. * during the setup stage */
  936. urb->actual_length = max(urb->actual_length, 0);
  937. /* Note that the queue has stopped and save
  938. * the next toggle value */
  939. qh->element = UHCI_PTR_TERM;
  940. qh->is_stopped = 1;
  941. qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL);
  942. qh->initial_toggle = uhci_toggle(td_token(td)) ^
  943. (ret == -EREMOTEIO);
  944. } else /* Short packet received */
  945. ret = uhci_fixup_short_transfer(uhci, qh, urbp);
  946. return ret;
  947. }
  948. /*
  949. * Isochronous transfers
  950. */
  951. static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
  952. struct uhci_qh *qh)
  953. {
  954. struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
  955. int i, frame;
  956. unsigned long destination, status;
  957. struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
  958. /* Values must not be too big (could overflow below) */
  959. if (urb->interval >= UHCI_NUMFRAMES ||
  960. urb->number_of_packets >= UHCI_NUMFRAMES)
  961. return -EFBIG;
  962. /* Check the period and figure out the starting frame number */
  963. if (!qh->bandwidth_reserved) {
  964. qh->period = urb->interval;
  965. if (urb->transfer_flags & URB_ISO_ASAP) {
  966. qh->phase = -1; /* Find the best phase */
  967. i = uhci_check_bandwidth(uhci, qh);
  968. if (i)
  969. return i;
  970. /* Allow a little time to allocate the TDs */
  971. uhci_get_current_frame_number(uhci);
  972. frame = uhci->frame_number + 10;
  973. /* Move forward to the first frame having the
  974. * correct phase */
  975. urb->start_frame = frame + ((qh->phase - frame) &
  976. (qh->period - 1));
  977. } else {
  978. i = urb->start_frame - uhci->last_iso_frame;
  979. if (i <= 0 || i >= UHCI_NUMFRAMES)
  980. return -EINVAL;
  981. qh->phase = urb->start_frame & (qh->period - 1);
  982. i = uhci_check_bandwidth(uhci, qh);
  983. if (i)
  984. return i;
  985. }
  986. } else if (qh->period != urb->interval) {
  987. return -EINVAL; /* Can't change the period */
  988. } else { /* Pick up where the last URB leaves off */
  989. if (list_empty(&qh->queue)) {
  990. frame = qh->iso_frame;
  991. } else {
  992. struct urb *lurb;
  993. lurb = list_entry(qh->queue.prev,
  994. struct urb_priv, node)->urb;
  995. frame = lurb->start_frame +
  996. lurb->number_of_packets *
  997. lurb->interval;
  998. }
  999. if (urb->transfer_flags & URB_ISO_ASAP)
  1000. urb->start_frame = frame;
  1001. else if (urb->start_frame != frame)
  1002. return -EINVAL;
  1003. }
  1004. /* Make sure we won't have to go too far into the future */
  1005. if (uhci_frame_before_eq(uhci->last_iso_frame + UHCI_NUMFRAMES,
  1006. urb->start_frame + urb->number_of_packets *
  1007. urb->interval))
  1008. return -EFBIG;
  1009. status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
  1010. destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
  1011. for (i = 0; i < urb->number_of_packets; i++) {
  1012. td = uhci_alloc_td(uhci);
  1013. if (!td)
  1014. return -ENOMEM;
  1015. uhci_add_td_to_urbp(td, urbp);
  1016. uhci_fill_td(td, status, destination |
  1017. uhci_explen(urb->iso_frame_desc[i].length),
  1018. urb->transfer_dma +
  1019. urb->iso_frame_desc[i].offset);
  1020. }
  1021. /* Set the interrupt-on-completion flag on the last packet. */
  1022. td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
  1023. /* Add the TDs to the frame list */
  1024. frame = urb->start_frame;
  1025. list_for_each_entry(td, &urbp->td_list, list) {
  1026. uhci_insert_td_in_frame_list(uhci, td, frame);
  1027. frame += qh->period;
  1028. }
  1029. if (list_empty(&qh->queue)) {
  1030. qh->iso_packet_desc = &urb->iso_frame_desc[0];
  1031. qh->iso_frame = urb->start_frame;
  1032. qh->iso_status = 0;
  1033. }
  1034. qh->skel = uhci->skel_iso_qh;
  1035. if (!qh->bandwidth_reserved)
  1036. uhci_reserve_bandwidth(uhci, qh);
  1037. return 0;
  1038. }
  1039. static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
  1040. {
  1041. struct uhci_td *td, *tmp;
  1042. struct urb_priv *urbp = urb->hcpriv;
  1043. struct uhci_qh *qh = urbp->qh;
  1044. list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
  1045. unsigned int ctrlstat;
  1046. int status;
  1047. int actlength;
  1048. if (uhci_frame_before_eq(uhci->cur_iso_frame, qh->iso_frame))
  1049. return -EINPROGRESS;
  1050. uhci_remove_tds_from_frame(uhci, qh->iso_frame);
  1051. ctrlstat = td_status(td);
  1052. if (ctrlstat & TD_CTRL_ACTIVE) {
  1053. status = -EXDEV; /* TD was added too late? */
  1054. } else {
  1055. status = uhci_map_status(uhci_status_bits(ctrlstat),
  1056. usb_pipeout(urb->pipe));
  1057. actlength = uhci_actual_length(ctrlstat);
  1058. urb->actual_length += actlength;
  1059. qh->iso_packet_desc->actual_length = actlength;
  1060. qh->iso_packet_desc->status = status;
  1061. }
  1062. if (status) {
  1063. urb->error_count++;
  1064. qh->iso_status = status;
  1065. }
  1066. uhci_remove_td_from_urbp(td);
  1067. uhci_free_td(uhci, td);
  1068. qh->iso_frame += qh->period;
  1069. ++qh->iso_packet_desc;
  1070. }
  1071. return qh->iso_status;
  1072. }
  1073. static int uhci_urb_enqueue(struct usb_hcd *hcd,
  1074. struct usb_host_endpoint *hep,
  1075. struct urb *urb, gfp_t mem_flags)
  1076. {
  1077. int ret;
  1078. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  1079. unsigned long flags;
  1080. struct urb_priv *urbp;
  1081. struct uhci_qh *qh;
  1082. spin_lock_irqsave(&uhci->lock, flags);
  1083. ret = urb->status;
  1084. if (ret != -EINPROGRESS) /* URB already unlinked! */
  1085. goto done;
  1086. ret = -ENOMEM;
  1087. urbp = uhci_alloc_urb_priv(uhci, urb);
  1088. if (!urbp)
  1089. goto done;
  1090. if (hep->hcpriv)
  1091. qh = (struct uhci_qh *) hep->hcpriv;
  1092. else {
  1093. qh = uhci_alloc_qh(uhci, urb->dev, hep);
  1094. if (!qh)
  1095. goto err_no_qh;
  1096. }
  1097. urbp->qh = qh;
  1098. switch (qh->type) {
  1099. case USB_ENDPOINT_XFER_CONTROL:
  1100. ret = uhci_submit_control(uhci, urb, qh);
  1101. break;
  1102. case USB_ENDPOINT_XFER_BULK:
  1103. ret = uhci_submit_bulk(uhci, urb, qh);
  1104. break;
  1105. case USB_ENDPOINT_XFER_INT:
  1106. ret = uhci_submit_interrupt(uhci, urb, qh);
  1107. break;
  1108. case USB_ENDPOINT_XFER_ISOC:
  1109. urb->error_count = 0;
  1110. ret = uhci_submit_isochronous(uhci, urb, qh);
  1111. break;
  1112. }
  1113. if (ret != 0)
  1114. goto err_submit_failed;
  1115. /* Add this URB to the QH */
  1116. urbp->qh = qh;
  1117. list_add_tail(&urbp->node, &qh->queue);
  1118. /* If the new URB is the first and only one on this QH then either
  1119. * the QH is new and idle or else it's unlinked and waiting to
  1120. * become idle, so we can activate it right away. But only if the
  1121. * queue isn't stopped. */
  1122. if (qh->queue.next == &urbp->node && !qh->is_stopped) {
  1123. uhci_activate_qh(uhci, qh);
  1124. uhci_urbp_wants_fsbr(uhci, urbp);
  1125. }
  1126. goto done;
  1127. err_submit_failed:
  1128. if (qh->state == QH_STATE_IDLE)
  1129. uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
  1130. err_no_qh:
  1131. uhci_free_urb_priv(uhci, urbp);
  1132. done:
  1133. spin_unlock_irqrestore(&uhci->lock, flags);
  1134. return ret;
  1135. }
  1136. static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  1137. {
  1138. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  1139. unsigned long flags;
  1140. struct urb_priv *urbp;
  1141. struct uhci_qh *qh;
  1142. spin_lock_irqsave(&uhci->lock, flags);
  1143. urbp = urb->hcpriv;
  1144. if (!urbp) /* URB was never linked! */
  1145. goto done;
  1146. qh = urbp->qh;
  1147. /* Remove Isochronous TDs from the frame list ASAP */
  1148. if (qh->type == USB_ENDPOINT_XFER_ISOC) {
  1149. uhci_unlink_isochronous_tds(uhci, urb);
  1150. mb();
  1151. /* If the URB has already started, update the QH unlink time */
  1152. uhci_get_current_frame_number(uhci);
  1153. if (uhci_frame_before_eq(urb->start_frame, uhci->frame_number))
  1154. qh->unlink_frame = uhci->frame_number;
  1155. }
  1156. uhci_unlink_qh(uhci, qh);
  1157. done:
  1158. spin_unlock_irqrestore(&uhci->lock, flags);
  1159. return 0;
  1160. }
  1161. /*
  1162. * Finish unlinking an URB and give it back
  1163. */
  1164. static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
  1165. struct urb *urb)
  1166. __releases(uhci->lock)
  1167. __acquires(uhci->lock)
  1168. {
  1169. struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
  1170. /* When giving back the first URB in an Isochronous queue,
  1171. * reinitialize the QH's iso-related members for the next URB. */
  1172. if (qh->type == USB_ENDPOINT_XFER_ISOC &&
  1173. urbp->node.prev == &qh->queue &&
  1174. urbp->node.next != &qh->queue) {
  1175. struct urb *nurb = list_entry(urbp->node.next,
  1176. struct urb_priv, node)->urb;
  1177. qh->iso_packet_desc = &nurb->iso_frame_desc[0];
  1178. qh->iso_frame = nurb->start_frame;
  1179. qh->iso_status = 0;
  1180. }
  1181. /* Take the URB off the QH's queue. If the queue is now empty,
  1182. * this is a perfect time for a toggle fixup. */
  1183. list_del_init(&urbp->node);
  1184. if (list_empty(&qh->queue) && qh->needs_fixup) {
  1185. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  1186. usb_pipeout(urb->pipe), qh->initial_toggle);
  1187. qh->needs_fixup = 0;
  1188. }
  1189. uhci_free_urb_priv(uhci, urbp);
  1190. spin_unlock(&uhci->lock);
  1191. usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb);
  1192. spin_lock(&uhci->lock);
  1193. /* If the queue is now empty, we can unlink the QH and give up its
  1194. * reserved bandwidth. */
  1195. if (list_empty(&qh->queue)) {
  1196. uhci_unlink_qh(uhci, qh);
  1197. if (qh->bandwidth_reserved)
  1198. uhci_release_bandwidth(uhci, qh);
  1199. }
  1200. }
  1201. /*
  1202. * Scan the URBs in a QH's queue
  1203. */
  1204. #define QH_FINISHED_UNLINKING(qh) \
  1205. (qh->state == QH_STATE_UNLINKING && \
  1206. uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
  1207. static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
  1208. {
  1209. struct urb_priv *urbp;
  1210. struct urb *urb;
  1211. int status;
  1212. while (!list_empty(&qh->queue)) {
  1213. urbp = list_entry(qh->queue.next, struct urb_priv, node);
  1214. urb = urbp->urb;
  1215. if (qh->type == USB_ENDPOINT_XFER_ISOC)
  1216. status = uhci_result_isochronous(uhci, urb);
  1217. else
  1218. status = uhci_result_common(uhci, urb);
  1219. if (status == -EINPROGRESS)
  1220. break;
  1221. spin_lock(&urb->lock);
  1222. if (urb->status == -EINPROGRESS) /* Not dequeued */
  1223. urb->status = status;
  1224. else
  1225. status = ECONNRESET; /* Not -ECONNRESET */
  1226. spin_unlock(&urb->lock);
  1227. /* Dequeued but completed URBs can't be given back unless
  1228. * the QH is stopped or has finished unlinking. */
  1229. if (status == ECONNRESET) {
  1230. if (QH_FINISHED_UNLINKING(qh))
  1231. qh->is_stopped = 1;
  1232. else if (!qh->is_stopped)
  1233. return;
  1234. }
  1235. uhci_giveback_urb(uhci, qh, urb);
  1236. if (status < 0 && qh->type != USB_ENDPOINT_XFER_ISOC)
  1237. break;
  1238. }
  1239. /* If the QH is neither stopped nor finished unlinking (normal case),
  1240. * our work here is done. */
  1241. if (QH_FINISHED_UNLINKING(qh))
  1242. qh->is_stopped = 1;
  1243. else if (!qh->is_stopped)
  1244. return;
  1245. /* Otherwise give back each of the dequeued URBs */
  1246. restart:
  1247. list_for_each_entry(urbp, &qh->queue, node) {
  1248. urb = urbp->urb;
  1249. if (urb->status != -EINPROGRESS) {
  1250. /* Fix up the TD links and save the toggles for
  1251. * non-Isochronous queues. For Isochronous queues,
  1252. * test for too-recent dequeues. */
  1253. if (!uhci_cleanup_queue(uhci, qh, urb)) {
  1254. qh->is_stopped = 0;
  1255. return;
  1256. }
  1257. uhci_giveback_urb(uhci, qh, urb);
  1258. goto restart;
  1259. }
  1260. }
  1261. qh->is_stopped = 0;
  1262. /* There are no more dequeued URBs. If there are still URBs on the
  1263. * queue, the QH can now be re-activated. */
  1264. if (!list_empty(&qh->queue)) {
  1265. if (qh->needs_fixup)
  1266. uhci_fixup_toggles(qh, 0);
  1267. /* If the first URB on the queue wants FSBR but its time
  1268. * limit has expired, set the next TD to interrupt on
  1269. * completion before reactivating the QH. */
  1270. urbp = list_entry(qh->queue.next, struct urb_priv, node);
  1271. if (urbp->fsbr && qh->wait_expired) {
  1272. struct uhci_td *td = list_entry(urbp->td_list.next,
  1273. struct uhci_td, list);
  1274. td->status |= __cpu_to_le32(TD_CTRL_IOC);
  1275. }
  1276. uhci_activate_qh(uhci, qh);
  1277. }
  1278. /* The queue is empty. The QH can become idle if it is fully
  1279. * unlinked. */
  1280. else if (QH_FINISHED_UNLINKING(qh))
  1281. uhci_make_qh_idle(uhci, qh);
  1282. }
  1283. /*
  1284. * Check for queues that have made some forward progress.
  1285. * Returns 0 if the queue is not Isochronous, is ACTIVE, and
  1286. * has not advanced since last examined; 1 otherwise.
  1287. *
  1288. * Early Intel controllers have a bug which causes qh->element sometimes
  1289. * not to advance when a TD completes successfully. The queue remains
  1290. * stuck on the inactive completed TD. We detect such cases and advance
  1291. * the element pointer by hand.
  1292. */
  1293. static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh)
  1294. {
  1295. struct urb_priv *urbp = NULL;
  1296. struct uhci_td *td;
  1297. int ret = 1;
  1298. unsigned status;
  1299. if (qh->type == USB_ENDPOINT_XFER_ISOC)
  1300. goto done;
  1301. /* Treat an UNLINKING queue as though it hasn't advanced.
  1302. * This is okay because reactivation will treat it as though
  1303. * it has advanced, and if it is going to become IDLE then
  1304. * this doesn't matter anyway. Furthermore it's possible
  1305. * for an UNLINKING queue not to have any URBs at all, or
  1306. * for its first URB not to have any TDs (if it was dequeued
  1307. * just as it completed). So it's not easy in any case to
  1308. * test whether such queues have advanced. */
  1309. if (qh->state != QH_STATE_ACTIVE) {
  1310. urbp = NULL;
  1311. status = 0;
  1312. } else {
  1313. urbp = list_entry(qh->queue.next, struct urb_priv, node);
  1314. td = list_entry(urbp->td_list.next, struct uhci_td, list);
  1315. status = td_status(td);
  1316. if (!(status & TD_CTRL_ACTIVE)) {
  1317. /* We're okay, the queue has advanced */
  1318. qh->wait_expired = 0;
  1319. qh->advance_jiffies = jiffies;
  1320. goto done;
  1321. }
  1322. ret = 0;
  1323. }
  1324. /* The queue hasn't advanced; check for timeout */
  1325. if (qh->wait_expired)
  1326. goto done;
  1327. if (time_after(jiffies, qh->advance_jiffies + QH_WAIT_TIMEOUT)) {
  1328. /* Detect the Intel bug and work around it */
  1329. if (qh->post_td && qh_element(qh) ==
  1330. cpu_to_le32(qh->post_td->dma_handle)) {
  1331. qh->element = qh->post_td->link;
  1332. qh->advance_jiffies = jiffies;
  1333. ret = 1;
  1334. goto done;
  1335. }
  1336. qh->wait_expired = 1;
  1337. /* If the current URB wants FSBR, unlink it temporarily
  1338. * so that we can safely set the next TD to interrupt on
  1339. * completion. That way we'll know as soon as the queue
  1340. * starts moving again. */
  1341. if (urbp && urbp->fsbr && !(status & TD_CTRL_IOC))
  1342. uhci_unlink_qh(uhci, qh);
  1343. } else {
  1344. /* Unmoving but not-yet-expired queues keep FSBR alive */
  1345. if (urbp)
  1346. uhci_urbp_wants_fsbr(uhci, urbp);
  1347. }
  1348. done:
  1349. return ret;
  1350. }
  1351. /*
  1352. * Process events in the schedule, but only in one thread at a time
  1353. */
  1354. static void uhci_scan_schedule(struct uhci_hcd *uhci)
  1355. {
  1356. int i;
  1357. struct uhci_qh *qh;
  1358. /* Don't allow re-entrant calls */
  1359. if (uhci->scan_in_progress) {
  1360. uhci->need_rescan = 1;
  1361. return;
  1362. }
  1363. uhci->scan_in_progress = 1;
  1364. rescan:
  1365. uhci->need_rescan = 0;
  1366. uhci->fsbr_is_wanted = 0;
  1367. uhci_clear_next_interrupt(uhci);
  1368. uhci_get_current_frame_number(uhci);
  1369. uhci->cur_iso_frame = uhci->frame_number;
  1370. /* Go through all the QH queues and process the URBs in each one */
  1371. for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
  1372. uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
  1373. struct uhci_qh, node);
  1374. while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
  1375. uhci->next_qh = list_entry(qh->node.next,
  1376. struct uhci_qh, node);
  1377. if (uhci_advance_check(uhci, qh)) {
  1378. uhci_scan_qh(uhci, qh);
  1379. if (qh->state == QH_STATE_ACTIVE) {
  1380. uhci_urbp_wants_fsbr(uhci,
  1381. list_entry(qh->queue.next, struct urb_priv, node));
  1382. }
  1383. }
  1384. }
  1385. }
  1386. uhci->last_iso_frame = uhci->cur_iso_frame;
  1387. if (uhci->need_rescan)
  1388. goto rescan;
  1389. uhci->scan_in_progress = 0;
  1390. if (uhci->fsbr_is_on && !uhci->fsbr_is_wanted &&
  1391. !uhci->fsbr_expiring) {
  1392. uhci->fsbr_expiring = 1;
  1393. mod_timer(&uhci->fsbr_timer, jiffies + FSBR_OFF_DELAY);
  1394. }
  1395. if (list_empty(&uhci->skel_unlink_qh->node))
  1396. uhci_clear_next_interrupt(uhci);
  1397. else
  1398. uhci_set_next_interrupt(uhci);
  1399. }