diff --git a/clock.c b/clock.c index 17c9687..c59046a 100644 --- a/clock.c +++ b/clock.c @@ -801,19 +801,18 @@ static void clock_remove_port(struct clock *c, struct port *p) } struct clock *clock_create(enum clock_type type, struct config *config, - int phc_index) + const char *phc_device) { enum timestamp_type timestamping = config_get_int(config, NULL, "time_stamping"); int fadj = 0, max_adj = 0, sw_ts = timestamping == TS_SOFTWARE ? 1 : 0; enum servo_type servo = config_get_int(config, NULL, "clock_servo"); + int phc_index, required_modes = 0; struct clock *c = &the_clock; - int required_modes = 0; struct port *p; unsigned char oui[OUI_LEN]; char phc[32], *tmp; - struct interface *udsif = &c->uds_interface; - struct interface *iface = STAILQ_FIRST(&config->interfaces); + struct interface *iface, *udsif = &c->uds_interface; struct timespec ts; int sfl; @@ -936,6 +935,29 @@ struct clock *clock_create(enum clock_type type, struct config *config, } iface = STAILQ_FIRST(&config->interfaces); + + /* determine PHC Clock index */ + if (config_get_int(config, NULL, "free_running")) { + phc_index = -1; + } else if (config_get_int(config, NULL, "time_stamping") == TS_SOFTWARE || + config_get_int(config, NULL, "time_stamping") == TS_LEGACY_HW) { + phc_index = -1; + } else if (phc_device) { + if (1 != sscanf(phc_device, "/dev/ptp%d", &phc_index)) { + pr_err("bad ptp device string"); + return NULL; + } + } else if (iface->ts_info.valid) { + phc_index = iface->ts_info.phc_index; + } else { + pr_err("PTP device not specified and automatic determination" + " is not supported. Please specify PTP device."); + return NULL; + } + if (phc_index >= 0) { + pr_info("selected /dev/ptp%d as PTP clock", phc_index); + } + if (generate_clock_identity(&c->dds.clockIdentity, iface->name)) { pr_err("failed to generate a clock identity"); return NULL; diff --git a/clock.h b/clock.h index d4a57e9..fcd9328 100644 --- a/clock.h +++ b/clock.h @@ -78,12 +78,12 @@ struct config *clock_config(struct clock *c); * * @param type Specifies which type of clock to create. * @param config Pointer to the configuration database. - * @param phc_index PTP hardware clock device to use. - * Pass -1 to select CLOCK_REALTIME. + * @param phc_device PTP hardware clock device to use. Pass NULL for automatic + * selection based on the network interface. * @return A pointer to the single global clock instance. */ struct clock *clock_create(enum clock_type type, struct config *config, - int phc_index); + const char *phc_device); /** * Obtains a clock's default data set. diff --git a/ptp4l.c b/ptp4l.c index c3694b0..a87e7e6 100644 --- a/ptp4l.c +++ b/ptp4l.c @@ -73,11 +73,9 @@ static void usage(char *progname) int main(int argc, char *argv[]) { char *config = NULL, *req_phc = NULL, *progname; - int c, err = -1; - struct interface *iface; + int c, err = -1, print_level; struct clock *clock = NULL; struct config *cfg; - int phc_index = -1, print_level; if (handle_term_signals()) return -1; @@ -197,33 +195,8 @@ int main(int argc, char *argv[]) goto out; } - /* determine PHC Clock index */ - iface = STAILQ_FIRST(&cfg->interfaces); - if (config_get_int(cfg, NULL, "free_running")) { - phc_index = -1; - } else if (config_get_int(cfg, NULL, "time_stamping") == TS_SOFTWARE || - config_get_int(cfg, NULL, "time_stamping") == TS_LEGACY_HW) { - phc_index = -1; - } else if (req_phc) { - if (1 != sscanf(req_phc, "/dev/ptp%d", &phc_index)) { - fprintf(stderr, "bad ptp device string\n"); - goto out; - } - } else if (iface->ts_info.valid) { - phc_index = iface->ts_info.phc_index; - } else { - fprintf(stderr, "ptp device not specified and\n" - "automatic determination is not\n" - "supported. please specify ptp device\n"); - goto out; - } - - if (phc_index >= 0) { - pr_info("selected /dev/ptp%d as PTP clock", phc_index); - } - clock = clock_create(cfg->n_interfaces > 1 ? CLOCK_TYPE_BOUNDARY : - CLOCK_TYPE_ORDINARY, cfg, phc_index); + CLOCK_TYPE_ORDINARY, cfg, req_phc); if (!clock) { fprintf(stderr, "failed to create a clock\n"); goto out;