OLD | NEW |
(Empty) | |
| 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 #ifndef CHROME_BROWSER_USB_USB_DEVICE_H_ |
| 6 #define CHROME_BROWSER_USB_USB_DEVICE_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <map> |
| 10 |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/synchronization/lock.h" |
| 13 #include "net/base/completion_callback.h" |
| 14 #include "net/base/io_buffer.h" |
| 15 #include "third_party/libusb/libusb/libusb.h" |
| 16 |
| 17 typedef libusb_device* PlatformUsbDevice; |
| 18 typedef libusb_device_handle* PlatformUsbDeviceHandle; |
| 19 typedef libusb_transfer* PlatformUsbTransferHandle; |
| 20 |
| 21 class UsbService; |
| 22 |
| 23 namespace net { |
| 24 class IOBuffer; |
| 25 } // namespace net |
| 26 |
| 27 // A UsbDevice wraps the platform's underlying representation of what a USB |
| 28 // device actually is, and provides accessors for performing many of the |
| 29 // standard USB operations. |
| 30 class UsbDevice : public base::RefCounted<UsbDevice> { |
| 31 public: |
| 32 enum TransferDirection { IN, OUT }; |
| 33 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; |
| 34 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; |
| 35 |
| 36 // Usually you will not want to directly create a UsbDevice, favoring to let |
| 37 // the UsbService take care of the logistics of getting a platform device |
| 38 // handle and handling events for it. |
| 39 UsbDevice(UsbService* service, PlatformUsbDeviceHandle handle); |
| 40 |
| 41 PlatformUsbDeviceHandle handle() { return handle_; } |
| 42 |
| 43 // Close the USB device and release the underlying platform device. |
| 44 void Close(); |
| 45 |
| 46 void ControlTransfer(const TransferDirection direction, |
| 47 const TransferRequestType request_type, |
| 48 const TransferRecipient recipient, |
| 49 const uint8 request, |
| 50 const uint16 value, |
| 51 const uint16 index, |
| 52 net::IOBuffer* buffer, |
| 53 const size_t length, |
| 54 const unsigned int timeout, |
| 55 const net::CompletionCallback& callback); |
| 56 |
| 57 void BulkTransfer(const TransferDirection direction, |
| 58 const uint8 endpoint, |
| 59 net::IOBuffer* buffer, |
| 60 const size_t length, |
| 61 const unsigned int timeout, |
| 62 const net::CompletionCallback& callback); |
| 63 |
| 64 void InterruptTransfer(const TransferDirection direction, |
| 65 const uint8 endpoint, |
| 66 net::IOBuffer* buffer, |
| 67 const size_t length, |
| 68 const unsigned int timeout, |
| 69 const net::CompletionCallback& callback); |
| 70 |
| 71 // Normal code should not call this function. It is called by the platform's |
| 72 // callback mechanism in such a way that it cannot be made private. Invokes |
| 73 // the callbacks associated with a given transfer, and removes it from the |
| 74 // in-flight transfer set. |
| 75 void TransferComplete(PlatformUsbTransferHandle transfer); |
| 76 |
| 77 private: |
| 78 struct Transfer { |
| 79 Transfer(); |
| 80 ~Transfer(); |
| 81 |
| 82 scoped_refptr<net::IOBuffer> buffer; |
| 83 net::CompletionCallback callback; |
| 84 }; |
| 85 |
| 86 friend class base::RefCounted<UsbDevice>; |
| 87 virtual ~UsbDevice(); |
| 88 |
| 89 // Checks that the device has not yet been closed. |
| 90 void CheckDevice(); |
| 91 |
| 92 // Starts tracking the USB transfer associated with a platform transfer |
| 93 // handle. Retains the buffer and copies the completion callback until the |
| 94 // transfer finishes, whereupon it invokes the callback then releases the |
| 95 // buffer. |
| 96 void AddTransfer(PlatformUsbTransferHandle handle, net::IOBuffer* buffer, |
| 97 const net::CompletionCallback& callback); |
| 98 |
| 99 // The UsbService isn't referenced here to prevent a dependency cycle between |
| 100 // the service and the devices. Since a service owns every device, and is |
| 101 // responsible for its destruction, there is no case where a UsbDevice can |
| 102 // have outlived its originating UsbService. |
| 103 UsbService* const service_; |
| 104 PlatformUsbDeviceHandle handle_; |
| 105 |
| 106 // transfers_ tracks all in-flight transfers associated with this device, |
| 107 // allowing the device to retain the buffer and callback associated with a |
| 108 // transfer until such time that it completes. It is protected by lock_. |
| 109 base::Lock lock_; |
| 110 std::map<PlatformUsbTransferHandle, Transfer> transfers_; |
| 111 |
| 112 DISALLOW_EVIL_CONSTRUCTORS(UsbDevice); |
| 113 }; |
| 114 |
| 115 #endif // CHROME_BROWSER_USB_USB_DEVICE_H_ |
OLD | NEW |