]>
Commit | Line | Data |
---|---|---|
74ba9207 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
a0880df0 RG |
2 | /* |
3 | * pps-ldisc.c -- PPS line discipline | |
4 | * | |
a0880df0 | 5 | * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it> |
a0880df0 RG |
6 | */ |
7 | ||
7f7cce74 AG |
8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
9 | ||
a0880df0 RG |
10 | #include <linux/module.h> |
11 | #include <linux/serial_core.h> | |
12 | #include <linux/tty.h> | |
13 | #include <linux/pps_kernel.h> | |
ce3da1a6 | 14 | #include <linux/bug.h> |
a0880df0 | 15 | |
593fb1ae | 16 | static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status) |
a0880df0 | 17 | { |
593fb1ae GS |
18 | struct pps_device *pps; |
19 | struct pps_event_time ts; | |
20 | ||
21 | pps_get_ts(&ts); | |
a0880df0 | 22 | |
593fb1ae | 23 | pps = pps_lookup_dev(tty); |
ce3da1a6 GS |
24 | /* |
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; | |
5e196d34 | 30 | |
a0880df0 | 31 | /* Now do the PPS event report */ |
593fb1ae | 32 | pps_event(pps, &ts, status ? PPS_CAPTUREASSERT : |
5e196d34 | 33 | PPS_CAPTURECLEAR, NULL); |
a0880df0 | 34 | |
5e196d34 AG |
35 | dev_dbg(pps->dev, "PPS %s at %lu\n", |
36 | status ? "assert" : "clear", jiffies); | |
a0880df0 RG |
37 | } |
38 | ||
39 | static int (*alias_n_tty_open)(struct tty_struct *tty); | |
40 | ||
41 | static 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; | |
5e196d34 | 46 | struct pps_device *pps; |
a0880df0 RG |
47 | 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 | ||
5e196d34 | 57 | pps = pps_register_source(&info, PPS_CAPTUREBOTH | \ |
a0880df0 | 58 | PPS_OFFSETASSERT | PPS_OFFSETCLEAR); |
3b1ad360 | 59 | if (IS_ERR(pps)) { |
a0880df0 | 60 | pr_err("cannot register PPS source \"%s\"\n", info.path); |
3b1ad360 | 61 | return PTR_ERR(pps); |
a0880df0 | 62 | } |
03a7ffe4 | 63 | pps->lookup_cookie = tty; |
a0880df0 | 64 | |
03a7ffe4 | 65 | /* Now open the base class N_TTY ldisc */ |
a0880df0 | 66 | ret = alias_n_tty_open(tty); |
5e196d34 AG |
67 | if (ret < 0) { |
68 | pr_err("cannot open tty ldisc \"%s\"\n", info.path); | |
69 | goto err_unregister; | |
70 | } | |
a0880df0 | 71 | |
5e196d34 | 72 | dev_info(pps->dev, "source \"%s\" added\n", info.path); |
a0880df0 RG |
73 | |
74 | return 0; | |
5e196d34 AG |
75 | |
76 | err_unregister: | |
5e196d34 AG |
77 | pps_unregister_source(pps); |
78 | return ret; | |
a0880df0 RG |
79 | } |
80 | ||
81 | static void (*alias_n_tty_close)(struct tty_struct *tty); | |
82 | ||
83 | static void pps_tty_close(struct tty_struct *tty) | |
84 | { | |
03a7ffe4 | 85 | struct pps_device *pps = pps_lookup_dev(tty); |
a0880df0 | 86 | |
a0880df0 RG |
87 | alias_n_tty_close(tty); |
88 | ||
ce3da1a6 GS |
89 | if (WARN_ON(!pps)) |
90 | return; | |
91 | ||
5e196d34 AG |
92 | dev_info(pps->dev, "removed\n"); |
93 | pps_unregister_source(pps); | |
a0880df0 RG |
94 | } |
95 | ||
96 | static struct tty_ldisc_ops pps_ldisc_ops; | |
97 | ||
98 | /* | |
99 | * Module stuff | |
100 | */ | |
101 | ||
102 | static 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; | |
fbadf70a | 115 | pps_ldisc_ops.num = N_PPS; |
a0880df0 RG |
116 | 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 | ||
fbadf70a | 121 | err = tty_register_ldisc(&pps_ldisc_ops); |
a0880df0 RG |
122 | 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 | ||
130 | static void __exit pps_tty_cleanup(void) | |
131 | { | |
132 | int err; | |
133 | ||
134 | err = tty_unregister_ldisc(N_PPS); | |
135 | if (err) | |
136 | pr_err("can't unregister PPS line discipline\n"); | |
137 | else | |
138 | pr_info("PPS line discipline removed\n"); | |
139 | } | |
140 | ||
141 | module_init(pps_tty_init); | |
142 | module_exit(pps_tty_cleanup); | |
143 | ||
144 | MODULE_ALIAS_LDISC(N_PPS); | |
145 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); | |
146 | MODULE_DESCRIPTION("PPS TTY device driver"); | |
147 | MODULE_LICENSE("GPL"); |