Add a simple HID device

It currently has a hard-coded report of 4 2-byte joystick values
plus 8 buttons.

Also fix a few things I noticed in the CDC code while working on the HID
code.

Change-Id: Icce6c6ad686fdf974924daab6cb40b7b7d0f7996
diff --git a/motors/usb/usb.h b/motors/usb/usb.h
index 8b2cc67..4db29dc 100644
--- a/motors/usb/usb.h
+++ b/motors/usb/usb.h
@@ -2,6 +2,7 @@
 #define MOTORS_USB_USB_H_
 
 #include <assert.h>
+#include <string.h>
 #include <string>
 #include <vector>
 #include <memory>
@@ -76,6 +77,10 @@
   kString = 0x23,
   kInterface = 0x24,
   kEndpoint = 0x25,
+
+  kHidHid = 0x21,
+  kHidReport = 0x22,
+  kHidPhysical = 0x23,
 };
 
 // The names of the setup request types from the standard.
@@ -101,6 +106,16 @@
 constexpr int kOther = 3;
 }  // namespace standard_setup_recipients
 
+// The HID class specification says this. Can't find any mention in the main
+// standard.
+#define G_DESCRIPTOR_TYPE_TYPE(descriptor_type) \
+  ((descriptor_type) >> 5 & UINT8_C(3))
+namespace standard_descriptor_type_types {
+constexpr int kStandard = 0;
+constexpr int kClass = 1;
+constexpr int kVendor = 2;
+}  // namespace standard_descriptor_type_types
+
 class UsbFunction;
 
 // Allows building up a list of descriptors. This supports a much nicer API than
@@ -185,6 +200,13 @@
     return CreateDescriptor(length, static_cast<uint8_t>(descriptor_type));
   }
 
+  void AddPremadeDescriptor(const uint8_t *data, int length) {
+    const int start_index = data_.size();
+    const int end_index = start_index + length;
+    data_.resize(end_index);
+    memcpy(&data_[start_index], data, length);
+  }
+
   void CheckFinished() const { assert(open_descriptors_ == 0); }
 
   int CurrentSize() const { return data_.size(); }
@@ -469,6 +491,9 @@
     return device_->config_descriptor_list_.CreateDescriptor(length,
                                                              descriptor_type);
   }
+  void AddPremadeDescriptor(const uint8_t *data, int length) {
+    device_->config_descriptor_list_.AddPremadeDescriptor(data, length);
+  }
 
   UsbDevice *device() const { return device_; }
 
@@ -485,6 +510,11 @@
     return SetupResponse::kIgnored;
   }
 
+  virtual SetupResponse HandleGetDescriptor(
+      const UsbDevice::SetupPacket & /*setup_packet*/) {
+    return SetupResponse::kIgnored;
+  }
+
   virtual void HandleOutFinished(int endpoint, BdtEntry *bdt_entry) = 0;
   virtual void HandleInFinished(int endpoint, BdtEntry *bdt_entry,
                                 EvenOdd odd) = 0;