blob: 1b3c6eda12ba85cb0b4b939c978d3b361dce612e [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
Alexander Gordeev7f7cce72011-01-12 17:00:52 -080022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080024#include <linux/module.h>
25#include <linux/serial_core.h>
26#include <linux/tty.h>
27#include <linux/pps_kernel.h>
28
29#define PPS_TTY_MAGIC 0x0001
30
31static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080032 struct pps_event_time *ts)
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080033{
Alexander Gordeev5e196d32011-01-12 17:00:51 -080034 struct pps_device *pps = (struct pps_device *)tty->disc_data;
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080035 struct pps_event_time __ts;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080036
37 /* First of all we get the time stamp... */
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080038 pps_get_ts(&__ts);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080039
40 /* Does caller give us a timestamp? */
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080041 if (!ts) /* No. Do it ourself! */
42 ts = &__ts;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080043
Alexander Gordeev5e196d32011-01-12 17:00:51 -080044 BUG_ON(pps == NULL);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080045
Alexander Gordeev5e196d32011-01-12 17:00:51 -080046 /* Now do the PPS event report */
47 pps_event(pps, ts, status ? PPS_CAPTUREASSERT :
48 PPS_CAPTURECLEAR, NULL);
49
50 dev_dbg(pps->dev, "PPS %s at %lu\n",
51 status ? "assert" : "clear", jiffies);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080052}
53
54static int (*alias_n_tty_open)(struct tty_struct *tty);
55
56static int pps_tty_open(struct tty_struct *tty)
57{
58 struct pps_source_info info;
59 struct tty_driver *drv = tty->driver;
60 int index = tty->index + drv->name_base;
Alexander Gordeev5e196d32011-01-12 17:00:51 -080061 struct pps_device *pps;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080062 int ret;
63
64 info.owner = THIS_MODULE;
65 info.dev = NULL;
66 snprintf(info.name, PPS_MAX_NAME_LEN, "%s%d", drv->driver_name, index);
67 snprintf(info.path, PPS_MAX_NAME_LEN, "/dev/%s%d", drv->name, index);
68 info.mode = PPS_CAPTUREBOTH | \
69 PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \
70 PPS_CANWAIT | PPS_TSFMT_TSPEC;
71
Alexander Gordeev5e196d32011-01-12 17:00:51 -080072 pps = pps_register_source(&info, PPS_CAPTUREBOTH | \
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080073 PPS_OFFSETASSERT | PPS_OFFSETCLEAR);
Alexander Gordeev5e196d32011-01-12 17:00:51 -080074 if (pps == NULL) {
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080075 pr_err("cannot register PPS source \"%s\"\n", info.path);
Alexander Gordeev5e196d32011-01-12 17:00:51 -080076 return -ENOMEM;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080077 }
Alexander Gordeev5e196d32011-01-12 17:00:51 -080078 tty->disc_data = pps;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080079
80 /* Should open N_TTY ldisc too */
81 ret = alias_n_tty_open(tty);
Alexander Gordeev5e196d32011-01-12 17:00:51 -080082 if (ret < 0) {
83 pr_err("cannot open tty ldisc \"%s\"\n", info.path);
84 goto err_unregister;
85 }
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080086
Alexander Gordeev5e196d32011-01-12 17:00:51 -080087 dev_info(pps->dev, "source \"%s\" added\n", info.path);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080088
89 return 0;
Alexander Gordeev5e196d32011-01-12 17:00:51 -080090
91err_unregister:
92 tty->disc_data = NULL;
93 pps_unregister_source(pps);
94 return ret;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080095}
96
97static void (*alias_n_tty_close)(struct tty_struct *tty);
98
99static void pps_tty_close(struct tty_struct *tty)
100{
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800101 struct pps_device *pps = (struct pps_device *)tty->disc_data;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -0800102
Rodolfo Giomettia0880df2010-03-10 15:23:47 -0800103 alias_n_tty_close(tty);
104
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800105 tty->disc_data = NULL;
106 dev_info(pps->dev, "removed\n");
107 pps_unregister_source(pps);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -0800108}
109
110static struct tty_ldisc_ops pps_ldisc_ops;
111
112/*
113 * Module stuff
114 */
115
116static int __init pps_tty_init(void)
117{
118 int err;
119
120 /* Inherit the N_TTY's ops */
121 n_tty_inherit_ops(&pps_ldisc_ops);
122
123 /* Save N_TTY's open()/close() methods */
124 alias_n_tty_open = pps_ldisc_ops.open;
125 alias_n_tty_close = pps_ldisc_ops.close;
126
127 /* Init PPS_TTY data */
128 pps_ldisc_ops.owner = THIS_MODULE;
129 pps_ldisc_ops.magic = PPS_TTY_MAGIC;
130 pps_ldisc_ops.name = "pps_tty";
131 pps_ldisc_ops.dcd_change = pps_tty_dcd_change;
132 pps_ldisc_ops.open = pps_tty_open;
133 pps_ldisc_ops.close = pps_tty_close;
134
135 err = tty_register_ldisc(N_PPS, &pps_ldisc_ops);
136 if (err)
137 pr_err("can't register PPS line discipline\n");
138 else
139 pr_info("PPS line discipline registered\n");
140
141 return err;
142}
143
144static void __exit pps_tty_cleanup(void)
145{
146 int err;
147
148 err = tty_unregister_ldisc(N_PPS);
149 if (err)
150 pr_err("can't unregister PPS line discipline\n");
151 else
152 pr_info("PPS line discipline removed\n");
153}
154
155module_init(pps_tty_init);
156module_exit(pps_tty_cleanup);
157
158MODULE_ALIAS_LDISC(N_PPS);
159MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
160MODULE_DESCRIPTION("PPS TTY device driver");
161MODULE_LICENSE("GPL");