blob: d73c4c2ed4e13aedb80c757d397e4846ae34d16e [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rodolfo Giomettia0880df2010-03-10 15:23:47 -08002/*
3 * pps-ldisc.c -- PPS line discipline
4 *
Rodolfo Giomettia0880df2010-03-10 15:23:47 -08005 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
Rodolfo Giomettia0880df2010-03-10 15:23:47 -08006 */
7
Alexander Gordeev7f7cce72011-01-12 17:00:52 -08008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080010#include <linux/module.h>
11#include <linux/serial_core.h>
12#include <linux/tty.h>
13#include <linux/pps_kernel.h>
George Spelvince3da1a2013-02-10 04:43:41 -050014#include <linux/bug.h>
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080015
George Spelvin593fb1ae42013-02-12 02:00:43 -050016static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status)
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080017{
George Spelvin593fb1ae42013-02-12 02:00:43 -050018 struct pps_device *pps;
19 struct pps_event_time ts;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080020
George Spelvin593fb1ae42013-02-12 02:00:43 -050021 pps_get_ts(&ts);
22
23 pps = pps_lookup_dev(tty);
George Spelvince3da1a2013-02-10 04:43:41 -050024 /*
25 * This should never fail, but the ldisc locking is very
26 * convoluted, so don't crash just in case.
27 */
28 if (WARN_ON_ONCE(pps == NULL))
29 return;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080030
Alexander Gordeev5e196d32011-01-12 17:00:51 -080031 /* Now do the PPS event report */
George Spelvin593fb1ae42013-02-12 02:00:43 -050032 pps_event(pps, &ts, status ? PPS_CAPTUREASSERT :
Alexander Gordeev5e196d32011-01-12 17:00:51 -080033 PPS_CAPTURECLEAR, NULL);
34
35 dev_dbg(pps->dev, "PPS %s at %lu\n",
36 status ? "assert" : "clear", jiffies);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080037}
38
39static int (*alias_n_tty_open)(struct tty_struct *tty);
40
41static int pps_tty_open(struct tty_struct *tty)
42{
43 struct pps_source_info info;
44 struct tty_driver *drv = tty->driver;
45 int index = tty->index + drv->name_base;
Alexander Gordeev5e196d32011-01-12 17:00:51 -080046 struct pps_device *pps;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080047 int ret;
48
49 info.owner = THIS_MODULE;
50 info.dev = NULL;
51 snprintf(info.name, PPS_MAX_NAME_LEN, "%s%d", drv->driver_name, index);
52 snprintf(info.path, PPS_MAX_NAME_LEN, "/dev/%s%d", drv->name, index);
53 info.mode = PPS_CAPTUREBOTH | \
54 PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \
55 PPS_CANWAIT | PPS_TSFMT_TSPEC;
56
Alexander Gordeev5e196d32011-01-12 17:00:51 -080057 pps = pps_register_source(&info, PPS_CAPTUREBOTH | \
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080058 PPS_OFFSETASSERT | PPS_OFFSETCLEAR);
YueHaibing3b1ad3602018-11-26 18:24:22 +080059 if (IS_ERR(pps)) {
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080060 pr_err("cannot register PPS source \"%s\"\n", info.path);
YueHaibing3b1ad3602018-11-26 18:24:22 +080061 return PTR_ERR(pps);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080062 }
George Spelvin03a7ffe42013-02-10 04:41:56 -050063 pps->lookup_cookie = tty;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080064
George Spelvin03a7ffe42013-02-10 04:41:56 -050065 /* Now open the base class N_TTY ldisc */
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080066 ret = alias_n_tty_open(tty);
Alexander Gordeev5e196d32011-01-12 17:00:51 -080067 if (ret < 0) {
68 pr_err("cannot open tty ldisc \"%s\"\n", info.path);
69 goto err_unregister;
70 }
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080071
Alexander Gordeev5e196d32011-01-12 17:00:51 -080072 dev_info(pps->dev, "source \"%s\" added\n", info.path);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080073
74 return 0;
Alexander Gordeev5e196d32011-01-12 17:00:51 -080075
76err_unregister:
Alexander Gordeev5e196d32011-01-12 17:00:51 -080077 pps_unregister_source(pps);
78 return ret;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080079}
80
81static void (*alias_n_tty_close)(struct tty_struct *tty);
82
83static void pps_tty_close(struct tty_struct *tty)
84{
George Spelvin03a7ffe42013-02-10 04:41:56 -050085 struct pps_device *pps = pps_lookup_dev(tty);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080086
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080087 alias_n_tty_close(tty);
88
George Spelvince3da1a2013-02-10 04:43:41 -050089 if (WARN_ON(!pps))
90 return;
91
Alexander Gordeev5e196d32011-01-12 17:00:51 -080092 dev_info(pps->dev, "removed\n");
93 pps_unregister_source(pps);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080094}
95
96static struct tty_ldisc_ops pps_ldisc_ops;
97
98/*
99 * Module stuff
100 */
101
102static int __init pps_tty_init(void)
103{
104 int err;
105
106 /* Inherit the N_TTY's ops */
107 n_tty_inherit_ops(&pps_ldisc_ops);
108
109 /* Save N_TTY's open()/close() methods */
110 alias_n_tty_open = pps_ldisc_ops.open;
111 alias_n_tty_close = pps_ldisc_ops.close;
112
113 /* Init PPS_TTY data */
114 pps_ldisc_ops.owner = THIS_MODULE;
Jiri Slabyfbadf70a2021-05-05 11:19:07 +0200115 pps_ldisc_ops.num = N_PPS;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -0800116 pps_ldisc_ops.name = "pps_tty";
117 pps_ldisc_ops.dcd_change = pps_tty_dcd_change;
118 pps_ldisc_ops.open = pps_tty_open;
119 pps_ldisc_ops.close = pps_tty_close;
120
Jiri Slabyfbadf70a2021-05-05 11:19:07 +0200121 err = tty_register_ldisc(&pps_ldisc_ops);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -0800122 if (err)
123 pr_err("can't register PPS line discipline\n");
124 else
125 pr_info("PPS line discipline registered\n");
126
127 return err;
128}
129
130static void __exit pps_tty_cleanup(void)
131{
Jiri Slaby357a6a82021-05-05 11:19:11 +0200132 tty_unregister_ldisc(&pps_ldisc_ops);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -0800133}
134
135module_init(pps_tty_init);
136module_exit(pps_tty_cleanup);
137
138MODULE_ALIAS_LDISC(N_PPS);
139MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
140MODULE_DESCRIPTION("PPS TTY device driver");
141MODULE_LICENSE("GPL");