fhci-q.c 6.7 KB

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