blob: 6c3f54252f63563bda7749168af986afca613890 [file] [log] [blame]
Josh Gao44c688c2017-01-11 17:37:35 -08001#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 Gao5f2c5bb2019-06-19 14:14:57 -070019#include <linux/usb/functionfs.h>
20
Josh Gao44c688c2017-01-11 17:37:35 -080021#include <atomic>
22#include <condition_variable>
23#include <mutex>
Jerry Zhangb156c602018-05-07 15:14:47 -070024#include <vector>
Josh Gao44c688c2017-01-11 17:37:35 -080025
Josh Gao860cc5a2018-08-21 15:44:49 -070026#include <android-base/unique_fd.h>
Jerry Zhangecee4342017-07-18 14:07:57 -070027#include <asyncio/AsyncIO.h>
28
29struct aio_block {
30 std::vector<struct iocb> iocb;
31 std::vector<struct iocb*> iocbs;
32 std::vector<struct io_event> events;
33 aio_context_t ctx;
34 int num_submitted;
35 int fd;
36};
37
Josh Gao44c688c2017-01-11 17:37:35 -080038struct usb_handle {
Josh Gao08718242020-03-27 18:09:56 -070039 usb_handle() {}
Josh Gao44c688c2017-01-11 17:37:35 -080040
41 std::condition_variable notify;
42 std::mutex lock;
Josh Gao44c688c2017-01-11 17:37:35 -080043 bool open_new_connection = true;
44
45 int (*write)(usb_handle* h, const void* data, int len);
chihhao.chen8c544b62019-05-20 16:55:55 +080046 int (*read)(usb_handle* h, void* data, int len, bool allow_partial);
Josh Gao44c688c2017-01-11 17:37:35 -080047 void (*close)(usb_handle* h);
48
49 // FunctionFS
Josh Gao860cc5a2018-08-21 15:44:49 -070050 android::base::unique_fd control;
51 android::base::unique_fd bulk_out; // "out" from the host's perspective => source for adbd
52 android::base::unique_fd bulk_in; // "in" from the host's perspective => sink for adbd
Jerry Zhang55205a52017-01-04 12:34:38 -080053
Josh Gao860cc5a2018-08-21 15:44:49 -070054 // Access to these blocks is very not thread safe. Have one block for each of the
Jerry Zhangecee4342017-07-18 14:07:57 -070055 // read and write threads.
56 struct aio_block read_aiob;
57 struct aio_block write_aiob;
Jerry Zhangcda7c3b2018-05-21 14:22:43 -070058
59 bool reads_zero_packets;
60 size_t io_size;
Josh Gao44c688c2017-01-11 17:37:35 -080061};
62
Josh Gao08718242020-03-27 18:09:56 -070063usb_handle* create_usb_handle(unsigned num_bufs, unsigned io_size);