OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/usb/usb_descriptors.h" | 5 #include "device/usb/usb_descriptors.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 } | 75 } |
76 std::advance(it, length); | 76 std::advance(it, length); |
77 } | 77 } |
78 } | 78 } |
79 | 79 |
80 void OnDoneReadingConfigDescriptors( | 80 void OnDoneReadingConfigDescriptors( |
81 scoped_refptr<UsbDeviceHandle> device_handle, | 81 scoped_refptr<UsbDeviceHandle> device_handle, |
82 std::unique_ptr<UsbDeviceDescriptor> desc, | 82 std::unique_ptr<UsbDeviceDescriptor> desc, |
83 const base::Callback<void(std::unique_ptr<UsbDeviceDescriptor>)>& | 83 const base::Callback<void(std::unique_ptr<UsbDeviceDescriptor>)>& |
84 callback) { | 84 callback) { |
85 if (desc->num_configurations == desc->configurations.size()) | 85 if (desc->num_configurations == desc->configurations.size()) { |
86 callback.Run(std::move(desc)); | 86 callback.Run(std::move(desc)); |
87 else | 87 } else { |
| 88 LOG(ERROR) << "Failed to read all configuration descriptors. Expected " |
| 89 << static_cast<int>(desc->num_configurations) << ", got " |
| 90 << desc->configurations.size() << "."; |
88 callback.Run(nullptr); | 91 callback.Run(nullptr); |
| 92 } |
89 } | 93 } |
90 | 94 |
91 void OnReadConfigDescriptor(UsbDeviceDescriptor* desc, | 95 void OnReadConfigDescriptor(UsbDeviceDescriptor* desc, |
92 const base::Closure& closure, | 96 const base::Closure& closure, |
93 UsbTransferStatus status, | 97 UsbTransferStatus status, |
94 scoped_refptr<IOBuffer> buffer, | 98 scoped_refptr<IOBuffer> buffer, |
95 size_t length) { | 99 size_t length) { |
96 if (status == USB_TRANSFER_COMPLETED) | 100 if (status == USB_TRANSFER_COMPLETED) { |
97 desc->Parse(std::vector<uint8_t>(buffer->data(), buffer->data() + length)); | 101 if (!desc->Parse( |
| 102 std::vector<uint8_t>(buffer->data(), buffer->data() + length))) { |
| 103 LOG(ERROR) << "Failed to parse configuration descriptor."; |
| 104 } |
| 105 } else { |
| 106 LOG(ERROR) << "Failed to read configuration descriptor."; |
| 107 } |
98 closure.Run(); | 108 closure.Run(); |
99 } | 109 } |
100 | 110 |
101 void OnReadConfigDescriptorHeader(scoped_refptr<UsbDeviceHandle> device_handle, | 111 void OnReadConfigDescriptorHeader(scoped_refptr<UsbDeviceHandle> device_handle, |
102 UsbDeviceDescriptor* desc, | 112 UsbDeviceDescriptor* desc, |
103 uint8_t index, | 113 uint8_t index, |
104 const base::Closure& closure, | 114 const base::Closure& closure, |
105 UsbTransferStatus status, | 115 UsbTransferStatus status, |
106 scoped_refptr<IOBuffer> header, | 116 scoped_refptr<IOBuffer> header, |
107 size_t length) { | 117 size_t length) { |
108 if (status == USB_TRANSFER_COMPLETED && length == 4) { | 118 if (status == USB_TRANSFER_COMPLETED && length == 4) { |
109 const uint8_t* data = reinterpret_cast<const uint8_t*>(header->data()); | 119 const uint8_t* data = reinterpret_cast<const uint8_t*>(header->data()); |
110 uint16_t total_length = data[2] | data[3] << 8; | 120 uint16_t total_length = data[2] | data[3] << 8; |
111 scoped_refptr<IOBuffer> buffer = new IOBuffer(total_length); | 121 scoped_refptr<IOBuffer> buffer = new IOBuffer(total_length); |
112 device_handle->ControlTransfer( | 122 device_handle->ControlTransfer( |
113 USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD, | 123 USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD, |
114 UsbDeviceHandle::DEVICE, kGetDescriptorRequest, | 124 UsbDeviceHandle::DEVICE, kGetDescriptorRequest, |
115 kConfigurationDescriptorType << 8 | index, 0, buffer, total_length, | 125 kConfigurationDescriptorType << 8 | index, 0, buffer, total_length, |
116 kControlTransferTimeout, | 126 kControlTransferTimeout, |
117 base::Bind(&OnReadConfigDescriptor, desc, closure)); | 127 base::Bind(&OnReadConfigDescriptor, desc, closure)); |
118 } else { | 128 } else { |
| 129 LOG(ERROR) << "Failed to read length for configuration " |
| 130 << static_cast<int>(index) << "."; |
119 closure.Run(); | 131 closure.Run(); |
120 } | 132 } |
121 } | 133 } |
122 | 134 |
123 void OnReadDeviceDescriptor( | 135 void OnReadDeviceDescriptor( |
124 scoped_refptr<UsbDeviceHandle> device_handle, | 136 scoped_refptr<UsbDeviceHandle> device_handle, |
125 const base::Callback<void(std::unique_ptr<UsbDeviceDescriptor>)>& callback, | 137 const base::Callback<void(std::unique_ptr<UsbDeviceDescriptor>)>& callback, |
126 UsbTransferStatus status, | 138 UsbTransferStatus status, |
127 scoped_refptr<IOBuffer> buffer, | 139 scoped_refptr<IOBuffer> buffer, |
128 size_t length) { | 140 size_t length) { |
129 if (status != USB_TRANSFER_COMPLETED) { | 141 if (status != USB_TRANSFER_COMPLETED) { |
| 142 LOG(ERROR) << "Failed to read device descriptor."; |
130 callback.Run(nullptr); | 143 callback.Run(nullptr); |
131 return; | 144 return; |
132 } | 145 } |
133 | 146 |
134 std::unique_ptr<UsbDeviceDescriptor> desc(new UsbDeviceDescriptor()); | 147 std::unique_ptr<UsbDeviceDescriptor> desc(new UsbDeviceDescriptor()); |
135 if (!desc->Parse( | 148 if (!desc->Parse( |
136 std::vector<uint8_t>(buffer->data(), buffer->data() + length))) { | 149 std::vector<uint8_t>(buffer->data(), buffer->data() + length))) { |
| 150 LOG(ERROR) << "Device descriptor parsing error."; |
137 callback.Run(nullptr); | 151 callback.Run(nullptr); |
138 return; | 152 return; |
139 } | 153 } |
140 | 154 |
141 if (desc->num_configurations == 0) { | 155 if (desc->num_configurations == 0) { |
142 callback.Run(std::move(desc)); | 156 callback.Run(std::move(desc)); |
143 return; | 157 return; |
144 } | 158 } |
145 | 159 |
146 uint8_t num_configurations = desc->num_configurations; | 160 uint8_t num_configurations = desc->num_configurations; |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 case kDeviceDescriptorType: | 437 case kDeviceDescriptorType: |
424 if (configurations.size() > 0 || length < kDeviceDescriptorLength) | 438 if (configurations.size() > 0 || length < kDeviceDescriptorLength) |
425 return false; | 439 return false; |
426 usb_version = data[2] | data[3] << 8; | 440 usb_version = data[2] | data[3] << 8; |
427 device_class = data[4]; | 441 device_class = data[4]; |
428 device_subclass = data[5]; | 442 device_subclass = data[5]; |
429 device_protocol = data[6]; | 443 device_protocol = data[6]; |
430 vendor_id = data[8] | data[9] << 8; | 444 vendor_id = data[8] | data[9] << 8; |
431 product_id = data[10] | data[11] << 8; | 445 product_id = data[10] | data[11] << 8; |
432 device_version = data[12] | data[13] << 8; | 446 device_version = data[12] | data[13] << 8; |
| 447 i_manufacturer = data[14]; |
| 448 i_product = data[15]; |
| 449 i_serial_number = data[16]; |
433 num_configurations = data[17]; | 450 num_configurations = data[17]; |
434 break; | 451 break; |
435 case kConfigurationDescriptorType: | 452 case kConfigurationDescriptorType: |
436 if (length < kConfigurationDescriptorLength) | 453 if (length < kConfigurationDescriptorLength) |
437 return false; | 454 return false; |
438 if (last_config) | 455 if (last_config) |
439 last_config->AssignFirstInterfaceNumbers(); | 456 last_config->AssignFirstInterfaceNumbers(); |
440 configurations.emplace_back(data); | 457 configurations.emplace_back(data); |
441 last_config = &configurations.back(); | 458 last_config = &configurations.back(); |
442 last_interface = nullptr; | 459 last_interface = nullptr; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 callback.Run(std::move(index_map)); | 535 callback.Run(std::move(index_map)); |
519 return; | 536 return; |
520 } | 537 } |
521 | 538 |
522 ReadStringDescriptor(device_handle, 0, 0, | 539 ReadStringDescriptor(device_handle, 0, 0, |
523 base::Bind(&OnReadLanguageIds, device_handle, | 540 base::Bind(&OnReadLanguageIds, device_handle, |
524 base::Passed(&index_map), callback)); | 541 base::Passed(&index_map), callback)); |
525 } | 542 } |
526 | 543 |
527 } // namespace device | 544 } // namespace device |
OLD | NEW |