Skip to content

Commit d4fab49

Browse files
committed
attempt to fix cyberpower issues fixed by debug logging
Signed-off-by: Jordan Rodgers <[email protected]>
1 parent 8989fda commit d4fab49

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

drivers/cps-hid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static usb_device_id_t cps_usb_device_table[] = {
8686
{ USB_DEVICE(CPS_VENDORID, 0x0005), NULL },
8787
/* Dynex DX-800U?, CP1200AVR/BC1200D, CP825AVR-G, CP1000AVRLCD, CP1000PFCLCD, CP1500C, CP550HG, etc. */
8888
{ USB_DEVICE(CPS_VENDORID, 0x0501), &cps_battery_scale },
89-
/* OR2200LCDRM2U, OR700LCDRM1U, PR6000LCDRTXL5U, CP1350EPFCLCD */
89+
/* OR2200LCDRM2U, OR700LCDRM1U, PR6000LCDRTXL5U, CP1350EPFCLCD, CP1500AVRLCD3, CP1500PFCLCD */
9090
{ USB_DEVICE(CPS_VENDORID, 0x0601), NULL },
9191

9292
/* Cyber Energy branded devices by CPS */

drivers/libhid.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,10 @@ static struct {
405405
* since it's used to produce sub-drivers "stub" using
406406
* scripts/subdriver/gen-usbhid-subdriver.sh
407407
*/
408-
void HIDDumpTree(hid_dev_handle_t udev, HIDDevice_t *hd, usage_tables_t *utab)
408+
void HIDDumpTree(hid_dev_handle_t udev, HIDDevice_t *hd, usage_tables_t *utab, int force_fetch)
409409
{
410410
size_t i;
411+
int should_output_debug = (nut_debug_level >= 1);
411412
#if (defined SHUT_MODE) && SHUT_MODE
412413
NUT_UNUSED_VARIABLE(hd);
413414
#else /* !SHUT_MODE => USB */
@@ -416,16 +417,18 @@ void HIDDumpTree(hid_dev_handle_t udev, HIDDevice_t *hd, usage_tables_t *utab)
416417
int productID = hd->ProductID;
417418
#endif /* SHUT_MODE / USB */
418419

419-
/* Do not go further if we already know nothing will be displayed.
420-
* Some reports take a while before they timeout, so if these are
420+
/* Some reports take a while before they timeout, so if these are
421421
* not used in the driver, they should only be fetched when we're
422-
* in debug mode
422+
* in debug mode OR when force_fetch is enabled (needed for some
423+
* devices like CPS 0x0601 to properly initialize report buffer cache).
423424
*/
424-
if (nut_debug_level < 1) {
425+
if (!should_output_debug && !force_fetch) {
425426
return;
426427
}
427428

428-
upsdebugx(1, "%" PRIuSIZE " HID objects found", pDesc->nitems);
429+
if (should_output_debug) {
430+
upsdebugx(1, "%" PRIuSIZE " HID objects found", pDesc->nitems);
431+
}
429432

430433
for (i = 0; i < pDesc->nitems; i++)
431434
{
@@ -452,18 +455,24 @@ void HIDDumpTree(hid_dev_handle_t udev, HIDDevice_t *hd, usage_tables_t *utab)
452455
}
453456
#endif /* SHUT_MODE / USB */
454457

455-
/* Get data value */
458+
/* Get data value - always fetch to populate report buffer cache */
456459
if (HIDGetDataValue(udev, pData, &value, MAX_TS) == 1) {
457-
upsdebugx(1, "Path: %s, Type: %s, ReportID: 0x%02x, Offset: %i, Size: %i, Value: %g",
458-
HIDGetDataItem(pData, utab), HIDDataType(pData), pData->ReportID, pData->Offset, pData->Size, value);
460+
if (should_output_debug) {
461+
upsdebugx(1, "Path: %s, Type: %s, ReportID: 0x%02x, Offset: %i, Size: %i, Value: %g",
462+
HIDGetDataItem(pData, utab), HIDDataType(pData), pData->ReportID, pData->Offset, pData->Size, value);
463+
}
459464
continue;
460465
}
461466

462-
upsdebugx(1, "Path: %s, Type: %s, ReportID: 0x%02x, Offset: %i, Size: %i",
463-
HIDGetDataItem(pData, utab), HIDDataType(pData), pData->ReportID, pData->Offset, pData->Size);
467+
if (should_output_debug) {
468+
upsdebugx(1, "Path: %s, Type: %s, ReportID: 0x%02x, Offset: %i, Size: %i",
469+
HIDGetDataItem(pData, utab), HIDDataType(pData), pData->ReportID, pData->Offset, pData->Size);
470+
}
464471
}
465472

466-
fflush(stdout);
473+
if (should_output_debug) {
474+
fflush(stdout);
475+
}
467476
}
468477

469478
/* Returns text string which can be used to display type of data

drivers/libhid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ int HIDGetEvents(hid_dev_handle_t udev, HIDData_t **event, int eventlen);
153153
/*
154154
* Support functions
155155
* -------------------------------------------------------------------------- */
156-
void HIDDumpTree(hid_dev_handle_t udev, HIDDevice_t *hd, usage_tables_t *utab);
156+
void HIDDumpTree(hid_dev_handle_t udev, HIDDevice_t *hd, usage_tables_t *utab, int force_fetch);
157157
const char *HIDDataType(const HIDData_t *hiddata);
158158

159159
void free_report_buffer(reportbuf_t *rbuf);

drivers/usbhid-ups.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define DRIVER_VERSION "0.71"
3333

3434
#define HU_VAR_WAITBEFORERECONNECT "waitbeforereconnect"
35+
#define HU_VAR_EXPLOREHIDALL "explorehidall"
3536

3637
#include "main.h" /* Must be first, includes "config.h" */
3738
#include "nut_stdint.h"
@@ -142,6 +143,7 @@ bool_t use_interrupt_pipe = TRUE;
142143
bool_t use_interrupt_pipe = FALSE;
143144
#endif
144145
static size_t interrupt_pipe_EIO_count = 0; /* How many times we had I/O errors since last reconnect? */
146+
static int explorehidall = -1; /* Force all HID reports fetch on init (for some CPS devices); -1=unset, 0=off, 1=on */
145147

146148
/**
147149
* How many times do we tolerate having "0 HID objects" in a row?
@@ -1317,6 +1319,9 @@ void upsdrv_makevartable(void)
13171319

13181320
addvar(VAR_FLAG, "pollonly", "Don't use interrupt pipe, only use polling (recommended for CPS devices)");
13191321

1322+
addvar(VAR_FLAG, HU_VAR_EXPLOREHIDALL,
1323+
"Fetch all HID reports during initialization (needed for some CPS devices)");
1324+
13201325
addvar(VAR_VALUE, "interrupt_pipe_no_events_tolerance", "How many times in a row do we tolerate \"Got 0 HID objects\" from USB interrupts?");
13211326

13221327
addvar(VAR_FLAG, "onlinedischarge",
@@ -1665,6 +1670,21 @@ void upsdrv_initinfo(void)
16651670
#endif
16661671
}
16671672

1673+
/* fetch all HID reports on init (auto-enabled for some CPS models) */
1674+
if (testvar(HU_VAR_EXPLOREHIDALL)) {
1675+
explorehidall = 1;
1676+
#if !((defined SHUT_MODE) && SHUT_MODE)
1677+
} else {
1678+
/* auto-enable for CPS ProductID 0x0601 which needs this for stable communication
1679+
* (CP1500AVRLCD3, CP1500PFCLCD, etc.) - see https://github.com/networkupstools/nut/issues/3116 */
1680+
if (subdriver == &cps_subdriver && curDevice.ProductID == 0x0601) {
1681+
explorehidall = 1;
1682+
upsdebugx(1, "Enabling '%s' for CPS device 0x%04x",
1683+
HU_VAR_EXPLOREHIDALL, curDevice.ProductID);
1684+
}
1685+
#endif
1686+
}
1687+
16681688
val = getval("interrupt_pipe_no_events_tolerance");
16691689
if (!val || !str_to_long(val, &interrupt_pipe_no_events_tolerance, 10)) {
16701690
interrupt_pipe_no_events_tolerance = -1;
@@ -2105,7 +2125,7 @@ static int callback(
21052125
"please note that a wrong subdriver could have been chosen above; "
21062126
"consider testing others with an explicit driver option",
21072127
__func__);
2108-
HIDDumpTree(udev, arghd, subdriver->utab);
2128+
HIDDumpTree(udev, arghd, subdriver->utab, explorehidall);
21092129

21102130
#if !((defined SHUT_MODE) && SHUT_MODE)
21112131
/* create a new matcher for later matching */

0 commit comments

Comments
 (0)