Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 2 | /* |
| 3 | * PTP 1588 clock support - private declarations for the core module. |
| 4 | * |
| 5 | * Copyright (C) 2010 OMICRON electronics GmbH |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 6 | */ |
| 7 | #ifndef _PTP_PRIVATE_H_ |
| 8 | #define _PTP_PRIVATE_H_ |
| 9 | |
| 10 | #include <linux/cdev.h> |
| 11 | #include <linux/device.h> |
Grygorii Strashko | d9535cb | 2017-07-28 17:30:02 -0500 | [diff] [blame] | 12 | #include <linux/kthread.h> |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 13 | #include <linux/mutex.h> |
| 14 | #include <linux/posix-clock.h> |
| 15 | #include <linux/ptp_clock.h> |
| 16 | #include <linux/ptp_clock_kernel.h> |
| 17 | #include <linux/time.h> |
| 18 | |
| 19 | #define PTP_MAX_TIMESTAMPS 128 |
| 20 | #define PTP_BUF_TIMESTAMPS 30 |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 21 | #define PTP_DEFAULT_MAX_VCLOCKS 20 |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 22 | |
| 23 | struct timestamp_event_queue { |
| 24 | struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS]; |
| 25 | int head; |
| 26 | int tail; |
| 27 | spinlock_t lock; |
| 28 | }; |
| 29 | |
| 30 | struct ptp_clock { |
| 31 | struct posix_clock clock; |
Vladis Dronov | a33121e | 2019-12-27 03:26:27 +0100 | [diff] [blame] | 32 | struct device dev; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 33 | struct ptp_clock_info *info; |
| 34 | dev_t devid; |
| 35 | int index; /* index into clocks.map */ |
| 36 | struct pps_device *pps_source; |
Richard Cochran | 39a8cbd | 2012-09-22 07:02:01 +0000 | [diff] [blame] | 37 | long dialed_frequency; /* remembers the frequency adjustment */ |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 38 | struct timestamp_event_queue tsevq; /* simple fifo for time stamps */ |
| 39 | struct mutex tsevq_mux; /* one process at a time reading the fifo */ |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 40 | struct mutex pincfg_mux; /* protect concurrent info->pin_config access */ |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 41 | wait_queue_head_t tsev_wq; |
| 42 | int defunct; /* tells readers to go away when clock is being removed */ |
Richard Cochran | 653104d | 2014-03-20 22:21:54 +0100 | [diff] [blame] | 43 | struct device_attribute *pin_dev_attr; |
| 44 | struct attribute **pin_attr; |
| 45 | struct attribute_group pin_attr_group; |
Dmitry Torokhov | 85a66e5 | 2017-02-14 10:23:34 -0800 | [diff] [blame] | 46 | /* 1st entry is a pointer to the real group, 2nd is NULL terminator */ |
| 47 | const struct attribute_group *pin_attr_groups[2]; |
Grygorii Strashko | d9535cb | 2017-07-28 17:30:02 -0500 | [diff] [blame] | 48 | struct kthread_worker *kworker; |
| 49 | struct kthread_delayed_work aux_work; |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 50 | unsigned int max_vclocks; |
| 51 | unsigned int n_vclocks; |
Yangbo Lu | 44c494c | 2021-06-30 16:11:54 +0800 | [diff] [blame] | 52 | int *vclock_index; |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 53 | struct mutex n_vclocks_mux; /* protect concurrent n_vclocks access */ |
| 54 | bool is_virtual_clock; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
Yangbo Lu | 5d43f95 | 2021-06-30 16:11:52 +0800 | [diff] [blame] | 57 | #define info_to_vclock(d) container_of((d), struct ptp_vclock, info) |
| 58 | #define cc_to_vclock(d) container_of((d), struct ptp_vclock, cc) |
| 59 | #define dw_to_vclock(d) container_of((d), struct ptp_vclock, refresh_work) |
| 60 | |
| 61 | struct ptp_vclock { |
| 62 | struct ptp_clock *pclock; |
| 63 | struct ptp_clock_info info; |
| 64 | struct ptp_clock *clock; |
| 65 | struct cyclecounter cc; |
| 66 | struct timecounter tc; |
| 67 | spinlock_t lock; /* protects tc/cc */ |
| 68 | }; |
| 69 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 70 | /* |
| 71 | * The function queue_cnt() is safe for readers to call without |
| 72 | * holding q->lock. Readers use this function to verify that the queue |
| 73 | * is nonempty before proceeding with a dequeue operation. The fact |
| 74 | * that a writer might concurrently increment the tail does not |
| 75 | * matter, since the queue remains nonempty nonetheless. |
| 76 | */ |
| 77 | static inline int queue_cnt(struct timestamp_event_queue *q) |
| 78 | { |
| 79 | int cnt = q->tail - q->head; |
| 80 | return cnt < 0 ? PTP_MAX_TIMESTAMPS + cnt : cnt; |
| 81 | } |
| 82 | |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 83 | /* Check if ptp virtual clock is in use */ |
| 84 | static inline bool ptp_vclock_in_use(struct ptp_clock *ptp) |
| 85 | { |
| 86 | bool in_use = false; |
| 87 | |
| 88 | if (mutex_lock_interruptible(&ptp->n_vclocks_mux)) |
| 89 | return true; |
| 90 | |
| 91 | if (!ptp->is_virtual_clock && ptp->n_vclocks) |
| 92 | in_use = true; |
| 93 | |
| 94 | mutex_unlock(&ptp->n_vclocks_mux); |
| 95 | |
| 96 | return in_use; |
| 97 | } |
| 98 | |
Yangbo Lu | acb288e | 2021-06-30 16:11:55 +0800 | [diff] [blame] | 99 | extern struct class *ptp_class; |
| 100 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 101 | /* |
| 102 | * see ptp_chardev.c |
| 103 | */ |
| 104 | |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 105 | /* caller must hold pincfg_mux */ |
| 106 | int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin, |
| 107 | enum ptp_pin_function func, unsigned int chan); |
| 108 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 109 | long ptp_ioctl(struct posix_clock *pc, |
| 110 | unsigned int cmd, unsigned long arg); |
| 111 | |
| 112 | int ptp_open(struct posix_clock *pc, fmode_t fmode); |
| 113 | |
| 114 | ssize_t ptp_read(struct posix_clock *pc, |
| 115 | uint flags, char __user *buf, size_t cnt); |
| 116 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 117 | __poll_t ptp_poll(struct posix_clock *pc, |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 118 | struct file *fp, poll_table *wait); |
| 119 | |
| 120 | /* |
| 121 | * see ptp_sysfs.c |
| 122 | */ |
| 123 | |
Greg Kroah-Hartman | 3499116 | 2013-07-24 15:05:20 -0700 | [diff] [blame] | 124 | extern const struct attribute_group *ptp_groups[]; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 125 | |
Dmitry Torokhov | 85a66e5 | 2017-02-14 10:23:34 -0800 | [diff] [blame] | 126 | int ptp_populate_pin_groups(struct ptp_clock *ptp); |
| 127 | void ptp_cleanup_pin_groups(struct ptp_clock *ptp); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 128 | |
Yangbo Lu | 5d43f95 | 2021-06-30 16:11:52 +0800 | [diff] [blame] | 129 | struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock); |
| 130 | void ptp_vclock_unregister(struct ptp_vclock *vclock); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 131 | #endif |