blob: 767e6b54c5d2edbcc542818bfc9318b278af0d7b [file] [log] [blame]
Petr Havlenac9288142012-11-15 14:07:10 +05301/*
2 * Copyright 2011, Havlena Petr <havlenapetr@gmail.com>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_NDEBUG 0
18#include <cutils/log.h>
19
20#include <stdint.h>
21#include <string.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <sys/types.h>
25#include <linux/fb.h>
26
27#include "hardware/hdmi.h"
28
29#include "SecHDMI.h"
30
31using namespace android;
32
33#define RETURN_EINVAL_IF(hw) \
34 if(!hw) { \
35 ALOGE("%s: %i - Can't obtain hw driver!", __func__, __LINE__); \
36 return -EINVAL; \
37 }
38
39struct sec_hdmi_device_t {
40 hdmi_device_t base;
41 /* Sec specific "private" data can go here (base.priv) */
42 SecHDMI* hw;
43 int lcd_width;
44 int lcd_height;
45};
46
47static SecHDMI* sec_obtain_hw(struct hdmi_device_t* device)
48{
49 if(!device) {
50 ALOGE("Can't obtain hdmi base device!");
51 return NULL;
52 }
53
54 struct sec_hdmi_device_t* dev =
55 (struct sec_hdmi_device_t *) device;
56 if(!dev) {
57 ALOGE("Can't obtain SEC hdmi device!");
58 return NULL;
59 }
60
61 return dev->hw;
62}
63
64static int hdmi_connect(struct hdmi_device_t* dev)
65{
66 ALOGV("connect is called");
67
68 SecHDMI* hw = sec_obtain_hw(dev);
69 RETURN_EINVAL_IF(hw);
70
71 return hw->connect();
72}
73
74static int hdmi_disconnect(struct hdmi_device_t* dev)
75{
76 ALOGV("disconnect is called");
77
78 SecHDMI* hw = sec_obtain_hw(dev);
79 RETURN_EINVAL_IF(hw);
80
81 return hw->disconnect();
82}
83
84static int hdmi_clear(struct hdmi_device_t* dev, int hdmiLayer)
85{
86 ALOGV("clear is called");
87
88 SecHDMI* hw = sec_obtain_hw(dev);
89 RETURN_EINVAL_IF(hw);
90
91 return 0/*hw->clear(hdmiLayer) ? 0 : -1*/;
92}
93
94static int hdmi_blit(struct hdmi_device_t* dev, int srcW, int srcH, int srcColorFormat,
95 uint32_t srcYAddr, uint32_t srcCbAddr, uint32_t srcCrAddr,
96 int dstX, int dstY,
97 int layer,
98 int num_of_hwc_layer)
99{
100 ALOGV("blit is called");
101
102 SecHDMI* hw = sec_obtain_hw(dev);
103 RETURN_EINVAL_IF(hw);
104
105 return hw->flush(srcW, srcH, srcColorFormat, srcYAddr, srcCbAddr, srcCrAddr,
106 dstX, dstY, layer, num_of_hwc_layer);
107}
108
109static int hdmi_close(struct hdmi_device_t *dev)
110{
111 ALOGV("close is called");
112
113 if (!dev) {
114 return 0;
115 }
116
117 SecHDMI* hw = sec_obtain_hw(dev);
118 if(hw) {
119 hw->destroy();
120 delete hw;
121 }
122
123 free(dev);
124
125 return 0;
126}
127
128static int hdmi_get_lcd_size(int* width, int* height)
129{
130 char const * const device_template[] = {
131 "/dev/graphics/fb%u",
132 "/dev/fb%u",
133 0 };
134
135 int fd = -1;
136 char name[64];
137
138 for(int i = 0; fd < 0 && device_template[i]; i++) {
139 snprintf(name, 64, device_template[i], 0);
140 fd = open(name, O_RDWR, 0);
141 }
142
143 if (fd < 0) {
144 return -1;
145 }
146
147 struct fb_var_screeninfo info;
148 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) < 0) {
149 close(fd);
150 return -2;
151 }
152
153 *width = info.xres;
154 *height = info.yres;
155
156 close(fd);
157 return 0;
158}
159
160static int hdmi_open(const struct hw_module_t *module, char const *name,
161 struct hw_device_t **device)
162{
163 int lcdWidth, lcdHeight;
164
165 ALOGV("open: open with %s", name);
166
167 if (strcmp("hdmi-test", name) &&
168 strcmp("hdmi-service", name) &&
169 strcmp("hdmi-composer", name)) {
170 return -EINVAL;
171 }
172
173 if(hdmi_get_lcd_size(&lcdWidth, &lcdHeight) < 0) {
174 return -EINVAL;
175 }
176
177 struct sec_hdmi_device_t *hdmi_dev =
178 (struct sec_hdmi_device_t *) malloc(sizeof(struct sec_hdmi_device_t));
179 if(!hdmi_dev) {
180 return -ENOMEM;
181 }
182 memset(hdmi_dev, 0, sizeof(*hdmi_dev));
183
184 hdmi_dev->base.common.tag = HARDWARE_DEVICE_TAG;
185 hdmi_dev->base.common.version = 0;
186 hdmi_dev->base.common.module = (struct hw_module_t *)module;
187 hdmi_dev->base.common.close = (int (*)(struct hw_device_t *))hdmi_close;
188 hdmi_dev->base.connect = hdmi_connect;
189 hdmi_dev->base.disconnect = hdmi_disconnect;
190 hdmi_dev->base.clear = hdmi_clear;
191 hdmi_dev->base.blit = hdmi_blit;
192 hdmi_dev->lcd_width = lcdWidth;
193 hdmi_dev->lcd_height = lcdHeight;
194
195 *device = &hdmi_dev->base.common;
196
197 hdmi_dev->hw = new SecHDMI();
198 if(hdmi_dev->hw->create(lcdWidth, lcdHeight) < 0) {
199 hdmi_close((hdmi_device_t *)hdmi_dev);
200 return -EINVAL;
201 }
202
203 ALOGI("initzialized for lcd size: %dx%d", lcdWidth, lcdHeight);
204
205 return 0;
206}
207
208static struct hw_module_methods_t hal_module_methods = {
209 open: hdmi_open,
210};
211
212extern "C" {
213 struct hw_module_t HAL_MODULE_INFO_SYM = {
214 tag: HARDWARE_MODULE_TAG,
215 version_major: 1,
216 version_minor: 0,
217 id: HDMI_HARDWARE_MODULE_ID,
218 name: "Samsung S5PC11X hdmi module",
219 author: "Havlena Petr <havlenapetr@gmail.com>",
220 methods: &hal_module_methods,
221 };
222}