1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#ifndef USB_HOST_LINUX_ADAPTER_H
17094332d3Sopenharmony_ci#define USB_HOST_LINUX_ADAPTER_H
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_ci#include "usb_raw_api_library.h"
20094332d3Sopenharmony_ci
21094332d3Sopenharmony_ci#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
22094332d3Sopenharmony_ci#include <sys/syscall.h>
23094332d3Sopenharmony_cistatic inline pid_t gettid()
24094332d3Sopenharmony_ci{
25094332d3Sopenharmony_ci    return syscall(SYS_gettid);
26094332d3Sopenharmony_ci}
27094332d3Sopenharmony_ci#endif
28094332d3Sopenharmony_ci
29094332d3Sopenharmony_ci#define USBDEV_PATH     "/dev"
30094332d3Sopenharmony_ci#define USB_DEV_FS_PATH "/dev/bus/usb"
31094332d3Sopenharmony_ci
32094332d3Sopenharmony_ci#define BUS_OFFSET      8
33094332d3Sopenharmony_ci
34094332d3Sopenharmony_ci#define MAX_BULK_DATA_BUFFER_LENGTH     4096
35094332d3Sopenharmony_ci#define MAX_BULK_URBS_PER_REQUEST       1
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_ci#define MAX_ISO_PACKETS_PER_URB         128
38094332d3Sopenharmony_ci#define MAX_ISO_DATA_BUFFER_LEN         (8 * 1024)
39094332d3Sopenharmony_ci#define MAX_ISO_URBS_PER_REQUEST        (((MAX_ISO_DATA_BUFFER_LEN - 1) / MAX_ISO_PACKETS_PER_URB) + 1)
40094332d3Sopenharmony_ci
41094332d3Sopenharmony_ci#define URBS_PER_REQUEST                MAX(MAX_BULK_URBS_PER_REQUEST, MAX_ISO_URBS_PER_REQUEST)
42094332d3Sopenharmony_ci
43094332d3Sopenharmony_ci#define MAX_ISO_SHARE_MEMERY_SIZE       sizeof(struct UsbHostRequest) + \
44094332d3Sopenharmony_ci    (URBS_PER_REQUEST * sizeof(struct UsbAdapterUrb)) + MAX_ISO_DATA_BUFFER_LEN
45094332d3Sopenharmony_ci#define MAX_BULK_SHARE_MEMERY_SIZE      sizeof(struct UsbHostRequest) + \
46094332d3Sopenharmony_ci    (URBS_PER_REQUEST * sizeof(struct UsbAdapterUrb)) + MAX_BULK_DATA_BUFFER_LENGTH
47094332d3Sopenharmony_ci#define MAX_CTRL_BUFFER_LENGTH  4096
48094332d3Sopenharmony_ci#define MAX_DRIVER_NAME_LENGTH  256
49094332d3Sopenharmony_ci#define DISCONNECT_CLAIM_EXCEPT_DRIVER  2
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_ci#define USB_ADAPTER_URB_TYPE_ISO                0
52094332d3Sopenharmony_ci#define USB_ADAPTER_URB_TYPE_INTERRUPT          1
53094332d3Sopenharmony_ci#define USB_ADAPTER_URB_TYPE_CONTROL            2
54094332d3Sopenharmony_ci#define USB_ADAPTER_URB_TYPE_BULK               3
55094332d3Sopenharmony_ci#define USB_ADAPTER_URB_SHORT_NOT_OK            0x01
56094332d3Sopenharmony_ci#define USB_ADAPTER_URB_ISO_ASAP                0x02
57094332d3Sopenharmony_ci#define USB_ADAPTER_URB_BULK_CONTINUATION       0x04
58094332d3Sopenharmony_ci#define USB_ADAPTER_URB_QUEUE_BULK              0x10
59094332d3Sopenharmony_ci#define USB_ADAPTER_URB_ZERO_PACKET             0x40
60094332d3Sopenharmony_ci#define USB_ADAPTER_CAP_ZERO_PACKET             0x01
61094332d3Sopenharmony_ci#define USB_ADAPTER_CAP_BULK_CONTINUATION       0x02
62094332d3Sopenharmony_ci#define USB_ADAPTER_CAP_NO_PACKET_SIZE_LIM      0x04
63094332d3Sopenharmony_ci#define USB_ADAPTER_CAP_BULK_SCATTER_GATHER     0x08
64094332d3Sopenharmony_ci#define USB_ADAPTER_CAP_REAP_AFTER_DISCONNECT   0x10
65094332d3Sopenharmony_ci
66094332d3Sopenharmony_ci#define USBDEVFS_CONTROL            _IOWR('U', 0, struct UsbControlRequestData)
67094332d3Sopenharmony_ci#define USBDEVFS_BULK               _IOWR('U', 2, struct UsbAdapterBulkTransfer)
68094332d3Sopenharmony_ci#define USBDEVFS_SETINTERFACE       _IOR('U', 4, struct UsbAdapterSetInterface)
69094332d3Sopenharmony_ci#define USBDEVFS_SETCONFIGURATION   _IOR('U', 5, unsigned int)
70094332d3Sopenharmony_ci#define USBDEVFS_GETDRIVER          _IOW('U', 8, struct UsbAdapterGetdriver)
71094332d3Sopenharmony_ci#define USBDEVFS_SUBMITURB          _IOR('U', 10, struct UsbAdapterUrb)
72094332d3Sopenharmony_ci#define USBDEVFS_DISCARDURB         _IO('U', 11)
73094332d3Sopenharmony_ci#define USBDEVFS_REAPURB            _IOW('U', 12, void *)
74094332d3Sopenharmony_ci#define USBDEVFS_CLAIMINTERFACE     _IOR('U', 15, unsigned int)
75094332d3Sopenharmony_ci#define USBDEVFS_RELEASEINTERFACE   _IOR('U', 16, unsigned int)
76094332d3Sopenharmony_ci#define USBDEVFS_IOCTL              _IOWR('U', 18, struct UsbAdapterIoctl)
77094332d3Sopenharmony_ci#define USBDEVFS_RESET              _IO('U', 20)
78094332d3Sopenharmony_ci#define USBDEVFS_CLEAR_HALT         _IOR('U', 21, unsigned int)
79094332d3Sopenharmony_ci#define USBDEVFS_DISCONNECT         _IO('U', 22)
80094332d3Sopenharmony_ci#define USBDEVFS_CONNECT            _IO('U', 23)
81094332d3Sopenharmony_ci#define USBDEVFS_GET_CAPABILITIES   _IOR('U', 26, unsigned int)
82094332d3Sopenharmony_ci#define USBDEVFS_DISCONNECT_CLAIM   _IOR('U', 27, struct UsbAdapterDisconnectClaim)
83094332d3Sopenharmony_ci#define USBDEVFS_ALLOC_STREAMS      _IOR('U', 28, struct UsbAdapterStreams)
84094332d3Sopenharmony_ci#define USBDEVFS_FREE_STREAMS       _IOR('U', 29, struct UsbAdapterStreams)
85094332d3Sopenharmony_ci#define USBDEVFS_GET_SPEED          _IO('U', 31)
86094332d3Sopenharmony_ci
87094332d3Sopenharmony_cistruct UsbAdapterBulkTransfer {
88094332d3Sopenharmony_ci    unsigned int ep;
89094332d3Sopenharmony_ci    unsigned int len;
90094332d3Sopenharmony_ci    unsigned int timeout; /* in milliseconds */
91094332d3Sopenharmony_ci    void *data;
92094332d3Sopenharmony_ci};
93094332d3Sopenharmony_ci
94094332d3Sopenharmony_cistruct UsbAdapterSetInterface {
95094332d3Sopenharmony_ci    unsigned int interface;
96094332d3Sopenharmony_ci    unsigned int altSetting;
97094332d3Sopenharmony_ci};
98094332d3Sopenharmony_ci
99094332d3Sopenharmony_cistruct UsbAdapterStreams {
100094332d3Sopenharmony_ci    unsigned int numStreams;
101094332d3Sopenharmony_ci    unsigned int numEps;
102094332d3Sopenharmony_ci    unsigned char eps[0];
103094332d3Sopenharmony_ci};
104094332d3Sopenharmony_ci
105094332d3Sopenharmony_cistruct UsbAdapterGetdriver {
106094332d3Sopenharmony_ci    unsigned int interface;
107094332d3Sopenharmony_ci    char driver[MAX_DRIVER_NAME_LENGTH];
108094332d3Sopenharmony_ci};
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_cistruct UsbAdapterIoctl {
111094332d3Sopenharmony_ci    unsigned int interface;
112094332d3Sopenharmony_ci    unsigned int code;
113094332d3Sopenharmony_ci    void *data;
114094332d3Sopenharmony_ci};
115094332d3Sopenharmony_ci
116094332d3Sopenharmony_cistruct UsbAdapterDisconnectClaim {
117094332d3Sopenharmony_ci    unsigned int interface;
118094332d3Sopenharmony_ci    unsigned int flags;
119094332d3Sopenharmony_ci    char driver[MAX_DRIVER_NAME_LENGTH];
120094332d3Sopenharmony_ci};
121094332d3Sopenharmony_ci
122094332d3Sopenharmony_cistruct UsbOsAdapterOps {
123094332d3Sopenharmony_ci    int32_t (*init)(const struct UsbSession *session);
124094332d3Sopenharmony_ci    void (*exit)(const struct UsbSession *session);
125094332d3Sopenharmony_ci    struct UsbDeviceHandle *(*openDevice)(struct UsbSession *session, uint8_t busNum, uint8_t usbAddr);
126094332d3Sopenharmony_ci    void (*closeDevice)(struct UsbDeviceHandle *devHandle);
127094332d3Sopenharmony_ci    int32_t (*getConfigDescriptor)(const struct UsbDevice *device, uint8_t configIndex, void *buffer, size_t len);
128094332d3Sopenharmony_ci    int32_t (*getConfiguration)(const struct UsbDeviceHandle *devHandle, uint8_t *activeConfig);
129094332d3Sopenharmony_ci    int32_t (*setConfiguration)(struct UsbDeviceHandle *devHandle, int32_t activeConfig);
130094332d3Sopenharmony_ci    int32_t (*claimInterface)(const struct UsbDeviceHandle *devHandle, unsigned int interfaceNumber);
131094332d3Sopenharmony_ci    int32_t (*releaseInterface)(const struct UsbDeviceHandle *devHandle, unsigned int interfaceNumber);
132094332d3Sopenharmony_ci    int32_t (*setInterfaceAltsetting)(const struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber,
133094332d3Sopenharmony_ci        uint8_t altsetting);
134094332d3Sopenharmony_ci    int32_t (*clearHalt)(const struct UsbDeviceHandle *devHandle, unsigned int endpoint);
135094332d3Sopenharmony_ci    int32_t (*resetDevice)(struct UsbDeviceHandle *devHandle);
136094332d3Sopenharmony_ci    struct UsbHostRequest *(*allocRequest)(const struct UsbDeviceHandle *handle, int32_t isoPackets, size_t len);
137094332d3Sopenharmony_ci    struct UsbHostRequest *(*allocRequestByMmap)(const struct UsbDeviceHandle *handle, int32_t isoPackets, size_t len);
138094332d3Sopenharmony_ci    int32_t (*freeRequest)(struct UsbHostRequest *request);
139094332d3Sopenharmony_ci    int32_t (*freeRequestByMmap)(struct UsbHostRequest *request);
140094332d3Sopenharmony_ci    int32_t (*submitRequest)(struct UsbHostRequest *request);
141094332d3Sopenharmony_ci    int32_t (*cancelRequest)(struct UsbHostRequest *request);
142094332d3Sopenharmony_ci    int32_t (*urbCompleteHandle)(const struct UsbDeviceHandle *devHandle);
143094332d3Sopenharmony_ci    int32_t (*detachKernelDriverAndClaim)(const struct UsbDeviceHandle *handle, uint32_t interfaceNumber);
144094332d3Sopenharmony_ci    int32_t (*attachKernelDriver)(const struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber);
145094332d3Sopenharmony_ci    int32_t (*detachKernelDriver)(const struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber);
146094332d3Sopenharmony_ci    int32_t (*usbControlMsg)(const struct UsbDeviceHandle *devHandle, struct UsbControlRequestData *ctrlData);
147094332d3Sopenharmony_ci    int32_t (*getUsbSpeed)(const struct UsbDeviceHandle *handle);
148094332d3Sopenharmony_ci    bool (*getInterfaceActiveStatus)(const struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber);
149094332d3Sopenharmony_ci    int32_t (*getDeviceSpeed)(const struct UsbDeviceHandle *devHandle);
150094332d3Sopenharmony_ci};
151094332d3Sopenharmony_ci#ifdef __cplusplus
152094332d3Sopenharmony_ciextern "C" {
153094332d3Sopenharmony_ci#endif
154094332d3Sopenharmony_ci
155094332d3Sopenharmony_cistruct UsbOsAdapterOps *UsbAdapterGetOps(void);
156094332d3Sopenharmony_ciUsbRawTidType UsbAdapterGetTid(void);
157094332d3Sopenharmony_ciint32_t UsbAdapterRegisterSignal(void);
158094332d3Sopenharmony_ciint32_t UsbAdapterKillSignal(struct UsbDeviceHandle *devHandle, UsbRawTidType tid);
159094332d3Sopenharmony_ciint32_t AdapterAtomicInc(OsalAtomic *v);
160094332d3Sopenharmony_ciint32_t AdapterAtomicDec(OsalAtomic *v);
161094332d3Sopenharmony_ci
162094332d3Sopenharmony_ci#ifdef __cplusplus
163094332d3Sopenharmony_ci}
164094332d3Sopenharmony_ci#endif
165094332d3Sopenharmony_ci#endif /* USB_HOST_LINUX_ADAPTER_H */
166