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 UsbInterfaceAltSettingDescriptor::UsbInterfaceAltSettingDescriptor(
     89     scoped_refptr<const UsbConfigDescriptor> config,
     90     PlatformUsbInterfaceDescriptor descriptor)
     91     : config_(config), descriptor_(descriptor) {
     92 }
     93 
     94 UsbInterfaceAltSettingDescriptor::~UsbInterfaceAltSettingDescriptor() {}
     95 
     96 size_t UsbInterfaceAltSettingDescriptor::GetNumEndpoints() const {
     97   return descriptor_->bNumEndpoints;
     98 }
     99 
    100 scoped_refptr<const UsbEndpointDescriptor>
    101     UsbInterfaceAltSettingDescriptor::GetEndpoint(size_t index) const {
    102   return new UsbEndpointDescriptor(config_, &descriptor_->endpoint[index]);
    103 }
    104 
    105 int UsbInterfaceAltSettingDescriptor::GetInterfaceNumber() const {
    106   return descriptor_->bInterfaceNumber;
    107 }
    108 
    109 int UsbInterfaceAltSettingDescriptor::GetAlternateSetting() const {
    110   return descriptor_->bAlternateSetting;
    111 }
    112 
    113 int UsbInterfaceAltSettingDescriptor::GetInterfaceClass() const {
    114   return descriptor_->bInterfaceClass;
    115 }
    116 
    117 int UsbInterfaceAltSettingDescriptor::GetInterfaceSubclass() const {
    118   return descriptor_->bInterfaceSubClass;
    119 }
    120 
    121 int UsbInterfaceAltSettingDescriptor::GetInterfaceProtocol() const {
    122   return descriptor_->bInterfaceProtocol;
    123 }
    124 
    125 UsbInterfaceDescriptor::UsbInterfaceDescriptor(
    126     scoped_refptr<const UsbConfigDescriptor> config,
    127     PlatformUsbInterface usbInterface)
    128     : config_(config), interface_(usbInterface) {
    129 }
    130 
    131 UsbInterfaceDescriptor::~UsbInterfaceDescriptor() {}
    132 
    133 size_t UsbInterfaceDescriptor::GetNumAltSettings() const {
    134   return interface_->num_altsetting;
    135 }
    136 
    137 scoped_refptr<const UsbInterfaceAltSettingDescriptor>
    138     UsbInterfaceDescriptor::GetAltSetting(size_t index) const {
    139   return new UsbInterfaceAltSettingDescriptor(config_,
    140                                               &interface_->altsetting[index]);
    141 }
    142 
    143 UsbConfigDescriptor::UsbConfigDescriptor(PlatformUsbConfigDescriptor config)
    144     : config_(config) {
    145 }
    146 
    147 UsbConfigDescriptor::~UsbConfigDescriptor() {
    148   if (config_ != NULL) {
    149     libusb_free_config_descriptor(config_);
    150     config_ = NULL;
    151   }
    152 }
    153 
    154 size_t UsbConfigDescriptor::GetNumInterfaces() const {
    155   return config_->bNumInterfaces;
    156 }
    157 
    158 scoped_refptr<const UsbInterfaceDescriptor>
    159     UsbConfigDescriptor::GetInterface(size_t index) const {
    160   return new UsbInterfaceDescriptor(this, &config_->interface[index]);
    161 }
    162