summaryrefslogtreecommitdiffstats
path: root/exynos3/s5pc110/libhdmi/hal_module.cpp
blob: 767e6b54c5d2edbcc542818bfc9318b278af0d7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright 2011, Havlena Petr <havlenapetr@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_NDEBUG 0
#include <cutils/log.h>

#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <linux/fb.h>

#include "hardware/hdmi.h"

#include "SecHDMI.h"

using namespace android;

#define RETURN_EINVAL_IF(hw)                                            \
    if(!hw) {                                                           \
        ALOGE("%s: %i - Can't obtain hw driver!", __func__, __LINE__);  \
        return -EINVAL;                                                 \
    }

struct sec_hdmi_device_t {
    hdmi_device_t   base;
    /* Sec specific "private" data can go here (base.priv) */
    SecHDMI*        hw;
    int             lcd_width;
    int             lcd_height;
};

static SecHDMI* sec_obtain_hw(struct hdmi_device_t* device)
{
    if(!device) {
        ALOGE("Can't obtain hdmi base device!");
        return NULL;
    }

    struct sec_hdmi_device_t* dev =
            (struct sec_hdmi_device_t *) device;
    if(!dev) {
        ALOGE("Can't obtain SEC hdmi device!");
        return NULL;
    }

    return dev->hw;
}

static int hdmi_connect(struct hdmi_device_t* dev)
{
    ALOGV("connect is called");

    SecHDMI* hw = sec_obtain_hw(dev);
    RETURN_EINVAL_IF(hw);

    return hw->connect();
}

static int hdmi_disconnect(struct hdmi_device_t* dev)
{
    ALOGV("disconnect is called");

    SecHDMI* hw = sec_obtain_hw(dev);
    RETURN_EINVAL_IF(hw);

    return hw->disconnect();
}

static int hdmi_clear(struct hdmi_device_t* dev, int hdmiLayer)
{
    ALOGV("clear is called");

    SecHDMI* hw = sec_obtain_hw(dev);
    RETURN_EINVAL_IF(hw);

    return 0/*hw->clear(hdmiLayer) ? 0 : -1*/;
}

static int hdmi_blit(struct hdmi_device_t* dev, int srcW, int srcH, int srcColorFormat,
                        uint32_t srcYAddr, uint32_t srcCbAddr, uint32_t srcCrAddr,
                        int dstX, int dstY,
                        int layer,
                        int num_of_hwc_layer)
{
    ALOGV("blit is called");

    SecHDMI* hw = sec_obtain_hw(dev);
    RETURN_EINVAL_IF(hw);

    return hw->flush(srcW, srcH, srcColorFormat, srcYAddr, srcCbAddr, srcCrAddr,
                     dstX, dstY, layer, num_of_hwc_layer);
}

static int hdmi_close(struct hdmi_device_t *dev)
{
    ALOGV("close is called");

    if (!dev) {
        return 0;
    }

    SecHDMI* hw = sec_obtain_hw(dev);
    if(hw) {
        hw->destroy();
        delete hw;
    }

    free(dev);

    return 0;
}

static int hdmi_get_lcd_size(int* width, int* height)
{
    char const * const device_template[] = {
                "/dev/graphics/fb%u",
                "/dev/fb%u",
                0 };

    int fd = -1;
    char name[64];

    for(int i = 0; fd < 0 && device_template[i]; i++) {
        snprintf(name, 64, device_template[i], 0);
        fd = open(name, O_RDWR, 0);
    }

    if (fd < 0) {
        return -1;
    }

    struct fb_var_screeninfo info;
    if (ioctl(fd, FBIOGET_VSCREENINFO, &info) < 0) {
        close(fd);
        return -2;
    }

    *width  = info.xres;
    *height = info.yres;
    
    close(fd);
    return 0;
}

static int hdmi_open(const struct hw_module_t *module, char const *name,
                         struct hw_device_t **device)
{
    int lcdWidth, lcdHeight;

    ALOGV("open: open with %s", name);

    if (strcmp("hdmi-test", name) &&
        strcmp("hdmi-service", name) &&
        strcmp("hdmi-composer", name)) {
        return -EINVAL;
    }

    if(hdmi_get_lcd_size(&lcdWidth, &lcdHeight) < 0) {
        return -EINVAL;
    }

    struct sec_hdmi_device_t *hdmi_dev =
           (struct sec_hdmi_device_t *) malloc(sizeof(struct sec_hdmi_device_t));
    if(!hdmi_dev) {
        return -ENOMEM;
    }
    memset(hdmi_dev, 0, sizeof(*hdmi_dev));

    hdmi_dev->base.common.tag = HARDWARE_DEVICE_TAG;
    hdmi_dev->base.common.version = 0;
    hdmi_dev->base.common.module = (struct hw_module_t *)module;
    hdmi_dev->base.common.close = (int (*)(struct hw_device_t *))hdmi_close;
    hdmi_dev->base.connect = hdmi_connect;
    hdmi_dev->base.disconnect = hdmi_disconnect;
    hdmi_dev->base.clear = hdmi_clear;
    hdmi_dev->base.blit = hdmi_blit;
    hdmi_dev->lcd_width = lcdWidth;
    hdmi_dev->lcd_height = lcdHeight;

    *device = &hdmi_dev->base.common;

    hdmi_dev->hw = new SecHDMI();
    if(hdmi_dev->hw->create(lcdWidth, lcdHeight) < 0) {
        hdmi_close((hdmi_device_t *)hdmi_dev);
        return -EINVAL;
    }

    ALOGI("initzialized for lcd size: %dx%d", lcdWidth, lcdHeight);

    return 0;
}

static struct hw_module_methods_t hal_module_methods = {
    open: hdmi_open,
};

extern "C" {
    struct hw_module_t HAL_MODULE_INFO_SYM = {
        tag: HARDWARE_MODULE_TAG,
        version_major: 1,
        version_minor: 0,
        id: HDMI_HARDWARE_MODULE_ID,
        name: "Samsung S5PC11X hdmi module",
        author: "Havlena Petr <havlenapetr@gmail.com>",
        methods: &hal_module_methods,
    };
}