1 /*
2  * Copyright (C) 2011 The Android Open Source Project
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 #include <errno.h>
18 #include <fcntl.h>
19 #include <linux/fb.h>
20 #include <pthread.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/ioctl.h>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <time.h>
29 #include <unistd.h>
30 
31 #include "common.h"
32 #include "device.h"
33 #include "ui.h"
34 #include "screen_ui.h"
35 
36 #define kFBDevice "/dev/graphics/fb0"
37 
38 #define FBIO_PSB_SET_RGBX       _IOWR('F', 0x42, struct fb_var_screeninfo)
39 #define FBIO_PSB_SET_RMODE      _IOWR('F', 0x43, struct fb_var_screeninfo)
40 
41 class FuguUI : public ScreenRecoveryUI {
42   public:
Init()43     void Init() override {
44         SetupDisplayMode();
45         ScreenRecoveryUI::Init();
46     }
47 
SetupDisplayMode()48     void SetupDisplayMode() {
49         printf("opening fb %s\n", kFBDevice);
50         int fb_dev = open(kFBDevice, O_RDWR);
51         if (fb_dev == -1) {
52             fprintf(stderr, "FAIL: failed to open \"%s\": %s\n", kFBDevice, strerror(errno));
53             return;
54         }
55 
56         struct fb_var_screeninfo current_mode;
57         if (ioctl(fb_dev, FBIO_PSB_SET_RMODE, &current_mode) == -1) {
58             fprintf(stderr, "FAIL: unable to set RGBX mode on display controller: %s\n",
59                     strerror(errno));
60             return;
61         }
62 
63         if (ioctl(fb_dev, FBIOGET_VSCREENINFO, &current_mode) == -1) {
64             fprintf(stderr, "FAIL: unable to get mode: %s\n", strerror(errno));
65             return;
66         }
67 
68         if (ioctl(fb_dev, FBIOBLANK, FB_BLANK_POWERDOWN) == -1) {
69             fprintf(stderr, "FAIL: unable to blank display: %s\n", strerror(errno));
70             return;
71         }
72 
73         current_mode.bits_per_pixel = 32;
74         current_mode.red.offset = 0;
75         current_mode.red.length = 8;
76         current_mode.green.offset = 8;
77         current_mode.green.length = 8;
78         current_mode.blue.offset = 16;
79         current_mode.blue.length = 8;
80 
81         if (ioctl(fb_dev, FBIOPUT_VSCREENINFO, &current_mode) == -1) {
82             fprintf(stderr, "FAIL: unable to set mode: %s\n", strerror(errno));
83             return;
84         }
85 
86         if (ioctl(fb_dev, FBIO_PSB_SET_RGBX, &current_mode) == -1) {
87             fprintf(stderr, "FAIL: unable to set RGBX mode on display controller: %s\n",
88                     strerror(errno));
89             return;
90         }
91 
92         if (ioctl(fb_dev, FBIOBLANK, FB_BLANK_UNBLANK) == -1) {
93             fprintf(stderr, "FAIL: unable to unblank display: %s\n", strerror(errno));
94             return;
95         }
96     }
97 };
98 
make_device()99 Device* make_device() {
100     return new Device(new FuguUI);
101 }
102