[banjo][usb] Move usb_request_t to a banjo library

This is preparation for moving the remaining USB protocols to banjo

This requires the following garnet change:
https://fuchsia-review.googlesource.com/c/garnet/+/233495

TEST: garnet builds on both platforms, USB works on NUC.

This can be shared by future banjo libraries for the USB device, function,
HCI and DCI protocols.

Change-Id: I9e7182b1098a226323418d65cf8727d0dc6db07e
diff --git a/system/banjo/ddk-driver/driver.banjo b/system/banjo/ddk-driver/driver.banjo
index 52934b1..73c3d68 100644
--- a/system/banjo/ddk-driver/driver.banjo
+++ b/system/banjo/ddk-driver/driver.banjo
@@ -54,3 +54,11 @@
     /// One or more of DEVICE_ADD_*
     uint32 flags;
 };
+
+[repr = "C"]
+struct PhysIterSgEntry {
+    /// length starting at the scatter gather entry offset, must be non zero
+    usize length;
+    /// offset relative to the buffer's vmo_offset
+    usize offset;
+};
diff --git a/system/banjo/ddk-protocol-usb-request/rules.mk b/system/banjo/ddk-protocol-usb-request/rules.mk
new file mode 100644
index 0000000..2d52b37
--- /dev/null
+++ b/system/banjo/ddk-protocol-usb-request/rules.mk
@@ -0,0 +1,24 @@
+# Copyright 2018 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+MODULE_TYPE := banjo
+
+MODULE_PACKAGE := banjo
+
+MODULE_BANJO_LIBRARY := ddk.protocol.usb.request
+
+MODULE_BANJO_NAME := usb/request
+
+MODULE_BANJO_DEPS := \
+    system/banjo/ddk-driver \
+    system/banjo/zircon-hw-usb \
+
+MODULE_SRCS += $(LOCAL_DIR)/usb-request.banjo
+
+include make/module.mk
+
diff --git a/system/banjo/ddk-protocol-usb-request/usb-request.banjo b/system/banjo/ddk-protocol-usb-request/usb-request.banjo
new file mode 100644
index 0000000..e5dddcc
--- /dev/null
+++ b/system/banjo/ddk-protocol-usb-request/usb-request.banjo
@@ -0,0 +1,68 @@
+// Copyright 2018 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+library ddk.protocol.usb.request;
+
+using ddk.driver;
+using zircon.hw.usb;
+using zx;
+
+/// Should be set by the requestor.
+struct UsbHeader {
+    /// Frame number for scheduling isochronous transfers.
+    uint64 frame;
+    uint32 device_id;
+    /// bEndpointAddress from endpoint descriptor.
+    uint8 ep_address;
+    /// Number of bytes to transfer.
+    zx.off length;
+    /// Send zero length packet if length is multiple of max packet size.
+    bool send_zlp;
+};
+
+/// Response data.
+/// (Filled in by processor before |UsbRequestComplete()| is called)
+struct UsbResponse {
+    /// Status of transaction.
+    zx.status status;
+    /// Number of bytes actually transferred (on success).
+    zx.off actual;
+};
+
+struct UsbRequest {
+    UsbHeader header;
+
+    /// For control transactions.
+    zircon.hw.usb.UsbSetup setup;
+
+    /// VMO handle for payload.
+    handle<vmo> vmo_handle;
+    usize size;
+    /// Offset of the start of data from first page address of the vmo.
+    zx.off offset;
+    /// Mapped address of the first page of the vmo.
+    /// Add offset to get actual data.
+    uint64 virt;
+
+    handle pmt;
+    /// Phys addresses of the payload.
+    vector<zx.paddr>? phys;
+
+    vector<ddk.driver.PhysIterSgEntry>? sg;
+
+    UsbResponse response;
+
+    /// usb_request_release() frees the request if this is true.
+    bool release_frees;
+
+    usize alloc_size;
+
+    /// For requests queued on endpoints which have batching enabled via
+    /// usb_configure_batch_callback().
+    /// Set by the requester if a callback is required on this request's completion.
+    /// This is useful for isochronous requests, where the requester does not care about
+    /// most callbacks.
+    /// The requester should ensure the last request has this set to true.
+    bool require_batch_cb;
+};
diff --git a/system/dev/audio/usb-audio/rules.mk b/system/dev/audio/usb-audio/rules.mk
index eed64d5..190ea1a 100644
--- a/system/dev/audio/usb-audio/rules.mk
+++ b/system/dev/audio/usb-audio/rules.mk
@@ -44,5 +44,6 @@
 
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/block/ums-function/rules.mk b/system/dev/block/ums-function/rules.mk
index 2272b4b..2be10bc 100644
--- a/system/dev/block/ums-function/rules.mk
+++ b/system/dev/block/ums-function/rules.mk
@@ -16,6 +16,7 @@
 MODULE_LIBS := system/ulib/driver system/ulib/zircon system/ulib/c
 
 MODULE_BANJO_LIBS := \
