• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef USB_HOST_LINUX_ADAPTER_H
17 #define USB_HOST_LINUX_ADAPTER_H
18 
19 #include "usb_raw_api_library.h"
20 
21 #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
22 #include <sys/syscall.h>
gettid()23 static inline pid_t gettid()
24 {
25     return syscall(SYS_gettid);
26 }
27 #endif
28 
29 #define USBDEV_PATH     "/dev"
30 #define USB_DEV_FS_PATH "/dev/bus/usb"
31 
32 #define BUS_OFFSET      8
33 
34 #define MAX_BULK_DATA_BUFFER_LENGTH     4096
35 #define MAX_BULK_URBS_PER_REQUEST       1
36 
37 #define MAX_ISO_PACKETS_PER_URB         128
38 #define MAX_ISO_DATA_BUFFER_LEN         (8 * 1024)
39 #define MAX_ISO_URBS_PER_REQUEST        (((MAX_ISO_DATA_BUFFER_LEN - 1) / MAX_ISO_PACKETS_PER_URB) + 1)
40 
41 #define URBS_PER_REQUEST                MAX(MAX_BULK_URBS_PER_REQUEST, MAX_ISO_URBS_PER_REQUEST)
42 
43 #define MAX_ISO_SHARE_MEMERY_SIZE       sizeof(struct UsbHostRequest) + \
44     (URBS_PER_REQUEST * sizeof(struct UsbAdapterUrb)) + MAX_ISO_DATA_BUFFER_LEN
45 #define MAX_BULK_SHARE_MEMERY_SIZE      sizeof(struct UsbHostRequest) + \
46     (URBS_PER_REQUEST * sizeof(struct UsbAdapterUrb)) + MAX_BULK_DATA_BUFFER_LENGTH
47 #define MAX_CTRL_BUFFER_LENGTH  4096
48 #define MAX_DRIVER_NAME_LENGTH  256
49 #define DISCONNECT_CLAIM_EXCEPT_DRIVER  2
50 
51 #define USB_ADAPTER_URB_TYPE_ISO                0
52 #define USB_ADAPTER_URB_TYPE_INTERRUPT          1
53 #define USB_ADAPTER_URB_TYPE_CONTROL            2
54 #define USB_ADAPTER_URB_TYPE_BULK               3
55 #define USB_ADAPTER_URB_SHORT_NOT_OK            0x01
56 #define USB_ADAPTER_URB_ISO_ASAP                0x02
57 #define USB_ADAPTER_URB_BULK_CONTINUATION       0x04
58 #define USB_ADAPTER_URB_QUEUE_BULK              0x10
59 #define USB_ADAPTER_URB_ZERO_PACKET             0x40
60 #define USB_ADAPTER_CAP_ZERO_PACKET             0x01
61 #define USB_ADAPTER_CAP_BULK_CONTINUATION       0x02
62 #define USB_ADAPTER_CAP_NO_PACKET_SIZE_LIM      0x04
63 #define USB_ADAPTER_CAP_BULK_SCATTER_GATHER     0x08
64 #define USB_ADAPTER_CAP_REAP_AFTER_DISCONNECT   0x10
65 
66 #define USBDEVFS_CONTROL            _IOWR('U', 0, struct UsbControlRequestData)
67 #define USBDEVFS_SETINTERFACE       _IOR('U', 4, struct UsbAdapterSetInterface)
68 #define USBDEVFS_SETCONFIGURATION   _IOR('U', 5, unsigned int)
69 #define USBDEVFS_GETDRIVER          _IOW('U', 8, struct UsbAdapterGetdriver)
70 #define USBDEVFS_SUBMITURB          _IOR('U', 10, struct UsbAdapterUrb)
71 #define USBDEVFS_DISCARDURB         _IO('U', 11)
72 #define USBDEVFS_REAPURB            _IOW('U', 12, void *)
73 #define USBDEVFS_CLAIMINTERFACE     _IOR('U', 15, unsigned int)
74 #define USBDEVFS_RELEASEINTERFACE   _IOR('U', 16, unsigned int)
75 #define USBDEVFS_IOCTL              _IOWR('U', 18, struct UsbAdapterIoctl)
76 #define USBDEVFS_RESET              _IO('U', 20)
77 #define USBDEVFS_CLEAR_HALT         _IOR('U', 21, unsigned int)
78 #define USBDEVFS_DISCONNECT         _IO('U', 22)
79 #define USBDEVFS_CONNECT            _IO('U', 23)
80 #define USBDEVFS_GET_CAPABILITIES   _IOR('U', 26, unsigned int)
81 #define USBDEVFS_DISCONNECT_CLAIM   _IOR('U', 27, struct UsbAdapterDisconnectClaim)
82 #define USBDEVFS_ALLOC_STREAMS      _IOR('U', 28, struct UsbAdapterStreams)
83 #define USBDEVFS_FREE_STREAMS       _IOR('U', 29, struct UsbAdapterStreams)
84 
85 struct UsbAdapterSetInterface {
86     unsigned int interface;
87     unsigned int altSetting;
88 };
89 
90 struct UsbAdapterStreams {
91     unsigned int numStreams;
92     unsigned int numEps;
93     unsigned char eps[0];
94 };
95 
96 struct UsbAdapterGetdriver {
97     unsigned int interface;
98     char driver[MAX_DRIVER_NAME_LENGTH];
99 };
100 
101 struct UsbAdapterIoctl {
102     unsigned int interface;
103     unsigned int code;
104     void *data;
105 };
106 
107 struct UsbAdapterDisconnectClaim {
108     unsigned int interface;
109     unsigned int flags;
110     char driver[MAX_DRIVER_NAME_LENGTH];
111 };
112 
113 struct UsbOsAdapterOps {
114     int32_t (*init)(const struct UsbSession *session);
115     void (*exit)(const struct UsbSession *session);
116     struct UsbDeviceHandle *(*openDevice)(struct UsbSession *session, uint8_t busNum, uint8_t usbAddr);
117     void (*closeDevice)(struct UsbDeviceHandle *devHandle);
118     int32_t (*getConfigDescriptor)(const struct UsbDevice *device, uint8_t configIndex, void *buffer, size_t len);
119     int32_t (*getConfiguration)(const struct UsbDeviceHandle *devHandle, uint8_t *activeConfig);
120     int32_t (*setConfiguration)(struct UsbDeviceHandle *devHandle, int32_t activeConfig);
121     int32_t (*claimInterface)(const struct UsbDeviceHandle *devHandle, unsigned int interfaceNumber);
122     int32_t (*releaseInterface)(const struct UsbDeviceHandle *devHandle, unsigned int interfaceNumber);
123     int32_t (*setInterfaceAltsetting)(const struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber,
124         uint8_t altsetting);
125     int32_t (*clearHalt)(const struct UsbDeviceHandle *devHandle, unsigned int endpoint);
126     int32_t (*resetDevice)(const struct UsbDeviceHandle *devHandle);
127     struct UsbHostRequest *(*allocRequest)(const struct UsbDeviceHandle *handle, int32_t isoPackets, size_t len);
128     struct UsbHostRequest *(*allocRequestByMmap)(const struct UsbDeviceHandle *handle, int32_t isoPackets, size_t len);
129     int32_t (*freeRequest)(struct UsbHostRequest *request);
130     int32_t (*freeRequestByMmap)(struct UsbHostRequest *request);
131     int32_t (*submitRequest)(struct UsbHostRequest *request);
132     int32_t (*cancelRequest)(struct UsbHostRequest *request);
133     int32_t (*urbCompleteHandle)(const struct UsbDeviceHandle *devHandle);
134     int32_t (*detachKernelDriverAndClaim)(const struct UsbDeviceHandle *handle, uint32_t interfaceNumber);
135     void (*attachKernelDriver)(const struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber);
136 };
137 #ifdef __cplusplus
138 extern "C" {
139 #endif
140 
141 struct UsbOsAdapterOps *UsbAdapterGetOps(void);
142 UsbRawTidType UsbAdapterGetTid(void);
143 int32_t UsbAdapterRegisterSignal(void);
144 int32_t UsbAdapterKillSignal(struct UsbDeviceHandle *devHandle, UsbRawTidType tid);
145 int32_t AdapterAtomicInc(OsalAtomic *v);
146 int32_t AdapterAtomicDec(OsalAtomic *v);
147 
148 #ifdef __cplusplus
149 }
150 #endif
151 #endif /* USB_HOST_LINUX_ADAPTER_H */
152