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