summaryrefslogtreecommitdiffstats
path: root/exynos3/s5pc110/libhdmi/SecHDMI.cpp
blob: eeacb4d50e1f51f83977168c2b8e9b1e8e2d9a79 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/*
 * 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 <utils/Log.h>

#include <string.h>
#include <stdlib.h>
#include <sys/poll.h>

#include <sec_lcd.h>

#include "SecHDMI.h"
#include "fimd.h"

using namespace android;

#define RETURN_IF(return_value)                                      \
    if (return_value < 0) {                                          \
        ALOGE("%s::%d fail. errno: %s",                              \
             __func__, __LINE__, strerror(errno));                   \
        return -1;                                                   \
    }

#define ALOG_IF(return_value)                                        \
    if (return_value < 0) {                                          \
        ALOGE("%s::%d fail. errno: %s",                              \
             __func__, __LINE__, strerror(errno));                   \
    }

#define ALIGN_TO_32B(x)     ((((x) + (1 <<  5) - 1) >>  5) <<  5)
#define ALIGN_TO_128B(x)    ((((x) + (1 <<  7) - 1) >>  7) <<  7)
#define ALIGN_TO_8KB(x)     ((((x) + (1 << 13) - 1) >> 13) << 13)

struct s5p_tv_standart_internal {
    int index;
    unsigned long value;
} s5p_tv_standards[] = {
    {
        S5P_TV_STD_NTSC_M,
        V4L2_STD_NTSC_M,
    }, {
        S5P_TV_STD_PAL_BDGHI,
        V4L2_STD_PAL_BDGHI,
    }, {
        S5P_TV_STD_PAL_M,
        V4L2_STD_PAL_M,
    }, {
        S5P_TV_STD_PAL_N,
        V4L2_STD_PAL_N,
    }, {
        S5P_TV_STD_PAL_Nc,
        V4L2_STD_PAL_Nc,
    }, {
        S5P_TV_STD_PAL_60,
        V4L2_STD_PAL_60,
    }, {
        S5P_TV_STD_NTSC_443,
        V4L2_STD_NTSC_443,
    }, {
        S5P_TV_STD_480P_60_16_9,
        V4L2_STD_480P_60_16_9,
    }, {
        S5P_TV_STD_480P_60_4_3,
        V4L2_STD_480P_60_4_3,
    }, {
        S5P_TV_STD_576P_50_16_9,
        V4L2_STD_576P_50_16_9,
    }, {
        S5P_TV_STD_576P_50_4_3,
        V4L2_STD_576P_50_4_3,
    }, {
        S5P_TV_STD_720P_60,
        V4L2_STD_720P_60,
    }, {
        S5P_TV_STD_720P_50,
        V4L2_STD_720P_50,
    },
};

static inline int calcFrameSize(int format, int width, int height)
{
    int size = 0;
    
    switch (format) {
        case V4L2_PIX_FMT_YUV420:
        case V4L2_PIX_FMT_NV12:
        case V4L2_PIX_FMT_NV21:
            size = (width * height * 3 / 2);
            break;
            
        case V4L2_PIX_FMT_NV12T:
            size = ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height)) +
            ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height / 2));
            break;
            
        case V4L2_PIX_FMT_YUV422P:
        case V4L2_PIX_FMT_YUYV:
        case V4L2_PIX_FMT_UYVY:
            size = (width * height * 2);
            break;
            
        default :
            ALOGE("ERR(%s):Invalid V4L2 pixel format(%d)\n", __func__, format);
        case V4L2_PIX_FMT_RGB565:
            size = (width * height * 2);
            break;
    }
    return size;
}

static int get_pixel_depth(unsigned int fmt)
{
    int depth = 0;
    
    switch (fmt) {
        case V4L2_PIX_FMT_NV12:
            depth = 12;
            break;
        case V4L2_PIX_FMT_NV12T:
            depth = 12;
            break;
        case V4L2_PIX_FMT_NV21:
            depth = 12;
            break;
        case V4L2_PIX_FMT_YUV420:
            depth = 12;
            break;
            
        case V4L2_PIX_FMT_RGB565:
        case V4L2_PIX_FMT_YUYV:
        case V4L2_PIX_FMT_YVYU:
        case V4L2_PIX_FMT_UYVY:
        case V4L2_PIX_FMT_VYUY:
        case V4L2_PIX_FMT_NV16:
        case V4L2_PIX_FMT_NV61:
        case V4L2_PIX_FMT_YUV422P:
            depth = 16;
            break;
            
        case V4L2_PIX_FMT_RGB32:
            depth = 32;
            break;
    }
    
    return depth;
}

// ======================================================================
// Video ioctls

static int tv20_v4l2_querycap(int fp)
{
    struct v4l2_capability cap;
    
    int ret = ioctl(fp, VIDIOC_QUERYCAP, &cap);
    if (ret < 0) {
        ALOGE("ERR(%s):VIDIOC_QUERYCAP failed", __func__);
        return -1;
    }
    
    if (!(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) {
        ALOGE("ERR(%s):no output devices\n", __func__);
        return -1;
    }
    ALOGV("Name of cap driver is %s", cap.driver);
    
    return ret;
}

static const __u8* tv20_v4l2_enum_output(int fp, int index)
{
    static struct v4l2_output output;
    
    output.index = index;
    if (ioctl(fp, VIDIOC_ENUMOUTPUT, &output) != 0) {
        ALOGE("ERR(%s):No matching index found", __func__);
        return NULL;
    }
    ALOGV("Name of output channel[%d] is %s", output.index, output.name);
    
    return output.name;
}

static const __u8* tv20_v4l2_enum_standarts(int fp, int index)
{
    static struct v4l2_standard standart;
    
    standart.index = index;
    if (ioctl(fp, VIDIOC_ENUMSTD, &standart) != 0) {
        ALOGE("ERR(%s):No matching index found\n", __func__);
        return NULL;
    }
    ALOGV("Name of output standart[%d] is %s\n", standart.index, standart.name);
    
    return standart.name;
}

static int tv20_v4l2_s_output(int fp, int index)
{
    struct v4l2_output output;
    int ret;
    
    output.index = index;
    
    ret = ioctl(fp, VIDIOC_S_OUTPUT, &output);
    if (ret < 0) {
        ALOGE("ERR(%s):VIDIOC_S_OUPUT failed\n", __func__);
        return ret;
    }
    return ret;
}

static int tv20_v4l2_s_std(int fp, unsigned long id)
{
    v4l2_std_id std;
    int ret;
    
    std = id;
    
    ret = ioctl(fp, VIDIOC_S_STD, &std);
    if (ret < 0) {
        ALOGE("ERR(%s):VIDIOC_S_OUPUT failed\n", __func__);
        return ret;
    }
    return ret;
}

static int tv20_v4l2_enum_fmt(int fp, unsigned int fmt)
{
    struct v4l2_fmtdesc fmtdesc;
    int found = 0;
    
    fmtdesc.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
    fmtdesc.index = 0;
    
    while (ioctl(fp, VIDIOC_ENUM_FMT, &fmtdesc) == 0) {
        if (fmtdesc.pixelformat == fmt) {
            ALOGV("passed fmt = %#x found pixel format[%d]: %s\n", fmt, fmtdesc.index, fmtdesc.description);
            found = 1;
            break;
        }
        
        fmtdesc.index++;
    }
    
    if (!found) {
        ALOGE("unsupported pixel format\n");
        return -1;
    }
    
    return 0;
}

static int tv20_v4l2_s_fmt(int fp, int width, int height,
                           unsigned int fmt, unsigned int yAddr, unsigned int cAddr)
{
    struct v4l2_format v4l2_fmt;
    struct v4l2_pix_format_s5p_tvout pixfmt;
    int ret;

    v4l2_fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
#if 0
    ret = ioctl(fp, VIDIOC_G_FMT, &v4l2_fmt);
    if (ret < 0) {
        ALOGE("ERR(%s):VIDIOC_G_FMT failed", __func__);
        return -1;
    }
#endif

    memset(&pixfmt, 0, sizeof(pixfmt));
    pixfmt.pix_fmt.width = width;
    pixfmt.pix_fmt.height = height;
    pixfmt.pix_fmt.pixelformat = fmt;
    pixfmt.pix_fmt.sizeimage = (width * height * get_pixel_depth(fmt)) / 8;
    pixfmt.pix_fmt.field = V4L2_FIELD_NONE;

    // here we must set addresses of our memory for video out
    pixfmt.base_y = (void *)yAddr;
    pixfmt.base_c = (void* )cAddr;

    v4l2_fmt.fmt.pix = pixfmt.pix_fmt;
    memcpy(v4l2_fmt.fmt.raw_data, &pixfmt,
           sizeof(struct v4l2_pix_format_s5p_tvout));

    /* Set up for capture */
    ret = ioctl(fp, VIDIOC_S_FMT, &v4l2_fmt);
    if (ret < 0) {
        ALOGE("ERR(%s):VIDIOC_S_FMT failed\n", __func__);
        return -1;
    }
    return 0;
}

