1*cf84ac9aSAndroid Build Coastguard Worker /*
2*cf84ac9aSAndroid Build Coastguard Worker * Check decoding of PTP_* commands of ioctl syscall.
3*cf84ac9aSAndroid Build Coastguard Worker *
4*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2018 Harsha Sharma <[email protected]>
5*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2018 The strace developers.
6*cf84ac9aSAndroid Build Coastguard Worker * All rights reserved.
7*cf84ac9aSAndroid Build Coastguard Worker *
8*cf84ac9aSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
9*cf84ac9aSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
10*cf84ac9aSAndroid Build Coastguard Worker * are met:
11*cf84ac9aSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
12*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
13*cf84ac9aSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
14*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
15*cf84ac9aSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
16*cf84ac9aSAndroid Build Coastguard Worker * 3. The name of the author may not be used to endorse or promote products
17*cf84ac9aSAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
18*cf84ac9aSAndroid Build Coastguard Worker *
19*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20*cf84ac9aSAndroid Build Coastguard Worker * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21*cf84ac9aSAndroid Build Coastguard Worker * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22*cf84ac9aSAndroid Build Coastguard Worker * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23*cf84ac9aSAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24*cf84ac9aSAndroid Build Coastguard Worker * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*cf84ac9aSAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*cf84ac9aSAndroid Build Coastguard Worker * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*cf84ac9aSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*cf84ac9aSAndroid Build Coastguard Worker */
30*cf84ac9aSAndroid Build Coastguard Worker
31*cf84ac9aSAndroid Build Coastguard Worker #include "tests.h"
32*cf84ac9aSAndroid Build Coastguard Worker
33*cf84ac9aSAndroid Build Coastguard Worker #ifdef HAVE_STRUCT_PTP_SYS_OFFSET
34*cf84ac9aSAndroid Build Coastguard Worker
35*cf84ac9aSAndroid Build Coastguard Worker #include <errno.h>
36*cf84ac9aSAndroid Build Coastguard Worker #include <fcntl.h>
37*cf84ac9aSAndroid Build Coastguard Worker #include <inttypes.h>
38*cf84ac9aSAndroid Build Coastguard Worker #include <stdio.h>
39*cf84ac9aSAndroid Build Coastguard Worker #include <stdlib.h>
40*cf84ac9aSAndroid Build Coastguard Worker #include <unistd.h>
41*cf84ac9aSAndroid Build Coastguard Worker #include <sys/ioctl.h>
42*cf84ac9aSAndroid Build Coastguard Worker #include <linux/ptp_clock.h>
43*cf84ac9aSAndroid Build Coastguard Worker
44*cf84ac9aSAndroid Build Coastguard Worker #include "xlat.h"
45*cf84ac9aSAndroid Build Coastguard Worker #include "xlat/ptp_flags_options.h"
46*cf84ac9aSAndroid Build Coastguard Worker
47*cf84ac9aSAndroid Build Coastguard Worker static void
test_no_device(void)48*cf84ac9aSAndroid Build Coastguard Worker test_no_device(void)
49*cf84ac9aSAndroid Build Coastguard Worker {
50*cf84ac9aSAndroid Build Coastguard Worker TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_clock_caps, caps);
51*cf84ac9aSAndroid Build Coastguard Worker fill_memory(caps, sizeof(*caps));
52*cf84ac9aSAndroid Build Coastguard Worker
53*cf84ac9aSAndroid Build Coastguard Worker TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_sys_offset, sysoff);
54*cf84ac9aSAndroid Build Coastguard Worker fill_memory(sysoff, sizeof(*sysoff));
55*cf84ac9aSAndroid Build Coastguard Worker
56*cf84ac9aSAndroid Build Coastguard Worker TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_extts_request, extts);
57*cf84ac9aSAndroid Build Coastguard Worker fill_memory(extts, sizeof(*extts));
58*cf84ac9aSAndroid Build Coastguard Worker
59*cf84ac9aSAndroid Build Coastguard Worker TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_perout_request, perout);
60*cf84ac9aSAndroid Build Coastguard Worker fill_memory(perout, sizeof(*perout));
61*cf84ac9aSAndroid Build Coastguard Worker
62*cf84ac9aSAndroid Build Coastguard Worker int saved_errno;
63*cf84ac9aSAndroid Build Coastguard Worker
64*cf84ac9aSAndroid Build Coastguard Worker /* PTP_CLOCK_GETCAPS */
65*cf84ac9aSAndroid Build Coastguard Worker ioctl(-1, PTP_CLOCK_GETCAPS, NULL);
66*cf84ac9aSAndroid Build Coastguard Worker printf("ioctl(-1, PTP_CLOCK_GETCAPS, NULL) = -1 EBADF (%m)\n");
67*cf84ac9aSAndroid Build Coastguard Worker ioctl(-1, PTP_CLOCK_GETCAPS, caps);
68*cf84ac9aSAndroid Build Coastguard Worker printf("ioctl(-1, PTP_CLOCK_GETCAPS, %p) = -1 EBADF (%m)\n", caps);
69*cf84ac9aSAndroid Build Coastguard Worker
70*cf84ac9aSAndroid Build Coastguard Worker /* PTP_SYS_OFFSET */
71*cf84ac9aSAndroid Build Coastguard Worker ioctl(-1, PTP_SYS_OFFSET, NULL);
72*cf84ac9aSAndroid Build Coastguard Worker printf("ioctl(-1, PTP_SYS_OFFSET, NULL) = -1 EBADF (%m)\n");
73*cf84ac9aSAndroid Build Coastguard Worker ioctl(-1, PTP_SYS_OFFSET, sysoff);
74*cf84ac9aSAndroid Build Coastguard Worker printf("ioctl(-1, PTP_SYS_OFFSET, {n_samples=%u}) = -1 EBADF (%m)\n",
75*cf84ac9aSAndroid Build Coastguard Worker sysoff->n_samples);
76*cf84ac9aSAndroid Build Coastguard Worker
77*cf84ac9aSAndroid Build Coastguard Worker /* PTP_ENABLE_PPS */
78*cf84ac9aSAndroid Build Coastguard Worker ioctl(-1, PTP_ENABLE_PPS, 0);
79*cf84ac9aSAndroid Build Coastguard Worker printf("ioctl(-1, PTP_ENABLE_PPS, 0) = -1 EBADF (%m)\n");
80*cf84ac9aSAndroid Build Coastguard Worker ioctl(-1, PTP_ENABLE_PPS, 1);
81*cf84ac9aSAndroid Build Coastguard Worker printf("ioctl(-1, PTP_ENABLE_PPS, 1) = -1 EBADF (%m)\n");
82*cf84ac9aSAndroid Build Coastguard Worker
83*cf84ac9aSAndroid Build Coastguard Worker /* PTP_EXTTS_REQUEST */
84*cf84ac9aSAndroid Build Coastguard Worker ioctl(-1, PTP_EXTTS_REQUEST, NULL);
85*cf84ac9aSAndroid Build Coastguard Worker printf("ioctl(-1, PTP_EXTTS_REQUEST, NULL) = -1 EBADF (%m)\n");
86*cf84ac9aSAndroid Build Coastguard Worker ioctl(-1, PTP_EXTTS_REQUEST, extts);
87*cf84ac9aSAndroid Build Coastguard Worker saved_errno = errno;
88*cf84ac9aSAndroid Build Coastguard Worker printf("ioctl(-1, PTP_EXTTS_REQUEST, {index=%d, flags=", extts->index);
89*cf84ac9aSAndroid Build Coastguard Worker printflags(ptp_flags_options, extts->flags, "PTP_???");
90*cf84ac9aSAndroid Build Coastguard Worker errno = saved_errno;
91*cf84ac9aSAndroid Build Coastguard Worker printf("}) = -1 EBADF (%m)\n");
92*cf84ac9aSAndroid Build Coastguard Worker
93*cf84ac9aSAndroid Build Coastguard Worker /* PTP_PEROUT_REQUEST */
94*cf84ac9aSAndroid Build Coastguard Worker ioctl(-1, PTP_PEROUT_REQUEST, NULL);
95*cf84ac9aSAndroid Build Coastguard Worker printf("ioctl(-1, PTP_PEROUT_REQUEST, NULL) = -1 EBADF (%m)\n");
96*cf84ac9aSAndroid Build Coastguard Worker ioctl(-1, PTP_PEROUT_REQUEST, perout);
97*cf84ac9aSAndroid Build Coastguard Worker saved_errno = errno;
98*cf84ac9aSAndroid Build Coastguard Worker printf("ioctl(-1, PTP_PEROUT_REQUEST, {start={sec=%" PRId64
99*cf84ac9aSAndroid Build Coastguard Worker ", nsec=%" PRIu32 "}, period={sec=%" PRId64 ", nsec=%" PRIu32 "}"
100*cf84ac9aSAndroid Build Coastguard Worker ", index=%d, flags=",
101*cf84ac9aSAndroid Build Coastguard Worker (int64_t) perout->start.sec, perout->start.nsec,
102*cf84ac9aSAndroid Build Coastguard Worker (int64_t)perout->period.sec, perout->period.nsec, perout->index);
103*cf84ac9aSAndroid Build Coastguard Worker printflags(ptp_flags_options, perout->flags, "PTP_???");
104*cf84ac9aSAndroid Build Coastguard Worker errno = saved_errno;
105*cf84ac9aSAndroid Build Coastguard Worker printf("}) = -1 EBADF (%m)\n");
106*cf84ac9aSAndroid Build Coastguard Worker
107*cf84ac9aSAndroid Build Coastguard Worker /* unrecognized */
108*cf84ac9aSAndroid Build Coastguard Worker ioctl(-1, _IOC(_IOC_READ, PTP_CLK_MAGIC, 0xff, 0xfe), 0);
109*cf84ac9aSAndroid Build Coastguard Worker printf("ioctl(-1, _IOC(_IOC_READ, %#x, 0xff, 0xfe), 0)"
110*cf84ac9aSAndroid Build Coastguard Worker " = -1 EBADF (%m)\n", PTP_CLK_MAGIC);
111*cf84ac9aSAndroid Build Coastguard Worker
112*cf84ac9aSAndroid Build Coastguard Worker const unsigned long arg = (unsigned long) 0xfacefeeddeadbeefULL;
113*cf84ac9aSAndroid Build Coastguard Worker ioctl(-1, _IOC(_IOC_WRITE, PTP_CLK_MAGIC, 0xfd, 0xfc), arg);
114*cf84ac9aSAndroid Build Coastguard Worker printf("ioctl(-1, _IOC(_IOC_WRITE, %#x, 0xfd, 0xfc), %#lx)"
115*cf84ac9aSAndroid Build Coastguard Worker " = -1 EBADF (%m)\n", PTP_CLK_MAGIC, arg);
116*cf84ac9aSAndroid Build Coastguard Worker }
117*cf84ac9aSAndroid Build Coastguard Worker
118*cf84ac9aSAndroid Build Coastguard Worker int
main(void)119*cf84ac9aSAndroid Build Coastguard Worker main(void)
120*cf84ac9aSAndroid Build Coastguard Worker {
121*cf84ac9aSAndroid Build Coastguard Worker test_no_device();
122*cf84ac9aSAndroid Build Coastguard Worker
123*cf84ac9aSAndroid Build Coastguard Worker puts("+++ exited with 0 +++");
124*cf84ac9aSAndroid Build Coastguard Worker return 0;
125*cf84ac9aSAndroid Build Coastguard Worker }
126*cf84ac9aSAndroid Build Coastguard Worker
127*cf84ac9aSAndroid Build Coastguard Worker #else
128*cf84ac9aSAndroid Build Coastguard Worker
129*cf84ac9aSAndroid Build Coastguard Worker SKIP_MAIN_UNDEFINED("HAVE_STRUCT_PTP_SYS_OFFSET")
130*cf84ac9aSAndroid Build Coastguard Worker
131*cf84ac9aSAndroid Build Coastguard Worker #endif /* HAVE_STRUCT_PTP_SYS_OFFSET */
132