diff --git a/drivers/pps/clients/pps-ldisc.c b/drivers/pps/clients/pps-ldisc.c
index d73c4c2ed4e1..6221700ab3ad 100644
--- a/drivers/pps/clients/pps-ldisc.c
+++ b/drivers/pps/clients/pps-ldisc.c
@@ -8,12 +8,14 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
+#include <linux/moduleparam.h>
#include <linux/serial_core.h>
#include <linux/tty.h>
#include <linux/pps_kernel.h>
#include <linux/bug.h>
+#include <linux/string.h>
-static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status)
+static void pps_tty_activepin_change(struct tty_struct *tty, unsigned int status)
{
struct pps_device *pps;
struct pps_event_time ts;@@ -36,6 +38,29 @@ static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status)
status ? "assert" : "clear", jiffies);
}
+static void pps_tty_activepin_change_invertlogic(struct tty_struct *tty, unsigned int status)
+{
+ struct pps_device *pps;
+ struct pps_event_time ts;
+
+ pps_get_ts(&ts);
+
+ pps = pps_lookup_dev(tty);
+ /*
+ * This should never fail, but the ldisc locking is very
+ * convoluted, so don't crash just in case.
+ */
+ if (WARN_ON_ONCE(pps == NULL))
+ return;
+
+ /* Now do the PPS event report */
+ pps_event(pps, &ts, status ? PPS_CAPTURECLEAR :
+ PPS_CAPTUREASSERT, NULL);
+
+ dev_dbg(pps->dev, "PPS %s at %lu\n",
+ status ? "assert" : "clear", jiffies);
+}
+
static int (*alias_n_tty_open)(struct tty_struct *tty);
static int pps_tty_open(struct tty_struct *tty)@@ -99,10 +124,118 @@ static struct tty_ldisc_ops pps_ldisc_ops;
* Module stuff
*/
+/*
+ * activepin parameter (dcd or cts) to handle the case of
+ * some SBC computers with incomplete UART wiring (no dcd)
+ * and still allow using cts as the 1PPS signal input pin
+ */
+
+static char *activepin = "dcd";
+
+/*
+ * invertlogic parameter (0 or 1) allows handling the case
+ * of some uarts (e.g. TTL UARTs) were the rising / falling
+ * edges have inverted logic with respect to conventions of
+ * the RS232 standard
+ */
+
+static bool invertlogic;
+
+static void pps_ldisc_update_settings(void)
+{
+ pr_info("%s: activepin=%s invertlogic=%c\n",
+ __func__, activepin, invertlogic?'Y':'N');
+
+ if (strncasecmp(activepin, "dcd", 3) == 0) {
+ pps_ldisc_ops.cts_change = 0;
+ if (!invertlogic)
+ pps_ldisc_ops.dcd_change = pps_tty_activepin_change;
+ else
+ pps_ldisc_ops.dcd_change = pps_tty_activepin_change_invertlogic;
+ } else if (strncasecmp(activepin, "cts", 3) == 0) {
+ pps_ldisc_ops.dcd_change = 0;
+ if (!invertlogic)
+ pps_ldisc_ops.cts_change = pps_tty_activepin_change;
+ else
+ pps_ldisc_ops.cts_change = pps_tty_activepin_change_invertlogic;
+ } else {
+ pps_ldisc_ops.cts_change = 0;
+ pps_ldisc_ops.dcd_change = 0;
+ }
+}
+
+static int pps_set_activepin(const char *val, const struct kernel_param *kp)
+{
+ int res = 0;
+ bool update_setting = false;
+ int lenval = strlen(val);
+ bool currentvalue_is_dcd = (strncasecmp(activepin, "dcd", 3) == 0);
+
+ if ((lenval == 3 && strncasecmp(val, "dcd", lenval) == 0) ||
+ (lenval == 4 && strncasecmp(val, "dcd\n", lenval) == 0)) {
+ if (!currentvalue_is_dcd) {
+ update_setting = true;
+ res = param_set_charp("dcd", kp);
+ }
+ } else if ((lenval == 3 && strncasecmp(val, "cts", lenval) == 0) ||
+ (lenval == 4 && strncasecmp(val, "cts\n", lenval) == 0)) {
+ if (currentvalue_is_dcd) {
+ update_setting = true;
+ res = param_set_charp("cts", kp);
+ }
+ } else
+ res = -1;
+
+ if (res == 0 && update_setting)
+ pps_ldisc_update_settings();
+ else if (res != 0)
+ pr_info("unable to set activepin parameter to %s\n", val);
+
+ return res;
+}
+
+static const struct kernel_param_ops activepin_ops = {
+ .set = &pps_set_activepin,
+ .get = ¶m_get_charp,
+};
+
+module_param_cb(activepin, &activepin_ops, &activepin, 0664);
+
+MODULE_PARM_DESC(activepin, \
+"\n\t\tparameter can be either dcd (default) or cts and defines the\n\
+\t\tphysical UART pin which receives the analogic PPS signal.");
+
+static int pps_set_invertlogic(const char *val, const struct kernel_param *kp)
+{
+ bool lastvalue = *(bool *)(kp->arg);
+ int res = param_set_bool(val, kp);
+
+ if (res == 0 && lastvalue != invertlogic)
+ pps_ldisc_update_settings();
+
+ return res;
+}
+
+static const struct kernel_param_ops invertlogic_ops = {
+ .set = &pps_set_invertlogic,
+ .get = ¶m_get_bool,
+};
+
+module_param_cb(invertlogic, &invertlogic_ops, &invertlogic, 0664);
+
+MODULE_PARM_DESC(invertlogic, \
+"\n\t\tflag can be either N (default) or Y and allows handling some\n\
+\t\tUARTs (e.g. TTL UARTs) whose reference edge vs. status logic\n\
+\t\t(rising/falling vs. active/inactive) is the opposite of the\n\
+\t\tRS232 standard.");
+
static int __init pps_tty_init(void)
{
int err;
+ pr_info("%s: activepin=%s invertlogic=%c\n",
+ __func__, activepin, invertlogic?'Y':'N');
+
/* Inherit the N_TTY's ops */
n_tty_inherit_ops(&pps_ldisc_ops);
@@ -114,7 +247,7 @@ static int __init pps_tty_init(void)
pps_ldisc_ops.owner = THIS_MODULE;
pps_ldisc_ops.num = N_PPS;
pps_ldisc_ops.name = "pps_tty";
- pps_ldisc_ops.dcd_change = pps_tty_dcd_change;
+ pps_ldisc_update_settings();
pps_ldisc_ops.open = pps_tty_open;
pps_ldisc_ops.close = pps_tty_close;
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 1e738f265eea..abbab3fb1282 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -3096,8 +3096,21 @@ EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
*/
void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
{
+ struct tty_port *port = &uport->state->port;
+ struct tty_struct *tty = port->tty;
+ struct tty_ldisc *ld;
+
lockdep_assert_held_once(&uport->lock);
+ if (tty) {
+ ld = tty_ldisc_ref(tty);
+ if (ld) {
+ if (ld->ops->cts_change)
+ ld->ops->cts_change(tty, status);
+ tty_ldisc_deref(ld);
+ }
+ }
+
uport->icount.cts++;
if (uart_softcts_mode(uport)) {diff --git a/include/linux/tty_ldisc.h b/include/linux/tty_ldisc.h
index b85d84fb5f49..1ec282b7c584 100644
--- a/include/linux/tty_ldisc.h
+++ b/include/linux/tty_ldisc.h
@@ -112,6 +112,11 @@ struct tty_struct;
* Tells the discipline that the DCD pin has changed its status.
* Used exclusively by the N_PPS (Pulse-Per-Second) line discipline.
*
+ * void (*cts_change)(struct tty_struct *tty, unsigned int status)
+ *
+ * Tells the discipline that the CTS pin has changed its status.
+ * Used exclusively by the N_PPS (Pulse-Per-Second) line discipline.
+ *
* int (*receive_buf2)(struct tty_struct *, const unsigned char *cp,
* char *fp, int count);
*
@@ -208,6 +213,7 @@ struct tty_ldisc_ops {
const char *fp, int count);
void (*write_wakeup)(struct tty_struct *);
void (*dcd_change)(struct tty_struct *, unsigned int);
+ void (*cts_change)(struct tty_struct *, unsigned int);
int (*receive_buf2)(struct tty_struct *, const unsigned char *cp,
const char *fp, int count);
--
2.25.1