static int tv20_v4l2_streamon(int fp)
{
    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
    int ret;
    
    ret = ioctl(fp, VIDIOC_STREAMON, &type);
    if (ret < 0) {
        ALOGE("ERR(%s):VIDIOC_STREAMON failed\n", __func__);
        return ret;
    }
    
    return ret;
}

static int tv20_v4l2_streamoff(int fp)
{
    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
    int ret;
    
    ALOGV("%s :", __func__);
    ret = ioctl(fp, VIDIOC_STREAMOFF, &type);
    if (ret < 0) {
        ALOGE("ERR(%s):VIDIOC_STREAMOFF failed\n", __func__);
        return ret;
    }
    
    return ret;
}

static int tv20_v4l2_g_parm(int fp, struct v4l2_streamparm *streamparm)
{
    int ret;
    
    streamparm->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
    
    ret = ioctl(fp, VIDIOC_G_PARM, streamparm);
    if (ret < 0) {
        ALOGE("ERR(%s):VIDIOC_G_PARM failed\n", __func__);
        return -1;
    }
    
    ALOGV("%s : timeperframe: numerator %d, denominator %d\n", __func__,
         streamparm->parm.capture.timeperframe.numerator,
         streamparm->parm.capture.timeperframe.denominator);
    
    return 0;
}

