blob: 20fc9f7645c8741015ec3d960c6ba9a072e01613 [file] [log] [blame]
Rodolfo Giomettia0880df2010-03-10 15:23:47 -08001/*
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
29static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080030 struct pps_event_time *ts)
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080031{
32 int id = (long)tty->disc_data;
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080033 struct pps_event_time __ts;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080034
35 /* First of all we get the time stamp... */
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080036 pps_get_ts(&__ts);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080037
38 /* Does caller give us a timestamp? */
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080039 if (!ts) /* No. Do it ourself! */
40 ts = &__ts;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080041
42 /* Now do the PPS event report */
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080043 pps_event(id, ts, status ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR,
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080044 NULL);
45
46 pr_debug("PPS %s at %lu on source #%d\n",
47 status ? "assert" : "clear", jiffies, id);
48}
49
50static int (*alias_n_tty_open)(struct tty_struct *tty);
51
52static 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
85static void (*alias_n_tty_close)(struct tty_struct *tty);
86
87static 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
97static struct tty_ldisc_ops pps_ldisc_ops;
98
99/*
100 * Module stuff
101 */
102
103static 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
131static 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
142module_init(pps_tty_init);
143module_exit(pps_tty_cleanup);
144
145MODULE_ALIAS_LDISC(N_PPS);
146MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
147MODULE_DESCRIPTION("PPS TTY device driver");
148MODULE_LICENSE("GPL");