1 /*
2 * Copyright (c) 2014, 2016-2017, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #define DEBUG 0
31 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
32
33 #include <errno.h>
34 #include <fcntl.h>
35
36 #include <cstdlib>
37
38 #include <log/log.h>
39 #include <utils/Trace.h>
40
41 #include <hardware/hdmi_cec.h>
42 #include "qhdmi_cec.h"
43 #include "QHDMIClient.h"
44
45 namespace qhdmicec {
46
47 const int NUM_HDMI_PORTS = 1;
48 const int MAX_SYSFS_DATA = 128;
49 const int MAX_CEC_FRAME_SIZE = 20;
50 const int MAX_SEND_MESSAGE_RETRIES = 1;
51
52 enum {
53 LOGICAL_ADDRESS_SET = 1,
54 LOGICAL_ADDRESS_UNSET = -1,
55 };
56
57 // Offsets of members of struct hdmi_cec_msg
58 // drivers/video/msm/mdss/mdss_hdmi_cec.c
59 // XXX: Get this from a driver header
60 enum {
61 CEC_OFFSET_SENDER_ID,
62 CEC_OFFSET_RECEIVER_ID,
63 CEC_OFFSET_OPCODE,
64 CEC_OFFSET_OPERAND,
65 CEC_OFFSET_FRAME_LENGTH = 17,
66 CEC_OFFSET_RETRANSMIT,
67 };
68
69 //Forward declarations
70 static void cec_close_context(cec_context_t* ctx __unused);
71 static int cec_enable(cec_context_t *ctx, int enable);
72 static int cec_is_connected(const struct hdmi_cec_device* dev, int port_id);
73
read_node(const char * path,char * data)74 static ssize_t read_node(const char *path, char *data)
75 {
76 ssize_t err = 0;
77 FILE *fp = NULL;
78 err = access(path, R_OK);
79 if (!err) {
80 fp = fopen(path, "r");
81 if (fp) {
82 err = fread(data, sizeof(char), MAX_SYSFS_DATA ,fp);
83 fclose(fp);
84 }
85 }
86 return err;
87 }
88
write_node(const char * path,const char * data,size_t len)89 static ssize_t write_node(const char *path, const char *data, size_t len)
90 {
91 ssize_t err = 0;
92 int fd = -1;
93 err = access(path, W_OK);
94 if (!err) {
95 fd = open(path, O_WRONLY);
96 errno = 0;
97 err = write(fd, data, len);
98 if (err < 0) {
99 err = -errno;
100 }
101 close(fd);
102 } else {
103 ALOGE("%s: Failed to access path: %s error: %s",
104 __FUNCTION__, path, strerror(errno));
105 err = -errno;
106 }
107 return err;
108 }
109
110 // Helper function to write integer values to the full sysfs path
write_int_to_node(cec_context_t * ctx,const char * path_postfix,const int value)111 static ssize_t write_int_to_node(cec_context_t *ctx,
112 const char *path_postfix,
113 const int value)
114 {
115 char sysfs_full_path[MAX_PATH_LENGTH];
116 char sysfs_data[MAX_SYSFS_DATA];
117 snprintf(sysfs_data, sizeof(sysfs_data), "%d",value);
118 snprintf(sysfs_full_path,sizeof(sysfs_full_path), "%s/%s",
119 ctx->fb_sysfs_path, path_postfix);
120 ssize_t err = write_node(sysfs_full_path, sysfs_data, strlen(sysfs_data));
121 return err;
122 }
123
hex_to_string(const char * msg,ssize_t len,char * str)124 static void hex_to_string(const char *msg, ssize_t len, char *str)
125 {
126 //Functions assumes sufficient memory in str
127 char *ptr = str;
128 for(int i=0; i < len ; i++) {
129 ptr += snprintf(ptr, 3, "%02X", msg[i]);
130 // Overwrite null termination of snprintf in all except the last byte
131 if (i < len - 1)
132 *ptr = ':';
133 ptr++;
134 }
135 }
136
cec_get_fb_node_number(cec_context_t * ctx)137 static ssize_t cec_get_fb_node_number(cec_context_t *ctx)
138 {
139 //XXX: Do this from a common utility library across the display HALs
140 const int MAX_FB_DEVICES = 2;
141 ssize_t len = 0;
142 char fb_type_path[MAX_PATH_LENGTH];
143 char fb_type[MAX_SYSFS_DATA];
144 const char *dtv_panel_str = "dtv panel";
145
146 for(int num = 0; num < MAX_FB_DEVICES; num++) {
147 snprintf(fb_type_path, sizeof(fb_type_path),"%s%d/msm_fb_type",
148 SYSFS_BASE,num);
149 ALOGD_IF(DEBUG, "%s: num: %d fb_type_path: %s", __FUNCTION__, num, fb_type_path);
150 len = read_node(fb_type_path, fb_type);
151 ALOGD_IF(DEBUG, "%s: fb_type:%s", __FUNCTION__, fb_type);
152 if(len > 0 && (strncmp(fb_type, dtv_panel_str, strlen(dtv_panel_str)) == 0)){
153 ALOGD_IF(DEBUG, "%s: Found DTV panel at fb%d", __FUNCTION__, num);
154 ctx->fb_num = num;
155 snprintf(ctx->fb_sysfs_path, sizeof(ctx->fb_sysfs_path),
156 "%s%d", SYSFS_BASE, num);
157 break;
158 }
159 }
160 if (len < 0)
161 return len;
162 else
163 return 0;
164 }
165
cec_add_logical_address(const struct hdmi_cec_device * dev,cec_logical_address_t addr)166 static int cec_add_logical_address(const struct hdmi_cec_device* dev,
167 cec_logical_address_t addr)
168 {
169 if (addr < CEC_ADDR_TV || addr > CEC_ADDR_BROADCAST) {
170 ALOGE("%s: Received invalid address: %d ", __FUNCTION__, addr);
171 return -EINVAL;
172 }
173 cec_context_t* ctx = (cec_context_t*)(dev);
174 ctx->logical_address[addr] = LOGICAL_ADDRESS_SET;
175
176 //XXX: We can get multiple logical addresses here but we can only send one
177 //to the driver. Store locally for now
178 ssize_t err = write_int_to_node(ctx, "cec/logical_addr", addr);
179 ALOGI("%s: Allocated logical address: %d ", __FUNCTION__, addr);
180 return (int) err;
181 }
182
cec_clear_logical_address(const struct hdmi_cec_device * dev)183 static void cec_clear_logical_address(const struct hdmi_cec_device* dev)
184 {
185 cec_context_t* ctx = (cec_context_t*)(dev);
186 memset(ctx->logical_address, LOGICAL_ADDRESS_UNSET,
187 sizeof(ctx->logical_address));
188 //XXX: Find logical_addr that needs to be reset
189 write_int_to_node(ctx, "cec/logical_addr", 15);
190 ALOGD_IF(DEBUG, "%s: Cleared logical addresses", __FUNCTION__);
191 }
192
cec_get_physical_address(const struct hdmi_cec_device * dev,uint16_t * addr)193 static int cec_get_physical_address(const struct hdmi_cec_device* dev,
194 uint16_t* addr)
195 {
196 cec_context_t* ctx = (cec_context_t*)(dev);
197 char pa_path[MAX_PATH_LENGTH];
198 char pa_data[MAX_SYSFS_DATA];
199 snprintf (pa_path, sizeof(pa_path),"%s/pa",
200 ctx->fb_sysfs_path);
201 int err = (int) read_node(pa_path, pa_data);
202 *addr = (uint16_t) atoi(pa_data);
203 ALOGD_IF(DEBUG, "%s: Physical Address: 0x%x", __FUNCTION__, *addr);
204 if (err < 0)
205 return err;
206 else
207 return 0;
208 }
209
cec_send_message(const struct hdmi_cec_device * dev,const cec_message_t * msg)210 static int cec_send_message(const struct hdmi_cec_device* dev,
211 const cec_message_t* msg)
212 {
213 ATRACE_CALL();
214 if(cec_is_connected(dev, 0) <= 0)
215 return HDMI_RESULT_FAIL;
216
217 cec_context_t* ctx = (cec_context_t*)(dev);
218 ALOGD_IF(DEBUG, "%s: initiator: %d destination: %d length: %u",
219 __FUNCTION__, msg->initiator, msg->destination,
220 (uint32_t) msg->length);
221
222 // Dump message received from framework
223 char dump[128];
224 if(msg->length > 0) {
225 hex_to_string((char*)msg->body, msg->length, dump);
226 ALOGD_IF(DEBUG, "%s: message from framework: %s", __FUNCTION__, dump);
227 }
228
229 char write_msg_path[MAX_PATH_LENGTH];
230 char write_msg[MAX_CEC_FRAME_SIZE];
231 memset(write_msg, 0, sizeof(write_msg));
232 // See definition of struct hdmi_cec_msg in driver code
233 // drivers/video/msm/mdss/mdss_hdmi_cec.c
234 // Write header block
235 // XXX: Include this from header in kernel
236 write_msg[CEC_OFFSET_SENDER_ID] = msg->initiator;
237 write_msg[CEC_OFFSET_RECEIVER_ID] = msg->destination;
238 //Kernel splits opcode/operand, but Android sends it in one byte array
239 write_msg[CEC_OFFSET_OPCODE] = msg->body[0];
240 if(msg->length > 1) {
241 memcpy(&write_msg[CEC_OFFSET_OPERAND], &msg->body[1],
242 sizeof(char)*(msg->length - 1));
243 }
244 //msg length + initiator + destination
245 write_msg[CEC_OFFSET_FRAME_LENGTH] = (unsigned char) (msg->length + 1);
246 hex_to_string(write_msg, sizeof(write_msg), dump);
247 ALOGD_IF(DEBUG, "%s: message to driver: %s", __FUNCTION__, dump);
248 snprintf(write_msg_path, sizeof(write_msg_path), "%s/cec/wr_msg",
249 ctx->fb_sysfs_path);
250 int retry_count = 0;
251 ssize_t err = 0;
252 //HAL spec requires us to retry at least once.
253 while (true) {
254 err = write_node(write_msg_path, write_msg, sizeof(write_msg));
255 retry_count++;
256 if (err == -EAGAIN && retry_count <= MAX_SEND_MESSAGE_RETRIES) {
257 ALOGE("%s: CEC line busy, retrying", __FUNCTION__);
258 } else {
259 break;
260 }
261 }
262
263 if (err < 0) {
264 if (err == -ENXIO) {
265 ALOGI("%s: No device exists with the destination address",
266 __FUNCTION__);
267 return HDMI_RESULT_NACK;
268 } else if (err == -EAGAIN) {
269 ALOGE("%s: CEC line is busy, max retry count exceeded",
270 __FUNCTION__);
271 return HDMI_RESULT_BUSY;
272 } else {
273 return HDMI_RESULT_FAIL;
274 ALOGE("%s: Failed to send CEC message err: %zd - %s",
275 __FUNCTION__, err, strerror(int(-err)));
276 }
277 } else {
278 ALOGD_IF(DEBUG, "%s: Sent CEC message - %zd bytes written",
279 __FUNCTION__, err);
280 return HDMI_RESULT_SUCCESS;
281 }
282 }
283
cec_receive_message(cec_context_t * ctx,char * msg,ssize_t len)284 void cec_receive_message(cec_context_t *ctx, char *msg, ssize_t len)
285 {
286 if(!ctx->system_control)
287 return;
288
289 char dump[128];
290 if(len > 0) {
291 hex_to_string(msg, len, dump);
292 ALOGD_IF(DEBUG, "%s: Message from driver: %s", __FUNCTION__, dump);
293 }
294
295 hdmi_event_t event;
296 event.type = HDMI_EVENT_CEC_MESSAGE;
297 event.dev = (hdmi_cec_device *) ctx;
298 // Remove initiator/destination from this calculation
299 event.cec.length = msg[CEC_OFFSET_FRAME_LENGTH] - 1;
300 event.cec.initiator = (cec_logical_address_t) msg[CEC_OFFSET_SENDER_ID];
301 event.cec.destination = (cec_logical_address_t) msg[CEC_OFFSET_RECEIVER_ID];
302 //Copy opcode and operand
303 size_t copy_size = event.cec.length > sizeof(event.cec.body) ?
304 sizeof(event.cec.body) : event.cec.length;
305 memcpy(event.cec.body, &msg[CEC_OFFSET_OPCODE],copy_size);
306 hex_to_string((char *) event.cec.body, copy_size, dump);
307 ALOGD_IF(DEBUG, "%s: Message to framework: %s", __FUNCTION__, dump);
308 ctx->callback.callback_func(&event, ctx->callback.callback_arg);
309 }
310
cec_hdmi_hotplug(cec_context_t * ctx,int connected)311 void cec_hdmi_hotplug(cec_context_t *ctx, int connected)
312 {
313 //Ignore unplug events when system control is disabled
314 if(!ctx->system_control && connected == 0)
315 return;
316 hdmi_event_t event;
317 event.type = HDMI_EVENT_HOT_PLUG;
318 event.dev = (hdmi_cec_device *) ctx;
319 event.hotplug.connected = connected ? HDMI_CONNECTED : HDMI_NOT_CONNECTED;
320 ctx->callback.callback_func(&event, ctx->callback.callback_arg);
321 }
322
cec_register_event_callback(const struct hdmi_cec_device * dev,event_callback_t callback,void * arg)323 static void cec_register_event_callback(const struct hdmi_cec_device* dev,
324 event_callback_t callback, void* arg)
325 {
326 ALOGD_IF(DEBUG, "%s: Registering callback", __FUNCTION__);
327 cec_context_t* ctx = (cec_context_t*)(dev);
328 ctx->callback.callback_func = callback;
329 ctx->callback.callback_arg = arg;
330 }
331
cec_get_version(const struct hdmi_cec_device * dev,int * version)332 static void cec_get_version(const struct hdmi_cec_device* dev, int* version)
333 {
334 cec_context_t* ctx = (cec_context_t*)(dev);
335 *version = ctx->version;
336 ALOGD_IF(DEBUG, "%s: version: %d", __FUNCTION__, *version);
337 }
338
cec_get_vendor_id(const struct hdmi_cec_device * dev,uint32_t * vendor_id)339 static void cec_get_vendor_id(const struct hdmi_cec_device* dev,
340 uint32_t* vendor_id)
341 {
342 cec_context_t* ctx = (cec_context_t*)(dev);
343 *vendor_id = ctx->vendor_id;
344 ALOGD_IF(DEBUG, "%s: vendor id: %u", __FUNCTION__, *vendor_id);
345 }
346
cec_get_port_info(const struct hdmi_cec_device * dev,struct hdmi_port_info * list[],int * total)347 static void cec_get_port_info(const struct hdmi_cec_device* dev,
348 struct hdmi_port_info* list[], int* total)
349 {
350 ALOGD_IF(DEBUG, "%s: Get port info", __FUNCTION__);
351 cec_context_t* ctx = (cec_context_t*)(dev);
352 *total = NUM_HDMI_PORTS;
353 *list = ctx->port_info;
354 }
355
cec_set_option(const struct hdmi_cec_device * dev,int flag,int value)356 static void cec_set_option(const struct hdmi_cec_device* dev, int flag,
357 int value)
358 {
359 cec_context_t* ctx = (cec_context_t*)(dev);
360 switch (flag) {
361 case HDMI_OPTION_WAKEUP:
362 ALOGD_IF(DEBUG, "%s: Wakeup: value: %d", __FUNCTION__, value);
363 //XXX
364 break;
365 case HDMI_OPTION_ENABLE_CEC:
366 ALOGD_IF(DEBUG, "%s: Enable CEC: value: %d", __FUNCTION__, value);
367 cec_enable(ctx, value? 1 : 0);
368 break;
369 case HDMI_OPTION_SYSTEM_CEC_CONTROL:
370 ALOGD_IF(DEBUG, "%s: system_control: value: %d",
371 __FUNCTION__, value);
372 ctx->system_control = !!value;
373 break;
374 }
375 }
376
cec_set_audio_return_channel(const struct hdmi_cec_device * dev,int port,int flag)377 static void cec_set_audio_return_channel(const struct hdmi_cec_device* dev,
378 int port, int flag)
379 {
380 cec_context_t* ctx = (cec_context_t*)(dev);
381 ctx->arc_enabled = flag ? true : false;
382 ALOGD_IF(DEBUG, "%s: ARC flag: %d port: %d", __FUNCTION__, flag, port);
383 }
384
cec_is_connected(const struct hdmi_cec_device * dev,int port_id)385 static int cec_is_connected(const struct hdmi_cec_device* dev, int port_id)
386 {
387 // Ignore port_id since we have only one port
388 int connected = 0;
389 cec_context_t* ctx = (cec_context_t*)(dev);
390 char connected_path[MAX_PATH_LENGTH];
391 char connected_data[MAX_SYSFS_DATA];
392 snprintf (connected_path, sizeof(connected_path),"%s/connected",
393 ctx->fb_sysfs_path);
394 ssize_t err = read_node(connected_path, connected_data);
395 connected = atoi(connected_data);
396
397 ALOGD_IF(DEBUG, "%s: HDMI at port %d is - %s", __FUNCTION__, port_id,
398 connected ? "connected":"disconnected");
399 if (err < 0)
400 return (int) err;
401 else
402 return connected;
403 }
404
cec_device_close(struct hw_device_t * dev)405 static int cec_device_close(struct hw_device_t *dev)
406 {
407 ALOGD_IF(DEBUG, "%s: Close CEC HAL ", __FUNCTION__);
408 if (!dev) {
409 ALOGE("%s: NULL device pointer", __FUNCTION__);
410 return -EINVAL;
411 }
412 cec_context_t* ctx = (cec_context_t*)(dev);
413 cec_close_context(ctx);
414 free(dev);
415 return 0;
416 }
417
cec_enable(cec_context_t * ctx,int enable)418 static int cec_enable(cec_context_t *ctx, int enable)
419 {
420 ssize_t err;
421 // Enable CEC
422 int value = enable ? 0x3 : 0x0;
423 err = write_int_to_node(ctx, "cec/enable", value);
424 if(err < 0) {
425 ALOGE("%s: Failed to toggle CEC: enable: %d",
426 __FUNCTION__, enable);
427 return (int) err;
428 }
429 ctx->enabled = enable;
430 return 0;
431 }
432
cec_init_context(cec_context_t * ctx)433 static void cec_init_context(cec_context_t *ctx)
434 {
435 ALOGD_IF(DEBUG, "%s: Initializing context", __FUNCTION__);
436 cec_get_fb_node_number(ctx);
437
438 //Initialize ports - We support only one output port
439 ctx->port_info = new hdmi_port_info[NUM_HDMI_PORTS];
440 ctx->port_info[0].type = HDMI_OUTPUT;
441 ctx->port_info[0].port_id = 1;
442 ctx->port_info[0].cec_supported = 1;
443 //XXX: Enable ARC if supported
444 ctx->port_info[0].arc_supported = 0;
445 cec_get_physical_address((hdmi_cec_device *) ctx,
446 &ctx->port_info[0].physical_address );
447
448 ctx->version = 0x4;
449 ctx->vendor_id = 0xA47733;
450 cec_clear_logical_address((hdmi_cec_device_t*)ctx);
451
452 //Set up listener for HDMI events
453 ctx->disp_client = new qClient::QHDMIClient();
454 ctx->disp_client->setCECContext(ctx);
455 ctx->disp_client->registerClient(ctx->disp_client);
456
457 //Enable CEC - framework expects it to be enabled by default
458 cec_enable(ctx, true);
459
460 ALOGD("%s: CEC enabled", __FUNCTION__);
461 }
462
cec_close_context(cec_context_t * ctx __unused)463 static void cec_close_context(cec_context_t* ctx __unused)
464 {
465 ALOGD("%s: Closing context", __FUNCTION__);
466 }
467
cec_device_open(const struct hw_module_t * module,const char * name,struct hw_device_t ** device)468 static int cec_device_open(const struct hw_module_t* module,
469 const char* name,
470 struct hw_device_t** device)
471 {
472 ALOGD_IF(DEBUG, "%s: name: %s", __FUNCTION__, name);
473 int status = -EINVAL;
474 if (!strcmp(name, HDMI_CEC_HARDWARE_INTERFACE )) {
475 struct cec_context_t *dev;
476 dev = (cec_context_t *) calloc (1, sizeof(*dev));
477 if (dev) {
478 cec_init_context(dev);
479
480 //Setup CEC methods
481 dev->device.common.tag = HARDWARE_DEVICE_TAG;
482 dev->device.common.version = HDMI_CEC_DEVICE_API_VERSION_1_0;
483 dev->device.common.module = const_cast<hw_module_t* >(module);
484 dev->device.common.close = cec_device_close;
485 dev->device.add_logical_address = cec_add_logical_address;
486 dev->device.clear_logical_address = cec_clear_logical_address;
487 dev->device.get_physical_address = cec_get_physical_address;
488 dev->device.send_message = cec_send_message;
489 dev->device.register_event_callback = cec_register_event_callback;
490 dev->device.get_version = cec_get_version;
491 dev->device.get_vendor_id = cec_get_vendor_id;
492 dev->device.get_port_info = cec_get_port_info;
493 dev->device.set_option = cec_set_option;
494 dev->device.set_audio_return_channel = cec_set_audio_return_channel;
495 dev->device.is_connected = cec_is_connected;
496
497 *device = &dev->device.common;
498 status = 0;
499 } else {
500 status = -EINVAL;
501 }
502 }
503 return status;
504 }
505 }; //namespace qhdmicec
506
507 // Standard HAL module, should be outside qhdmicec namespace
508 static struct hw_module_methods_t cec_module_methods = {
509 .open = qhdmicec::cec_device_open
510 };
511
512 hdmi_module_t HAL_MODULE_INFO_SYM = {
513 .common = {
514 .tag = HARDWARE_MODULE_TAG,
515 .version_major = 1,
516 .version_minor = 0,
517 .id = HDMI_CEC_HARDWARE_MODULE_ID,
518 .name = "QTI HDMI CEC module",
519 .author = "The Linux Foundation",
520 .methods = &cec_module_methods,
521 }
522 };
523
524
525