static int tv20_v4l2_s_parm(int fp, struct v4l2_streamparm *streamparm)
{
    int ret;
    
    streamparm->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
    
    ret = ioctl(fp, VIDIOC_S_PARM, streamparm);
    if (ret < 0) {
        ALOGE("ERR(%s):VIDIOC_S_PARM failed\n", __func__);
        return ret;
    }
    return 0;
}

static int tv20_v4l2_s_crop(int fp, int offset_x, int offset_y, int width, int height)
{
    struct v4l2_crop crop;
    
    crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
    crop.c.left    = offset_x;
    crop.c.top = offset_y;
    crop.c.width = width;
    crop.c.height = height;
    
    int ret = ioctl(fp, VIDIOC_S_CROP, &crop);
    if (ret < 0) {
        ALOGE("ERR(%s):VIDIOC_S_PARM failed\n", __func__);
        return ret;
    }
    return 0;
}

static int tv20_v4l2_start_overlay(int fp)
{
    int ret, start = 1;
    
    ret = ioctl(fp, VIDIOC_OVERLAY, &start);
    if (ret < 0) {
        ALOGE("ERR(%s): VIDIOC_OVERLAY start failed\n", __func__);
        return ret;
    }
    
    return ret;
}

static int tv20_v4l2_stop_overlay(int fp)
{
    int ret, stop = 0;
    
    ret = ioctl(fp, VIDIOC_OVERLAY, &stop);
    if (ret < 0) {
        ALOGE("ERR(%s): VIDIOC_OVERLAY stop failed\n", __func__);
        return ret;
    }
    
    return ret;
}

static int tv20_v4l2_s_baseaddr(int fp, void *base_addr)
{
    int ret;

    ret = ioctl(fp, S5PTVFB_WIN_SET_ADDR, base_addr);
    if (ret < 0) {
        ALOGE("ERR(%s): VIDIOC_S_BASEADDR failed %d", __func__, ret);
        return ret;
    }

    return 0;
}

static int tv20_v4l2_s_position(int fp, int x, int y)
{
    int ret;
    struct s5ptvfb_user_window window;

    memset(&window, 0, sizeof(struct s5ptvfb_user_window));
    window.x = x;
    window.y = y;

    ret = ioctl(fp, S5PTVFB_WIN_POSITION, &window);
    if (ret < 0) {
        ALOGE("ERR(%s): VIDIOC_S_WIN_POSITION failed %d", __func__, ret);
        return ret;
    }

    return 0;
}

