fix VIDIOC_DQEVENT (v4l2) ioctl fallback for pre-5.6 kernels
[musl.git] / src / misc / ioctl.c
blob492828119aeaa8583ed7dd72ab67f89507e8aa9c
1 #include <sys/ioctl.h>
2 #include <stdarg.h>
3 #include <errno.h>
4 #include <time.h>
5 #include <sys/time.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include "syscall.h"
11 #define alignof(t) offsetof(struct { char c; t x; }, x)
13 #define W 1
14 #define R 2
15 #define WR 3
17 struct ioctl_compat_map {
18 int new_req, old_req;
19 unsigned char old_size, dir, force_align, noffs;
20 unsigned char offsets[8];
23 #define NINTH(a,b,c,d,e,f,g,h,i,...) i
24 #define COUNT(...) NINTH(__VA_ARGS__,8,7,6,5,4,3,2,1,0)
25 #define OFFS(...) COUNT(__VA_ARGS__), { __VA_ARGS__ }
27 /* yields a type for a struct with original size n, with a misaligned
28 * timeval/timespec expanded from 32- to 64-bit. for use with ioctl
29 * number producing macros; only size of result is meaningful. */
30 #define new_misaligned(n) struct { int i; time_t t; char c[(n)-4]; }
32 struct v4l2_event {
33 uint32_t a;
34 uint64_t b[8];
35 uint32_t c[2], ts[2], d[9];
38 static const struct ioctl_compat_map compat_map[] = {
39 { SIOCGSTAMP, SIOCGSTAMP_OLD, 8, R, 0, OFFS(0, 4) },
40 { SIOCGSTAMPNS, SIOCGSTAMPNS_OLD, 8, R, 0, OFFS(0, 4) },
42 /* SNDRV_TIMER_IOCTL_STATUS */
43 { _IOR('T', 0x14, char[96]), _IOR('T', 0x14, 88), 88, R, 0, OFFS(0,4) },
45 /* SNDRV_PCM_IOCTL_STATUS[_EXT] */
46 { _IOR('A', 0x20, char[128]), _IOR('A', 0x20, char[108]), 108, R, 1, OFFS(4,8,12,16,52,56,60,64) },
47 { _IOWR('A', 0x24, char[128]), _IOWR('A', 0x24, char[108]), 108, WR, 1, OFFS(4,8,12,16,52,56,60,64) },
49 /* SNDRV_RAWMIDI_IOCTL_STATUS */
50 { _IOWR('W', 0x20, char[48]), _IOWR('W', 0x20, char[36]), 36, WR, 1, OFFS(4,8) },
52 /* SNDRV_PCM_IOCTL_SYNC_PTR - with 3 subtables */
53 { _IOWR('A', 0x23, char[136]), _IOWR('A', 0x23, char[132]), 0, WR, 1, 0 },
54 { 0, 0, 4, WR, 1, 0 }, /* snd_pcm_sync_ptr (flags only) */
55 { 0, 0, 32, WR, 1, OFFS(8,12,16,24,28) }, /* snd_pcm_mmap_status */
56 { 0, 0, 8, WR, 1, OFFS(0,4) }, /* snd_pcm_mmap_control */
58 /* VIDIOC_QUERYBUF, VIDIOC_QBUF, VIDIOC_DQBUF, VIDIOC_PREPARE_BUF */
59 { _IOWR('V', 9, new_misaligned(68)), _IOWR('V', 9, char[68]), 68, WR, 1, OFFS(20, 24) },
60 { _IOWR('V', 15, new_misaligned(68)), _IOWR('V', 15, char[68]), 68, WR, 1, OFFS(20, 24) },
61 { _IOWR('V', 17, new_misaligned(68)), _IOWR('V', 17, char[68]), 68, WR, 1, OFFS(20, 24) },
62 { _IOWR('V', 93, new_misaligned(68)), _IOWR('V', 93, char[68]), 68, WR, 1, OFFS(20, 24) },
64 /* VIDIOC_DQEVENT */
65 { _IOR('V', 89, new_misaligned(120)), _IOR('V', 89, struct v4l2_event), sizeof(struct v4l2_event),
66 R, 0, OFFS(offsetof(struct v4l2_event, ts[0]), offsetof(struct v4l2_event, ts[1])) },
68 /* VIDIOC_OMAP3ISP_STAT_REQ */
69 { _IOWR('V', 192+6, char[32]), _IOWR('V', 192+6, char[24]), 22, WR, 0, OFFS(0,4) },
71 /* PPPIOCGIDLE */
72 { _IOR('t', 63, char[16]), _IOR('t', 63, char[8]), 8, R, 0, OFFS(0,4) },
74 /* PPGETTIME, PPSETTIME */
75 { _IOR('p', 0x95, char[16]), _IOR('p', 0x95, char[8]), 8, R, 0, OFFS(0,4) },
76 { _IOW('p', 0x96, char[16]), _IOW('p', 0x96, char[8]), 8, W, 0, OFFS(0,4) },
78 /* LPSETTIMEOUT */
79 { _IOW(0x6, 0xf, char[16]), 0x060f, 8, W, 0, OFFS(0,4) },
82 static void convert_ioctl_struct(const struct ioctl_compat_map *map, char *old, char *new, int dir)
84 int new_offset = 0;
85 int old_offset = 0;
86 int old_size = map->old_size;
87 if (!(dir & map->dir)) return;
88 if (!map->old_size) {
89 /* offsets hard-coded for SNDRV_PCM_IOCTL_SYNC_PTR;
90 * if another exception appears this needs changing. */
91 convert_ioctl_struct(map+1, old, new, dir);
92 convert_ioctl_struct(map+2, old+4, new+8, dir);
93 convert_ioctl_struct(map+3, old+68, new+72, dir);
94 return;
96 for (int i=0; i < map->noffs; i++) {
97 int ts_offset = map->offsets[i];
98 int len = ts_offset-old_offset;
99 if (dir==W) memcpy(old+old_offset, new+new_offset, len);
100 else memcpy(new+new_offset, old+old_offset, len);
101 new_offset += len;
102 old_offset += len;
103 long long new_ts;
104 long old_ts;
105 int align = map->force_align ? sizeof(time_t) : alignof(time_t);
106 new_offset += (align-1) & -new_offset;
107 if (dir==W) {
108 memcpy(&new_ts, new+new_offset, sizeof new_ts);
109 old_ts = new_ts;
110 memcpy(old+old_offset, &old_ts, sizeof old_ts);
111 } else {
112 memcpy(&old_ts, old+old_offset, sizeof old_ts);
113 new_ts = old_ts;
114 memcpy(new+new_offset, &new_ts, sizeof new_ts);
116 new_offset += sizeof new_ts;
117 old_offset += sizeof old_ts;
119 if (dir==W) memcpy(old+old_offset, new+new_offset, old_size-old_offset);
120 else memcpy(new+new_offset, old+old_offset, old_size-old_offset);
123 int ioctl(int fd, int req, ...)
125 void *arg;
126 va_list ap;
127 va_start(ap, req);
128 arg = va_arg(ap, void *);
129 va_end(ap);
130 int r = __syscall(SYS_ioctl, fd, req, arg);
131 if (SIOCGSTAMP != SIOCGSTAMP_OLD && req && r==-ENOTTY) {
132 for (int i=0; i<sizeof compat_map/sizeof *compat_map; i++) {
133 if (compat_map[i].new_req != req) continue;
134 union {
135 long long align;
136 char buf[256];
137 } u;
138 convert_ioctl_struct(&compat_map[i], u.buf, arg, W);
139 r = __syscall(SYS_ioctl, fd, compat_map[i].old_req, u.buf);
140 if (r<0) break;
141 convert_ioctl_struct(&compat_map[i], u.buf, arg, R);
142 break;
145 return __syscall_ret(r);