-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioctl.c
267 lines (221 loc) · 7.55 KB
/
ioctl.c
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
#include <fcntl.h> // open
#include <unistd.h> // exit, getpagesize
#include <sys/ioctl.h> // ioctl
#include <stdio.h> // printf
#include <stdlib.h> // exit
#include <sys/mman.h> // mmap, munmap
#include <memory.h> // memset
#include <signal.h>
#define UINT32 unsigned int
#define LOG(...) fprintf(stderr, __VA_ARGS__)
#include "vc088x_cif_drv.h"
#include "android_pmem.h"
#define DEVICE_FILE_NAME "/dev/cif"
#define PMEM_VDEC_FILE_NAME "/dev/pmem-vdec"
// Sensor mode selector
#if 0
// Mode 1 (photo only?)
#define SENSOR_MODE 1
#define FRAME_WIDTH 1280
#define FRAME_HEIGHT 1024
#else
// Mode 2 (video?)
#define SENSOR_MODE 0
#define FRAME_WIDTH 640
#define FRAME_HEIGHT 480
#endif
// Capture mode selector
#if 0
// For preview
#define IO_CFG_REQUEST V8CIF_PREPATH_CFG
#define IO_FRAME_REQUEST V8CIF_PREPATH_GETFRM
#else
// For capture (quicker than preview)
#define IO_CFG_REQUEST V8CIF_CAPPATH_CFG
#define IO_FRAME_REQUEST V8CIF_RECORD_GETFRM
#endif
// Manufacturer's way
//#define FRAME_SIZE_BYTES (3 * FRAME_WIDTH * FRAME_HEIGHT >> 1)
#define FRAME_SIZE_BYTES (2 * FRAME_WIDTH * FRAME_HEIGHT)
void ioctl_getinfo(int file_desc)
{
int ret_val;
V8CIFSNRINFO sinfo;
sinfo.type = 1;
ret_val = ioctl(file_desc, V8CIF_SNR_GETINFO, &sinfo);
if (ret_val < 0) {
LOG("ioctl req getinfo failed:%d\n", ret_val);
exit(-1);
}
LOG("Command succeeded, info:\n");
LOG(" totalItems: %d\n", sinfo.totalItems);
LOG(" type: %d", sinfo.type);
LOG(" jpegSupported: %d\n", sinfo.jpegSupported);
LOG("\n Modes:\n");
int i;
if (sinfo.totalItems > SNR_ITEM_NUM) {
sinfo.totalItems = SNR_ITEM_NUM;
}
for (i = 0; i < sinfo.totalItems; ++i) {
LOG(" %d x %d\n", sinfo.width[i], sinfo.height[i]);
}
}
void ioctl_power(int file_desc, int on)
{
int ret_val;
if (on) {
ret_val = ioctl(file_desc, V8CIF_SNR_PWRON, 1); // 1 means "front sensor", doesn't really used by Viewpad 10e driver
} else {
ret_val = ioctl(file_desc, V8CIF_SNR_PWRDOWN, 1);
}
if (ret_val < 0) {
LOG("ioctl req power failed:%d\n", ret_val);
exit(-1);
}
}
int g_runLoop = 1; // When this goes 0, program should stop capture loop
void ioctl_grab(int file_desc, unsigned char *framebuf, UINT32 hwoffset)
{
int ret_val;
LOG("Setting resolution...\n");
ret_val = ioctl(file_desc, V8CIF_SNR_CFG, SENSOR_MODE); // Set resolution (and fps?)
if (ret_val < 0) {
LOG("ioctl set resolution failed:%d\n", ret_val);
exit(-1);
}
LOG("Configuring grab...\n");
V8CIFPATHINFO cpath;
cpath.dst_type = 0;
cpath.dst_width = FRAME_WIDTH;
cpath.dst_height = FRAME_HEIGHT;
// Looks like these values are ignored by driver, setting these to 0 to crash if I'm wrong
cpath.dst_addr[0] = hwoffset;
cpath.dst_addr[1] = hwoffset;
cpath.dst_addr[2] = hwoffset;
ret_val = ioctl(file_desc, IO_CFG_REQUEST, &cpath);
if (ret_val < 0) {
LOG("ioctl grab failed:%d\n", ret_val);
exit(-1);
}
/*ret_val = ioctl(file_desc, V8CIF_SETPREVIEWSTOP, 0);
if (ret_val < 0) {
LOG("ioctl previewstop failed:%d\n", ret_val);
exit(-1);
}*/
LOG("Begin cycle...\n");
while (g_runLoop) {
V8CIFFRMINFO frinfo;
frinfo.dst_addr_y = (UINT32)hwoffset;
frinfo.dst_addr_uv = (UINT32)hwoffset + FRAME_WIDTH * FRAME_HEIGHT;
frinfo.cap_mode = CAP_NORMAL; // Manufacturer uses 0
ret_val = ioctl(file_desc, IO_FRAME_REQUEST, &frinfo);
if (ret_val < 0) {
LOG("ioctl grab failed:%d\n", ret_val);
exit(-1);
}
// Stream to stdout
fwrite(framebuf, 1, FRAME_SIZE_BYTES, stdout);
//usleep(200000); // Pause between frames, removed as capturing a frames takes long time already
}
LOG("Finished cycle.\n");
}
int opendev(const char *devname, int mode)
{
int file_desc;
file_desc = open(devname, 2, mode); // [XXX] Manufacturer's code uses flag 2, but why?!
if (file_desc < 0) {
LOG("Can't open device file: %s\n", devname);
exit(-1);
}
return file_desc;
}
// Had to reverse-engineer this one from "libcamera.so", no manufacturer source code available
/*
Some hints and theory on using this mechanism:
http://www.phonesdevelopers.info/1722480/
https://android.googlesource.com/platform/frameworks/native/+/252778f/libs/binder/MemoryHeapPmem.cpp
*/
void *PhyMemAlloc(int pmem_dev, struct pmem_region *sub)
{
const size_t pagesize = getpagesize();
LOG("[D] Page size: %d\n", pagesize);
int size = sub->len;
size = (size + pagesize-1) & ~(pagesize-1);
void *ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, pmem_dev, 0); // prot == 3, flags == 1
LOG("[D] ioctl command packet (should be 0x40047001): %08X\n", PMEM_MAP);
// Should be PMEM_MAP, but manufacturer uses "read" version: 0x40047001, and a weird argument
//int err = ioctl(pmem_dev, PMEM_MAP, sub);
UINT32 hwptr;
int err = ioctl(pmem_dev, 0x40047001, &hwptr);
sub->offset = hwptr;
if (err < 0) {
LOG("PMEM_MAP failed (%s), mFD=%d, sub.offset=%lu, sub.size=%lu",
err, pmem_dev, sub->offset, sub->len);
exit(-1);
} else {
LOG("PMEM_MAP success, sub.offset=%lu, sub.size=%lu\n",
sub->offset, sub->len);
}
LOG("Allocated userland ptr: %08X, hw offset: %08X\n", ptr, hwptr);
if (ptr) {
LOG("Wiping %d bytes of memory...\n", sub->len);
memset(ptr, 0, size);
}
return ptr;
}
void PhyMemFree(int pmem_dev, void *ptr, struct pmem_region *sub)
{
if (munmap(ptr, sub->len) > 0) {
LOG("Cannot munmap ptr: %08X, size: %d\n", ptr, sub->len);
}
// Manufacturer doesn't use this part
/*int err = ioctl(pmem_dev, PMEM_UNMAP, &sub);
if (err < 0) {
LOG("PMEM_UNMAP failed (%d), mFD=%d, sub.offset=%lu, sub.size=%lu\n",
err, pmem_dev, sub->offset, sub->len);
exit(-1);
}*/
}
void PhyMemInfo(int pmem_dev)
{
int ret_val, rsize;
ret_val = ioctl(pmem_dev, PMEM_GET_TOTAL_SIZE, &rsize);
if (ret_val < 0) {
LOG("ioctl PMEM_GET_TOTAL_SIZE failed: %d\n", ret_val);
exit(-1);
}
LOG("Total physical memory size: %d, retval: %d\n", rsize, ret_val);
}
void sig_handler(int signum)
{
g_runLoop = 0;
LOG("Stopping...\n");
}
void main()
{
signal(SIGINT, sig_handler);
signal(SIGPIPE, sig_handler);
int camera_dev, pmem_dev, ret_val;
camera_dev = opendev(DEVICE_FILE_NAME, O_RDONLY);
pmem_dev = opendev(PMEM_VDEC_FILE_NAME, O_RDONLY);
// Poke physmem mapper
LOG("Mapping memory...\n");
struct pmem_region sub = { 0, FRAME_SIZE_BYTES };
void *ptr = PhyMemAlloc(pmem_dev, &sub);
PhyMemInfo(pmem_dev);
LOG("[D] ioctl packet codes:\n");
LOG("\tV8CIF_PREPATH_GETFRM our: %08X, manuf: %08X\n", V8CIF_PREPATH_GETFRM, 0x800C6307);
LOG("\tV8CIF_CAPPATH_GETFRM our: %08X, manuf: %08X\n", V8CIF_CAPPATH_GETFRM, 0x800C6308);
LOG("\tV8CIF_RECORD_GETFRM our: %08X, manuf: %08X\n", V8CIF_RECORD_GETFRM, 0x800C630E);
LOG("\tV8CIF_PREPATH_CFG our: %08X, manuf: %08X\n", V8CIF_PREPATH_CFG, 0x0);
LOG("\tV8CIF_CAPPATH_CFG our: %08X, manuf: %08X\n", V8CIF_CAPPATH_CFG, 0x0);
// Grab some
ioctl_power(camera_dev, 1); // Turn on
ioctl_getinfo(camera_dev); // Get camera info
ioctl_grab(camera_dev, ptr, sub.offset);
ioctl_power(camera_dev, 0); // Turn off
PhyMemFree(pmem_dev, ptr, &sub);
LOG("Closing...\n");
close(camera_dev);
close(pmem_dev);
}