Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 18 | #include <map> |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 19 | #include <memory> |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <libusb/libusb.h> |
| 22 | |
| 23 | #include "common/libs/fs/shared_fd.h" |
Greg Hartman | a4ff248 | 2017-10-03 16:35:00 -0700 | [diff] [blame^] | 24 | #include "common/libs/threads/cuttlefish_thread.h" |
Tomasz Wiszkowski | 93ea805 | 2017-09-21 10:02:45 -0700 | [diff] [blame] | 25 | #include "guest/commands/usbforward/transport_request.h" |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 26 | |
| 27 | namespace usb_forward { |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 28 | |
| 29 | // USBServer exposes access to USB devices over pipe (virtio channel etc). |
| 30 | // Usage: |
| 31 | // |
| 32 | // avd::SharedFD pipe = avd::SharedFD::Open(pipe_path, O_RDWR); |
| 33 | // USBServer server(pipe); |
| 34 | // CHECK(server.Init()); |
| 35 | // server.Serve(); |
| 36 | class USBServer final { |
| 37 | public: |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 38 | USBServer(const avd::SharedFD& fd); |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 39 | ~USBServer() = default; |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 40 | |
| 41 | // Serve incoming USB requests. |
| 42 | void Serve(); |
| 43 | |
| 44 | private: |
Tomasz Wiszkowski | 482292e | 2017-08-04 16:33:00 -0700 | [diff] [blame] | 45 | // HandleDeviceEvent opens and closes Android Gadget device, whenever it |
| 46 | // appears / disappears. |
| 47 | static int HandleDeviceEvent(libusb_context*, libusb_device*, |
| 48 | libusb_hotplug_event event, void* self_raw); |
| 49 | |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 50 | // Handle CmdDeviceList request. |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 51 | void HandleDeviceList(uint32_t tag); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 52 | |
| 53 | // Handle CmdAttach request. |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 54 | void HandleAttach(uint32_t tag); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 55 | |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 56 | // Handle CmdControlTransfer request. |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 57 | void HandleControlTransfer(uint32_t tag); |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 58 | |
| 59 | // Handle CmdDataTransfer request. |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 60 | void HandleDataTransfer(uint32_t tag); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 61 | |
Tomasz Wiszkowski | cdf5748 | 2017-08-14 12:47:25 -0700 | [diff] [blame] | 62 | // Handle CmdHeartbeat request. |
| 63 | void HandleHeartbeat(uint32_t tag); |
| 64 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 65 | // OnAsyncDataTransferComplete handles end of asynchronous data transfer cycle |
| 66 | // and sends response back to caller. |
| 67 | void OnTransferComplete(uint32_t tag, bool is_data_in, bool is_success, |
| 68 | const uint8_t* buffer, int32_t actual_length); |
| 69 | |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 70 | // Initialize, Configure and start libusb. |
| 71 | void InitLibUSB(); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 72 | |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 73 | // Stop, Deconfigure and Clean up libusb. |
| 74 | void ExitLibUSB(); |
| 75 | |
| 76 | // Extract device info, if device is available. |
| 77 | bool GetDeviceInfo(DeviceInfo* info, std::vector<InterfaceInfo>* ifaces); |
| 78 | |
| 79 | // Handle asynchronous libusb events. |
| 80 | static void* ProcessLibUSBRequests(void* self_ptr); |
| 81 | |
| 82 | std::shared_ptr<libusb_device_handle> handle_; |
| 83 | libusb_hotplug_callback_handle hotplug_handle_; |
| 84 | |
| 85 | std::unique_ptr<avd::ScopedThread> libusb_thread_; |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 86 | avd::Mutex write_mutex_; |
| 87 | avd::SharedFD fd_; |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 88 | avd::SharedFD device_event_fd_; |
| 89 | avd::SharedFD thread_event_fd_; |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 90 | |
| 91 | avd::Mutex requests_mutex_; |
| 92 | std::map<uint32_t, std::unique_ptr<TransportRequest>> requests_in_flight_; |
| 93 | |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 94 | USBServer(const USBServer& other) = delete; |
| 95 | USBServer& operator=(const USBServer& other) = delete; |
| 96 | }; |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 97 | |
Tomasz Wiszkowski | cdf5748 | 2017-08-14 12:47:25 -0700 | [diff] [blame] | 98 | } // namespace usb_forward |