2 * Freescale QUICC Engine USB Host Controller Driver
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>
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.
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/spinlock.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/list.h>
24 #include <linux/usb.h>
25 #include <linux/usb/hcd.h>
28 /* maps the hardware error code to the USB error code */
29 static int status_to_error(u32 status
)
31 if (status
== USB_TD_OK
)
33 else if (status
& USB_TD_RX_ER_CRC
)
35 else if (status
& USB_TD_RX_ER_NONOCT
)
37 else if (status
& USB_TD_RX_ER_OVERUN
)
39 else if (status
& USB_TD_RX_ER_BITSTUFF
)
41 else if (status
& USB_TD_RX_ER_PID
)
43 else if (status
& (USB_TD_TX_ER_NAK
| USB_TD_TX_ER_TIMEOUT
))
45 else if (status
& USB_TD_TX_ER_STALL
)
47 else if (status
& USB_TD_TX_ER_UNDERUN
)
49 else if (status
& USB_TD_RX_DATA_UNDERUN
)
51 else if (status
& USB_TD_RX_DATA_OVERUN
)
57 void fhci_add_td_to_frame(struct fhci_time_frame
*frame
, struct td
*td
)
59 list_add_tail(&td
->frame_lh
, &frame
->tds_list
);
62 void fhci_add_tds_to_ed(struct ed
*ed
, struct td
**td_list
, int number
)
66 for (i
= 0; i
< number
; i
++) {
67 struct td
*td
= td_list
[i
];
68 list_add_tail(&td
->node
, &ed
->td_list
);
70 if (ed
->td_head
== NULL
)
71 ed
->td_head
= td_list
[0];
74 static struct td
*peek_td_from_ed(struct ed
*ed
)
78 if (!list_empty(&ed
->td_list
))
79 td
= list_entry(ed
->td_list
.next
, struct td
, node
);
86 struct td
*fhci_remove_td_from_frame(struct fhci_time_frame
*frame
)
90 if (!list_empty(&frame
->tds_list
)) {
91 td
= list_entry(frame
->tds_list
.next
, struct td
, frame_lh
);
92 list_del_init(frame
->tds_list
.next
);
99 struct td
*fhci_peek_td_from_frame(struct fhci_time_frame
*frame
)
103 if (!list_empty(&frame
->tds_list
))
104 td
= list_entry(frame
->tds_list
.next
, struct td
, frame_lh
);
111 struct td
*fhci_remove_td_from_ed(struct ed
*ed
)
115 if (!list_empty(&ed
->td_list
)) {
116 td
= list_entry(ed
->td_list
.next
, struct td
, node
);
117 list_del_init(ed
->td_list
.next
);
119 /* if this TD was the ED's head, find next TD */
120 if (!list_empty(&ed
->td_list
))
121 ed
->td_head
= list_entry(ed
->td_list
.next
, struct td
,
131 struct td
*fhci_remove_td_from_done_list(struct fhci_controller_list
*p_list
)
135 if (!list_empty(&p_list
->done_list
)) {
136 td
= list_entry(p_list
->done_list
.next
, struct td
, node
);
137 list_del_init(p_list
->done_list
.next
);
144 void fhci_move_td_from_ed_to_done_list(struct fhci_usb
*usb
, struct ed
*ed
)
149 list_del_init(&td
->node
);
151 /* If this TD was the ED's head,find next TD */
152 if (!list_empty(&ed
->td_list
))
153 ed
->td_head
= list_entry(ed
->td_list
.next
, struct td
, node
);
156 ed
->state
= FHCI_ED_SKIP
;
158 ed
->toggle_carry
= td
->toggle
;
159 list_add_tail(&td
->node
, &usb
->hc_list
->done_list
);
161 usb
->transfer_confirm(usb
->fhci
);
164 /* free done FHCI URB resource such as ED and TD */
165 static void free_urb_priv(struct fhci_hcd
*fhci
, struct urb
*urb
)
168 struct urb_priv
*urb_priv
= urb
->hcpriv
;
169 struct ed
*ed
= urb_priv
->ed
;
171 for (i
= 0; i
< urb_priv
->num_of_tds
; i
++) {
172 list_del_init(&urb_priv
->tds
[i
]->node
);
173 fhci_recycle_empty_td(fhci
, urb_priv
->tds
[i
]);
176 /* if this TD was the ED's head,find the next TD */
177 if (!list_empty(&ed
->td_list
))
178 ed
->td_head
= list_entry(ed
->td_list
.next
, struct td
, node
);
182 kfree(urb_priv
->tds
);
186 /* if this TD was the ED's head,find next TD */
187 if (ed
->td_head
== NULL
)
188 list_del_init(&ed
->node
);
192 /* this routine called to complete and free done URB */
193 void fhci_urb_complete_free(struct fhci_hcd
*fhci
, struct urb
*urb
)
195 free_urb_priv(fhci
, urb
);
197 if (urb
->status
== -EINPROGRESS
) {
198 if (urb
->actual_length
!= urb
->transfer_buffer_length
&&
199 urb
->transfer_flags
& URB_SHORT_NOT_OK
)
200 urb
->status
= -EREMOTEIO
;
205 usb_hcd_unlink_urb_from_ep(fhci_to_hcd(fhci
), urb
);
207 spin_unlock(&fhci
->lock
);
209 usb_hcd_giveback_urb(fhci_to_hcd(fhci
), urb
, urb
->status
);
211 spin_lock(&fhci
->lock
);
215 * caculate transfer length/stats and update the urb
216 * Precondition: irqsafe(only for urb-?status locking)
218 void fhci_done_td(struct urb
*urb
, struct td
*td
)
220 struct ed
*ed
= td
->ed
;
223 /* ISO...drivers see per-TD length/status */
224 if (ed
->mode
== FHCI_TF_ISO
) {
226 if (!(urb
->transfer_flags
& URB_SHORT_NOT_OK
&&
227 cc
== USB_TD_RX_DATA_UNDERUN
))
230 if (usb_pipeout(urb
->pipe
))
231 len
= urb
->iso_frame_desc
[td
->iso_index
].length
;
233 len
= td
->actual_len
;
235 urb
->actual_length
+= len
;
236 urb
->iso_frame_desc
[td
->iso_index
].actual_length
= len
;
237 urb
->iso_frame_desc
[td
->iso_index
].status
=
241 /* BULK,INT,CONTROL... drivers see aggregate length/status,
242 * except that "setup" bytes aren't counted and "short" transfers
243 * might not be reported as errors.
246 if (td
->error_cnt
>= 3)
247 urb
->error_count
= 3;
249 /* control endpoint only have soft stalls */
251 /* update packet status if needed(short may be ok) */
252 if (!(urb
->transfer_flags
& URB_SHORT_NOT_OK
) &&
253 cc
== USB_TD_RX_DATA_UNDERUN
) {
254 ed
->state
= FHCI_ED_OPER
;
257 if (cc
!= USB_TD_OK
) {
258 if (urb
->status
== -EINPROGRESS
)
259 urb
->status
= status_to_error(cc
);
262 /* count all non-empty packets except control SETUP packet */
263 if (td
->type
!= FHCI_TA_SETUP
|| td
->iso_index
!= 0)
264 urb
->actual_length
+= td
->actual_len
;
268 /* there are some pedning request to unlink */
269 void fhci_del_ed_list(struct fhci_hcd
*fhci
, struct ed
*ed
)
271 struct td
*td
= peek_td_from_ed(ed
);
272 struct urb
*urb
= td
->urb
;
273 struct urb_priv
*urb_priv
= urb
->hcpriv
;
275 if (urb_priv
->state
== URB_DEL
) {
276 td
= fhci_remove_td_from_ed(ed
);
277 /* HC may have partly processed this TD */
278 if (td
->status
!= USB_TD_INPROGRESS
)
279 fhci_done_td(urb
, td
);
281 /* URB is done;clean up */
282 if (++(urb_priv
->tds_cnt
) == urb_priv
->num_of_tds
)
283 fhci_urb_complete_free(fhci
, urb
);