-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfunctionfs.py
More file actions
314 lines (275 loc) · 11.2 KB
/
functionfs.py
File metadata and controls
314 lines (275 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# This file is part of python-functionfs
# Copyright (C) 2016-2021 Vincent Pelletier <[email protected]>
#
# python-functionfs is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# python-functionfs is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with python-functionfs. If not, see <http://www.gnu.org/licenses/>.
"""
Linux functionfs definitions.
From linux/usb/functionfs.h .
"""
# pylint: disable=too-few-public-methods
import ctypes
import ioctl_opt
from .common import u8, le16, le32, Enum
from .ch9 import (
USBEndpointDescriptor,
USBCtrlRequest,
)
DESCRIPTORS_MAGIC = 1
STRINGS_MAGIC = 2
DESCRIPTORS_MAGIC_V2 = 3
FLAGS = Enum({
'HAS_FS_DESC': 1,
'HAS_HS_DESC': 2,
'HAS_SS_DESC': 4,
'HAS_MS_OS_DESC': 8,
'VIRTUAL_ADDR': 16,
'EVENTFD': 32,
'ALL_CTRL_RECIP': 64,
'CONFIG0_SETUP': 128,
})
# Descriptor of an non-audio endpoint
class DescsHeadV2(ctypes.LittleEndianStructure):
"""
| off | name | type | description |
|-----+-----------+--------------+--------------------------------------|
| 0 | magic | LE32 | FUNCTIONFS_DESCRIPTORS_MAGIC_V2 |
| 4 | length | LE32 | length of the whole data chunk |
| 8 | flags | LE32 | combination of functionfs_flags |
| | eventfd | LE32 | eventfd file descriptor |
| | fs_count | LE32 | number of full-speed descriptors |
| | hs_count | LE32 | number of high-speed descriptors |
| | ss_count | LE32 | number of super-speed descriptors |
| | os_count | LE32 | number of MS OS descriptors |
| | fs_descrs | Descriptor[] | list of full-speed descriptors |
| | hs_descrs | Descriptor[] | list of high-speed descriptors |
| | ss_descrs | Descriptor[] | list of super-speed descriptors |
| | os_descrs | OSDesc[] | list of MS OS descriptors |
Depending on which flags are set, various fields may be missing in the
structure. Any flags that are not recognised cause the whole block to be
rejected with -ENOSYS.
"""
_pack_ = 1
_fields_ = [
('magic', le32),
('length', le32),
('flags', le32),
]
# le32 fs_count, hs_count, fs_count; must be included manually in
# the structure taking flags into consideration.
class DescsHead(ctypes.LittleEndianStructure):
"""
Legacy descriptors format (deprecated as of 3.14):
| off | name | type | description |
|-----+-----------+--------------+--------------------------------------|
| 0 | magic | LE32 | FUNCTIONFS_DESCRIPTORS_MAGIC |
| 4 | length | LE32 | length of the whole data chunk |
| 8 | fs_count | LE32 | number of full-speed descriptors |
| 12 | hs_count | LE32 | number of high-speed descriptors |
| 16 | fs_descrs | Descriptor[] | list of full-speed descriptors |
| | hs_descrs | Descriptor[] | list of high-speed descriptors |
"""
_pack_ = 1
_fields_ = [
('magic', le32),
('length', le32),
('fs_count', le32),
('hs_count', le32),
]
class OSDescHeaderBCount(ctypes.LittleEndianStructure):
_fields_ = (
('bCount', u8),
('Reserved', u8),
)
class OSDescHeader(ctypes.LittleEndianStructure):
"""
MS OS Descriptor header
OSDesc[] is an array of valid MS OS Feature Descriptors which have one of
the following formats:
| off | name | type | description |
|-----+-----------------+------+--------------------------|
| 0 | interface | U8 | related interface number |
| 1 | dwLength | U32 | length of the descriptor |
| 5 | bcdVersion | U16 | currently supported: 1 |
| 7 | wIndex | U16 | currently supported: 4 |
| 9 | bCount | U8 | number of ext. compat. |
| 10 | Reserved | U8 | 0 |
| 11 | ExtCompat[] | | list of ext. compat. d. |
| off | name | type | description |
|-----+-----------------+------+--------------------------|
| 0 | interface | U8 | related interface number |
| 1 | dwLength | U32 | length of the descriptor |
| 5 | bcdVersion | U16 | currently supported: 1 |
| 7 | wIndex | U16 | currently supported: 5 |
| 9 | wCount | U16 | number of ext. compat. |
| 11 | ExtProp[] | | list of ext. prop. d. |
"""
_pack_ = 1
_anonymous_ = [
'u',
]
_fields_ = [
('interface', u8),
('dwLength', le32),
('bcdVersion', le16),
('wIndex', le16),
(
'u',
type(
'Count',
(ctypes.Union, ),
{
'_fields_': [
('b', OSDescHeaderBCount),
('wCount', le16),
],
},
),
),
]
class OSExt(ctypes.LittleEndianStructure):
pass
class OSExtCompatDesc(OSExt):
"""
ExtCompat[] is an array of valid Extended Compatiblity descriptors
which have the following format:
| off | name | type | description |
|-----+-----------------------+------+-------------------------------------|
| 0 | bFirstInterfaceNumber | U8 | index of the interface or of the 1st|
| | | | interface in an IAD group |
| 1 | Reserved | U8 | 1 |
| 2 | CompatibleID | U8[8]| compatible ID string |
| 10 | SubCompatibleID | U8[8]| subcompatible ID string |
| 18 | Reserved | U8[6]| 0 |
"""
_fields_ = [
('bFirstInterfaceNumber', u8),
('Reserved1', u8),
('CompatibleID', u8 * 8),
('SubCompatibleID', u8 * 8),
('Reserved2', u8 * 6),
]
class OSExtPropDescHead(OSExt):
"""
ExtProp[] is an array of valid Extended Properties descriptors
which have the following format:
| off | name | type | description |
|-----+-----------------------+------+-------------------------------------|
| 0 | dwSize | U32 | length of the descriptor |
| 4 | dwPropertyDataType | U32 | 1..7 |
| 8 | wPropertyNameLength | U16 | bPropertyName length (NL) |
| 10 | bPropertyName |U8[NL]| name of this property |
|10+NL| dwPropertyDataLength | U32 | bPropertyData length (DL) |
|14+NL| bProperty |U8[DL]| payload of this property |
"""
_pack_ = 1
_fields_ = [
('dwSize', le32),
('dwPropertyDataType', le32),
('wPropertyNameLength', le16),
]
class StringsHead(ctypes.LittleEndianStructure):
"""
Strings format:
| off | name | type | description |
|-----+------------+-----------------------+----------------------------|
| 0 | magic | LE32 | FUNCTIONFS_STRINGS_MAGIC |
| 4 | length | LE32 | length of the data chunk |
| 8 | str_count | LE32 | number of strings |
| 12 | lang_count | LE32 | number of languages |
| 16 | stringtab | StringTab[lang_count] | table of strings per lang |
"""
_pack_ = 1
_fields_ = [
('magic', le32),
('length', le32),
('str_count', le32),
('lang_count', le32),
]
class StringBase(ctypes.LittleEndianStructure):
"""
For each language there is one stringtab entry (ie. there are lang_count
stringtab in total). Each StringTab has following format:
| off | name | type | description |
|-----+---------+-------------------+------------------------------------|
| 0 | lang | LE16 | language code |
| 2 | strings | String[str_count] | array of strings in given language |
For each string there is one strings entry (ie. there are str_count
string entries). Each String is a NUL terminated string encoded in
UTF-8.
"""
_pack_ = 1
_fields_ = [
('lang', le16),
]
EVENT_TYPE = Enum({
'BIND': 0,
'UNBIND': 1,
'ENABLE': 2,
'DISABLE': 3,
'SETUP': 4,
'SUSPEND': 5,
'RESUME': 6,
})
class Event(ctypes.LittleEndianStructure):
"""
Events are delivered on the ep0 file descriptor, when the user mode driver
reads from this file descriptor after writing the descriptors. Don't
stop polling this descriptor.
NOTE: this structure must stay the same size and layout on
both 32-bit and 64-bit kernels.
"""
_pack_ = 1
_fields_ = [
(
'u',
type(
'u',
(ctypes.Union, ),
{
'_fields_': [
# SETUP: packet; DATA phase i/o precedes next event
# (setup.bmRequestType & USB_DIR_IN) flags direction
('setup', USBCtrlRequest),
],
}
),
),
# event_type
('type', u8),
('_pad', u8 * 3),
]
# Endpoint ioctls
# The same as in gadgetfs
# IN transfers may be reported to the gadget driver as complete
# when the fifo is loaded, before the host reads the data;
# OUT transfers may be reported to the host's "client" driver as
# complete when they're sitting in the FIFO unread.
# THIS returns how many bytes are "unclaimed" in the endpoint fifo
# (needed for precise fault handling, when the hardware allows it)
FIFO_STATUS = ioctl_opt.IO(ord('g'), 1)
# discards any unclaimed data in the fifo.
FIFO_FLUSH = ioctl_opt.IO(ord('g'), 2)
# resets endpoint halt+toggle; used to implement set_interface.
# some hardware (like pxa2xx) can't support this.
CLEAR_HALT = ioctl_opt.IO(ord('g'), 3)
# Specific for functionfs
# Returns reverse mapping of an interface. Called on EP0. If there
# is no such interface returns -EDOM. If function is not active
# returns -ENODEV.
INTERFACE_REVMAP = ioctl_opt.IO(ord('g'), 128)
# Returns real bEndpointAddress of an endpoint. If function is not
# active returns -ENODEV.
ENDPOINT_REVMAP = ioctl_opt.IO(ord('g'), 129)
# Returns endpoint descriptor. If function is not active returns -ENODEV.
ENDPOINT_DESC = ioctl_opt.IOR(ord('g'), 130, USBEndpointDescriptor)