Blame phc.c

Packit 9c3e7e
/**
Packit 9c3e7e
 * @file phc.c
Packit 9c3e7e
 * @note Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
Packit 9c3e7e
 *
Packit 9c3e7e
 * This program is free software; you can redistribute it and/or modify
Packit 9c3e7e
 * it under the terms of the GNU General Public License as published by
Packit 9c3e7e
 * the Free Software Foundation; either version 2 of the License, or
Packit 9c3e7e
 * (at your option) any later version.
Packit 9c3e7e
 *
Packit 9c3e7e
 * This program is distributed in the hope that it will be useful,
Packit 9c3e7e
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9c3e7e
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 9c3e7e
 * GNU General Public License for more details.
Packit 9c3e7e
 *
Packit 9c3e7e
 * You should have received a copy of the GNU General Public License along
Packit 9c3e7e
 * with this program; if not, write to the Free Software Foundation, Inc.,
Packit 9c3e7e
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit 9c3e7e
 */
Packit 9c3e7e
#include <stdio.h>
Packit 9c3e7e
#include <sys/ioctl.h>
Packit 9c3e7e
#include <sys/types.h>
Packit 9c3e7e
#include <sys/stat.h>
Packit 9c3e7e
#include <fcntl.h>
Packit 9c3e7e
#include <unistd.h>
Packit 9c3e7e
Packit 9c3e7e
#include <linux/ptp_clock.h>
Packit 9c3e7e
Packit 9c3e7e
#include "phc.h"
Packit 9c3e7e
Packit 9c3e7e
/*
Packit 9c3e7e
 * On 32 bit platforms, the PHC driver's maximum adjustment (type
Packit 9c3e7e
 * 'int' in units of ppb) can overflow the timex.freq field (type
Packit 9c3e7e
 * 'long'). So in this case we clamp the maximum to the largest
Packit 9c3e7e
 * possible adjustment that fits into a 32 bit long.
Packit 9c3e7e
 */
Packit 9c3e7e
#define BITS_PER_LONG	(sizeof(long)*8)
Packit 9c3e7e
#define MAX_PPB_32	32767999	/* 2^31 - 1 / 65.536 */
Packit 9c3e7e
Packit 9c3e7e
static int phc_get_caps(clockid_t clkid, struct ptp_clock_caps *caps);
Packit 9c3e7e
Packit 9c3e7e
clockid_t phc_open(char *phc)
Packit 9c3e7e
{
Packit 9c3e7e
	clockid_t clkid;
Packit 9c3e7e
	struct ptp_clock_caps caps;
Packit 9c3e7e
	int fd = open(phc, O_RDWR);
Packit 9c3e7e
Packit 9c3e7e
	if (fd < 0)
Packit 9c3e7e
		return CLOCK_INVALID;
Packit 9c3e7e
Packit 9c3e7e
	clkid = FD_TO_CLOCKID(fd);
Packit 9c3e7e
	/* check if clkid is valid */
Packit 9c3e7e
	if (phc_get_caps(clkid, &caps)) {
Packit 9c3e7e
		close(fd);
Packit 9c3e7e
		return CLOCK_INVALID;
Packit 9c3e7e
	}
Packit 9c3e7e
Packit 9c3e7e
	return clkid;
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
void phc_close(clockid_t clkid)
Packit 9c3e7e
{
Packit 9c3e7e
	if (clkid == CLOCK_INVALID)
Packit 9c3e7e
		return;
Packit 9c3e7e
Packit 9c3e7e
	close(CLOCKID_TO_FD(clkid));
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
static int phc_get_caps(clockid_t clkid, struct ptp_clock_caps *caps)
Packit 9c3e7e
{
Packit 9c3e7e
	int fd = CLOCKID_TO_FD(clkid), err;
Packit 9c3e7e
Packit 9c3e7e
	err = ioctl(fd, PTP_CLOCK_GETCAPS, caps);
Packit 9c3e7e
	if (err)
Packit 9c3e7e
		perror("PTP_CLOCK_GETCAPS");
Packit 9c3e7e
	return err;
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
int phc_max_adj(clockid_t clkid)
Packit 9c3e7e
{
Packit 9c3e7e
	int max;
Packit 9c3e7e
	struct ptp_clock_caps caps;
Packit 9c3e7e
Packit 9c3e7e
	if (phc_get_caps(clkid, &caps))
Packit 9c3e7e
		return 0;
Packit 9c3e7e
Packit 9c3e7e
	max = caps.max_adj;
Packit 9c3e7e
Packit 9c3e7e
	if (BITS_PER_LONG == 32 && max > MAX_PPB_32)
Packit 9c3e7e
		max = MAX_PPB_32;
Packit 9c3e7e
Packit 9c3e7e
	return max;
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
int phc_has_pps(clockid_t clkid)
Packit 9c3e7e
{
Packit 9c3e7e
	struct ptp_clock_caps caps;
Packit 9c3e7e
Packit 9c3e7e
	if (phc_get_caps(clkid, &caps))
Packit 9c3e7e
		return 0;
Packit 9c3e7e
	return caps.pps;
Packit 9c3e7e
}