28 static void uhci_set_next_interrupt(
struct uhci_hcd *uhci)
31 mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
35 static inline void uhci_clear_next_interrupt(
struct uhci_hcd *uhci)
46 static void uhci_fsbr_on(
struct uhci_hcd *uhci)
54 lqh =
list_entry(uhci->skel_async_qh->node.prev,
59 static void uhci_fsbr_off(
struct uhci_hcd *uhci)
66 lqh =
list_entry(uhci->skel_async_qh->node.prev,
75 if (!(urb->transfer_flags & URB_NO_FSBR))
79 static void uhci_urbp_wants_fsbr(
struct uhci_hcd *uhci,
struct urb_priv *urbp)
92 static void uhci_fsbr_timeout(
unsigned long _uhci)
102 spin_unlock_irqrestore(&uhci->
lock, flags);
118 INIT_LIST_HEAD(&td->
list);
126 if (!list_empty(&td->
list))
134 static inline void uhci_fill_td(
struct uhci_hcd *uhci,
struct uhci_td *td,
142 static void uhci_add_td_to_urbp(
struct uhci_td *td,
struct urb_priv *urbp)
147 static void uhci_remove_td_from_urbp(
struct uhci_td *td)
149 list_del_init(&td->
list);
155 static inline void uhci_insert_td_in_frame_list(
struct uhci_hcd *uhci,
156 struct uhci_td *td,
unsigned framenum)
160 td->
frame = framenum;
182 static inline void uhci_remove_td_from_frame_list(
struct uhci_hcd *uhci,
186 if (td->
frame == -1) {
192 if (list_empty(&td->
fl_list)) {
215 static inline void uhci_remove_tds_from_frame(
struct uhci_hcd *uhci,
216 unsigned int framenum)
228 while (!list_empty(&ftd->
fl_list))
229 list_del_init(ftd->
fl_list.prev);
236 static void uhci_unlink_isochronous_tds(
struct uhci_hcd *uhci,
struct urb *urb)
242 uhci_remove_td_from_frame_list(uhci, td);
246 struct usb_device *
udev,
struct usb_host_endpoint *
hep)
255 memset(qh, 0,
sizeof(*qh));
261 INIT_LIST_HEAD(&qh->
queue);
262 INIT_LIST_HEAD(&qh->
node);
265 qh->
type = usb_endpoint_type(&hep->desc);
281 usb_endpoint_dir_in(&hep->desc),
283 usb_endpoint_maxp(&hep->desc))
296 if (!list_empty(&qh->
queue))
315 static int uhci_cleanup_queue(
struct uhci_hcd *uhci,
struct uhci_qh *qh,
318 struct urb_priv *urbp = urb->hcpriv;
373 static void uhci_fixup_toggles(
struct uhci_hcd *uhci,
struct uhci_qh *qh,
417 usb_settoggle(qh->
udev, usb_pipeendpoint(pipe),
418 usb_pipeout(pipe), toggle);
470 pqh->
link = link_to_new_qh;
475 uhci->skel_term_qh->link = link_to_new_qh;
481 static void uhci_activate_qh(
struct uhci_hcd *uhci,
struct uhci_qh *qh)
514 link_interrupt(uhci, qh);
516 link_async(uhci, qh);
522 static void unlink_interrupt(
struct uhci_hcd *uhci,
struct uhci_qh *qh)
540 pqh->
link = link_to_next_qh;
545 uhci->skel_term_qh->link = link_to_next_qh;
563 unlink_interrupt(uhci, qh);
565 unlink_async(uhci, qh);
567 uhci_get_current_frame_number(uhci);
571 if (list_empty(&uhci->skel_unlink_qh->node) || uhci->
is_stopped)
572 uhci_set_next_interrupt(uhci);
578 list_move_tail(&qh->
node, &uhci->skel_unlink_qh->node);
587 static void uhci_make_qh_idle(
struct uhci_hcd *uhci,
struct uhci_qh *qh)
599 uhci_free_td(uhci, qh->
post_td);
616 highest_load =
max_t(
int, highest_load, uhci->
load[phase]);
624 static int uhci_check_bandwidth(
struct uhci_hcd *uhci,
struct uhci_qh *qh)
631 minimax_load = uhci_highest_load(uhci, qh->
phase, qh->
period);
637 minimax_load = uhci_highest_load(uhci, qh->
phase, qh->
period);
638 for (phase = 1; phase < max_phase; ++
phase) {
639 load = uhci_highest_load(uhci, phase, qh->
period);
640 if (load < minimax_load) {
648 if (minimax_load + qh->
load > 900) {
650 "period %d, phase %d, %d + %d us\n",
660 static void uhci_reserve_bandwidth(
struct uhci_hcd *uhci,
struct uhci_qh *qh)
666 for (i = qh->
phase; i < MAX_PHASE; i += qh->period) {
670 uhci_to_hcd(uhci)->self.bandwidth_allocated =
674 ++uhci_to_hcd(uhci)->self.bandwidth_int_reqs;
678 ++uhci_to_hcd(uhci)->self.bandwidth_isoc_reqs;
684 "%s dev %d ep%02x-%s, period %d, phase %d, %d us\n",
685 "reserve", qh->
udev->devnum,
686 qh->
hep->desc.bEndpointAddress, p,
693 static void uhci_release_bandwidth(
struct uhci_hcd *uhci,
struct uhci_qh *qh)
699 for (i = qh->
phase; i < MAX_PHASE; i += qh->period) {
703 uhci_to_hcd(uhci)->self.bandwidth_allocated =
707 --uhci_to_hcd(uhci)->self.bandwidth_int_reqs;
711 --uhci_to_hcd(uhci)->self.bandwidth_isoc_reqs;
717 "%s dev %d ep%02x-%s, period %d, phase %d, %d us\n",
718 "release", qh->
udev->devnum,
719 qh->
hep->desc.bEndpointAddress, p,
728 urbp = kmem_cache_zalloc(uhci_up_cachep,
GFP_ATOMIC);
735 INIT_LIST_HEAD(&urbp->
node);
736 INIT_LIST_HEAD(&urbp->
td_list);
741 static void uhci_free_urb_priv(
struct uhci_hcd *uhci,
746 if (!list_empty(&urbp->
node))
751 uhci_remove_td_from_urbp(td);
752 uhci_free_td(uhci, td);
766 static int uhci_map_status(
int status,
int dir_out)
790 static int uhci_submit_control(
struct uhci_hcd *uhci,
struct urb *urb,
794 unsigned long destination,
status;
795 int maxsze = usb_endpoint_maxp(&qh->
hep->desc);
796 int len = urb->transfer_buffer_length;
799 struct urb_priv *urbp = urb->hcpriv;
814 uhci_add_td_to_urbp(td, urbp);
815 uhci_fill_td(uhci, td, status, destination |
uhci_explen(8),
827 if (usb_pipeout(urb->pipe) || len == 0)
845 td = uhci_alloc_td(uhci);
853 uhci_add_td_to_urbp(td, urbp);
854 uhci_fill_td(uhci, td, status,
865 td = uhci_alloc_td(uhci);
874 uhci_add_td_to_urbp(td, urbp);
882 td = uhci_alloc_td(uhci);
901 uhci_add_fsbr(uhci, urb);
909 uhci_remove_td_from_urbp(qh->
dummy_td);
916 static int uhci_submit_common(
struct uhci_hcd *uhci,
struct urb *urb,
920 unsigned long destination,
status;
921 int maxsze = usb_endpoint_maxp(&qh->
hep->desc);
922 int len = urb->transfer_buffer_length;
926 struct urb_priv *urbp = urb->hcpriv;
936 toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
937 usb_pipeout(urb->pipe));
943 if (usb_pipein(urb->pipe))
946 i = urb->num_mapped_sgs;
947 if (len > 0 && i > 0) {
957 data = urb->transfer_dma;
970 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
975 td = uhci_alloc_td(uhci);
980 uhci_add_td_to_urbp(td, urbp);
981 uhci_fill_td(uhci, td, status,
990 this_sg_len -= pktsze;
992 if (this_sg_len <= 0) {
993 if (--i <= 0 || len <= 0)
1008 if ((urb->transfer_flags & URB_ZERO_PACKET) &&
1009 usb_pipeout(urb->pipe) && len == 0 &&
1010 urb->transfer_buffer_length > 0) {
1011 td = uhci_alloc_td(uhci);
1016 uhci_add_td_to_urbp(td, urbp);
1017 uhci_fill_td(uhci, td, status,
1037 td = uhci_alloc_td(uhci);
1047 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1048 usb_pipeout(urb->pipe), toggle);
1053 uhci_remove_td_from_urbp(qh->
dummy_td);
1057 static int uhci_submit_bulk(
struct uhci_hcd *uhci,
struct urb *urb,
1068 ret = uhci_submit_common(uhci, urb, qh);
1070 uhci_add_fsbr(uhci, urb);
1074 static int uhci_submit_interrupt(
struct uhci_hcd *uhci,
struct urb *urb,
1088 for (exponent = 7; exponent >= 0; --
exponent) {
1089 if ((1 << exponent) <= urb->interval)
1104 ret = uhci_check_bandwidth(uhci, qh);
1105 }
while (ret != 0 && --exponent >= 0);
1108 }
else if (qh->
period > urb->interval)
1111 ret = uhci_submit_common(uhci, urb, qh);
1113 urb->interval = qh->
period;
1115 uhci_reserve_bandwidth(uhci, qh);
1123 static int uhci_fixup_short_transfer(
struct uhci_hcd *uhci,
1138 tmp = td->
list.prev;
1148 uhci_fixup_toggles(uhci, qh, 1);
1150 if (list_empty(&urbp->
td_list))
1158 while (tmp != &urbp->
td_list) {
1162 uhci_remove_td_from_urbp(td);
1163 uhci_free_td(uhci, td);
1171 static int uhci_result_common(
struct uhci_hcd *uhci,
struct urb *urb)
1173 struct urb_priv *urbp = urb->hcpriv;
1180 unsigned int ctrlstat;
1189 urb->actual_length += len;
1192 ret = uhci_map_status(status,
1197 "%s: failed with status %x\n",
1200 if (
debug > 1 && errbuf) {
1202 uhci_show_qh(uhci, urbp->
qh, errbuf,
1219 else if (urb->transfer_flags & URB_SHORT_NOT_OK)
1227 uhci_remove_td_from_urbp(td);
1229 uhci_free_td(uhci, qh->
post_td);
1248 ret = uhci_fixup_short_transfer(uhci, qh, urbp);
1255 static int uhci_submit_isochronous(
struct uhci_hcd *uhci,
struct urb *urb,
1260 unsigned long destination,
status;
1270 qh->
period = urb->interval;
1271 if (urb->transfer_flags & URB_ISO_ASAP) {
1273 i = uhci_check_bandwidth(uhci, qh);
1278 uhci_get_current_frame_number(uhci);
1283 urb->start_frame = frame + ((qh->
phase -
frame) &
1290 i = uhci_check_bandwidth(uhci, qh);
1295 }
else if (qh->
period != urb->interval) {
1300 if (list_empty(&qh->
queue)) {
1307 frame = lurb->start_frame +
1308 lurb->number_of_packets *
1311 if (urb->transfer_flags & URB_ISO_ASAP) {
1315 uhci_get_current_frame_number(uhci);
1322 urb->start_frame =
frame;
1327 urb->start_frame + urb->number_of_packets *
1334 for (i = 0; i < urb->number_of_packets; i++) {
1335 td = uhci_alloc_td(uhci);
1339 uhci_add_td_to_urbp(td, urbp);
1340 uhci_fill_td(uhci, td, status, destination |
1343 urb->iso_frame_desc[i].offset);
1350 frame = urb->start_frame;
1352 uhci_insert_td_in_frame_list(uhci, td, frame);
1356 if (list_empty(&qh->
queue)) {
1363 uhci_reserve_bandwidth(uhci, qh);
1367 static int uhci_result_isochronous(
struct uhci_hcd *uhci,
struct urb *urb)
1370 struct urb_priv *urbp = urb->hcpriv;
1374 unsigned int ctrlstat;
1381 uhci_remove_tds_from_frame(uhci, qh->
iso_frame);
1384 if (ctrlstat & TD_CTRL_ACTIVE) {
1388 usb_pipeout(urb->pipe));
1391 urb->actual_length += actlength;
1398 uhci_remove_td_from_urbp(td);
1399 uhci_free_td(uhci, td);
1406 static int uhci_urb_enqueue(
struct usb_hcd *hcd,
1407 struct urb *urb,
gfp_t mem_flags)
1410 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1411 unsigned long flags;
1419 goto done_not_linked;
1422 urbp = uhci_alloc_urb_priv(uhci, urb);
1426 if (urb->ep->hcpriv)
1427 qh = urb->ep->hcpriv;
1429 qh = uhci_alloc_qh(uhci, urb->dev, urb->ep);
1437 ret = uhci_submit_control(uhci, urb, qh);
1440 ret = uhci_submit_bulk(uhci, urb, qh);
1443 ret = uhci_submit_interrupt(uhci, urb, qh);
1446 urb->error_count = 0;
1447 ret = uhci_submit_isochronous(uhci, urb, qh);
1451 goto err_submit_failed;
1461 uhci_activate_qh(uhci, qh);
1462 uhci_urbp_wants_fsbr(uhci, urbp);
1468 uhci_make_qh_idle(uhci, qh);
1470 uhci_free_urb_priv(uhci, urbp);
1475 spin_unlock_irqrestore(&uhci->
lock, flags);
1479 static int uhci_urb_dequeue(
struct usb_hcd *hcd,
struct urb *urb,
int status)
1481 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1482 unsigned long flags;
1491 qh = ((
struct urb_priv *) urb->hcpriv)->qh;
1495 uhci_unlink_isochronous_tds(uhci, urb);
1499 uhci_get_current_frame_number(uhci);
1504 uhci_unlink_qh(uhci, qh);
1507 spin_unlock_irqrestore(&uhci->
lock, flags);
1514 static void uhci_giveback_urb(
struct uhci_hcd *uhci,
struct uhci_qh *qh,
1515 struct urb *urb,
int status)
1526 urb->actual_length -=
min_t(
u32, 8, urb->actual_length);
1543 list_del_init(&urbp->
node);
1545 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1550 uhci_free_urb_priv(uhci, urbp);
1553 spin_unlock(&uhci->
lock);
1555 spin_lock(&uhci->
lock);
1559 if (list_empty(&qh->
queue)) {
1560 uhci_unlink_qh(uhci, qh);
1562 uhci_release_bandwidth(uhci, qh);
1569 #define QH_FINISHED_UNLINKING(qh) \
1570 (qh->state == QH_STATE_UNLINKING && \
1571 uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
1579 while (!list_empty(&qh->
queue)) {
1584 status = uhci_result_isochronous(uhci, urb);
1586 status = uhci_result_common(uhci, urb);
1592 if (urb->unlinked) {
1599 uhci_giveback_urb(uhci, qh, urb, status);
1615 if (urb->unlinked) {
1620 if (!uhci_cleanup_queue(uhci, qh, urb)) {
1624 uhci_giveback_urb(uhci, qh, urb, 0);
1632 if (!list_empty(&qh->
queue)) {
1634 uhci_fixup_toggles(uhci, qh, 0);
1647 uhci_activate_qh(uhci, qh);
1653 uhci_make_qh_idle(uhci, qh);
1666 static int uhci_advance_check(
struct uhci_hcd *uhci,
struct uhci_qh *qh)
1692 if (!(status & TD_CTRL_ACTIVE)) {
1724 uhci_unlink_qh(uhci, qh);
1729 uhci_urbp_wants_fsbr(uhci, urbp);
1739 static void uhci_scan_schedule(
struct uhci_hcd *uhci)
1754 uhci_clear_next_interrupt(uhci);
1755 uhci_get_current_frame_number(uhci);
1766 if (uhci_advance_check(uhci, qh)) {
1767 uhci_scan_qh(uhci, qh);
1769 uhci_urbp_wants_fsbr(uhci,
1787 if (list_empty(&uhci->skel_unlink_qh->node))
1788 uhci_clear_next_interrupt(uhci);
1790 uhci_set_next_interrupt(uhci);