-    system/banjo/ddk-protocol-block
+    system/banjo/ddk-protocol-block \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/block/usb-mass-storage/rules.mk b/system/dev/block/usb-mass-storage/rules.mk
index c097e90..e2b960c 100644
--- a/system/dev/block/usb-mass-storage/rules.mk
+++ b/system/dev/block/usb-mass-storage/rules.mk
@@ -19,5 +19,6 @@
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-block \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/bluetooth/bt-transport-usb/rules.mk b/system/dev/bluetooth/bt-transport-usb/rules.mk
index bed8a96..91ff239 100644
--- a/system/dev/bluetooth/bt-transport-usb/rules.mk
+++ b/system/dev/bluetooth/bt-transport-usb/rules.mk
@@ -17,5 +17,6 @@
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-bt-hci \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/ethernet/asix-88179/rules.mk b/system/dev/ethernet/asix-88179/rules.mk
index 8f3532b..637ceed 100644
--- a/system/dev/ethernet/asix-88179/rules.mk
+++ b/system/dev/ethernet/asix-88179/rules.mk
@@ -18,5 +18,6 @@
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-ethernet \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/ethernet/asix-88772b/rules.mk b/system/dev/ethernet/asix-88772b/rules.mk
index 0dc7f77..c26145c 100644
--- a/system/dev/ethernet/asix-88772b/rules.mk
+++ b/system/dev/ethernet/asix-88772b/rules.mk
@@ -17,6 +17,7 @@
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-ethernet \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
 
diff --git a/system/dev/ethernet/rndis/rules.mk b/system/dev/ethernet/rndis/rules.mk
index 6b6d12d..138f791 100644
--- a/system/dev/ethernet/rndis/rules.mk
+++ b/system/dev/ethernet/rndis/rules.mk
@@ -17,6 +17,7 @@
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-ethernet \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
 
diff --git a/system/dev/ethernet/usb-cdc-ecm/rules.mk b/system/dev/ethernet/usb-cdc-ecm/rules.mk
index b607976..0087cc5 100644
--- a/system/dev/ethernet/usb-cdc-ecm/rules.mk
+++ b/system/dev/ethernet/usb-cdc-ecm/rules.mk
@@ -18,6 +18,7 @@
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-ethernet \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
 
diff --git a/system/dev/ethernet/usb-cdc-function/rules.mk b/system/dev/ethernet/usb-cdc-function/rules.mk
index 7d2d4af..32cef3b 100644
--- a/system/dev/ethernet/usb-cdc-function/rules.mk
+++ b/system/dev/ethernet/usb-cdc-function/rules.mk
@@ -23,6 +23,7 @@
 
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-ethernet \
+    system/banjo/ddk-protocol-usb-request \
 
 MODULE_HEADER_DEPS := \
 	system/ulib/inet6 \
diff --git a/system/dev/input/usb-hid/rules.mk b/system/dev/input/usb-hid/rules.mk
index d3f8ae4..64bfc9f 100644
--- a/system/dev/input/usb-hid/rules.mk
+++ b/system/dev/input/usb-hid/rules.mk
@@ -18,5 +18,6 @@
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-hidbus \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/lib/usb/rules.mk b/system/dev/lib/usb/rules.mk
index 409481d..ad7bc9e 100644
--- a/system/dev/lib/usb/rules.mk
+++ b/system/dev/lib/usb/rules.mk
@@ -16,6 +16,7 @@
 
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 MODULE_PACKAGE := src
 
diff --git a/system/dev/lib/usb/usb-request.c b/system/dev/lib/usb/usb-request.c
index 299319d..f05c714 100644
--- a/system/dev/lib/usb/usb-request.c
+++ b/system/dev/lib/usb/usb-request.c
@@ -55,7 +55,7 @@
             return status;
         }
 
-        req->virt = (void *)mapped_addr;
+        req->virt = mapped_addr;
         req->offset = 0;
         req->size = data_size;
     }
@@ -103,7 +103,7 @@
 
     req->alloc_size = req_size;
     req->vmo_handle = dup_handle;
-    req->virt = (void *)mapped_addr;
+    req->virt = mapped_addr;
     req->offset = vmo_offset;
     req->size = size;
 
@@ -147,7 +147,7 @@
     }
 
     req->vmo_handle = dup_handle;
-    req->virt = (void *)mapped_addr;
+    req->virt = mapped_addr;
     req->offset = vmo_offset;
     req->size = size;
 
