diff options
author | reillyg <reillyg@chromium.org> | 2014-09-04 12:40:30 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-09-04 19:44:47 +0000 |
commit | ee658b744765469a302bb0f742b84a8c1aad283e (patch) | |
tree | ffb9cbeb6018923e49bb9f0fd1823225b9751635 /tools/usb_gadget | |
parent | 20533738ea152a9ac7331a066866e0ffcbd0d1a4 (diff) | |
download | chromium_src-ee658b744765469a302bb0f742b84a8c1aad283e.zip chromium_src-ee658b744765469a302bb0f742b84a8c1aad283e.tar.gz chromium_src-ee658b744765469a302bb0f742b84a8c1aad283e.tar.bz2 |
USB interrupt and bulk transfer tests using an echo gadget.
Add tests for interrupt and bulk USB transfers using a gadget device
that echos packets back to the host. This change depends on the removal
of the //content dependency in usb_service so that it no longer depends
explicitly on BrowserThread::FILE for I/O operations.
BUG=
Review URL: https://codereview.chromium.org/516003002
Cr-Commit-Position: refs/heads/master@{#293324}
Diffstat (limited to 'tools/usb_gadget')
-rw-r--r-- | tools/usb_gadget/__main__.py | 2 | ||||
-rw-r--r-- | tools/usb_gadget/echo_gadget.py | 217 | ||||
-rw-r--r-- | tools/usb_gadget/usb_constants.py | 1 |
3 files changed, 220 insertions, 0 deletions
diff --git a/tools/usb_gadget/__main__.py b/tools/usb_gadget/__main__.py index e82dd6d..0391a34 100644 --- a/tools/usb_gadget/__main__.py +++ b/tools/usb_gadget/__main__.py @@ -9,6 +9,7 @@ import argparse import netifaces from tornado import ioloop +import echo_gadget import hid_echo_gadget import keyboard_gadget import linux_gadgetfs @@ -49,6 +50,7 @@ def main(): server.chip = linux_gadgetfs.LinuxGadgetfs(server.hardware) server.SwitchGadget(server.default) + echo_gadget.RegisterHandlers() hid_echo_gadget.RegisterHandlers() keyboard_gadget.RegisterHandlers() mouse_gadget.RegisterHandlers() diff --git a/tools/usb_gadget/echo_gadget.py b/tools/usb_gadget/echo_gadget.py new file mode 100644 index 0000000..b260381 --- /dev/null +++ b/tools/usb_gadget/echo_gadget.py @@ -0,0 +1,217 @@ +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""USB echo gadget module. + +This gadget has pairs of IN/OUT endpoints that echo packets back to the host. +""" + +import math +import struct +import uuid + +import gadget +import usb_constants +import usb_descriptors + + +class EchoGadget(gadget.Gadget): + """Echo gadget. + """ + + def __init__(self): + """Create an echo gadget. + """ + device_desc = usb_descriptors.DeviceDescriptor( + idVendor=usb_constants.VendorID.GOOGLE, + idProduct=usb_constants.ProductID.GOOGLE_ECHO_GADGET, + bcdUSB=0x0200, + iManufacturer=1, + iProduct=2, + iSerialNumber=3, + bcdDevice=0x0100) + + fs_config_desc = usb_descriptors.ConfigurationDescriptor( + bmAttributes=0x80, + MaxPower=50) + fs_intr_interface_desc = usb_descriptors.InterfaceDescriptor( + bInterfaceNumber=0, + bInterfaceClass=usb_constants.DeviceClass.VENDOR, + bInterfaceSubClass=0, + bInterfaceProtocol=0, + iInterface=4, + ) + fs_intr_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor( + bEndpointAddress=0x01, + bmAttributes=usb_constants.TransferType.INTERRUPT, + wMaxPacketSize=64, + bInterval=1 # 1ms + )) + fs_intr_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor( + bEndpointAddress=0x81, + bmAttributes=usb_constants.TransferType.INTERRUPT, + wMaxPacketSize=64, + bInterval=1 # 1ms + )) + fs_config_desc.AddInterface(fs_intr_interface_desc) + + fs_bulk_interface_desc = usb_descriptors.InterfaceDescriptor( + bInterfaceNumber=1, + bInterfaceClass=usb_constants.DeviceClass.VENDOR, + bInterfaceSubClass=0, + bInterfaceProtocol=0, + iInterface=5 + ) + fs_bulk_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor( + bEndpointAddress=0x02, + bmAttributes=usb_constants.TransferType.BULK, + wMaxPacketSize=64, + bInterval=0 + )) + fs_bulk_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor( + bEndpointAddress=0x82, + bmAttributes=usb_constants.TransferType.BULK, + wMaxPacketSize=64, + bInterval=0 + )) + fs_config_desc.AddInterface(fs_bulk_interface_desc) + + fs_config_desc.AddInterface(usb_descriptors.InterfaceDescriptor( + bInterfaceNumber=2, + bInterfaceClass=usb_constants.DeviceClass.VENDOR, + bInterfaceSubClass=0, + bInterfaceProtocol=0, + iInterface=6 + )) + fs_isoc_interface_desc = usb_descriptors.InterfaceDescriptor( + bInterfaceNumber=2, + bAlternateSetting=1, + bInterfaceClass=usb_constants.DeviceClass.VENDOR, + bInterfaceSubClass=0, + bInterfaceProtocol=0, + iInterface=6 + ) + fs_isoc_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor( + bEndpointAddress=0x03, + bmAttributes=usb_constants.TransferType.ISOCHRONOUS, + wMaxPacketSize=1023, + bInterval=1 # 1ms + )) + fs_isoc_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor( + bEndpointAddress=0x83, + bmAttributes=usb_constants.TransferType.ISOCHRONOUS, + wMaxPacketSize=1023, + bInterval=1 # 1ms + )) + fs_config_desc.AddInterface(fs_isoc_interface_desc) + + hs_config_desc = usb_descriptors.ConfigurationDescriptor( + bmAttributes=0x80, + MaxPower=50) + + hs_intr_interface_desc = usb_descriptors.InterfaceDescriptor( + bInterfaceNumber=0, + bInterfaceClass=usb_constants.DeviceClass.VENDOR, + bInterfaceSubClass=0, + bInterfaceProtocol=0, + iInterface=4 + ) + hs_intr_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor( + bEndpointAddress=0x01, + bmAttributes=usb_constants.TransferType.INTERRUPT, + wMaxPacketSize=64, + bInterval=4 # 1ms + )) + hs_intr_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor( + bEndpointAddress=0x81, + bmAttributes=usb_constants.TransferType.INTERRUPT, + wMaxPacketSize=64, + bInterval=4 # 1ms + )) + hs_config_desc.AddInterface(hs_intr_interface_desc) + + hs_bulk_interface_desc = usb_descriptors.InterfaceDescriptor( + bInterfaceNumber=1, + bInterfaceClass=usb_constants.DeviceClass.VENDOR, + bInterfaceSubClass=0, + bInterfaceProtocol=0, + iInterface=5 + ) + hs_bulk_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor( + bEndpointAddress=0x02, + bmAttributes=usb_constants.TransferType.BULK, + wMaxPacketSize=512, + bInterval=0 + )) + hs_bulk_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor( + bEndpointAddress=0x82, + bmAttributes=usb_constants.TransferType.BULK, + wMaxPacketSize=512, + bInterval=0 + )) + hs_config_desc.AddInterface(hs_bulk_interface_desc) + + hs_config_desc.AddInterface(usb_descriptors.InterfaceDescriptor( + bInterfaceNumber=2, + bInterfaceClass=usb_constants.DeviceClass.VENDOR, + bInterfaceSubClass=0, + bInterfaceProtocol=0, + iInterface=6 + )) + hs_isoc_interface_desc = usb_descriptors.InterfaceDescriptor( + bInterfaceNumber=2, + bAlternateSetting=1, + bInterfaceClass=usb_constants.DeviceClass.VENDOR, + bInterfaceSubClass=0, + bInterfaceProtocol=0, + iInterface=6 + ) + hs_isoc_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor( + bEndpointAddress=0x03, + bmAttributes=usb_constants.TransferType.ISOCHRONOUS, + wMaxPacketSize=1024, + bInterval=4 # 1ms + )) + hs_isoc_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor( + bEndpointAddress=0x83, + bmAttributes=usb_constants.TransferType.ISOCHRONOUS, + wMaxPacketSize=1024, + bInterval=4 # 1ms + )) + hs_config_desc.AddInterface(hs_isoc_interface_desc) + + super(EchoGadget, self).__init__( + device_desc, fs_config_desc, hs_config_desc) + self.AddStringDescriptor(1, 'Google Inc.') + self.AddStringDescriptor(2, 'Echo Gadget') + self.AddStringDescriptor(3, '{:06X}'.format(uuid.getnode())) + self.AddStringDescriptor(4, 'Interrupt Echo') + self.AddStringDescriptor(5, 'Bulk Echo') + self.AddStringDescriptor(6, 'Isochronous Echo') + + def ReceivePacket(self, endpoint, data): + """Echo a packet back to the host. + + Args: + endpoint: Incoming endpoint (must be an OUT pipe). + data: Packet data. + """ + assert endpoint & usb_constants.Dir.IN == 0 + + self.SendPacket(endpoint | usb_constants.Dir.IN, data) + +def RegisterHandlers(): + """Registers web request handlers with the application server.""" + + import server + from tornado import web + + class WebConfigureHandler(web.RequestHandler): + + def post(self): + server.SwitchGadget(EchoGadget()) + + server.app.add_handlers('.*$', [ + (r'/echo/configure', WebConfigureHandler), + ]) diff --git a/tools/usb_gadget/usb_constants.py b/tools/usb_gadget/usb_constants.py index f79454a..442674b 100644 --- a/tools/usb_gadget/usb_constants.py +++ b/tools/usb_gadget/usb_constants.py @@ -166,3 +166,4 @@ class ProductID(object): GOOGLE_KEYBOARD_GADGET = 0x58F1 GOOGLE_MOUSE_GADGET = 0x58F2 GOOGLE_HID_ECHO_GADGET = 0x58F3 + GOOGLE_ECHO_GADGET = 0x58F4 |