OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_device_handle_win.h" | 5 #include "device/usb/usb_device_handle_win.h" |
6 | 6 |
7 #include <usbioctl.h> | 7 #include <usbioctl.h> |
8 #include <usbspec.h> | 8 #include <usbspec.h> |
9 #include <winioctl.h> | 9 #include <winioctl.h> |
10 #include <winusb.h> | 10 #include <winusb.h> |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 | 151 |
152 void UsbDeviceHandleWin::ClearHalt(uint8_t endpoint, | 152 void UsbDeviceHandleWin::ClearHalt(uint8_t endpoint, |
153 const ResultCallback& callback) { | 153 const ResultCallback& callback) { |
154 DCHECK(thread_checker_.CalledOnValidThread()); | 154 DCHECK(thread_checker_.CalledOnValidThread()); |
155 if (!device_) { | 155 if (!device_) { |
156 callback.Run(false); | 156 callback.Run(false); |
157 return; | 157 return; |
158 } | 158 } |
159 } | 159 } |
160 | 160 |
161 void UsbDeviceHandleWin::ControlTransfer(UsbEndpointDirection direction, | 161 void UsbDeviceHandleWin::ControlTransfer(UsbTransferDirection direction, |
162 TransferRequestType request_type, | 162 UsbControlTransferType request_type, |
163 TransferRecipient recipient, | 163 UsbControlTransferRecipient recipient, |
164 uint8_t request, | 164 uint8_t request, |
165 uint16_t value, | 165 uint16_t value, |
166 uint16_t index, | 166 uint16_t index, |
167 scoped_refptr<net::IOBuffer> buffer, | 167 scoped_refptr<net::IOBuffer> buffer, |
168 size_t length, | 168 size_t length, |
169 unsigned int timeout, | 169 unsigned int timeout, |
170 const TransferCallback& callback) { | 170 const TransferCallback& callback) { |
171 DCHECK(thread_checker_.CalledOnValidThread()); | 171 DCHECK(thread_checker_.CalledOnValidThread()); |
172 | 172 |
173 if (!device_) { | 173 if (!device_) { |
174 task_runner_->PostTask( | 174 task_runner_->PostTask( |
175 FROM_HERE, base::Bind(callback, USB_TRANSFER_DISCONNECT, nullptr, 0)); | 175 FROM_HERE, |
176 base::Bind(callback, UsbTransferStatus::DISCONNECT, nullptr, 0)); | |
176 return; | 177 return; |
177 } | 178 } |
178 | 179 |
179 if (hub_handle_.IsValid()) { | 180 if (hub_handle_.IsValid()) { |
mcasas
2017/04/17 17:15:15
nit: this method would be clear with early-returns
Reilly Grant (use Gerrit)
2017/04/17 19:20:48
I have a follow-up CL that adds more logic to this
| |
180 if (direction == USB_DIRECTION_INBOUND && | 181 if (direction == UsbTransferDirection::INBOUND && |
181 request_type == TransferRequestType::STANDARD && | 182 request_type == UsbControlTransferType::STANDARD && |
182 recipient == TransferRecipient::DEVICE && | 183 recipient == UsbControlTransferRecipient::DEVICE && |
183 request == USB_REQUEST_GET_DESCRIPTOR) { | 184 request == USB_REQUEST_GET_DESCRIPTOR) { |
184 if ((value >> 8) == USB_DEVICE_DESCRIPTOR_TYPE) { | 185 if ((value >> 8) == USB_DEVICE_DESCRIPTOR_TYPE) { |
185 auto* node_connection_info = new USB_NODE_CONNECTION_INFORMATION_EX; | 186 auto* node_connection_info = new USB_NODE_CONNECTION_INFORMATION_EX; |
186 node_connection_info->ConnectionIndex = device_->port_number(); | 187 node_connection_info->ConnectionIndex = device_->port_number(); |
187 | 188 |
188 Request* request = MakeRequest(hub_handle_.Get()); | 189 Request* request = MakeRequest(hub_handle_.Get()); |
189 request->MaybeStartWatching( | 190 request->MaybeStartWatching( |
190 DeviceIoControl(hub_handle_.Get(), | 191 DeviceIoControl(hub_handle_.Get(), |
191 IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, | 192 IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, |
192 node_connection_info, sizeof(*node_connection_info), | 193 node_connection_info, sizeof(*node_connection_info), |
(...skipping 25 matching lines...) Expand all Loading... | |
218 request->overlapped()), | 219 request->overlapped()), |
219 base::Bind(&UsbDeviceHandleWin::GotDescriptorFromNodeConnection, | 220 base::Bind(&UsbDeviceHandleWin::GotDescriptorFromNodeConnection, |
220 weak_factory_.GetWeakPtr(), callback, request_buffer, | 221 weak_factory_.GetWeakPtr(), callback, request_buffer, |
221 buffer, length)); | 222 buffer, length)); |
222 return; | 223 return; |
223 } | 224 } |
224 } | 225 } |
225 | 226 |
226 // Unsupported transfer for hub. | 227 // Unsupported transfer for hub. |
227 task_runner_->PostTask( | 228 task_runner_->PostTask( |
228 FROM_HERE, base::Bind(callback, USB_TRANSFER_ERROR, nullptr, 0)); | 229 FROM_HERE, |
230 base::Bind(callback, UsbTransferStatus::TRANSFER_ERROR, nullptr, 0)); | |
229 return; | 231 return; |
230 } | 232 } |
231 | 233 |
232 // Regular control transfers unimplemented. | 234 // Regular control transfers unimplemented. |
233 task_runner_->PostTask(FROM_HERE, | 235 task_runner_->PostTask( |
234 base::Bind(callback, USB_TRANSFER_ERROR, nullptr, 0)); | 236 FROM_HERE, |
237 base::Bind(callback, UsbTransferStatus::TRANSFER_ERROR, nullptr, 0)); | |
235 } | 238 } |
236 | 239 |
237 void UsbDeviceHandleWin::IsochronousTransferIn( | 240 void UsbDeviceHandleWin::IsochronousTransferIn( |
238 uint8_t endpoint_number, | 241 uint8_t endpoint_number, |
239 const std::vector<uint32_t>& packet_lengths, | 242 const std::vector<uint32_t>& packet_lengths, |
240 unsigned int timeout, | 243 unsigned int timeout, |
241 const IsochronousTransferCallback& callback) { | 244 const IsochronousTransferCallback& callback) { |
242 DCHECK(thread_checker_.CalledOnValidThread()); | 245 DCHECK(thread_checker_.CalledOnValidThread()); |
243 } | 246 } |
244 | 247 |
245 void UsbDeviceHandleWin::IsochronousTransferOut( | 248 void UsbDeviceHandleWin::IsochronousTransferOut( |
246 uint8_t endpoint_number, | 249 uint8_t endpoint_number, |
247 scoped_refptr<net::IOBuffer> buffer, | 250 scoped_refptr<net::IOBuffer> buffer, |
248 const std::vector<uint32_t>& packet_lengths, | 251 const std::vector<uint32_t>& packet_lengths, |
249 unsigned int timeout, | 252 unsigned int timeout, |
250 const IsochronousTransferCallback& callback) { | 253 const IsochronousTransferCallback& callback) { |
251 DCHECK(thread_checker_.CalledOnValidThread()); | 254 DCHECK(thread_checker_.CalledOnValidThread()); |
252 } | 255 } |
253 | 256 |
254 void UsbDeviceHandleWin::GenericTransfer(UsbEndpointDirection direction, | 257 void UsbDeviceHandleWin::GenericTransfer(UsbTransferDirection direction, |
255 uint8_t endpoint_number, | 258 uint8_t endpoint_number, |
256 scoped_refptr<net::IOBuffer> buffer, | 259 scoped_refptr<net::IOBuffer> buffer, |
257 size_t length, | 260 size_t length, |
258 unsigned int timeout, | 261 unsigned int timeout, |
259 const TransferCallback& callback) { | 262 const TransferCallback& callback) { |
260 // This one must be callable from any thread. | 263 // This one must be callable from any thread. |
261 } | 264 } |
262 | 265 |
263 const UsbInterfaceDescriptor* UsbDeviceHandleWin::FindInterfaceByEndpoint( | 266 const UsbInterfaceDescriptor* UsbDeviceHandleWin::FindInterfaceByEndpoint( |
264 uint8_t endpoint_address) { | 267 uint8_t endpoint_address) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 DWORD win32_result, | 306 DWORD win32_result, |
304 size_t bytes_transferred) { | 307 size_t bytes_transferred) { |
305 USB_NODE_CONNECTION_INFORMATION_EX* node_connection_info = | 308 USB_NODE_CONNECTION_INFORMATION_EX* node_connection_info = |
306 static_cast<USB_NODE_CONNECTION_INFORMATION_EX*>( | 309 static_cast<USB_NODE_CONNECTION_INFORMATION_EX*>( |
307 node_connection_info_ptr); | 310 node_connection_info_ptr); |
308 std::unique_ptr<Request> request = UnlinkRequest(request_ptr); | 311 std::unique_ptr<Request> request = UnlinkRequest(request_ptr); |
309 | 312 |
310 if (win32_result != ERROR_SUCCESS) { | 313 if (win32_result != ERROR_SUCCESS) { |
311 SetLastError(win32_result); | 314 SetLastError(win32_result); |
312 USB_PLOG(ERROR) << "Failed to get node connection information"; | 315 USB_PLOG(ERROR) << "Failed to get node connection information"; |
313 callback.Run(USB_TRANSFER_ERROR, nullptr, 0); | 316 callback.Run(UsbTransferStatus::TRANSFER_ERROR, nullptr, 0); |
314 return; | 317 return; |
315 } | 318 } |
316 | 319 |
317 DCHECK_EQ(bytes_transferred, sizeof(USB_NODE_CONNECTION_INFORMATION_EX)); | 320 DCHECK_EQ(bytes_transferred, sizeof(USB_NODE_CONNECTION_INFORMATION_EX)); |
318 bytes_transferred = std::min(sizeof(USB_DEVICE_DESCRIPTOR), buffer_length); | 321 bytes_transferred = std::min(sizeof(USB_DEVICE_DESCRIPTOR), buffer_length); |
319 memcpy(buffer->data(), &node_connection_info->DeviceDescriptor, | 322 memcpy(buffer->data(), &node_connection_info->DeviceDescriptor, |
320 bytes_transferred); | 323 bytes_transferred); |
321 callback.Run(USB_TRANSFER_COMPLETED, buffer, bytes_transferred); | 324 callback.Run(UsbTransferStatus::COMPLETED, buffer, bytes_transferred); |
322 } | 325 } |
323 | 326 |
324 void UsbDeviceHandleWin::GotDescriptorFromNodeConnection( | 327 void UsbDeviceHandleWin::GotDescriptorFromNodeConnection( |
325 const TransferCallback& callback, | 328 const TransferCallback& callback, |
326 scoped_refptr<net::IOBuffer> request_buffer, | 329 scoped_refptr<net::IOBuffer> request_buffer, |
327 scoped_refptr<net::IOBuffer> original_buffer, | 330 scoped_refptr<net::IOBuffer> original_buffer, |
328 size_t original_buffer_length, | 331 size_t original_buffer_length, |
329 Request* request_ptr, | 332 Request* request_ptr, |
330 DWORD win32_result, | 333 DWORD win32_result, |
331 size_t bytes_transferred) { | 334 size_t bytes_transferred) { |
332 std::unique_ptr<Request> request = UnlinkRequest(request_ptr); | 335 std::unique_ptr<Request> request = UnlinkRequest(request_ptr); |
333 | 336 |
334 if (win32_result != ERROR_SUCCESS) { | 337 if (win32_result != ERROR_SUCCESS) { |
335 SetLastError(win32_result); | 338 SetLastError(win32_result); |
336 USB_PLOG(ERROR) << "Failed to read descriptor from node connection"; | 339 USB_PLOG(ERROR) << "Failed to read descriptor from node connection"; |
337 callback.Run(USB_TRANSFER_ERROR, nullptr, 0); | 340 callback.Run(UsbTransferStatus::TRANSFER_ERROR, nullptr, 0); |
338 return; | 341 return; |
339 } | 342 } |
340 | 343 |
341 DCHECK_GE(bytes_transferred, sizeof(USB_DESCRIPTOR_REQUEST)); | 344 DCHECK_GE(bytes_transferred, sizeof(USB_DESCRIPTOR_REQUEST)); |
342 bytes_transferred -= sizeof(USB_DESCRIPTOR_REQUEST); | 345 bytes_transferred -= sizeof(USB_DESCRIPTOR_REQUEST); |
343 memcpy(original_buffer->data(), | 346 memcpy(original_buffer->data(), |
344 request_buffer->data() + sizeof(USB_DESCRIPTOR_REQUEST), | 347 request_buffer->data() + sizeof(USB_DESCRIPTOR_REQUEST), |
345 bytes_transferred); | 348 bytes_transferred); |
346 callback.Run(USB_TRANSFER_COMPLETED, original_buffer, bytes_transferred); | 349 callback.Run(UsbTransferStatus::COMPLETED, original_buffer, |
350 bytes_transferred); | |
347 } | 351 } |
348 | 352 |
349 } // namespace device | 353 } // namespace device |
OLD | NEW |