summaryrefslogtreecommitdiff
path: root/chromium/device/usb/usb_descriptors.h
blob: b0f5be6b6f4904313e1ec5707093f841afb7b206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 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.

#ifndef DEVICE_USB_USB_DESCRIPTORS_H_
#define DEVICE_USB_USB_DESCRIPTORS_H_

#include <stdint.h>

#include <map>
#include <memory>
#include <vector>

#include "base/callback_forward.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string16.h"
#include "device/usb/public/interfaces/device.mojom.h"

namespace device {

class UsbDeviceHandle;

using UsbTransferType = mojom::UsbTransferType;
using UsbTransferDirection = mojom::UsbTransferDirection;

enum UsbSynchronizationType {
  USB_SYNCHRONIZATION_NONE = 0,
  USB_SYNCHRONIZATION_ASYNCHRONOUS,
  USB_SYNCHRONIZATION_ADAPTIVE,
  USB_SYNCHRONIZATION_SYNCHRONOUS,
};

enum UsbUsageType {
  // Isochronous endpoint usages.
  USB_USAGE_DATA = 0,
  USB_USAGE_FEEDBACK,
  USB_USAGE_EXPLICIT_FEEDBACK,
  // Interrupt endpoint usages.
  USB_USAGE_PERIODIC,
  USB_USAGE_NOTIFICATION,
  // Not currently defined by any spec.
  USB_USAGE_RESERVED,
};

struct UsbEndpointDescriptor {
  UsbEndpointDescriptor(const uint8_t* data);
  UsbEndpointDescriptor(uint8_t address,
                        uint8_t attributes,
                        uint16_t maximum_packet_size,
                        uint8_t polling_interval);
  UsbEndpointDescriptor() = delete;
  UsbEndpointDescriptor(const UsbEndpointDescriptor& other);
  ~UsbEndpointDescriptor();

  uint8_t address;
  UsbTransferDirection direction;
  uint16_t maximum_packet_size;
  UsbSynchronizationType synchronization_type;
  UsbTransferType transfer_type;
  UsbUsageType usage_type;
  uint8_t polling_interval;
  std::vector<uint8_t> extra_data;
};

struct UsbInterfaceDescriptor {
  UsbInterfaceDescriptor(const uint8_t* data);
  UsbInterfaceDescriptor(uint8_t interface_number,
                         uint8_t alternate_setting,
                         uint8_t interface_class,
                         uint8_t interface_subclass,
                         uint8_t interface_protocol);
  UsbInterfaceDescriptor() = delete;
  UsbInterfaceDescriptor(const UsbInterfaceDescriptor& other);
  ~UsbInterfaceDescriptor();

  uint8_t interface_number;
  uint8_t alternate_setting;
  uint8_t interface_class;
  uint8_t interface_subclass;
  uint8_t interface_protocol;
  std::vector<UsbEndpointDescriptor> endpoints;
  std::vector<uint8_t> extra_data;
  // First interface of the function to which this interface belongs.
  uint8_t first_interface;
};

struct UsbConfigDescriptor {
  UsbConfigDescriptor(const uint8_t* data);
  UsbConfigDescriptor(uint8_t configuration_value,
                      bool self_powered,
                      bool remote_wakeup,
                      uint8_t maximum_power);
  UsbConfigDescriptor() = delete;
  UsbConfigDescriptor(const UsbConfigDescriptor& other);
  ~UsbConfigDescriptor();

  // Scans through |extra_data| for interface association descriptors and
  // populates |first_interface| for each interface in this configuration.
  void AssignFirstInterfaceNumbers();

  uint8_t configuration_value;
  bool self_powered;
  bool remote_wakeup;
  uint8_t maximum_power;
  std::vector<UsbInterfaceDescriptor> interfaces;
  std::vector<uint8_t> extra_data;
};

struct UsbDeviceDescriptor {
  UsbDeviceDescriptor();
  UsbDeviceDescriptor(const UsbDeviceDescriptor& other);
  ~UsbDeviceDescriptor();

  // Parses |buffer| for USB descriptors. Any configuration descriptors found
  // will be added to |configurations|. If a device descriptor is found it will
  // be used to populate this struct's fields. This function may be called more
  // than once (i.e. for multiple buffers containing a configuration descriptor
  // each).
  bool Parse(const std::vector<uint8_t>& buffer);

  uint16_t usb_version = 0;
  uint8_t device_class = 0;
  uint8_t device_subclass = 0;
  uint8_t device_protocol = 0;
  uint16_t vendor_id = 0;
  uint16_t product_id = 0;
  uint16_t device_version = 0;
  uint8_t i_manufacturer = 0;
  uint8_t i_product = 0;
  uint8_t i_serial_number = 0;
  uint8_t num_configurations = 0;
  std::vector<UsbConfigDescriptor> configurations;
};

void ReadUsbDescriptors(
    scoped_refptr<UsbDeviceHandle> device_handle,
    const base::Callback<void(std::unique_ptr<UsbDeviceDescriptor>)>& callback);

bool ParseUsbStringDescriptor(const std::vector<uint8_t>& descriptor,
                              base::string16* output);

void ReadUsbStringDescriptors(
    scoped_refptr<UsbDeviceHandle> device_handle,
    std::unique_ptr<std::map<uint8_t, base::string16>> index_map,
    const base::Callback<
        void(std::unique_ptr<std::map<uint8_t, base::string16>>)>& callback);

}  // namespace device

#endif  // DEVICE_USB_USB_DESCRIPTORS_H_