diff --git a/system/dev/serial/ftdi/rules.mk b/system/dev/serial/ftdi/rules.mk
index 913e671..7e7893c 100644
--- a/system/dev/serial/ftdi/rules.mk
+++ b/system/dev/serial/ftdi/rules.mk
@@ -17,5 +17,6 @@
     system/banjo/ddk-protocol-serial \
     system/banjo/ddk-protocol-serial-impl \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/test/ddk-test/rules.mk b/system/dev/test/ddk-test/rules.mk
index 0339513..6657ffe 100644
--- a/system/dev/test/ddk-test/rules.mk
+++ b/system/dev/test/ddk-test/rules.mk
@@ -29,5 +29,6 @@
 
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-test \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/usb/dwc3/rules.mk b/system/dev/usb/dwc3/rules.mk
index 9d25312..d27e5f6 100644
--- a/system/dev/usb/dwc3/rules.mk
+++ b/system/dev/usb/dwc3/rules.mk
@@ -34,5 +34,6 @@
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-platform-device \
     system/banjo/ddk-protocol-usb-mode-switch \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/usb/mt-musb-peripheral/rules.mk b/system/dev/usb/mt-musb-peripheral/rules.mk
index e9e7c57..c967fb0 100644
--- a/system/dev/usb/mt-musb-peripheral/rules.mk
+++ b/system/dev/usb/mt-musb-peripheral/rules.mk
@@ -33,5 +33,6 @@
     system/banjo/ddk-protocol-gpio \
     system/banjo/ddk-protocol-i2c \
     system/banjo/ddk-protocol-platform-device \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/usb/usb-bus/rules.mk b/system/dev/usb/usb-bus/rules.mk
index 2245c45..1765ead 100644
--- a/system/dev/usb/usb-bus/rules.mk
+++ b/system/dev/usb/usb-bus/rules.mk
@@ -33,5 +33,6 @@
     system/banjo/ddk-protocol-usb-bus \
     system/banjo/ddk-protocol-usb-composite \
     system/banjo/ddk-protocol-usb-hub \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/usb/usb-composite/rules.mk b/system/dev/usb/usb-composite/rules.mk
index f2a4ba3..c8d5f07 100644
--- a/system/dev/usb/usb-composite/rules.mk
+++ b/system/dev/usb/usb-composite/rules.mk
@@ -24,5 +24,6 @@
 
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/usb/usb-dfu/rules.mk b/system/dev/usb/usb-dfu/rules.mk
index c9e9143..4fdc791 100644
--- a/system/dev/usb/usb-dfu/rules.mk
+++ b/system/dev/usb/usb-dfu/rules.mk
@@ -32,5 +32,6 @@
 
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/usb/usb-hub/rules.mk b/system/dev/usb/usb-hub/rules.mk
index 0024e24..fac4ed4 100644
--- a/system/dev/usb/usb-hub/rules.mk
+++ b/system/dev/usb/usb-hub/rules.mk
@@ -25,5 +25,6 @@
     system/banjo/ddk-protocol-usb-bus \
     system/banjo/ddk-protocol-usb-composite \
     system/banjo/ddk-protocol-usb-hub \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/usb/usb-peripheral-test/rules.mk b/system/dev/usb/usb-peripheral-test/rules.mk
index 8ec0802..3210669 100644
--- a/system/dev/usb/usb-peripheral-test/rules.mk
+++ b/system/dev/usb/usb-peripheral-test/rules.mk
@@ -22,6 +22,9 @@
     system/ulib/zircon \
     system/ulib/c \
 
+MODULE_BANJO_LIBS := \
+    system/banjo/ddk-protocol-usb-request \
+
 MODULE_HEADER_DEPS := \
     system/ulib/inet6 \
 
diff --git a/system/dev/usb/usb-peripheral/rules.mk b/system/dev/usb/usb-peripheral/rules.mk
index 255ba6c..c9b0888 100644
--- a/system/dev/usb/usb-peripheral/rules.mk
+++ b/system/dev/usb/usb-peripheral/rules.mk
@@ -25,6 +25,7 @@
 
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-usb-mode-switch \
+    system/banjo/ddk-protocol-usb-request \
 
 # Set default configuration here, rather than relying on usbctl to do it
 MODULE_DEFINES := USB_DEVICE_VID=0x18D1 \
diff --git a/system/dev/usb/usb-test/flash-programmer/rules.mk b/system/dev/usb/usb-test/flash-programmer/rules.mk
index 74df833..851bb54 100644
--- a/system/dev/usb/usb-test/flash-programmer/rules.mk
+++ b/system/dev/usb/usb-test/flash-programmer/rules.mk
@@ -28,6 +28,7 @@
 
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 ifeq ($(call TOBOOL,$(INTERNAL_ACCESS)),true)
 MODULE_FIRMWARE := fx3-boot/Fx3BootAppGcc.img
