blob: a94f73e1480d0e5b3c45eee167fdc95a3f1303c2 [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>
George Spelvince3da1a2013-02-10 04:43:41 -050028#include <linux/bug.h>
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080029
30#define PPS_TTY_MAGIC 0x0001
31
32static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080033 struct pps_event_time *ts)
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080034{
George Spelvin03a7ffe42013-02-10 04:41:56 -050035 struct pps_device *pps = pps_lookup_dev(tty);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080036
George Spelvince3da1a2013-02-10 04:43:41 -050037 /*
38 * This should never fail, but the ldisc locking is very
39 * convoluted, so don't crash just in case.
40 */
41 if (WARN_ON_ONCE(pps == NULL))
42 return;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080043
Alexander Gordeev5e196d32011-01-12 17:00:51 -080044 /* Now do the PPS event report */
45 pps_event(pps, ts, status ? PPS_CAPTUREASSERT :
46 PPS_CAPTURECLEAR, NULL);
47
48 dev_dbg(pps->dev, "PPS %s at %lu\n",
49 status ? "assert" : "clear", jiffies);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080050}
51
52static int (*alias_n_tty_open)(struct tty_struct *tty);
53
54static 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;
Alexander Gordeev5e196d32011-01-12 17:00:51 -080059 struct pps_device *pps;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080060 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
Alexander Gordeev5e196d32011-01-12 17:00:51 -080070 pps = pps_register_source(&info, PPS_CAPTUREBOTH | \
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080071 PPS_OFFSETASSERT | PPS_OFFSETCLEAR);
Alexander Gordeev5e196d32011-01-12 17:00:51 -080072 if (pps == NULL) {
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080073 pr_err("cannot register PPS source \"%s\"\n", info.path);
Alexander Gordeev5e196d32011-01-12 17:00:51 -080074 return -ENOMEM;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080075 }
George Spelvin03a7ffe42013-02-10 04:41:56 -050076 pps->lookup_cookie = tty;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080077
George Spelvin03a7ffe42013-02-10 04:41:56 -050078 /* Now open the base class N_TTY ldisc */
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080079 ret = alias_n_tty_open(tty);
Alexander Gordeev5e196d32011-01-12 17:00:51 -080080 if (ret < 0) {
81 pr_err("cannot open tty ldisc \"%s\"\n", info.path);
82 goto err_unregister;
83 }
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080084
Alexander Gordeev5e196d32011-01-12 17:00:51 -080085 dev_info(pps->dev, "source \"%s\" added\n", info.path);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080086
87 return 0;
Alexander Gordeev5e196d32011-01-12 17:00:51 -080088
89err_unregister:
Alexander Gordeev5e196d32011-01-12 17:00:51 -080090 pps_unregister_source(pps);
91 return ret;
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080092}
93
94static void (*alias_n_tty_close)(struct tty_struct *tty);
95
96static void pps_tty_close(struct tty_struct *tty)
97{
George Spelvin03a7ffe42013-02-10 04:41:56 -050098 struct pps_device *pps = pps_lookup_dev(tty);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -080099
Rodolfo Giomettia0880df2010-03-10 15:23:47 -0800100 alias_n_tty_close(tty);
101
George Spelvince3da1a2013-02-10 04:43:41 -0500102 if (WARN_ON(!pps))
103 return;
104
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800105 dev_info(pps->dev, "removed\n");
106 pps_unregister_source(pps);
Rodolfo Giomettia0880df2010-03-10 15:23:47 -0800107}
108
109static struct tty_ldisc_ops pps_ldisc_ops;
110
111/*
112 * Module stuff
113 */
114
115static int __init pps_tty_init(void)
116{
117 int err;
118
119 /* Inherit the N_TTY's ops */
120 n_tty_inherit_ops(&pps_ldisc_ops);
121
122 /* Save N_TTY's open()/close() methods */
123 alias_n_tty_open = pps_ldisc_ops.open;
124 alias_n_tty_close = pps_ldisc_ops.close;
125
126 /* Init PPS_TTY data */
127 pps_ldisc_ops.owner = THIS_MODULE;
128 pps_ldisc_ops.magic = PPS_TTY_MAGIC;
129 pps_ldisc_ops.name = "pps_tty";
130 pps_ldisc_ops.dcd_change = pps_tty_dcd_change;
131 pps_ldisc_ops.open = pps_tty_open;
132 pps_ldisc_ops.close = pps_tty_close;
133
134 err = tty_register_ldisc(N_PPS, &pps_ldisc_ops);
135 if (err)
136 pr_err("can't register PPS line discipline\n");
137 else
138 pr_info("PPS line discipline registered\n");
139
140 return err;
141}
142
143static void __exit pps_tty_cleanup(void)
144{
145 int err;
146
147 err = tty_unregister_ldisc(N_PPS);
148 if (err)
149 pr_err("can't unregister PPS line discipline\n");
150 else
151 pr_info("PPS line discipline removed\n");
152}
153
154module_init(pps_tty_init);
155module_exit(pps_tty_cleanup);
156
157MODULE_ALIAS_LDISC(N_PPS);
158MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
159MODULE_DESCRIPTION("PPS TTY device driver");
160MODULE_LICENSE("GPL");