uhci-q.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  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 Alan Stern, stern@rowland.harvard.edu
  17. */
  18. static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
  19. static void uhci_unlink_generic(struct uhci_hcd *uhci, struct urb *urb);
  20. static void uhci_remove_pending_urbps(struct uhci_hcd *uhci);
  21. static void uhci_free_pending_qhs(struct uhci_hcd *uhci);
  22. static void uhci_free_pending_tds(struct uhci_hcd *uhci);
  23. /*
  24. * Technically, updating td->status here is a race, but it's not really a
  25. * problem. The worst that can happen is that we set the IOC bit again
  26. * generating a spurious interrupt. We could fix this by creating another
  27. * QH and leaving the IOC bit always set, but then we would have to play
  28. * games with the FSBR code to make sure we get the correct order in all
  29. * the cases. I don't think it's worth the effort
  30. */
  31. static inline void uhci_set_next_interrupt(struct uhci_hcd *uhci)
  32. {
  33. if (uhci->is_stopped)
  34. mod_timer(&uhci->stall_timer, jiffies);
  35. uhci->term_td->status |= cpu_to_le32(TD_CTRL_IOC);
  36. }
  37. static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci)
  38. {
  39. uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);
  40. }
  41. static inline void uhci_moveto_complete(struct uhci_hcd *uhci,
  42. struct urb_priv *urbp)
  43. {
  44. list_move_tail(&urbp->urb_list, &uhci->complete_list);
  45. }
  46. static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci, struct usb_device *dev)
  47. {
  48. dma_addr_t dma_handle;
  49. struct uhci_td *td;
  50. td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle);
  51. if (!td)
  52. return NULL;
  53. td->dma_handle = dma_handle;
  54. td->link = UHCI_PTR_TERM;
  55. td->buffer = 0;
  56. td->frame = -1;
  57. td->dev = dev;
  58. INIT_LIST_HEAD(&td->list);
  59. INIT_LIST_HEAD(&td->remove_list);
  60. INIT_LIST_HEAD(&td->fl_list);
  61. usb_get_dev(dev);
  62. return td;
  63. }
  64. static inline void uhci_fill_td(struct uhci_td *td, u32 status,
  65. u32 token, u32 buffer)
  66. {
  67. td->status = cpu_to_le32(status);
  68. td->token = cpu_to_le32(token);
  69. td->buffer = cpu_to_le32(buffer);
  70. }
  71. /*
  72. * We insert Isochronous URB's directly into the frame list at the beginning
  73. */
  74. static void uhci_insert_td_frame_list(struct uhci_hcd *uhci, struct uhci_td *td, unsigned framenum)
  75. {
  76. framenum &= (UHCI_NUMFRAMES - 1);
  77. td->frame = framenum;
  78. /* Is there a TD already mapped there? */
  79. if (uhci->fl->frame_cpu[framenum]) {
  80. struct uhci_td *ftd, *ltd;
  81. ftd = uhci->fl->frame_cpu[framenum];
  82. ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
  83. list_add_tail(&td->fl_list, &ftd->fl_list);
  84. td->link = ltd->link;
  85. wmb();
  86. ltd->link = cpu_to_le32(td->dma_handle);
  87. } else {
  88. td->link = uhci->fl->frame[framenum];
  89. wmb();
  90. uhci->fl->frame[framenum] = cpu_to_le32(td->dma_handle);
  91. uhci->fl->frame_cpu[framenum] = td;
  92. }
  93. }
  94. static void uhci_remove_td(struct uhci_hcd *uhci, struct uhci_td *td)
  95. {
  96. /* If it's not inserted, don't remove it */
  97. if (td->frame == -1 && list_empty(&td->fl_list))
  98. return;
  99. if (td->frame != -1 && uhci->fl->frame_cpu[td->frame] == td) {
  100. if (list_empty(&td->fl_list)) {
  101. uhci->fl->frame[td->frame] = td->link;
  102. uhci->fl->frame_cpu[td->frame] = NULL;
  103. } else {
  104. struct uhci_td *ntd;
  105. ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list);
  106. uhci->fl->frame[td->frame] = cpu_to_le32(ntd->dma_handle);
  107. uhci->fl->frame_cpu[td->frame] = ntd;
  108. }
  109. } else {
  110. struct uhci_td *ptd;
  111. ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list);
  112. ptd->link = td->link;
  113. }
  114. wmb();
  115. td->link = UHCI_PTR_TERM;
  116. list_del_init(&td->fl_list);
  117. td->frame = -1;
  118. }
  119. /*
  120. * Inserts a td list into qh.
  121. */
  122. static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct urb *urb, __le32 breadth)
  123. {
  124. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  125. struct uhci_td *td;
  126. __le32 *plink;
  127. /* Ordering isn't important here yet since the QH hasn't been */
  128. /* inserted into the schedule yet */
  129. plink = &qh->element;
  130. list_for_each_entry(td, &urbp->td_list, list) {
  131. *plink = cpu_to_le32(td->dma_handle) | breadth;
  132. plink = &td->link;
  133. }
  134. *plink = UHCI_PTR_TERM;
  135. }
  136. static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
  137. {
  138. if (!list_empty(&td->list))
  139. dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
  140. if (!list_empty(&td->remove_list))
  141. dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td);
  142. if (!list_empty(&td->fl_list))
  143. dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
  144. if (td->dev)
  145. usb_put_dev(td->dev);
  146. dma_pool_free(uhci->td_pool, td, td->dma_handle);
  147. }
  148. static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci, struct usb_device *dev)
  149. {
  150. dma_addr_t dma_handle;
  151. struct uhci_qh *qh;
  152. qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle);
  153. if (!qh)
  154. return NULL;
  155. qh->dma_handle = dma_handle;
  156. qh->element = UHCI_PTR_TERM;
  157. qh->link = UHCI_PTR_TERM;
  158. qh->dev = dev;
  159. qh->urbp = NULL;
  160. INIT_LIST_HEAD(&qh->list);
  161. INIT_LIST_HEAD(&qh->remove_list);
  162. usb_get_dev(dev);
  163. return qh;
  164. }
  165. static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
  166. {
  167. if (!list_empty(&qh->list))
  168. dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
  169. if (!list_empty(&qh->remove_list))
  170. dev_warn(uhci_dev(uhci), "qh %p still in remove_list!\n", qh);
  171. if (qh->dev)
  172. usb_put_dev(qh->dev);
  173. dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
  174. }
  175. /*
  176. * Append this urb's qh after the last qh in skelqh->list
  177. *
  178. * Note that urb_priv.queue_list doesn't have a separate queue head;
  179. * it's a ring with every element "live".
  180. */
  181. static void uhci_insert_qh(struct uhci_hcd *uhci, struct uhci_qh *skelqh, struct urb *urb)
  182. {
  183. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  184. struct urb_priv *turbp;
  185. struct uhci_qh *lqh;
  186. /* Grab the last QH */
  187. lqh = list_entry(skelqh->list.prev, struct uhci_qh, list);
  188. /* Point to the next skelqh */
  189. urbp->qh->link = lqh->link;
  190. wmb(); /* Ordering is important */
  191. /*
  192. * Patch QHs for previous endpoint's queued URBs? HC goes
  193. * here next, not to the next skelqh it now points to.
  194. *
  195. * lqh --> td ... --> qh ... --> td --> qh ... --> td
  196. * | | |
  197. * v v v
  198. * +<----------------+-----------------+
  199. * v
  200. * newqh --> td ... --> td
  201. * |
  202. * v
  203. * ...
  204. *
  205. * The HC could see (and use!) any of these as we write them.
  206. */
  207. lqh->link = cpu_to_le32(urbp->qh->dma_handle) | UHCI_PTR_QH;
  208. if (lqh->urbp) {
  209. list_for_each_entry(turbp, &lqh->urbp->queue_list, queue_list)
  210. turbp->qh->link = lqh->link;
  211. }
  212. list_add_tail(&urbp->qh->list, &skelqh->list);
  213. }
  214. /*
  215. * Start removal of QH from schedule; it finishes next frame.
  216. * TDs should be unlinked before this is called.
  217. */
  218. static void uhci_remove_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
  219. {
  220. struct uhci_qh *pqh;
  221. __le32 newlink;
  222. if (!qh)
  223. return;
  224. /*
  225. * Only go through the hoops if it's actually linked in
  226. */
  227. if (!list_empty(&qh->list)) {
  228. /* If our queue is nonempty, make the next URB the head */
  229. if (!list_empty(&qh->urbp->queue_list)) {
  230. struct urb_priv *nurbp;
  231. nurbp = list_entry(qh->urbp->queue_list.next,
  232. struct urb_priv, queue_list);
  233. nurbp->queued = 0;
  234. list_add(&nurbp->qh->list, &qh->list);
  235. newlink = cpu_to_le32(nurbp->qh->dma_handle) | UHCI_PTR_QH;
  236. } else
  237. newlink = qh->link;
  238. /* Fix up the previous QH's queue to link to either
  239. * the new head of this queue or the start of the
  240. * next endpoint's queue. */
  241. pqh = list_entry(qh->list.prev, struct uhci_qh, list);
  242. pqh->link = newlink;
  243. if (pqh->urbp) {
  244. struct urb_priv *turbp;
  245. list_for_each_entry(turbp, &pqh->urbp->queue_list,
  246. queue_list)
  247. turbp->qh->link = newlink;
  248. }
  249. wmb();
  250. /* Leave qh->link in case the HC is on the QH now, it will */
  251. /* continue the rest of the schedule */
  252. qh->element = UHCI_PTR_TERM;
  253. list_del_init(&qh->list);
  254. }
  255. list_del_init(&qh->urbp->queue_list);
  256. qh->urbp = NULL;
  257. uhci_get_current_frame_number(uhci);
  258. if (uhci->frame_number + uhci->is_stopped != uhci->qh_remove_age) {
  259. uhci_free_pending_qhs(uhci);
  260. uhci->qh_remove_age = uhci->frame_number;
  261. }
  262. /* Check to see if the remove list is empty. Set the IOC bit */
  263. /* to force an interrupt so we can remove the QH */
  264. if (list_empty(&uhci->qh_remove_list))
  265. uhci_set_next_interrupt(uhci);
  266. list_add(&qh->remove_list, &uhci->qh_remove_list);
  267. }
  268. static int uhci_fixup_toggle(struct urb *urb, unsigned int toggle)
  269. {
  270. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  271. struct uhci_td *td;
  272. list_for_each_entry(td, &urbp->td_list, list) {
  273. if (toggle)
  274. td->token |= cpu_to_le32(TD_TOKEN_TOGGLE);
  275. else
  276. td->token &= ~cpu_to_le32(TD_TOKEN_TOGGLE);
  277. toggle ^= 1;
  278. }
  279. return toggle;
  280. }
  281. /* This function will append one URB's QH to another URB's QH. This is for */
  282. /* queuing interrupt, control or bulk transfers */
  283. static void uhci_append_queued_urb(struct uhci_hcd *uhci, struct urb *eurb, struct urb *urb)
  284. {
  285. struct urb_priv *eurbp, *urbp, *furbp, *lurbp;
  286. struct uhci_td *lltd;
  287. eurbp = eurb->hcpriv;
  288. urbp = urb->hcpriv;
  289. /* Find the first URB in the queue */
  290. furbp = eurbp;
  291. if (eurbp->queued) {
  292. list_for_each_entry(furbp, &eurbp->queue_list, queue_list)
  293. if (!furbp->queued)
  294. break;
  295. }
  296. lurbp = list_entry(furbp->queue_list.prev, struct urb_priv, queue_list);
  297. lltd = list_entry(lurbp->td_list.prev, struct uhci_td, list);
  298. /* Control transfers always start with toggle 0 */
  299. if (!usb_pipecontrol(urb->pipe))
  300. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  301. usb_pipeout(urb->pipe),
  302. uhci_fixup_toggle(urb,
  303. uhci_toggle(td_token(lltd)) ^ 1));
  304. /* All qh's in the queue need to link to the next queue */
  305. urbp->qh->link = eurbp->qh->link;
  306. wmb(); /* Make sure we flush everything */
  307. lltd->link = cpu_to_le32(urbp->qh->dma_handle) | UHCI_PTR_QH;
  308. list_add_tail(&urbp->queue_list, &furbp->queue_list);
  309. urbp->queued = 1;
  310. }
  311. static void uhci_delete_queued_urb(struct uhci_hcd *uhci, struct urb *urb)
  312. {
  313. struct urb_priv *urbp, *nurbp, *purbp, *turbp;
  314. struct uhci_td *pltd;
  315. unsigned int toggle;
  316. urbp = urb->hcpriv;
  317. if (list_empty(&urbp->queue_list))
  318. return;
  319. nurbp = list_entry(urbp->queue_list.next, struct urb_priv, queue_list);
  320. /*
  321. * Fix up the toggle for the following URBs in the queue.
  322. * Only needed for bulk and interrupt: control and isochronous
  323. * endpoints don't propagate toggles between messages.
  324. */
  325. if (usb_pipebulk(urb->pipe) || usb_pipeint(urb->pipe)) {
  326. if (!urbp->queued)
  327. /* We just set the toggle in uhci_unlink_generic */
  328. toggle = usb_gettoggle(urb->dev,
  329. usb_pipeendpoint(urb->pipe),
  330. usb_pipeout(urb->pipe));
  331. else {
  332. /* If we're in the middle of the queue, grab the */
  333. /* toggle from the TD previous to us */
  334. purbp = list_entry(urbp->queue_list.prev,
  335. struct urb_priv, queue_list);
  336. pltd = list_entry(purbp->td_list.prev,
  337. struct uhci_td, list);
  338. toggle = uhci_toggle(td_token(pltd)) ^ 1;
  339. }
  340. list_for_each_entry(turbp, &urbp->queue_list, queue_list) {
  341. if (!turbp->queued)
  342. break;
  343. toggle = uhci_fixup_toggle(turbp->urb, toggle);
  344. }
  345. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  346. usb_pipeout(urb->pipe), toggle);
  347. }
  348. if (urbp->queued) {
  349. /* We're somewhere in the middle (or end). The case where
  350. * we're at the head is handled in uhci_remove_qh(). */
  351. purbp = list_entry(urbp->queue_list.prev, struct urb_priv,
  352. queue_list);
  353. pltd = list_entry(purbp->td_list.prev, struct uhci_td, list);
  354. if (nurbp->queued)
  355. pltd->link = cpu_to_le32(nurbp->qh->dma_handle) | UHCI_PTR_QH;
  356. else
  357. /* The next URB happens to be the beginning, so */
  358. /* we're the last, end the chain */
  359. pltd->link = UHCI_PTR_TERM;
  360. }
  361. /* urbp->queue_list is handled in uhci_remove_qh() */
  362. }
  363. static struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci, struct urb *urb)
  364. {
  365. struct urb_priv *urbp;
  366. urbp = kmem_cache_alloc(uhci_up_cachep, SLAB_ATOMIC);
  367. if (!urbp)
  368. return NULL;
  369. memset((void *)urbp, 0, sizeof(*urbp));
  370. urbp->inserttime = jiffies;
  371. urbp->fsbrtime = jiffies;
  372. urbp->urb = urb;
  373. INIT_LIST_HEAD(&urbp->td_list);
  374. INIT_LIST_HEAD(&urbp->queue_list);
  375. INIT_LIST_HEAD(&urbp->urb_list);
  376. list_add_tail(&urbp->urb_list, &uhci->urb_list);
  377. urb->hcpriv = urbp;
  378. return urbp;
  379. }
  380. static void uhci_add_td_to_urb(struct urb *urb, struct uhci_td *td)
  381. {
  382. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  383. td->urb = urb;
  384. list_add_tail(&td->list, &urbp->td_list);
  385. }
  386. static void uhci_remove_td_from_urb(struct uhci_td *td)
  387. {
  388. if (list_empty(&td->list))
  389. return;
  390. list_del_init(&td->list);
  391. td->urb = NULL;
  392. }
  393. static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb)
  394. {
  395. struct uhci_td *td, *tmp;
  396. struct urb_priv *urbp;
  397. urbp = (struct urb_priv *)urb->hcpriv;
  398. if (!urbp)
  399. return;
  400. if (!list_empty(&urbp->urb_list))
  401. dev_warn(uhci_dev(uhci), "urb %p still on uhci->urb_list "
  402. "or uhci->remove_list!\n", urb);
  403. uhci_get_current_frame_number(uhci);
  404. if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) {
  405. uhci_free_pending_tds(uhci);
  406. uhci->td_remove_age = uhci->frame_number;
  407. }
  408. /* Check to see if the remove list is empty. Set the IOC bit */
  409. /* to force an interrupt so we can remove the TD's*/
  410. if (list_empty(&uhci->td_remove_list))
  411. uhci_set_next_interrupt(uhci);
  412. list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
  413. uhci_remove_td_from_urb(td);
  414. uhci_remove_td(uhci, td);
  415. list_add(&td->remove_list, &uhci->td_remove_list);
  416. }
  417. urb->hcpriv = NULL;
  418. kmem_cache_free(uhci_up_cachep, urbp);
  419. }
  420. static void uhci_inc_fsbr(struct uhci_hcd *uhci, struct urb *urb)
  421. {
  422. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  423. if ((!(urb->transfer_flags & URB_NO_FSBR)) && !urbp->fsbr) {
  424. urbp->fsbr = 1;
  425. if (!uhci->fsbr++ && !uhci->fsbrtimeout)
  426. uhci->skel_term_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
  427. }
  428. }
  429. static void uhci_dec_fsbr(struct uhci_hcd *uhci, struct urb *urb)
  430. {
  431. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  432. if ((!(urb->transfer_flags & URB_NO_FSBR)) && urbp->fsbr) {
  433. urbp->fsbr = 0;
  434. if (!--uhci->fsbr)
  435. uhci->fsbrtimeout = jiffies + FSBR_DELAY;
  436. }
  437. }
  438. /*
  439. * Map status to standard result codes
  440. *
  441. * <status> is (td_status(td) & 0xF60000), a.k.a.
  442. * uhci_status_bits(td_status(td)).
  443. * Note: <status> does not include the TD_CTRL_NAK bit.
  444. * <dir_out> is True for output TDs and False for input TDs.
  445. */
  446. static int uhci_map_status(int status, int dir_out)
  447. {
  448. if (!status)
  449. return 0;
  450. if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
  451. return -EPROTO;
  452. if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
  453. if (dir_out)
  454. return -EPROTO;
  455. else
  456. return -EILSEQ;
  457. }
  458. if (status & TD_CTRL_BABBLE) /* Babble */
  459. return -EOVERFLOW;
  460. if (status & TD_CTRL_DBUFERR) /* Buffer error */
  461. return -ENOSR;
  462. if (status & TD_CTRL_STALLED) /* Stalled */
  463. return -EPIPE;
  464. WARN_ON(status & TD_CTRL_ACTIVE); /* Active */
  465. return 0;
  466. }
  467. /*
  468. * Control transfers
  469. */
  470. static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb)
  471. {
  472. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  473. struct uhci_td *td;
  474. struct uhci_qh *qh, *skelqh;
  475. unsigned long destination, status;
  476. int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
  477. int len = urb->transfer_buffer_length;
  478. dma_addr_t data = urb->transfer_dma;
  479. /* The "pipe" thing contains the destination in bits 8--18 */
  480. destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
  481. /* 3 errors */
  482. status = TD_CTRL_ACTIVE | uhci_maxerr(3);
  483. if (urb->dev->speed == USB_SPEED_LOW)
  484. status |= TD_CTRL_LS;
  485. /*
  486. * Build the TD for the control request setup packet
  487. */
  488. td = uhci_alloc_td(uhci, urb->dev);
  489. if (!td)
  490. return -ENOMEM;
  491. uhci_add_td_to_urb(urb, td);
  492. uhci_fill_td(td, status, destination | uhci_explen(7),
  493. urb->setup_dma);
  494. /*
  495. * If direction is "send", change the packet ID from SETUP (0x2D)
  496. * to OUT (0xE1). Else change it from SETUP to IN (0x69) and
  497. * set Short Packet Detect (SPD) for all data packets.
  498. */
  499. if (usb_pipeout(urb->pipe))
  500. destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
  501. else {
  502. destination ^= (USB_PID_SETUP ^ USB_PID_IN);
  503. status |= TD_CTRL_SPD;
  504. }
  505. /*
  506. * Build the DATA TD's
  507. */
  508. while (len > 0) {
  509. int pktsze = len;
  510. if (pktsze > maxsze)
  511. pktsze = maxsze;
  512. td = uhci_alloc_td(uhci, urb->dev);
  513. if (!td)
  514. return -ENOMEM;
  515. /* Alternate Data0/1 (start with Data1) */
  516. destination ^= TD_TOKEN_TOGGLE;
  517. uhci_add_td_to_urb(urb, td);
  518. uhci_fill_td(td, status, destination | uhci_explen(pktsze - 1),
  519. data);
  520. data += pktsze;
  521. len -= pktsze;
  522. }
  523. /*
  524. * Build the final TD for control status
  525. */
  526. td = uhci_alloc_td(uhci, urb->dev);
  527. if (!td)
  528. return -ENOMEM;
  529. /*
  530. * It's IN if the pipe is an output pipe or we're not expecting
  531. * data back.
  532. */
  533. destination &= ~TD_TOKEN_PID_MASK;
  534. if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
  535. destination |= USB_PID_IN;
  536. else
  537. destination |= USB_PID_OUT;
  538. destination |= TD_TOKEN_TOGGLE; /* End in Data1 */
  539. status &= ~TD_CTRL_SPD;
  540. uhci_add_td_to_urb(urb, td);
  541. uhci_fill_td(td, status | TD_CTRL_IOC,
  542. destination | uhci_explen(UHCI_NULL_DATA_SIZE), 0);
  543. qh = uhci_alloc_qh(uhci, urb->dev);
  544. if (!qh)
  545. return -ENOMEM;
  546. urbp->qh = qh;
  547. qh->urbp = urbp;
  548. uhci_insert_tds_in_qh(qh, urb, UHCI_PTR_BREADTH);
  549. /* Low-speed transfers get a different queue, and won't hog the bus.
  550. * Also, some devices enumerate better without FSBR; the easiest way
  551. * to do that is to put URBs on the low-speed queue while the device
  552. * is in the DEFAULT state. */
  553. if (urb->dev->speed == USB_SPEED_LOW ||
  554. urb->dev->state == USB_STATE_DEFAULT)
  555. skelqh = uhci->skel_ls_control_qh;
  556. else {
  557. skelqh = uhci->skel_fs_control_qh;
  558. uhci_inc_fsbr(uhci, urb);
  559. }
  560. if (eurb)
  561. uhci_append_queued_urb(uhci, eurb, urb);
  562. else
  563. uhci_insert_qh(uhci, skelqh, urb);
  564. return -EINPROGRESS;
  565. }
  566. /*
  567. * If control-IN transfer was short, the status packet wasn't sent.
  568. * This routine changes the element pointer in the QH to point at the
  569. * status TD. It's safe to do this even while the QH is live, because
  570. * the hardware only updates the element pointer following a successful
  571. * transfer. The inactive TD for the short packet won't cause an update,
  572. * so the pointer won't get overwritten. The next time the controller
  573. * sees this QH, it will send the status packet.
  574. */
  575. static int usb_control_retrigger_status(struct uhci_hcd *uhci, struct urb *urb)
  576. {
  577. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  578. struct uhci_td *td;
  579. urbp->short_control_packet = 1;
  580. td = list_entry(urbp->td_list.prev, struct uhci_td, list);
  581. urbp->qh->element = cpu_to_le32(td->dma_handle);
  582. return -EINPROGRESS;
  583. }
  584. static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
  585. {
  586. struct list_head *tmp, *head;
  587. struct urb_priv *urbp = urb->hcpriv;
  588. struct uhci_td *td;
  589. unsigned int status;
  590. int ret = 0;
  591. if (list_empty(&urbp->td_list))
  592. return -EINVAL;
  593. head = &urbp->td_list;
  594. if (urbp->short_control_packet) {
  595. tmp = head->prev;
  596. goto status_stage;
  597. }
  598. tmp = head->next;
  599. td = list_entry(tmp, struct uhci_td, list);
  600. /* The first TD is the SETUP stage, check the status, but skip */
  601. /* the count */
  602. status = uhci_status_bits(td_status(td));
  603. if (status & TD_CTRL_ACTIVE)
  604. return -EINPROGRESS;
  605. if (status)
  606. goto td_error;
  607. urb->actual_length = 0;
  608. /* The rest of the TD's (but the last) are data */
  609. tmp = tmp->next;
  610. while (tmp != head && tmp->next != head) {
  611. unsigned int ctrlstat;
  612. td = list_entry(tmp, struct uhci_td, list);
  613. tmp = tmp->next;
  614. ctrlstat = td_status(td);
  615. status = uhci_status_bits(ctrlstat);
  616. if (status & TD_CTRL_ACTIVE)
  617. return -EINPROGRESS;
  618. urb->actual_length += uhci_actual_length(ctrlstat);
  619. if (status)
  620. goto td_error;
  621. /* Check to see if we received a short packet */
  622. if (uhci_actual_length(ctrlstat) <
  623. uhci_expected_length(td_token(td))) {
  624. if (urb->transfer_flags & URB_SHORT_NOT_OK) {
  625. ret = -EREMOTEIO;
  626. goto err;
  627. }
  628. if (uhci_packetid(td_token(td)) == USB_PID_IN)
  629. return usb_control_retrigger_status(uhci, urb);
  630. else
  631. return 0;
  632. }
  633. }
  634. status_stage:
  635. td = list_entry(tmp, struct uhci_td, list);
  636. /* Control status stage */
  637. status = td_status(td);
  638. #ifdef I_HAVE_BUGGY_APC_BACKUPS
  639. /* APC BackUPS Pro kludge */
  640. /* It tries to send all of the descriptor instead of the amount */
  641. /* we requested */
  642. if (status & TD_CTRL_IOC && /* IOC is masked out by uhci_status_bits */
  643. status & TD_CTRL_ACTIVE &&
  644. status & TD_CTRL_NAK)
  645. return 0;
  646. #endif
  647. status = uhci_status_bits(status);
  648. if (status & TD_CTRL_ACTIVE)
  649. return -EINPROGRESS;
  650. if (status)
  651. goto td_error;
  652. return 0;
  653. td_error:
  654. ret = uhci_map_status(status, uhci_packetout(td_token(td)));
  655. err:
  656. if ((debug == 1 && ret != -EPIPE) || debug > 1) {
  657. /* Some debugging code */
  658. dev_dbg(uhci_dev(uhci), "%s: failed with status %x\n",
  659. __FUNCTION__, status);
  660. if (errbuf) {
  661. /* Print the chain for debugging purposes */
  662. uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
  663. lprintk(errbuf);
  664. }
  665. }
  666. return ret;
  667. }
  668. /*
  669. * Common submit for bulk and interrupt
  670. */
  671. static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb, struct uhci_qh *skelqh)
  672. {
  673. struct uhci_td *td;
  674. struct uhci_qh *qh;
  675. unsigned long destination, status;
  676. int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
  677. int len = urb->transfer_buffer_length;
  678. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  679. dma_addr_t data = urb->transfer_dma;
  680. if (len < 0)
  681. return -EINVAL;
  682. /* The "pipe" thing contains the destination in bits 8--18 */
  683. destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
  684. status = uhci_maxerr(3) | TD_CTRL_ACTIVE;
  685. if (urb->dev->speed == USB_SPEED_LOW)
  686. status |= TD_CTRL_LS;
  687. if (usb_pipein(urb->pipe))
  688. status |= TD_CTRL_SPD;
  689. /*
  690. * Build the DATA TD's
  691. */
  692. do { /* Allow zero length packets */
  693. int pktsze = maxsze;
  694. if (pktsze >= len) {
  695. pktsze = len;
  696. if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
  697. status &= ~TD_CTRL_SPD;
  698. }
  699. td = uhci_alloc_td(uhci, urb->dev);
  700. if (!td)
  701. return -ENOMEM;
  702. uhci_add_td_to_urb(urb, td);
  703. uhci_fill_td(td, status, destination | uhci_explen(pktsze - 1) |
  704. (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  705. usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE_SHIFT),
  706. data);
  707. data += pktsze;
  708. len -= maxsze;
  709. usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  710. usb_pipeout(urb->pipe));
  711. } while (len > 0);
  712. /*
  713. * URB_ZERO_PACKET means adding a 0-length packet, if direction
  714. * is OUT and the transfer_length was an exact multiple of maxsze,
  715. * hence (len = transfer_length - N * maxsze) == 0
  716. * however, if transfer_length == 0, the zero packet was already
  717. * prepared above.
  718. */
  719. if (usb_pipeout(urb->pipe) && (urb->transfer_flags & URB_ZERO_PACKET) &&
  720. !len && urb->transfer_buffer_length) {
  721. td = uhci_alloc_td(uhci, urb->dev);
  722. if (!td)
  723. return -ENOMEM;
  724. uhci_add_td_to_urb(urb, td);
  725. uhci_fill_td(td, status, destination | uhci_explen(UHCI_NULL_DATA_SIZE) |
  726. (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  727. usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE_SHIFT),
  728. data);
  729. usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  730. usb_pipeout(urb->pipe));
  731. }
  732. /* Set the interrupt-on-completion flag on the last packet.
  733. * A more-or-less typical 4 KB URB (= size of one memory page)
  734. * will require about 3 ms to transfer; that's a little on the
  735. * fast side but not enough to justify delaying an interrupt
  736. * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
  737. * flag setting. */
  738. td->status |= cpu_to_le32(TD_CTRL_IOC);
  739. qh = uhci_alloc_qh(uhci, urb->dev);
  740. if (!qh)
  741. return -ENOMEM;
  742. urbp->qh = qh;
  743. qh->urbp = urbp;
  744. /* Always breadth first */
  745. uhci_insert_tds_in_qh(qh, urb, UHCI_PTR_BREADTH);
  746. if (eurb)
  747. uhci_append_queued_urb(uhci, eurb, urb);
  748. else
  749. uhci_insert_qh(uhci, skelqh, urb);
  750. return -EINPROGRESS;
  751. }
  752. /*
  753. * Common result for bulk and interrupt
  754. */
  755. static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
  756. {
  757. struct urb_priv *urbp = urb->hcpriv;
  758. struct uhci_td *td;
  759. unsigned int status = 0;
  760. int ret = 0;
  761. urb->actual_length = 0;
  762. list_for_each_entry(td, &urbp->td_list, list) {
  763. unsigned int ctrlstat = td_status(td);
  764. status = uhci_status_bits(ctrlstat);
  765. if (status & TD_CTRL_ACTIVE)
  766. return -EINPROGRESS;
  767. urb->actual_length += uhci_actual_length(ctrlstat);
  768. if (status)
  769. goto td_error;
  770. if (uhci_actual_length(ctrlstat) <
  771. uhci_expected_length(td_token(td))) {
  772. if (urb->transfer_flags & URB_SHORT_NOT_OK) {
  773. ret = -EREMOTEIO;
  774. goto err;
  775. } else
  776. return 0;
  777. }
  778. }
  779. return 0;
  780. td_error:
  781. ret = uhci_map_status(status, uhci_packetout(td_token(td)));
  782. err:
  783. /*
  784. * Enable this chunk of code if you want to see some more debugging.
  785. * But be careful, it has the tendancy to starve out khubd and prevent
  786. * disconnects from happening successfully if you have a slow debug
  787. * log interface (like a serial console.
  788. */
  789. #if 0
  790. if ((debug == 1 && ret != -EPIPE) || debug > 1) {
  791. /* Some debugging code */
  792. dev_dbg(uhci_dev(uhci), "%s: failed with status %x\n",
  793. __FUNCTION__, status);
  794. if (errbuf) {
  795. /* Print the chain for debugging purposes */
  796. uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
  797. lprintk(errbuf);
  798. }
  799. }
  800. #endif
  801. return ret;
  802. }
  803. static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb)
  804. {
  805. int ret;
  806. /* Can't have low-speed bulk transfers */
  807. if (urb->dev->speed == USB_SPEED_LOW)
  808. return -EINVAL;
  809. ret = uhci_submit_common(uhci, urb, eurb, uhci->skel_bulk_qh);
  810. if (ret == -EINPROGRESS)
  811. uhci_inc_fsbr(uhci, urb);
  812. return ret;
  813. }
  814. static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb)
  815. {
  816. /* USB 1.1 interrupt transfers only involve one packet per interval;
  817. * that's the uhci_submit_common() "breadth first" policy. Drivers
  818. * can submit urbs of any length, but longer ones might need many
  819. * intervals to complete.
  820. */
  821. return uhci_submit_common(uhci, urb, eurb, uhci->skelqh[__interval_to_skel(urb->interval)]);
  822. }
  823. /*
  824. * Isochronous transfers
  825. */
  826. static int isochronous_find_limits(struct uhci_hcd *uhci, struct urb *urb, unsigned int *start, unsigned int *end)
  827. {
  828. struct urb *last_urb = NULL;
  829. struct urb_priv *up;
  830. int ret = 0;
  831. list_for_each_entry(up, &uhci->urb_list, urb_list) {
  832. struct urb *u = up->urb;
  833. /* look for pending URB's with identical pipe handle */
  834. if ((urb->pipe == u->pipe) && (urb->dev == u->dev) &&
  835. (u->status == -EINPROGRESS) && (u != urb)) {
  836. if (!last_urb)
  837. *start = u->start_frame;
  838. last_urb = u;
  839. }
  840. }
  841. if (last_urb) {
  842. *end = (last_urb->start_frame + last_urb->number_of_packets *
  843. last_urb->interval) & (UHCI_NUMFRAMES-1);
  844. ret = 0;
  845. } else
  846. ret = -1; /* no previous urb found */
  847. return ret;
  848. }
  849. static int isochronous_find_start(struct uhci_hcd *uhci, struct urb *urb)
  850. {
  851. int limits;
  852. unsigned int start = 0, end = 0;
  853. if (urb->number_of_packets > 900) /* 900? Why? */
  854. return -EFBIG;
  855. limits = isochronous_find_limits(uhci, urb, &start, &end);
  856. if (urb->transfer_flags & URB_ISO_ASAP) {
  857. if (limits) {
  858. uhci_get_current_frame_number(uhci);
  859. urb->start_frame = (uhci->frame_number + 10)
  860. & (UHCI_NUMFRAMES - 1);
  861. } else
  862. urb->start_frame = end;
  863. } else {
  864. urb->start_frame &= (UHCI_NUMFRAMES - 1);
  865. /* FIXME: Sanity check */
  866. }
  867. return 0;
  868. }
  869. /*
  870. * Isochronous transfers
  871. */
  872. static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb)
  873. {
  874. struct uhci_td *td;
  875. int i, ret, frame;
  876. int status, destination;
  877. status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
  878. destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
  879. ret = isochronous_find_start(uhci, urb);
  880. if (ret)
  881. return ret;
  882. frame = urb->start_frame;
  883. for (i = 0; i < urb->number_of_packets; i++, frame += urb->interval) {
  884. if (!urb->iso_frame_desc[i].length)
  885. continue;
  886. td = uhci_alloc_td(uhci, urb->dev);
  887. if (!td)
  888. return -ENOMEM;
  889. uhci_add_td_to_urb(urb, td);
  890. uhci_fill_td(td, status, destination | uhci_explen(urb->iso_frame_desc[i].length - 1),
  891. urb->transfer_dma + urb->iso_frame_desc[i].offset);
  892. if (i + 1 >= urb->number_of_packets)
  893. td->status |= cpu_to_le32(TD_CTRL_IOC);
  894. uhci_insert_td_frame_list(uhci, td, frame);
  895. }
  896. return -EINPROGRESS;
  897. }
  898. static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
  899. {
  900. struct uhci_td *td;
  901. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  902. int status;
  903. int i, ret = 0;
  904. urb->actual_length = 0;
  905. i = 0;
  906. list_for_each_entry(td, &urbp->td_list, list) {
  907. int actlength;
  908. unsigned int ctrlstat = td_status(td);
  909. if (ctrlstat & TD_CTRL_ACTIVE)
  910. return -EINPROGRESS;
  911. actlength = uhci_actual_length(ctrlstat);
  912. urb->iso_frame_desc[i].actual_length = actlength;
  913. urb->actual_length += actlength;
  914. status = uhci_map_status(uhci_status_bits(ctrlstat),
  915. usb_pipeout(urb->pipe));
  916. urb->iso_frame_desc[i].status = status;
  917. if (status) {
  918. urb->error_count++;
  919. ret = status;
  920. }
  921. i++;
  922. }
  923. return ret;
  924. }
  925. static struct urb *uhci_find_urb_ep(struct uhci_hcd *uhci, struct urb *urb)
  926. {
  927. struct urb_priv *up;
  928. /* We don't match Isoc transfers since they are special */
  929. if (usb_pipeisoc(urb->pipe))
  930. return NULL;
  931. list_for_each_entry(up, &uhci->urb_list, urb_list) {
  932. struct urb *u = up->urb;
  933. if (u->dev == urb->dev && u->status == -EINPROGRESS) {
  934. /* For control, ignore the direction */
  935. if (usb_pipecontrol(urb->pipe) &&
  936. (u->pipe & ~USB_DIR_IN) == (urb->pipe & ~USB_DIR_IN))
  937. return u;
  938. else if (u->pipe == urb->pipe)
  939. return u;
  940. }
  941. }
  942. return NULL;
  943. }
  944. static int uhci_urb_enqueue(struct usb_hcd *hcd,
  945. struct usb_host_endpoint *ep,
  946. struct urb *urb, int mem_flags)
  947. {
  948. int ret;
  949. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  950. unsigned long flags;
  951. struct urb *eurb;
  952. int bustime;
  953. spin_lock_irqsave(&uhci->lock, flags);
  954. ret = urb->status;
  955. if (ret != -EINPROGRESS) /* URB already unlinked! */
  956. goto out;
  957. eurb = uhci_find_urb_ep(uhci, urb);
  958. if (!uhci_alloc_urb_priv(uhci, urb)) {
  959. ret = -ENOMEM;
  960. goto out;
  961. }
  962. switch (usb_pipetype(urb->pipe)) {
  963. case PIPE_CONTROL:
  964. ret = uhci_submit_control(uhci, urb, eurb);
  965. break;
  966. case PIPE_INTERRUPT:
  967. if (!eurb) {
  968. bustime = usb_check_bandwidth(urb->dev, urb);
  969. if (bustime < 0)
  970. ret = bustime;
  971. else {
  972. ret = uhci_submit_interrupt(uhci, urb, eurb);
  973. if (ret == -EINPROGRESS)
  974. usb_claim_bandwidth(urb->dev, urb, bustime, 0);
  975. }
  976. } else { /* inherit from parent */
  977. urb->bandwidth = eurb->bandwidth;
  978. ret = uhci_submit_interrupt(uhci, urb, eurb);
  979. }
  980. break;
  981. case PIPE_BULK:
  982. ret = uhci_submit_bulk(uhci, urb, eurb);
  983. break;
  984. case PIPE_ISOCHRONOUS:
  985. bustime = usb_check_bandwidth(urb->dev, urb);
  986. if (bustime < 0) {
  987. ret = bustime;
  988. break;
  989. }
  990. ret = uhci_submit_isochronous(uhci, urb);
  991. if (ret == -EINPROGRESS)
  992. usb_claim_bandwidth(urb->dev, urb, bustime, 1);
  993. break;
  994. }
  995. if (ret != -EINPROGRESS) {
  996. /* Submit failed, so delete it from the urb_list */
  997. struct urb_priv *urbp = urb->hcpriv;
  998. list_del_init(&urbp->urb_list);
  999. uhci_destroy_urb_priv(uhci, urb);
  1000. } else
  1001. ret = 0;
  1002. out:
  1003. spin_unlock_irqrestore(&uhci->lock, flags);
  1004. return ret;
  1005. }
  1006. /*
  1007. * Return the result of a transfer
  1008. */
  1009. static void uhci_transfer_result(struct uhci_hcd *uhci, struct urb *urb)
  1010. {
  1011. int ret = -EINPROGRESS;
  1012. struct urb_priv *urbp;
  1013. spin_lock(&urb->lock);
  1014. urbp = (struct urb_priv *)urb->hcpriv;
  1015. if (urb->status != -EINPROGRESS) /* URB already dequeued */
  1016. goto out;
  1017. switch (usb_pipetype(urb->pipe)) {
  1018. case PIPE_CONTROL:
  1019. ret = uhci_result_control(uhci, urb);
  1020. break;
  1021. case PIPE_BULK:
  1022. case PIPE_INTERRUPT:
  1023. ret = uhci_result_common(uhci, urb);
  1024. break;
  1025. case PIPE_ISOCHRONOUS:
  1026. ret = uhci_result_isochronous(uhci, urb);
  1027. break;
  1028. }
  1029. if (ret == -EINPROGRESS)
  1030. goto out;
  1031. urb->status = ret;
  1032. switch (usb_pipetype(urb->pipe)) {
  1033. case PIPE_CONTROL:
  1034. case PIPE_BULK:
  1035. case PIPE_ISOCHRONOUS:
  1036. /* Release bandwidth for Interrupt or Isoc. transfers */
  1037. if (urb->bandwidth)
  1038. usb_release_bandwidth(urb->dev, urb, 1);
  1039. uhci_unlink_generic(uhci, urb);
  1040. break;
  1041. case PIPE_INTERRUPT:
  1042. /* Release bandwidth for Interrupt or Isoc. transfers */
  1043. /* Make sure we don't release if we have a queued URB */
  1044. if (list_empty(&urbp->queue_list) && urb->bandwidth)
  1045. usb_release_bandwidth(urb->dev, urb, 0);
  1046. else
  1047. /* bandwidth was passed on to queued URB, */
  1048. /* so don't let usb_unlink_urb() release it */
  1049. urb->bandwidth = 0;
  1050. uhci_unlink_generic(uhci, urb);
  1051. break;
  1052. default:
  1053. dev_info(uhci_dev(uhci), "%s: unknown pipe type %d "
  1054. "for urb %p\n",
  1055. __FUNCTION__, usb_pipetype(urb->pipe), urb);
  1056. }
  1057. /* Move it from uhci->urb_list to uhci->complete_list */
  1058. uhci_moveto_complete(uhci, urbp);
  1059. out:
  1060. spin_unlock(&urb->lock);
  1061. }
  1062. static void uhci_unlink_generic(struct uhci_hcd *uhci, struct urb *urb)
  1063. {
  1064. struct list_head *head;
  1065. struct uhci_td *td;
  1066. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  1067. int prevactive = 0;
  1068. uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
  1069. /*
  1070. * Now we need to find out what the last successful toggle was
  1071. * so we can update the local data toggle for the next transfer
  1072. *
  1073. * There are 2 ways the last successful completed TD is found:
  1074. *
  1075. * 1) The TD is NOT active and the actual length < expected length
  1076. * 2) The TD is NOT active and it's the last TD in the chain
  1077. *
  1078. * and a third way the first uncompleted TD is found:
  1079. *
  1080. * 3) The TD is active and the previous TD is NOT active
  1081. *
  1082. * Control and Isochronous ignore the toggle, so this is safe
  1083. * for all types
  1084. *
  1085. * FIXME: The toggle fixups won't be 100% reliable until we
  1086. * change over to using a single queue for each endpoint and
  1087. * stop the queue before unlinking.
  1088. */
  1089. head = &urbp->td_list;
  1090. list_for_each_entry(td, head, list) {
  1091. unsigned int ctrlstat = td_status(td);
  1092. if (!(ctrlstat & TD_CTRL_ACTIVE) &&
  1093. (uhci_actual_length(ctrlstat) <
  1094. uhci_expected_length(td_token(td)) ||
  1095. td->list.next == head))
  1096. usb_settoggle(urb->dev, uhci_endpoint(td_token(td)),
  1097. uhci_packetout(td_token(td)),
  1098. uhci_toggle(td_token(td)) ^ 1);
  1099. else if ((ctrlstat & TD_CTRL_ACTIVE) && !prevactive)
  1100. usb_settoggle(urb->dev, uhci_endpoint(td_token(td)),
  1101. uhci_packetout(td_token(td)),
  1102. uhci_toggle(td_token(td)));
  1103. prevactive = ctrlstat & TD_CTRL_ACTIVE;
  1104. }
  1105. uhci_delete_queued_urb(uhci, urb);
  1106. /* The interrupt loop will reclaim the QH's */
  1107. uhci_remove_qh(uhci, urbp->qh);
  1108. urbp->qh = NULL;
  1109. }
  1110. static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  1111. {
  1112. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  1113. unsigned long flags;
  1114. struct urb_priv *urbp;
  1115. spin_lock_irqsave(&uhci->lock, flags);
  1116. urbp = urb->hcpriv;
  1117. if (!urbp) /* URB was never linked! */
  1118. goto done;
  1119. list_del_init(&urbp->urb_list);
  1120. uhci_unlink_generic(uhci, urb);
  1121. uhci_get_current_frame_number(uhci);
  1122. if (uhci->frame_number + uhci->is_stopped != uhci->urb_remove_age) {
  1123. uhci_remove_pending_urbps(uhci);
  1124. uhci->urb_remove_age = uhci->frame_number;
  1125. }
  1126. /* If we're the first, set the next interrupt bit */
  1127. if (list_empty(&uhci->urb_remove_list))
  1128. uhci_set_next_interrupt(uhci);
  1129. list_add_tail(&urbp->urb_list, &uhci->urb_remove_list);
  1130. done:
  1131. spin_unlock_irqrestore(&uhci->lock, flags);
  1132. return 0;
  1133. }
  1134. static int uhci_fsbr_timeout(struct uhci_hcd *uhci, struct urb *urb)
  1135. {
  1136. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  1137. struct list_head *head;
  1138. struct uhci_td *td;
  1139. int count = 0;
  1140. uhci_dec_fsbr(uhci, urb);
  1141. urbp->fsbr_timeout = 1;
  1142. /*
  1143. * Ideally we would want to fix qh->element as well, but it's
  1144. * read/write by the HC, so that can introduce a race. It's not
  1145. * really worth the hassle
  1146. */
  1147. head = &urbp->td_list;
  1148. list_for_each_entry(td, head, list) {
  1149. /*
  1150. * Make sure we don't do the last one (since it'll have the
  1151. * TERM bit set) as well as we skip every so many TD's to
  1152. * make sure it doesn't hog the bandwidth
  1153. */
  1154. if (td->list.next != head && (count % DEPTH_INTERVAL) ==
  1155. (DEPTH_INTERVAL - 1))
  1156. td->link |= UHCI_PTR_DEPTH;
  1157. count++;
  1158. }
  1159. return 0;
  1160. }
  1161. static void uhci_free_pending_qhs(struct uhci_hcd *uhci)
  1162. {
  1163. struct uhci_qh *qh, *tmp;
  1164. list_for_each_entry_safe(qh, tmp, &uhci->qh_remove_list, remove_list) {
  1165. list_del_init(&qh->remove_list);
  1166. uhci_free_qh(uhci, qh);
  1167. }
  1168. }
  1169. static void uhci_free_pending_tds(struct uhci_hcd *uhci)
  1170. {
  1171. struct uhci_td *td, *tmp;
  1172. list_for_each_entry_safe(td, tmp, &uhci->td_remove_list, remove_list) {
  1173. list_del_init(&td->remove_list);
  1174. uhci_free_td(uhci, td);
  1175. }
  1176. }
  1177. static void
  1178. uhci_finish_urb(struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
  1179. __releases(uhci->lock)
  1180. __acquires(uhci->lock)
  1181. {
  1182. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  1183. uhci_destroy_urb_priv(uhci, urb);
  1184. spin_unlock(&uhci->lock);
  1185. usb_hcd_giveback_urb(hcd, urb, regs);
  1186. spin_lock(&uhci->lock);
  1187. }
  1188. static void uhci_finish_completion(struct uhci_hcd *uhci, struct pt_regs *regs)
  1189. {
  1190. struct urb_priv *urbp, *tmp;
  1191. list_for_each_entry_safe(urbp, tmp, &uhci->complete_list, urb_list) {
  1192. struct urb *urb = urbp->urb;
  1193. list_del_init(&urbp->urb_list);
  1194. uhci_finish_urb(uhci_to_hcd(uhci), urb, regs);
  1195. }
  1196. }
  1197. static void uhci_remove_pending_urbps(struct uhci_hcd *uhci)
  1198. {
  1199. /* Splice the urb_remove_list onto the end of the complete_list */
  1200. list_splice_init(&uhci->urb_remove_list, uhci->complete_list.prev);
  1201. }
  1202. /* Process events in the schedule, but only in one thread at a time */
  1203. static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
  1204. {
  1205. struct urb_priv *urbp, *tmp;
  1206. /* Don't allow re-entrant calls */
  1207. if (uhci->scan_in_progress) {
  1208. uhci->need_rescan = 1;
  1209. return;
  1210. }
  1211. uhci->scan_in_progress = 1;
  1212. rescan:
  1213. uhci->need_rescan = 0;
  1214. uhci_clear_next_interrupt(uhci);
  1215. uhci_get_current_frame_number(uhci);
  1216. if (uhci->frame_number + uhci->is_stopped != uhci->qh_remove_age)
  1217. uhci_free_pending_qhs(uhci);
  1218. if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age)
  1219. uhci_free_pending_tds(uhci);
  1220. if (uhci->frame_number + uhci->is_stopped != uhci->urb_remove_age)
  1221. uhci_remove_pending_urbps(uhci);
  1222. /* Walk the list of pending URBs to see which ones completed
  1223. * (must be _safe because uhci_transfer_result() dequeues URBs) */
  1224. list_for_each_entry_safe(urbp, tmp, &uhci->urb_list, urb_list) {
  1225. struct urb *urb = urbp->urb;
  1226. /* Checks the status and does all of the magic necessary */
  1227. uhci_transfer_result(uhci, urb);
  1228. }
  1229. uhci_finish_completion(uhci, regs);
  1230. /* If the controller is stopped, we can finish these off right now */
  1231. if (uhci->is_stopped) {
  1232. uhci_free_pending_qhs(uhci);
  1233. uhci_free_pending_tds(uhci);
  1234. uhci_remove_pending_urbps(uhci);
  1235. }
  1236. if (uhci->need_rescan)
  1237. goto rescan;
  1238. uhci->scan_in_progress = 0;
  1239. if (list_empty(&uhci->urb_remove_list) &&
  1240. list_empty(&uhci->td_remove_list) &&
  1241. list_empty(&uhci->qh_remove_list))
  1242. uhci_clear_next_interrupt(uhci);
  1243. else
  1244. uhci_set_next_interrupt(uhci);
  1245. /* Wake up anyone waiting for an URB to complete */
  1246. wake_up_all(&uhci->waitqh);
  1247. }
  1248. static void check_fsbr(struct uhci_hcd *uhci)
  1249. {
  1250. struct urb_priv *up;
  1251. list_for_each_entry(up, &uhci->urb_list, urb_list) {
  1252. struct urb *u = up->urb;
  1253. spin_lock(&u->lock);
  1254. /* Check if the FSBR timed out */
  1255. if (up->fsbr && !up->fsbr_timeout && time_after_eq(jiffies, up->fsbrtime + IDLE_TIMEOUT))
  1256. uhci_fsbr_timeout(uhci, u);
  1257. spin_unlock(&u->lock);
  1258. }
  1259. /* Really disable FSBR */
  1260. if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) {
  1261. uhci->fsbrtimeout = 0;
  1262. uhci->skel_term_qh->link = UHCI_PTR_TERM;
  1263. }
  1264. }