summaryrefslogtreecommitdiff
path: root/firmware/usbstack/usb_hid.c
diff options
context:
space:
mode:
authorJames Buren <braewoods+rb@braewoods.net>2021-03-05 11:51:44 -0600
committerSolomon Peachy <pizza@shaftnet.org>2021-03-06 04:15:02 +0000
commitf647cde3c72d05fd9ba1a179ae1638883997ed2f (patch)
tree73830ff1e9b918861541b0cb8ff84f7eb0c74c74 /firmware/usbstack/usb_hid.c
parent9cf45374e07c6a56f598f47d1fd83eab0291047e (diff)
downloadrockbox-f647cde3c72d05fd9ba1a179ae1638883997ed2f.tar.gz
rockbox-f647cde3c72d05fd9ba1a179ae1638883997ed2f.zip
usb_hid: add support for Battery Strength
This allows rockbox to report its battery status through the HID Battery Strength method that is available through the Device Controls usage page. Change-Id: Ia7a7dd9b9d476dd9df5a5f5becabc5ae823e9a89
Diffstat (limited to 'firmware/usbstack/usb_hid.c')
-rw-r--r--firmware/usbstack/usb_hid.c32
1 files changed, 31 insertions, 1 deletions
diff --git a/firmware/usbstack/usb_hid.c b/firmware/usbstack/usb_hid.c
index ed0816395a..0b672e6452 100644
--- a/firmware/usbstack/usb_hid.c
+++ b/firmware/usbstack/usb_hid.c
@@ -23,6 +23,7 @@
23#include "usb_core.h" 23#include "usb_core.h"
24#include "usb_drv.h" 24#include "usb_drv.h"
25#include "kernel.h" 25#include "kernel.h"
26#include "powermgmt.h"
26#include "usb_hid.h" 27#include "usb_hid.h"
27#include "usb_class_driver.h" 28#include "usb_class_driver.h"
28/*#define LOGF_ENABLE*/ 29/*#define LOGF_ENABLE*/
@@ -109,6 +110,7 @@ typedef enum
109#ifdef HAVE_USB_HID_MOUSE 110#ifdef HAVE_USB_HID_MOUSE
110 REPORT_ID_MOUSE, 111 REPORT_ID_MOUSE,
111#endif 112#endif
113 REPORT_ID_BACKGROUND,
112 REPORT_ID_COUNT, 114 REPORT_ID_COUNT,
113} report_id_t; 115} report_id_t;
114 116
@@ -450,6 +452,15 @@ static uint8_t buf_set_mouse(unsigned char *buf, int id)
450} 452}
451#endif /* HAVE_USB_HID_MOUSE */ 453#endif /* HAVE_USB_HID_MOUSE */
452 454
455#define BUF_LEN_BACKGROUND 1
456static uint8_t buf_set_background(unsigned char *buf, int id)
457{
458 memset(buf, 0, BUF_LEN_BACKGROUND);
459 buf[0] = (uint8_t)id;
460
461 return BUF_LEN_BACKGROUND;
462}
463
453static size_t descriptor_report_get(unsigned char *dest) 464static size_t descriptor_report_get(unsigned char *dest)
454{ 465{
455 unsigned char *report = dest; 466 unsigned char *report = dest;
@@ -551,6 +562,24 @@ static size_t descriptor_report_get(unsigned char *dest)
551 PACK_VAL(report, END_COLLECTION); 562 PACK_VAL(report, END_COLLECTION);
552#endif /* HAVE_USB_HID_MOUSE */ 563#endif /* HAVE_USB_HID_MOUSE */
553 564
565 /* Background controls */
566 usb_hid_report = &usb_hid_reports[REPORT_ID_BACKGROUND];
567 usb_hid_report->usage_page = HID_USAGE_PAGE_GENERIC_DEVICE_CONTROLS;
568 usb_hid_report->buf_set = buf_set_background;
569 usb_hid_report->is_key_released = 0;
570
571 pack_parameter(&report, 0, 1, USAGE_PAGE, HID_USAGE_PAGE_GENERIC_DEVICE_CONTROLS);
572 pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DEVICE_BACKGROUND_CONTROLS);
573 pack_parameter(&report, 0, 1, COLLECTION, COLLECTION_APPLICATION);
574 pack_parameter(&report, 0, 1, REPORT_ID, REPORT_ID_BACKGROUND);
575 pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DEVICE_BATTERY_STRENGTH);
576 pack_parameter(&report, 0, 1, LOGICAL_MINIMUM, 0);
577 pack_parameter(&report, 0, 1, LOGICAL_MAXIMUM, 100);
578 pack_parameter(&report, 0, 1, REPORT_SIZE, 8);
579 pack_parameter(&report, 0, 1, REPORT_COUNT, 1);
580 pack_parameter(&report, 0, 1, INPUT, MAIN_ITEM_VARIABLE);
581 PACK_VAL(report, END_COLLECTION);
582
554 return (size_t)(report - dest); 583 return (size_t)(report - dest);
555} 584}
556 585
@@ -591,9 +620,9 @@ int usb_hid_get_config_descriptor(unsigned char *dest, int max_packet_size)
591void usb_hid_init_connection(void) 620void usb_hid_init_connection(void)
592{ 621{
593 logf("hid: init connection"); 622 logf("hid: init connection");
594
595 active = true; 623 active = true;
596 currently_sending = false; 624 currently_sending = false;
625 set_battery_reporting(true);
597} 626}
598 627
599/* called by usb_core_init() */ 628/* called by usb_core_init() */
@@ -614,6 +643,7 @@ void usb_hid_init(void)
614void usb_hid_disconnect(void) 643void usb_hid_disconnect(void)
615{ 644{
616 logf("hid: disconnect"); 645 logf("hid: disconnect");
646 set_battery_reporting(false);
617 active = false; 647 active = false;
618 currently_sending = false; 648 currently_sending = false;
619} 649}