Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PPS core file |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/sched.h> |
| 28 | #include <linux/uaccess.h> |
| 29 | #include <linux/idr.h> |
| 30 | #include <linux/cdev.h> |
| 31 | #include <linux/poll.h> |
| 32 | #include <linux/pps_kernel.h> |
| 33 | |
| 34 | /* |
| 35 | * Local variables |
| 36 | */ |
| 37 | |
| 38 | static dev_t pps_devt; |
| 39 | static struct class *pps_class; |
| 40 | |
| 41 | /* |
| 42 | * Char device methods |
| 43 | */ |
| 44 | |
| 45 | static unsigned int pps_cdev_poll(struct file *file, poll_table *wait) |
| 46 | { |
| 47 | struct pps_device *pps = file->private_data; |
| 48 | |
| 49 | poll_wait(file, &pps->queue, wait); |
| 50 | |
| 51 | return POLLIN | POLLRDNORM; |
| 52 | } |
| 53 | |
| 54 | static int pps_cdev_fasync(int fd, struct file *file, int on) |
| 55 | { |
| 56 | struct pps_device *pps = file->private_data; |
| 57 | return fasync_helper(fd, file, on, &pps->async_queue); |
| 58 | } |
| 59 | |
| 60 | static long pps_cdev_ioctl(struct file *file, |
| 61 | unsigned int cmd, unsigned long arg) |
| 62 | { |
| 63 | struct pps_device *pps = file->private_data; |
| 64 | struct pps_kparams params; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 65 | void __user *uarg = (void __user *) arg; |
| 66 | int __user *iuarg = (int __user *) arg; |
| 67 | int err; |
| 68 | |
| 69 | switch (cmd) { |
| 70 | case PPS_GETPARAMS: |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 71 | dev_dbg(pps->dev, "PPS_GETPARAMS\n"); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 72 | |
Rodolfo Giometti | cbf83cc5 | 2009-11-11 14:26:52 -0800 | [diff] [blame] | 73 | spin_lock_irq(&pps->lock); |
| 74 | |
| 75 | /* Get the current parameters */ |
| 76 | params = pps->params; |
| 77 | |
| 78 | spin_unlock_irq(&pps->lock); |
| 79 | |
| 80 | err = copy_to_user(uarg, ¶ms, sizeof(struct pps_kparams)); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 81 | if (err) |
| 82 | return -EFAULT; |
| 83 | |
| 84 | break; |
| 85 | |
| 86 | case PPS_SETPARAMS: |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 87 | dev_dbg(pps->dev, "PPS_SETPARAMS\n"); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 88 | |
| 89 | /* Check the capabilities */ |
| 90 | if (!capable(CAP_SYS_TIME)) |
| 91 | return -EPERM; |
| 92 | |
| 93 | err = copy_from_user(¶ms, uarg, sizeof(struct pps_kparams)); |
| 94 | if (err) |
| 95 | return -EFAULT; |
| 96 | if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) { |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 97 | dev_dbg(pps->dev, "capture mode unspecified (%x)\n", |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 98 | params.mode); |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | |
| 102 | /* Check for supported capabilities */ |
| 103 | if ((params.mode & ~pps->info.mode) != 0) { |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 104 | dev_dbg(pps->dev, "unsupported capabilities (%x)\n", |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 105 | params.mode); |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | |
| 109 | spin_lock_irq(&pps->lock); |
| 110 | |
| 111 | /* Save the new parameters */ |
| 112 | pps->params = params; |
| 113 | |
| 114 | /* Restore the read only parameters */ |
| 115 | if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) { |
| 116 | /* section 3.3 of RFC 2783 interpreted */ |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 117 | dev_dbg(pps->dev, "time format unspecified (%x)\n", |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 118 | params.mode); |
| 119 | pps->params.mode |= PPS_TSFMT_TSPEC; |
| 120 | } |
| 121 | if (pps->info.mode & PPS_CANWAIT) |
| 122 | pps->params.mode |= PPS_CANWAIT; |
| 123 | pps->params.api_version = PPS_API_VERS; |
| 124 | |
| 125 | spin_unlock_irq(&pps->lock); |
| 126 | |
| 127 | break; |
| 128 | |
| 129 | case PPS_GETCAP: |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 130 | dev_dbg(pps->dev, "PPS_GETCAP\n"); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 131 | |
| 132 | err = put_user(pps->info.mode, iuarg); |
| 133 | if (err) |
| 134 | return -EFAULT; |
| 135 | |
| 136 | break; |
| 137 | |
Alexander Gordeev | 86d921f | 2011-01-12 17:00:49 -0800 | [diff] [blame] | 138 | case PPS_FETCH: { |
| 139 | struct pps_fdata fdata; |
Alexander Gordeev | 3003d55 | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 140 | unsigned int ev; |
Alexander Gordeev | 86d921f | 2011-01-12 17:00:49 -0800 | [diff] [blame] | 141 | |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 142 | dev_dbg(pps->dev, "PPS_FETCH\n"); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 143 | |
| 144 | err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata)); |
| 145 | if (err) |
| 146 | return -EFAULT; |
| 147 | |
Alexander Gordeev | 3003d55 | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 148 | ev = pps->last_ev; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 149 | |
| 150 | /* Manage the timeout */ |
| 151 | if (fdata.timeout.flags & PPS_TIME_INVALID) |
Alexander Gordeev | 3003d55 | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 152 | err = wait_event_interruptible(pps->queue, |
| 153 | ev != pps->last_ev); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 154 | else { |
Alexander Gordeev | 86d921f | 2011-01-12 17:00:49 -0800 | [diff] [blame] | 155 | unsigned long ticks; |
| 156 | |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 157 | dev_dbg(pps->dev, "timeout %lld.%09d\n", |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 158 | (long long) fdata.timeout.sec, |
| 159 | fdata.timeout.nsec); |
| 160 | ticks = fdata.timeout.sec * HZ; |
| 161 | ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ); |
| 162 | |
| 163 | if (ticks != 0) { |
| 164 | err = wait_event_interruptible_timeout( |
Alexander Gordeev | 3003d55 | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 165 | pps->queue, |
| 166 | ev != pps->last_ev, |
| 167 | ticks); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 168 | if (err == 0) |
| 169 | return -ETIMEDOUT; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* Check for pending signals */ |
| 174 | if (err == -ERESTARTSYS) { |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 175 | dev_dbg(pps->dev, "pending signal caught\n"); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 176 | return -EINTR; |
| 177 | } |
| 178 | |
| 179 | /* Return the fetched timestamp */ |
| 180 | spin_lock_irq(&pps->lock); |
| 181 | |
| 182 | fdata.info.assert_sequence = pps->assert_sequence; |
| 183 | fdata.info.clear_sequence = pps->clear_sequence; |
| 184 | fdata.info.assert_tu = pps->assert_tu; |
| 185 | fdata.info.clear_tu = pps->clear_tu; |
| 186 | fdata.info.current_mode = pps->current_mode; |
| 187 | |
| 188 | spin_unlock_irq(&pps->lock); |
| 189 | |
| 190 | err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata)); |
| 191 | if (err) |
| 192 | return -EFAULT; |
| 193 | |
| 194 | break; |
Alexander Gordeev | 86d921f | 2011-01-12 17:00:49 -0800 | [diff] [blame] | 195 | } |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 196 | default: |
| 197 | return -ENOTTY; |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int pps_cdev_open(struct inode *inode, struct file *file) |
| 205 | { |
| 206 | struct pps_device *pps = container_of(inode->i_cdev, |
| 207 | struct pps_device, cdev); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 208 | file->private_data = pps; |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static int pps_cdev_release(struct inode *inode, struct file *file) |
| 214 | { |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Char device stuff |
| 220 | */ |
| 221 | |
| 222 | static const struct file_operations pps_cdev_fops = { |
| 223 | .owner = THIS_MODULE, |
| 224 | .llseek = no_llseek, |
| 225 | .poll = pps_cdev_poll, |
| 226 | .fasync = pps_cdev_fasync, |
| 227 | .unlocked_ioctl = pps_cdev_ioctl, |
| 228 | .open = pps_cdev_open, |
| 229 | .release = pps_cdev_release, |
| 230 | }; |
| 231 | |
| 232 | int pps_register_cdev(struct pps_device *pps) |
| 233 | { |
| 234 | int err; |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 235 | dev_t devt; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 236 | |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 237 | devt = MKDEV(MAJOR(pps_devt), pps->id); |
| 238 | |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 239 | cdev_init(&pps->cdev, &pps_cdev_fops); |
| 240 | pps->cdev.owner = pps->info.owner; |
| 241 | |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 242 | err = cdev_add(&pps->cdev, devt, 1); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 243 | if (err) { |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 244 | pr_err("%s: failed to add char device %d:%d\n", |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 245 | pps->info.name, MAJOR(pps_devt), pps->id); |
| 246 | return err; |
| 247 | } |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 248 | pps->dev = device_create(pps_class, pps->info.dev, devt, pps, |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 249 | "pps%d", pps->id); |
Joonwoo Park | 054b2b1 | 2009-08-26 14:29:18 -0700 | [diff] [blame] | 250 | if (IS_ERR(pps->dev)) |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 251 | goto del_cdev; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 252 | |
| 253 | pr_debug("source %s got cdev (%d:%d)\n", pps->info.name, |
| 254 | MAJOR(pps_devt), pps->id); |
| 255 | |
| 256 | return 0; |
| 257 | |
| 258 | del_cdev: |
| 259 | cdev_del(&pps->cdev); |
| 260 | |
| 261 | return err; |
| 262 | } |
| 263 | |
| 264 | void pps_unregister_cdev(struct pps_device *pps) |
| 265 | { |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 266 | device_destroy(pps_class, pps->dev->devt); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 267 | cdev_del(&pps->cdev); |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Module stuff |
| 272 | */ |
| 273 | |
| 274 | static void __exit pps_exit(void) |
| 275 | { |
| 276 | class_destroy(pps_class); |
| 277 | unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES); |
| 278 | } |
| 279 | |
| 280 | static int __init pps_init(void) |
| 281 | { |
| 282 | int err; |
| 283 | |
| 284 | pps_class = class_create(THIS_MODULE, "pps"); |
| 285 | if (!pps_class) { |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 286 | pr_err("failed to allocate class\n"); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 287 | return -ENOMEM; |
| 288 | } |
| 289 | pps_class->dev_attrs = pps_attrs; |
| 290 | |
| 291 | err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps"); |
| 292 | if (err < 0) { |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame^] | 293 | pr_err("failed to allocate char device region\n"); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 294 | goto remove_class; |
| 295 | } |
| 296 | |
| 297 | pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS); |
| 298 | pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti " |
| 299 | "<giometti@linux.it>\n", PPS_VERSION); |
| 300 | |
| 301 | return 0; |
| 302 | |
| 303 | remove_class: |
| 304 | class_destroy(pps_class); |
| 305 | |
| 306 | return err; |
| 307 | } |
| 308 | |
| 309 | subsys_initcall(pps_init); |
| 310 | module_exit(pps_exit); |
| 311 | |
| 312 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
| 313 | MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION); |
| 314 | MODULE_LICENSE("GPL"); |