]>
Commit | Line | Data |
---|---|---|
a0880df0 RG |
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, | |
6f4229b5 | 30 | struct pps_event_time *ts) |
a0880df0 | 31 | { |
5e196d34 | 32 | struct pps_device *pps = (struct pps_device *)tty->disc_data; |
6f4229b5 | 33 | struct pps_event_time __ts; |
a0880df0 RG |
34 | |
35 | /* First of all we get the time stamp... */ | |
6f4229b5 | 36 | pps_get_ts(&__ts); |
a0880df0 RG |
37 | |
38 | /* Does caller give us a timestamp? */ | |
6f4229b5 AG |
39 | if (!ts) /* No. Do it ourself! */ |
40 | ts = &__ts; | |
a0880df0 | 41 | |
5e196d34 AG |
42 | BUG_ON(pps == NULL); |
43 | ||
a0880df0 | 44 | /* Now do the PPS event report */ |
5e196d34 AG |
45 | pps_event(pps, ts, status ? PPS_CAPTUREASSERT : |
46 | PPS_CAPTURECLEAR, NULL); | |
a0880df0 | 47 | |
5e196d34 AG |
48 | dev_dbg(pps->dev, "PPS %s at %lu\n", |
49 | status ? "assert" : "clear", jiffies); | |
a0880df0 RG |
50 | } |
51 | ||
52 | static int (*alias_n_tty_open)(struct tty_struct *tty); | |
53 | ||
54 | static int pps_tty_open(struct tty_struct *tty) | |
55 | { | |
56 | struct pps_source_info info; | |
57 | struct tty_driver *drv = tty->driver; | |
58 | int index = tty->index + drv->name_base; | |
5e196d34 | 59 | struct pps_device *pps; |
a0880df0 RG |
60 | int ret; |
61 | ||
62 | info.owner = THIS_MODULE; | |
63 | info.dev = NULL; | |
64 | snprintf(info.name, PPS_MAX_NAME_LEN, "%s%d", drv->driver_name, index); | |
65 | snprintf(info.path, PPS_MAX_NAME_LEN, "/dev/%s%d", drv->name, index); | |
66 | info.mode = PPS_CAPTUREBOTH | \ | |
67 | PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \ | |
68 | PPS_CANWAIT | PPS_TSFMT_TSPEC; | |
69 | ||
5e196d34 | 70 | pps = pps_register_source(&info, PPS_CAPTUREBOTH | \ |
a0880df0 | 71 | PPS_OFFSETASSERT | PPS_OFFSETCLEAR); |
5e196d34 | 72 | if (pps == NULL) { |
a0880df0 | 73 | pr_err("cannot register PPS source \"%s\"\n", info.path); |
5e196d34 | 74 | return -ENOMEM; |
a0880df0 | 75 | } |
5e196d34 | 76 | tty->disc_data = pps; |
a0880df0 RG |
77 | |
78 | /* Should open N_TTY ldisc too */ | |
79 | ret = alias_n_tty_open(tty); | |
5e196d34 AG |
80 | if (ret < 0) { |
81 | pr_err("cannot open tty ldisc \"%s\"\n", info.path); | |
82 | goto err_unregister; | |
83 | } | |
a0880df0 | 84 | |
5e196d34 | 85 | dev_info(pps->dev, "source \"%s\" added\n", info.path); |
a0880df0 RG |
86 | |
87 | return 0; | |
5e196d34 AG |
88 | |
89 | err_unregister: | |
90 | tty->disc_data = NULL; | |
91 | pps_unregister_source(pps); | |
92 | return ret; | |
a0880df0 RG |
93 | } |
94 | ||
95 | static void (*alias_n_tty_close)(struct tty_struct *tty); | |
96 | ||
97 | static void pps_tty_close(struct tty_struct *tty) | |
98 | { | |
5e196d34 | 99 | struct pps_device *pps = (struct pps_device *)tty->disc_data; |
a0880df0 | 100 | |
a0880df0 RG |
101 | alias_n_tty_close(tty); |
102 | ||
5e196d34 AG |
103 | tty->disc_data = NULL; |
104 | dev_info(pps->dev, "removed\n"); | |
105 | pps_unregister_source(pps); | |
a0880df0 RG |
106 | } |
107 | ||
108 | static struct tty_ldisc_ops pps_ldisc_ops; | |
109 | ||
110 | /* | |
111 | * Module stuff | |
112 | */ | |
113 | ||
114 | static int __init pps_tty_init(void) | |
115 | { | |
116 | int err; | |
117 | ||
118 | /* Inherit the N_TTY's ops */ | |
119 | n_tty_inherit_ops(&pps_ldisc_ops); | |
120 | ||
121 | /* Save N_TTY's open()/close() methods */ | |
122 | alias_n_tty_open = pps_ldisc_ops.open; | |
123 | alias_n_tty_close = pps_ldisc_ops.close; | |
124 | ||
125 | /* Init PPS_TTY data */ | |
126 | pps_ldisc_ops.owner = THIS_MODULE; | |
127 | pps_ldisc_ops.magic = PPS_TTY_MAGIC; | |
128 | pps_ldisc_ops.name = "pps_tty"; | |
129 | pps_ldisc_ops.dcd_change = pps_tty_dcd_change; | |
130 | pps_ldisc_ops.open = pps_tty_open; | |
131 | pps_ldisc_ops.close = pps_tty_close; | |
132 | ||
133 | err = tty_register_ldisc(N_PPS, &pps_ldisc_ops); | |
134 | if (err) | |
135 | pr_err("can't register PPS line discipline\n"); | |
136 | else | |
137 | pr_info("PPS line discipline registered\n"); | |
138 | ||
139 | return err; | |
140 | } | |
141 | ||
142 | static void __exit pps_tty_cleanup(void) | |
143 | { | |
144 | int err; | |
145 | ||
146 | err = tty_unregister_ldisc(N_PPS); | |
147 | if (err) | |
148 | pr_err("can't unregister PPS line discipline\n"); | |
149 | else | |
150 | pr_info("PPS line discipline removed\n"); | |
151 | } | |
152 | ||
153 | module_init(pps_tty_init); | |
154 | module_exit(pps_tty_cleanup); | |
155 | ||
156 | MODULE_ALIAS_LDISC(N_PPS); | |
157 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); | |
158 | MODULE_DESCRIPTION("PPS TTY device driver"); | |
159 | MODULE_LICENSE("GPL"); |