Skip to content

Commit ffa45de

Browse files
authored
Merge pull request #8989 from MariuszCwikla/hid_interface_name
usb_hid.set_interface_name
2 parents 73fc021 + 52c38c8 commit ffa45de

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

shared-bindings/usb_hid/__init__.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,39 @@ STATIC mp_obj_t usb_hid_get_boot_device(void) {
155155
}
156156
MP_DEFINE_CONST_FUN_OBJ_0(usb_hid_get_boot_device_obj, usb_hid_get_boot_device);
157157

158+
159+
//| def set_interface_name(interface_name: str) -> None:
160+
//| """Override HID interface name in the USB Interface Descriptor.
161+
//|
162+
//| ``interface_name`` must be an ASCII string (or buffer) of at most 126.
163+
//|
164+
//| This method must be called in boot.py to have any effect.
165+
//|
166+
//| Not available on boards without native USB support.
167+
//| """
168+
//| ...
169+
//|
170+
STATIC mp_obj_t usb_hid_set_interface_name(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
171+
static const mp_arg_t allowed_args[] = {
172+
{ MP_QSTR_interface_name, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = mp_const_none} }
173+
};
174+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
175+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t *)&args);
176+
177+
mp_buffer_info_t interface_name;
178+
mp_get_buffer_raise(args[0].u_obj, &interface_name, MP_BUFFER_READ);
179+
mp_arg_validate_length_range(interface_name.len, 1, 126, MP_QSTR_interface_name);
180+
181+
if (custom_usb_hid_interface_name == NULL) {
182+
custom_usb_hid_interface_name = port_malloc(sizeof(char) * 128, false);
183+
}
184+
memcpy(custom_usb_hid_interface_name, interface_name.buf, interface_name.len);
185+
custom_usb_hid_interface_name[interface_name.len] = 0;
186+
187+
return mp_const_none;
188+
}
189+
MP_DEFINE_CONST_FUN_OBJ_KW(usb_hid_set_interface_name_obj, 1, usb_hid_set_interface_name);
190+
158191
// usb_hid.devices is set once the usb devices are determined, after boot.py runs.
159192
STATIC mp_map_elem_t usb_hid_module_globals_table[] = {
160193
{ MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid) },
@@ -163,6 +196,7 @@ STATIC mp_map_elem_t usb_hid_module_globals_table[] = {
163196
{ MP_ROM_QSTR(MP_QSTR_disable), MP_OBJ_FROM_PTR(&usb_hid_disable_obj) },
164197
{ MP_ROM_QSTR(MP_QSTR_enable), MP_OBJ_FROM_PTR(&usb_hid_enable_obj) },
165198
{ MP_ROM_QSTR(MP_QSTR_get_boot_device), MP_OBJ_FROM_PTR(&usb_hid_get_boot_device_obj) },
199+
{ MP_ROM_QSTR(MP_QSTR_set_interface_name), MP_OBJ_FROM_PTR(&usb_hid_set_interface_name_obj) },
166200
};
167201

168202
STATIC MP_DEFINE_MUTABLE_DICT(usb_hid_module_globals, usb_hid_module_globals_table);

shared-module/usb_hid/Device.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ const usb_hid_device_obj_t usb_hid_device_consumer_control_obj = {
164164
.out_report_lengths = { 0, },
165165
};
166166

167+
char *custom_usb_hid_interface_name;
168+
167169
STATIC size_t get_report_id_idx(usb_hid_device_obj_t *self, size_t report_id) {
168170
for (size_t i = 0; i < self->num_report_ids; i++) {
169171
if (report_id == self->report_ids[i]) {

shared-module/usb_hid/Device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ extern const usb_hid_device_obj_t usb_hid_device_consumer_control_obj;
5454

5555
void usb_hid_device_create_report_buffers(usb_hid_device_obj_t *self);
5656

57+
extern char *custom_usb_hid_interface_name;
58+
5759
#endif /* SHARED_MODULE_USB_HID_DEVICE_H */

shared-module/usb_hid/__init__.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ size_t usb_hid_descriptor_length(void) {
163163
return sizeof(usb_hid_descriptor_template);
164164
}
165165

166-
static const char usb_hid_interface_name[] = USB_INTERFACE_NAME " HID";
167-
168166
// This is the interface descriptor, not the report descriptor.
169167
size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *descriptor_counts, uint8_t *current_interface_string, uint16_t report_descriptor_length, uint8_t boot_device) {
168+
const char *usb_hid_interface_name;
169+
170170
memcpy(descriptor_buf, usb_hid_descriptor_template, sizeof(usb_hid_descriptor_template));
171171

172172
descriptor_buf[HID_DESCRIPTOR_INTERFACE_INDEX] = descriptor_counts->current_interface;
@@ -177,6 +177,11 @@ size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *desc
177177
descriptor_buf[HID_DESCRIPTOR_INTERFACE_PROTOCOL_INDEX] = boot_device; // 1: keyboard, 2: mouse
178178
}
179179

180+
if (custom_usb_hid_interface_name == NULL) {
181+
usb_hid_interface_name = USB_INTERFACE_NAME " HID";
182+
} else {
183+
usb_hid_interface_name = custom_usb_hid_interface_name;
184+
}
180185
usb_add_interface_string(*current_interface_string, usb_hid_interface_name);
181186
descriptor_buf[HID_DESCRIPTOR_INTERFACE_STRING_INDEX] = *current_interface_string;
182187
(*current_interface_string)++;

0 commit comments

Comments
 (0)