2eafecc4fa309913bd9e9e984e0f55b7569a4168
2 * OHCI HCD (Host Controller Driver) for USB.
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
7 * This file is licenced under the GPL.
10 #include <linux/irq.h>
12 /*-------------------------------------------------------------------------*/
15 * URB goes back to driver, and isn't reissued.
16 * It's completely gone from HC data structures.
17 * PRECONDITION: ahcd lock held, irqs blocked.
20 finish_urb(struct admhcd
*ahcd
, struct urb
*urb
)
21 __releases(ahcd
->lock
)
22 __acquires(ahcd
->lock
)
24 urb_priv_free(ahcd
, urb
->hcpriv
);
27 spin_lock(&urb
->lock
);
28 if (likely(urb
->status
== -EINPROGRESS
))
31 /* report short control reads right even though the data TD always
32 * has TD_R set. (much simpler, but creates the 1-td limit.)
34 if (unlikely(urb
->transfer_flags
& URB_SHORT_NOT_OK
)
35 && unlikely(usb_pipecontrol(urb
->pipe
))
36 && urb
->actual_length
< urb
->transfer_buffer_length
37 && usb_pipein(urb
->pipe
)
38 && urb
->status
== 0) {
39 urb
->status
= -EREMOTEIO
;
40 #ifdef ADMHC_VERBOSE_DEBUG
41 urb_print(ahcd
, urb
, "SHORT", usb_pipeout (urb
->pipe
));
44 spin_unlock(&urb
->lock
);
46 switch (usb_pipetype(urb
->pipe
)) {
47 case PIPE_ISOCHRONOUS
:
48 admhcd_to_hcd(ahcd
)->self
.bandwidth_isoc_reqs
--;
51 admhcd_to_hcd(ahcd
)->self
.bandwidth_int_reqs
--;
55 #ifdef ADMHC_VERBOSE_DEBUG
56 urb_print(ahcd
, urb
, "FINISH", 0);
59 /* urb->complete() can reenter this HCD */
60 spin_unlock(&ahcd
->lock
);
61 usb_hcd_giveback_urb(admhcd_to_hcd(ahcd
), urb
);
62 spin_lock(&ahcd
->lock
);
66 /*-------------------------------------------------------------------------*
67 * ED handling functions
68 *-------------------------------------------------------------------------*/
70 static struct ed
*ed_create(struct admhcd
*ahcd
, unsigned int type
, u32 info
)
75 ed
= ed_alloc(ahcd
, GFP_ATOMIC
);
79 /* dummy td; end of td list for this ed */
80 td
= td_alloc(ahcd
, GFP_ATOMIC
);
88 case PIPE_ISOCHRONOUS
:
97 ed
->hwINFO
= cpu_to_hc32(ahcd
, info
);
98 ed
->hwTailP
= cpu_to_hc32(ahcd
, td
->td_dma
);
99 ed
->hwHeadP
= cpu_to_hc32(ahcd
, td
->td_dma
);
109 /* get and maybe (re)init an endpoint. init _should_ be done only as part
110 * of enumeration, usb_set_configuration() or usb_set_interface().
112 static struct ed
*ed_get(struct admhcd
*ahcd
, struct usb_host_endpoint
*ep
,
113 struct usb_device
*udev
, unsigned int pipe
, int interval
)
118 spin_lock_irqsave(&ahcd
->lock
, flags
);
123 /* FIXME: usbcore changes dev->devnum before SET_ADDRESS
124 * suceeds ... otherwise we wouldn't need "pipe".
126 info
= usb_pipedevice(pipe
);
127 info
|= (ep
->desc
.bEndpointAddress
& ~USB_DIR_IN
) << ED_EN_SHIFT
;
128 info
|= le16_to_cpu(ep
->desc
.wMaxPacketSize
) << ED_MPS_SHIFT
;
129 if (udev
->speed
== USB_SPEED_FULL
)
130 info
|= ED_SPEED_FULL
;
132 ed
= ed_create(ahcd
, usb_pipetype(pipe
), info
);
136 spin_unlock_irqrestore(&ahcd
->lock
, flags
);
141 /* link an ed into the HC chain */
142 static int ed_schedule(struct admhcd
*ahcd
, struct ed
*ed
)
146 if (admhcd_to_hcd(ahcd
)->state
== HC_STATE_QUIESCING
)
149 if (ed
->state
!= ED_NEW
)
152 admhc_dump_ed(ahcd
, "ED-SCHED", ed
, 0);
156 admhc_dma_lock(ahcd
);
157 ed
->hwINFO
&= ~cpu_to_hc32(ahcd
, ED_SKIP
);
159 old_tail
= ahcd
->ed_tails
[ed
->type
];
161 ed
->ed_next
= old_tail
->ed_next
;
163 ed
->ed_next
->ed_prev
= ed
;
164 ed
->hwNextED
= cpu_to_hc32(ahcd
, ed
->ed_next
->dma
);
166 ed
->ed_prev
= old_tail
;
168 old_tail
->ed_next
= ed
;
169 old_tail
->hwNextED
= cpu_to_hc32(ahcd
, ed
->dma
);
171 ahcd
->ed_tails
[ed
->type
] = ed
;
172 admhc_dma_unlock(ahcd
);
174 admhc_intr_enable(ahcd
, ADMHC_INTR_SOFI
);
179 static void ed_deschedule(struct admhcd
*ahcd
, struct ed
*ed
)
181 admhc_dump_ed(ahcd
, "ED-DESCHED", ed
, 0);
183 /* remove this ED from the HC list */
184 admhc_dma_lock(ahcd
);
185 ed
->ed_prev
->hwNextED
= ed
->hwNextED
;
186 admhc_dma_unlock(ahcd
);
188 /* and remove it from our list */
189 ed
->ed_prev
->ed_next
= ed
->ed_next
;
192 ed
->ed_next
->ed_prev
= ed
->ed_prev
;
196 if (ahcd
->ed_tails
[ed
->type
] == ed
)
197 ahcd
->ed_tails
[ed
->type
] = ed
->ed_prev
;
202 static void ed_start_deschedule(struct admhcd
*ahcd
, struct ed
*ed
)
204 admhc_dump_ed(ahcd
, "ED-UNLINK", ed
, 0);
206 admhc_dma_lock(ahcd
);
207 ed
->hwINFO
|= cpu_to_hc32(ahcd
, ED_SKIP
);
208 admhc_dma_unlock(ahcd
);
210 ed
->state
= ED_UNLINK
;
212 /* add this ED into the remove list */
213 ed
->ed_rm_next
= ahcd
->ed_rm_list
;
214 ahcd
->ed_rm_list
= ed
;
216 /* SOF interrupt might get delayed; record the frame counter value that
217 * indicates when the HC isn't looking at it, so concurrent unlinks
218 * behave. frame_no wraps every 2^16 msec, and changes right before
221 ed
->tick
= admhc_frame_no(ahcd
) + 1;
223 /* enable SOF interrupt */
224 admhc_intr_enable(ahcd
, ADMHC_INTR_SOFI
);
227 /*-------------------------------------------------------------------------*
228 * TD handling functions
229 *-------------------------------------------------------------------------*/
231 static void td_fill(struct admhcd
*ahcd
, u32 info
, dma_addr_t data
, int len
,
237 if (up
->td_idx
>= up
->td_cnt
) {
238 admhc_dbg(ahcd
, "td_fill error, idx=%d, cnt=%d\n", up
->td_idx
,
243 td
= up
->td
[up
->td_idx
];
249 if (up
->td_idx
== up
->td_cnt
-1)
254 cbl
|= (len
& TD_BL_MASK
);
258 /* setup hardware specific fields */
259 td
->hwINFO
= cpu_to_hc32(ahcd
, info
);
260 td
->hwDBP
= cpu_to_hc32(ahcd
, data
);
261 td
->hwCBL
= cpu_to_hc32(ahcd
, cbl
);
264 up
->td
[up
->td_idx
-1]->hwNextTD
= cpu_to_hc32(ahcd
, td
->td_dma
);
269 /*-------------------------------------------------------------------------*/
271 /* Prepare all TDs of a transfer, and queue them onto the ED.
272 * Caller guarantees HC is active.
273 * Usually the ED is already on the schedule, so TDs might be
274 * processed as soon as they're queued.
276 static void td_submit_urb(struct admhcd
*ahcd
, struct urb
*urb
)
278 struct urb_priv
*urb_priv
= urb
->hcpriv
;
280 int data_len
= urb
->transfer_buffer_length
;
283 int is_out
= usb_pipeout(urb
->pipe
);
286 /* OHCI handles the bulk/interrupt data toggles itself. We just
287 * use the device toggle bits for resetting, and rely on the fact
288 * that resetting toggle is meaningless if the endpoint is active.
291 if (usb_gettoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
), is_out
)) {
295 usb_settoggle(urb
->dev
, usb_pipeendpoint (urb
->pipe
),
299 urb_priv
->td_idx
= 0;
302 data
= urb
->transfer_dma
;
306 /* NOTE: TD_CC is set so we can tell which TDs the HC processed by
307 * using TD_CC_GET, as well as by seeing them on the done list.
308 * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
310 switch (urb_priv
->ed
->type
) {
313 ? TD_T_CARRY
| TD_SCC_NOTACCESSED
| TD_DP_OUT
314 : TD_T_CARRY
| TD_SCC_NOTACCESSED
| TD_DP_IN
;
316 /* setup service interval and starting frame number */
317 info
|= (urb
->start_frame
& TD_FN_MASK
);
318 info
|= (urb
->interval
& TD_ISI_MASK
) << TD_ISI_SHIFT
;
320 td_fill(ahcd
, info
, data
, data_len
, urb_priv
);
323 admhcd_to_hcd(ahcd
)->self
.bandwidth_int_reqs
++;
328 ? TD_SCC_NOTACCESSED
| TD_DP_OUT
329 : TD_SCC_NOTACCESSED
| TD_DP_IN
;
331 /* TDs _could_ transfer up to 8K each */
332 while (data_len
> TD_DATALEN_MAX
) {
333 td_fill(ahcd
, info
| ((cnt
) ? TD_T_CARRY
: toggle
),
334 data
, TD_DATALEN_MAX
, urb_priv
);
335 data
+= TD_DATALEN_MAX
;
336 data_len
-= TD_DATALEN_MAX
;
340 td_fill(ahcd
, info
| ((cnt
) ? TD_T_CARRY
: toggle
), data
,
344 if ((urb
->transfer_flags
& URB_ZERO_PACKET
)
345 && (cnt
< urb_priv
->td_cnt
)) {
346 td_fill(ahcd
, info
| ((cnt
) ? TD_T_CARRY
: toggle
),
352 /* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
353 * any DATA phase works normally, and the STATUS ack is special.
356 /* fill a TD for the setup */
357 info
= TD_SCC_NOTACCESSED
| TD_DP_SETUP
| TD_T_DATA0
;
358 td_fill(ahcd
, info
, urb
->setup_dma
, 8, urb_priv
);
362 /* fill a TD for the data */
363 info
= TD_SCC_NOTACCESSED
| TD_T_DATA1
;
364 info
|= is_out
? TD_DP_OUT
: TD_DP_IN
;
365 /* NOTE: mishandles transfers >8K, some >4K */
366 td_fill(ahcd
, info
, data
, data_len
, urb_priv
);
370 /* fill a TD for the ACK */
371 info
= (is_out
|| data_len
== 0)
372 ? TD_SCC_NOTACCESSED
| TD_DP_IN
| TD_T_DATA1
373 : TD_SCC_NOTACCESSED
| TD_DP_OUT
| TD_T_DATA1
;
374 td_fill(ahcd
, info
, data
, 0, urb_priv
);
379 /* ISO has no retransmit, so no toggle;
380 * Each TD could handle multiple consecutive frames (interval 1);
381 * we could often reduce the number of TDs here.
383 case PIPE_ISOCHRONOUS
:
384 info
= TD_SCC_NOTACCESSED
;
385 for (cnt
= 0; cnt
< urb
->number_of_packets
; cnt
++) {
386 int frame
= urb
->start_frame
;
388 frame
+= cnt
* urb
->interval
;
390 td_fill(ahcd
, info
| frame
,
391 data
+ urb
->iso_frame_desc
[cnt
].offset
,
392 urb
->iso_frame_desc
[cnt
].length
,
395 admhcd_to_hcd(ahcd
)->self
.bandwidth_isoc_reqs
++;
399 if (urb_priv
->td_cnt
!= cnt
)
400 admhc_err(ahcd
, "bad number of tds created for urb %p\n", urb
);
402 urb_priv
->td_idx
= 0;
405 /* calculate transfer length/status and update the urb
406 * PRECONDITION: irqsafe (only for urb->status locking)
408 static int td_done(struct admhcd
*ahcd
, struct urb
*urb
, struct td
*td
)
410 u32 info
= hc32_to_cpup(ahcd
, &td
->hwINFO
);
411 u32 dbp
= hc32_to_cpup(ahcd
, &td
->hwDBP
);
412 u32 cbl
= TD_BL_GET(hc32_to_cpup(ahcd
, &td
->hwCBL
));
413 int type
= usb_pipetype(urb
->pipe
);
416 cc
= TD_CC_GET(info
);
418 /* ISO ... drivers see per-TD length/status */
419 if (type
== PIPE_ISOCHRONOUS
) {
424 /* NOTE: assumes FC in tdINFO == 0, and that
425 * only the first of 0..MAXPSW psws is used.
429 if (tdINFO
& TD_CC
) /* hc didn't touch? */
432 if (usb_pipeout (urb
->pipe
))
433 dlen
= urb
->iso_frame_desc
[td
->index
].length
;
435 /* short reads are always OK for ISO */
436 if (cc
== TD_DATAUNDERRUN
)
438 dlen
= tdPSW
& 0x3ff;
441 urb
->actual_length
+= dlen
;
442 urb
->iso_frame_desc
[td
->index
].actual_length
= dlen
;
443 urb
->iso_frame_desc
[td
->index
].status
= cc_to_error
[cc
];
445 if (cc
!= TD_CC_NOERROR
)
447 "urb %p iso td %p (%d) len %d cc %d\n",
448 urb
, td
, 1 + td
->index
, dlen
, cc
);
450 /* BULK, INT, CONTROL ... drivers see aggregate length/status,
451 * except that "setup" bytes aren't counted and "short" transfers
452 * might not be reported as errors.
455 admhc_dump_td(ahcd
, "td_done", td
);
457 /* count all non-empty packets except control SETUP packet */
458 if ((type
!= PIPE_CONTROL
|| td
->index
!= 0) && dbp
!= 0) {
459 urb
->actual_length
+= dbp
- td
->data_dma
+ cbl
;
466 /*-------------------------------------------------------------------------*/
468 static inline struct td
*
469 ed_halted(struct admhcd
*ahcd
, struct td
*td
, int cc
, struct td
*rev
)
472 struct urb
*urb
= td
->urb
;
473 struct ed
*ed
= td
->ed
;
474 struct list_head
*tmp
= td
->td_list
.next
;
475 __hc32 toggle
= ed
->hwHeadP
& cpu_to_hc32 (ahcd
, ED_C
);
477 admhc_dump_ed(ahcd
, "ed halted", td
->ed
, 1);
478 /* clear ed halt; this is the td that caused it, but keep it inactive
479 * until its urb->complete() has a chance to clean up.
481 ed
->hwINFO
|= cpu_to_hc32 (ahcd
, ED_SKIP
);
483 ed
->hwHeadP
&= ~cpu_to_hc32 (ahcd
, ED_H
);
485 /* put any later tds from this urb onto the donelist, after 'td',
486 * order won't matter here: no errors, and nothing was transferred.
487 * also patch the ed so it looks as if those tds completed normally.
489 while (tmp
!= &ed
->td_list
) {
493 next
= list_entry(tmp
, struct td
, td_list
);
494 tmp
= next
->td_list
.next
;
496 if (next
->urb
!= urb
)
499 /* NOTE: if multi-td control DATA segments get supported,
500 * this urb had one of them, this td wasn't the last td
501 * in that segment (TD_R clear), this ed halted because
502 * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
503 * then we need to leave the control STATUS packet queued
508 info
|= cpu_to_hc32 (ahcd
, TD_DONE
);
509 info
&= ~cpu_to_hc32 (ahcd
, TD_CC
);
513 next
->next_dl_td
= rev
;
516 ed
->hwHeadP
= next
->hwNextTD
| toggle
;
519 /* help for troubleshooting: report anything that
520 * looks odd ... that doesn't include protocol stalls
521 * (or maybe some other things)
524 case TD_CC_DATAUNDERRUN
:
525 if ((urb
->transfer_flags
& URB_SHORT_NOT_OK
) == 0)
529 if (usb_pipecontrol (urb
->pipe
))
534 "urb %p path %s ep%d%s %08x cc %d --> status %d\n",
535 urb
, urb
->dev
->devpath
,
536 usb_pipeendpoint (urb
->pipe
),
537 usb_pipein (urb
->pipe
) ? "in" : "out",
538 hc32_to_cpu(ahcd
, td
->hwINFO
),
539 cc
, cc_to_error
[cc
]);
548 /*-------------------------------------------------------------------------*/
550 static int ed_next_urb(struct admhcd
*ahcd
, struct ed
*ed
)
555 if (ed
->state
!= ED_IDLE
)
561 if (list_empty(&ed
->urb_pending
))
564 up
= list_entry(ed
->urb_pending
.next
, struct urb_priv
, pending
);
565 list_del(&up
->pending
);
569 #ifdef ADMHC_VERBOSE_DEBUG
570 urb_print(ahcd
, up
->urb
, "NEXT", 0);
571 admhc_dump_ed(ahcd
, " ", ed
, 0);
574 up
->td
[up
->td_cnt
-1]->hwNextTD
= cpu_to_hc32(ahcd
, ed
->dummy
->td_dma
);
576 admhc_dma_lock(ahcd
);
577 carry
= hc32_to_cpup(ahcd
, &ed
->hwHeadP
) & ED_C
;
578 ed
->hwHeadP
= cpu_to_hc32(ahcd
, up
->td
[0]->td_dma
| carry
);
579 ed
->hwINFO
&= ~cpu_to_hc32(ahcd
, ED_SKIP
);
580 admhc_dma_unlock(ahcd
);
585 static void ed_update(struct admhcd
*ahcd
, struct ed
*ed
, int partial
)
597 #ifdef ADMHC_VERBOSE_DEBUG
598 urb_print(ahcd
, urb
, "UPDATE", 0);
600 admhc_dump_ed(ahcd
, "ED-UPDATE", ed
, 1);
603 for (; up
->td_idx
< up
->td_cnt
; up
->td_idx
++) {
604 struct td
*td
= up
->td
[up
->td_idx
];
606 if (hc32_to_cpup(ahcd
, &td
->hwINFO
) & TD_OWN
)
609 cc
= td_done(ahcd
, urb
, td
);
610 if (cc
!= TD_CC_NOERROR
) {
612 "urb %p td %p (%d) cc %d, len=%d/%d\n",
613 urb
, td
, td
->index
, cc
,
615 urb
->transfer_buffer_length
);
617 up
->td_idx
= up
->td_cnt
;
622 if ((up
->td_idx
!= up
->td_cnt
) && (!partial
))
623 /* the URB is not completed yet */
626 /* update packet status if needed (short is normally ok) */
627 if (cc
== TD_CC_DATAUNDERRUN
628 && !(urb
->transfer_flags
& URB_SHORT_NOT_OK
))
631 if (cc
!= TD_CC_NOERROR
&& cc
< TD_CC_HCD0
) {
632 spin_lock(&urb
->lock
);
633 if (urb
->status
== -EINPROGRESS
)
634 urb
->status
= cc_to_error
[cc
];
635 spin_unlock(&urb
->lock
);
638 finish_urb(ahcd
, urb
);
640 ed
->urb_active
= NULL
;
644 /* there are some tds completed; called in_irq(), with HCD locked */
645 static void admhc_td_complete(struct admhcd
*ahcd
)
649 for (ed
= ahcd
->ed_head
; ed
; ed
= ed
->ed_next
) {
650 if (ed
->state
!= ED_OPER
)
653 if (hc32_to_cpup(ahcd
, &ed
->hwINFO
) & ED_SKIP
)
656 if (hc32_to_cpup(ahcd
, &ed
->hwHeadP
) & ED_H
) {
661 ed_update(ahcd
, ed
, 0);
665 /* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
666 static void admhc_finish_unlinks(struct admhcd
*ahcd
, u16 tick
)
670 for (ed
= ahcd
->ed_head
; ed
; ed
= ed
->ed_next
) {
671 if (ed
->state
!= ED_UNLINK
)
674 if (likely(HC_IS_RUNNING(admhcd_to_hcd(ahcd
)->state
)))
675 if (tick_before(tick
, ed
->tick
))
678 /* process partial status */
679 ed_update(ahcd
, ed
, 1);
683 static void admhc_sof_refill(struct admhcd
*ahcd
)
688 for (ed
= ahcd
->ed_head
; ed
; ed
= ed
->ed_next
) {
690 if (hc32_to_cpup(ahcd
, &ed
->hwHeadP
) & ED_H
) {
691 ed_update(ahcd
, ed
, 1);
692 ed
->hwHeadP
&= ~cpu_to_hc32 (ahcd
, ED_H
);
695 if (ed_next_urb(ahcd
, ed
)) {
700 ed_deschedule(ahcd
, ed
);
706 admhc_intr_disable(ahcd
, ADMHC_INTR_SOFI
);
707 admhc_dma_disable(ahcd
);
709 admhc_intr_enable(ahcd
, ADMHC_INTR_SOFI
);
710 admhc_dma_enable(ahcd
);
This page took 0.071692 seconds and 3 git commands to generate.