Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

testptp: Add option to open PHC in readonly mode

PTP Hardware Clocks no longer require WRITE permission to perform
readonly operations, such as listing device capabilities or listening to
EXTTS events once they have been enabled by a process with WRITE
permissions.

Add '-r' option to testptp to open the PHC in readonly mode instead of
the default read-write mode. Skip enabling EXTTS if readonly mode is
requested.

Acked-by: Richard Cochran <richardcochran@gmail.com>
Reviewed-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
Signed-off-by: Wojtek Wasko <wwasko@nvidia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Wojtek Wasko and committed by
David S. Miller
76868642 b4e53b15

+23 -14
+23 -14
tools/testing/selftests/ptp/testptp.c
··· 140 140 " -H val set output phase to 'val' nanoseconds (requires -p)\n" 141 141 " -w val set output pulse width to 'val' nanoseconds (requires -p)\n" 142 142 " -P val enable or disable (val=1|0) the system clock PPS\n" 143 + " -r open the ptp clock in readonly mode\n" 143 144 " -s set the ptp clock time from the system time\n" 144 145 " -S set the system time from the ptp clock time\n" 145 146 " -t val shift the ptp clock time by 'val' seconds\n" ··· 189 188 int pin_index = -1, pin_func; 190 189 int pps = -1; 191 190 int seconds = 0; 191 + int readonly = 0; 192 192 int settime = 0; 193 193 int channel = -1; 194 194 clockid_t ext_clockid = CLOCK_REALTIME; ··· 202 200 203 201 progname = strrchr(argv[0], '/'); 204 202 progname = progname ? 1+progname : argv[0]; 205 - while (EOF != (c = getopt(argc, argv, "cd:e:f:F:ghH:i:k:lL:n:o:p:P:sSt:T:w:x:Xy:z"))) { 203 + while (EOF != (c = getopt(argc, argv, "cd:e:f:F:ghH:i:k:lL:n:o:p:P:rsSt:T:w:x:Xy:z"))) { 206 204 switch (c) { 207 205 case 'c': 208 206 capabilities = 1; ··· 253 251 break; 254 252 case 'P': 255 253 pps = atoi(optarg); 254 + break; 255 + case 'r': 256 + readonly = 1; 256 257 break; 257 258 case 's': 258 259 settime = 1; ··· 313 308 } 314 309 } 315 310 316 - fd = open(device, O_RDWR); 311 + fd = open(device, readonly ? O_RDONLY : O_RDWR); 317 312 if (fd < 0) { 318 313 fprintf(stderr, "opening %s: %s\n", device, strerror(errno)); 319 314 return -1; ··· 441 436 } 442 437 443 438 if (extts) { 444 - memset(&extts_request, 0, sizeof(extts_request)); 445 - extts_request.index = index; 446 - extts_request.flags = PTP_ENABLE_FEATURE; 447 - if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) { 448 - perror("PTP_EXTTS_REQUEST"); 449 - extts = 0; 450 - } else { 451 - puts("external time stamp request okay"); 439 + if (!readonly) { 440 + memset(&extts_request, 0, sizeof(extts_request)); 441 + extts_request.index = index; 442 + extts_request.flags = PTP_ENABLE_FEATURE; 443 + if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) { 444 + perror("PTP_EXTTS_REQUEST"); 445 + extts = 0; 446 + } else { 447 + puts("external time stamp request okay"); 448 + } 452 449 } 453 450 for (; extts; extts--) { 454 451 cnt = read(fd, &event, sizeof(event)); ··· 462 455 event.t.sec, event.t.nsec); 463 456 fflush(stdout); 464 457 } 465 - /* Disable the feature again. */ 466 - extts_request.flags = 0; 467 - if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) { 468 - perror("PTP_EXTTS_REQUEST"); 458 + if (!readonly) { 459 + /* Disable the feature again. */ 460 + extts_request.flags = 0; 461 + if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) { 462 + perror("PTP_EXTTS_REQUEST"); 463 + } 469 464 } 470 465 } 471 466