// ======================================================================
// Audio ioctls

static int tv20_v4l2_audio_enable(int fp)
{
    return ioctl(fp, VIDIOC_INIT_AUDIO, 1);
}

static int tv20_v4l2_audio_disable(int fp)
{
    return ioctl(fp, VIDIOC_INIT_AUDIO, 0);
}

static int tv20_v4l2_audio_mute(int fp)
{
    return ioctl(fp, VIDIOC_AV_MUTE, 1);
}

static int tv20_v4l2_audio_unmute(int fp)
{
    return ioctl(fp, VIDIOC_AV_MUTE, 0);
}

static int tv20_v4l2_audio_get_mute_state(int fp)
{
    return ioctl(fp, VIDIOC_G_AVMUTE, 0);
}

// ======================================================================
// Class which comunicate with kernel driver

SecHDMI::SecHDMI()
    : mTvOutFd(-1),
      mTvOutVFd(-1),
      mLcdFd(-1),
      mHdcpEnabled(0),
      mFlagConnected(false)
{
    ALOGV("%s", __func__);

    memset(&mParams, 0, sizeof(struct v4l2_streamparm));
    memset(&mFlagLayerEnable, 0, sizeof(bool) * S5P_TV_LAYER_MAX);

    int ret = ioctl(mTvOutFd, VIDIOC_HDCP_ENABLE, &mHdcpEnabled);
    ALOG_IF(ret);
}

SecHDMI::~SecHDMI()
{
    destroy();
}

/* static */
int SecHDMI::getCableStatus()
{
    int  fd = 0;
    char value[8] = {0};

    ALOGV("%s", __func__);

    fd = open("/sys/class/switch/h2w/state", O_RDWR);
    if(fd < 0) {
        goto close;
    }

    if(read(fd, &value, 8) <= 0) {
        goto close;
    }

close:
    close(fd);
    return strtol(value, NULL, 10);
}

const __u8* SecHDMI::getName(int index)
{
    ALOGV("%s", __func__);
    return tv20_v4l2_enum_output(mTvOutFd, index);
}

int SecHDMI::destroy()
{
    ALOGV("%s", __func__);

    if(mFlagConnected) {
        disconnect();
    }
    if(mTvOutFd > 0) {
        close(mTvOutFd);
        mTvOutFd = -1;
    }
    if(mFimc.dev_fd > 0) {
        fimc_close(&mFimc);
        mFimc.dev_fd = -1;
    }
    if (mLcdFd > 0) {
        fb_close(mLcdFd);
        mLcdFd = -1;
    }

    return 0;
}

int SecHDMI::startLayer(s5p_tv_layer layer)
{
    int ret;

    if (mFlagLayerEnable[layer]) {
        return 0;
    }

    switch (layer) {
    case S5P_TV_LAYER_VIDEO:
        if(mTvOutVFd < 0) {
            mTvOutVFd = open(TVOUT_DEV_V, O_RDWR);
            RETURN_IF(mTvOutVFd);
        }
        ret = tv20_v4l2_start_overlay(mTvOutVFd);
        RETURN_IF(ret);
        break;
    case S5P_TV_LAYER_GRAPHIC_0 :
        ret = ioctl(0/*fp_tvout_g0*/, FBIOBLANK, (void *)FB_BLANK_UNBLANK);
        RETURN_IF(ret);
        break;
    case S5P_TV_LAYER_GRAPHIC_1 :
        ret = ioctl(0/*fp_tvout_g1*/, FBIOBLANK, (void *)FB_BLANK_UNBLANK);
        RETURN_IF(ret);
        break;
    default :
        RETURN_IF(-1);
    }

    mFlagLayerEnable[layer] = true;

    return 0;
}

