-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheizoctl.c
1780 lines (1557 loc) · 51 KB
/
eizoctl.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* eizoctl.c: EIZO Monitor Control
*
* This program stays independent of the liberty library
* in order to build on Windows.
*
* Copyright (c) 2025, Přemysl Eric Janouch <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
// On Windows, vswprintf() interprets %s in the width of the format string,
// and %hs is not really compliant with any standard:
// https://devblogs.microsoft.com/oldnewthing/20190830-00/?p=102823
#ifdef _WIN32
#define __USE_MINGW_ANSI_STDIO
#endif
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <math.h>
#include <assert.h>
#include <getopt.h>
#include <hidapi.h>
#include "config.h"
#undef PROGRAM_NAME
#define PROGRAM_NAME "eizoctl"
#ifdef __OpenBSD__
#define hid_init hidapi_hid_init
#endif
#if defined __MINGW_GNU_PRINTF
#define ATTRIBUTE_PRINTF(x, y) __MINGW_GNU_PRINTF((x), (y))
#elif defined __GNUC__
#define ATTRIBUTE_PRINTF(x, y) __attribute__((format(printf, x, y)))
#else
#define ATTRIBUTE_PRINTF(x, y)
#endif
static uint16_t
peek_u16le(const uint8_t *p)
{
return (uint16_t) p[1] << 8 | p[0];
}
static void
put_u16le(uint8_t *p, uint16_t value)
{
p[0] = value;
p[1] = value >> 8;
}
// --- Partial USB HID report descriptor parser --------------------------------
// This parser only needs to support EIZO monitors,
// which keep to a simple scheme, so it is appropriately simplified.
enum {
PARSER_REPORT_LIMIT = 256,
};
enum {
PARSER_ITEM_TYPE_MAIN = 0,
PARSER_ITEM_TYPE_GLOBAL,
PARSER_ITEM_TYPE_LOCAL,
PARSER_ITEM_TYPE_RESERVED,
PARSER_ITEM_TAG_LONG = 0xf,
PARSER_ITEM_TAG_MAIN_INPUT = 0x8,
PARSER_ITEM_TAG_MAIN_OUTPUT = 0x9,
PARSER_ITEM_TAG_MAIN_FEATURE = 0xb,
PARSER_ITEM_TAG_MAIN_COLLECTION = 0xa,
PARSER_ITEM_TAG_MAIN_END_COLLECTION = 0xc,
PARSER_ITEM_TAG_GLOBAL_USAGE_PAGE = 0x0,
PARSER_ITEM_TAG_GLOBAL_LOGICAL_MINIMUM = 0x1,
PARSER_ITEM_TAG_GLOBAL_LOGICAL_MAXIMUM = 0x2,
PARSER_ITEM_TAG_GLOBAL_PHYSICAL_MINIMUM = 0x3,
PARSER_ITEM_TAG_GLOBAL_PHYSICAL_MAXIMUM = 0x4,
PARSER_ITEM_TAG_GLOBAL_UNIT_EXPONENT = 0x5,
PARSER_ITEM_TAG_GLOBAL_UNIT = 0x6,
PARSER_ITEM_TAG_GLOBAL_REPORT_SIZE = 0x7,
PARSER_ITEM_TAG_GLOBAL_REPORT_ID = 0x8,
PARSER_ITEM_TAG_GLOBAL_REPORT_COUNT = 0x9,
PARSER_ITEM_TAG_GLOBAL_PUSH = 0xa,
PARSER_ITEM_TAG_GLOBAL_POP = 0xb,
PARSER_ITEM_TAG_LOCAL_USAGE = 0x0,
PARSER_ITEM_TAG_LOCAL_USAGE_MINIMUM = 0x1,
PARSER_ITEM_TAG_LOCAL_USAGE_MAXIMUM = 0x2,
PARSER_ITEM_TAG_LOCAL_DESIGNATOR_INDEX = 0x3,
PARSER_ITEM_TAG_LOCAL_DESIGNATOR_MINIMUM = 0x4,
PARSER_ITEM_TAG_LOCAL_DESIGNATOR_MAXIMUM = 0x5,
PARSER_ITEM_TAG_LOCAL_STRING_INDEX = 0x7,
PARSER_ITEM_TAG_LOCAL_STRING_MINIMUM = 0x8,
PARSER_ITEM_TAG_LOCAL_STRING_MAXIMUM = 0x9,
PARSER_ITEM_TAG_LOCAL_DELIMITER = 0xa,
};
struct parser_report {
uint32_t usage;
int32_t logical_minimum;
int32_t logical_maximum;
uint32_t report_size;
uint32_t report_id;
uint32_t report_count;
};
struct parser {
struct parser_state_global {
uint32_t usage_page;
int32_t logical_minimum; // \_ If neither is negative,
int32_t logical_maximum; // / the report field is unsigned.
int32_t physical_minimum; // \ These actually
int32_t physical_maximum; // > start as UNDEFINED,
int32_t unit_exponent; // / which we can't express.
int32_t unit;
uint32_t report_size;
uint32_t report_id;
uint32_t report_count;
} global;
struct parser_state_local {
uint32_t usage;
uint32_t usage_minimum;
uint32_t usage_maximum;
uint32_t designator_index;
uint32_t designator_minimum;
uint32_t designator_maximum;
uint32_t string_index;
uint32_t string_minimum;
uint32_t string_maximum;
uint32_t delimiter;
} local;
struct parser_report input[PARSER_REPORT_LIMIT];
struct parser_report feature[PARSER_REPORT_LIMIT];
};
static const char *
parse_item_set(struct parser *parser, uint32_t flags, bool feature)
{
bool array__variable = (flags >> 1) & 1;
bool absolute__relative = (flags >> 2) & 1;
bool non_volatile__volatile = (flags >> 7) & 1;
if (!array__variable) {
// Skip: This occurs at the end of the secondary descriptor.
return NULL;
}
if (absolute__relative)
return "Report item kind not supported: relative";
if (feature && non_volatile__volatile)
return "Report item kind not supported: volatile";
// We should really decide by the data length instead.
uint32_t usage = parser->local.usage;
if (usage < 0x10000)
usage = parser->global.usage_page << 16 | usage;
if (!usage)
return "zero Usage";
struct parser_state_global *global = &parser->global;
if (!global->report_id)
return "missing Report ID";
if (global->report_id >= PARSER_REPORT_LIMIT)
return "Report ID is too high";
if (global->report_size % 8) {
// Skip: This occurs at the end of the secondary descriptor.
return NULL;
}
struct parser_report *report = feature
? &parser->feature[global->report_id]
: &parser->input[global->report_id];
if (report->usage)
return "only one item per Report is supported";
report->usage = usage;
report->logical_minimum = global->logical_minimum;
report->logical_maximum = global->logical_maximum;
report->report_size = global->report_size;
report->report_id = global->report_id;
report->report_count = global->report_count;
return NULL;
}
static const char *
parse_item(
struct parser *parser, uint8_t type, uint8_t tag, int32_t s, uint32_t u)
{
switch (type) {
case PARSER_ITEM_TYPE_MAIN: {
const char *err = NULL;
switch (tag) {
break; case PARSER_ITEM_TAG_MAIN_INPUT:
err = parse_item_set(parser, u, false);
break; case PARSER_ITEM_TAG_MAIN_OUTPUT:
return "output items are not supported";
break; case PARSER_ITEM_TAG_MAIN_FEATURE:
err = parse_item_set(parser, u, true);
break; case PARSER_ITEM_TAG_MAIN_COLLECTION:
// Ignore for now.
// Top level Collections must be Application.
break; case PARSER_ITEM_TAG_MAIN_END_COLLECTION:
// Ignore for now.
break; default:
return "unsupported Main item tag";
}
parser->local = (struct parser_state_local) {};
return err;
}
case PARSER_ITEM_TYPE_GLOBAL:
switch (tag) {
break; case PARSER_ITEM_TAG_GLOBAL_USAGE_PAGE:
parser->global.usage_page = u;
break; case PARSER_ITEM_TAG_GLOBAL_LOGICAL_MINIMUM:
parser->global.logical_minimum = s;
break; case PARSER_ITEM_TAG_GLOBAL_LOGICAL_MAXIMUM:
parser->global.logical_maximum = s;
break; case PARSER_ITEM_TAG_GLOBAL_PHYSICAL_MINIMUM:
parser->global.physical_minimum = s;
break; case PARSER_ITEM_TAG_GLOBAL_PHYSICAL_MAXIMUM:
parser->global.physical_maximum = s;
break; case PARSER_ITEM_TAG_GLOBAL_UNIT_EXPONENT:
parser->global.unit_exponent = s;
break; case PARSER_ITEM_TAG_GLOBAL_UNIT:
parser->global.unit = s;
break; case PARSER_ITEM_TAG_GLOBAL_REPORT_SIZE:
parser->global.report_size = u;
break; case PARSER_ITEM_TAG_GLOBAL_REPORT_ID:
parser->global.report_id = u;
break; case PARSER_ITEM_TAG_GLOBAL_REPORT_COUNT:
parser->global.report_count = u;
break; case PARSER_ITEM_TAG_GLOBAL_PUSH:
return "state pushing is not supported";
break; case PARSER_ITEM_TAG_GLOBAL_POP:
return "state pushing is not supported";
break; default:
return "unsupported Global item tag";
}
break;
case PARSER_ITEM_TYPE_LOCAL:
switch (tag) {
break; case PARSER_ITEM_TAG_LOCAL_USAGE:
// Note that reports can have multiple usages.
parser->local.usage = u;
break; case PARSER_ITEM_TAG_LOCAL_USAGE_MINIMUM:
parser->local.usage_minimum = u;
break; case PARSER_ITEM_TAG_LOCAL_USAGE_MAXIMUM:
parser->local.usage_maximum = u;
break; case PARSER_ITEM_TAG_LOCAL_DESIGNATOR_INDEX:
parser->local.designator_index = u;
break; case PARSER_ITEM_TAG_LOCAL_DESIGNATOR_MINIMUM:
parser->local.designator_minimum = u;
break; case PARSER_ITEM_TAG_LOCAL_DESIGNATOR_MAXIMUM:
parser->local.designator_maximum = u;
break; case PARSER_ITEM_TAG_LOCAL_STRING_INDEX:
parser->local.string_index = u;
break; case PARSER_ITEM_TAG_LOCAL_STRING_MINIMUM:
parser->local.string_minimum = u;
break; case PARSER_ITEM_TAG_LOCAL_STRING_MAXIMUM:
parser->local.string_maximum = u;
break; case PARSER_ITEM_TAG_LOCAL_DELIMITER:
parser->local.delimiter = u;
break; default:
return "unsupported Local item tag";
}
break;
case PARSER_ITEM_TYPE_RESERVED:
// Completely unnecessary.
return "long/reserved items are not supported";
}
return NULL;
}
static const char *
parse_descriptor(struct parser *parser, const uint8_t *descriptor, size_t len)
{
// USB HID 5.2 Report Descriptors
const uint8_t *p = descriptor, *end = p + len;
while (p != end) {
// USB HID 5.3 Generic Item Format
// USB HID 6.2.2.1 Items Types and Tags
uint8_t prefix = *p++,
size = prefix & 0x3,
type = (prefix >> 2) & 0x3,
tag = prefix >> 4;
size += size == 3;
if (p + size > end)
return "item overflow";
uint32_t uvalue = 0;
int32_t svalue = 0;
switch (size) {
break; case 0:
break; case 1:
uvalue = p[0];
svalue = (int8_t) p[0];
break; case 2:
uvalue = p[0] | p[1] << 8;
svalue = (int16_t) (p[0] | p[1] << 8);
break; case 4:
uvalue = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
svalue = (int32_t) uvalue;
}
p += size;
const char *err = parse_item(parser, type, tag, svalue, uvalue);
if (err)
return err;
}
return NULL;
}
// --- EIZO monitor control ----------------------------------------------------
enum {
USB_VID_EIZO = 0x056d,
USB_USAGE_PAGE__MONITOR = 0x80,
USB_USAGE_PAGE__VESA_VIRTUAL_CONTROLS = 0x82,
USB_USAGE_PAGE_MONITOR_ID__MONITOR_CONTROL = 0x01,
EIZO_REPORT_ID_SECONDARY_DESCRIPTOR = 1,
EIZO_REPORT_ID_SET = 2,
EIZO_REPORT_ID_GET = 3,
EIZO_REPORT_ID_SET_LONG = 4,
EIZO_REPORT_ID_GET_LONG = 5,
EIZO_REPORT_ID_PIN_CODE = 6,
EIZO_REPORT_ID_RESULT = 7,
EIZO_REPORT_ID_SERIAL_PRODUCT = 8,
EIZO_REPORT_ID_PROFILE = 9,
EIZO_REPORT_ID_CRITICAL_SECTION = 10,
// I'm not sure of the interaction.
EIZO_REPORT_ID_CRITICAL_SET = 11,
EIZO_REPORT_ID_CRITICAL_GET = 12,
EIZO_REPORT_ID_CRITICAL_SET_LONG = 13,
EIZO_REPORT_ID_CRITICAL_GET_LONG = 14,
EIZO_REPORT_ID_COUNT = 10,
EIZO_SUBREPORT_COUNT = PARSER_REPORT_LIMIT,
EIZO_PROFILE_KEY_INPUT_PORTS = 0x53,
EIZO_PROFILE_KEY_USB_C_INPUT_PORTS = 0x61,
};
struct eizo_monitor {
uint16_t vid, pid; ///< USB device identification
hid_device *dev; ///< HID device handle
uint16_t pin_code; ///< Anti-race counter
char serial[9], product[17]; ///< Serial number, product name
struct eizo_profile_item {
uint8_t len;
uint8_t data[255];
} profile[256];
struct parser_report
report_input[EIZO_REPORT_ID_COUNT],
report_feature[EIZO_REPORT_ID_COUNT],
subreports_input[EIZO_SUBREPORT_COUNT],
subreports_feature[EIZO_SUBREPORT_COUNT];
// As a theme, we spend memory in order to limit code and dependencies.
char error[1024];
};
static bool
eizo_monitor_failf(struct eizo_monitor *m, const char *format, ...)
ATTRIBUTE_PRINTF(2, 3);
static bool
eizo_monitor_failf(struct eizo_monitor *m, const char *format, ...)
{
int len = *m->product
? snprintf(m->error, sizeof m->error, "%s %s: ", m->product, m->serial)
: snprintf(m->error, sizeof m->error, "%04x:%04x: ", m->vid, m->pid);
if (len >= 0) {
va_list ap;
va_start(ap, format);
(void) vsnprintf(m->error + len, sizeof m->error - len, format, ap);
va_end(ap);
}
return false;
}
static size_t
eizo_monitor_report_len(const struct parser_report *r)
{
assert(r->report_size % 8 == 0);
return r->report_size / 8 * r->report_count;
}
static bool
eizo_monitor_read_secondary_descriptor(struct eizo_monitor *m)
{
const struct parser_report *r =
&m->report_feature[EIZO_REPORT_ID_SECONDARY_DESCRIPTOR];
size_t lenr = 1 + r->report_size / 8 * r->report_count;
uint8_t buf[1024] = {r->report_id};
enum { HEADER_LEN = 1 + 2 + 2 };
if (sizeof buf < lenr)
return eizo_monitor_failf(m, "buffer too short");
if (hid_send_feature_report(m->dev, buf, lenr) < 0)
return eizo_monitor_failf(m,
"secondary descriptor Set_Feature failed: %ls", hid_error(m->dev));
uint8_t descriptor[8 << 10];
if (hid_get_feature_report(m->dev, buf, lenr) < 0)
return eizo_monitor_failf(m,
"secondary descriptor Get_Feature failed: %ls", hid_error(m->dev));
size_t offset = peek_u16le(&buf[1]);
if (offset)
return eizo_monitor_failf(m,
"secondary descriptor starts at an unexpected offset");
size_t descriptor_len = peek_u16le(&buf[3]);
if (descriptor_len > sizeof descriptor)
return eizo_monitor_failf(m, "secondary descriptor is too long A");
memcpy(descriptor + offset, &buf[HEADER_LEN], lenr - HEADER_LEN);
offset += lenr - HEADER_LEN;
while (offset < descriptor_len) {
if (hid_get_feature_report(m->dev, buf, lenr) < 0)
return eizo_monitor_failf(m,
"secondary descriptor Get_Feature failed: %ls",
hid_error(m->dev));
if (peek_u16le(&buf[1]) != offset)
return eizo_monitor_failf(m,
"secondary descriptor starts at an unexpected offset");
// We could also limit the amount we copy, but whatever.
if (offset + lenr - HEADER_LEN > sizeof descriptor)
return eizo_monitor_failf(m, "secondary descriptor is too long");
memcpy(descriptor + offset, &buf[HEADER_LEN], lenr - HEADER_LEN);
offset += lenr - HEADER_LEN;
}
#if DUMP_DESCRIPTORS
for (size_t i = 0; i < descriptor_len; i++)
printf("%02x ", descriptor[i]);
printf("\n");
#endif
struct parser parser = {};
const char *err = parse_descriptor(&parser, descriptor, descriptor_len);
if (err)
return eizo_monitor_failf(m, "secondary descriptor: %s", err);
memcpy(m->subreports_feature, parser.feature, sizeof m->subreports_feature);
memcpy(m->subreports_input, parser.input, sizeof m->subreports_input);
return true;
}
static bool
eizo_monitor_read_profile(struct eizo_monitor *m)
{
const struct parser_report *r = &m->report_feature[EIZO_REPORT_ID_PROFILE];
size_t lenr = 1 + eizo_monitor_report_len(r);
uint8_t buf[1024] = {r->report_id};
enum { HEADER_LEN = 1 + 2 + 2 };
if (sizeof buf < lenr)
return eizo_monitor_failf(m, "buffer too short");
if (hid_get_feature_report(m->dev, buf, lenr) < 0)
return eizo_monitor_failf(m,
"profile Get_Feature failed: %ls", hid_error(m->dev));
const uint8_t *p = buf + 1, *end = buf + lenr;
while (p + 2 <= end && p[0] != 0xff && p + 2 + p[1] <= end) {
struct eizo_profile_item *item = &m->profile[p[0]];
item->len = p[1];
memcpy(item->data, p + 2, item->len);
p += 2 + item->len;
}
return true;
}
static bool
eizo_monitor_open(struct eizo_monitor *m, const struct hid_device_info *info)
{
m->vid = info->vendor_id;
m->pid = info->product_id;
if (info->usage_page != USB_USAGE_PAGE__MONITOR ||
info->usage != USB_USAGE_PAGE_MONITOR_ID__MONITOR_CONTROL)
return eizo_monitor_failf(m, "unexpected HID usage");
// There can be more displays with the same VID/PID,
// and info does not contain the serial number to tell them apart.
hid_device *dev = hid_open_path(info->path);
if (!dev)
return eizo_monitor_failf(m, "%ls", hid_error(NULL));
uint8_t descriptor[HID_API_MAX_REPORT_DESCRIPTOR_SIZE] = {};
int len = hid_get_report_descriptor(dev, descriptor, sizeof descriptor);
if (len < 0) {
eizo_monitor_failf(m, "failed to read report descriptor");
goto out;
}
struct parser parser = {};
const char *err = parse_descriptor(&parser, descriptor, len);
if (err) {
eizo_monitor_failf(m, "failed to parse report descriptor: %s", err);
goto out;
}
for (unsigned id = 1; id < EIZO_REPORT_ID_COUNT; id++) {
if (parser.feature[id].usage == (0xff300000 | id))
continue;
eizo_monitor_failf(m, "EIZO HID report %u not supported", id);
goto out;
}
#if DUMP_DESCRIPTORS
for (size_t i = 0; i < (size_t) len; i++)
printf("%02x ", descriptor[i]);
printf("\n");
#endif
uint8_t pinbuf[3] = {EIZO_REPORT_ID_PIN_CODE, 0, 0};
if (hid_get_feature_report(dev, pinbuf, sizeof pinbuf) != sizeof pinbuf) {
eizo_monitor_failf(m, "failed to get PIN code: %ls", hid_error(dev));
goto out;
}
// ---
// Get it now, so that we have better error messages.
uint8_t idbuf[32] = {EIZO_REPORT_ID_SERIAL_PRODUCT};
size_t idlen = 1 + eizo_monitor_report_len(&parser.feature[idbuf[0]]);
if (sizeof idbuf < idlen) {
eizo_monitor_failf(m, "%s: %s",
"failed to get serial number and product", "report too long");
goto out;
}
if (hid_get_feature_report(dev, idbuf, idlen) != (int) idlen) {
eizo_monitor_failf(m, "%s: %ls",
"failed to get serial number and product", hid_error(dev));
goto out;
}
for (size_t i = idlen; --i && idbuf[i] == ' '; )
idbuf[i] = 0;
m->dev = dev;
m->pin_code = peek_u16le(&pinbuf[1]);
memcpy(m->serial, &idbuf[1], 8);
memcpy(m->product, &idbuf[9], idlen - 9);
memcpy(m->report_feature, parser.feature, sizeof m->report_feature);
memcpy(m->report_input, parser.input, sizeof m->report_input);
// Note that there are also "reduced models" without secondary descriptors.
// Those are all very old.
if (eizo_monitor_read_secondary_descriptor(m) &&
eizo_monitor_read_profile(m))
return true;
out:
hid_close(dev);
return false;
}
static void
eizo_monitor_close(struct eizo_monitor *m)
{
if (m->dev)
hid_close(m->dev);
*m = (struct eizo_monitor) {};
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static const struct parser_report *
eizo_monitor_subreport(const struct eizo_monitor *m, uint32_t usage)
{
// It doesn't really matter how efficient this is.
size_t len = sizeof m->subreports_feature / sizeof m->subreports_feature[0];
for (size_t i = 0; i < len; i++)
if (m->subreports_feature[i].usage == usage)
return &m->subreports_feature[i];
return NULL;
}
static bool
eizo_monitor_set(
struct eizo_monitor *m, uint32_t usage, const uint8_t *data, size_t len)
{
const struct parser_report *subr = eizo_monitor_subreport(m, usage),
*set1 = &m->report_feature[EIZO_REPORT_ID_SET],
*set2 = &m->report_feature[EIZO_REPORT_ID_SET_LONG];
if (!subr)
return eizo_monitor_failf(m, "set: usage not found: %#08x", usage);
size_t len1 = 1 + eizo_monitor_report_len(set1);
size_t len2 = 1 + eizo_monitor_report_len(set2);
// We need to encapsulate it.
uint8_t buf[1024] = {};
enum { HEADER_LEN = 1 + 4 + 2 };
if (len != eizo_monitor_report_len(subr) ||
sizeof buf < HEADER_LEN + len ||
sizeof buf < len1 || sizeof buf < len2)
return eizo_monitor_failf(m, "set: length check failed");
put_u16le(&buf[1], usage >> 16);
put_u16le(&buf[3], usage);
put_u16le(&buf[5], m->pin_code);
memcpy(&buf[7], data, len);
const struct parser_report *r = len1 >= HEADER_LEN + len ? set1 : set2;
size_t lenr = 1 + eizo_monitor_report_len(r);
buf[0] = r->report_id;
if (hid_send_feature_report(m->dev, buf, lenr) < 0)
return eizo_monitor_failf(m,
"set: Set_Feature failed: %ls", hid_error(m->dev));
// Don't use EIZO_REPORT_ID_RESULT now.
return true;
}
static bool
eizo_monitor_get(
struct eizo_monitor *m, uint32_t usage, uint8_t *data, size_t len)
{
const struct parser_report *subr = eizo_monitor_subreport(m, usage),
*get1 = &m->report_feature[EIZO_REPORT_ID_GET],
*get2 = &m->report_feature[EIZO_REPORT_ID_GET_LONG];
if (!subr)
return eizo_monitor_failf(m, "get: usage not found: %#08x", usage);
size_t len1 = 1 + eizo_monitor_report_len(get1);
size_t len2 = 1 + eizo_monitor_report_len(get2);
// We need to encapsulate it.
uint8_t buf[1024] = {};
enum { HEADER_LEN = 1 + 4 + 2 };
if (len != eizo_monitor_report_len(subr) ||
sizeof buf < HEADER_LEN + len ||
sizeof buf < len1 || sizeof buf < len2)
return eizo_monitor_failf(m, "get: length check failed");
put_u16le(&buf[1], usage >> 16);
put_u16le(&buf[3], usage);
put_u16le(&buf[5], m->pin_code);
const struct parser_report *r = len1 >= HEADER_LEN + len ? get1 : get2;
size_t lenr = 1 + eizo_monitor_report_len(r);
buf[0] = r->report_id;
if (hid_send_feature_report(m->dev, buf, lenr) < 0)
return eizo_monitor_failf(m,
"get: Set_Feature failed: %ls", hid_error(m->dev));
if (hid_get_feature_report(m->dev, buf, lenr) < 0)
return eizo_monitor_failf(m,
"get: Get_Feature failed: %ls", hid_error(m->dev));
if ((uint32_t) (peek_u16le(&buf[1]) << 16 | peek_u16le(&buf[3])) != usage ||
peek_u16le(&buf[5]) != m->pin_code)
return eizo_monitor_failf(m, "get: invalid result");
memcpy(data, buf + HEADER_LEN, len);
return true;
}
// --- EIZO monitor utilities --------------------------------------------------
enum {
DSUB1 = 0x100,
DSUB2 = 0x101,
DVI1 = 0x200,
DVI2 = 0x201,
DP1 = 0x300,
DP2 = 0x301,
HDMI1 = 0x400,
HDMI2 = 0x401,
};
static const char
// USB-C maps to a DisplayPort port, if present.
*g_port_names_usb_c[] = {"USB-C", "USBC", NULL},
**g_port_names[] = {
NULL,
(const char *[]) {"D-Sub", "DSub", "VGA", NULL},
(const char *[]) {"DVI", NULL},
(const char *[]) {"DisplayPort", "DP", NULL},
(const char *[]) {"HDMI", NULL},
};
// Monitors may or may not report these in their profile.
static const struct eizo_product {
const char *product;
const uint16_t *ports;
} g_products[] = {
{"CG243W", (const uint16_t[]) {DVI1, DVI2, DP1, 0}},
{"CG223W", (const uint16_t[]) {DVI1, DVI2, DP1, 0}},
{"CG245W", (const uint16_t[]) {DVI1, DVI2, DP1, 0}},
{"CG275W", (const uint16_t[]) {DVI1, DP1, DP2, 0}},
{"SX2462W", (const uint16_t[]) {DVI1, DVI2, DP1, 0}},
{"SX2262W", (const uint16_t[]) {DVI1, DVI2, DP1, 0}},
{"SX2762W", (const uint16_t[]) {DVI1, DP1, DP2, 0}},
{"S2232W", (const uint16_t[]) {DVI1, DSUB1, 0}},
{"S2432W", (const uint16_t[]) {DVI1, DSUB1, 0}},
{"S2242W", (const uint16_t[]) {DVI1, DSUB1, 0}},
{"S2233W", (const uint16_t[]) {DP1, DVI1, DSUB1, 0}},
{"S2433W", (const uint16_t[]) {DP1, DVI1, DSUB1, 0}},
{"S2243W", (const uint16_t[]) {DP1, DVI1, DSUB1, 0}},
{"EV2333W", (const uint16_t[]) {DP1, DVI1, DSUB1, 0}},
{"EV2334W", (const uint16_t[]) {HDMI1, DVI1, DSUB1, 0}},
{"EV2436W", (const uint16_t[]) {DP1, DVI1, DSUB1, 0}},
{"EV3237", (const uint16_t[]) {DVI1, DP1, DP2, HDMI1, 0}},
{"EV2450", (const uint16_t[]) {DSUB1, DVI1, DP1, HDMI1, 0}},
{"EV2455", (const uint16_t[]) {DSUB1, DVI1, DP1, HDMI1, 0}},
{"EV2750", (const uint16_t[]) {DVI1, DP1, HDMI1, 0}},
{"EV2451", (const uint16_t[]) {DSUB1, DVI1, DP1, HDMI1, 0}},
{"EV2456", (const uint16_t[]) {DSUB1, DVI1, DP1, HDMI1, 0}},
{"EV2457", (const uint16_t[]) {DVI1, DP1, HDMI1, 0}},
{"EV2480", (const uint16_t[]) {DP1, DP2, HDMI1, 0}},
{"EV2485", (const uint16_t[]) {DP1, DP2, HDMI1, 0}},
{"EV2490", (const uint16_t[]) {DP1, DP2, HDMI1, 0}},
{"EV2495", (const uint16_t[]) {DP1, DP2, HDMI1, 0}},
{"EV2780", (const uint16_t[]) {DP1, DP2, HDMI1, 0}},
{"EV2795", (const uint16_t[]) {DP1, DP2, HDMI1, 0}},
{"EV3895", (const uint16_t[]) {DP1, DP2, HDMI1, HDMI2, 0}},
{"CG3145", (const uint16_t[]) {DP1, DP2, HDMI1, HDMI2, 0}},
{"CG3146", (const uint16_t[]) {
0xa00, 0xa01, 0xa02, 0xa03, 0xa10, 0xa11, 0xa20, DP1, HDMI1, 0}},
{"EV2785", (const uint16_t[]) {DP1, DP2, HDMI1, HDMI2, 0}},
{"EV3285", (const uint16_t[]) {DP1, DP2, HDMI1, HDMI2, 0}},
{"CG319X", (const uint16_t[]) {DP1, DP2, HDMI1, HDMI2, 0}},
{"CG279X", (const uint16_t[]) {DVI1, DP1, DP2, HDMI1, 0}},
{"CG2700X", (const uint16_t[]) {DP1, DP2, HDMI1, 0}},
{"CG2700S", (const uint16_t[]) {DP1, DP2, HDMI1, 0}},
{"CS2731", (const uint16_t[]) {DVI1, DP1, DP2, HDMI1, 0}},
{"CS2410", (const uint16_t[]) {DVI1, DP1, HDMI1, 0}},
{"CS2740", (const uint16_t[]) {DP1, DP2, HDMI1, 0}},
{"EV2760", (const uint16_t[]) {DVI1, DP1, DP2, HDMI1, 0}},
{"EV2360", (const uint16_t[]) {DSUB1, DP1, HDMI1, 0}},
{"EV2460", (const uint16_t[]) {DSUB1, DVI1, DP1, HDMI1, 0}},
{"EV2781", (const uint16_t[]) {DP1, DP2, HDMI1, 0}},
{"EV2736W", (const uint16_t[]) {DP1, DVI1, 0}},
{}
};
static const uint16_t *
eizo_ports_by_product_name(const char *product)
{
for (size_t i = 0; g_products[i].product; i++)
if (!strcmp(g_products[i].product, product))
return g_products[i].ports;
return NULL;
}
// Match port names case-insensitively, with an optional one-based suffix.
static bool
eizo_port_by_name_in_group(const char *name, const char **group, uint8_t *index)
{
for (; *group; group++) {
size_t len = strlen(*group);
if (strncasecmp(*group, name, len))
continue;
if (!*(name += len))
return true;
char *end = NULL;
errno = 0;
long n = strtol(name, &end, 10);
if (errno || *end || n < 1 || n > 0x100)
return false;
*index = --n;
return true;
}
return false;
}
static uint16_t
eizo_port_by_name(const char *name)
{
char *end = NULL;
errno = 0;
long n = strtol(name, &end, 16);
if (!errno && !*end && n >= 0x100 && n <= UINT16_MAX)
return n;
uint8_t index = 0;
for (size_t i = 1; i < sizeof g_port_names / sizeof g_port_names[0]; i++)
if (eizo_port_by_name_in_group(name, g_port_names[i], &index))
return i * 0x100 | index;
return index;
}
static const char *
eizo_port_to_name(uint16_t port)
{
const char *stem = NULL;
uint16_t group = port >> 8, number = port & 0xff;
if (group && group < sizeof g_port_names / sizeof g_port_names[0])
stem = g_port_names[group][0];
static char buf[32] = "";
if (!stem)
snprintf(buf, sizeof buf, "%x", port);
else if (!number)
snprintf(buf, sizeof buf, "%s", stem);
else
snprintf(buf, sizeof buf, "%s %d", stem, (number + 1));
return buf;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
enum {
EIZO_USAGE_BRIGHTNESS = 0x00820010,
EIZO_USAGE_INPUT_PORT = 0xff010048,
EIZO_USAGE_RESTART = 0xff0200f4,
};
static bool
eizo_get_brightness(struct eizo_monitor *m, double *brightness)
{
const struct parser_report *subr =
eizo_monitor_subreport(m, EIZO_USAGE_BRIGHTNESS);
if (!subr)
return eizo_monitor_failf(m, "missing HID usage");
// NOTE: this oddly doesn't work when there's no signal.
uint8_t buf[2] = {};
if (!eizo_monitor_get(m, EIZO_USAGE_BRIGHTNESS, buf, sizeof buf))
return false;
*brightness = (double) peek_u16le(buf) / subr->logical_maximum;
return true;
}
static bool
eizo_set_brightness(struct eizo_monitor *m, double brightness)
{
const struct parser_report *subr =
eizo_monitor_subreport(m, EIZO_USAGE_BRIGHTNESS);
if (!subr)
return eizo_monitor_failf(m, "missing HID usage");
if (brightness < 0)
brightness = 0;
if (brightness > 1)
brightness = 1;
uint8_t buf[2] = {};
put_u16le(buf, subr->logical_maximum * brightness);
return eizo_monitor_set(m, EIZO_USAGE_BRIGHTNESS, buf, sizeof buf);
}
static bool
eizo_get_input_port(struct eizo_monitor *m, uint16_t *port)
{
const struct parser_report *subr =
eizo_monitor_subreport(m, EIZO_USAGE_INPUT_PORT);
if (!subr)
return eizo_monitor_failf(m, "missing HID usage");
uint8_t buf[2] = {};
if (!eizo_monitor_get(m, EIZO_USAGE_INPUT_PORT, buf, sizeof buf))
return false;
*port = peek_u16le(buf);
return true;
}
static void
eizo_get_input_ports(struct eizo_monitor *m, uint16_t *ports, size_t size)
{
struct eizo_profile_item *item = &m->profile[EIZO_PROFILE_KEY_INPUT_PORTS];
if (item->len) {
for (size_t i = 0; i < size && i < item->len / 4; i++)
ports[i] = peek_u16le(item->data + i * 4);
} else {
const uint16_t *db = eizo_ports_by_product_name(m->product);
for (size_t i = 0; i < size && db && db[i]; i++)
ports[i] = db[i];
}
}
static uint16_t
eizo_resolve_port_by_name(struct eizo_monitor *m, const char *port)
{
uint8_t usb_c_index = 0;
if (eizo_port_by_name_in_group(port, g_port_names_usb_c, &usb_c_index)) {
struct eizo_profile_item *item =
&m->profile[EIZO_PROFILE_KEY_USB_C_INPUT_PORTS];
if (item->len / 2 > usb_c_index)
return peek_u16le(item->data + usb_c_index * 2);
}
return eizo_port_by_name(port);
}
static const char *
eizo_resolve_port_to_name(struct eizo_monitor *m, uint16_t port)
{
// USB-C ports are a bit tricky, they only need to be /displayed/ as such.
struct eizo_profile_item *item =
&m->profile[EIZO_PROFILE_KEY_USB_C_INPUT_PORTS];
for (uint8_t i = 0; i < item->len / 2; i++) {
if (port != peek_u16le(item->data + i * 2))
continue;
static char buf[32] = "";
if (!i)
snprintf(buf, sizeof buf, "%s", g_port_names_usb_c[0]);
else
snprintf(buf, sizeof buf, "%s %u", g_port_names_usb_c[0], (i + 1));
return buf;
}
return eizo_port_to_name(port);
}
static bool
eizo_set_input_port(struct eizo_monitor *m, uint16_t port)
{
const struct parser_report *subr =
eizo_monitor_subreport(m, EIZO_USAGE_INPUT_PORT);
if (!subr)
return eizo_monitor_failf(m, "missing HID usage");
uint8_t buf[2] = {};
put_u16le(buf, port);
return eizo_monitor_set(m, EIZO_USAGE_INPUT_PORT, buf, sizeof buf);
}
static bool
eizo_restart(struct eizo_monitor *m)
{
const struct parser_report *subr =
eizo_monitor_subreport(m, EIZO_USAGE_RESTART);
if (!subr)
return eizo_monitor_failf(m, "missing HID usage");
uint8_t buf[1] = {};
return eizo_monitor_set(m, EIZO_USAGE_RESTART, buf, 1);
}
// --- Main --------------------------------------------------------------------
struct catbuf {
char buf[4096];
size_t len;
};
static const char *
catf(struct catbuf *b, const char *format, ...) ATTRIBUTE_PRINTF(2, 3);
static const char *
catf(struct catbuf *b, const char *format, ...)
{
va_list ap;
va_start(ap, format);
int result = vsnprintf(b->buf + b->len, sizeof b->buf - b->len, format, ap);
va_end(ap);
if (result >= 0) {
b->len += result;
if (b->len >= sizeof b->buf)
b->len = sizeof b->buf - 1;
}
return b->buf;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
typedef void (*print_fn)(const char *format, ...) ATTRIBUTE_PRINTF(1, 2);
static void print_dummy(const char *format, ...)
{
(void) format;
}
static bool
eizo_watch(struct eizo_monitor *m, print_fn output, print_fn error)