fhci-q.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Freescale QUICC Engine USB Host Controller Driver
  3. *
  4. * Copyright (c) Freescale Semicondutor, Inc. 2006.
  5. * Shlomi Gridish <gridish@freescale.com>
  6. * Jerry Huang <Chang-Ming.Huang@freescale.com>
  7. * Copyright (c) Logic Product Development, Inc. 2007
  8. * Peter Barada <peterb@logicpd.com>
  9. * Copyright (c) MontaVista Software, Inc. 2008.
  10. * Anton Vorontsov <avorontsov@ru.mvista.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/types.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/errno.h>
  21. #include <linux/list.h>
  22. #include <linux/usb.h>
  23. #include "../core/hcd.h"
  24. #include "fhci.h"
  25. /* maps the hardware error code to the USB error code */
  26. static int status_to_error(u32 status)
  27. {
  28. if (status == USB_TD_OK)
  29. return 0;
  30. else if (status & USB_TD_RX_ER_CRC)
  31. return -EILSEQ;
  32. else if (status & USB_TD_RX_ER_NONOCT)
  33. return -EPROTO;
  34. else if (status & USB_TD_RX_ER_OVERUN)
  35. return -ECOMM;
  36. else if (status & USB_TD_RX_ER_BITSTUFF)
  37. return -EPROTO;
  38. else if (status & USB_TD_RX_ER_PID)
  39. return -EILSEQ;
  40. else if (status & (USB_TD_TX_ER_NAK | USB_TD_TX_ER_TIMEOUT))
  41. return -ETIMEDOUT;
  42. else if (status & USB_TD_TX_ER_STALL)
  43. return -EPIPE;
  44. else if (status & USB_TD_TX_ER_UNDERUN)
  45. return -ENOSR;
  46. else if (status & USB_TD_RX_DATA_UNDERUN)
  47. return -EREMOTEIO;
  48. else if (status & USB_TD_RX_DATA_OVERUN)
  49. return -EOVERFLOW;
  50. else
  51. return -EINVAL;
  52. }
  53. void fhci_add_td_to_frame(struct fhci_time_frame *frame, struct td *td)
  54. {
  55. list_add_tail(&td->frame_lh, &frame->tds_list);
  56. }
  57. void fhci_add_tds_to_ed(struct ed *ed, struct td **td_list, int number)
  58. {
  59. int i;
  60. for (i = 0; i < number; i++) {
  61. struct td *td = td_list[i];
  62. list_add_tail(&td->node, &ed->td_list);
  63. }
  64. if (ed->td_head == NULL)
  65. ed->td_head = td_list[0];
  66. }
  67. static struct td *peek_td_from_ed(struct ed *ed)
  68. {
  69. struct td *td;
  70. if (!list_empty(&ed->td_list))
  71. td = list_entry(ed->td_list.next, struct td, node);
  72. else
  73. td = NULL;
  74. return td;
  75. }
  76. struct td *fhci_remove_td_from_frame(struct fhci_time_frame *frame)
  77. {
  78. struct td *td;
  79. if (!list_empty(&frame->tds_list)) {
  80. td = list_entry(frame->tds_list.next, struct td, frame_lh);
  81. list_del_init(frame->tds_list.next);
  82. } else
  83. td = NULL;
  84. return td;
  85. }
  86. struct td *fhci_peek_td_from_frame(struct fhci_time_frame *frame)
  87. {
  88. struct td *td;
  89. if (!list_empty(&frame->tds_list))
  90. td = list_entry(frame->tds_list.next, struct td, frame_lh);
  91. else
  92. td = NULL;
  93. return td;
  94. }
  95. struct td *fhci_remove_td_from_ed(struct ed *ed)
  96. {
  97. struct td *td;
  98. if (!list_empty(&ed->td_list)) {
  99. td = list_entry(ed->td_list.next, struct td, node);
  100. list_del_init(ed->td_list.next);
  101. /* if this TD was the ED's head, find next TD */
  102. if (!list_empty(&ed->td_list))
  103. ed->td_head = list_entry(ed->td_list.next, struct td,
  104. node);
  105. else
  106. ed->td_head = NULL;
  107. } else
  108. td = NULL;
  109. return td;
  110. }
  111. struct td *fhci_remove_td_from_done_list(struct fhci_controller_list *p_list)
  112. {
  113. struct td *td;
  114. if (!list_empty(&p_list->done_list)) {
  115. td = list_entry(p_list->done_list.next, struct td, node);
  116. list_del_init(p_list->done_list.next);
  117. } else
  118. td = NULL;
  119. return td;
  120. }
  121. void fhci_move_td_from_ed_to_done_list(struct fhci_usb *usb, struct ed *ed)
  122. {
  123. struct td *td;
  124. td = ed->td_head;
  125. list_del_init(&td->node);
  126. /* If this TD was the ED's head,find next TD */
  127. if (!list_empty(&ed->td_list))
  128. ed->td_head = list_entry(ed->td_list.next, struct td, node);
  129. else {
  130. ed->td_head = NULL;
  131. ed->state = FHCI_ED_SKIP;
  132. }
  133. ed->toggle_carry = td->toggle;
  134. list_add_tail(&td->node, &usb->hc_list->done_list);
  135. if (td->ioc)
  136. usb->transfer_confirm(usb->fhci);
  137. }
  138. /* free done FHCI URB resource such as ED and TD */
  139. static void free_urb_priv(struct fhci_hcd *fhci, struct urb *urb)
  140. {
  141. int i;
  142. struct urb_priv *urb_priv = urb->hcpriv;
  143. struct ed *ed = urb_priv->ed;
  144. for (i = 0; i < urb_priv->num_of_tds; i++) {
  145. list_del_init(&urb_priv->tds[i]->node);
  146. fhci_recycle_empty_td(fhci, urb_priv->tds[i]);
  147. }
  148. /* if this TD was the ED's head,find the next TD */
  149. if (!list_empty(&ed->td_list))
  150. ed->td_head = list_entry(ed->td_list.next, struct td, node);
  151. else
  152. ed->td_head = NULL;
  153. kfree(urb_priv->tds);
  154. kfree(urb_priv);
  155. urb->hcpriv = NULL;
  156. /* if this TD was the ED's head,find next TD */
  157. if (ed->td_head == NULL)
  158. list_del_init(&ed->node);
  159. fhci->active_urbs--;
  160. }
  161. /* this routine called to complete and free done URB */
  162. void fhci_urb_complete_free(struct fhci_hcd *fhci, struct urb *urb)
  163. {
  164. free_urb_priv(fhci, urb);
  165. if (urb->status == -EINPROGRESS) {
  166. if (urb->actual_length != urb->transfer_buffer_length &&
  167. urb->transfer_flags & URB_SHORT_NOT_OK)
  168. urb->status = -EREMOTEIO;
  169. else
  170. urb->status = 0;
  171. }
  172. usb_hcd_unlink_urb_from_ep(fhci_to_hcd(fhci), urb);
  173. spin_unlock(&fhci->lock);
  174. usb_hcd_giveback_urb(fhci_to_hcd(fhci), urb, urb->status);
  175. spin_lock(&fhci->lock);
  176. }
  177. /*
  178. * caculate transfer length/stats and update the urb
  179. * Precondition: irqsafe(only for urb-?status locking)
  180. */
  181. void fhci_done_td(struct urb *urb, struct td *td)
  182. {
  183. struct ed *ed = td->ed;
  184. u32 cc = td->status;
  185. /* ISO...drivers see per-TD length/status */
  186. if (ed->mode == FHCI_TF_ISO) {
  187. u32 len;
  188. if (!(urb->transfer_flags & URB_SHORT_NOT_OK &&
  189. cc == USB_TD_RX_DATA_UNDERUN))
  190. cc = USB_TD_OK;
  191. if (usb_pipeout(urb->pipe))
  192. len = urb->iso_frame_desc[td->iso_index].length;
  193. else
  194. len = td->actual_len;
  195. urb->actual_length += len;
  196. urb->iso_frame_desc[td->iso_index].actual_length = len;
  197. urb->iso_frame_desc[td->iso_index].status =
  198. status_to_error(cc);
  199. }
  200. /* BULK,INT,CONTROL... drivers see aggregate length/status,
  201. * except that "setup" bytes aren't counted and "short" transfers
  202. * might not be reported as errors.
  203. */
  204. else {
  205. if (td->error_cnt >= 3)
  206. urb->error_count = 3;
  207. /* control endpoint only have soft stalls */
  208. /* update packet status if needed(short may be ok) */
  209. if (!(urb->transfer_flags & URB_SHORT_NOT_OK) &&
  210. cc == USB_TD_RX_DATA_UNDERUN) {
  211. ed->state = FHCI_ED_OPER;
  212. cc = USB_TD_OK;
  213. }
  214. if (cc != USB_TD_OK) {
  215. if (urb->status == -EINPROGRESS)
  216. urb->status = status_to_error(cc);
  217. }
  218. /* count all non-empty packets except control SETUP packet */
  219. if (td->type != FHCI_TA_SETUP || td->iso_index != 0)
  220. urb->actual_length += td->actual_len;
  221. }
  222. }
  223. /* there are some pedning request to unlink */
  224. void fhci_del_ed_list(struct fhci_hcd *fhci, struct ed *ed)
  225. {
  226. struct td *td = peek_td_from_ed(ed);
  227. struct urb *urb = td->urb;
  228. struct urb_priv *urb_priv = urb->hcpriv;
  229. if (urb_priv->state == URB_DEL) {
  230. td = fhci_remove_td_from_ed(ed);
  231. /* HC may have partly processed this TD */
  232. if (td->status != USB_TD_INPROGRESS)
  233. fhci_done_td(urb, td);
  234. /* URB is done;clean up */
  235. if (++(urb_priv->tds_cnt) == urb_priv->num_of_tds)
  236. fhci_urb_complete_free(fhci, urb);
  237. }
  238. }