int SecHDMI::stopLayer(s5p_tv_layer layer)
{
    int ret;

    if (!mFlagLayerEnable[layer]) {
        return 0;
    }

    switch (layer) {
    case S5P_TV_LAYER_VIDEO:
        ret = tv20_v4l2_stop_overlay(mTvOutVFd);
        RETURN_IF(ret);
        close(mTvOutVFd);
        mTvOutVFd = -1;
        break;
    case S5P_TV_LAYER_GRAPHIC_0 :
        ret = ioctl(0/*fp_tvout_g0*/, FBIOBLANK, (void *)FB_BLANK_POWERDOWN);
        RETURN_IF(ret);
        break;
    case S5P_TV_LAYER_GRAPHIC_1 :
        ret = ioctl(0/*fp_tvout_g1*/, FBIOBLANK, (void *)FB_BLANK_POWERDOWN);
        RETURN_IF(ret);
        break;
    default :
        RETURN_IF(-1);
    }

    mFlagLayerEnable[layer] = false;

    return 0;
}

int SecHDMI::create(int width, int height)
{
    int ret, y_size;
    unsigned int addr;

    ALOGV("%s", __func__);

    mTvOutFd = open(TVOUT_DEV, O_RDWR);
    RETURN_IF(mTvOutFd);

    memset(&mFimc, 0, sizeof(s5p_fimc_t));
    mFimc.dev_fd = -1;
    ret = fimc_open(&mFimc, "/dev/video2");
    RETURN_IF(ret);

    ALOGV("query capabilities");
    ret = tv20_v4l2_querycap(mTvOutFd);
    RETURN_IF(ret);

    struct s5p_tv_standart_internal std =
            s5p_tv_standards[(int) S5P_TV_STD_PAL_BDGHI];

    ALOGV("searching for standart: %i", std.index);
    if(!tv20_v4l2_enum_standarts(mTvOutFd, std.index))
        return -1;

    ret = tv20_v4l2_s_std(mTvOutFd, std.value);
    RETURN_IF(ret);

    ALOGV("searching for output: %i", S5P_TV_OUTPUT_TYPE_COMPOSITE);
    if (!tv20_v4l2_enum_output(mTvOutFd, S5P_TV_OUTPUT_TYPE_COMPOSITE))
        return -1;

    ret = tv20_v4l2_s_output(mTvOutFd, S5P_TV_OUTPUT_TYPE_COMPOSITE);
    RETURN_IF(ret);

    struct v4l2_window_s5p_tvout* p =
            (struct v4l2_window_s5p_tvout*)&mParams.parm.raw_data;
    p->win.w.top = 0;
    p->win.w.left = 0;
    p->win.w.width = width;
    p->win.w.height = height;

    ALOGV("searching for format: %i", V4L2_PIX_FMT_NV12);
    ret = tv20_v4l2_enum_fmt(mTvOutFd, V4L2_PIX_FMT_NV12);
    RETURN_IF(ret);

    addr = (unsigned int) mFimc.out_buf.phys_addr;
    y_size = ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height));
    ret = tv20_v4l2_s_fmt(mTvOutFd, width, height, V4L2_PIX_FMT_NV12, 
                          (unsigned int) addr,
                          (unsigned int) addr + y_size);
    RETURN_IF(ret);

    return 0;
}

int SecHDMI::connect()
{
    int ret;

    ALOGV("%s", __func__);

    RETURN_IF(mTvOutFd);

    if(mFlagConnected) {
        return 0;
    }

#if 0
    ret = getCableStatus() <= 0 ? -1 : 0;
    RETURN_IF(ret);
#endif

    ret = tv20_v4l2_s_parm(mTvOutFd, &mParams);
    RETURN_IF(ret);

    ret = tv20_v4l2_streamon(mTvOutFd);
    RETURN_IF(ret);

#if 0
    ret = startLayer(S5P_TV_LAYER_VIDEO);
    RETURN_IF(ret);
#endif

    mFlagConnected = true;

    return 0;
}

int SecHDMI::disconnect()
{
    int ret;

    ALOGV("%s", __func__);

    RETURN_IF(mTvOutFd);

    if(!mFlagConnected) {
        return 0;
    }

    ret = tv20_v4l2_streamoff(mTvOutFd);
    RETURN_IF(ret);

#if 0
    ret = stopLayer(S5P_TV_LAYER_VIDEO);
    RETURN_IF(ret);
#endif

    mFlagConnected = false;

    return 0;
}

