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 "components/usb_service/usb_device_handle_impl.h" | 5 #include "components/usb_service/usb_device_handle_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/bind.h" |
| 11 #include "base/location.h" |
| 12 #include "base/single_thread_task_runner.h" |
11 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
12 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
13 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/thread_task_runner_handle.h" |
14 #include "components/usb_service/usb_context.h" | 17 #include "components/usb_service/usb_context.h" |
15 #include "components/usb_service/usb_device_impl.h" | 18 #include "components/usb_service/usb_device_impl.h" |
16 #include "components/usb_service/usb_error.h" | 19 #include "components/usb_service/usb_error.h" |
17 #include "components/usb_service/usb_interface.h" | 20 #include "components/usb_service/usb_interface.h" |
18 #include "components/usb_service/usb_service.h" | 21 #include "components/usb_service/usb_service.h" |
19 #include "content/public/browser/browser_thread.h" | |
20 #include "third_party/libusb/src/libusb/libusb.h" | 22 #include "third_party/libusb/src/libusb/libusb.h" |
21 | 23 |
22 using content::BrowserThread; | |
23 | |
24 namespace usb_service { | 24 namespace usb_service { |
25 | 25 |
26 typedef libusb_device* PlatformUsbDevice; | 26 typedef libusb_device* PlatformUsbDevice; |
27 | 27 |
28 void HandleTransferCompletion(usb_service::PlatformUsbTransferHandle transfer); | 28 void HandleTransferCompletion(usb_service::PlatformUsbTransferHandle transfer); |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 static uint8 ConvertTransferDirection(const UsbEndpointDirection direction) { | 32 static uint8 ConvertTransferDirection(const UsbEndpointDirection direction) { |
33 switch (direction) { | 33 switch (direction) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 case LIBUSB_TRANSFER_OVERFLOW: | 96 case LIBUSB_TRANSFER_OVERFLOW: |
97 return USB_TRANSFER_OVERFLOW; | 97 return USB_TRANSFER_OVERFLOW; |
98 case LIBUSB_TRANSFER_CANCELLED: | 98 case LIBUSB_TRANSFER_CANCELLED: |
99 return USB_TRANSFER_CANCELLED; | 99 return USB_TRANSFER_CANCELLED; |
100 default: | 100 default: |
101 NOTREACHED(); | 101 NOTREACHED(); |
102 return USB_TRANSFER_ERROR; | 102 return USB_TRANSFER_ERROR; |
103 } | 103 } |
104 } | 104 } |
105 | 105 |
106 static void LIBUSB_CALL | |
107 PlatformTransferCompletionCallback(PlatformUsbTransferHandle transfer) { | |
108 BrowserThread::PostTask(BrowserThread::FILE, | |
109 FROM_HERE, | |
110 base::Bind(HandleTransferCompletion, transfer)); | |
111 } | |
112 | |
113 } // namespace | 106 } // namespace |
114 | 107 |
115 void HandleTransferCompletion(PlatformUsbTransferHandle transfer) { | |
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
117 UsbDeviceHandleImpl* const device_handle = | |
118 reinterpret_cast<UsbDeviceHandleImpl*>(transfer->user_data); | |
119 CHECK(device_handle) << "Device handle is closed before transfer finishes."; | |
120 device_handle->TransferComplete(transfer); | |
121 libusb_free_transfer(transfer); | |
122 } | |
123 | |
124 class UsbDeviceHandleImpl::InterfaceClaimer | 108 class UsbDeviceHandleImpl::InterfaceClaimer |
125 : public base::RefCountedThreadSafe<UsbDeviceHandleImpl::InterfaceClaimer> { | 109 : public base::RefCountedThreadSafe<UsbDeviceHandleImpl::InterfaceClaimer> { |
126 public: | 110 public: |
127 InterfaceClaimer(const scoped_refptr<UsbDeviceHandleImpl> handle, | 111 InterfaceClaimer(const scoped_refptr<UsbDeviceHandleImpl> handle, |
128 const int interface_number); | 112 const int interface_number); |
129 | 113 |
130 bool Claim() const; | 114 bool Claim() const; |
131 | 115 |
132 int alternate_setting() const { return alternate_setting_; } | 116 int alternate_setting() const { return alternate_setting_; } |
133 void set_alternate_setting(const int alternate_setting) { | 117 void set_alternate_setting(const int alternate_setting) { |
(...skipping 29 matching lines...) Expand all Loading... |
163 if (rv != LIBUSB_SUCCESS) { | 147 if (rv != LIBUSB_SUCCESS) { |
164 VLOG(1) << "Failed to claim interface: " << ConvertErrorToString(rv); | 148 VLOG(1) << "Failed to claim interface: " << ConvertErrorToString(rv); |
165 } | 149 } |
166 return rv == LIBUSB_SUCCESS; | 150 return rv == LIBUSB_SUCCESS; |
167 } | 151 } |
168 | 152 |
169 struct UsbDeviceHandleImpl::Transfer { | 153 struct UsbDeviceHandleImpl::Transfer { |
170 Transfer(); | 154 Transfer(); |
171 ~Transfer(); | 155 ~Transfer(); |
172 | 156 |
| 157 void Complete(UsbTransferStatus status, size_t bytes_transferred); |
| 158 |
173 UsbTransferType transfer_type; | 159 UsbTransferType transfer_type; |
174 scoped_refptr<net::IOBuffer> buffer; | 160 scoped_refptr<net::IOBuffer> buffer; |
175 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> claimed_interface; | 161 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> claimed_interface; |
176 scoped_refptr<base::MessageLoopProxy> message_loop_proxy; | 162 scoped_refptr<base::SingleThreadTaskRunner> task_runner; |
177 size_t length; | 163 size_t length; |
178 UsbTransferCallback callback; | 164 UsbTransferCallback callback; |
179 }; | 165 }; |
180 | 166 |
181 UsbDeviceHandleImpl::Transfer::Transfer() | 167 UsbDeviceHandleImpl::Transfer::Transfer() |
182 : transfer_type(USB_TRANSFER_CONTROL), length(0) { | 168 : transfer_type(USB_TRANSFER_CONTROL), length(0) { |
183 } | 169 } |
184 | 170 |
185 UsbDeviceHandleImpl::Transfer::~Transfer() { | 171 UsbDeviceHandleImpl::Transfer::~Transfer() { |
186 } | 172 } |
187 | 173 |
| 174 void UsbDeviceHandleImpl::Transfer::Complete(UsbTransferStatus status, |
| 175 size_t bytes_transferred) { |
| 176 if (task_runner->RunsTasksOnCurrentThread()) { |
| 177 callback.Run(status, buffer, bytes_transferred); |
| 178 } else { |
| 179 task_runner->PostTask( |
| 180 FROM_HERE, base::Bind(callback, status, buffer, bytes_transferred)); |
| 181 } |
| 182 } |
| 183 |
188 UsbDeviceHandleImpl::UsbDeviceHandleImpl( | 184 UsbDeviceHandleImpl::UsbDeviceHandleImpl( |
189 scoped_refptr<UsbContext> context, | 185 scoped_refptr<UsbContext> context, |
190 UsbDeviceImpl* device, | 186 UsbDeviceImpl* device, |
191 PlatformUsbDeviceHandle handle, | 187 PlatformUsbDeviceHandle handle, |
192 scoped_refptr<UsbConfigDescriptor> interfaces) | 188 scoped_refptr<UsbConfigDescriptor> interfaces) |
193 : device_(device), | 189 : device_(device), |
194 handle_(handle), | 190 handle_(handle), |
195 interfaces_(interfaces), | 191 interfaces_(interfaces), |
196 context_(context) { | 192 context_(context), |
197 DCHECK(thread_checker_.CalledOnValidThread()); | 193 task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
198 DCHECK(handle) << "Cannot create device with NULL handle."; | 194 DCHECK(handle) << "Cannot create device with NULL handle."; |
199 DCHECK(interfaces_.get()) << "Unable to list interfaces"; | 195 DCHECK(interfaces_.get()) << "Unable to list interfaces"; |
200 } | 196 } |
201 | 197 |
202 UsbDeviceHandleImpl::~UsbDeviceHandleImpl() { | 198 UsbDeviceHandleImpl::~UsbDeviceHandleImpl() { |
203 DCHECK(thread_checker_.CalledOnValidThread()); | 199 DCHECK(thread_checker_.CalledOnValidThread()); |
204 | 200 |
205 libusb_close(handle_); | 201 libusb_close(handle_); |
206 handle_ = NULL; | 202 handle_ = NULL; |
207 } | 203 } |
208 | 204 |
209 scoped_refptr<UsbDevice> UsbDeviceHandleImpl::GetDevice() const { | 205 scoped_refptr<UsbDevice> UsbDeviceHandleImpl::GetDevice() const { |
210 return static_cast<UsbDevice*>(device_); | 206 return static_cast<UsbDevice*>(device_); |
211 } | 207 } |
212 | 208 |
213 void UsbDeviceHandleImpl::Close() { | 209 void UsbDeviceHandleImpl::Close() { |
214 DCHECK(thread_checker_.CalledOnValidThread()); | 210 DCHECK(thread_checker_.CalledOnValidThread()); |
215 if (device_) | 211 if (device_) |
216 device_->Close(this); | 212 device_->Close(this); |
217 } | 213 } |
218 | 214 |
219 void UsbDeviceHandleImpl::TransferComplete(PlatformUsbTransferHandle handle) { | 215 /* static */ |
| 216 void LIBUSB_CALL UsbDeviceHandleImpl::PlatformTransferCallback( |
| 217 PlatformUsbTransferHandle transfer) { |
| 218 UsbDeviceHandleImpl* device_handle = |
| 219 reinterpret_cast<UsbDeviceHandleImpl*>(transfer->user_data); |
| 220 device_handle->task_runner_->PostTask( |
| 221 FROM_HERE, |
| 222 base::Bind( |
| 223 &UsbDeviceHandleImpl::CompleteTransfer, device_handle, transfer)); |
| 224 } |
| 225 |
| 226 void UsbDeviceHandleImpl::CompleteTransfer(PlatformUsbTransferHandle handle) { |
220 DCHECK(ContainsKey(transfers_, handle)) << "Missing transfer completed"; | 227 DCHECK(ContainsKey(transfers_, handle)) << "Missing transfer completed"; |
221 | 228 |
222 Transfer transfer = transfers_[handle]; | 229 Transfer transfer = transfers_[handle]; |
223 transfers_.erase(handle); | 230 transfers_.erase(handle); |
224 | 231 |
225 DCHECK_GE(handle->actual_length, 0) << "Negative actual length received"; | 232 DCHECK_GE(handle->actual_length, 0) << "Negative actual length received"; |
226 size_t actual_length = | 233 size_t actual_length = |
227 static_cast<size_t>(std::max(handle->actual_length, 0)); | 234 static_cast<size_t>(std::max(handle->actual_length, 0)); |
228 | 235 |
229 DCHECK(transfer.length >= actual_length) | 236 DCHECK(transfer.length >= actual_length) |
230 << "data too big for our buffer (libusb failure?)"; | 237 << "data too big for our buffer (libusb failure?)"; |
231 | 238 |
232 scoped_refptr<net::IOBuffer> buffer = transfer.buffer; | |
233 switch (transfer.transfer_type) { | 239 switch (transfer.transfer_type) { |
234 case USB_TRANSFER_CONTROL: | 240 case USB_TRANSFER_CONTROL: |
235 // If the transfer is a control transfer we do not expose the control | 241 // If the transfer is a control transfer we do not expose the control |
236 // setup header to the caller. This logic strips off the header if | 242 // setup header to the caller. This logic strips off the header if |
237 // present before invoking the callback provided with the transfer. | 243 // present before invoking the callback provided with the transfer. |
238 if (actual_length > 0) { | 244 if (actual_length > 0) { |
239 CHECK(transfer.length >= LIBUSB_CONTROL_SETUP_SIZE) | 245 CHECK(transfer.length >= LIBUSB_CONTROL_SETUP_SIZE) |
240 << "buffer was not correctly set: too small for the control header"; | 246 << "buffer was not correctly set: too small for the control header"; |
241 | 247 |
242 if (transfer.length >= (LIBUSB_CONTROL_SETUP_SIZE + actual_length)) { | 248 if (transfer.length >= (LIBUSB_CONTROL_SETUP_SIZE + actual_length)) { |
243 // If the payload is zero bytes long, pad out the allocated buffer | 249 // If the payload is zero bytes long, pad out the allocated buffer |
244 // size to one byte so that an IOBuffer of that size can be allocated. | 250 // size to one byte so that an IOBuffer of that size can be allocated. |
245 scoped_refptr<net::IOBuffer> resized_buffer = | 251 scoped_refptr<net::IOBuffer> resized_buffer = |
246 new net::IOBuffer(static_cast<int>( | 252 new net::IOBuffer(static_cast<int>( |
247 std::max(actual_length, static_cast<size_t>(1)))); | 253 std::max(actual_length, static_cast<size_t>(1)))); |
248 memcpy(resized_buffer->data(), | 254 memcpy(resized_buffer->data(), |
249 buffer->data() + LIBUSB_CONTROL_SETUP_SIZE, | 255 transfer.buffer->data() + LIBUSB_CONTROL_SETUP_SIZE, |
250 actual_length); | 256 actual_length); |
251 buffer = resized_buffer; | 257 transfer.buffer = resized_buffer; |
252 } | 258 } |
253 } | 259 } |
254 break; | 260 break; |
255 | 261 |
256 case USB_TRANSFER_ISOCHRONOUS: | 262 case USB_TRANSFER_ISOCHRONOUS: |
257 // Isochronous replies might carry data in the different isoc packets even | 263 // Isochronous replies might carry data in the different isoc packets even |
258 // if the transfer actual_data value is zero. Furthermore, not all of the | 264 // if the transfer actual_data value is zero. Furthermore, not all of the |
259 // received packets might contain data, so we need to calculate how many | 265 // received packets might contain data, so we need to calculate how many |
260 // data bytes we are effectively providing and pack the results. | 266 // data bytes we are effectively providing and pack the results. |
261 if (actual_length == 0) { | 267 if (actual_length == 0) { |
262 size_t packet_buffer_start = 0; | 268 size_t packet_buffer_start = 0; |
263 for (int i = 0; i < handle->num_iso_packets; ++i) { | 269 for (int i = 0; i < handle->num_iso_packets; ++i) { |
264 PlatformUsbIsoPacketDescriptor packet = &handle->iso_packet_desc[i]; | 270 PlatformUsbIsoPacketDescriptor packet = &handle->iso_packet_desc[i]; |
265 if (packet->actual_length > 0) { | 271 if (packet->actual_length > 0) { |
266 // We don't need to copy as long as all packets until now provide | 272 // We don't need to copy as long as all packets until now provide |
267 // all the data the packet can hold. | 273 // all the data the packet can hold. |
268 if (actual_length < packet_buffer_start) { | 274 if (actual_length < packet_buffer_start) { |
269 CHECK(packet_buffer_start + packet->actual_length <= | 275 CHECK(packet_buffer_start + packet->actual_length <= |
270 transfer.length); | 276 transfer.length); |
271 memmove(buffer->data() + actual_length, | 277 memmove(transfer.buffer->data() + actual_length, |
272 buffer->data() + packet_buffer_start, | 278 transfer.buffer->data() + packet_buffer_start, |
273 packet->actual_length); | 279 packet->actual_length); |
274 } | 280 } |
275 actual_length += packet->actual_length; | 281 actual_length += packet->actual_length; |
276 } | 282 } |
277 | 283 |
278 packet_buffer_start += packet->length; | 284 packet_buffer_start += packet->length; |
279 } | 285 } |
280 } | 286 } |
281 break; | 287 break; |
282 | 288 |
283 case USB_TRANSFER_BULK: | 289 case USB_TRANSFER_BULK: |
284 case USB_TRANSFER_INTERRUPT: | 290 case USB_TRANSFER_INTERRUPT: |
285 break; | 291 break; |
286 | 292 |
287 default: | 293 default: |
288 NOTREACHED() << "Invalid usb transfer type"; | 294 NOTREACHED() << "Invalid usb transfer type"; |
289 break; | 295 break; |
290 } | 296 } |
291 | 297 |
292 transfer.message_loop_proxy->PostTask( | 298 transfer.Complete(ConvertTransferStatus(handle->status), actual_length); |
293 FROM_HERE, | 299 libusb_free_transfer(handle); |
294 base::Bind(transfer.callback, | |
295 ConvertTransferStatus(handle->status), | |
296 buffer, | |
297 actual_length)); | |
298 | 300 |
299 // Must release interface first before actually delete this. | 301 // Must release interface first before actually delete this. |
300 transfer.claimed_interface = NULL; | 302 transfer.claimed_interface = NULL; |
301 } | 303 } |
302 | 304 |
303 bool UsbDeviceHandleImpl::ClaimInterface(const int interface_number) { | 305 bool UsbDeviceHandleImpl::ClaimInterface(const int interface_number) { |
304 DCHECK(thread_checker_.CalledOnValidThread()); | 306 DCHECK(thread_checker_.CalledOnValidThread()); |
305 if (!device_) | 307 if (!device_) |
306 return false; | 308 return false; |
307 if (ContainsKey(claimed_interfaces_, interface_number)) | 309 if (ContainsKey(claimed_interfaces_, interface_number)) |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 CreateRequestType(direction, request_type, recipient); | 547 CreateRequestType(direction, request_type, recipient); |
546 libusb_fill_control_setup(reinterpret_cast<uint8*>(resized_buffer->data()), | 548 libusb_fill_control_setup(reinterpret_cast<uint8*>(resized_buffer->data()), |
547 converted_type, | 549 converted_type, |
548 request, | 550 request, |
549 value, | 551 value, |
550 index, | 552 index, |
551 static_cast<int16>(length)); | 553 static_cast<int16>(length)); |
552 libusb_fill_control_transfer(transfer, | 554 libusb_fill_control_transfer(transfer, |
553 handle_, | 555 handle_, |
554 reinterpret_cast<uint8*>(resized_buffer->data()), | 556 reinterpret_cast<uint8*>(resized_buffer->data()), |
555 PlatformTransferCompletionCallback, | 557 &UsbDeviceHandleImpl::PlatformTransferCallback, |
556 this, | 558 this, |
557 timeout); | 559 timeout); |
558 | 560 |
559 BrowserThread::PostTask(BrowserThread::FILE, | 561 PostOrSubmitTransfer( |
560 FROM_HERE, | 562 transfer, USB_TRANSFER_CONTROL, resized_buffer, resized_length, callback); |
561 base::Bind(&UsbDeviceHandleImpl::SubmitTransfer, | |
562 this, | |
563 transfer, | |
564 USB_TRANSFER_CONTROL, | |
565 resized_buffer, | |
566 resized_length, | |
567 base::MessageLoopProxy::current(), | |
568 callback)); | |
569 } | 563 } |
570 | 564 |
571 void UsbDeviceHandleImpl::BulkTransfer(const UsbEndpointDirection direction, | 565 void UsbDeviceHandleImpl::BulkTransfer(const UsbEndpointDirection direction, |
572 const uint8 endpoint, | 566 const uint8 endpoint, |
573 net::IOBuffer* buffer, | 567 net::IOBuffer* buffer, |
574 const size_t length, | 568 const size_t length, |
575 const unsigned int timeout, | 569 const unsigned int timeout, |
576 const UsbTransferCallback& callback) { | 570 const UsbTransferCallback& callback) { |
577 if (!device_) { | 571 if (!device_) { |
578 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | 572 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); |
579 return; | 573 return; |
580 } | 574 } |
581 | 575 |
582 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); | 576 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); |
583 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; | 577 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; |
584 libusb_fill_bulk_transfer(transfer, | 578 libusb_fill_bulk_transfer(transfer, |
585 handle_, | 579 handle_, |
586 new_endpoint, | 580 new_endpoint, |
587 reinterpret_cast<uint8*>(buffer->data()), | 581 reinterpret_cast<uint8*>(buffer->data()), |
588 static_cast<int>(length), | 582 static_cast<int>(length), |
589 PlatformTransferCompletionCallback, | 583 &UsbDeviceHandleImpl::PlatformTransferCallback, |
590 this, | 584 this, |
591 timeout); | 585 timeout); |
592 | 586 |
593 BrowserThread::PostTask(BrowserThread::FILE, | 587 PostOrSubmitTransfer(transfer, USB_TRANSFER_BULK, buffer, length, callback); |
594 FROM_HERE, | |
595 base::Bind(&UsbDeviceHandleImpl::SubmitTransfer, | |
596 this, | |
597 transfer, | |
598 USB_TRANSFER_BULK, | |
599 make_scoped_refptr(buffer), | |
600 length, | |
601 base::MessageLoopProxy::current(), | |
602 callback)); | |
603 } | 588 } |
604 | 589 |
605 void UsbDeviceHandleImpl::InterruptTransfer( | 590 void UsbDeviceHandleImpl::InterruptTransfer( |
606 const UsbEndpointDirection direction, | 591 const UsbEndpointDirection direction, |
607 const uint8 endpoint, | 592 const uint8 endpoint, |
608 net::IOBuffer* buffer, | 593 net::IOBuffer* buffer, |
609 const size_t length, | 594 const size_t length, |
610 const unsigned int timeout, | 595 const unsigned int timeout, |
611 const UsbTransferCallback& callback) { | 596 const UsbTransferCallback& callback) { |
612 if (!device_) { | 597 if (!device_) { |
613 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | 598 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); |
614 return; | 599 return; |
615 } | 600 } |
616 | 601 |
617 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); | 602 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); |
618 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; | 603 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; |
619 libusb_fill_interrupt_transfer(transfer, | 604 libusb_fill_interrupt_transfer(transfer, |
620 handle_, | 605 handle_, |
621 new_endpoint, | 606 new_endpoint, |
622 reinterpret_cast<uint8*>(buffer->data()), | 607 reinterpret_cast<uint8*>(buffer->data()), |
623 static_cast<int>(length), | 608 static_cast<int>(length), |
624 PlatformTransferCompletionCallback, | 609 &UsbDeviceHandleImpl::PlatformTransferCallback, |
625 this, | 610 this, |
626 timeout); | 611 timeout); |
627 BrowserThread::PostTask(BrowserThread::FILE, | 612 |
628 FROM_HERE, | 613 PostOrSubmitTransfer( |
629 base::Bind(&UsbDeviceHandleImpl::SubmitTransfer, | 614 transfer, USB_TRANSFER_INTERRUPT, buffer, length, callback); |
630 this, | |
631 transfer, | |
632 USB_TRANSFER_INTERRUPT, | |
633 make_scoped_refptr(buffer), | |
634 length, | |
635 base::MessageLoopProxy::current(), | |
636 callback)); | |
637 } | 615 } |
638 | 616 |
639 void UsbDeviceHandleImpl::IsochronousTransfer( | 617 void UsbDeviceHandleImpl::IsochronousTransfer( |
640 const UsbEndpointDirection direction, | 618 const UsbEndpointDirection direction, |
641 const uint8 endpoint, | 619 const uint8 endpoint, |
642 net::IOBuffer* buffer, | 620 net::IOBuffer* buffer, |
643 const size_t length, | 621 const size_t length, |
644 const unsigned int packets, | 622 const unsigned int packets, |
645 const unsigned int packet_length, | 623 const unsigned int packet_length, |
646 const unsigned int timeout, | 624 const unsigned int timeout, |
647 const UsbTransferCallback& callback) { | 625 const UsbTransferCallback& callback) { |
648 if (!device_) { | 626 if (!device_) { |
649 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | 627 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); |
650 return; | 628 return; |
651 } | 629 } |
652 | 630 |
653 const uint64 total_length = packets * packet_length; | 631 const uint64 total_length = packets * packet_length; |
654 CHECK(packets <= length && total_length <= length) | 632 CHECK(packets <= length && total_length <= length) |
655 << "transfer length is too small"; | 633 << "transfer length is too small"; |
656 | 634 |
657 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(packets); | 635 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(packets); |
658 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; | 636 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; |
659 libusb_fill_iso_transfer(transfer, | 637 libusb_fill_iso_transfer(transfer, |
660 handle_, | 638 handle_, |
661 new_endpoint, | 639 new_endpoint, |
662 reinterpret_cast<uint8*>(buffer->data()), | 640 reinterpret_cast<uint8*>(buffer->data()), |
663 static_cast<int>(length), | 641 static_cast<int>(length), |
664 packets, | 642 packets, |
665 PlatformTransferCompletionCallback, | 643 &UsbDeviceHandleImpl::PlatformTransferCallback, |
666 this, | 644 this, |
667 timeout); | 645 timeout); |
668 libusb_set_iso_packet_lengths(transfer, packet_length); | 646 libusb_set_iso_packet_lengths(transfer, packet_length); |
669 | 647 |
670 BrowserThread::PostTask(BrowserThread::FILE, | 648 PostOrSubmitTransfer( |
671 FROM_HERE, | 649 transfer, USB_TRANSFER_ISOCHRONOUS, buffer, length, callback); |
672 base::Bind(&UsbDeviceHandleImpl::SubmitTransfer, | |
673 this, | |
674 transfer, | |
675 USB_TRANSFER_ISOCHRONOUS, | |
676 make_scoped_refptr(buffer), | |
677 length, | |
678 base::MessageLoopProxy::current(), | |
679 callback)); | |
680 } | 650 } |
681 | 651 |
682 void UsbDeviceHandleImpl::RefreshEndpointMap() { | 652 void UsbDeviceHandleImpl::RefreshEndpointMap() { |
683 DCHECK(thread_checker_.CalledOnValidThread()); | 653 DCHECK(thread_checker_.CalledOnValidThread()); |
684 endpoint_map_.clear(); | 654 endpoint_map_.clear(); |
685 for (ClaimedInterfaceMap::iterator it = claimed_interfaces_.begin(); | 655 for (ClaimedInterfaceMap::iterator it = claimed_interfaces_.begin(); |
686 it != claimed_interfaces_.end(); | 656 it != claimed_interfaces_.end(); |
687 ++it) { | 657 ++it) { |
688 scoped_refptr<const UsbInterfaceAltSettingDescriptor> interface_desc = | 658 scoped_refptr<const UsbInterfaceAltSettingDescriptor> interface_desc = |
689 interfaces_->GetInterface(it->first) | 659 interfaces_->GetInterface(it->first) |
690 ->GetAltSetting(it->second->alternate_setting()); | 660 ->GetAltSetting(it->second->alternate_setting()); |
691 for (size_t i = 0; i < interface_desc->GetNumEndpoints(); i++) { | 661 for (size_t i = 0; i < interface_desc->GetNumEndpoints(); i++) { |
692 scoped_refptr<const UsbEndpointDescriptor> endpoint = | 662 scoped_refptr<const UsbEndpointDescriptor> endpoint = |
693 interface_desc->GetEndpoint(i); | 663 interface_desc->GetEndpoint(i); |
694 endpoint_map_[endpoint->GetAddress()] = it->first; | 664 endpoint_map_[endpoint->GetAddress()] = it->first; |
695 } | 665 } |
696 } | 666 } |
697 } | 667 } |
698 | 668 |
699 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> | 669 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> |
700 UsbDeviceHandleImpl::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { | 670 UsbDeviceHandleImpl::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { |
701 unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK; | 671 unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK; |
702 if (ContainsKey(endpoint_map_, address)) | 672 if (ContainsKey(endpoint_map_, address)) |
703 return claimed_interfaces_[endpoint_map_[address]]; | 673 return claimed_interfaces_[endpoint_map_[address]]; |
704 return NULL; | 674 return NULL; |
705 } | 675 } |
706 | 676 |
| 677 void UsbDeviceHandleImpl::PostOrSubmitTransfer( |
| 678 PlatformUsbTransferHandle transfer, |
| 679 UsbTransferType transfer_type, |
| 680 net::IOBuffer* buffer, |
| 681 size_t length, |
| 682 const UsbTransferCallback& callback) { |
| 683 if (task_runner_->RunsTasksOnCurrentThread()) { |
| 684 SubmitTransfer(transfer, |
| 685 transfer_type, |
| 686 buffer, |
| 687 length, |
| 688 base::ThreadTaskRunnerHandle::Get(), |
| 689 callback); |
| 690 } else { |
| 691 task_runner_->PostTask(FROM_HERE, |
| 692 base::Bind(&UsbDeviceHandleImpl::SubmitTransfer, |
| 693 this, |
| 694 transfer, |
| 695 transfer_type, |
| 696 make_scoped_refptr(buffer), |
| 697 length, |
| 698 base::ThreadTaskRunnerHandle::Get(), |
| 699 callback)); |
| 700 } |
| 701 } |
| 702 |
707 void UsbDeviceHandleImpl::SubmitTransfer( | 703 void UsbDeviceHandleImpl::SubmitTransfer( |
708 PlatformUsbTransferHandle handle, | 704 PlatformUsbTransferHandle handle, |
709 UsbTransferType transfer_type, | 705 UsbTransferType transfer_type, |
710 net::IOBuffer* buffer, | 706 net::IOBuffer* buffer, |
711 const size_t length, | 707 const size_t length, |
712 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 708 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
713 const UsbTransferCallback& callback) { | 709 const UsbTransferCallback& callback) { |
714 DCHECK(thread_checker_.CalledOnValidThread()); | 710 DCHECK(thread_checker_.CalledOnValidThread()); |
715 if (!device_) { | |
716 message_loop_proxy->PostTask( | |
717 FROM_HERE, | |
718 base::Bind( | |
719 callback, USB_TRANSFER_DISCONNECT, make_scoped_refptr(buffer), 0)); | |
720 } | |
721 | 711 |
722 Transfer transfer; | 712 Transfer transfer; |
723 transfer.transfer_type = transfer_type; | 713 transfer.transfer_type = transfer_type; |
724 transfer.buffer = buffer; | 714 transfer.buffer = buffer; |
725 transfer.length = length; | 715 transfer.length = length; |
726 transfer.callback = callback; | 716 transfer.callback = callback; |
727 transfer.message_loop_proxy = message_loop_proxy; | 717 transfer.task_runner = task_runner; |
| 718 |
| 719 if (!device_) { |
| 720 transfer.Complete(USB_TRANSFER_DISCONNECT, 0); |
| 721 return; |
| 722 } |
728 | 723 |
729 // It's OK for this method to return NULL. libusb_submit_transfer will fail if | 724 // It's OK for this method to return NULL. libusb_submit_transfer will fail if |
730 // it requires an interface we didn't claim. | 725 // it requires an interface we didn't claim. |
731 transfer.claimed_interface = GetClaimedInterfaceForEndpoint(handle->endpoint); | 726 transfer.claimed_interface = GetClaimedInterfaceForEndpoint(handle->endpoint); |
732 | 727 |
733 const int rv = libusb_submit_transfer(handle); | 728 const int rv = libusb_submit_transfer(handle); |
734 if (rv == LIBUSB_SUCCESS) { | 729 if (rv == LIBUSB_SUCCESS) { |
735 transfers_[handle] = transfer; | 730 transfers_[handle] = transfer; |
736 } else { | 731 } else { |
737 VLOG(1) << "Failed to submit transfer: " << ConvertErrorToString(rv); | 732 VLOG(1) << "Failed to submit transfer: " << ConvertErrorToString(rv); |
738 message_loop_proxy->PostTask( | 733 transfer.Complete(USB_TRANSFER_ERROR, 0); |
739 FROM_HERE, | |
740 base::Bind( | |
741 callback, USB_TRANSFER_ERROR, make_scoped_refptr(buffer), 0)); | |
742 } | 734 } |
743 } | 735 } |
744 | 736 |
745 void UsbDeviceHandleImpl::InternalClose() { | 737 void UsbDeviceHandleImpl::InternalClose() { |
746 DCHECK(thread_checker_.CalledOnValidThread()); | 738 DCHECK(thread_checker_.CalledOnValidThread()); |
747 if (!device_) | 739 if (!device_) |
748 return; | 740 return; |
749 | 741 |
750 // Cancel all the transfers. | 742 // Cancel all the transfers. |
751 for (TransferMap::iterator it = transfers_.begin(); it != transfers_.end(); | 743 for (TransferMap::iterator it = transfers_.begin(); it != transfers_.end(); |
752 ++it) { | 744 ++it) { |
753 // The callback will be called some time later. | 745 // The callback will be called some time later. |
754 libusb_cancel_transfer(it->first); | 746 libusb_cancel_transfer(it->first); |
755 } | 747 } |
756 | 748 |
757 // Attempt-release all the interfaces. | 749 // Attempt-release all the interfaces. |
758 // It will be retained until the transfer cancellation is finished. | 750 // It will be retained until the transfer cancellation is finished. |
759 claimed_interfaces_.clear(); | 751 claimed_interfaces_.clear(); |
760 | 752 |
761 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to | 753 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to |
762 // finish. | 754 // finish. |
763 device_ = NULL; | 755 device_ = NULL; |
764 } | 756 } |
765 | 757 |
766 } // namespace usb_service | 758 } // namespace usb_service |
OLD | NEW |