1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/irqnr.h>
34#include <linux/time.h>
35#include <linux/slab.h>
36#include <linux/parport.h>
37#include <linux/pps_kernel.h>
38
39#define DRVDESC "parallel port PPS client"
40
41
42
43#define CLEAR_WAIT_MAX 100
44#define CLEAR_WAIT_MAX_ERRORS 5
45
46static unsigned int clear_wait = 100;
47MODULE_PARM_DESC(clear_wait,
48 "Maximum number of port reads when polling for signal clear,"
49 " zero turns clear edge capture off entirely");
50module_param(clear_wait, uint, 0);
51
52static DEFINE_IDA(pps_client_index);
53
54
55struct pps_client_pp {
56 struct pardevice *pardev;
57 struct pps_device *pps;
58 unsigned int cw;
59 unsigned int cw_err;
60 int index;
61};
62
63static inline int signal_is_set(struct parport *port)
64{
65 return (port->ops->read_status(port) & PARPORT_STATUS_ACK) != 0;
66}
67
68
69static void parport_irq(void *handle)
70{
71 struct pps_event_time ts_assert, ts_clear;
72 struct pps_client_pp *dev = handle;
73 struct parport *port = dev->pardev->port;
74 unsigned int i;
75 unsigned long flags;
76
77
78 pps_get_ts(&ts_assert);
79
80 if (dev->cw == 0)
81
82 goto out_assert;
83
84
85
86
87
88
89
90
91
92
93
94
95
96 local_irq_save(flags);
97
98 if (!signal_is_set(port)) {
99 local_irq_restore(flags);
100 dev_err(dev->pps->dev, "lost the signal\n");
101 goto out_assert;
102 }
103
104
105 for (i = dev->cw; i; i--)
106 if (!signal_is_set(port)) {
107 pps_get_ts(&ts_clear);
108 local_irq_restore(flags);
109 dev->cw_err = 0;
110 goto out_both;
111 }
112 local_irq_restore(flags);
113
114
115 dev->cw_err++;
116 if (dev->cw_err >= CLEAR_WAIT_MAX_ERRORS) {
117 dev_err(dev->pps->dev, "disabled clear edge capture after %d"
118 " timeouts\n", dev->cw_err);
119 dev->cw = 0;
120 dev->cw_err = 0;
121 }
122
123out_assert:
124
125 pps_event(dev->pps, &ts_assert,
126 PPS_CAPTUREASSERT, NULL);
127 return;
128
129out_both:
130
131 pps_event(dev->pps, &ts_assert,
132 PPS_CAPTUREASSERT, NULL);
133
134 pps_event(dev->pps, &ts_clear,
135 PPS_CAPTURECLEAR, NULL);
136 return;
137}
138
139static void parport_attach(struct parport *port)
140{
141 struct pardev_cb pps_client_cb;
142 int index;
143 struct pps_client_pp *device;
144 struct pps_source_info info = {
145 .name = KBUILD_MODNAME,
146 .path = "",
147 .mode = PPS_CAPTUREBOTH | \
148 PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \
149 PPS_ECHOASSERT | PPS_ECHOCLEAR | \
150 PPS_CANWAIT | PPS_TSFMT_TSPEC,
151 .owner = THIS_MODULE,
152 .dev = NULL
153 };
154
155 device = kzalloc(sizeof(struct pps_client_pp), GFP_KERNEL);
156 if (!device) {
157 pr_err("memory allocation failed, not attaching\n");
158 return;
159 }
160
161 index = ida_simple_get(&pps_client_index, 0, 0, GFP_KERNEL);
162 memset(&pps_client_cb, 0, sizeof(pps_client_cb));
163 pps_client_cb.private = device;
164 pps_client_cb.irq_func = parport_irq;
165 pps_client_cb.flags = PARPORT_FLAG_EXCL;
166 device->pardev = parport_register_dev_model(port,
167 KBUILD_MODNAME,
168 &pps_client_cb,
169 index);
170 if (!device->pardev) {
171 pr_err("couldn't register with %s\n", port->name);
172 goto err_free;
173 }
174
175 if (parport_claim_or_block(device->pardev) < 0) {
176 pr_err("couldn't claim %s\n", port->name);
177 goto err_unregister_dev;
178 }
179
180 device->pps = pps_register_source(&info,
181 PPS_CAPTUREBOTH | PPS_OFFSETASSERT | PPS_OFFSETCLEAR);
182 if (device->pps == NULL) {
183 pr_err("couldn't register PPS source\n");
184 goto err_release_dev;
185 }
186
187 device->cw = clear_wait;
188
189 port->ops->enable_irq(port);
190 device->index = index;
191
192 pr_info("attached to %s\n", port->name);
193
194 return;
195
196err_release_dev:
197 parport_release(device->pardev);
198err_unregister_dev:
199 parport_unregister_device(device->pardev);
200err_free:
201 ida_simple_remove(&pps_client_index, index);
202 kfree(device);
203}
204
205static void parport_detach(struct parport *port)
206{
207 struct pardevice *pardev = port->cad;
208 struct pps_client_pp *device;
209
210
211 if (!pardev || strcmp(pardev->name, KBUILD_MODNAME))
212
213 return;
214
215 device = pardev->private;
216
217 port->ops->disable_irq(port);
218 pps_unregister_source(device->pps);
219 parport_release(pardev);
220 parport_unregister_device(pardev);
221 ida_simple_remove(&pps_client_index, device->index);
222 kfree(device);
223}
224
225static struct parport_driver pps_parport_driver = {
226 .name = KBUILD_MODNAME,
227 .match_port = parport_attach,
228 .detach = parport_detach,
229 .devmodel = true,
230};
231
232
233
234static int __init pps_parport_init(void)
235{
236 int ret;
237
238 pr_info(DRVDESC "\n");
239
240 if (clear_wait > CLEAR_WAIT_MAX) {
241 pr_err("clear_wait value should be not greater"
242 " then %d\n", CLEAR_WAIT_MAX);
243 return -EINVAL;
244 }
245
246 ret = parport_register_driver(&pps_parport_driver);
247 if (ret) {
248 pr_err("unable to register with parport\n");
249 return ret;
250 }
251
252 return 0;
253}
254
255static void __exit pps_parport_exit(void)
256{
257 parport_unregister_driver(&pps_parport_driver);
258}
259
260module_init(pps_parport_init);
261module_exit(pps_parport_exit);
262
263MODULE_AUTHOR("Alexander Gordeev <lasaine@lvk.cs.msu.su>");
264MODULE_DESCRIPTION(DRVDESC);
265MODULE_LICENSE("GPL");
266