-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathi2c_hid_mediakeys.c
More file actions
727 lines (648 loc) · 21.1 KB
/
i2c_hid_mediakeys.c
File metadata and controls
727 lines (648 loc) · 21.1 KB
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
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "config.h"
#include "console.h"
#include "task.h"
#include "hooks.h"
#include "i2c.h"
#include "util.h"
#include "timer.h"
#include "chipset.h"
#include "hwtimer.h"
#include "math_util.h"
#include "util.h"
#include "i2c_hid.h"
#include "i2c_hid_mediakeys.h"
/* Chip specific */
#include "registers.h"
#define HID_SLAVE_CTRL 3
#define REPORT_ID_RADIO 0x01
#define REPORT_ID_CONSUMER 0x02
#define REPORT_ID_SENSOR 0x03
#define REPORT_ID_KEYBOARD_BACKLIGHT 0x05
#define ALS_REPORT_STOP 0x00
#define ALS_REPORT_POLLING 0x01
#define ALS_REPORT_THRES 0x02
/*
* See hid usage tables for consumer page
* https://www.usb.org/hid
*/
#define BUTTON_ID_BRIGHTNESS_INCREMENT 0x006F
#define BUTTON_ID_BRIGHTNESS_DECREMENT 0x0070
#define EVENT_HID_HOST_IRQ 0x8000
#define EVENT_REPORT_ILLUMINANCE_VALUE 0x4000
#define CPRINTS(format, args...) cprints(CC_KEYBOARD, format, ## args)
#define CPRINTF(format, args...) cprintf(CC_KEYBOARD, format, ## args)
static uint8_t key_states[HID_KEY_MAX];
static uint32_t update_key;
struct radio_report {
uint8_t state;
} __packed;
struct consumer_button_report {
uint16_t button_id;
} __packed;
struct als_input_report {
uint8_t sensor_state;
uint8_t event_type;
uint16_t illuminanceValue;
} __packed;
struct als_feature_report {
/* Common properties */
uint8_t connection_type;
uint8_t reporting_state;
uint8_t power_state;
uint8_t sensor_state;
uint32_t report_interval;
/* Properties specific to this sensor */
uint16_t sensitivity;
uint16_t maximum;
uint16_t minimum;
} __packed;
struct keyboard_backlight_report {
uint8_t level;
} __packed;
static struct radio_report radio_button;
static struct consumer_button_report consumer_button;
static struct als_input_report als_sensor;
static struct als_feature_report als_feature;
static struct keyboard_backlight_report keyboard_backlight;
int update_hid_key(enum media_key key, bool pressed)
{
if (key >= HID_KEY_MAX) {
return EC_ERROR_INVAL;
}
if (key == HID_KEY_AIRPLANE_MODE || key == HID_KEY_KEYBOARD_BACKLIGHT) {
key_states[key] = pressed;
if (pressed)
task_set_event(TASK_ID_HID, 1 << key, 0);
} else if (key_states[key] != pressed) {
key_states[key] = pressed;
task_set_event(TASK_ID_HID, 1 << key, 0);
}
return EC_SUCCESS;
}
void kblight_update_hid(uint8_t percent)
{
keyboard_backlight.level = percent;
update_hid_key(HID_KEY_KEYBOARD_BACKLIGHT, 1);
}
/* Called on AP S5 -> S3 transition */
static void hid_startup(void)
{
/*reset after lines go high*/
MCHP_I2C_CTRL(HID_SLAVE_CTRL) = BIT(7) |
BIT(6) |
BIT(3) |
BIT(0);
}
DECLARE_HOOK(HOOK_CHIPSET_STARTUP,
hid_startup,
HOOK_PRIO_DEFAULT);
/* HID input report descriptor
*
* For a complete reference, please see the following docs on usb.org
*
* 1. Device Class Definition for HID
* 2. HID Usage Tables
*/
static const uint8_t report_desc[] = {
/* Airplane Radio Collection */
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
0x09, 0x0C, /* USAGE (Wireless Radio Controls) */
0xA1, 0x01, /* COLLECTION (Application) */
0x85, REPORT_ID_RADIO, /* Report ID (Radio) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x09, 0xC6, /* USAGE (Wireless Radio Button) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x81, 0x06, /* INPUT (Data,Var,Rel) */
0x75, 0x07, /* REPORT_SIZE (7) */
0x81, 0x03, /* INPUT (Cnst,Var,Abs) */
0xC0, /* END_COLLECTION */
/* Consumer controls collection */
0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
0x09, 0x01, /* USAGE (Consumer Control) */
0xA1, 0x01, /* COLLECTION (Application) */
0x85, REPORT_ID_CONSUMER, /* Report ID (Consumer) */
0x15, 0x00, /* LOGICAL_MINIMUM (0x0) */
0x26, 0xFF, 0x03, /* LOGICAL_MAXIMUM (0x3FF) */
0x19, 0x00, /* Usage Minimum (0) */
0x2A, 0xFF, 0x03, /* Usage Maximum (0) */
0x75, 0x10, /* Report Size (16) */
0x95, 0x01, /* Report Count (1) */
0x81, 0x00, /* Input (Data,Arr,Abs) */
0xC0, /* END_COLLECTION */
/* Sensor controls collection */
0x05, 0x20, /* USAGE_PAGE (sensor) */
0x09, 0x41, /* USAGE ID (Light: Ambient Light) */
0xA1, 0x00, /* COLLECTION (Physical) */
0x85, REPORT_ID_SENSOR, /* Report ID (Sensor) */
0x05, 0x20, /* USAGE PAGE (Sensor) */
0x0A, 0x09, 0x03, /* USAGE ID (Property: Sensor Connection Type) */
0x15, 0x00, /* LOGICAL_MINIMUN (0x00) */
0x25, 0x02, /* LOGICAL_MAXIMUM (0x02) */
0x75, 0x08, /* Report Size (8) */
0x95, 0x01, /* Report Count (1) */
0xA1, 0x02, /* COLLECTION (logical) */
0x0A, 0x30, 0x08, /* Connection Type: PC Integrated */
0x0A, 0x31, 0x08, /* Connection Type: PC Attached */
0x0A, 0x32, 0x08, /* Connection Type: PC External */
0xB1, 0x00, /* Feature (Data,Arr,Abs) */
0xC0, /* END_COLLECTION */
0x0A, 0x16, 0x03, /* USAGE ID (Property: Reporting State) */
0x15, 0x00, /* LOGICAL_MINIMUN (0x00) */
0x25, 0x05, /* LOGICAL_MAXIMUM (0x05) */
0x75, 0x08, /* Report Size (8) */
0x95, 0x01, /* Report Count (1) */
0xA1, 0x02, /* COLLECTION (logical) */
0x0A, 0x40, 0x08, /* Reporting State: Report No Events */
0x0A, 0x41, 0x08, /* Reporting State: Report All Events */
0x0A, 0x42, 0x08, /* Reporting State: Report Threshold Events */
0x0A, 0x43, 0x08, /* Reporting State: Wake On No Events */
0x0A, 0x44, 0x08, /* Reporting State: Wake On All Events */
0x0A, 0x45, 0x08, /* Reporting State: Wake On Threshold Events */
0xB1, 0x00, /* Feature (Data,Arr,Abs) */
0xC0, /* END_COLLECTION */
0x0A, 0x19, 0x03, /* USAGE ID (Property: Power State Undefined Select) */
0x15, 0x00, /* LOGICAL_MINIMUN (0x00) */
0x25, 0x05, /* LOGICAL_MAXIMUM (0x05) */
0x75, 0x08, /* Report Size (8) */
0x95, 0x01, /* Report Count (1) */
0xA1, 0x02, /* COLLECTION (logical) */
0x0A, 0x50, 0x08, /* Power State: Undefined */
0x0A, 0x51, 0x08, /* Power State: D0 Full Power */
0x0A, 0x52, 0x08, /* Power State: D1 Low Power */
0x0A, 0x53, 0x08, /* Power State: D2 Standby Power with Wakeup */
0x0A, 0x54, 0x08, /* Power State: D3 sleep with Wakeup */
0x0A, 0x55, 0x08, /* Power State: D4 Power Off */
0xB1, 0x00, /* Feature (Data,Arr,Abs) */
0xC0, /* END_COLLECTION */
0x0A, 0x01, 0x02, /* USAGE ID (Event: Sensor State) */
0x15, 0x00, /* LOGICAL_MINIMUN (0x00) */
0x25, 0x06, /* LOGICAL_MAXIMUM (0x06) */
0x75, 0x08, /* Report Size (8) */
0x95, 0x01, /* Report Count (1) */
0xA1, 0x02, /* COLLECTION (logical) */
0x0A, 0x00, 0x08, /* Sensor State: Undefined */
0x0A, 0x01, 0x08, /* Sensor State: Ready */
0x0A, 0x02, 0x08, /* Sensor State: Not Available */
0x0A, 0x03, 0x08, /* Sensor State: No Data */
0x0A, 0x04, 0x08, /* Sensor State: Initializing */
0x0A, 0x05, 0x08, /* Sensor State: Access Denied */
0x0A, 0x06, 0x08, /* Sensor State: Error */
0xB1, 0x00, /* Feature (Data,Arr,Abs) */
0xC0, /* END_COLLECTION */
0x0A, 0x0E, 0x03, /* USAGE ID (Property: Report Interval) */
0x15, 0x00, /* LOGICAL_MINIMUN (0x00) */
0x27, 0xFF, 0xFF, 0xFF, 0XFF, /* LOGICAL_MAXIMUM (0xFFFFFFFF) */
0x75, 0x20, /* Report Size (32) */
0x95, 0x01, /* Report Count (1) */
0x55, 0x00, /* UNIT EXPONENT (0x00) */
0xB1, 0x02, /* Feature (Data,Var,Abs) */
0x0A, 0xD1, 0xE4, /* USAGE ID (Modified Change Sensitivity Percent of Range) */
0x15, 0x00, /* LOGICAL_MINIMUN (0x00) */
0x26, 0x10, 0x27, /* LOGICAL_MAXIMUM (0x2710) */
0x75, 0x10, /* Report Size (16) */
0x95, 0x01, /* Report Count (1) */
0x55, 0x0E, /* UNIT EXPONENT (0x0E) */
0xB1, 0x02, /* Feature (Data,Var,Abs) */
0x0A, 0xD1, 0x24, /* USAGE ID (Modified Maximum) */
0x15, 0x00, /* LOGICAL_MINIMUN (0x00) */
0x26, 0xFF, 0xFF, /* LOGICAL_MAXIMUM (0xFFFF) */
0x75, 0x10, /* Report Size (16) */
0x95, 0x01, /* Report Count (1) */
0x55, 0x00, /* UNIT EXPONENT (0x00) */
0xB1, 0x02, /* Feature (Data,Var,Abs) */
0x0A, 0xD1, 0x34, /* USAGE ID (Modified Minimum) */
0x15, 0x00, /* LOGICAL_MINIMUN (0x00) */
0x26, 0xFF, 0xFF, /* LOGICAL_MAXIMUM (0xFFFF) */
0x75, 0x10, /* Report Size (16) */
0x95, 0x01, /* Report Count (1) */
0x55, 0x00, /* UNIT EXPONENT (0x00) */
0xB1, 0x02, /* Feature (Data,Var,Abs) */
0x05, 0x20, /* USAGE PAGE (Sensor) */
0x0A, 0x01, 0x02, /* USAGE ID (Event: Sensor State) */
0x15, 0x00, /* LOGICAL_MINIMUN (0x00) */
0x25, 0x06, /* LOGICAL_MAXIMUM (0x06) */
0x75, 0x08, /* Report Size (8) */
0x95, 0x01, /* Report Count (1) */
0xA1, 0x02, /* COLLECTION (logical) */
0x0A, 0x00, 0x08, /* Sensor State: Undefined */
0x0A, 0x01, 0x08, /* Sensor State: Ready */
0x0A, 0x02, 0x08, /* Sensor State: Not Available */
0x0A, 0x03, 0x08, /* Sensor State: No Data */
0x0A, 0x04, 0x08, /* Sensor State: Initializing */
0x0A, 0x05, 0x08, /* Sensor State: Access Denied */
0x0A, 0x06, 0x08, /* Sensor State: Error */
0x81, 0x00, /* Input (Data,Arr,Abs) */
0xC0, /* END_COLLECTION */
0x0A, 0x02, 0x02, /* USAGE (Sensor event) */
0x15, 0x00, /* LOGICAL_MINIMUN (0x00) */
0x25, 0x05, /* LOGICAL_MAXIMUM (0x05) */
0x75, 0x08, /* Report Size (8) */
0x95, 0x01, /* Report Count (1) */
0xA1, 0x02, /* COLLECTION (logical) */
0x0A, 0x10, 0x08, /* Sensor Event: Unknown */
0x0A, 0x11, 0x08, /* Sensor Event: State Changed */
0x0A, 0x12, 0x08, /* Sensor Event: Property Changed */
0x0A, 0x13, 0x08, /* Sensor Event: Data Updated */
0x0A, 0x14, 0x08, /* Sensor Event: Poll Response */
0x0A, 0x15, 0x08, /* Sensor Event: Change Sensitivity */
0x81, 0x00, /* Input (Data,Arr,Abs) */
0xC0, /* END_COLLECTION */
0x0A, 0xD1, 0x04, /* USAGE (Data Field: Illuminance) */
0x15, 0x00, /* LOGICAL_MINIMUN (0x00) */
0x26, 0xFF, 0xFF, /* LOGICAL_MAXIMUM (0XFFFF) */
0x55, 0x00, /* UNIT EXPONENT (0x00) */
0x75, 0x10, /* Report Size (16) */
0x95, 0x01, /* Report Count (1) */
0x81, 0x02, /* Input (Data,Arr,Abs) */
0xC0, /* END_COLLECTION */
/* Keyboard Backlight Level Collection */
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
0x09, 0x06, /* USAGE (Keyboard) */
0xA1, 0x01, /* COLLECTION (Application) */
0x85, REPORT_ID_KEYBOARD_BACKLIGHT, /* Report ID (Keyboard Backlight) */
0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
0x09, 0x7B, /* USAGE (Keyboard Backlight Set Level) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x64, /* LOGICAL_MAXIMUM (100) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x81, 0x02, /* INPUT (Data,Var,Abs) */
0xC0,
};
static struct i2c_hid_descriptor hid_desc = {
.wHIDDescLength = I2C_HID_DESC_LENGTH,
.bcdVersion = I2C_HID_BCD_VERSION,
.wReportDescLength = sizeof(report_desc),
.wReportDescRegister = I2C_HID_REPORT_DESC_REGISTER,
.wInputRegister = I2C_HID_INPUT_REPORT_REGISTER,
.wMaxInputLength = I2C_HID_HEADER_SIZE +
sizeof(struct als_input_report), /*Note if there are multiple reports this has to be max*/
.wOutputRegister = 0,
.wMaxOutputLength = 0,
.wCommandRegister = I2C_HID_COMMAND_REGISTER,
.wDataRegister = I2C_HID_DATA_REGISTER,
.wVendorID = I2C_HID_MEDIAKEYS_VENDOR_ID,
.wProductID = I2C_HID_MEDIAKEYS_PRODUCT_ID,
.wVersionID = I2C_HID_MEDIAKEYS_FW_VERSION,
};
/*
* In I2C HID, the host would request for an input report immediately following
* the protocol initialization. The device is required to respond with exactly
* 2 empty bytes. Furthermore, some hosts may use a single byte SMBUS read to
* check if the device exists on the specified I2C address.
*
* These variables record if such probing/initialization have been done before.
*/
static bool pending_probe;
static bool pending_reset;
/* Current active report buffer index */
static int report_active_index;
/* Current input mode */
static uint8_t input_mode;
void i2c_hid_mediakeys_init(void)
{
input_mode = 0;
report_active_index = 0;
/* Respond probing requests for now. */
pending_probe = false;
pending_reset = false;
}
void i2c_hid_als_init(void)
{
als_feature.connection_type = HID_INTEGRATED;
als_feature.reporting_state = HID_ALL_EVENTS;
als_feature.power_state = HID_D0_FULL_POWER;
als_feature.sensor_state = HID_READY;
als_feature.report_interval = 100;
als_feature.sensitivity = HID_ALS_SENSITIVITY;
als_feature.maximum = HID_ALS_MAX;
als_feature.minimum = HID_ALS_MIN;
als_sensor.event_type = 0x04; /* HID_DATA_UPDATED */
als_sensor.sensor_state = 0x02; /* HID READY */
als_sensor.illuminanceValue = 0x0000;
}
static int als_polling_mode_count;
void report_illuminance_value(void)
{
uint16_t newIlluminaceValue = *(uint16_t *)host_get_memmap(EC_MEMMAP_ALS);
static int granularity;
/* We need to polling the ALS value at least 6 seconds */
if (als_polling_mode_count <= 60) {
als_polling_mode_count++; /* time base 100ms */
/* bypass the EC_MEMMAP_ALS value to input report */
als_sensor.illuminanceValue = newIlluminaceValue;
task_set_event(TASK_ID_HID, ((1 << HID_ALS_REPORT_LUX) |
EVENT_REPORT_ILLUMINANCE_VALUE), 0);
} else {
if (ABS(als_sensor.illuminanceValue - newIlluminaceValue) > granularity) {
als_sensor.illuminanceValue = newIlluminaceValue;
task_set_event(TASK_ID_HID, ((1 << HID_ALS_REPORT_LUX) |
EVENT_REPORT_ILLUMINANCE_VALUE), 0);
} else {
task_set_event(TASK_ID_HID, EVENT_REPORT_ILLUMINANCE_VALUE, 0);
}
}
/**
* To ensure the best experience the ALS should have a granularity of
* at most 1 lux when the ambient light is below 25 lux and a granularity
* of at most 4% of the ambient light when it is above 25 lux.
* This enable the adaptive brightness algorithm to perform smooth screen
* brightness transitions.
*/
if (newIlluminaceValue < 25)
granularity = 1;
else
granularity = newIlluminaceValue*4/100;
}
DECLARE_DEFERRED(report_illuminance_value);
static void i2c_hid_send_response(void)
{
task_set_event(TASK_ID_HID, EVENT_HID_HOST_IRQ, 0);
}
static void als_report_control(uint8_t report_mode)
{
if (report_mode == 0x01) {
/* als report mode = polling */
hook_call_deferred(&report_illuminance_value_data,
((int) als_feature.report_interval) * MSEC);
} else if (report_mode == 0x02) {
/* als report mode = threshold */
;
} else {
/* stop report als value */
hook_call_deferred(&report_illuminance_value_data, -1);
als_polling_mode_count = 0;
}
}
static size_t fill_report(uint8_t *buffer, uint8_t report_id, const void *data,
size_t data_len)
{
size_t response_len = I2C_HID_HEADER_SIZE + data_len;
buffer[0] = response_len & 0xFF;
buffer[1] = (response_len >> 8) & 0xFF;
buffer[2] = report_id;
memcpy(buffer + I2C_HID_HEADER_SIZE, data, data_len);
return response_len;
}
static void extract_report(size_t len, const uint8_t *buffer, void *data,
size_t data_len)
{
if (len != 9 + data_len) {
CPRINTS("I2C-HID: SET_REPORT buffer length mismatch");
return;
}
memcpy(data, buffer + 9, data_len);
}
static int i2c_hid_touchpad_command_process(size_t len, uint8_t *buffer)
{
uint8_t command = buffer[3] & 0x0F;
uint8_t power_state = buffer[2] & 0x03;
uint8_t report_id = buffer[2] & 0x0F;
uint8_t report_type = (buffer[2] & 0x30) >> 4;
size_t response_len = 0;
switch (command) {
case I2C_HID_CMD_RESET:
i2c_hid_mediakeys_init();
/* Wait for the 2-bytes I2C read following the protocol reset. */
pending_probe = false;
pending_reset = true;
input_mode = REPORT_ID_RADIO;
i2c_hid_send_response();
break;
case I2C_HID_CMD_GET_REPORT:
switch (report_id) {
case REPORT_ID_RADIO:
response_len =
fill_report(buffer, report_id,
&radio_button,
sizeof(struct radio_report));
break;
case REPORT_ID_CONSUMER:
response_len =
fill_report(buffer, report_id,
&consumer_button,
sizeof(struct consumer_button_report));
break;
case REPORT_ID_SENSOR:
if (report_type == 0x01) {
response_len =
fill_report(buffer, report_id,
&als_sensor,
sizeof(struct als_input_report));
} else if (report_type == 0x03) {
response_len =
fill_report(buffer, report_id,
&als_feature,
sizeof(struct als_feature_report));
}
break;
case REPORT_ID_KEYBOARD_BACKLIGHT:
response_len =
fill_report(buffer, report_id,
&keyboard_backlight,
sizeof(struct keyboard_backlight_report));
break;
default:
response_len = 2;
buffer[0] = response_len;
buffer[1] = 0;
break;
}
break;
case I2C_HID_CMD_SET_REPORT:
switch (report_id) {
case REPORT_ID_SENSOR:
extract_report(len, buffer, &als_feature, sizeof(struct als_feature_report));
break;
default:
break;
}
break;
case I2C_HID_CMD_SET_POWER:
/*
* Return the power setting so the user can actually set the
* touch controller's power state in board level.
*/
*buffer = power_state;
response_len = 1;
if (power_state == 0x00) {
i2c_hid_als_init();
als_report_control(ALS_REPORT_POLLING);
} else
als_report_control(ALS_REPORT_STOP);
break;
default:
return 0;
}
return response_len;
}
int i2c_hid_process(unsigned int len, uint8_t *buffer)
{
size_t response_len = 0;
int reg;
if (len == 0)
reg = I2C_HID_INPUT_REPORT_REGISTER;
else
reg = UINT16_FROM_BYTE_ARRAY_LE(buffer, 0);
switch (reg) {
case I2C_HID_MEDIAKEYS_HID_DESC_REGISTER:
memcpy(buffer, &hid_desc, sizeof(hid_desc));
response_len = sizeof(hid_desc);
break;
case I2C_HID_REPORT_DESC_REGISTER:
memcpy(buffer, report_desc, sizeof(report_desc));
response_len = sizeof(report_desc);
break;
case I2C_HID_INPUT_REPORT_REGISTER:
/* Single-byte read probing. */
if (pending_probe) {
buffer[0] = 0;
response_len = 1;
break;
}
/* Reset protocol: 2 empty bytes. */
if (pending_reset) {
pending_reset = false;
memset(buffer, 0, hid_desc.wMaxInputLength);
response_len = hid_desc.wMaxInputLength;
break;
}
/* Common input report requests. */
if (input_mode == REPORT_ID_RADIO) {
response_len =
fill_report(buffer, REPORT_ID_RADIO,
&radio_button,
sizeof(struct radio_report));
} else if (input_mode == REPORT_ID_CONSUMER) {
response_len =
fill_report(buffer, REPORT_ID_CONSUMER,
&consumer_button,
sizeof(struct consumer_button_report));
} else if (input_mode == REPORT_ID_SENSOR) {
response_len =
fill_report(buffer, REPORT_ID_SENSOR,
&als_sensor,
sizeof(struct als_input_report));
} else if (input_mode == REPORT_ID_KEYBOARD_BACKLIGHT) {
response_len =
fill_report(buffer, REPORT_ID_KEYBOARD_BACKLIGHT,
&keyboard_backlight,
sizeof(struct keyboard_backlight_report));
};
break;
case I2C_HID_COMMAND_REGISTER:
response_len = i2c_hid_touchpad_command_process(len, buffer);
break;
default:
/* Unknown register has been received. */
return 0;
}
return response_len;
}
/*I2C Write from master */
void i2c_data_received(int port, uint8_t *buf, int len)
{
i2c_hid_process(len, buf);
task_set_event(TASK_ID_HID, TASK_EVENT_I2C_IDLE, 0);
}
/* I2C Read from master */
/* CTS I2C protocol implementation */
int i2c_set_response(int port, uint8_t *buf, int len)
{
int ret = 0;
ret = i2c_hid_process(len, buf);
gpio_set_level(GPIO_SOC_EC_INT_L, 1);
task_set_event(TASK_ID_HID, TASK_EVENT_I2C_IDLE, 0);
return ret;
}
void hid_irq_to_host(void)
{
uint32_t i2c_evt;
int timeout = 0;
gpio_set_level(GPIO_SOC_EC_INT_L, 0);
/* wait for host to perform i2c transaction or timeout
* this happens in an interrupt context, so the interrupt will handle
* the data request and ack a task event signifying we are done.
*/
i2c_evt = task_wait_event_mask(TASK_EVENT_I2C_IDLE, 100*MSEC);
if (i2c_evt & TASK_EVENT_TIMER) {
CPRINTS("I2CHID no host response");
} else {
/*CPRINTS("I2CHID host handled response");*/
}
/* wait for bus to be not busy */
while (((MCHP_I2C_STATUS(HID_SLAVE_CTRL) & BIT(0)) == 0) && ++timeout < 1000) {
usleep(10);
}
/*Deassert interrupt */
gpio_set_level(GPIO_SOC_EC_INT_L, 1);
usleep(10);
}
void hid_handler_task(void *p)
{
uint32_t event;
size_t i;
i2c_hid_mediakeys_init();
while (1) {
event = task_wait_event(-1);
if (event & TASK_EVENT_I2C_IDLE) {
/* TODO host is requesting data from device */
}
if (event & EVENT_HID_HOST_IRQ) {
hid_irq_to_host();
}
if (event & EVENT_REPORT_ILLUMINANCE_VALUE) {
/* start reporting illuminance value in S0*/
hook_call_deferred(&report_illuminance_value_data,
((int) als_feature.report_interval) * MSEC);
}
if (event & 0xFFFF) {
for (i = 0; i < 15; i++) {
/**
* filter EVENT_REPORT_ILLUMINANCE_VALUE
* this event only call deferred
*/
if ((event & 0xBFFF) & (1<<i)) {
update_key = i;
switch (i) {
case HID_KEY_DISPLAY_BRIGHTNESS_UP:
input_mode = REPORT_ID_CONSUMER;
if (key_states[i]) {
consumer_button.button_id = BUTTON_ID_BRIGHTNESS_INCREMENT;
} else {
consumer_button.button_id = 0;
}
break;
case HID_KEY_DISPLAY_BRIGHTNESS_DN:
input_mode = REPORT_ID_CONSUMER;
if (key_states[i]) {
consumer_button.button_id = BUTTON_ID_BRIGHTNESS_DECREMENT;
} else {
consumer_button.button_id = 0;
}
break;
case HID_KEY_AIRPLANE_MODE:
input_mode = REPORT_ID_RADIO;
radio_button.state = key_states[i] ? 1 : 0;
break;
case HID_KEY_KEYBOARD_BACKLIGHT:
input_mode = REPORT_ID_KEYBOARD_BACKLIGHT;
break;
case HID_ALS_REPORT_LUX:
input_mode = REPORT_ID_SENSOR;
break;
}
/* we don't need to assert the interrupt when system state in S0ix */
if (chipset_in_state(CHIPSET_STATE_ON))
hid_irq_to_host();
}
}
}
}
};