uhci-q.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  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_alloc(uhci_up_cachep, GFP_ATOMIC);
  534. if (!urbp)
  535. return NULL;
  536. memset((void *)urbp, 0, sizeof(*urbp));
  537. urbp->urb = urb;
  538. urb->hcpriv = urbp;
  539. INIT_LIST_HEAD(&urbp->node);
  540. INIT_LIST_HEAD(&urbp->td_list);
  541. return urbp;
  542. }
  543. static void uhci_free_urb_priv(struct uhci_hcd *uhci,
  544. struct urb_priv *urbp)
  545. {
  546. struct uhci_td *td, *tmp;
  547. if (!list_empty(&urbp->node))
  548. dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
  549. urbp->urb);
  550. list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
  551. uhci_remove_td_from_urbp(td);
  552. uhci_free_td(uhci, td);
  553. }
  554. urbp->urb->hcpriv = NULL;
  555. kmem_cache_free(uhci_up_cachep, urbp);
  556. }
  557. /*
  558. * Map status to standard result codes
  559. *
  560. * <status> is (td_status(td) & 0xF60000), a.k.a.
  561. * uhci_status_bits(td_status(td)).
  562. * Note: <status> does not include the TD_CTRL_NAK bit.
  563. * <dir_out> is True for output TDs and False for input TDs.
  564. */
  565. static int uhci_map_status(int status, int dir_out)
  566. {
  567. if (!status)
  568. return 0;
  569. if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
  570. return -EPROTO;
  571. if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
  572. if (dir_out)
  573. return -EPROTO;
  574. else
  575. return -EILSEQ;
  576. }
  577. if (status & TD_CTRL_BABBLE) /* Babble */
  578. return -EOVERFLOW;
  579. if (status & TD_CTRL_DBUFERR) /* Buffer error */
  580. return -ENOSR;
  581. if (status & TD_CTRL_STALLED) /* Stalled */
  582. return -EPIPE;
  583. return 0;
  584. }
  585. /*
  586. * Control transfers
  587. */
  588. static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
  589. struct uhci_qh *qh)
  590. {
  591. struct uhci_td *td;
  592. unsigned long destination, status;
  593. int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
  594. int len = urb->transfer_buffer_length;
  595. dma_addr_t data = urb->transfer_dma;
  596. __le32 *plink;
  597. struct urb_priv *urbp = urb->hcpriv;
  598. /* The "pipe" thing contains the destination in bits 8--18 */
  599. destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
  600. /* 3 errors, dummy TD remains inactive */
  601. status = uhci_maxerr(3);
  602. if (urb->dev->speed == USB_SPEED_LOW)
  603. status |= TD_CTRL_LS;
  604. /*
  605. * Build the TD for the control request setup packet
  606. */
  607. td = qh->dummy_td;
  608. uhci_add_td_to_urbp(td, urbp);
  609. uhci_fill_td(td, status, destination | uhci_explen(8),
  610. urb->setup_dma);
  611. plink = &td->link;
  612. status |= TD_CTRL_ACTIVE;
  613. /*
  614. * If direction is "send", change the packet ID from SETUP (0x2D)
  615. * to OUT (0xE1). Else change it from SETUP to IN (0x69) and
  616. * set Short Packet Detect (SPD) for all data packets.
  617. */
  618. if (usb_pipeout(urb->pipe))
  619. destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
  620. else {
  621. destination ^= (USB_PID_SETUP ^ USB_PID_IN);
  622. status |= TD_CTRL_SPD;
  623. }
  624. /*
  625. * Build the DATA TDs
  626. */
  627. while (len > 0) {
  628. int pktsze = min(len, maxsze);
  629. td = uhci_alloc_td(uhci);
  630. if (!td)
  631. goto nomem;
  632. *plink = cpu_to_le32(td->dma_handle);
  633. /* Alternate Data0/1 (start with Data1) */
  634. destination ^= TD_TOKEN_TOGGLE;
  635. uhci_add_td_to_urbp(td, urbp);
  636. uhci_fill_td(td, status, destination | uhci_explen(pktsze),
  637. data);
  638. plink = &td->link;
  639. data += pktsze;
  640. len -= pktsze;
  641. }
  642. /*
  643. * Build the final TD for control status
  644. */
  645. td = uhci_alloc_td(uhci);
  646. if (!td)
  647. goto nomem;
  648. *plink = cpu_to_le32(td->dma_handle);
  649. /*
  650. * It's IN if the pipe is an output pipe or we're not expecting
  651. * data back.
  652. */
  653. destination &= ~TD_TOKEN_PID_MASK;
  654. if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
  655. destination |= USB_PID_IN;
  656. else
  657. destination |= USB_PID_OUT;
  658. destination |= TD_TOKEN_TOGGLE; /* End in Data1 */
  659. status &= ~TD_CTRL_SPD;
  660. uhci_add_td_to_urbp(td, urbp);
  661. uhci_fill_td(td, status | TD_CTRL_IOC,
  662. destination | uhci_explen(0), 0);
  663. plink = &td->link;
  664. /*
  665. * Build the new dummy TD and activate the old one
  666. */
  667. td = uhci_alloc_td(uhci);
  668. if (!td)
  669. goto nomem;
  670. *plink = cpu_to_le32(td->dma_handle);
  671. uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
  672. wmb();
  673. qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
  674. qh->dummy_td = td;
  675. /* Low-speed transfers get a different queue, and won't hog the bus.
  676. * Also, some devices enumerate better without FSBR; the easiest way
  677. * to do that is to put URBs on the low-speed queue while the device
  678. * isn't in the CONFIGURED state. */
  679. if (urb->dev->speed == USB_SPEED_LOW ||
  680. urb->dev->state != USB_STATE_CONFIGURED)
  681. qh->skel = uhci->skel_ls_control_qh;
  682. else {
  683. qh->skel = uhci->skel_fs_control_qh;
  684. uhci_add_fsbr(uhci, urb);
  685. }
  686. urb->actual_length = -8; /* Account for the SETUP packet */
  687. return 0;
  688. nomem:
  689. /* Remove the dummy TD from the td_list so it doesn't get freed */
  690. uhci_remove_td_from_urbp(qh->dummy_td);
  691. return -ENOMEM;
  692. }
  693. /*
  694. * Common submit for bulk and interrupt
  695. */
  696. static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
  697. struct uhci_qh *qh)
  698. {
  699. struct uhci_td *td;
  700. unsigned long destination, status;
  701. int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
  702. int len = urb->transfer_buffer_length;
  703. dma_addr_t data = urb->transfer_dma;
  704. __le32 *plink;
  705. struct urb_priv *urbp = urb->hcpriv;
  706. unsigned int toggle;
  707. if (len < 0)
  708. return -EINVAL;
  709. /* The "pipe" thing contains the destination in bits 8--18 */
  710. destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
  711. toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  712. usb_pipeout(urb->pipe));
  713. /* 3 errors, dummy TD remains inactive */
  714. status = uhci_maxerr(3);
  715. if (urb->dev->speed == USB_SPEED_LOW)
  716. status |= TD_CTRL_LS;
  717. if (usb_pipein(urb->pipe))
  718. status |= TD_CTRL_SPD;
  719. /*
  720. * Build the DATA TDs
  721. */
  722. plink = NULL;
  723. td = qh->dummy_td;
  724. do { /* Allow zero length packets */
  725. int pktsze = maxsze;
  726. if (len <= pktsze) { /* The last packet */
  727. pktsze = len;
  728. if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
  729. status &= ~TD_CTRL_SPD;
  730. }
  731. if (plink) {
  732. td = uhci_alloc_td(uhci);
  733. if (!td)
  734. goto nomem;
  735. *plink = cpu_to_le32(td->dma_handle);
  736. }
  737. uhci_add_td_to_urbp(td, urbp);
  738. uhci_fill_td(td, status,
  739. destination | uhci_explen(pktsze) |
  740. (toggle << TD_TOKEN_TOGGLE_SHIFT),
  741. data);
  742. plink = &td->link;
  743. status |= TD_CTRL_ACTIVE;
  744. data += pktsze;
  745. len -= maxsze;
  746. toggle ^= 1;
  747. } while (len > 0);
  748. /*
  749. * URB_ZERO_PACKET means adding a 0-length packet, if direction
  750. * is OUT and the transfer_length was an exact multiple of maxsze,
  751. * hence (len = transfer_length - N * maxsze) == 0
  752. * however, if transfer_length == 0, the zero packet was already
  753. * prepared above.
  754. */
  755. if ((urb->transfer_flags & URB_ZERO_PACKET) &&
  756. usb_pipeout(urb->pipe) && len == 0 &&
  757. urb->transfer_buffer_length > 0) {
  758. td = uhci_alloc_td(uhci);
  759. if (!td)
  760. goto nomem;
  761. *plink = cpu_to_le32(td->dma_handle);
  762. uhci_add_td_to_urbp(td, urbp);
  763. uhci_fill_td(td, status,
  764. destination | uhci_explen(0) |
  765. (toggle << TD_TOKEN_TOGGLE_SHIFT),
  766. data);
  767. plink = &td->link;
  768. toggle ^= 1;
  769. }
  770. /* Set the interrupt-on-completion flag on the last packet.
  771. * A more-or-less typical 4 KB URB (= size of one memory page)
  772. * will require about 3 ms to transfer; that's a little on the
  773. * fast side but not enough to justify delaying an interrupt
  774. * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
  775. * flag setting. */
  776. td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
  777. /*
  778. * Build the new dummy TD and activate the old one
  779. */
  780. td = uhci_alloc_td(uhci);
  781. if (!td)
  782. goto nomem;
  783. *plink = cpu_to_le32(td->dma_handle);
  784. uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
  785. wmb();
  786. qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
  787. qh->dummy_td = td;
  788. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  789. usb_pipeout(urb->pipe), toggle);
  790. return 0;
  791. nomem:
  792. /* Remove the dummy TD from the td_list so it doesn't get freed */
  793. uhci_remove_td_from_urbp(qh->dummy_td);
  794. return -ENOMEM;
  795. }
  796. static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
  797. struct uhci_qh *qh)
  798. {
  799. int ret;
  800. /* Can't have low-speed bulk transfers */
  801. if (urb->dev->speed == USB_SPEED_LOW)
  802. return -EINVAL;
  803. qh->skel = uhci->skel_bulk_qh;
  804. ret = uhci_submit_common(uhci, urb, qh);
  805. if (ret == 0)
  806. uhci_add_fsbr(uhci, urb);
  807. return ret;
  808. }
  809. static int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
  810. struct uhci_qh *qh)
  811. {
  812. int ret;
  813. /* USB 1.1 interrupt transfers only involve one packet per interval.
  814. * Drivers can submit URBs of any length, but longer ones will need
  815. * multiple intervals to complete.
  816. */
  817. if (!qh->bandwidth_reserved) {
  818. int exponent;
  819. /* Figure out which power-of-two queue to use */
  820. for (exponent = 7; exponent >= 0; --exponent) {
  821. if ((1 << exponent) <= urb->interval)
  822. break;
  823. }
  824. if (exponent < 0)
  825. return -EINVAL;
  826. qh->period = 1 << exponent;
  827. qh->skel = uhci->skelqh[UHCI_SKEL_INDEX(exponent)];
  828. /* For now, interrupt phase is fixed by the layout
  829. * of the QH lists. */
  830. qh->phase = (qh->period / 2) & (MAX_PHASE - 1);
  831. ret = uhci_check_bandwidth(uhci, qh);
  832. if (ret)
  833. return ret;
  834. } else if (qh->period > urb->interval)
  835. return -EINVAL; /* Can't decrease the period */
  836. ret = uhci_submit_common(uhci, urb, qh);
  837. if (ret == 0) {
  838. urb->interval = qh->period;
  839. if (!qh->bandwidth_reserved)
  840. uhci_reserve_bandwidth(uhci, qh);
  841. }
  842. return ret;
  843. }
  844. /*
  845. * Fix up the data structures following a short transfer
  846. */
  847. static int uhci_fixup_short_transfer(struct uhci_hcd *uhci,
  848. struct uhci_qh *qh, struct urb_priv *urbp)
  849. {
  850. struct uhci_td *td;
  851. struct list_head *tmp;
  852. int ret;
  853. td = list_entry(urbp->td_list.prev, struct uhci_td, list);
  854. if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
  855. /* When a control transfer is short, we have to restart
  856. * the queue at the status stage transaction, which is
  857. * the last TD. */
  858. WARN_ON(list_empty(&urbp->td_list));
  859. qh->element = cpu_to_le32(td->dma_handle);
  860. tmp = td->list.prev;
  861. ret = -EINPROGRESS;
  862. } else {
  863. /* When a bulk/interrupt transfer is short, we have to
  864. * fix up the toggles of the following URBs on the queue
  865. * before restarting the queue at the next URB. */
  866. qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1;
  867. uhci_fixup_toggles(qh, 1);
  868. if (list_empty(&urbp->td_list))
  869. td = qh->post_td;
  870. qh->element = td->link;
  871. tmp = urbp->td_list.prev;
  872. ret = 0;
  873. }
  874. /* Remove all the TDs we skipped over, from tmp back to the start */
  875. while (tmp != &urbp->td_list) {
  876. td = list_entry(tmp, struct uhci_td, list);
  877. tmp = tmp->prev;
  878. uhci_remove_td_from_urbp(td);
  879. uhci_free_td(uhci, td);
  880. }
  881. return ret;
  882. }
  883. /*
  884. * Common result for control, bulk, and interrupt
  885. */
  886. static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
  887. {
  888. struct urb_priv *urbp = urb->hcpriv;
  889. struct uhci_qh *qh = urbp->qh;
  890. struct uhci_td *td, *tmp;
  891. unsigned status;
  892. int ret = 0;
  893. list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
  894. unsigned int ctrlstat;
  895. int len;
  896. ctrlstat = td_status(td);
  897. status = uhci_status_bits(ctrlstat);
  898. if (status & TD_CTRL_ACTIVE)
  899. return -EINPROGRESS;
  900. len = uhci_actual_length(ctrlstat);
  901. urb->actual_length += len;
  902. if (status) {
  903. ret = uhci_map_status(status,
  904. uhci_packetout(td_token(td)));
  905. if ((debug == 1 && ret != -EPIPE) || debug > 1) {
  906. /* Some debugging code */
  907. dev_dbg(&urb->dev->dev,
  908. "%s: failed with status %x\n",
  909. __FUNCTION__, status);
  910. if (debug > 1 && errbuf) {
  911. /* Print the chain for debugging */
  912. uhci_show_qh(urbp->qh, errbuf,
  913. ERRBUF_LEN, 0);
  914. lprintk(errbuf);
  915. }
  916. }
  917. } else if (len < uhci_expected_length(td_token(td))) {
  918. /* We received a short packet */
  919. if (urb->transfer_flags & URB_SHORT_NOT_OK)
  920. ret = -EREMOTEIO;
  921. /* Fixup needed only if this isn't the URB's last TD */
  922. else if (&td->list != urbp->td_list.prev)
  923. ret = 1;
  924. }
  925. uhci_remove_td_from_urbp(td);
  926. if (qh->post_td)
  927. uhci_free_td(uhci, qh->post_td);
  928. qh->post_td = td;
  929. if (ret != 0)
  930. goto err;
  931. }
  932. return ret;
  933. err:
  934. if (ret < 0) {
  935. /* In case a control transfer gets an error
  936. * during the setup stage */
  937. urb->actual_length = max(urb->actual_length, 0);
  938. /* Note that the queue has stopped and save
  939. * the next toggle value */
  940. qh->element = UHCI_PTR_TERM;
  941. qh->is_stopped = 1;
  942. qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL);
  943. qh->initial_toggle = uhci_toggle(td_token(td)) ^
  944. (ret == -EREMOTEIO);
  945. } else /* Short packet received */
  946. ret = uhci_fixup_short_transfer(uhci, qh, urbp);
  947. return ret;
  948. }
  949. /*
  950. * Isochronous transfers
  951. */
  952. static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
  953. struct uhci_qh *qh)
  954. {
  955. struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
  956. int i, frame;
  957. unsigned long destination, status;
  958. struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
  959. /* Values must not be too big (could overflow below) */
  960. if (urb->interval >= UHCI_NUMFRAMES ||
  961. urb->number_of_packets >= UHCI_NUMFRAMES)
  962. return -EFBIG;
  963. /* Check the period and figure out the starting frame number */
  964. if (!qh->bandwidth_reserved) {
  965. qh->period = urb->interval;
  966. if (urb->transfer_flags & URB_ISO_ASAP) {
  967. qh->phase = -1; /* Find the best phase */
  968. i = uhci_check_bandwidth(uhci, qh);
  969. if (i)
  970. return i;
  971. /* Allow a little time to allocate the TDs */
  972. uhci_get_current_frame_number(uhci);
  973. frame = uhci->frame_number + 10;
  974. /* Move forward to the first frame having the
  975. * correct phase */
  976. urb->start_frame = frame + ((qh->phase - frame) &
  977. (qh->period - 1));
  978. } else {
  979. i = urb->start_frame - uhci->last_iso_frame;
  980. if (i <= 0 || i >= UHCI_NUMFRAMES)
  981. return -EINVAL;
  982. qh->phase = urb->start_frame & (qh->period - 1);
  983. i = uhci_check_bandwidth(uhci, qh);
  984. if (i)
  985. return i;
  986. }
  987. } else if (qh->period != urb->interval) {
  988. return -EINVAL; /* Can't change the period */
  989. } else { /* Pick up where the last URB leaves off */
  990. if (list_empty(&qh->queue)) {
  991. frame = qh->iso_frame;
  992. } else {
  993. struct urb *lurb;
  994. lurb = list_entry(qh->queue.prev,
  995. struct urb_priv, node)->urb;
  996. frame = lurb->start_frame +
  997. lurb->number_of_packets *
  998. lurb->interval;
  999. }
  1000. if (urb->transfer_flags & URB_ISO_ASAP)
  1001. urb->start_frame = frame;
  1002. else if (urb->start_frame != frame)
  1003. return -EINVAL;
  1004. }
  1005. /* Make sure we won't have to go too far into the future */
  1006. if (uhci_frame_before_eq(uhci->last_iso_frame + UHCI_NUMFRAMES,
  1007. urb->start_frame + urb->number_of_packets *
  1008. urb->interval))
  1009. return -EFBIG;
  1010. status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
  1011. destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
  1012. for (i = 0; i < urb->number_of_packets; i++) {
  1013. td = uhci_alloc_td(uhci);
  1014. if (!td)
  1015. return -ENOMEM;
  1016. uhci_add_td_to_urbp(td, urbp);
  1017. uhci_fill_td(td, status, destination |
  1018. uhci_explen(urb->iso_frame_desc[i].length),
  1019. urb->transfer_dma +
  1020. urb->iso_frame_desc[i].offset);
  1021. }
  1022. /* Set the interrupt-on-completion flag on the last packet. */
  1023. td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
  1024. /* Add the TDs to the frame list */
  1025. frame = urb->start_frame;
  1026. list_for_each_entry(td, &urbp->td_list, list) {
  1027. uhci_insert_td_in_frame_list(uhci, td, frame);
  1028. frame += qh->period;
  1029. }
  1030. if (list_empty(&qh->queue)) {
  1031. qh->iso_packet_desc = &urb->iso_frame_desc[0];
  1032. qh->iso_frame = urb->start_frame;
  1033. qh->iso_status = 0;
  1034. }
  1035. qh->skel = uhci->skel_iso_qh;
  1036. if (!qh->bandwidth_reserved)
  1037. uhci_reserve_bandwidth(uhci, qh);
  1038. return 0;
  1039. }
  1040. static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
  1041. {
  1042. struct uhci_td *td, *tmp;
  1043. struct urb_priv *urbp = urb->hcpriv;
  1044. struct uhci_qh *qh = urbp->qh;
  1045. list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
  1046. unsigned int ctrlstat;
  1047. int status;
  1048. int actlength;
  1049. if (uhci_frame_before_eq(uhci->cur_iso_frame, qh->iso_frame))
  1050. return -EINPROGRESS;
  1051. uhci_remove_tds_from_frame(uhci, qh->iso_frame);
  1052. ctrlstat = td_status(td);
  1053. if (ctrlstat & TD_CTRL_ACTIVE) {
  1054. status = -EXDEV; /* TD was added too late? */
  1055. } else {
  1056. status = uhci_map_status(uhci_status_bits(ctrlstat),
  1057. usb_pipeout(urb->pipe));
  1058. actlength = uhci_actual_length(ctrlstat);
  1059. urb->actual_length += actlength;
  1060. qh->iso_packet_desc->actual_length = actlength;
  1061. qh->iso_packet_desc->status = status;
  1062. }
  1063. if (status) {
  1064. urb->error_count++;
  1065. qh->iso_status = status;
  1066. }
  1067. uhci_remove_td_from_urbp(td);
  1068. uhci_free_td(uhci, td);
  1069. qh->iso_frame += qh->period;
  1070. ++qh->iso_packet_desc;
  1071. }
  1072. return qh->iso_status;
  1073. }
  1074. static int uhci_urb_enqueue(struct usb_hcd *hcd,
  1075. struct usb_host_endpoint *hep,
  1076. struct urb *urb, gfp_t mem_flags)
  1077. {
  1078. int ret;
  1079. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  1080. unsigned long flags;
  1081. struct urb_priv *urbp;
  1082. struct uhci_qh *qh;
  1083. spin_lock_irqsave(&uhci->lock, flags);
  1084. ret = urb->status;
  1085. if (ret != -EINPROGRESS) /* URB already unlinked! */
  1086. goto done;
  1087. ret = -ENOMEM;
  1088. urbp = uhci_alloc_urb_priv(uhci, urb);
  1089. if (!urbp)
  1090. goto done;
  1091. if (hep->hcpriv)
  1092. qh = (struct uhci_qh *) hep->hcpriv;
  1093. else {
  1094. qh = uhci_alloc_qh(uhci, urb->dev, hep);
  1095. if (!qh)
  1096. goto err_no_qh;
  1097. }
  1098. urbp->qh = qh;
  1099. switch (qh->type) {
  1100. case USB_ENDPOINT_XFER_CONTROL:
  1101. ret = uhci_submit_control(uhci, urb, qh);
  1102. break;
  1103. case USB_ENDPOINT_XFER_BULK:
  1104. ret = uhci_submit_bulk(uhci, urb, qh);
  1105. break;
  1106. case USB_ENDPOINT_XFER_INT:
  1107. ret = uhci_submit_interrupt(uhci, urb, qh);
  1108. break;
  1109. case USB_ENDPOINT_XFER_ISOC:
  1110. urb->error_count = 0;
  1111. ret = uhci_submit_isochronous(uhci, urb, qh);
  1112. break;
  1113. }
  1114. if (ret != 0)
  1115. goto err_submit_failed;
  1116. /* Add this URB to the QH */
  1117. urbp->qh = qh;
  1118. list_add_tail(&urbp->node, &qh->queue);
  1119. /* If the new URB is the first and only one on this QH then either
  1120. * the QH is new and idle or else it's unlinked and waiting to
  1121. * become idle, so we can activate it right away. But only if the
  1122. * queue isn't stopped. */
  1123. if (qh->queue.next == &urbp->node && !qh->is_stopped) {
  1124. uhci_activate_qh(uhci, qh);
  1125. uhci_urbp_wants_fsbr(uhci, urbp);
  1126. }
  1127. goto done;
  1128. err_submit_failed:
  1129. if (qh->state == QH_STATE_IDLE)
  1130. uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
  1131. err_no_qh:
  1132. uhci_free_urb_priv(uhci, urbp);
  1133. done:
  1134. spin_unlock_irqrestore(&uhci->lock, flags);
  1135. return ret;
  1136. }
  1137. static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  1138. {
  1139. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  1140. unsigned long flags;
  1141. struct urb_priv *urbp;
  1142. struct uhci_qh *qh;
  1143. spin_lock_irqsave(&uhci->lock, flags);
  1144. urbp = urb->hcpriv;
  1145. if (!urbp) /* URB was never linked! */
  1146. goto done;
  1147. qh = urbp->qh;
  1148. /* Remove Isochronous TDs from the frame list ASAP */
  1149. if (qh->type == USB_ENDPOINT_XFER_ISOC) {
  1150. uhci_unlink_isochronous_tds(uhci, urb);
  1151. mb();
  1152. /* If the URB has already started, update the QH unlink time */
  1153. uhci_get_current_frame_number(uhci);
  1154. if (uhci_frame_before_eq(urb->start_frame, uhci->frame_number))
  1155. qh->unlink_frame = uhci->frame_number;
  1156. }
  1157. uhci_unlink_qh(uhci, qh);
  1158. done:
  1159. spin_unlock_irqrestore(&uhci->lock, flags);
  1160. return 0;
  1161. }
  1162. /*
  1163. * Finish unlinking an URB and give it back
  1164. */
  1165. static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
  1166. struct urb *urb)
  1167. __releases(uhci->lock)
  1168. __acquires(uhci->lock)
  1169. {
  1170. struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
  1171. /* When giving back the first URB in an Isochronous queue,
  1172. * reinitialize the QH's iso-related members for the next URB. */
  1173. if (qh->type == USB_ENDPOINT_XFER_ISOC &&
  1174. urbp->node.prev == &qh->queue &&
  1175. urbp->node.next != &qh->queue) {
  1176. struct urb *nurb = list_entry(urbp->node.next,
  1177. struct urb_priv, node)->urb;
  1178. qh->iso_packet_desc = &nurb->iso_frame_desc[0];
  1179. qh->iso_frame = nurb->start_frame;
  1180. qh->iso_status = 0;
  1181. }
  1182. /* Take the URB off the QH's queue. If the queue is now empty,
  1183. * this is a perfect time for a toggle fixup. */
  1184. list_del_init(&urbp->node);
  1185. if (list_empty(&qh->queue) && qh->needs_fixup) {
  1186. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  1187. usb_pipeout(urb->pipe), qh->initial_toggle);
  1188. qh->needs_fixup = 0;
  1189. }
  1190. uhci_free_urb_priv(uhci, urbp);
  1191. spin_unlock(&uhci->lock);
  1192. usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb);
  1193. spin_lock(&uhci->lock);
  1194. /* If the queue is now empty, we can unlink the QH and give up its
  1195. * reserved bandwidth. */
  1196. if (list_empty(&qh->queue)) {
  1197. uhci_unlink_qh(uhci, qh);
  1198. if (qh->bandwidth_reserved)
  1199. uhci_release_bandwidth(uhci, qh);
  1200. }
  1201. }
  1202. /*
  1203. * Scan the URBs in a QH's queue
  1204. */
  1205. #define QH_FINISHED_UNLINKING(qh) \
  1206. (qh->state == QH_STATE_UNLINKING && \
  1207. uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
  1208. static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
  1209. {
  1210. struct urb_priv *urbp;
  1211. struct urb *urb;
  1212. int status;
  1213. while (!list_empty(&qh->queue)) {
  1214. urbp = list_entry(qh->queue.next, struct urb_priv, node);
  1215. urb = urbp->urb;
  1216. if (qh->type == USB_ENDPOINT_XFER_ISOC)
  1217. status = uhci_result_isochronous(uhci, urb);
  1218. else
  1219. status = uhci_result_common(uhci, urb);
  1220. if (status == -EINPROGRESS)
  1221. break;
  1222. spin_lock(&urb->lock);
  1223. if (urb->status == -EINPROGRESS) /* Not dequeued */
  1224. urb->status = status;
  1225. else
  1226. status = ECONNRESET; /* Not -ECONNRESET */
  1227. spin_unlock(&urb->lock);
  1228. /* Dequeued but completed URBs can't be given back unless
  1229. * the QH is stopped or has finished unlinking. */
  1230. if (status == ECONNRESET) {
  1231. if (QH_FINISHED_UNLINKING(qh))
  1232. qh->is_stopped = 1;
  1233. else if (!qh->is_stopped)
  1234. return;
  1235. }
  1236. uhci_giveback_urb(uhci, qh, urb);
  1237. if (status < 0 && qh->type != USB_ENDPOINT_XFER_ISOC)
  1238. break;
  1239. }
  1240. /* If the QH is neither stopped nor finished unlinking (normal case),
  1241. * our work here is done. */
  1242. if (QH_FINISHED_UNLINKING(qh))
  1243. qh->is_stopped = 1;
  1244. else if (!qh->is_stopped)
  1245. return;
  1246. /* Otherwise give back each of the dequeued URBs */
  1247. restart:
  1248. list_for_each_entry(urbp, &qh->queue, node) {
  1249. urb = urbp->urb;
  1250. if (urb->status != -EINPROGRESS) {
  1251. /* Fix up the TD links and save the toggles for
  1252. * non-Isochronous queues. For Isochronous queues,
  1253. * test for too-recent dequeues. */
  1254. if (!uhci_cleanup_queue(uhci, qh, urb)) {
  1255. qh->is_stopped = 0;
  1256. return;
  1257. }
  1258. uhci_giveback_urb(uhci, qh, urb);
  1259. goto restart;
  1260. }
  1261. }
  1262. qh->is_stopped = 0;
  1263. /* There are no more dequeued URBs. If there are still URBs on the
  1264. * queue, the QH can now be re-activated. */
  1265. if (!list_empty(&qh->queue)) {
  1266. if (qh->needs_fixup)
  1267. uhci_fixup_toggles(qh, 0);
  1268. /* If the first URB on the queue wants FSBR but its time
  1269. * limit has expired, set the next TD to interrupt on
  1270. * completion before reactivating the QH. */
  1271. urbp = list_entry(qh->queue.next, struct urb_priv, node);
  1272. if (urbp->fsbr && qh->wait_expired) {
  1273. struct uhci_td *td = list_entry(urbp->td_list.next,
  1274. struct uhci_td, list);
  1275. td->status |= __cpu_to_le32(TD_CTRL_IOC);
  1276. }
  1277. uhci_activate_qh(uhci, qh);
  1278. }
  1279. /* The queue is empty. The QH can become idle if it is fully
  1280. * unlinked. */
  1281. else if (QH_FINISHED_UNLINKING(qh))
  1282. uhci_make_qh_idle(uhci, qh);
  1283. }
  1284. /*
  1285. * Check for queues that have made some forward progress.
  1286. * Returns 0 if the queue is not Isochronous, is ACTIVE, and
  1287. * has not advanced since last examined; 1 otherwise.
  1288. *
  1289. * Early Intel controllers have a bug which causes qh->element sometimes
  1290. * not to advance when a TD completes successfully. The queue remains
  1291. * stuck on the inactive completed TD. We detect such cases and advance
  1292. * the element pointer by hand.
  1293. */
  1294. static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh)
  1295. {
  1296. struct urb_priv *urbp = NULL;
  1297. struct uhci_td *td;
  1298. int ret = 1;
  1299. unsigned status;
  1300. if (qh->type == USB_ENDPOINT_XFER_ISOC)
  1301. goto done;
  1302. /* Treat an UNLINKING queue as though it hasn't advanced.
  1303. * This is okay because reactivation will treat it as though
  1304. * it has advanced, and if it is going to become IDLE then
  1305. * this doesn't matter anyway. Furthermore it's possible
  1306. * for an UNLINKING queue not to have any URBs at all, or
  1307. * for its first URB not to have any TDs (if it was dequeued
  1308. * just as it completed). So it's not easy in any case to
  1309. * test whether such queues have advanced. */
  1310. if (qh->state != QH_STATE_ACTIVE) {
  1311. urbp = NULL;
  1312. status = 0;
  1313. } else {
  1314. urbp = list_entry(qh->queue.next, struct urb_priv, node);
  1315. td = list_entry(urbp->td_list.next, struct uhci_td, list);
  1316. status = td_status(td);
  1317. if (!(status & TD_CTRL_ACTIVE)) {
  1318. /* We're okay, the queue has advanced */
  1319. qh->wait_expired = 0;
  1320. qh->advance_jiffies = jiffies;
  1321. goto done;
  1322. }
  1323. ret = 0;
  1324. }
  1325. /* The queue hasn't advanced; check for timeout */
  1326. if (qh->wait_expired)
  1327. goto done;
  1328. if (time_after(jiffies, qh->advance_jiffies + QH_WAIT_TIMEOUT)) {
  1329. /* Detect the Intel bug and work around it */
  1330. if (qh->post_td && qh_element(qh) ==
  1331. cpu_to_le32(qh->post_td->dma_handle)) {
  1332. qh->element = qh->post_td->link;
  1333. qh->advance_jiffies = jiffies;
  1334. ret = 1;
  1335. goto done;
  1336. }
  1337. qh->wait_expired = 1;
  1338. /* If the current URB wants FSBR, unlink it temporarily
  1339. * so that we can safely set the next TD to interrupt on
  1340. * completion. That way we'll know as soon as the queue
  1341. * starts moving again. */
  1342. if (urbp && urbp->fsbr && !(status & TD_CTRL_IOC))
  1343. uhci_unlink_qh(uhci, qh);
  1344. } else {
  1345. /* Unmoving but not-yet-expired queues keep FSBR alive */
  1346. if (urbp)
  1347. uhci_urbp_wants_fsbr(uhci, urbp);
  1348. }
  1349. done:
  1350. return ret;
  1351. }
  1352. /*
  1353. * Process events in the schedule, but only in one thread at a time
  1354. */
  1355. static void uhci_scan_schedule(struct uhci_hcd *uhci)
  1356. {
  1357. int i;
  1358. struct uhci_qh *qh;
  1359. /* Don't allow re-entrant calls */
  1360. if (uhci->scan_in_progress) {
  1361. uhci->need_rescan = 1;
  1362. return;
  1363. }
  1364. uhci->scan_in_progress = 1;
  1365. rescan:
  1366. uhci->need_rescan = 0;
  1367. uhci->fsbr_is_wanted = 0;
  1368. uhci_clear_next_interrupt(uhci);
  1369. uhci_get_current_frame_number(uhci);
  1370. uhci->cur_iso_frame = uhci->frame_number;
  1371. /* Go through all the QH queues and process the URBs in each one */
  1372. for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
  1373. uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
  1374. struct uhci_qh, node);
  1375. while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
  1376. uhci->next_qh = list_entry(qh->node.next,
  1377. struct uhci_qh, node);
  1378. if (uhci_advance_check(uhci, qh)) {
  1379. uhci_scan_qh(uhci, qh);
  1380. if (qh->state == QH_STATE_ACTIVE) {
  1381. uhci_urbp_wants_fsbr(uhci,
  1382. list_entry(qh->queue.next, struct urb_priv, node));
  1383. }
  1384. }
  1385. }
  1386. }
  1387. uhci->last_iso_frame = uhci->cur_iso_frame;
  1388. if (uhci->need_rescan)
  1389. goto rescan;
  1390. uhci->scan_in_progress = 0;
  1391. if (uhci->fsbr_is_on && !uhci->fsbr_is_wanted &&
  1392. !uhci->fsbr_expiring) {
  1393. uhci->fsbr_expiring = 1;
  1394. mod_timer(&uhci->fsbr_timer, jiffies + FSBR_OFF_DELAY);
  1395. }
  1396. if (list_empty(&uhci->skel_unlink_qh->node))
  1397. uhci_clear_next_interrupt(uhci);
  1398. else
  1399. uhci_set_next_interrupt(uhci);
  1400. }