USB: fix usbmon and DMA mapping for scatter-gather URBs
[safe/jmp/linux-2.6] / drivers / usb / host / xhci-ring.c
index 2e34633..c1359ed 100644 (file)
@@ -65,6 +65,7 @@
  */
 
 #include <linux/scatterlist.h>
+#include <linux/slab.h>
 #include "xhci.h"
 
 /*
@@ -241,10 +242,27 @@ static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
        int i;
        union xhci_trb *enq = ring->enqueue;
        struct xhci_segment *enq_seg = ring->enq_seg;
+       struct xhci_segment *cur_seg;
+       unsigned int left_on_ring;
 
        /* Check if ring is empty */
-       if (enq == ring->dequeue)
+       if (enq == ring->dequeue) {
+               /* Can't use link trbs */
+               left_on_ring = TRBS_PER_SEGMENT - 1;
+               for (cur_seg = enq_seg->next; cur_seg != enq_seg;
+                               cur_seg = cur_seg->next)
+                       left_on_ring += TRBS_PER_SEGMENT - 1;
+
+               /* Always need one TRB free in the ring. */
+               left_on_ring -= 1;
+               if (num_trbs > left_on_ring) {
+                       xhci_warn(xhci, "Not enough room on ring; "
+                                       "need %u TRBs, %u TRBs left\n",
+                                       num_trbs, left_on_ring);
+                       return 0;
+               }
                return 1;
+       }
        /* Make sure there's an extra empty TRB available */
        for (i = 0; i <= num_trbs; ++i) {
                if (enq == ring->dequeue)
@@ -577,6 +595,8 @@ static void handle_stopped_endpoint(struct xhci_hcd *xhci,
                /* Otherwise just ring the doorbell to restart the ring */
                ring_ep_doorbell(xhci, slot_id, ep_index);
        }
+       ep->stopped_td = NULL;
+       ep->stopped_trb = NULL;
 
        /*
         * Drop the lock and complete the URBs in the cancelled TD list.
@@ -903,28 +923,32 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
                                virt_dev->in_ctx);
                /* Input ctx add_flags are the endpoint index plus one */
                ep_index = xhci_last_valid_endpoint(ctrl_ctx->add_flags) - 1;
-               ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
-               if (!ep_ring) {
-                       /* This must have been an initial configure endpoint */
-                       xhci->devs[slot_id]->cmd_status =
-                               GET_COMP_CODE(event->status);
-                       complete(&xhci->devs[slot_id]->cmd_completion);
-                       break;
-               }
-               ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
-               xhci_dbg(xhci, "Completed config ep cmd - last ep index = %d, "
-                               "state = %d\n", ep_index, ep_state);
+               /* A usb_set_interface() call directly after clearing a halted
+                * condition may race on this quirky hardware.
+                * Not worth worrying about, since this is prototype hardware.
+                */
                if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
-                               ep_state & EP_HALTED) {
+                               ep_index != (unsigned int) -1 &&
+                               ctrl_ctx->add_flags - SLOT_FLAG ==
+                                       ctrl_ctx->drop_flags) {
+                       ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
+                       ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
+                       if (!(ep_state & EP_HALTED))
+                               goto bandwidth_change;
+                       xhci_dbg(xhci, "Completed config ep cmd - "
+                                       "last ep index = %d, state = %d\n",
+                                       ep_index, ep_state);
                        /* Clear our internal halted state and restart ring */
                        xhci->devs[slot_id]->eps[ep_index].ep_state &=
                                ~EP_HALTED;
                        ring_ep_doorbell(xhci, slot_id, ep_index);
-               } else {
-                       xhci->devs[slot_id]->cmd_status =
-                               GET_COMP_CODE(event->status);
-                       complete(&xhci->devs[slot_id]->cmd_completion);
+                       break;
                }
+bandwidth_change:
+               xhci_dbg(xhci, "Completed config ep cmd\n");
+               xhci->devs[slot_id]->cmd_status =
+                       GET_COMP_CODE(event->status);
+               complete(&xhci->devs[slot_id]->cmd_completion);
                break;
        case TRB_TYPE(TRB_EVAL_CONTEXT):
                virt_dev = xhci->devs[slot_id];
@@ -949,6 +973,17 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
        case TRB_TYPE(TRB_RESET_EP):
                handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
                break;
+       case TRB_TYPE(TRB_RESET_DEV):
+               xhci_dbg(xhci, "Completed reset device command.\n");
+               slot_id = TRB_TO_SLOT_ID(
+                               xhci->cmd_ring->dequeue->generic.field[3]);
+               virt_dev = xhci->devs[slot_id];
+               if (virt_dev)
+                       handle_cmd_in_cmd_wait_list(xhci, virt_dev, event);
+               else
+                       xhci_warn(xhci, "Reset device command completion "
+                                       "for disabled slot %u\n", slot_id);
+               break;
        default:
                /* Skip over unknown commands on the event ring */
                xhci->error_bitmask |= 1 << 6;
@@ -1045,8 +1080,13 @@ static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
        ep->ep_state |= EP_HALTED;
        ep->stopped_td = td;
        ep->stopped_trb = event_trb;
+
        xhci_queue_reset_ep(xhci, slot_id, ep_index);
        xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
+
+       ep->stopped_td = NULL;
+       ep->stopped_trb = NULL;
+
        xhci_ring_cmd_db(xhci);
 }
 
@@ -1076,6 +1116,20 @@ static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
        return 0;
 }
 
+int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
+{
+       if (trb_comp_code >= 224 && trb_comp_code <= 255) {
+               /* Vendor defined "informational" completion code,
+                * treat as not-an-error.
+                */
+               xhci_dbg(xhci, "Vendor defined info completion code %u\n",
+                               trb_comp_code);
+               xhci_dbg(xhci, "Treating code as success.\n");
+               return 1;
+       }
+       return 0;
+}
+
 /*
  * If this function returns an error condition, it means it got a Transfer
  * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
@@ -1192,13 +1246,7 @@ static int handle_tx_event(struct xhci_hcd *xhci,
                status = -ENOSR;
                break;
        default:
-               if (trb_comp_code >= 224 && trb_comp_code <= 255) {
-                       /* Vendor defined "informational" completion code,
-                        * treat as not-an-error.
-                        */
-                       xhci_dbg(xhci, "Vendor defined info completion code %u\n",
-                                       trb_comp_code);
-                       xhci_dbg(xhci, "Treating code as success.\n");
+               if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
                        status = 0;
                        break;
                }
@@ -1914,7 +1962,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
        int running_total, trb_buff_len, ret;
        u64 addr;
 
-       if (urb->sg)
+       if (urb->num_sgs)
                return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
 
        ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
@@ -2177,6 +2225,14 @@ int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
                        false);
 }
 
+/* Queue a reset device command TRB */
+int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id)
+{
+       return queue_command(xhci, 0, 0, 0,
+                       TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
+                       false);
+}
+
 /* Queue a configure endpoint command TRB */
 int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
                u32 slot_id, bool command_must_succeed)