1 /* 2 * PPS core file 3 * 4 * 5 * Copyright (C) 2005-2009 Rodolfo Giometti <[email protected]> 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 22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 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/mutex.h> 31 #include <linux/cdev.h> 32 #include <linux/poll.h> 33 #include <linux/pps_kernel.h> 34 #include <linux/slab.h> 35 36 /* 37 * Local variables 38 */ 39 40 static dev_t pps_devt; 41 static struct class *pps_class; 42 43 static DEFINE_MUTEX(pps_idr_lock); 44 static DEFINE_IDR(pps_idr); 45 46 /* 47 * Char device methods 48 */ 49 50 static unsigned int pps_cdev_poll(struct file *file, poll_table *wait) 51 { 52 struct pps_device *pps = file->private_data; 53 54 poll_wait(file, &pps->queue, wait); 55 56 return POLLIN | POLLRDNORM; 57 } 58 59 static int pps_cdev_fasync(int fd, struct file *file, int on) 60 { 61 struct pps_device *pps = file->private_data; 62 return fasync_helper(fd, file, on, &pps->async_queue); 63 } 64 65 static long pps_cdev_ioctl(struct file *file, 66 unsigned int cmd, unsigned long arg) 67 { 68 struct pps_device *pps = file->private_data; 69 struct pps_kparams params; 70 void __user *uarg = (void __user *) arg; 71 int __user *iuarg = (int __user *) arg; 72 int err; 73 74 switch (cmd) { 75 case PPS_GETPARAMS: 76 dev_dbg(pps->dev, "PPS_GETPARAMS\n"); 77 78 spin_lock_irq(&pps->lock); 79 80 /* Get the current parameters */ 81 params = pps->params; 82 83 spin_unlock_irq(&pps->lock); 84 85 err = copy_to_user(uarg, ¶ms, sizeof(struct pps_kparams)); 86 if (err) 87 return -EFAULT; 88 89 break; 90 91 case PPS_SETPARAMS: 92 dev_dbg(pps->dev, "PPS_SETPARAMS\n"); 93 94 /* Check the capabilities */ 95 if (!capable(CAP_SYS_TIME)) 96 return -EPERM; 97 98 err = copy_from_user(¶ms, uarg, sizeof(struct pps_kparams)); 99 if (err) 100 return -EFAULT; 101 if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) { 102 dev_dbg(pps->dev, "capture mode unspecified (%x)\n", 103 params.mode); 104 return -EINVAL; 105 } 106 107 /* Check for supported capabilities */ 108 if ((params.mode & ~pps->info.mode) != 0) { 109 dev_dbg(pps->dev, "unsupported capabilities (%x)\n", 110 params.mode); 111 return -EINVAL; 112 } 113 114 spin_lock_irq(&pps->lock); 115 116 /* Save the new parameters */ 117 pps->params = params; 118 119 /* Restore the read only parameters */ 120 if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) { 121 /* section 3.3 of RFC 2783 interpreted */ 122 dev_dbg(pps->dev, "time format unspecified (%x)\n", 123 params.mode); 124 pps->params.mode |= PPS_TSFMT_TSPEC; 125 } 126 if (pps->info.mode & PPS_CANWAIT) 127 pps->params.mode |= PPS_CANWAIT; 128 pps->params.api_version = PPS_API_VERS; 129 130 spin_unlock_irq(&pps->lock); 131 132 break; 133 134 case PPS_GETCAP: 135 dev_dbg(pps->dev, "PPS_GETCAP\n"); 136 137 err = put_user(pps->info.mode, iuarg); 138 if (err) 139 return -EFAULT; 140 141 break; 142 143 case PPS_FETCH: { 144 struct pps_fdata fdata; 145 unsigned int ev; 146 147 dev_dbg(pps->dev, "PPS_FETCH\n"); 148 149 err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata)); 150 if (err) 151 return -EFAULT; 152 153 ev = pps->last_ev; 154 155 /* Manage the timeout */ 156 if (fdata.timeout.flags & PPS_TIME_INVALID) 157 err = wait_event_interruptible(pps->queue, 158 ev != pps->last_ev); 159 else { 160 unsigned long ticks; 161 162 dev_dbg(pps->dev, "timeout %lld.%09d\n", 163 (long long) fdata.timeout.sec, 164 fdata.timeout.nsec); 165 ticks = fdata.timeout.sec * HZ; 166 ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ); 167 168 if (ticks != 0) { 169 err = wait_event_interruptible_timeout( 170 pps->queue, 171 ev != pps->last_ev, 172 ticks); 173 if (err == 0) 174 return -ETIMEDOUT; 175 } 176 } 177 178 /* Check for pending signals */ 179 if (err == -ERESTARTSYS) { 180 dev_dbg(pps->dev, "pending signal caught\n"); 181 return -EINTR; 182 } 183 184 /* Return the fetched timestamp */ 185 spin_lock_irq(&pps->lock); 186 187 fdata.info.assert_sequence = pps->assert_sequence; 188 fdata.info.clear_sequence = pps->clear_sequence; 189 fdata.info.assert_tu = pps->assert_tu; 190 fdata.info.clear_tu = pps->clear_tu; 191 fdata.info.current_mode = pps->current_mode; 192 193 spin_unlock_irq(&pps->lock); 194 195 err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata)); 196 if (err) 197 return -EFAULT; 198 199 break; 200 } 201 default: 202 return -ENOTTY; 203 break; 204 } 205 206 return 0; 207 } 208 209 static int pps_cdev_open(struct inode *inode, struct file *file) 210 { 211 struct pps_device *pps = container_of(inode->i_cdev, 212 struct pps_device, cdev); 213 file->private_data = pps; 214 215 return 0; 216 } 217 218 static int pps_cdev_release(struct inode *inode, struct file *file) 219 { 220 return 0; 221 } 222 223 /* 224 * Char device stuff 225 */ 226 227 static const struct file_operations pps_cdev_fops = { 228 .owner = THIS_MODULE, 229 .llseek = no_llseek, 230 .poll = pps_cdev_poll, 231 .fasync = pps_cdev_fasync, 232 .unlocked_ioctl = pps_cdev_ioctl, 233 .open = pps_cdev_open, 234 .release = pps_cdev_release, 235 }; 236 237 static void pps_device_destruct(struct device *dev) 238 { 239 struct pps_device *pps = dev_get_drvdata(dev); 240 241 /* release id here to protect others from using it while it's 242 * still in use */ 243 mutex_lock(&pps_idr_lock); 244 idr_remove(&pps_idr, pps->id); 245 mutex_unlock(&pps_idr_lock); 246 247 kfree(dev); 248 kfree(pps); 249 } 250 251 int pps_register_cdev(struct pps_device *pps) 252 { 253 int err; 254 dev_t devt; 255 256 mutex_lock(&pps_idr_lock); 257 /* Get new ID for the new PPS source */ 258 if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0) { 259 mutex_unlock(&pps_idr_lock); 260 return -ENOMEM; 261 } 262 263 /* Now really allocate the PPS source. 264 * After idr_get_new() calling the new source will be freely available 265 * into the kernel. 266 */ 267 err = idr_get_new(&pps_idr, pps, &pps->id); 268 mutex_unlock(&pps_idr_lock); 269 270 if (err < 0) 271 return err; 272 273 pps->id &= MAX_ID_MASK; 274 if (pps->id >= PPS_MAX_SOURCES) { 275 pr_err("%s: too many PPS sources in the system\n", 276 pps->info.name); 277 err = -EBUSY; 278 goto free_idr; 279 } 280 281 devt = MKDEV(MAJOR(pps_devt), pps->id); 282 283 cdev_init(&pps->cdev, &pps_cdev_fops); 284 pps->cdev.owner = pps->info.owner; 285 286 err = cdev_add(&pps->cdev, devt, 1); 287 if (err) { 288 pr_err("%s: failed to add char device %d:%d\n", 289 pps->info.name, MAJOR(pps_devt), pps->id); 290 goto free_idr; 291 } 292 pps->dev = device_create(pps_class, pps->info.dev, devt, pps, 293 "pps%d", pps->id); 294 if (IS_ERR(pps->dev)) 295 goto del_cdev; 296 297 pps->dev->release = pps_device_destruct; 298 299 pr_debug("source %s got cdev (%d:%d)\n", pps->info.name, 300 MAJOR(pps_devt), pps->id); 301 302 return 0; 303 304 del_cdev: 305 cdev_del(&pps->cdev); 306 307 free_idr: 308 mutex_lock(&pps_idr_lock); 309 idr_remove(&pps_idr, pps->id); 310 mutex_unlock(&pps_idr_lock); 311 312 return err; 313 } 314 315 void pps_unregister_cdev(struct pps_device *pps) 316 { 317 device_destroy(pps_class, pps->dev->devt); 318 cdev_del(&pps->cdev); 319 } 320 321 /* 322 * Module stuff 323 */ 324 325 static void __exit pps_exit(void) 326 { 327 class_destroy(pps_class); 328 unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES); 329 } 330 331 static int __init pps_init(void) 332 { 333 int err; 334 335 pps_class = class_create(THIS_MODULE, "pps"); 336 if (!pps_class) { 337 pr_err("failed to allocate class\n"); 338 return -ENOMEM; 339 } 340 pps_class->dev_attrs = pps_attrs; 341 342 err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps"); 343 if (err < 0) { 344 pr_err("failed to allocate char device region\n"); 345 goto remove_class; 346 } 347 348 pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS); 349 pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti " 350 "<[email protected]>\n", PPS_VERSION); 351 352 return 0; 353 354 remove_class: 355 class_destroy(pps_class); 356 357 return err; 358 } 359 360 subsys_initcall(pps_init); 361 module_exit(pps_exit); 362 363 MODULE_AUTHOR("Rodolfo Giometti <[email protected]>"); 364 MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION); 365 MODULE_LICENSE("GPL"); 366