1570af302Sopenharmony_ci#include <sys/ioctl.h>
2570af302Sopenharmony_ci#include <stdarg.h>
3570af302Sopenharmony_ci#include <errno.h>
4570af302Sopenharmony_ci#include <time.h>
5570af302Sopenharmony_ci#include <sys/time.h>
6570af302Sopenharmony_ci#include <stddef.h>
7570af302Sopenharmony_ci#include <stdint.h>
8570af302Sopenharmony_ci#include <string.h>
9570af302Sopenharmony_ci#include <endian.h>
10570af302Sopenharmony_ci#include "syscall.h"
11570af302Sopenharmony_ci
12570af302Sopenharmony_ci#define alignof(t) offsetof(struct { char c; t x; }, x)
13570af302Sopenharmony_ci
14570af302Sopenharmony_ci#define W 1
15570af302Sopenharmony_ci#define R 2
16570af302Sopenharmony_ci#define WR 3
17570af302Sopenharmony_ci
18570af302Sopenharmony_cistruct ioctl_compat_map {
19570af302Sopenharmony_ci	int new_req, old_req;
20570af302Sopenharmony_ci	unsigned char old_size, dir, force_align, noffs;
21570af302Sopenharmony_ci	unsigned char offsets[8];
22570af302Sopenharmony_ci};
23570af302Sopenharmony_ci
24570af302Sopenharmony_ci#define NINTH(a,b,c,d,e,f,g,h,i,...) i
25570af302Sopenharmony_ci#define COUNT(...) NINTH(__VA_ARGS__,8,7,6,5,4,3,2,1,0)
26570af302Sopenharmony_ci#define OFFS(...) COUNT(__VA_ARGS__), { __VA_ARGS__ }
27570af302Sopenharmony_ci
28570af302Sopenharmony_ci/* yields a type for a struct with original size n, with a misaligned
29570af302Sopenharmony_ci * timeval/timespec expanded from 32- to 64-bit. for use with ioctl
30570af302Sopenharmony_ci * number producing macros; only size of result is meaningful. */
31570af302Sopenharmony_ci#define new_misaligned(n) struct { int i; time_t t; char c[(n)-4]; }
32570af302Sopenharmony_ci
33570af302Sopenharmony_cistruct v4l2_event {
34570af302Sopenharmony_ci	uint32_t a;
35570af302Sopenharmony_ci	uint64_t b[8];
36570af302Sopenharmony_ci	uint32_t c[2], ts[2], d[9];
37570af302Sopenharmony_ci};
38570af302Sopenharmony_ci
39570af302Sopenharmony_cistatic const struct ioctl_compat_map compat_map[] = {
40570af302Sopenharmony_ci	{ SIOCGSTAMP, SIOCGSTAMP_OLD, 8, R, 0, OFFS(0, 4) },
41570af302Sopenharmony_ci	{ SIOCGSTAMPNS, SIOCGSTAMPNS_OLD, 8, R, 0, OFFS(0, 4) },
42570af302Sopenharmony_ci
43570af302Sopenharmony_ci	/* SNDRV_TIMER_IOCTL_STATUS */
44570af302Sopenharmony_ci	{ _IOR('T', 0x14, char[96]), _IOR('T', 0x14, 88), 88, R, 0, OFFS(0,4) },
45570af302Sopenharmony_ci
46570af302Sopenharmony_ci	/* SNDRV_PCM_IOCTL_STATUS[_EXT] */
47570af302Sopenharmony_ci	{ _IOR('A', 0x20, char[128]), _IOR('A', 0x20, char[108]), 108, R, 1, OFFS(4,8,12,16,52,56,60,64) },
48570af302Sopenharmony_ci	{ _IOWR('A', 0x24, char[128]), _IOWR('A', 0x24, char[108]), 108, WR, 1, OFFS(4,8,12,16,52,56,60,64) },
49570af302Sopenharmony_ci
50570af302Sopenharmony_ci	/* SNDRV_RAWMIDI_IOCTL_STATUS */
51570af302Sopenharmony_ci	{ _IOWR('W', 0x20, char[48]), _IOWR('W', 0x20, char[36]), 36, WR, 1, OFFS(4,8) },
52570af302Sopenharmony_ci
53570af302Sopenharmony_ci	/* SNDRV_PCM_IOCTL_SYNC_PTR - with 3 subtables */
54570af302Sopenharmony_ci	{ _IOWR('A', 0x23, char[136]), _IOWR('A', 0x23, char[132]), 0, WR, 1, 0 },
55570af302Sopenharmony_ci	{ 0, 0, 4, WR, 1, 0 }, /* snd_pcm_sync_ptr (flags only) */
56570af302Sopenharmony_ci	{ 0, 0, 32, WR, 1, OFFS(8,12,16,24,28) }, /* snd_pcm_mmap_status */
57570af302Sopenharmony_ci	{ 0, 0, 4, WR, 1, 0 }, /* snd_pcm_mmap_control (each member) */
58570af302Sopenharmony_ci
59570af302Sopenharmony_ci	/* VIDIOC_QUERYBUF, VIDIOC_QBUF, VIDIOC_DQBUF, VIDIOC_PREPARE_BUF */
60570af302Sopenharmony_ci	{ _IOWR('V',  9, new_misaligned(68)), _IOWR('V',  9, char[68]), 68, WR, 1, OFFS(20, 24) },
61570af302Sopenharmony_ci	{ _IOWR('V', 15, new_misaligned(68)), _IOWR('V', 15, char[68]), 68, WR, 1, OFFS(20, 24) },
62570af302Sopenharmony_ci	{ _IOWR('V', 17, new_misaligned(68)), _IOWR('V', 17, char[68]), 68, WR, 1, OFFS(20, 24) },
63570af302Sopenharmony_ci	{ _IOWR('V', 93, new_misaligned(68)), _IOWR('V', 93, char[68]), 68, WR, 1, OFFS(20, 24) },
64570af302Sopenharmony_ci
65570af302Sopenharmony_ci	/* VIDIOC_DQEVENT */
66570af302Sopenharmony_ci	{ _IOR('V', 89, new_misaligned(120)), _IOR('V', 89, struct v4l2_event), sizeof(struct v4l2_event),
67570af302Sopenharmony_ci	  R, 0, OFFS(offsetof(struct v4l2_event, ts[0]), offsetof(struct v4l2_event, ts[1])) },
68570af302Sopenharmony_ci
69570af302Sopenharmony_ci	/* VIDIOC_OMAP3ISP_STAT_REQ */
70570af302Sopenharmony_ci	{ _IOWR('V', 192+6, char[32]), _IOWR('V', 192+6, char[24]), 22, WR, 0, OFFS(0,4) },
71570af302Sopenharmony_ci
72570af302Sopenharmony_ci	/* PPPIOCGIDLE */
73570af302Sopenharmony_ci	{ _IOR('t', 63, char[16]), _IOR('t', 63, char[8]), 8, R, 0, OFFS(0,4) },
74570af302Sopenharmony_ci
75570af302Sopenharmony_ci	/* PPGETTIME, PPSETTIME */
76570af302Sopenharmony_ci	{ _IOR('p', 0x95, char[16]), _IOR('p', 0x95, char[8]), 8, R, 0, OFFS(0,4) },
77570af302Sopenharmony_ci	{ _IOW('p', 0x96, char[16]), _IOW('p', 0x96, char[8]), 8, W, 0, OFFS(0,4) },
78570af302Sopenharmony_ci
79570af302Sopenharmony_ci	/* LPSETTIMEOUT */
80570af302Sopenharmony_ci	{ _IOW(0x6, 0xf, char[16]), 0x060f, 8, W, 0, OFFS(0,4) },
81570af302Sopenharmony_ci};
82570af302Sopenharmony_ci
83570af302Sopenharmony_cistatic void convert_ioctl_struct(const struct ioctl_compat_map *map, char *old, char *new, int dir)
84570af302Sopenharmony_ci{
85570af302Sopenharmony_ci	int new_offset = 0;
86570af302Sopenharmony_ci	int old_offset = 0;
87570af302Sopenharmony_ci	int old_size = map->old_size;
88570af302Sopenharmony_ci	if (!(dir & map->dir)) return;
89570af302Sopenharmony_ci	if (!map->old_size) {
90570af302Sopenharmony_ci		/* offsets hard-coded for SNDRV_PCM_IOCTL_SYNC_PTR;
91570af302Sopenharmony_ci		 * if another exception appears this needs changing. */
92570af302Sopenharmony_ci		convert_ioctl_struct(map+1, old, new, dir);
93570af302Sopenharmony_ci		convert_ioctl_struct(map+2, old+4, new+8, dir);
94570af302Sopenharmony_ci		/* snd_pcm_mmap_control, special-cased due to kernel
95570af302Sopenharmony_ci		 * type definition having been botched. */
96570af302Sopenharmony_ci		int adj = BYTE_ORDER==BIG_ENDIAN ? 4 : 0;
97570af302Sopenharmony_ci		convert_ioctl_struct(map+3, old+68, new+72+adj, dir);
98570af302Sopenharmony_ci		convert_ioctl_struct(map+3, old+72, new+76+3*adj, dir);
99570af302Sopenharmony_ci		return;
100570af302Sopenharmony_ci	}
101570af302Sopenharmony_ci	for (int i=0; i < map->noffs; i++) {
102570af302Sopenharmony_ci		int ts_offset = map->offsets[i];
103570af302Sopenharmony_ci		int len = ts_offset-old_offset;
104570af302Sopenharmony_ci		if (dir==W) memcpy(old+old_offset, new+new_offset, len);
105570af302Sopenharmony_ci		else memcpy(new+new_offset, old+old_offset, len);
106570af302Sopenharmony_ci		new_offset += len;
107570af302Sopenharmony_ci		old_offset += len;
108570af302Sopenharmony_ci		long long new_ts;
109570af302Sopenharmony_ci		long old_ts;
110570af302Sopenharmony_ci		int align = map->force_align ? sizeof(time_t) : alignof(time_t);
111570af302Sopenharmony_ci		new_offset += (align-1) & -new_offset;
112570af302Sopenharmony_ci		if (dir==W) {
113570af302Sopenharmony_ci			memcpy(&new_ts, new+new_offset, sizeof new_ts);
114570af302Sopenharmony_ci			old_ts = new_ts;
115570af302Sopenharmony_ci			memcpy(old+old_offset, &old_ts, sizeof old_ts);
116570af302Sopenharmony_ci		} else {
117570af302Sopenharmony_ci			memcpy(&old_ts, old+old_offset, sizeof old_ts);
118570af302Sopenharmony_ci			new_ts = old_ts;
119570af302Sopenharmony_ci			memcpy(new+new_offset, &new_ts, sizeof new_ts);
120570af302Sopenharmony_ci		}
121570af302Sopenharmony_ci		new_offset += sizeof new_ts;
122570af302Sopenharmony_ci		old_offset += sizeof old_ts;
123570af302Sopenharmony_ci	}
124570af302Sopenharmony_ci	if (dir==W) memcpy(old+old_offset, new+new_offset, old_size-old_offset);
125570af302Sopenharmony_ci	else memcpy(new+new_offset, old+old_offset, old_size-old_offset);
126570af302Sopenharmony_ci}
127570af302Sopenharmony_ci
128570af302Sopenharmony_ciint ioctl(int fd, int req, ...)
129570af302Sopenharmony_ci{
130570af302Sopenharmony_ci	void *arg;
131570af302Sopenharmony_ci	va_list ap;
132570af302Sopenharmony_ci	va_start(ap, req);
133570af302Sopenharmony_ci	arg = va_arg(ap, void *);
134570af302Sopenharmony_ci	va_end(ap);
135570af302Sopenharmony_ci	int r = __syscall(SYS_ioctl, fd, req, arg);
136570af302Sopenharmony_ci	if (SIOCGSTAMP != SIOCGSTAMP_OLD && req && r==-ENOTTY) {
137570af302Sopenharmony_ci		for (int i=0; i<sizeof compat_map/sizeof *compat_map; i++) {
138570af302Sopenharmony_ci			if (compat_map[i].new_req != req) continue;
139570af302Sopenharmony_ci			union {
140570af302Sopenharmony_ci				long long align;
141570af302Sopenharmony_ci				char buf[256];
142570af302Sopenharmony_ci			} u;
143570af302Sopenharmony_ci			convert_ioctl_struct(&compat_map[i], u.buf, arg, W);
144570af302Sopenharmony_ci			r = __syscall(SYS_ioctl, fd, compat_map[i].old_req, u.buf);
145570af302Sopenharmony_ci			if (r<0) break;
146570af302Sopenharmony_ci			convert_ioctl_struct(&compat_map[i], u.buf, arg, R);
147570af302Sopenharmony_ci			break;
148570af302Sopenharmony_ci		}
149570af302Sopenharmony_ci	}
150570af302Sopenharmony_ci	return __syscall_ret(r);
151570af302Sopenharmony_ci}
152