diff --git a/system/dev/usb/usb-test/fx3/rules.mk b/system/dev/usb/usb-test/fx3/rules.mk
index b09fb46..404fcb8 100644
--- a/system/dev/usb/usb-test/fx3/rules.mk
+++ b/system/dev/usb/usb-test/fx3/rules.mk
@@ -21,6 +21,7 @@
 
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 ifeq ($(call TOBOOL,$(INTERNAL_ACCESS)),true)
 MODULE_FIRMWARE := \
diff --git a/system/dev/usb/usb-test/usb-tester/rules.mk b/system/dev/usb/usb-test/usb-tester/rules.mk
index ba97c36..3429da9 100644
--- a/system/dev/usb/usb-test/usb-tester/rules.mk
+++ b/system/dev/usb/usb-test/usb-tester/rules.mk
@@ -26,5 +26,6 @@
 
 MODULE_BANJO_LIBS := \
     system/banjo/ddk-protocol-usb-composite \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/dev/usb/xhci/rules.mk b/system/dev/usb/xhci/rules.mk
index b06940b..8c434a7 100644
--- a/system/dev/usb/xhci/rules.mk
+++ b/system/dev/usb/xhci/rules.mk
@@ -43,5 +43,6 @@
     system/banjo/ddk-protocol-platform-device \
     system/banjo/ddk-protocol-usb-bus \
     system/banjo/ddk-protocol-usb-hub \
+    system/banjo/ddk-protocol-usb-request \
 
 include make/module.mk
diff --git a/system/ulib/ddk/include/ddk/protocol/usb.h b/system/ulib/ddk/include/ddk/protocol/usb.h
index d7f71a4..54d2491 100644
--- a/system/ulib/ddk/include/ddk/protocol/usb.h
+++ b/system/ulib/ddk/include/ddk/protocol/usb.h
@@ -5,6 +5,7 @@
 #pragma once
 
 #include <ddk/phys-iter.h>
+#include <ddk/protocol/usb/request.h>
 #include <sys/types.h>
 #include <zircon/compiler.h>
 #include <zircon/types.h>
@@ -29,70 +30,6 @@
 typedef void (*usb_batch_complete_cb)(usb_request_t** completed_reqs, size_t num_completed,
                                       void* cookie);
 
-// Should be set by the requester.
-typedef struct usb_header {
-    // frame number for scheduling isochronous transfers
-    uint64_t frame;
-    uint32_t device_id;
-    // bEndpointAddress from endpoint descriptor
-    uint8_t ep_address;
-    // number of bytes to transfer
-    zx_off_t length;
-    // send zero length packet if length is multiple of max packet size
-    bool send_zlp;
-} usb_header_t;
-
-// response data
-// (filled in by processor before usb_request_complete() is called)
-typedef struct usb_response {
-    // status of transaction
-    zx_status_t status;
-    // number of bytes actually transferred (on success)
-    zx_off_t actual;
-} usb_response_t;
-
-typedef struct usb_request {
-    usb_header_t header;
-
-    // for control transactions
-    usb_setup_t setup;
-
-    // vmo_handle for payload
-    zx_handle_t vmo_handle;
-    size_t size;
-    // offset of the start of data from first page address of the vmo.
-    zx_off_t offset;
-    // mapped address of the first page of the vmo.
-    // Add offset to get actual data.
-    void* virt;
-
-    zx_handle_t pmt;
-    // phys addresses of the payload.
-    zx_paddr_t* phys_list;
-    // Number of physical pages of the payload.
-    uint64_t phys_count;
-
-    // Scatter gather entries of the payload.
-    phys_iter_sg_entry_t* sg_list;
-    // Number of entries in the scatter gather list.
-    uint64_t sg_count;
-
-    usb_response_t response;
-
-    // usb_request_release() frees the request if this is true.
-    bool release_frees;
-    size_t alloc_size;
-
-    // For requests queued on endpoints which have batching enabled via
-    // usb_configure_batch_callback().
-    // Set by the requester if a callback is required on this request's completion.
-    // This is useful for isochronous requests, where the requester does not care about
-    // most callbacks.
-    // The requester should ensure the last request has this set to true.
-    bool require_batch_cb;
-} usb_request_t;
-
-
 typedef struct {
     zx_status_t (*control)(void* ctx, uint8_t request_type, uint8_t request, uint16_t value,
                            uint16_t index, void* data, size_t length, zx_time_t timeout,