blob: 056cea1e256c70395d5426cb1d926fe8ae03a667 [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);
192 //XXX: Not sure if this physical address is the same as the one in port info
193 *addr = ctx->port_info[0].physical_address;
194 ALOGD_IF(DEBUG, "%s: Physical Address: 0x%x", __FUNCTION__, *addr);
195 return 0;
196}
197
198static int cec_send_message(const struct hdmi_cec_device* dev,
199 const cec_message_t* msg)
200{
201 ATRACE_CALL();
202 cec_context_t* ctx = (cec_context_t*)(dev);
203 ALOGD_IF(DEBUG, "%s: initiator: %d destination: %d length: %u",
204 __FUNCTION__, msg->initiator, msg->destination,
205 (uint32_t) msg->length);
206
207 // Dump message received from framework
208 char dump[128];
209 if(msg->length > 0) {
210 hex_to_string((char*)msg->body, msg->length, dump);
211 ALOGD_IF(DEBUG, "%s: message from framework: %s", __FUNCTION__, dump);
212 }
213
214 char write_msg_path[MAX_PATH_LENGTH];
215 char write_msg[MAX_CEC_FRAME_SIZE];
216 memset(write_msg, 0, sizeof(write_msg));
217 // See definition of struct hdmi_cec_msg in driver code
218 // drivers/video/msm/mdss/mdss_hdmi_cec.c
219 // Write header block
220 // XXX: Include this from header in kernel
221 write_msg[CEC_OFFSET_SENDER_ID] = msg->initiator;
222 write_msg[CEC_OFFSET_RECEIVER_ID] = msg->destination;
223 //Kernel splits opcode/operand, but Android sends it in one byte array
224 write_msg[CEC_OFFSET_OPCODE] = msg->body[0];
225 if(msg->length > 1) {
226 memcpy(&write_msg[CEC_OFFSET_OPERAND], &msg->body[1],
227 sizeof(char)*(msg->length - 1));
228 }
229 //msg length + initiator + destination
230 write_msg[CEC_OFFSET_FRAME_LENGTH] = (unsigned char) (msg->length + 1);
231 hex_to_string(write_msg, sizeof(write_msg), dump);
232 ALOGD_IF(DEBUG, "%s: message to driver: %s", __FUNCTION__, dump);
233 snprintf(write_msg_path, sizeof(write_msg_path), "%s/cec/wr_msg",
234 ctx->fb_sysfs_path);
235 int retry_count = 0;
236 ssize_t err = 0;
237 //HAL spec requires us to retry at least once.
238 while (true) {
239 err = write_node(write_msg_path, write_msg, sizeof(write_msg));
240 retry_count++;
241 if (err == -EAGAIN && retry_count <= MAX_SEND_MESSAGE_RETRIES) {
242 ALOGE("%s: CEC line busy, retrying", __FUNCTION__);
243 } else {
244 break;
245 }
246 }
247
248 if (err < 0) {
249 if (err == -ENXIO) {
250 ALOGE("%s: No device exists with the destination address",
251 __FUNCTION__);
252 return HDMI_RESULT_NACK;
253 } else if (err == -EAGAIN) {
254 ALOGE("%s: CEC line is busy, max retry count exceeded",
255 __FUNCTION__);
256 return HDMI_RESULT_BUSY;
257 } else {
258 return HDMI_RESULT_FAIL;
259 ALOGE("%s: Failed to send CEC message err: %zd - %s",
260 __FUNCTION__, err, strerror(int(-err)));
261 }
262 } else {
263 ALOGD_IF(DEBUG, "%s: Sent CEC message - %zd bytes written",
264 __FUNCTION__, err);
265 return HDMI_RESULT_SUCCESS;
266 }
267}
268
269void cec_receive_message(cec_context_t *ctx, char *msg, ssize_t len)
270{
271 char dump[128];
272 if(len > 0) {
273 hex_to_string(msg, len, dump);
274 ALOGD_IF(DEBUG, "%s: Message from driver: %s", __FUNCTION__, dump);
275 }
276
277 hdmi_event_t event;
278 event.type = HDMI_EVENT_CEC_MESSAGE;
279 event.dev = (hdmi_cec_device *) ctx;
280 // Remove initiator/destination from this calculation
281 event.cec.length = msg[CEC_OFFSET_FRAME_LENGTH] - 1;
282 event.cec.initiator = (cec_logical_address_t) msg[CEC_OFFSET_SENDER_ID];
283 event.cec.destination = (cec_logical_address_t) msg[CEC_OFFSET_RECEIVER_ID];
284 //Copy opcode and operand
285 memcpy(event.cec.body, &msg[CEC_OFFSET_OPCODE], event.cec.length);
286 hex_to_string((char *) event.cec.body, event.cec.length, dump);
287 ALOGD_IF(DEBUG, "%s: Message to framework: %s", __FUNCTION__, dump);
288 ctx->callback.callback_func(&event, ctx->callback.callback_arg);
289}
290
291void cec_hdmi_hotplug(cec_context_t *ctx, int connected)
292{
293 hdmi_event_t event;
294 event.type = HDMI_EVENT_HOT_PLUG;
295 event.dev = (hdmi_cec_device *) ctx;
296 event.hotplug.connected = connected ? HDMI_CONNECTED : HDMI_NOT_CONNECTED;
297 ctx->callback.callback_func(&event, ctx->callback.callback_arg);
298}
299
300static void cec_register_event_callback(const struct hdmi_cec_device* dev,
301 event_callback_t callback, void* arg)
302{
303 ALOGD_IF(DEBUG, "%s: Registering callback", __FUNCTION__);
304 cec_context_t* ctx = (cec_context_t*)(dev);
305 ctx->callback.callback_func = callback;
306 ctx->callback.callback_arg = arg;
307}
308
309static void cec_get_version(const struct hdmi_cec_device* dev, int* version)
310{
311 cec_context_t* ctx = (cec_context_t*)(dev);
312 *version = ctx->version;
313 ALOGD_IF(DEBUG, "%s: version: %d", __FUNCTION__, *version);
314}
315
316static void cec_get_vendor_id(const struct hdmi_cec_device* dev,
317 uint32_t* vendor_id)
318{
319 cec_context_t* ctx = (cec_context_t*)(dev);
320 *vendor_id = ctx->vendor_id;
321 ALOGD_IF(DEBUG, "%s: vendor id: %u", __FUNCTION__, *vendor_id);
322}
323
324static void cec_get_port_info(const struct hdmi_cec_device* dev,
325 struct hdmi_port_info* list[], int* total)
326{
327 ALOGD_IF(DEBUG, "%s: Get port info", __FUNCTION__);
328 cec_context_t* ctx = (cec_context_t*)(dev);
329 *total = NUM_HDMI_PORTS;
330 *list = ctx->port_info;
331}
332
333static void cec_set_option(const struct hdmi_cec_device* dev, int flag,
334 int value)
335{
336 cec_context_t* ctx = (cec_context_t*)(dev);
337 ALOGD_IF(DEBUG, "%s: flag:%d value:%d", __FUNCTION__, flag, value);
338 switch (flag) {
339 case HDMI_OPTION_WAKEUP:
340 //XXX
341 break;
342 case HDMI_OPTION_ENABLE_CEC:
343 cec_enable(ctx, value? 1 : 0);
344 break;
345 case HDMI_OPTION_SYSTEM_CEC_CONTROL:
346 //XXX
347 break;
348 }
349}
350
351static void cec_set_audio_return_channel(const struct hdmi_cec_device* dev,
352 int flag)
353{
354 cec_context_t* ctx = (cec_context_t*)(dev);
355 ctx->arc_enabled = flag ? true : false;
356 ALOGD_IF(DEBUG, "%s: ARC flag: %d", __FUNCTION__, flag);
357}
358
359static int cec_is_connected(const struct hdmi_cec_device* dev, int port_id)
360{
361 // Ignore port_id since we have only one port
362 int connected = 0;
363 cec_context_t* ctx = (cec_context_t*)(dev);
364 char connected_path[MAX_PATH_LENGTH];
365 char connected_data[MAX_SYSFS_DATA];
366 snprintf (connected_path, sizeof(connected_path),"%s/connected",
367 ctx->fb_sysfs_path);
368 ssize_t err = read_node(connected_path, connected_data);
369 connected = atoi(connected_data);
370
371 ALOGD_IF(DEBUG, "%s: HDMI at port %d is - %s", __FUNCTION__, port_id,
372 connected ? "connected":"disconnected");
373 if (err)
374 return (int) err;
375 else
376 return connected;
377}
378
379static int cec_device_close(struct hw_device_t *dev)
380{
381 ALOGD_IF(DEBUG, "%s: Close CEC HAL ", __FUNCTION__);
382 if (!dev) {
383 ALOGE("%s: NULL device pointer", __FUNCTION__);
384 return -EINVAL;
385 }
386 cec_context_t* ctx = (cec_context_t*)(dev);
387 cec_close_context(ctx);
388 free(dev);
389 return 0;
390}
391
392static int cec_enable(cec_context_t *ctx, int enable)
393{
394 ssize_t err;
395 err = write_int_to_node(ctx, "cec/enable", !!enable);
396 if(err < 0) {
397 ALOGE("%s: Failed to toggle CEC: enable: %d",
398 __FUNCTION__, enable);
399 return (int) err;
400 }
401 ctx->enabled = enable;
402 return 0;
403}
404
405static void cec_init_context(cec_context_t *ctx)
406{
407 ALOGD_IF(DEBUG, "%s: Initializing context", __FUNCTION__);
408 cec_get_fb_node_number(ctx);
409
410 //Initialize ports - We support only one output port
411 ctx->port_info = new hdmi_port_info[NUM_HDMI_PORTS];
412 ctx->port_info[0].type = HDMI_OUTPUT;
413 //XXX: Updated l-dev has port_id field
414 ctx->port_info[0].port_id = 1;
415 ctx->port_info[0].cec_supported = 1;
416 //XXX: Enable ARC if supported
417 ctx->port_info[0].arc_supported = 0;
418 //XXX: Get physical address from driver
419 ctx->port_info[0].physical_address = 0x1000;
420
421 ctx->version = 0x4;
422 //XXX: Get vendor ID from driver - this is currently a placeholder value
423 ctx->vendor_id = 0x4571;
424 cec_clear_logical_address((hdmi_cec_device_t*)ctx);
425
426 //Set up listener for HDMI events
427 ctx->disp_client = new qClient::QHDMIClient();
428 ctx->disp_client->setCECContext(ctx);
429 ctx->disp_client->registerClient(ctx->disp_client);
430
431 //Enable CEC - framework expects it to be enabled by default
432 cec_enable(ctx, true);
433
434 ALOGD("%s: CEC enabled", __FUNCTION__);
435}
436
437static void cec_close_context(cec_context_t* ctx __unused)
438{
439 ALOGD("%s: Closing context", __FUNCTION__);
440}
441
442static int cec_device_open(const struct hw_module_t* module,
443 const char* name,
444 struct hw_device_t** device)
445{
446 ALOGD_IF(DEBUG, "%s: name: %s", __FUNCTION__, name);
447 int status = -EINVAL;
448 if (!strcmp(name, HDMI_CEC_HARDWARE_INTERFACE )) {
449 struct cec_context_t *dev;
450 dev = (cec_context_t *) calloc (1, sizeof(*dev));
451 if (dev) {
452 cec_init_context(dev);
453
454 //Setup CEC methods
455 dev->device.common.tag = HARDWARE_DEVICE_TAG;
456 dev->device.common.version = HDMI_CEC_DEVICE_API_VERSION_1_0;
457 dev->device.common.module = const_cast<hw_module_t* >(module);
458 dev->device.common.close = cec_device_close;
459 dev->device.add_logical_address = cec_add_logical_address;
460 dev->device.clear_logical_address = cec_clear_logical_address;
461 dev->device.get_physical_address = cec_get_physical_address;
462 dev->device.send_message = cec_send_message;
463 dev->device.register_event_callback = cec_register_event_callback;
464 dev->device.get_version = cec_get_version;
465 dev->device.get_vendor_id = cec_get_vendor_id;
466 dev->device.get_port_info = cec_get_port_info;
467 dev->device.set_option = cec_set_option;
468 dev->device.set_audio_return_channel = cec_set_audio_return_channel;
469 dev->device.is_connected = cec_is_connected;
470
471 *device = &dev->device.common;
472 status = 0;
473 } else {
474 status = -EINVAL;
475 }
476 }
477 return status;
478}
479}; //namespace qhdmicec
480
481// Standard HAL module, should be outside qhdmicec namespace
482static struct hw_module_methods_t cec_module_methods = {
483 .open = qhdmicec::cec_device_open
484};
485
486hdmi_module_t HAL_MODULE_INFO_SYM = {
487 .common = {
488 .tag = HARDWARE_MODULE_TAG,
489 .version_major = 1,
490 .version_minor = 0,
491 .id = HDMI_CEC_HARDWARE_MODULE_ID,
492 .name = "QTI HDMI CEC module",
493 .author = "The Linux Foundation",
494 .methods = &cec_module_methods,
495 }
496};
497
498