-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadafruit_usb_host_descriptors.py
72 lines (56 loc) · 1.99 KB
/
adafruit_usb_host_descriptors.py
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
# SPDX-FileCopyrightText: Copyright (c) 2023 Scott Shawcroft for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_usb_host_descriptors`
================================================================================
Helpers for getting USB descriptors
* Author(s): Scott Shawcroft
"""
import struct
from micropython import const
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_USB_Host_Descriptors.git"
# USB defines
# Use const for these internal values so that they are inlined with mpy-cross.
_DIR_OUT = const(0x00)
_DIR_IN = const(0x80)
_REQ_RCPT_DEVICE = const(0)
_REQ_TYPE_STANDARD = const(0x00)
_REQ_GET_DESCRIPTOR = const(6)
# No const because these are public
DESC_DEVICE = 0x01
DESC_CONFIGURATION = 0x02
DESC_STRING = 0x03
DESC_INTERFACE = 0x04
DESC_ENDPOINT = 0x05
def get_descriptor(device, desc_type, index, buf, language_id=0):
"""Fetch the descriptor from the device into buf."""
# Allow capitalization that matches the USB spec.
# pylint: disable=invalid-name
wValue = desc_type << 8 | index
wIndex = language_id
device.ctrl_transfer(
_REQ_RCPT_DEVICE | _REQ_TYPE_STANDARD | _DIR_IN,
_REQ_GET_DESCRIPTOR,
wValue,
wIndex,
buf,
)
def get_device_descriptor(device):
"""Fetch the device descriptor and return it."""
buf = bytearray(1)
get_descriptor(device, DESC_DEVICE, 0, buf)
full_buf = bytearray(buf[0])
get_descriptor(device, DESC_DEVICE, 0, full_buf)
return full_buf
def get_configuration_descriptor(device, index):
"""Fetch the configuration descriptor, its associated descriptors and return it."""
# Allow capitalization that matches the USB spec.
# pylint: disable=invalid-name
buf = bytearray(4)
get_descriptor(device, DESC_CONFIGURATION, index, buf)
wTotalLength = struct.unpack("<xxH", buf)[0]
full_buf = bytearray(wTotalLength)
get_descriptor(device, DESC_CONFIGURATION, index, full_buf)
return full_buf