int SecHDMI::flush(int srcW, int srcH, int srcColorFormat,
                   unsigned int srcYAddr, unsigned int srcCbAddr, unsigned int srcCrAddr,
                   int dstX, int dstY,
                   int layer,
                   int num_of_hwc_layer)
{
    int ret;

#if 0
    usleep(1000 * 10);
#else
    sec_img         src_img;
    sec_img         dst_img;
    sec_rect        src_rect;
    sec_rect        dst_rect;
    unsigned int    phyAddr[3/*MAX_NUM_PLANES*/];

    if(!srcYAddr) {
        struct s3cfb_next_info fb_info;

        if (mLcdFd < 0) {
            mLcdFd = fb_open(0);
        }

        RETURN_IF(mLcdFd);

        ret = ioctl(mLcdFd, S3CFB_GET_CURR_FB_INFO, &fb_info);
        RETURN_IF(ret);

        srcYAddr = fb_info.phy_start_addr;
        srcCbAddr = srcYAddr;
    }

    memset(&src_img, 0, sizeof(src_img));
    memset(&dst_img, 0, sizeof(src_img));
    memset(&src_rect, 0, sizeof(src_rect));
    memset(&dst_rect, 0, sizeof(src_rect));
    memset(&phyAddr, 0, sizeof(int) * sizeof(phyAddr));

    phyAddr[0] = srcYAddr;
    phyAddr[1] = srcCbAddr;
    phyAddr[2] = srcCrAddr;

    src_img.w       = srcW;
    src_img.h       = srcH;
    src_img.format  = HAL_PIXEL_FORMAT_YCbCr_420_SP/*srcColorFormat*/;
    src_img.base    = 0;
    src_img.offset  = 0;
    src_img.mem_id  = 0;
    src_img.mem_type = FIMC_MEM_TYPE_PHYS;
    src_img.w       = (src_img.w + 15) & (~15);
    src_img.h       = (src_img.h + 1)  & (~1) ;

    src_rect.x = 0;
    src_rect.y = 0;
    src_rect.w = src_img.w;
    src_rect.h = src_img.h;

    struct v4l2_window_s5p_tvout* p =
        (struct v4l2_window_s5p_tvout*)&mParams.parm.raw_data;
    if (!p) {
        return -1;
    }

    dst_img.w       = p->win.w.width;
    dst_img.h       = p->win.w.height;
    dst_img.format  = HAL_PIXEL_FORMAT_YCbCr_420_SP;
    dst_img.base    = (unsigned int) mFimc.out_buf.phys_addr;
    dst_img.offset  = 0;
    dst_img.mem_id  = 0;
    dst_img.mem_type = FIMC_MEM_TYPE_PHYS;

    dst_rect.x = p->win.w.top;
    dst_rect.y = p->win.w.left;
    dst_rect.w = dst_img.w;
    dst_rect.h = dst_img.h;

    ALOGV("%s::sr_x %d sr_y %d sr_w %d sr_h %d dr_x %d dr_y %d dr_w %d dr_h %d ",
          __func__, src_rect.x, src_rect.y, src_rect.w, src_rect.h,
          dst_rect.x, dst_rect.y, dst_rect.w, dst_rect.h);

    ret = fimc_flush(&mFimc, &src_img, &src_rect, &dst_img, &dst_rect,
                     phyAddr, 0);
    RETURN_IF(ret);

/*
    struct fb_var_screeninfo var;
    var.xres = srcW;
    var.yres = srcH;
    var.xres_virtual = var.xres;
    var.yres_virtual = var.yres;
    var.xoffset = 0;
    var.yoffset = 0;
    var.width = srcW;
    var.height = srcH;
    var.activate = FB_ACTIVATE_FORCE;
    if (srcColorFormat == HAL_PIXEL_FORMAT_RGB_565) {
        var.bits_per_pixel = 16;
        var.transp.length = 0;
    }
    else {
        var.bits_per_pixel = 32;
        var.transp.length = 8;
    }

    ret = tv20_v4l2_s_baseaddr(mTvOutFd, (void *)srcYAddr);
    RETURN_IF(ret);

    ret = fb_put_vscreeninfo(mLcdFd, &var);
    RETURN_IF(ret);

    ret = tv20_v4l2_s_position(mTvOutFd, dstX, dstY);
    RETURN_IF(ret);
*/
#endif

    return 0;
}