1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * PTP 1588 clock support - sysfs interface. 4 * 5 * Copyright (C) 2010 OMICRON electronics GmbH 6 * Copyright 2021 NXP 7 */ 8 #include <linux/capability.h> 9 #include <linux/slab.h> 10 11 #include "ptp_private.h" 12 13 static ssize_t clock_name_show(struct device *dev, 14 struct device_attribute *attr, char *page) 15 { 16 struct ptp_clock *ptp = dev_get_drvdata(dev); 17 return sysfs_emit(page, "%s\n", ptp->info->name); 18 } 19 static DEVICE_ATTR_RO(clock_name); 20 21 static ssize_t max_phase_adjustment_show(struct device *dev, 22 struct device_attribute *attr, 23 char *page) 24 { 25 struct ptp_clock *ptp = dev_get_drvdata(dev); 26 27 return snprintf(page, PAGE_SIZE - 1, "%d\n", 28 ptp->info->getmaxphase(ptp->info)); 29 } 30 static DEVICE_ATTR_RO(max_phase_adjustment); 31 32 #define PTP_SHOW_INT(name, var) \ 33 static ssize_t var##_show(struct device *dev, \ 34 struct device_attribute *attr, char *page) \ 35 { \ 36 struct ptp_clock *ptp = dev_get_drvdata(dev); \ 37 return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var); \ 38 } \ 39 static DEVICE_ATTR(name, 0444, var##_show, NULL); 40 41 PTP_SHOW_INT(max_adjustment, max_adj); 42 PTP_SHOW_INT(n_alarms, n_alarm); 43 PTP_SHOW_INT(n_external_timestamps, n_ext_ts); 44 PTP_SHOW_INT(n_periodic_outputs, n_per_out); 45 PTP_SHOW_INT(n_programmable_pins, n_pins); 46 PTP_SHOW_INT(pps_available, pps); 47 48 static ssize_t extts_enable_store(struct device *dev, 49 struct device_attribute *attr, 50 const char *buf, size_t count) 51 { 52 struct ptp_clock *ptp = dev_get_drvdata(dev); 53 struct ptp_clock_info *ops = ptp->info; 54 struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS }; 55 int cnt, enable; 56 int err = -EINVAL; 57 58 cnt = sscanf(buf, "%u %d", &req.extts.index, &enable); 59 if (cnt != 2) 60 goto out; 61 if (req.extts.index >= ops->n_ext_ts) 62 goto out; 63 64 err = ops->enable(ops, &req, enable ? 1 : 0); 65 if (err) 66 goto out; 67 68 return count; 69 out: 70 return err; 71 } 72 static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store); 73 74 static ssize_t extts_fifo_show(struct device *dev, 75 struct device_attribute *attr, char *page) 76 { 77 struct ptp_clock *ptp = dev_get_drvdata(dev); 78 struct timestamp_event_queue *queue = &ptp->tsevq; 79 struct ptp_extts_event event; 80 unsigned long flags; 81 size_t qcnt; 82 int cnt = 0; 83 84 memset(&event, 0, sizeof(event)); 85 86 if (mutex_lock_interruptible(&ptp->tsevq_mux)) 87 return -ERESTARTSYS; 88 89 spin_lock_irqsave(&queue->lock, flags); 90 qcnt = queue_cnt(queue); 91 if (qcnt) { 92 event = queue->buf[queue->head]; 93 /* Paired with READ_ONCE() in queue_cnt() */ 94 WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS); 95 } 96 spin_unlock_irqrestore(&queue->lock, flags); 97 98 if (!qcnt) 99 goto out; 100 101 cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n", 102 event.index, event.t.sec, event.t.nsec); 103 out: 104 mutex_unlock(&ptp->tsevq_mux); 105 return cnt; 106 } 107 static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL); 108 109 static ssize_t period_store(struct device *dev, 110 struct device_attribute *attr, 111 const char *buf, size_t count) 112 { 113 struct ptp_clock *ptp = dev_get_drvdata(dev); 114 struct ptp_clock_info *ops = ptp->info; 115 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT }; 116 int cnt, enable, err = -EINVAL; 117 118 cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index, 119 &req.perout.start.sec, &req.perout.start.nsec, 120 &req.perout.period.sec, &req.perout.period.nsec); 121 if (cnt != 5) 122 goto out; 123 if (req.perout.index >= ops->n_per_out) 124 goto out; 125 126 enable = req.perout.period.sec || req.perout.period.nsec; 127 err = ops->enable(ops, &req, enable); 128 if (err) 129 goto out; 130 131 return count; 132 out: 133 return err; 134 } 135 static DEVICE_ATTR(period, 0220, NULL, period_store); 136 137 static ssize_t pps_enable_store(struct device *dev, 138 struct device_attribute *attr, 139 const char *buf, size_t count) 140 { 141 struct ptp_clock *ptp = dev_get_drvdata(dev); 142 struct ptp_clock_info *ops = ptp->info; 143 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS }; 144 int cnt, enable; 145 int err = -EINVAL; 146 147 if (!capable(CAP_SYS_TIME)) 148 return -EPERM; 149 150 cnt = sscanf(buf, "%d", &enable); 151 if (cnt != 1) 152 goto out; 153 154 err = ops->enable(ops, &req, enable ? 1 : 0); 155 if (err) 156 goto out; 157 158 return count; 159 out: 160 return err; 161 } 162 static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store); 163 164 static int unregister_vclock(struct device *dev, void *data) 165 { 166 struct ptp_clock *ptp = dev_get_drvdata(dev); 167 struct ptp_clock_info *info = ptp->info; 168 struct ptp_vclock *vclock; 169 u32 *num = data; 170 171 vclock = info_to_vclock(info); 172 dev_info(dev->parent, "delete virtual clock ptp%d\n", 173 vclock->clock->index); 174 175 ptp_vclock_unregister(vclock); 176 (*num)--; 177 178 /* For break. Not error. */ 179 if (*num == 0) 180 return -EINVAL; 181 182 return 0; 183 } 184 185 static ssize_t n_vclocks_show(struct device *dev, 186 struct device_attribute *attr, char *page) 187 { 188 struct ptp_clock *ptp = dev_get_drvdata(dev); 189 ssize_t size; 190 191 if (mutex_lock_interruptible(&ptp->n_vclocks_mux)) 192 return -ERESTARTSYS; 193 194 size = snprintf(page, PAGE_SIZE - 1, "%u\n", ptp->n_vclocks); 195 196 mutex_unlock(&ptp->n_vclocks_mux); 197 198 return size; 199 } 200 201 static ssize_t n_vclocks_store(struct device *dev, 202 struct device_attribute *attr, 203 const char *buf, size_t count) 204 { 205 struct ptp_clock *ptp = dev_get_drvdata(dev); 206 struct ptp_vclock *vclock; 207 int err = -EINVAL; 208 u32 num, i; 209 210 if (kstrtou32(buf, 0, &num)) 211 return err; 212 213 if (mutex_lock_interruptible(&ptp->n_vclocks_mux)) 214 return -ERESTARTSYS; 215 216 if (num > ptp->max_vclocks) { 217 dev_err(dev, "max value is %d\n", ptp->max_vclocks); 218 goto out; 219 } 220 221 /* Need to create more vclocks */ 222 if (num > ptp->n_vclocks) { 223 for (i = 0; i < num - ptp->n_vclocks; i++) { 224 vclock = ptp_vclock_register(ptp); 225 if (!vclock) 226 goto out; 227 228 *(ptp->vclock_index + ptp->n_vclocks + i) = 229 vclock->clock->index; 230 231 dev_info(dev, "new virtual clock ptp%d\n", 232 vclock->clock->index); 233 } 234 } 235 236 /* Need to delete vclocks */ 237 if (num < ptp->n_vclocks) { 238 i = ptp->n_vclocks - num; 239 device_for_each_child_reverse(dev, &i, 240 unregister_vclock); 241 242 for (i = 1; i <= ptp->n_vclocks - num; i++) 243 *(ptp->vclock_index + ptp->n_vclocks - i) = -1; 244 } 245 246 /* Need to inform about changed physical clock behavior */ 247 if (!ptp->has_cycles) { 248 if (num == 0) 249 dev_info(dev, "only physical clock in use now\n"); 250 else 251 dev_info(dev, "guarantee physical clock free running\n"); 252 } 253 254 ptp->n_vclocks = num; 255 mutex_unlock(&ptp->n_vclocks_mux); 256 257 return count; 258 out: 259 mutex_unlock(&ptp->n_vclocks_mux); 260 return err; 261 } 262 static DEVICE_ATTR_RW(n_vclocks); 263 264 static ssize_t max_vclocks_show(struct device *dev, 265 struct device_attribute *attr, char *page) 266 { 267 struct ptp_clock *ptp = dev_get_drvdata(dev); 268 ssize_t size; 269 270 size = snprintf(page, PAGE_SIZE - 1, "%u\n", ptp->max_vclocks); 271 272 return size; 273 } 274 275 static ssize_t max_vclocks_store(struct device *dev, 276 struct device_attribute *attr, 277 const char *buf, size_t count) 278 { 279 struct ptp_clock *ptp = dev_get_drvdata(dev); 280 unsigned int *vclock_index; 281 int err = -EINVAL; 282 size_t size; 283 u32 max; 284 285 if (kstrtou32(buf, 0, &max) || max == 0) 286 return -EINVAL; 287 288 if (max == ptp->max_vclocks) 289 return count; 290 291 if (mutex_lock_interruptible(&ptp->n_vclocks_mux)) 292 return -ERESTARTSYS; 293 294 if (max < ptp->n_vclocks) 295 goto out; 296 297 size = sizeof(int) * max; 298 vclock_index = kzalloc(size, GFP_KERNEL); 299 if (!vclock_index) { 300 err = -ENOMEM; 301 goto out; 302 } 303 304 size = sizeof(int) * ptp->n_vclocks; 305 memcpy(vclock_index, ptp->vclock_index, size); 306 307 kfree(ptp->vclock_index); 308 ptp->vclock_index = vclock_index; 309 ptp->max_vclocks = max; 310 311 mutex_unlock(&ptp->n_vclocks_mux); 312 313 return count; 314 out: 315 mutex_unlock(&ptp->n_vclocks_mux); 316 return err; 317 } 318 static DEVICE_ATTR_RW(max_vclocks); 319 320 static struct attribute *ptp_attrs[] = { 321 &dev_attr_clock_name.attr, 322 323 &dev_attr_max_adjustment.attr, 324 &dev_attr_max_phase_adjustment.attr, 325 &dev_attr_n_alarms.attr, 326 &dev_attr_n_external_timestamps.attr, 327 &dev_attr_n_periodic_outputs.attr, 328 &dev_attr_n_programmable_pins.attr, 329 &dev_attr_pps_available.attr, 330 331 &dev_attr_extts_enable.attr, 332 &dev_attr_fifo.attr, 333 &dev_attr_period.attr, 334 &dev_attr_pps_enable.attr, 335 &dev_attr_n_vclocks.attr, 336 &dev_attr_max_vclocks.attr, 337 NULL 338 }; 339 340 static umode_t ptp_is_attribute_visible(struct kobject *kobj, 341 struct attribute *attr, int n) 342 { 343 struct device *dev = kobj_to_dev(kobj); 344 struct ptp_clock *ptp = dev_get_drvdata(dev); 345 struct ptp_clock_info *info = ptp->info; 346 umode_t mode = attr->mode; 347 348 if (attr == &dev_attr_extts_enable.attr || 349 attr == &dev_attr_fifo.attr) { 350 if (!info->n_ext_ts) 351 mode = 0; 352 } else if (attr == &dev_attr_period.attr) { 353 if (!info->n_per_out) 354 mode = 0; 355 } else if (attr == &dev_attr_pps_enable.attr) { 356 if (!info->pps) 357 mode = 0; 358 } else if (attr == &dev_attr_n_vclocks.attr || 359 attr == &dev_attr_max_vclocks.attr) { 360 if (ptp->is_virtual_clock) 361 mode = 0; 362 } else if (attr == &dev_attr_max_phase_adjustment.attr) { 363 if (!info->adjphase || !info->getmaxphase) 364 mode = 0; 365 } 366 367 return mode; 368 } 369 370 static const struct attribute_group ptp_group = { 371 .is_visible = ptp_is_attribute_visible, 372 .attrs = ptp_attrs, 373 }; 374 375 const struct attribute_group *ptp_groups[] = { 376 &ptp_group, 377 NULL 378 }; 379 380 static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name) 381 { 382 int i; 383 for (i = 0; i < ptp->info->n_pins; i++) { 384 if (!strcmp(ptp->info->pin_config[i].name, name)) 385 return i; 386 } 387 return -1; 388 } 389 390 static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr, 391 char *page) 392 { 393 struct ptp_clock *ptp = dev_get_drvdata(dev); 394 unsigned int func, chan; 395 int index; 396 397 index = ptp_pin_name2index(ptp, attr->attr.name); 398 if (index < 0) 399 return -EINVAL; 400 401 if (mutex_lock_interruptible(&ptp->pincfg_mux)) 402 return -ERESTARTSYS; 403 404 func = ptp->info->pin_config[index].func; 405 chan = ptp->info->pin_config[index].chan; 406 407 mutex_unlock(&ptp->pincfg_mux); 408 409 return sysfs_emit(page, "%u %u\n", func, chan); 410 } 411 412 static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr, 413 const char *buf, size_t count) 414 { 415 struct ptp_clock *ptp = dev_get_drvdata(dev); 416 unsigned int func, chan; 417 int cnt, err, index; 418 419 cnt = sscanf(buf, "%u %u", &func, &chan); 420 if (cnt != 2) 421 return -EINVAL; 422 423 index = ptp_pin_name2index(ptp, attr->attr.name); 424 if (index < 0) 425 return -EINVAL; 426 427 if (mutex_lock_interruptible(&ptp->pincfg_mux)) 428 return -ERESTARTSYS; 429 err = ptp_set_pinfunc(ptp, index, func, chan); 430 mutex_unlock(&ptp->pincfg_mux); 431 if (err) 432 return err; 433 434 return count; 435 } 436 437 int ptp_populate_pin_groups(struct ptp_clock *ptp) 438 { 439 struct ptp_clock_info *info = ptp->info; 440 int err = -ENOMEM, i, n_pins = info->n_pins; 441 442 if (!n_pins) 443 return 0; 444 445 ptp->pin_dev_attr = kcalloc(n_pins, sizeof(*ptp->pin_dev_attr), 446 GFP_KERNEL); 447 if (!ptp->pin_dev_attr) 448 goto no_dev_attr; 449 450 ptp->pin_attr = kcalloc(1 + n_pins, sizeof(*ptp->pin_attr), GFP_KERNEL); 451 if (!ptp->pin_attr) 452 goto no_pin_attr; 453 454 for (i = 0; i < n_pins; i++) { 455 struct device_attribute *da = &ptp->pin_dev_attr[i]; 456 sysfs_attr_init(&da->attr); 457 da->attr.name = info->pin_config[i].name; 458 da->attr.mode = 0644; 459 da->show = ptp_pin_show; 460 da->store = ptp_pin_store; 461 ptp->pin_attr[i] = &da->attr; 462 } 463 464 ptp->pin_attr_group.name = "pins"; 465 ptp->pin_attr_group.attrs = ptp->pin_attr; 466 467 ptp->pin_attr_groups[0] = &ptp->pin_attr_group; 468 469 return 0; 470 471 no_pin_attr: 472 kfree(ptp->pin_dev_attr); 473 no_dev_attr: 474 return err; 475 } 476 477 void ptp_cleanup_pin_groups(struct ptp_clock *ptp) 478 { 479 kfree(ptp->pin_attr); 480 kfree(ptp->pin_dev_attr); 481 } 482