146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev/* 246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * pps_gen_parport.c -- kernel parallel port PPS signal generator 346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * 446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * 546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * Copyright (C) 2009 Alexander Gordeev <lasaine@lvk.cs.msu.su> 646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * 746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * This program is free software; you can redistribute it and/or modify 846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * it under the terms of the GNU General Public License as published by 946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * the Free Software Foundation; either version 2 of the License, or 1046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * (at your option) any later version. 1146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * 1246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * This program is distributed in the hope that it will be useful, 1346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * but WITHOUT ANY WARRANTY; without even the implied warranty of 1446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * GNU General Public License for more details. 1646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * 1746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * You should have received a copy of the GNU General Public License 1846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * along with this program; if not, write to the Free Software 1946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 2046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev */ 2146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 2246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 2346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev/* 2446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * TODO: 2546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * fix issues when realtime clock is adjusted in a leap 2646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev */ 2746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 2846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 2946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 3046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#include <linux/kernel.h> 3146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#include <linux/module.h> 3246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#include <linux/init.h> 3346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#include <linux/time.h> 3446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#include <linux/hrtimer.h> 3546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#include <linux/parport.h> 3646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 3746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#define DRVDESC "parallel port PPS signal generator" 3846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 3946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#define SIGNAL 0 4046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#define NO_SIGNAL PARPORT_CONTROL_STROBE 4146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 4246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev/* module parameters */ 4346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 4446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#define SEND_DELAY_MAX 100000 4546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 4646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstatic unsigned int send_delay = 30000; 4746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander GordeevMODULE_PARM_DESC(delay, 4846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev "Delay between setting and dropping the signal (ns)"); 4946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevmodule_param_named(delay, send_delay, uint, 0); 5046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 5146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 5246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#define SAFETY_INTERVAL 3000 /* set the hrtimer earlier for safety (ns) */ 5346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 5446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev/* internal per port structure */ 5546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstruct pps_generator_pp { 5646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev struct pardevice *pardev; /* parport device */ 5746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev struct hrtimer timer; 5846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev long port_write_time; /* calibrated port write time (ns) */ 5946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev}; 6046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 6146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstatic struct pps_generator_pp device = { 6246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev .pardev = NULL, 6346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev}; 6446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 6546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstatic int attached; 6646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 6746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev/* calibrated time between a hrtimer event and the reaction */ 6846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstatic long hrtimer_error = SAFETY_INTERVAL; 6946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 7046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev/* the kernel hrtimer event */ 7146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstatic enum hrtimer_restart hrtimer_event(struct hrtimer *timer) 7246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev{ 7346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev struct timespec expire_time, ts1, ts2, ts3, dts; 7446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev struct pps_generator_pp *dev; 7546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev struct parport *port; 7646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev long lim, delta; 7746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev unsigned long flags; 7846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 79563558b2c7350371551bf08348ac61be62200505Alexander Gordeev /* We have to disable interrupts here. The idea is to prevent 80563558b2c7350371551bf08348ac61be62200505Alexander Gordeev * other interrupts on the same processor to introduce random 81563558b2c7350371551bf08348ac61be62200505Alexander Gordeev * lags while polling the clock. getnstimeofday() takes <1us on 82563558b2c7350371551bf08348ac61be62200505Alexander Gordeev * most machines while other interrupt handlers can take much 83563558b2c7350371551bf08348ac61be62200505Alexander Gordeev * more potentially. 84563558b2c7350371551bf08348ac61be62200505Alexander Gordeev * 85563558b2c7350371551bf08348ac61be62200505Alexander Gordeev * NB: approx time with blocked interrupts = 86563558b2c7350371551bf08348ac61be62200505Alexander Gordeev * send_delay + 3 * SAFETY_INTERVAL 87563558b2c7350371551bf08348ac61be62200505Alexander Gordeev */ 8846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev local_irq_save(flags); 8946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 9046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev /* first of all we get the time stamp... */ 9146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev getnstimeofday(&ts1); 9246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev expire_time = ktime_to_timespec(hrtimer_get_softexpires(timer)); 9346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev dev = container_of(timer, struct pps_generator_pp, timer); 9446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev lim = NSEC_PER_SEC - send_delay - dev->port_write_time; 9546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 9646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev /* check if we are late */ 9746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev if (expire_time.tv_sec != ts1.tv_sec || ts1.tv_nsec > lim) { 9846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev local_irq_restore(flags); 9946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev pr_err("we are late this time %ld.%09ld\n", 10046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev ts1.tv_sec, ts1.tv_nsec); 10146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev goto done; 10246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev } 10346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 10446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev /* busy loop until the time is right for an assert edge */ 10546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev do { 10646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev getnstimeofday(&ts2); 10746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev } while (expire_time.tv_sec == ts2.tv_sec && ts2.tv_nsec < lim); 10846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 10946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev /* set the signal */ 11046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev port = dev->pardev->port; 11146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev port->ops->write_control(port, SIGNAL); 11246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 11346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev /* busy loop until the time is right for a clear edge */ 11446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev lim = NSEC_PER_SEC - dev->port_write_time; 11546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev do { 11646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev getnstimeofday(&ts2); 11746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev } while (expire_time.tv_sec == ts2.tv_sec && ts2.tv_nsec < lim); 11846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 11946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev /* unset the signal */ 12046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev port->ops->write_control(port, NO_SIGNAL); 12146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 12246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev getnstimeofday(&ts3); 12346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 12446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev local_irq_restore(flags); 12546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 12646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev /* update calibrated port write time */ 12746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev dts = timespec_sub(ts3, ts2); 12846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev dev->port_write_time = 12946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev (dev->port_write_time + timespec_to_ns(&dts)) >> 1; 13046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 13146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevdone: 13246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev /* update calibrated hrtimer error */ 13346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev dts = timespec_sub(ts1, expire_time); 13446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev delta = timespec_to_ns(&dts); 13546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev /* If the new error value is bigger then the old, use the new 13646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * value, if not then slowly move towards the new value. This 13746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * way it should be safe in bad conditions and efficient in 13846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev * good conditions. 13946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev */ 14046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev if (delta >= hrtimer_error) 14146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev hrtimer_error = delta; 14246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev else 14346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev hrtimer_error = (3 * hrtimer_error + delta) >> 2; 14446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 14546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev /* update the hrtimer expire time */ 14646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev hrtimer_set_expires(timer, 14746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev ktime_set(expire_time.tv_sec + 1, 14846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev NSEC_PER_SEC - (send_delay + 14946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev dev->port_write_time + SAFETY_INTERVAL + 15046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 2 * hrtimer_error))); 15146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 15246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev return HRTIMER_RESTART; 15346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev} 15446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 15546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev/* calibrate port write time */ 15646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev#define PORT_NTESTS_SHIFT 5 15746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstatic void calibrate_port(struct pps_generator_pp *dev) 15846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev{ 15946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev struct parport *port = dev->pardev->port; 16046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev int i; 16146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev long acc = 0; 16246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 16346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev for (i = 0; i < (1 << PORT_NTESTS_SHIFT); i++) { 16446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev struct timespec a, b; 16546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev unsigned long irq_flags; 16646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 16746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev local_irq_save(irq_flags); 16846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev getnstimeofday(&a); 16946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev port->ops->write_control(port, NO_SIGNAL); 17046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev getnstimeofday(&b); 17146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev local_irq_restore(irq_flags); 17246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 17346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev b = timespec_sub(b, a); 17446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev acc += timespec_to_ns(&b); 17546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev } 17646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 17746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev dev->port_write_time = acc >> PORT_NTESTS_SHIFT; 17846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev pr_info("port write takes %ldns\n", dev->port_write_time); 17946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev} 18046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 18146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstatic inline ktime_t next_intr_time(struct pps_generator_pp *dev) 18246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev{ 18346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev struct timespec ts; 18446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 18546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev getnstimeofday(&ts); 18646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 18746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev return ktime_set(ts.tv_sec + 18846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev ((ts.tv_nsec > 990 * NSEC_PER_MSEC) ? 1 : 0), 18946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev NSEC_PER_SEC - (send_delay + 19046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev dev->port_write_time + 3 * SAFETY_INTERVAL)); 19146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev} 19246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 19346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstatic void parport_attach(struct parport *port) 19446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev{ 19546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev if (attached) { 19646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev /* we already have a port */ 19746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev return; 19846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev } 19946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 20046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev device.pardev = parport_register_device(port, KBUILD_MODNAME, 2014f542e3dd90a96ee0f8fcb8173cb4104f5f753e6Alexander Gordeev NULL, NULL, NULL, PARPORT_FLAG_EXCL, &device); 20246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev if (!device.pardev) { 20346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev pr_err("couldn't register with %s\n", port->name); 20446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev return; 20546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev } 20646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 20746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev if (parport_claim_or_block(device.pardev) < 0) { 20846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev pr_err("couldn't claim %s\n", port->name); 20946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev goto err_unregister_dev; 21046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev } 21146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 21246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev pr_info("attached to %s\n", port->name); 21346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev attached = 1; 21446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 21546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev calibrate_port(&device); 21646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 21746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev hrtimer_init(&device.timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); 21846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev device.timer.function = hrtimer_event; 21946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev hrtimer_start(&device.timer, next_intr_time(&device), HRTIMER_MODE_ABS); 22046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 22146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev return; 22246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 22346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeeverr_unregister_dev: 22446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev parport_unregister_device(device.pardev); 22546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev} 22646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 22746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstatic void parport_detach(struct parport *port) 22846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev{ 22946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev if (port->cad != device.pardev) 23046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev return; /* not our port */ 23146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 23246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev hrtimer_cancel(&device.timer); 23346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev parport_release(device.pardev); 23446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev parport_unregister_device(device.pardev); 23546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev} 23646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 23746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstatic struct parport_driver pps_gen_parport_driver = { 23846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev .name = KBUILD_MODNAME, 23946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev .attach = parport_attach, 24046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev .detach = parport_detach, 24146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev}; 24246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 24346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev/* module staff */ 24446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 24546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstatic int __init pps_gen_parport_init(void) 24646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev{ 24746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev int ret; 24846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 24946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev pr_info(DRVDESC "\n"); 25046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 25146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev if (send_delay > SEND_DELAY_MAX) { 25246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev pr_err("delay value should be not greater" 25346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev " then %d\n", SEND_DELAY_MAX); 25446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev return -EINVAL; 25546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev } 25646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 25746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev ret = parport_register_driver(&pps_gen_parport_driver); 25846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev if (ret) { 25946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev pr_err("unable to register with parport\n"); 26046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev return ret; 26146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev } 26246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 26346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev return 0; 26446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev} 26546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 26646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevstatic void __exit pps_gen_parport_exit(void) 26746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev{ 26846b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev parport_unregister_driver(&pps_gen_parport_driver); 26946b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev pr_info("hrtimer avg error is %ldns\n", hrtimer_error); 27046b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev} 27146b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 27246b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevmodule_init(pps_gen_parport_init); 27346b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeevmodule_exit(pps_gen_parport_exit); 27446b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander Gordeev 27546b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander GordeevMODULE_AUTHOR("Alexander Gordeev <lasaine@lvk.cs.msu.su>"); 27646b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander GordeevMODULE_DESCRIPTION(DRVDESC); 27746b402a0e5e4b4d81b11c32dfb2312bf5828ecb5Alexander GordeevMODULE_LICENSE("GPL"); 278