-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathi2c_hid_mediakeys.c
More file actions
480 lines (431 loc) · 12.2 KB
/
i2c_hid_mediakeys.c
File metadata and controls
480 lines (431 loc) · 12.2 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
/* 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 "hwtimer.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_SYSTEM 0x04
/*
* 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 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 system_report {
uint8_t state;
} __packed;
enum system_buttons: uint8_t {
SYSTEM_KEY_FN = BIT(0),
SYSTEM_KEY_FN_LOCK = BIT(1),
SYSTEM_FN_LOCK_INDICATOR = BIT(2),
SYSTEM_KEY_DISPLAY_TOGGLE = BIT(3),
};
static struct radio_report radio_button;
static struct consumer_button_report consumer_button;
static struct system_report system_buttons;
int update_hid_key(enum media_key key, bool pressed)
{
if (key >= HID_KEY_MAX) {
return EC_ERROR_INVAL;
}
if (key_states[key] != pressed) {
key_states[key] = pressed;
task_set_event(TASK_ID_HID, 1 << key, 0);
}
return EC_SUCCESS;
}
/* 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 */
/* System Control Collection */
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
0x09, 0x80, /* USAGE (System Control) */
0xA1, 0x01, /* COLLECTION (Application) */
0x85, REPORT_ID_SYSTEM, /* Report ID (System) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x09, 0x97, /* USAGE (System Function Shift) */
0x81, 0x02, /* INPUT (Data,Var,Abs) */
0x09, 0x98, /* USAGE (System Function Shift Lock) */
0x81, 0x06, /* INPUT (Data,Var,Rel) */
0x09, 0x99, /* USAGE (System Function Shift Lock Indicator) */
0x81, 0x02, /* INPUT (Data,Var,Abs) */
0x09, 0xB5, /* USAGE (System Display Toggle Int/Ext Mode) */
0x81, 0x06, /* INPUT (Data,Var,Rel) */
0x75, 0x04, /* REPORT_SIZE (4) */
0x81, 0x03, /* INPUT (Cnst,Var,Abs) */
0xC0, /* END_COLLECTION */
};
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 consumer_button_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;
}
static void i2c_hid_send_response(void)
{
task_set_event(TASK_ID_HID, 0x8000, 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 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;
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:
CPRINTF("HID RPT");
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_SYSTEM:
response_len =
fill_report(buffer, report_id,
&system_buttons,
sizeof(struct system_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_INPUT_MODE:
extract_report(len, buffer, &input_mode,
sizeof(input_mode));
break;
case REPORT_ID_REPORTING:
extract_report(len, buffer, &reporting,
sizeof(reporting));
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;
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;
buffer[0] = 0;
buffer[1] = 0;
response_len = 2;
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_SYSTEM) {
response_len =
fill_report(buffer, REPORT_ID_SYSTEM,
&system_buttons,
sizeof(struct system_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 & 0x8000) {
hid_irq_to_host();
}
if (event & 0xFFFF) {
for (i = 0; i < 15; i++) {
if (event & (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_FN:
input_mode = REPORT_ID_SYSTEM;
if (key_states[i]) {
system_buttons.state |= SYSTEM_KEY_FN;
} else {
system_buttons.state &= ~SYSTEM_KEY_FN;
}
break;
case HID_KEY_FN_LOCK:
input_mode = REPORT_ID_SYSTEM;
if (key_states[i]) {
system_buttons.state |= SYSTEM_KEY_FN_LOCK;
} else {
system_buttons.state &= ~SYSTEM_KEY_FN_LOCK;
}
break;
case HID_FN_LOCK_INDICATOR:
input_mode = REPORT_ID_SYSTEM;
if (key_states[i]) {
system_buttons.state |= SYSTEM_FN_LOCK_INDICATOR;
} else {
system_buttons.state &= ~SYSTEM_FN_LOCK_INDICATOR;
}
break;
case HID_KEY_DISPLAY_TOGGLE:
input_mode = REPORT_ID_SYSTEM;
if (key_states[i]) {
system_buttons.state |= SYSTEM_KEY_DISPLAY_TOGGLE;
} else {
system_buttons.state &= ~SYSTEM_KEY_DISPLAY_TOGGLE;
}
break;
}
hid_irq_to_host();
}
}
}
}
};