xref: /openbmc/linux/drivers/pps/pps.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2eae9d2baSRodolfo Giometti /*
3eae9d2baSRodolfo Giometti  * PPS core file
4eae9d2baSRodolfo Giometti  *
5eae9d2baSRodolfo Giometti  * Copyright (C) 2005-2009   Rodolfo Giometti <giometti@linux.it>
6eae9d2baSRodolfo Giometti  */
7eae9d2baSRodolfo Giometti 
87f7cce74SAlexander Gordeev #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9eae9d2baSRodolfo Giometti 
10eae9d2baSRodolfo Giometti #include <linux/kernel.h>
11eae9d2baSRodolfo Giometti #include <linux/module.h>
12eae9d2baSRodolfo Giometti #include <linux/init.h>
13eae9d2baSRodolfo Giometti #include <linux/sched.h>
14eae9d2baSRodolfo Giometti #include <linux/uaccess.h>
15eae9d2baSRodolfo Giometti #include <linux/idr.h>
162a5cd6e2SAlexander Gordeev #include <linux/mutex.h>
17eae9d2baSRodolfo Giometti #include <linux/cdev.h>
18eae9d2baSRodolfo Giometti #include <linux/poll.h>
19eae9d2baSRodolfo Giometti #include <linux/pps_kernel.h>
20083e5866SAlexander Gordeev #include <linux/slab.h>
21eae9d2baSRodolfo Giometti 
22717c0336SAlexander Gordeev #include "kc.h"
23717c0336SAlexander Gordeev 
24eae9d2baSRodolfo Giometti /*
25eae9d2baSRodolfo Giometti  * Local variables
26eae9d2baSRodolfo Giometti  */
27eae9d2baSRodolfo Giometti 
28eae9d2baSRodolfo Giometti static dev_t pps_devt;
29eae9d2baSRodolfo Giometti static struct class *pps_class;
30eae9d2baSRodolfo Giometti 
312a5cd6e2SAlexander Gordeev static DEFINE_MUTEX(pps_idr_lock);
32083e5866SAlexander Gordeev static DEFINE_IDR(pps_idr);
33083e5866SAlexander Gordeev 
34eae9d2baSRodolfo Giometti /*
35eae9d2baSRodolfo Giometti  * Char device methods
36eae9d2baSRodolfo Giometti  */
37eae9d2baSRodolfo Giometti 
pps_cdev_poll(struct file * file,poll_table * wait)38afc9a42bSAl Viro static __poll_t pps_cdev_poll(struct file *file, poll_table *wait)
39eae9d2baSRodolfo Giometti {
40eae9d2baSRodolfo Giometti 	struct pps_device *pps = file->private_data;
41eae9d2baSRodolfo Giometti 
42eae9d2baSRodolfo Giometti 	poll_wait(file, &pps->queue, wait);
43eae9d2baSRodolfo Giometti 
44a9a08845SLinus Torvalds 	return EPOLLIN | EPOLLRDNORM;
45eae9d2baSRodolfo Giometti }
46eae9d2baSRodolfo Giometti 
pps_cdev_fasync(int fd,struct file * file,int on)47eae9d2baSRodolfo Giometti static int pps_cdev_fasync(int fd, struct file *file, int on)
48eae9d2baSRodolfo Giometti {
49eae9d2baSRodolfo Giometti 	struct pps_device *pps = file->private_data;
50eae9d2baSRodolfo Giometti 	return fasync_helper(fd, file, on, &pps->async_queue);
51eae9d2baSRodolfo Giometti }
52eae9d2baSRodolfo Giometti 
pps_cdev_pps_fetch(struct pps_device * pps,struct pps_fdata * fdata)53c2a49fe8SMatt Ranostay static int pps_cdev_pps_fetch(struct pps_device *pps, struct pps_fdata *fdata)
54c2a49fe8SMatt Ranostay {
55c2a49fe8SMatt Ranostay 	unsigned int ev = pps->last_ev;
56c2a49fe8SMatt Ranostay 	int err = 0;
57c2a49fe8SMatt Ranostay 
58c2a49fe8SMatt Ranostay 	/* Manage the timeout */
59c2a49fe8SMatt Ranostay 	if (fdata->timeout.flags & PPS_TIME_INVALID)
60c2a49fe8SMatt Ranostay 		err = wait_event_interruptible(pps->queue,
61c2a49fe8SMatt Ranostay 				ev != pps->last_ev);
62c2a49fe8SMatt Ranostay 	else {
63c2a49fe8SMatt Ranostay 		unsigned long ticks;
64c2a49fe8SMatt Ranostay 
65c2a49fe8SMatt Ranostay 		dev_dbg(pps->dev, "timeout %lld.%09d\n",
66c2a49fe8SMatt Ranostay 				(long long) fdata->timeout.sec,
67c2a49fe8SMatt Ranostay 				fdata->timeout.nsec);
68c2a49fe8SMatt Ranostay 		ticks = fdata->timeout.sec * HZ;
69c2a49fe8SMatt Ranostay 		ticks += fdata->timeout.nsec / (NSEC_PER_SEC / HZ);
70c2a49fe8SMatt Ranostay 
71c2a49fe8SMatt Ranostay 		if (ticks != 0) {
72c2a49fe8SMatt Ranostay 			err = wait_event_interruptible_timeout(
73c2a49fe8SMatt Ranostay 					pps->queue,
74c2a49fe8SMatt Ranostay 					ev != pps->last_ev,
75c2a49fe8SMatt Ranostay 					ticks);
76c2a49fe8SMatt Ranostay 			if (err == 0)
77c2a49fe8SMatt Ranostay 				return -ETIMEDOUT;
78c2a49fe8SMatt Ranostay 		}
79c2a49fe8SMatt Ranostay 	}
80c2a49fe8SMatt Ranostay 
81c2a49fe8SMatt Ranostay 	/* Check for pending signals */
82c2a49fe8SMatt Ranostay 	if (err == -ERESTARTSYS) {
83c2a49fe8SMatt Ranostay 		dev_dbg(pps->dev, "pending signal caught\n");
84c2a49fe8SMatt Ranostay 		return -EINTR;
85c2a49fe8SMatt Ranostay 	}
86c2a49fe8SMatt Ranostay 
87c2a49fe8SMatt Ranostay 	return 0;
88c2a49fe8SMatt Ranostay }
89c2a49fe8SMatt Ranostay 
pps_cdev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)90eae9d2baSRodolfo Giometti static long pps_cdev_ioctl(struct file *file,
91eae9d2baSRodolfo Giometti 		unsigned int cmd, unsigned long arg)
92eae9d2baSRodolfo Giometti {
93eae9d2baSRodolfo Giometti 	struct pps_device *pps = file->private_data;
94eae9d2baSRodolfo Giometti 	struct pps_kparams params;
95eae9d2baSRodolfo Giometti 	void __user *uarg = (void __user *) arg;
96eae9d2baSRodolfo Giometti 	int __user *iuarg = (int __user *) arg;
97eae9d2baSRodolfo Giometti 	int err;
98eae9d2baSRodolfo Giometti 
99eae9d2baSRodolfo Giometti 	switch (cmd) {
100eae9d2baSRodolfo Giometti 	case PPS_GETPARAMS:
1017f7cce74SAlexander Gordeev 		dev_dbg(pps->dev, "PPS_GETPARAMS\n");
102eae9d2baSRodolfo Giometti 
103cbf83cc5SRodolfo Giometti 		spin_lock_irq(&pps->lock);
104cbf83cc5SRodolfo Giometti 
105cbf83cc5SRodolfo Giometti 		/* Get the current parameters */
106cbf83cc5SRodolfo Giometti 		params = pps->params;
107cbf83cc5SRodolfo Giometti 
108cbf83cc5SRodolfo Giometti 		spin_unlock_irq(&pps->lock);
109cbf83cc5SRodolfo Giometti 
110cbf83cc5SRodolfo Giometti 		err = copy_to_user(uarg, &params, sizeof(struct pps_kparams));
111eae9d2baSRodolfo Giometti 		if (err)
112eae9d2baSRodolfo Giometti 			return -EFAULT;
113eae9d2baSRodolfo Giometti 
114eae9d2baSRodolfo Giometti 		break;
115eae9d2baSRodolfo Giometti 
116eae9d2baSRodolfo Giometti 	case PPS_SETPARAMS:
1177f7cce74SAlexander Gordeev 		dev_dbg(pps->dev, "PPS_SETPARAMS\n");
118eae9d2baSRodolfo Giometti 
119eae9d2baSRodolfo Giometti 		/* Check the capabilities */
120eae9d2baSRodolfo Giometti 		if (!capable(CAP_SYS_TIME))
121eae9d2baSRodolfo Giometti 			return -EPERM;
122eae9d2baSRodolfo Giometti 
123eae9d2baSRodolfo Giometti 		err = copy_from_user(&params, uarg, sizeof(struct pps_kparams));
124eae9d2baSRodolfo Giometti 		if (err)
125eae9d2baSRodolfo Giometti 			return -EFAULT;
126eae9d2baSRodolfo Giometti 		if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
1277f7cce74SAlexander Gordeev 			dev_dbg(pps->dev, "capture mode unspecified (%x)\n",
128eae9d2baSRodolfo Giometti 								params.mode);
129eae9d2baSRodolfo Giometti 			return -EINVAL;
130eae9d2baSRodolfo Giometti 		}
131eae9d2baSRodolfo Giometti 
132eae9d2baSRodolfo Giometti 		/* Check for supported capabilities */
133eae9d2baSRodolfo Giometti 		if ((params.mode & ~pps->info.mode) != 0) {
1347f7cce74SAlexander Gordeev 			dev_dbg(pps->dev, "unsupported capabilities (%x)\n",
135eae9d2baSRodolfo Giometti 								params.mode);
136eae9d2baSRodolfo Giometti 			return -EINVAL;
137eae9d2baSRodolfo Giometti 		}
138eae9d2baSRodolfo Giometti 
139eae9d2baSRodolfo Giometti 		spin_lock_irq(&pps->lock);
140eae9d2baSRodolfo Giometti 
141eae9d2baSRodolfo Giometti 		/* Save the new parameters */
142eae9d2baSRodolfo Giometti 		pps->params = params;
143eae9d2baSRodolfo Giometti 
144eae9d2baSRodolfo Giometti 		/* Restore the read only parameters */
145eae9d2baSRodolfo Giometti 		if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
146eae9d2baSRodolfo Giometti 			/* section 3.3 of RFC 2783 interpreted */
1477f7cce74SAlexander Gordeev 			dev_dbg(pps->dev, "time format unspecified (%x)\n",
148eae9d2baSRodolfo Giometti 								params.mode);
149eae9d2baSRodolfo Giometti 			pps->params.mode |= PPS_TSFMT_TSPEC;
150eae9d2baSRodolfo Giometti 		}
151eae9d2baSRodolfo Giometti 		if (pps->info.mode & PPS_CANWAIT)
152eae9d2baSRodolfo Giometti 			pps->params.mode |= PPS_CANWAIT;
153eae9d2baSRodolfo Giometti 		pps->params.api_version = PPS_API_VERS;
154eae9d2baSRodolfo Giometti 
1555515e9a6SMiroslav Lichvar 		/*
1565515e9a6SMiroslav Lichvar 		 * Clear unused fields of pps_kparams to avoid leaking
1575515e9a6SMiroslav Lichvar 		 * uninitialized data of the PPS_SETPARAMS caller via
1585515e9a6SMiroslav Lichvar 		 * PPS_GETPARAMS
1595515e9a6SMiroslav Lichvar 		 */
1605515e9a6SMiroslav Lichvar 		pps->params.assert_off_tu.flags = 0;
1615515e9a6SMiroslav Lichvar 		pps->params.clear_off_tu.flags = 0;
1625515e9a6SMiroslav Lichvar 
163eae9d2baSRodolfo Giometti 		spin_unlock_irq(&pps->lock);
164eae9d2baSRodolfo Giometti 
165eae9d2baSRodolfo Giometti 		break;
166eae9d2baSRodolfo Giometti 
167eae9d2baSRodolfo Giometti 	case PPS_GETCAP:
1687f7cce74SAlexander Gordeev 		dev_dbg(pps->dev, "PPS_GETCAP\n");
169eae9d2baSRodolfo Giometti 
170eae9d2baSRodolfo Giometti 		err = put_user(pps->info.mode, iuarg);
171eae9d2baSRodolfo Giometti 		if (err)
172eae9d2baSRodolfo Giometti 			return -EFAULT;
173eae9d2baSRodolfo Giometti 
174eae9d2baSRodolfo Giometti 		break;
175eae9d2baSRodolfo Giometti 
17686d921f9SAlexander Gordeev 	case PPS_FETCH: {
17786d921f9SAlexander Gordeev 		struct pps_fdata fdata;
17886d921f9SAlexander Gordeev 
1797f7cce74SAlexander Gordeev 		dev_dbg(pps->dev, "PPS_FETCH\n");
180eae9d2baSRodolfo Giometti 
181eae9d2baSRodolfo Giometti 		err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
182eae9d2baSRodolfo Giometti 		if (err)
183eae9d2baSRodolfo Giometti 			return -EFAULT;
184eae9d2baSRodolfo Giometti 
185c2a49fe8SMatt Ranostay 		err = pps_cdev_pps_fetch(pps, &fdata);
186c2a49fe8SMatt Ranostay 		if (err)
187c2a49fe8SMatt Ranostay 			return err;
188eae9d2baSRodolfo Giometti 
189eae9d2baSRodolfo Giometti 		/* Return the fetched timestamp */
190eae9d2baSRodolfo Giometti 		spin_lock_irq(&pps->lock);
191eae9d2baSRodolfo Giometti 
192eae9d2baSRodolfo Giometti 		fdata.info.assert_sequence = pps->assert_sequence;
193eae9d2baSRodolfo Giometti 		fdata.info.clear_sequence = pps->clear_sequence;
194eae9d2baSRodolfo Giometti 		fdata.info.assert_tu = pps->assert_tu;
195eae9d2baSRodolfo Giometti 		fdata.info.clear_tu = pps->clear_tu;
196eae9d2baSRodolfo Giometti 		fdata.info.current_mode = pps->current_mode;
197eae9d2baSRodolfo Giometti 
198eae9d2baSRodolfo Giometti 		spin_unlock_irq(&pps->lock);
199eae9d2baSRodolfo Giometti 
200eae9d2baSRodolfo Giometti 		err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
201eae9d2baSRodolfo Giometti 		if (err)
202eae9d2baSRodolfo Giometti 			return -EFAULT;
203eae9d2baSRodolfo Giometti 
204eae9d2baSRodolfo Giometti 		break;
20586d921f9SAlexander Gordeev 	}
206717c0336SAlexander Gordeev 	case PPS_KC_BIND: {
207717c0336SAlexander Gordeev 		struct pps_bind_args bind_args;
208717c0336SAlexander Gordeev 
209717c0336SAlexander Gordeev 		dev_dbg(pps->dev, "PPS_KC_BIND\n");
210717c0336SAlexander Gordeev 
211717c0336SAlexander Gordeev 		/* Check the capabilities */
212717c0336SAlexander Gordeev 		if (!capable(CAP_SYS_TIME))
213717c0336SAlexander Gordeev 			return -EPERM;
214717c0336SAlexander Gordeev 
215717c0336SAlexander Gordeev 		if (copy_from_user(&bind_args, uarg,
216717c0336SAlexander Gordeev 					sizeof(struct pps_bind_args)))
217717c0336SAlexander Gordeev 			return -EFAULT;
218717c0336SAlexander Gordeev 
219717c0336SAlexander Gordeev 		/* Check for supported capabilities */
220717c0336SAlexander Gordeev 		if ((bind_args.edge & ~pps->info.mode) != 0) {
221717c0336SAlexander Gordeev 			dev_err(pps->dev, "unsupported capabilities (%x)\n",
222717c0336SAlexander Gordeev 					bind_args.edge);
223717c0336SAlexander Gordeev 			return -EINVAL;
224717c0336SAlexander Gordeev 		}
225717c0336SAlexander Gordeev 
226717c0336SAlexander Gordeev 		/* Validate parameters roughly */
227717c0336SAlexander Gordeev 		if (bind_args.tsformat != PPS_TSFMT_TSPEC ||
228717c0336SAlexander Gordeev 				(bind_args.edge & ~PPS_CAPTUREBOTH) != 0 ||
229717c0336SAlexander Gordeev 				bind_args.consumer != PPS_KC_HARDPPS) {
230717c0336SAlexander Gordeev 			dev_err(pps->dev, "invalid kernel consumer bind"
231717c0336SAlexander Gordeev 					" parameters (%x)\n", bind_args.edge);
232717c0336SAlexander Gordeev 			return -EINVAL;
233717c0336SAlexander Gordeev 		}
234717c0336SAlexander Gordeev 
235717c0336SAlexander Gordeev 		err = pps_kc_bind(pps, &bind_args);
236717c0336SAlexander Gordeev 		if (err < 0)
237717c0336SAlexander Gordeev 			return err;
238717c0336SAlexander Gordeev 
239717c0336SAlexander Gordeev 		break;
240717c0336SAlexander Gordeev 	}
241eae9d2baSRodolfo Giometti 	default:
242eae9d2baSRodolfo Giometti 		return -ENOTTY;
243eae9d2baSRodolfo Giometti 	}
244eae9d2baSRodolfo Giometti 
245eae9d2baSRodolfo Giometti 	return 0;
246eae9d2baSRodolfo Giometti }
247eae9d2baSRodolfo Giometti 
2482ac66657SMatt Ranostay #ifdef CONFIG_COMPAT
pps_cdev_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2492ac66657SMatt Ranostay static long pps_cdev_compat_ioctl(struct file *file,
2502ac66657SMatt Ranostay 		unsigned int cmd, unsigned long arg)
2512ac66657SMatt Ranostay {
252c2a49fe8SMatt Ranostay 	struct pps_device *pps = file->private_data;
253c2a49fe8SMatt Ranostay 	void __user *uarg = (void __user *) arg;
254c2a49fe8SMatt Ranostay 
2552ac66657SMatt Ranostay 	cmd = _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(void *));
2562ac66657SMatt Ranostay 
257c2a49fe8SMatt Ranostay 	if (cmd == PPS_FETCH) {
258c2a49fe8SMatt Ranostay 		struct pps_fdata_compat compat;
259c2a49fe8SMatt Ranostay 		struct pps_fdata fdata;
260c2a49fe8SMatt Ranostay 		int err;
261c2a49fe8SMatt Ranostay 
262c2a49fe8SMatt Ranostay 		dev_dbg(pps->dev, "PPS_FETCH\n");
263c2a49fe8SMatt Ranostay 
264c2a49fe8SMatt Ranostay 		err = copy_from_user(&compat, uarg, sizeof(struct pps_fdata_compat));
265c2a49fe8SMatt Ranostay 		if (err)
266c2a49fe8SMatt Ranostay 			return -EFAULT;
267c2a49fe8SMatt Ranostay 
268c2a49fe8SMatt Ranostay 		memcpy(&fdata.timeout, &compat.timeout,
269c2a49fe8SMatt Ranostay 					sizeof(struct pps_ktime_compat));
270c2a49fe8SMatt Ranostay 
271c2a49fe8SMatt Ranostay 		err = pps_cdev_pps_fetch(pps, &fdata);
272c2a49fe8SMatt Ranostay 		if (err)
273c2a49fe8SMatt Ranostay 			return err;
274c2a49fe8SMatt Ranostay 
275c2a49fe8SMatt Ranostay 		/* Return the fetched timestamp */
276c2a49fe8SMatt Ranostay 		spin_lock_irq(&pps->lock);
277c2a49fe8SMatt Ranostay 
278c2a49fe8SMatt Ranostay 		compat.info.assert_sequence = pps->assert_sequence;
279c2a49fe8SMatt Ranostay 		compat.info.clear_sequence = pps->clear_sequence;
280c2a49fe8SMatt Ranostay 		compat.info.current_mode = pps->current_mode;
281c2a49fe8SMatt Ranostay 
282c2a49fe8SMatt Ranostay 		memcpy(&compat.info.assert_tu, &pps->assert_tu,
283c2a49fe8SMatt Ranostay 				sizeof(struct pps_ktime_compat));
284c2a49fe8SMatt Ranostay 		memcpy(&compat.info.clear_tu, &pps->clear_tu,
285c2a49fe8SMatt Ranostay 				sizeof(struct pps_ktime_compat));
286c2a49fe8SMatt Ranostay 
287c2a49fe8SMatt Ranostay 		spin_unlock_irq(&pps->lock);
288c2a49fe8SMatt Ranostay 
289c2a49fe8SMatt Ranostay 		return copy_to_user(uarg, &compat,
290c2a49fe8SMatt Ranostay 				sizeof(struct pps_fdata_compat)) ? -EFAULT : 0;
291c2a49fe8SMatt Ranostay 	}
292c2a49fe8SMatt Ranostay 
2932ac66657SMatt Ranostay 	return pps_cdev_ioctl(file, cmd, arg);
2942ac66657SMatt Ranostay }
2952ac66657SMatt Ranostay #else
2962ac66657SMatt Ranostay #define pps_cdev_compat_ioctl	NULL
2972ac66657SMatt Ranostay #endif
2982ac66657SMatt Ranostay 
pps_cdev_open(struct inode * inode,struct file * file)299eae9d2baSRodolfo Giometti static int pps_cdev_open(struct inode *inode, struct file *file)
300eae9d2baSRodolfo Giometti {
301eae9d2baSRodolfo Giometti 	struct pps_device *pps = container_of(inode->i_cdev,
302eae9d2baSRodolfo Giometti 						struct pps_device, cdev);
303eae9d2baSRodolfo Giometti 	file->private_data = pps;
304d953e0e8SGeorge Spelvin 	kobject_get(&pps->dev->kobj);
305eae9d2baSRodolfo Giometti 	return 0;
306eae9d2baSRodolfo Giometti }
307eae9d2baSRodolfo Giometti 
pps_cdev_release(struct inode * inode,struct file * file)308eae9d2baSRodolfo Giometti static int pps_cdev_release(struct inode *inode, struct file *file)
309eae9d2baSRodolfo Giometti {
310d953e0e8SGeorge Spelvin 	struct pps_device *pps = container_of(inode->i_cdev,
311d953e0e8SGeorge Spelvin 						struct pps_device, cdev);
312d953e0e8SGeorge Spelvin 	kobject_put(&pps->dev->kobj);
313eae9d2baSRodolfo Giometti 	return 0;
314eae9d2baSRodolfo Giometti }
315eae9d2baSRodolfo Giometti 
316eae9d2baSRodolfo Giometti /*
317eae9d2baSRodolfo Giometti  * Char device stuff
318eae9d2baSRodolfo Giometti  */
319eae9d2baSRodolfo Giometti 
320eae9d2baSRodolfo Giometti static const struct file_operations pps_cdev_fops = {
321eae9d2baSRodolfo Giometti 	.owner		= THIS_MODULE,
322eae9d2baSRodolfo Giometti 	.llseek		= no_llseek,
323eae9d2baSRodolfo Giometti 	.poll		= pps_cdev_poll,
324eae9d2baSRodolfo Giometti 	.fasync		= pps_cdev_fasync,
3252ac66657SMatt Ranostay 	.compat_ioctl	= pps_cdev_compat_ioctl,
326eae9d2baSRodolfo Giometti 	.unlocked_ioctl	= pps_cdev_ioctl,
327eae9d2baSRodolfo Giometti 	.open		= pps_cdev_open,
328eae9d2baSRodolfo Giometti 	.release	= pps_cdev_release,
329eae9d2baSRodolfo Giometti };
330eae9d2baSRodolfo Giometti 
pps_device_destruct(struct device * dev)331083e5866SAlexander Gordeev static void pps_device_destruct(struct device *dev)
332083e5866SAlexander Gordeev {
333083e5866SAlexander Gordeev 	struct pps_device *pps = dev_get_drvdata(dev);
334083e5866SAlexander Gordeev 
335d953e0e8SGeorge Spelvin 	cdev_del(&pps->cdev);
336d953e0e8SGeorge Spelvin 
337d953e0e8SGeorge Spelvin 	/* Now we can release the ID for re-use */
338d953e0e8SGeorge Spelvin 	pr_debug("deallocating pps%d\n", pps->id);
3392a5cd6e2SAlexander Gordeev 	mutex_lock(&pps_idr_lock);
340083e5866SAlexander Gordeev 	idr_remove(&pps_idr, pps->id);
3412a5cd6e2SAlexander Gordeev 	mutex_unlock(&pps_idr_lock);
342083e5866SAlexander Gordeev 
343083e5866SAlexander Gordeev 	kfree(dev);
344083e5866SAlexander Gordeev 	kfree(pps);
345083e5866SAlexander Gordeev }
346083e5866SAlexander Gordeev 
pps_register_cdev(struct pps_device * pps)347eae9d2baSRodolfo Giometti int pps_register_cdev(struct pps_device *pps)
348eae9d2baSRodolfo Giometti {
349eae9d2baSRodolfo Giometti 	int err;
3505e196d34SAlexander Gordeev 	dev_t devt;
351eae9d2baSRodolfo Giometti 
3522a5cd6e2SAlexander Gordeev 	mutex_lock(&pps_idr_lock);
35319dd2da3STejun Heo 	/*
35419dd2da3STejun Heo 	 * Get new ID for the new PPS source.  After idr_alloc() calling
35519dd2da3STejun Heo 	 * the new source will be freely available into the kernel.
356083e5866SAlexander Gordeev 	 */
35719dd2da3STejun Heo 	err = idr_alloc(&pps_idr, pps, 0, PPS_MAX_SOURCES, GFP_KERNEL);
35819dd2da3STejun Heo 	if (err < 0) {
35919dd2da3STejun Heo 		if (err == -ENOSPC) {
360083e5866SAlexander Gordeev 			pr_err("%s: too many PPS sources in the system\n",
361083e5866SAlexander Gordeev 			       pps->info.name);
362083e5866SAlexander Gordeev 			err = -EBUSY;
363083e5866SAlexander Gordeev 		}
36419dd2da3STejun Heo 		goto out_unlock;
36519dd2da3STejun Heo 	}
36619dd2da3STejun Heo 	pps->id = err;
36719dd2da3STejun Heo 	mutex_unlock(&pps_idr_lock);
368083e5866SAlexander Gordeev 
3695e196d34SAlexander Gordeev 	devt = MKDEV(MAJOR(pps_devt), pps->id);
3705e196d34SAlexander Gordeev 
371eae9d2baSRodolfo Giometti 	cdev_init(&pps->cdev, &pps_cdev_fops);
372eae9d2baSRodolfo Giometti 	pps->cdev.owner = pps->info.owner;
373eae9d2baSRodolfo Giometti 
3745e196d34SAlexander Gordeev 	err = cdev_add(&pps->cdev, devt, 1);
375eae9d2baSRodolfo Giometti 	if (err) {
3767f7cce74SAlexander Gordeev 		pr_err("%s: failed to add char device %d:%d\n",
377eae9d2baSRodolfo Giometti 				pps->info.name, MAJOR(pps_devt), pps->id);
378083e5866SAlexander Gordeev 		goto free_idr;
379eae9d2baSRodolfo Giometti 	}
3805e196d34SAlexander Gordeev 	pps->dev = device_create(pps_class, pps->info.dev, devt, pps,
381eae9d2baSRodolfo Giometti 							"pps%d", pps->id);
382668f06b9SEmil Goode 	if (IS_ERR(pps->dev)) {
383668f06b9SEmil Goode 		err = PTR_ERR(pps->dev);
384eae9d2baSRodolfo Giometti 		goto del_cdev;
385668f06b9SEmil Goode 	}
386eae9d2baSRodolfo Giometti 
387d953e0e8SGeorge Spelvin 	/* Override the release function with our own */
388083e5866SAlexander Gordeev 	pps->dev->release = pps_device_destruct;
389083e5866SAlexander Gordeev 
390eae9d2baSRodolfo Giometti 	pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
391eae9d2baSRodolfo Giometti 			MAJOR(pps_devt), pps->id);
392eae9d2baSRodolfo Giometti 
393eae9d2baSRodolfo Giometti 	return 0;
394eae9d2baSRodolfo Giometti 
395eae9d2baSRodolfo Giometti del_cdev:
396eae9d2baSRodolfo Giometti 	cdev_del(&pps->cdev);
397eae9d2baSRodolfo Giometti 
398083e5866SAlexander Gordeev free_idr:
3992a5cd6e2SAlexander Gordeev 	mutex_lock(&pps_idr_lock);
400083e5866SAlexander Gordeev 	idr_remove(&pps_idr, pps->id);
40119dd2da3STejun Heo out_unlock:
4022a5cd6e2SAlexander Gordeev 	mutex_unlock(&pps_idr_lock);
403eae9d2baSRodolfo Giometti 	return err;
404eae9d2baSRodolfo Giometti }
405eae9d2baSRodolfo Giometti 
pps_unregister_cdev(struct pps_device * pps)406eae9d2baSRodolfo Giometti void pps_unregister_cdev(struct pps_device *pps)
407eae9d2baSRodolfo Giometti {
408d953e0e8SGeorge Spelvin 	pr_debug("unregistering pps%d\n", pps->id);
409513b032cSGeorge Spelvin 	pps->lookup_cookie = NULL;
4105e196d34SAlexander Gordeev 	device_destroy(pps_class, pps->dev->devt);
411eae9d2baSRodolfo Giometti }
412eae9d2baSRodolfo Giometti 
413eae9d2baSRodolfo Giometti /*
414513b032cSGeorge Spelvin  * Look up a pps device by magic cookie.
415513b032cSGeorge Spelvin  * The cookie is usually a pointer to some enclosing device, but this
416513b032cSGeorge Spelvin  * code doesn't care; you should never be dereferencing it.
417513b032cSGeorge Spelvin  *
418513b032cSGeorge Spelvin  * This is a bit of a kludge that is currently used only by the PPS
419513b032cSGeorge Spelvin  * serial line discipline.  It may need to be tweaked when a second user
420513b032cSGeorge Spelvin  * is found.
421513b032cSGeorge Spelvin  *
422513b032cSGeorge Spelvin  * There is no function interface for setting the lookup_cookie field.
423513b032cSGeorge Spelvin  * It's initialized to NULL when the pps device is created, and if a
424513b032cSGeorge Spelvin  * client wants to use it, just fill it in afterward.
425513b032cSGeorge Spelvin  *
426513b032cSGeorge Spelvin  * The cookie is automatically set to NULL in pps_unregister_source()
427513b032cSGeorge Spelvin  * so that it will not be used again, even if the pps device cannot
428513b032cSGeorge Spelvin  * be removed from the idr due to pending references holding the minor
429513b032cSGeorge Spelvin  * number in use.
430513b032cSGeorge Spelvin  */
pps_lookup_dev(void const * cookie)431513b032cSGeorge Spelvin struct pps_device *pps_lookup_dev(void const *cookie)
432513b032cSGeorge Spelvin {
433513b032cSGeorge Spelvin 	struct pps_device *pps;
434513b032cSGeorge Spelvin 	unsigned id;
435513b032cSGeorge Spelvin 
436513b032cSGeorge Spelvin 	rcu_read_lock();
437513b032cSGeorge Spelvin 	idr_for_each_entry(&pps_idr, pps, id)
438513b032cSGeorge Spelvin 		if (cookie == pps->lookup_cookie)
439513b032cSGeorge Spelvin 			break;
440513b032cSGeorge Spelvin 	rcu_read_unlock();
441513b032cSGeorge Spelvin 	return pps;
442513b032cSGeorge Spelvin }
443513b032cSGeorge Spelvin EXPORT_SYMBOL(pps_lookup_dev);
444513b032cSGeorge Spelvin 
445513b032cSGeorge Spelvin /*
446eae9d2baSRodolfo Giometti  * Module stuff
447eae9d2baSRodolfo Giometti  */
448eae9d2baSRodolfo Giometti 
pps_exit(void)449eae9d2baSRodolfo Giometti static void __exit pps_exit(void)
450eae9d2baSRodolfo Giometti {
451eae9d2baSRodolfo Giometti 	class_destroy(pps_class);
452eae9d2baSRodolfo Giometti 	unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
453eae9d2baSRodolfo Giometti }
454eae9d2baSRodolfo Giometti 
pps_init(void)455eae9d2baSRodolfo Giometti static int __init pps_init(void)
456eae9d2baSRodolfo Giometti {
457eae9d2baSRodolfo Giometti 	int err;
458eae9d2baSRodolfo Giometti 
459*1aaba11dSGreg Kroah-Hartman 	pps_class = class_create("pps");
4607ad12566SDan Carpenter 	if (IS_ERR(pps_class)) {
4617f7cce74SAlexander Gordeev 		pr_err("failed to allocate class\n");
4627ad12566SDan Carpenter 		return PTR_ERR(pps_class);
463eae9d2baSRodolfo Giometti 	}
464bd0eae4eSGreg Kroah-Hartman 	pps_class->dev_groups = pps_groups;
465eae9d2baSRodolfo Giometti 
466eae9d2baSRodolfo Giometti 	err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
467eae9d2baSRodolfo Giometti 	if (err < 0) {
4687f7cce74SAlexander Gordeev 		pr_err("failed to allocate char device region\n");
469eae9d2baSRodolfo Giometti 		goto remove_class;
470eae9d2baSRodolfo Giometti 	}
471eae9d2baSRodolfo Giometti 
472eae9d2baSRodolfo Giometti 	pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS);
473eae9d2baSRodolfo Giometti 	pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti "
474eae9d2baSRodolfo Giometti 		"<giometti@linux.it>\n", PPS_VERSION);
475eae9d2baSRodolfo Giometti 
476eae9d2baSRodolfo Giometti 	return 0;
477eae9d2baSRodolfo Giometti 
478eae9d2baSRodolfo Giometti remove_class:
479eae9d2baSRodolfo Giometti 	class_destroy(pps_class);
480eae9d2baSRodolfo Giometti 
481eae9d2baSRodolfo Giometti 	return err;
482eae9d2baSRodolfo Giometti }
483eae9d2baSRodolfo Giometti 
484eae9d2baSRodolfo Giometti subsys_initcall(pps_init);
485eae9d2baSRodolfo Giometti module_exit(pps_exit);
486eae9d2baSRodolfo Giometti 
487eae9d2baSRodolfo Giometti MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
488eae9d2baSRodolfo Giometti MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
489eae9d2baSRodolfo Giometti MODULE_LICENSE("GPL");
490