Home | History | Annotate | Download | only in usb
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/usb/usb_interface.h"
      6 
      7 #include "base/logging.h"
      8 #include "third_party/libusb/src/libusb/libusb.h"
      9 
     10 UsbEndpointDescriptor::UsbEndpointDescriptor(
     11     scoped_refptr<const UsbConfigDescriptor> config,
     12     PlatformUsbEndpointDescriptor descriptor)
     13     : config_(config), descriptor_(descriptor) {
     14 }
     15 
     16 UsbEndpointDescriptor::~UsbEndpointDescriptor() {}
     17 
     18 int UsbEndpointDescriptor::GetAddress() const {
     19   return descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK;
     20 }
     21 
     22 UsbEndpointDirection UsbEndpointDescriptor::GetDirection() const {
     23   switch (descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
     24     case LIBUSB_ENDPOINT_IN:
     25       return USB_DIRECTION_INBOUND;
     26     case LIBUSB_ENDPOINT_OUT:
     27       return USB_DIRECTION_OUTBOUND;
     28     default:
     29       NOTREACHED();
     30       return USB_DIRECTION_INBOUND;
     31   }
     32 }
     33 
     34 int UsbEndpointDescriptor::GetMaximumPacketSize() const {
     35   return descriptor_->wMaxPacketSize;
     36 }
     37 
     38 UsbSynchronizationType UsbEndpointDescriptor::GetSynchronizationType() const {
     39   switch (descriptor_->bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) {
     40     case LIBUSB_ISO_SYNC_TYPE_NONE:
     41       return USB_SYNCHRONIZATION_NONE;
     42     case LIBUSB_ISO_SYNC_TYPE_ASYNC:
     43       return USB_SYNCHRONIZATION_ASYNCHRONOUS;
     44     case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
     45       return USB_SYNCHRONIZATION_ADAPTIVE;
     46     case LIBUSB_ISO_SYNC_TYPE_SYNC:
     47       return USB_SYNCHRONIZATION_SYNCHRONOUS;
     48     default:
     49       NOTREACHED();
     50       return USB_SYNCHRONIZATION_NONE;
     51   }
     52 }
     53 
     54 UsbTransferType UsbEndpointDescriptor::GetTransferType() const {
     55   switch (descriptor_->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
     56     case LIBUSB_TRANSFER_TYPE_CONTROL:
     57       return USB_TRANSFER_CONTROL;
     58     case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
     59       return USB_TRANSFER_ISOCHRONOUS;
     60     case LIBUSB_TRANSFER_TYPE_BULK:
     61       return USB_TRANSFER_BULK;
     62     case LIBUSB_TRANSFER_TYPE_INTERRUPT:
     63       return USB_TRANSFER_INTERRUPT;
     64     default:
     65       NOTREACHED();
     66       return USB_TRANSFER_CONTROL;
     67   }
     68 }
     69 
     70 UsbUsageType UsbEndpointDescriptor::GetUsageType() const {
     71   switch (descriptor_->bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
     72     case LIBUSB_ISO_USAGE_TYPE_DATA:
     73       return USB_USAGE_DATA;
     74     case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
     75       return USB_USAGE_FEEDBACK;
     76     case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
     77       return USB_USAGE_EXPLICIT_FEEDBACK;
     78     default:
     79       NOTREACHED();
     80       return USB_USAGE_DATA;
     81   }
     82 }
     83 
     84 int UsbEndpointDescriptor::GetPollingInterval() const {
     85   return descriptor_->bInterval;
     86 }
     87 
     88 UsbInterfaceDescriptor::UsbInterfaceDescriptor(
     89     scoped_refptr<const UsbConfigDescriptor> config,
     90     PlatformUsbInterfaceDescriptor descriptor)
     91     : config_(config), descriptor_(descriptor) {
     92 }
     93 
     94 UsbInterfaceDescriptor::~UsbInterfaceDescriptor() {}
     95 
     96 size_t UsbInterfaceDescriptor::GetNumEndpoints() const {
     97   return descriptor_->bNumEndpoints;
     98 }
     99 
    100 scoped_refptr<const UsbEndpointDescriptor>
    101     UsbInterfaceDescriptor::GetEndpoint(size_t index) const {
    102   return make_scoped_refptr(new UsbEndpointDescriptor(config_,
    103       &descriptor_->endpoint[index]));
    104 }
    105 
    106 int UsbInterfaceDescriptor::GetInterfaceNumber() const {
    107   return descriptor_->bInterfaceNumber;
    108 }
    109 
    110 int UsbInterfaceDescriptor::GetAlternateSetting() const {
    111   return descriptor_->bAlternateSetting;
    112 }
    113 
    114 int UsbInterfaceDescriptor::GetInterfaceClass() const {
    115   return descriptor_->bInterfaceClass;
    116 }
    117 
    118 int UsbInterfaceDescriptor::GetInterfaceSubclass() const {
    119   return descriptor_->bInterfaceSubClass;
    120 }
    121 
    122 int UsbInterfaceDescriptor::GetInterfaceProtocol() const {
    123   return descriptor_->bInterfaceProtocol;
    124 }
    125 
    126 UsbInterface::UsbInterface(scoped_refptr<const UsbConfigDescriptor> config,
    127     PlatformUsbInterface usbInterface)
    128     : config_(config), interface_(usbInterface) {
    129 }
    130 
    131 UsbInterface::~UsbInterface() {}
    132 
    133 size_t UsbInterface::GetNumAltSettings() const {
    134   return interface_->num_altsetting;
    135 }
    136 
    137 scoped_refptr<const UsbInterfaceDescriptor>
    138     UsbInterface::GetAltSetting(size_t index) const {
    139   return make_scoped_refptr(new UsbInterfaceDescriptor(config_,
    140       &interface_->altsetting[index]));
    141 }
    142 
    143 UsbConfigDescriptor::UsbConfigDescriptor()
    144     : config_(NULL) {
    145 }
    146 
    147 UsbConfigDescriptor::~UsbConfigDescriptor() {
    148   if (config_ != NULL) {
    149     libusb_free_config_descriptor(config_);
    150     config_ = NULL;
    151   }
    152 }
    153 
    154 void UsbConfigDescriptor::Reset(PlatformUsbConfigDescriptor config) {
    155   config_ = config;
    156 }
    157 
    158 size_t UsbConfigDescriptor::GetNumInterfaces() const {
    159   return config_->bNumInterfaces;
    160 }
    161 
    162 scoped_refptr<const UsbInterface>
    163     UsbConfigDescriptor::GetInterface(size_t index) const {
    164   return make_scoped_refptr(new UsbInterface(make_scoped_refptr(this),
    165           &config_->interface[index]));
    166 }
    167