circuitpython/shared-module/usb_hid/Device.c

249 lines
10 KiB
C
Raw Normal View History

2018-07-31 05:42:04 -04:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 hathach for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
2021-04-28 23:48:26 -04:00
#include "py/gc.h"
2018-07-31 05:42:04 -04:00
#include "py/runtime.h"
#include "shared-bindings/usb_hid/Device.h"
2021-04-27 14:37:36 -04:00
#include "shared-module/usb_hid/__init__.h"
#include "shared-module/usb_hid/Device.h"
2018-08-16 17:21:44 -04:00
#include "supervisor/shared/translate.h"
#include "supervisor/shared/tick.h"
2018-07-31 05:42:04 -04:00
#include "tusb.h"
2021-04-27 14:37:36 -04:00
static const uint8_t keyboard_report_descriptor[] = {
2021-04-23 00:18:05 -04:00
0x05, 0x01, // 0,1 Usage Page (Generic Desktop Ctrls)
0x09, 0x06, // 2,3 Usage (Keyboard)
0xA1, 0x01, // 4,5 Collection (Application)
0x85, 0xFF, // 6,7 Report ID [SET AT RUNTIME]
#define KEYBOARD_REPORT_ID_INDEX (7)
0x05, 0x07, // Usage Page (Kbrd/Keypad)
0x19, 0xE0, // Usage Minimum (0xE0)
0x29, 0xE7, // Usage Maximum (0xE7)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x08, // Report Count (8)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x19, 0x00, // Usage Minimum (0x00)
0x29, 0xDD, // Usage Maximum (0xDD)
0x15, 0x00, // Logical Minimum (0)
0x25, 0xDD, // Logical Maximum (-35)
0x75, 0x08, // Report Size (8)
0x95, 0x06, // Report Count (6)
0x81, 0x00, // Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x05, 0x08, // Usage Page (LEDs)
0x19, 0x01, // Usage Minimum (Num Lock)
0x29, 0x05, // Usage Maximum (Kana)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x05, // Report Count (5)
0x91, 0x02, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0x95, 0x03, // Report Count (3)
0x91, 0x01, // Output (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0xC0, // End Collection
2021-04-26 23:54:01 -04:00
};
2021-04-23 00:18:05 -04:00
const usb_hid_device_obj_t usb_hid_device_keyboard_obj = {
2021-04-27 14:37:36 -04:00
.report_descriptor = keyboard_report_descriptor,
.report_descriptor_length = sizeof(keyboard_report_descriptor),
.usage_page = 0x01,
2021-04-23 00:18:05 -04:00
.usage = 0x06,
.in_report_length = 8,
.out_report_length = 1,
.report_id_index = KEYBOARD_REPORT_ID_INDEX,
};
2021-04-27 14:37:36 -04:00
static const uint8_t mouse_report_descriptor[] = {
2021-04-23 00:18:05 -04:00
0x05, 0x01, // 0,1 Usage Page (Generic Desktop Ctrls)
0x09, 0x02, // 2,3 Usage (Mouse)
0xA1, 0x01, // 4,5 Collection (Application)
0x09, 0x01, // 6,7 Usage (Pointer)
0xA1, 0x00, // 8,9 Collection (Physical)
0x85, 0xFF, // 10, 11 Report ID [SET AT RUNTIME]
#define MOUSE_REPORT_ID_INDEX (11)
0x05, 0x09, // Usage Page (Button)
0x19, 0x01, // Usage Minimum (0x01)
0x29, 0x05, // Usage Maximum (0x05)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x95, 0x05, // Report Count (5)
0x75, 0x01, // Report Size (1)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x95, 0x01, // Report Count (1)
0x75, 0x03, // Report Size (3)
0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y)
0x15, 0x81, // Logical Minimum (-127)
0x25, 0x7F, // Logical Maximum (127)
0x75, 0x08, // Report Size (8)
0x95, 0x02, // Report Count (2)
0x81, 0x06, // Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
0x09, 0x38, // Usage (Wheel)
0x15, 0x81, // Logical Minimum (-127)
0x25, 0x7F, // Logical Maximum (127)
0x75, 0x08, // Report Size (8)
0x95, 0x01, // Report Count (1)
0x81, 0x06, // Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
0xC0, // End Collection
0xC0, // End Collection
};
const usb_hid_device_obj_t usb_hid_device_mouse_obj = {
2021-04-27 14:37:36 -04:00
.report_descriptor = mouse_report_descriptor,
.report_descriptor_length = sizeof(mouse_report_descriptor),
.usage_page = 0x01,
2021-04-23 00:18:05 -04:00
.usage = 0x02,
.in_report_length = 4,
.out_report_length = 0,
.report_id_index = MOUSE_REPORT_ID_INDEX,
};
2021-04-27 14:37:36 -04:00
static const uint8_t consumer_control_report_descriptor[] = {
2021-04-23 00:18:05 -04:00
0x05, 0x0C, // 0,1 Usage Page (Consumer)
0x09, 0x01, // 2,3 Usage (Consumer Control)
0xA1, 0x01, // 4,5 Collection (Application)
0x85, 0xFF, // 6,7 Report ID [SET AT RUNTIME]
#define CONSUMER_CONTROL_REPORT_ID_INDEX (7)
0x75, 0x10, // Report Size (16)
0x95, 0x01, // Report Count (1)
0x15, 0x01, // Logical Minimum (1)
0x26, 0x8C, 0x02, // Logical Maximum (652)
0x19, 0x01, // Usage Minimum (Consumer Control)
0x2A, 0x8C, 0x02, // Usage Maximum (AC Send)
0x81, 0x00, // Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0, // End Collection
};
const usb_hid_device_obj_t usb_hid_device_consumer_control_obj = {
2021-04-27 14:37:36 -04:00
.report_descriptor = consumer_control_report_descriptor,
.report_descriptor_length = sizeof(consumer_control_report_descriptor),
.usage_page = 0x0C,
2021-04-23 00:18:05 -04:00
.usage = 0x01,
.in_report_length = 2,
.out_report_length = 0,
.report_id_index = CONSUMER_CONTROL_REPORT_ID_INDEX,
};
2021-04-27 14:37:36 -04:00
void common_hal_usb_hid_device_construct(usb_hid_device_obj_t *self, mp_obj_t report_descriptor, uint8_t usage_page, uint8_t usage, uint8_t in_report_length, uint8_t out_report_length, uint8_t report_id_index) {
2021-04-28 23:48:26 -04:00
// report buffer pointers are NULL at start, and are created when USB is initialized.
2021-04-23 21:44:13 -04:00
mp_buffer_info_t bufinfo;
2021-04-27 14:37:36 -04:00
mp_get_buffer_raise(report_descriptor, &bufinfo, MP_BUFFER_READ);
self->report_descriptor_length = bufinfo.len;
2021-04-23 21:44:13 -04:00
2021-04-28 23:48:26 -04:00
// Copy the raw the descriptor bytes into a heap obj. We don't keep the Python descriptor object.
uint8_t *descriptor_bytes = gc_alloc(bufinfo.len, false, false);
memcpy(descriptor_bytes, bufinfo.buf, bufinfo.len);
self->report_descriptor = descriptor_bytes;
2021-04-23 00:18:05 -04:00
self->usage_page = usage_page;
self->usage = usage;
self->in_report_length = in_report_length;
self->out_report_length = out_report_length;
self->report_id_index = report_id_index;
}
2018-07-31 05:42:04 -04:00
uint8_t common_hal_usb_hid_device_get_usage_page(usb_hid_device_obj_t *self) {
return self->usage_page;
}
uint8_t common_hal_usb_hid_device_get_usage(usb_hid_device_obj_t *self) {
return self->usage;
}
2021-03-15 09:57:36 -04:00
void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t *report, uint8_t len) {
2021-04-23 00:18:05 -04:00
if (len != self->in_report_length) {
mp_raise_ValueError_varg(translate("Buffer incorrect size. Should be %d bytes."), self->in_report_length);
2018-07-31 05:42:04 -04:00
}
// Wait until interface is ready, timeout = 2 seconds
uint64_t end_ticks = supervisor_ticks_ms64() + 2000;
2021-03-15 09:57:36 -04:00
while ((supervisor_ticks_ms64() < end_ticks) && !tud_hid_ready()) {
2019-09-03 14:44:46 -04:00
RUN_BACKGROUND_TASKS;
}
2018-07-31 05:42:04 -04:00
2021-03-15 09:57:36 -04:00
if (!tud_hid_ready()) {
2018-08-16 17:21:44 -04:00
mp_raise_msg(&mp_type_OSError, translate("USB Busy"));
2018-07-31 05:42:04 -04:00
}
2021-04-23 00:18:05 -04:00
memcpy(self->in_report_buffer, report, len);
2018-07-31 05:42:04 -04:00
2021-04-27 14:37:36 -04:00
if (!tud_hid_report(self->report_id, self->in_report_buffer, len)) {
2018-08-16 17:21:44 -04:00
mp_raise_msg(&mp_type_OSError, translate("USB Error"));
2018-07-31 05:42:04 -04:00
}
}
2021-04-28 23:48:26 -04:00
void usb_hid_device_create_report_buffers(usb_hid_device_obj_t *self) {
if (self->in_report_length > 0) {
self->in_report_buffer = gc_alloc(self->in_report_length, false, true /*long-lived*/);
}
if (self->out_report_length > 0) {
self->out_report_buffer = gc_alloc(self->out_report_length, false, true /*long-lived*/);
}
}
2021-04-28 23:48:26 -04:00
2018-07-31 05:42:04 -04:00
// Callbacks invoked when receive Get_Report request through control endpoint
2021-03-15 09:57:36 -04:00
uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t *buffer, uint16_t reqlen) {
(void)itf;
2018-07-31 05:42:04 -04:00
// only support Input Report
2021-03-15 09:57:36 -04:00
if (report_type != HID_REPORT_TYPE_INPUT) {
return 0;
}
2018-07-31 05:42:04 -04:00
// fill buffer with current report
2021-04-27 14:37:36 -04:00
memcpy(buffer, usb_hid_get_device_with_report_id(report_id)->in_report_buffer, reqlen);
2018-07-31 05:42:04 -04:00
return reqlen;
}
// Callbacks invoked when receive Set_Report request through control endpoint
2021-03-15 09:57:36 -04:00
void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t const *buffer, uint16_t bufsize) {
(void)itf;
2020-08-19 08:18:17 -04:00
if (report_type == HID_REPORT_TYPE_INVALID) {
report_id = buffer[0];
buffer++;
bufsize--;
} else if (report_type != HID_REPORT_TYPE_OUTPUT) {
return;
}
2021-04-27 14:37:36 -04:00
usb_hid_device_obj_t *hid_device = usb_hid_get_device_with_report_id(report_id);
2018-07-31 12:02:15 -04:00
2020-08-19 08:18:17 -04:00
if (hid_device && hid_device->out_report_length >= bufsize) {
memcpy(hid_device->out_report_buffer, buffer, bufsize);
2018-07-31 05:42:04 -04:00
}
}