--- zzzz-none-000/linux-3.10.107/drivers/ptp/ptp_sysfs.c 2017-06-27 09:49:32.000000000 +0000 +++ scorpion-7490-727/linux-3.10.107/drivers/ptp/ptp_sysfs.c 2021-02-04 17:41:59.000000000 +0000 @@ -18,6 +18,7 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include +#include #include "ptp_private.h" @@ -27,36 +28,45 @@ struct ptp_clock *ptp = dev_get_drvdata(dev); return snprintf(page, PAGE_SIZE-1, "%s\n", ptp->info->name); } +static DEVICE_ATTR(clock_name, 0444, clock_name_show, NULL); -#define PTP_SHOW_INT(name) \ -static ssize_t name##_show(struct device *dev, \ +#define PTP_SHOW_INT(name, var) \ +static ssize_t var##_show(struct device *dev, \ struct device_attribute *attr, char *page) \ { \ struct ptp_clock *ptp = dev_get_drvdata(dev); \ - return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->name); \ -} + return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var); \ +} \ +static DEVICE_ATTR(name, 0444, var##_show, NULL); + +PTP_SHOW_INT(max_adjustment, max_adj); +PTP_SHOW_INT(n_alarms, n_alarm); +PTP_SHOW_INT(n_external_timestamps, n_ext_ts); +PTP_SHOW_INT(n_periodic_outputs, n_per_out); +PTP_SHOW_INT(n_programmable_pins, n_pins); +PTP_SHOW_INT(pps_available, pps); + +static struct attribute *ptp_attrs[] = { + &dev_attr_clock_name.attr, + &dev_attr_max_adjustment.attr, + &dev_attr_n_alarms.attr, + &dev_attr_n_external_timestamps.attr, + &dev_attr_n_periodic_outputs.attr, + &dev_attr_n_programmable_pins.attr, + &dev_attr_pps_available.attr, + NULL, +}; + +static const struct attribute_group ptp_group = { + .attrs = ptp_attrs, +}; -PTP_SHOW_INT(max_adj); -PTP_SHOW_INT(n_alarm); -PTP_SHOW_INT(n_ext_ts); -PTP_SHOW_INT(n_per_out); -PTP_SHOW_INT(pps); - -#define PTP_RO_ATTR(_var, _name) { \ - .attr = { .name = __stringify(_name), .mode = 0444 }, \ - .show = _var##_show, \ -} - -struct device_attribute ptp_dev_attrs[] = { - PTP_RO_ATTR(clock_name, clock_name), - PTP_RO_ATTR(max_adj, max_adjustment), - PTP_RO_ATTR(n_alarm, n_alarms), - PTP_RO_ATTR(n_ext_ts, n_external_timestamps), - PTP_RO_ATTR(n_per_out, n_periodic_outputs), - PTP_RO_ATTR(pps, pps_available), - __ATTR_NULL, +const struct attribute_group *ptp_groups[] = { + &ptp_group, + NULL, }; + static ssize_t extts_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) @@ -168,6 +178,63 @@ return err; } +static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name) +{ + int i; + for (i = 0; i < ptp->info->n_pins; i++) { + if (!strcmp(ptp->info->pin_config[i].name, name)) + return i; + } + return -1; +} + +static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr, + char *page) +{ + struct ptp_clock *ptp = dev_get_drvdata(dev); + unsigned int func, chan; + int index; + + index = ptp_pin_name2index(ptp, attr->attr.name); + if (index < 0) + return -EINVAL; + + if (mutex_lock_interruptible(&ptp->pincfg_mux)) + return -ERESTARTSYS; + + func = ptp->info->pin_config[index].func; + chan = ptp->info->pin_config[index].chan; + + mutex_unlock(&ptp->pincfg_mux); + + return snprintf(page, PAGE_SIZE, "%u %u\n", func, chan); +} + +static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct ptp_clock *ptp = dev_get_drvdata(dev); + unsigned int func, chan; + int cnt, err, index; + + cnt = sscanf(buf, "%u %u", &func, &chan); + if (cnt != 2) + return -EINVAL; + + index = ptp_pin_name2index(ptp, attr->attr.name); + if (index < 0) + return -EINVAL; + + if (mutex_lock_interruptible(&ptp->pincfg_mux)) + return -ERESTARTSYS; + err = ptp_set_pinfunc(ptp, index, func, chan); + mutex_unlock(&ptp->pincfg_mux); + if (err) + return err; + + return count; +} + static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store); static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL); static DEVICE_ATTR(period, 0220, NULL, period_store); @@ -188,7 +255,54 @@ if (info->pps) device_remove_file(dev, &dev_attr_pps_enable); + if (info->n_pins) { + sysfs_remove_group(&dev->kobj, &ptp->pin_attr_group); + kfree(ptp->pin_attr); + kfree(ptp->pin_dev_attr); + } + return 0; +} + +static int ptp_populate_pins(struct ptp_clock *ptp) +{ + struct device *dev = ptp->dev; + struct ptp_clock_info *info = ptp->info; + int err = -ENOMEM, i, n_pins = info->n_pins; + + ptp->pin_dev_attr = kzalloc(n_pins * sizeof(*ptp->pin_dev_attr), + GFP_KERNEL); + if (!ptp->pin_dev_attr) + goto no_dev_attr; + + ptp->pin_attr = kzalloc((1 + n_pins) * sizeof(struct attribute *), + GFP_KERNEL); + if (!ptp->pin_attr) + goto no_pin_attr; + + for (i = 0; i < n_pins; i++) { + struct device_attribute *da = &ptp->pin_dev_attr[i]; + sysfs_attr_init(&da->attr); + da->attr.name = info->pin_config[i].name; + da->attr.mode = 0644; + da->show = ptp_pin_show; + da->store = ptp_pin_store; + ptp->pin_attr[i] = &da->attr; + } + + ptp->pin_attr_group.name = "pins"; + ptp->pin_attr_group.attrs = ptp->pin_attr; + + err = sysfs_create_group(&dev->kobj, &ptp->pin_attr_group); + if (err) + goto no_group; return 0; + +no_group: + kfree(ptp->pin_attr); +no_pin_attr: + kfree(ptp->pin_dev_attr); +no_dev_attr: + return err; } int ptp_populate_sysfs(struct ptp_clock *ptp) @@ -215,7 +329,15 @@ if (err) goto out4; } + if (info->n_pins) { + err = ptp_populate_pins(ptp); + if (err) + goto out5; + } return 0; +out5: + if (info->pps) + device_remove_file(dev, &dev_attr_pps_enable); out4: if (info->n_per_out) device_remove_file(dev, &dev_attr_period);