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