-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhid.py
More file actions
94 lines (81 loc) · 2.73 KB
/
hid.py
File metadata and controls
94 lines (81 loc) · 2.73 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
# This file is part of python-functionfs
# Copyright (C) 2018-2021 Vincent Pelletier <plr.vincent@gmail.com>
#
# 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/>.
"""
HID-specific definitions.
Built partly from linux/hid.h and from spec.
"""
# pylint: disable=too-few-public-methods
import ctypes
from .common import USBDescriptorHeader, u8, le16
from . import ch9
# USB HID (Human Interface Device) interface class code
USB_INTERFACE_CLASS_HID = ch9.USB_CLASS_HID
# USB HID interface subclass and protocol codes
USB_INTERFACE_SUBCLASS_NONE = 0
USB_INTERFACE_SUBCLASS_BOOT = 1
USB_INTERFACE_PROTOCOL_NONE = 0
USB_INTERFACE_PROTOCOL_KEYBOARD = 1
USB_INTERFACE_PROTOCOL_MOUSE = 2
# HID class requests
HID_REQ_GET_REPORT = 0x01
HID_REQ_GET_IDLE = 0x02
HID_REQ_GET_PROTOCOL = 0x03
HID_REQ_SET_REPORT = 0x09
HID_REQ_SET_IDLE = 0x0a
HID_REQ_SET_PROTOCOL = 0x0b
# HID class descriptor types
HID_DT_HID = ch9.USB_TYPE_CLASS | 1
HID_DT_REPORT = ch9.USB_TYPE_CLASS | 2
HID_DT_PHYSICAL = ch9.USB_TYPE_CLASS | 3
HID_MAX_DESCRIPTOR_SIZE = 4096
class _USBHIDDescriptor(USBDescriptorHeader):
"""
HID_DT_HID: HID descriptor
"""
_bDescriptorType = HID_DT_HID
_fields_ = [
('bcdHID', le16),
('bCountryCode', u8),
('bNumDescriptors', u8),
]
class USBHIDDescriptorTail(ctypes.LittleEndianStructure):
"""
HID_DT_HID descriptor ends with a variable number of these 2 fields.
"""
_pack_ = 1
_fields_ = [
('bDescriptorType', u8),
('wDescriptorLength', le16),
]
def getUSBHIDDescriptorClass(hid_descriptor_count=1):
"""
Concatenate as many USBHIDDescriptorTail as requested to USBHIDDescriptor,
and return resulting class.
hid_descriptor_count (int)
Number of HID descriptor entries.
Should be at least 1, as there must be one USB_DT_REPORT descriptor.
Note: as of this writing (circa 4.18), f_fs only supports exactly
1 HID descriptor entry.
"""
return type(
'USBHIDDescriptorWithTail',
(_USBHIDDescriptor, ),
{
'_fields_': [
('tail', USBHIDDescriptorTail * hid_descriptor_count),
]
}
)