fhci-q.c 6.9 KB

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