Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 2 | /* |
| 3 | * pps-ldisc.c -- PPS line discipline |
| 4 | * |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 5 | * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it> |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/serial_core.h> |
| 12 | #include <linux/tty.h> |
| 13 | #include <linux/pps_kernel.h> |
George Spelvin | ce3da1a | 2013-02-10 04:43:41 -0500 | [diff] [blame] | 14 | #include <linux/bug.h> |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 15 | |
| 16 | #define PPS_TTY_MAGIC 0x0001 |
| 17 | |
George Spelvin | 593fb1ae4 | 2013-02-12 02:00:43 -0500 | [diff] [blame] | 18 | static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status) |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 19 | { |
George Spelvin | 593fb1ae4 | 2013-02-12 02:00:43 -0500 | [diff] [blame] | 20 | struct pps_device *pps; |
| 21 | struct pps_event_time ts; |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 22 | |
George Spelvin | 593fb1ae4 | 2013-02-12 02:00:43 -0500 | [diff] [blame] | 23 | pps_get_ts(&ts); |
| 24 | |
| 25 | pps = pps_lookup_dev(tty); |
George Spelvin | ce3da1a | 2013-02-10 04:43:41 -0500 | [diff] [blame] | 26 | /* |
| 27 | * This should never fail, but the ldisc locking is very |
| 28 | * convoluted, so don't crash just in case. |
| 29 | */ |
| 30 | if (WARN_ON_ONCE(pps == NULL)) |
| 31 | return; |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 32 | |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 33 | /* Now do the PPS event report */ |
George Spelvin | 593fb1ae4 | 2013-02-12 02:00:43 -0500 | [diff] [blame] | 34 | pps_event(pps, &ts, status ? PPS_CAPTUREASSERT : |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 35 | PPS_CAPTURECLEAR, NULL); |
| 36 | |
| 37 | dev_dbg(pps->dev, "PPS %s at %lu\n", |
| 38 | status ? "assert" : "clear", jiffies); |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static int (*alias_n_tty_open)(struct tty_struct *tty); |
| 42 | |
| 43 | static int pps_tty_open(struct tty_struct *tty) |
| 44 | { |
| 45 | struct pps_source_info info; |
| 46 | struct tty_driver *drv = tty->driver; |
| 47 | int index = tty->index + drv->name_base; |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 48 | struct pps_device *pps; |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 49 | int ret; |
| 50 | |
| 51 | info.owner = THIS_MODULE; |
| 52 | info.dev = NULL; |
| 53 | snprintf(info.name, PPS_MAX_NAME_LEN, "%s%d", drv->driver_name, index); |
| 54 | snprintf(info.path, PPS_MAX_NAME_LEN, "/dev/%s%d", drv->name, index); |
| 55 | info.mode = PPS_CAPTUREBOTH | \ |
| 56 | PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \ |
| 57 | PPS_CANWAIT | PPS_TSFMT_TSPEC; |
| 58 | |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 59 | pps = pps_register_source(&info, PPS_CAPTUREBOTH | \ |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 60 | PPS_OFFSETASSERT | PPS_OFFSETCLEAR); |
YueHaibing | 3b1ad360 | 2018-11-26 18:24:22 +0800 | [diff] [blame] | 61 | if (IS_ERR(pps)) { |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 62 | pr_err("cannot register PPS source \"%s\"\n", info.path); |
YueHaibing | 3b1ad360 | 2018-11-26 18:24:22 +0800 | [diff] [blame] | 63 | return PTR_ERR(pps); |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 64 | } |
George Spelvin | 03a7ffe4 | 2013-02-10 04:41:56 -0500 | [diff] [blame] | 65 | pps->lookup_cookie = tty; |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 66 | |
George Spelvin | 03a7ffe4 | 2013-02-10 04:41:56 -0500 | [diff] [blame] | 67 | /* Now open the base class N_TTY ldisc */ |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 68 | ret = alias_n_tty_open(tty); |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 69 | if (ret < 0) { |
| 70 | pr_err("cannot open tty ldisc \"%s\"\n", info.path); |
| 71 | goto err_unregister; |
| 72 | } |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 73 | |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 74 | dev_info(pps->dev, "source \"%s\" added\n", info.path); |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 75 | |
| 76 | return 0; |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 77 | |
| 78 | err_unregister: |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 79 | pps_unregister_source(pps); |
| 80 | return ret; |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void (*alias_n_tty_close)(struct tty_struct *tty); |
| 84 | |
| 85 | static void pps_tty_close(struct tty_struct *tty) |
| 86 | { |
George Spelvin | 03a7ffe4 | 2013-02-10 04:41:56 -0500 | [diff] [blame] | 87 | struct pps_device *pps = pps_lookup_dev(tty); |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 88 | |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 89 | alias_n_tty_close(tty); |
| 90 | |
George Spelvin | ce3da1a | 2013-02-10 04:43:41 -0500 | [diff] [blame] | 91 | if (WARN_ON(!pps)) |
| 92 | return; |
| 93 | |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 94 | dev_info(pps->dev, "removed\n"); |
| 95 | pps_unregister_source(pps); |
Rodolfo Giometti | a0880df | 2010-03-10 15:23:47 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static struct tty_ldisc_ops pps_ldisc_ops; |
| 99 | |
| 100 | /* |
| 101 | * Module stuff |
| 102 | */ |
| 103 | |
| 104 | static int __init pps_tty_init(void) |
| 105 | { |
| 106 | int err; |
| 107 | |
| 108 | /* Inherit the N_TTY's ops */ |
| 109 | n_tty_inherit_ops(&pps_ldisc_ops); |
| 110 | |
| 111 | /* Save N_TTY's open()/close() methods */ |
| 112 | alias_n_tty_open = pps_ldisc_ops.open; |
| 113 | alias_n_tty_close = pps_ldisc_ops.close; |
| 114 | |
| 115 | /* Init PPS_TTY data */ |
| 116 | pps_ldisc_ops.owner = THIS_MODULE; |
| 117 | pps_ldisc_ops.magic = PPS_TTY_MAGIC; |
| 118 | pps_ldisc_ops.name = "pps_tty"; |
| 119 | pps_ldisc_ops.dcd_change = pps_tty_dcd_change; |
| 120 | pps_ldisc_ops.open = pps_tty_open; |
| 121 | pps_ldisc_ops.close = pps_tty_close; |
| 122 | |
| 123 | err = tty_register_ldisc(N_PPS, &pps_ldisc_ops); |
| 124 | if (err) |
| 125 | pr_err("can't register PPS line discipline\n"); |
| 126 | else |
| 127 | pr_info("PPS line discipline registered\n"); |
| 128 | |
| 129 | return err; |
| 130 | } |
| 131 | |
| 132 | static void __exit pps_tty_cleanup(void) |
| 133 | { |
| 134 | int err; |
| 135 | |
| 136 | err = tty_unregister_ldisc(N_PPS); |
| 137 | if (err) |
| 138 | pr_err("can't unregister PPS line discipline\n"); |
| 139 | else |
| 140 | pr_info("PPS line discipline removed\n"); |
| 141 | } |
| 142 | |
| 143 | module_init(pps_tty_init); |
| 144 | module_exit(pps_tty_cleanup); |
| 145 | |
| 146 | MODULE_ALIAS_LDISC(N_PPS); |
| 147 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
| 148 | MODULE_DESCRIPTION("PPS TTY device driver"); |
| 149 | MODULE_LICENSE("GPL"); |