Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * pps-ldisc.c -- PPS line discipline |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 2008 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 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/serial_core.h> |
| 24 | #include <linux/tty.h> |
| 25 | #include <linux/pps_kernel.h> |
| 26 | |
| 27 | #define PPS_TTY_MAGIC 0x0001 |
| 28 | |
| 29 | static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status, |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame^] | 30 | struct pps_event_time *ts) |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 31 | { |
| 32 | int id = (long)tty->disc_data; |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame^] | 33 | struct pps_event_time __ts; |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 34 | |
| 35 | /* First of all we get the time stamp... */ |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame^] | 36 | pps_get_ts(&__ts); |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 37 | |
| 38 | /* Does caller give us a timestamp? */ |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame^] | 39 | if (!ts) /* No. Do it ourself! */ |
| 40 | ts = &__ts; |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 41 | |
| 42 | /* Now do the PPS event report */ |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame^] | 43 | pps_event(id, ts, status ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR, |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 44 | NULL); |
| 45 | |
| 46 | pr_debug("PPS %s at %lu on source #%d\n", |
| 47 | status ? "assert" : "clear", jiffies, id); |
| 48 | } |
| 49 | |
| 50 | static int (*alias_n_tty_open)(struct tty_struct *tty); |
| 51 | |
| 52 | static int pps_tty_open(struct tty_struct *tty) |
| 53 | { |
| 54 | struct pps_source_info info; |
| 55 | struct tty_driver *drv = tty->driver; |
| 56 | int index = tty->index + drv->name_base; |
| 57 | int ret; |
| 58 | |
| 59 | info.owner = THIS_MODULE; |
| 60 | info.dev = NULL; |
| 61 | snprintf(info.name, PPS_MAX_NAME_LEN, "%s%d", drv->driver_name, index); |
| 62 | snprintf(info.path, PPS_MAX_NAME_LEN, "/dev/%s%d", drv->name, index); |
| 63 | info.mode = PPS_CAPTUREBOTH | \ |
| 64 | PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \ |
| 65 | PPS_CANWAIT | PPS_TSFMT_TSPEC; |
| 66 | |
| 67 | ret = pps_register_source(&info, PPS_CAPTUREBOTH | \ |
| 68 | PPS_OFFSETASSERT | PPS_OFFSETCLEAR); |
| 69 | if (ret < 0) { |
| 70 | pr_err("cannot register PPS source \"%s\"\n", info.path); |
| 71 | return ret; |
| 72 | } |
| 73 | tty->disc_data = (void *)(long)ret; |
| 74 | |
| 75 | /* Should open N_TTY ldisc too */ |
| 76 | ret = alias_n_tty_open(tty); |
| 77 | if (ret < 0) |
| 78 | pps_unregister_source((long)tty->disc_data); |
| 79 | |
| 80 | pr_info("PPS source #%d \"%s\" added\n", ret, info.path); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static void (*alias_n_tty_close)(struct tty_struct *tty); |
| 86 | |
| 87 | static void pps_tty_close(struct tty_struct *tty) |
| 88 | { |
| 89 | int id = (long)tty->disc_data; |
| 90 | |
| 91 | pps_unregister_source(id); |
| 92 | alias_n_tty_close(tty); |
| 93 | |
| 94 | pr_info("PPS source #%d removed\n", id); |
| 95 | } |
| 96 | |
| 97 | static struct tty_ldisc_ops pps_ldisc_ops; |
| 98 | |
| 99 | /* |
| 100 | * Module stuff |
| 101 | */ |
| 102 | |
| 103 | static int __init pps_tty_init(void) |
| 104 | { |
| 105 | int err; |
| 106 | |
| 107 | /* Inherit the N_TTY's ops */ |
| 108 | n_tty_inherit_ops(&pps_ldisc_ops); |
| 109 | |
| 110 | /* Save N_TTY's open()/close() methods */ |
| 111 | alias_n_tty_open = pps_ldisc_ops.open; |
| 112 | alias_n_tty_close = pps_ldisc_ops.close; |
| 113 | |
| 114 | /* Init PPS_TTY data */ |
| 115 | pps_ldisc_ops.owner = THIS_MODULE; |
| 116 | pps_ldisc_ops.magic = PPS_TTY_MAGIC; |
| 117 | pps_ldisc_ops.name = "pps_tty"; |
| 118 | pps_ldisc_ops.dcd_change = pps_tty_dcd_change; |
| 119 | pps_ldisc_ops.open = pps_tty_open; |
| 120 | pps_ldisc_ops.close = pps_tty_close; |
| 121 | |
| 122 | err = tty_register_ldisc(N_PPS, &pps_ldisc_ops); |
| 123 | if (err) |
| 124 | pr_err("can't register PPS line discipline\n"); |
| 125 | else |
| 126 | pr_info("PPS line discipline registered\n"); |
| 127 | |
| 128 | return err; |
| 129 | } |
| 130 | |
| 131 | static void __exit pps_tty_cleanup(void) |
| 132 | { |
| 133 | int err; |
| 134 | |
| 135 | err = tty_unregister_ldisc(N_PPS); |
| 136 | if (err) |
| 137 | pr_err("can't unregister PPS line discipline\n"); |
| 138 | else |
| 139 | pr_info("PPS line discipline removed\n"); |
| 140 | } |
| 141 | |
| 142 | module_init(pps_tty_init); |
| 143 | module_exit(pps_tty_cleanup); |
| 144 | |
| 145 | MODULE_ALIAS_LDISC(N_PPS); |
| 146 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
| 147 | MODULE_DESCRIPTION("PPS TTY device driver"); |
| 148 | MODULE_LICENSE("GPL"); |