uhci-q.c 36 KB

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