| Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 1 | #pragma once | 
|  | 2 |  | 
|  | 3 | /* | 
|  | 4 | * Copyright (C) 2017 The Android Open Source Project | 
|  | 5 | * | 
|  | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 7 | * you may not use this file except in compliance with the License. | 
|  | 8 | * You may obtain a copy of the License at | 
|  | 9 | * | 
|  | 10 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 11 | * | 
|  | 12 | * Unless required by applicable law or agreed to in writing, software | 
|  | 13 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 15 | * See the License for the specific language governing permissions and | 
|  | 16 | * limitations under the License. | 
|  | 17 | */ | 
|  | 18 |  | 
| Josh Gao | 5f2c5bb | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 19 | #include <linux/usb/functionfs.h> | 
|  | 20 |  | 
| Kelvin Zhang | 682e5b5 | 2022-07-12 17:37:54 -0700 | [diff] [blame] | 21 | #include <liburing.h> | 
| Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 22 | #include <atomic> | 
|  | 23 | #include <condition_variable> | 
| Kelvin Zhang | 682e5b5 | 2022-07-12 17:37:54 -0700 | [diff] [blame] | 24 | #include <memory> | 
| Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 25 | #include <mutex> | 
| Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 26 | #include <vector> | 
| Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 27 |  | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 28 | #include <android-base/unique_fd.h> | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 29 | #include <asyncio/AsyncIO.h> | 
|  | 30 |  | 
|  | 31 | struct aio_block { | 
|  | 32 | std::vector<struct iocb> iocb; | 
|  | 33 | std::vector<struct iocb*> iocbs; | 
|  | 34 | std::vector<struct io_event> events; | 
|  | 35 | aio_context_t ctx; | 
|  | 36 | int num_submitted; | 
|  | 37 | int fd; | 
|  | 38 | }; | 
|  | 39 |  | 
| Kelvin Zhang | 682e5b5 | 2022-07-12 17:37:54 -0700 | [diff] [blame] | 40 | int getMaxPacketSize(int ffs_fd); | 
| Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 41 |  | 
| Kelvin Zhang | 682e5b5 | 2022-07-12 17:37:54 -0700 | [diff] [blame] | 42 | enum class AIOType { SYNC_IO, AIO, IO_URING }; | 
|  | 43 |  | 
|  | 44 | struct usb_handle { | 
| Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 45 | std::condition_variable notify; | 
|  | 46 | std::mutex lock; | 
| Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 47 | bool open_new_connection = true; | 
|  | 48 |  | 
|  | 49 | int (*write)(usb_handle* h, const void* data, int len); | 
| chihhao.chen | 8c544b6 | 2019-05-20 16:55:55 +0800 | [diff] [blame] | 50 | int (*read)(usb_handle* h, void* data, int len, bool allow_partial); | 
| Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 51 | void (*close)(usb_handle* h); | 
|  | 52 |  | 
|  | 53 | // FunctionFS | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 54 | android::base::unique_fd control; | 
|  | 55 | android::base::unique_fd bulk_out;  // "out" from the host's perspective => source for adbd | 
|  | 56 | android::base::unique_fd bulk_in;   // "in" from the host's perspective => sink for adbd | 
| Jerry Zhang | 55205a5 | 2017-01-04 12:34:38 -0800 | [diff] [blame] | 57 |  | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 58 | // Access to these blocks is very not thread safe. Have one block for each of the | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 59 | // read and write threads. | 
|  | 60 | struct aio_block read_aiob; | 
|  | 61 | struct aio_block write_aiob; | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 62 |  | 
| Kelvin Zhang | 682e5b5 | 2022-07-12 17:37:54 -0700 | [diff] [blame] | 63 | io_uring ring; | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 64 | size_t io_size; | 
| Kelvin Zhang | 682e5b5 | 2022-07-12 17:37:54 -0700 | [diff] [blame] | 65 | AIOType aio_type; | 
| Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 66 | }; | 
|  | 67 |  | 
| Kelvin Zhang | 682e5b5 | 2022-07-12 17:37:54 -0700 | [diff] [blame] | 68 | std::unique_ptr<usb_handle> create_usb_handle(unsigned num_bufs, unsigned io_size); |