Commit 7c862ee9 authored by Marc Kleine-Budde's avatar Marc Kleine-Budde

Merge patch series "can: slcan: extend supported features (step 2)"

Dario Binacchi <dario.binacchi@amarulasolutions.com> says:

====================

With this series I try to finish the task, started with the series [1],
of completely removing the dependency of the slcan driver from the
userspace slcand/slcan_attach applications.

The series also contains patches that remove the legacy stuff (slcan_devs,
SLCAN_MAGIC, ...) and do some module cleanup.

The series has been created on top of the patches:

can: slcan: convert comments to network style comments
can: slcan: slcan_init() convert printk(LEVEL ...) to pr_level()
can: slcan: fix whitespace issues
can: slcan: convert comparison to NULL into !val
can: slcan: clean up if/else
can: slcan: use scnprintf() as a hardening measure
can: slcan: do not report txerr and rxerr during bus-off
can: slcan: do not sleep with a spin lock held

applied to linux-next.

[1] https://lore.kernel.org/all/20220628163137.413025-1-dario.binacchi@amarulasolutions.com

Changes since v3: https://lore.kernel.org/all/20220726210217.3368497-1-dario.binacchi@amarulasolutions.com
- Add Max Staudt's `Reviewed-by' tag.
- Drop the patch "ethtool: add support to get/set CAN bit time register".
- Drop the patch "can: slcan: add support to set bit time register (btr)".
- Remove the RFC prefix from the series.

Changes since v2: https://lore.kernel.org/all/20220725065419.3005015-1-dario.binacchi@amarulasolutions.com
- Update the commit message.
- Use 1 space in front of the =.
- Put the series as RFC again.
- Pick up the patch "can: slcan: use KBUILD_MODNAME and define pr_fmt to replace hardcoded names".
- Add the patch "ethtool: add support to get/set CAN bit time register"
  to the series.
- Add the patch "can: slcan: add support to set bit time register (btr)"
  to the series.
- Replace the link https://marc.info/?l=linux-can&m=165806705927851&w=2 with
  https://lore.kernel.org/all/507b5973-d673-4755-3b64-b41cb9a13b6f@hartkopp.net.
- Add the `Suggested-by' tag.

Changes since RFC: https://lore.kernel.org/all/20220716170007.2020037-1-dario.binacchi@amarulasolutions.com
- Re-add headers that export at least one symbol used by the module.
- Update the commit description.
- Drop the old "slcan" name to use the standard canX interface naming.
- Remove comment on listen-only command.
- Update the commit subject and description.
- Add the patch "MAINTAINERS: Add myself as maintainer of the SLCAN driver"
  to the series.

====================

mkl: rebased to can-next/master

Link: https://lore.kernel.org/all/20220728070254.267974-1-dario.binacchi@amarulasolutions.comSigned-off-by: default avatarMarc Kleine-Budde <mkl@pengutronix.de>
parents 9d8dd372 4aeccfd8
...@@ -18470,6 +18470,12 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab.git ...@@ -18470,6 +18470,12 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab.git
F: include/linux/sl?b*.h F: include/linux/sl?b*.h
F: mm/sl?b* F: mm/sl?b*
SLCAN CAN NETWORK DRIVER
M: Dario Binacchi <dario.binacchi@amarulasolutions.com>
L: linux-can@vger.kernel.org
S: Maintained
F: drivers/net/can/slcan/
SLEEPABLE READ-COPY UPDATE (SRCU) SLEEPABLE READ-COPY UPDATE (SRCU)
M: Lai Jiangshan <jiangshanlai@gmail.com> M: Lai Jiangshan <jiangshanlai@gmail.com>
M: "Paul E. McKenney" <paulmck@kernel.org> M: "Paul E. McKenney" <paulmck@kernel.org>
......
/* /*
* slcan.c - serial line CAN interface driver (using tty line discipline) * slcan.c - serial line CAN interface driver (using tty line discipline)
* *
* This file is derived from linux/drivers/net/slip/slip.c * This file is derived from linux/drivers/net/slip/slip.c and got
* inspiration from linux/drivers/net/can/can327.c for the rework made
* on the line discipline code.
* *
* slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk> * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk>
* Fred N. van Kempen <waltje@uwalt.nl.mugnet.org> * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
* slcan.c Author : Oliver Hartkopp <socketcan@hartkopp.net> * slcan.c Author : Oliver Hartkopp <socketcan@hartkopp.net>
* can327.c Author : Max Staudt <max-linux@enpas.org>
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the * under the terms of the GNU General Public License as published by the
...@@ -35,8 +38,9 @@ ...@@ -35,8 +38,9 @@
* *
*/ */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h> #include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/bitops.h> #include <linux/bitops.h>
...@@ -46,10 +50,6 @@ ...@@ -46,10 +50,6 @@
#include <linux/netdevice.h> #include <linux/netdevice.h>
#include <linux/skbuff.h> #include <linux/skbuff.h>
#include <linux/rtnetlink.h> #include <linux/rtnetlink.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/workqueue.h> #include <linux/workqueue.h>
...@@ -63,30 +63,22 @@ MODULE_ALIAS_LDISC(N_SLCAN); ...@@ -63,30 +63,22 @@ MODULE_ALIAS_LDISC(N_SLCAN);
MODULE_DESCRIPTION("serial line CAN interface"); MODULE_DESCRIPTION("serial line CAN interface");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>"); MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
MODULE_AUTHOR("Dario Binacchi <dario.binacchi@amarulasolutions.com>");
#define SLCAN_MAGIC 0x53CA
static int maxdev = 10; /* MAX number of SLCAN channels;
* This can be overridden with
* insmod slcan.ko maxdev=nnn
*/
module_param(maxdev, int, 0);
MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
/* maximum rx buffer len: extended CAN frame with timestamp */ /* maximum rx buffer len: extended CAN frame with timestamp */
#define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r") + 1) #define SLCAN_MTU (sizeof("T1111222281122334455667788EA5F\r") + 1)
#define SLC_CMD_LEN 1 #define SLCAN_CMD_LEN 1
#define SLC_SFF_ID_LEN 3 #define SLCAN_SFF_ID_LEN 3
#define SLC_EFF_ID_LEN 8 #define SLCAN_EFF_ID_LEN 8
#define SLC_STATE_LEN 1 #define SLCAN_STATE_LEN 1
#define SLC_STATE_BE_RXCNT_LEN 3 #define SLCAN_STATE_BE_RXCNT_LEN 3
#define SLC_STATE_BE_TXCNT_LEN 3 #define SLCAN_STATE_BE_TXCNT_LEN 3
#define SLC_STATE_FRAME_LEN (1 + SLC_CMD_LEN + SLC_STATE_BE_RXCNT_LEN + \ #define SLCAN_STATE_FRAME_LEN (1 + SLCAN_CMD_LEN + \
SLC_STATE_BE_TXCNT_LEN) SLCAN_STATE_BE_RXCNT_LEN + \
SLCAN_STATE_BE_TXCNT_LEN)
struct slcan { struct slcan {
struct can_priv can; struct can_priv can;
int magic;
/* Various fields. */ /* Various fields. */
struct tty_struct *tty; /* ptr to TTY structure */ struct tty_struct *tty; /* ptr to TTY structure */
...@@ -95,24 +87,21 @@ struct slcan { ...@@ -95,24 +87,21 @@ struct slcan {
struct work_struct tx_work; /* Flushes transmit buffer */ struct work_struct tx_work; /* Flushes transmit buffer */
/* These are pointers to the malloc()ed frame buffers. */ /* These are pointers to the malloc()ed frame buffers. */
unsigned char rbuff[SLC_MTU]; /* receiver buffer */ unsigned char rbuff[SLCAN_MTU]; /* receiver buffer */
int rcount; /* received chars counter */ int rcount; /* received chars counter */
unsigned char xbuff[SLC_MTU]; /* transmitter buffer */ unsigned char xbuff[SLCAN_MTU]; /* transmitter buffer*/
unsigned char *xhead; /* pointer to next XMIT byte */ unsigned char *xhead; /* pointer to next XMIT byte */
int xleft; /* bytes left in XMIT queue */ int xleft; /* bytes left in XMIT queue */
unsigned long flags; /* Flag values/ mode etc */ unsigned long flags; /* Flag values/ mode etc */
#define SLF_INUSE 0 /* Channel in use */ #define SLF_ERROR 0 /* Parity, etc. error */
#define SLF_ERROR 1 /* Parity, etc. error */ #define SLF_XCMD 1 /* Command transmission */
#define SLF_XCMD 2 /* Command transmission */
unsigned long cmd_flags; /* Command flags */ unsigned long cmd_flags; /* Command flags */
#define CF_ERR_RST 0 /* Reset errors on open */ #define CF_ERR_RST 0 /* Reset errors on open */
wait_queue_head_t xcmd_wait; /* Wait queue for commands */ wait_queue_head_t xcmd_wait; /* Wait queue for commands */
/* transmission */ /* transmission */
}; };
static struct net_device **slcan_devs;
static const u32 slcan_bitrate_const[] = { static const u32 slcan_bitrate_const[] = {
10000, 20000, 50000, 100000, 125000, 10000, 20000, 50000, 100000, 125000,
250000, 500000, 800000, 1000000 250000, 500000, 800000, 1000000
...@@ -179,7 +168,7 @@ int slcan_enable_err_rst_on_open(struct net_device *ndev, bool on) ...@@ -179,7 +168,7 @@ int slcan_enable_err_rst_on_open(struct net_device *ndev, bool on)
*************************************************************************/ *************************************************************************/
/* Send one completely decapsulated can_frame to the network layer */ /* Send one completely decapsulated can_frame to the network layer */
static void slc_bump_frame(struct slcan *sl) static void slcan_bump_frame(struct slcan *sl)
{ {
struct sk_buff *skb; struct sk_buff *skb;
struct can_frame *cf; struct can_frame *cf;
...@@ -199,10 +188,10 @@ static void slc_bump_frame(struct slcan *sl) ...@@ -199,10 +188,10 @@ static void slc_bump_frame(struct slcan *sl)
fallthrough; fallthrough;
case 't': case 't':
/* store dlc ASCII value and terminate SFF CAN ID string */ /* store dlc ASCII value and terminate SFF CAN ID string */
cf->len = sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN]; cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN];
sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN] = 0; sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN] = 0;
/* point to payload data behind the dlc */ /* point to payload data behind the dlc */
cmd += SLC_CMD_LEN + SLC_SFF_ID_LEN + 1; cmd += SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN + 1;
break; break;
case 'R': case 'R':
cf->can_id = CAN_RTR_FLAG; cf->can_id = CAN_RTR_FLAG;
...@@ -210,16 +199,16 @@ static void slc_bump_frame(struct slcan *sl) ...@@ -210,16 +199,16 @@ static void slc_bump_frame(struct slcan *sl)
case 'T': case 'T':
cf->can_id |= CAN_EFF_FLAG; cf->can_id |= CAN_EFF_FLAG;
/* store dlc ASCII value and terminate EFF CAN ID string */ /* store dlc ASCII value and terminate EFF CAN ID string */
cf->len = sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN]; cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN];
sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN] = 0; sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN] = 0;
/* point to payload data behind the dlc */ /* point to payload data behind the dlc */
cmd += SLC_CMD_LEN + SLC_EFF_ID_LEN + 1; cmd += SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN + 1;
break; break;
default: default:
goto decode_failed; goto decode_failed;
} }
if (kstrtou32(sl->rbuff + SLC_CMD_LEN, 16, &tmpid)) if (kstrtou32(sl->rbuff + SLCAN_CMD_LEN, 16, &tmpid))
goto decode_failed; goto decode_failed;
cf->can_id |= tmpid; cf->can_id |= tmpid;
...@@ -266,7 +255,7 @@ static void slc_bump_frame(struct slcan *sl) ...@@ -266,7 +255,7 @@ static void slc_bump_frame(struct slcan *sl)
* sb256256 : state bus-off: rx counter 256, tx counter 256 * sb256256 : state bus-off: rx counter 256, tx counter 256
* sa057033 : state active, rx counter 57, tx counter 33 * sa057033 : state active, rx counter 57, tx counter 33
*/ */
static void slc_bump_state(struct slcan *sl) static void slcan_bump_state(struct slcan *sl)
{ {
struct net_device *dev = sl->dev; struct net_device *dev = sl->dev;
struct sk_buff *skb; struct sk_buff *skb;
...@@ -292,16 +281,16 @@ static void slc_bump_state(struct slcan *sl) ...@@ -292,16 +281,16 @@ static void slc_bump_state(struct slcan *sl)
return; return;
} }
if (state == sl->can.state || sl->rcount < SLC_STATE_FRAME_LEN) if (state == sl->can.state || sl->rcount < SLCAN_STATE_FRAME_LEN)
return; return;
cmd += SLC_STATE_BE_RXCNT_LEN + SLC_CMD_LEN + 1; cmd += SLCAN_STATE_BE_RXCNT_LEN + SLCAN_CMD_LEN + 1;
cmd[SLC_STATE_BE_TXCNT_LEN] = 0; cmd[SLCAN_STATE_BE_TXCNT_LEN] = 0;
if (kstrtou32(cmd, 10, &txerr)) if (kstrtou32(cmd, 10, &txerr))
return; return;
*cmd = 0; *cmd = 0;
cmd -= SLC_STATE_BE_RXCNT_LEN; cmd -= SLCAN_STATE_BE_RXCNT_LEN;
if (kstrtou32(cmd, 10, &rxerr)) if (kstrtou32(cmd, 10, &rxerr))
return; return;
...@@ -330,7 +319,7 @@ static void slc_bump_state(struct slcan *sl) ...@@ -330,7 +319,7 @@ static void slc_bump_state(struct slcan *sl)
* e1a : len 1, errors: ACK error * e1a : len 1, errors: ACK error
* e3bcO: len 3, errors: Bit0 error, CRC error, Tx overrun error * e3bcO: len 3, errors: Bit0 error, CRC error, Tx overrun error
*/ */
static void slc_bump_err(struct slcan *sl) static void slcan_bump_err(struct slcan *sl)
{ {
struct net_device *dev = sl->dev; struct net_device *dev = sl->dev;
struct sk_buff *skb; struct sk_buff *skb;
...@@ -346,7 +335,7 @@ static void slc_bump_err(struct slcan *sl) ...@@ -346,7 +335,7 @@ static void slc_bump_err(struct slcan *sl)
else else
return; return;
if ((len + SLC_CMD_LEN + 1) > sl->rcount) if ((len + SLCAN_CMD_LEN + 1) > sl->rcount)
return; return;
skb = alloc_can_err_skb(dev, &cf); skb = alloc_can_err_skb(dev, &cf);
...@@ -354,7 +343,7 @@ static void slc_bump_err(struct slcan *sl) ...@@ -354,7 +343,7 @@ static void slc_bump_err(struct slcan *sl)
if (skb) if (skb)
cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
cmd += SLC_CMD_LEN + 1; cmd += SLCAN_CMD_LEN + 1;
for (i = 0; i < len; i++, cmd++) { for (i = 0; i < len; i++, cmd++) {
switch (*cmd) { switch (*cmd) {
case 'a': case 'a':
...@@ -443,7 +432,7 @@ static void slc_bump_err(struct slcan *sl) ...@@ -443,7 +432,7 @@ static void slc_bump_err(struct slcan *sl)
netif_rx(skb); netif_rx(skb);
} }
static void slc_bump(struct slcan *sl) static void slcan_bump(struct slcan *sl)
{ {
switch (sl->rbuff[0]) { switch (sl->rbuff[0]) {
case 'r': case 'r':
...@@ -453,11 +442,11 @@ static void slc_bump(struct slcan *sl) ...@@ -453,11 +442,11 @@ static void slc_bump(struct slcan *sl)
case 'R': case 'R':
fallthrough; fallthrough;
case 'T': case 'T':
return slc_bump_frame(sl); return slcan_bump_frame(sl);
case 'e': case 'e':
return slc_bump_err(sl); return slcan_bump_err(sl);
case 's': case 's':
return slc_bump_state(sl); return slcan_bump_state(sl);
default: default:
return; return;
} }
...@@ -469,12 +458,12 @@ static void slcan_unesc(struct slcan *sl, unsigned char s) ...@@ -469,12 +458,12 @@ static void slcan_unesc(struct slcan *sl, unsigned char s)
if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */ if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
sl->rcount > 4) sl->rcount > 4)
slc_bump(sl); slcan_bump(sl);
sl->rcount = 0; sl->rcount = 0;
} else { } else {
if (!test_bit(SLF_ERROR, &sl->flags)) { if (!test_bit(SLF_ERROR, &sl->flags)) {
if (sl->rcount < SLC_MTU) { if (sl->rcount < SLCAN_MTU) {
sl->rbuff[sl->rcount++] = s; sl->rbuff[sl->rcount++] = s;
return; return;
} }
...@@ -490,7 +479,7 @@ static void slcan_unesc(struct slcan *sl, unsigned char s) ...@@ -490,7 +479,7 @@ static void slcan_unesc(struct slcan *sl, unsigned char s)
*************************************************************************/ *************************************************************************/
/* Encapsulate one can_frame and stuff into a TTY queue. */ /* Encapsulate one can_frame and stuff into a TTY queue. */
static void slc_encaps(struct slcan *sl, struct can_frame *cf) static void slcan_encaps(struct slcan *sl, struct can_frame *cf)
{ {
int actual, i; int actual, i;
unsigned char *pos; unsigned char *pos;
...@@ -507,11 +496,11 @@ static void slc_encaps(struct slcan *sl, struct can_frame *cf) ...@@ -507,11 +496,11 @@ static void slc_encaps(struct slcan *sl, struct can_frame *cf)
/* determine number of chars for the CAN-identifier */ /* determine number of chars for the CAN-identifier */
if (cf->can_id & CAN_EFF_FLAG) { if (cf->can_id & CAN_EFF_FLAG) {
id &= CAN_EFF_MASK; id &= CAN_EFF_MASK;
endpos = pos + SLC_EFF_ID_LEN; endpos = pos + SLCAN_EFF_ID_LEN;
} else { } else {
*pos |= 0x20; /* convert R/T to lower case for SFF */ *pos |= 0x20; /* convert R/T to lower case for SFF */
id &= CAN_SFF_MASK; id &= CAN_SFF_MASK;
endpos = pos + SLC_SFF_ID_LEN; endpos = pos + SLCAN_SFF_ID_LEN;
} }
/* build 3 (SFF) or 8 (EFF) digit CAN identifier */ /* build 3 (SFF) or 8 (EFF) digit CAN identifier */
...@@ -521,7 +510,8 @@ static void slc_encaps(struct slcan *sl, struct can_frame *cf) ...@@ -521,7 +510,8 @@ static void slc_encaps(struct slcan *sl, struct can_frame *cf)
id >>= 4; id >>= 4;
} }
pos += (cf->can_id & CAN_EFF_FLAG) ? SLC_EFF_ID_LEN : SLC_SFF_ID_LEN; pos += (cf->can_id & CAN_EFF_FLAG) ?
SLCAN_EFF_ID_LEN : SLCAN_SFF_ID_LEN;
*pos++ = cf->len + '0'; *pos++ = cf->len + '0';
...@@ -557,9 +547,8 @@ static void slcan_transmit(struct work_struct *work) ...@@ -557,9 +547,8 @@ static void slcan_transmit(struct work_struct *work)
spin_lock_bh(&sl->lock); spin_lock_bh(&sl->lock);
/* First make sure we're connected. */ /* First make sure we're connected. */
if (!sl->tty || sl->magic != SLCAN_MAGIC || if (unlikely(!netif_running(sl->dev)) &&
(unlikely(!netif_running(sl->dev)) && likely(!test_bit(SLF_XCMD, &sl->flags))) {
likely(!test_bit(SLF_XCMD, &sl->flags)))) {
spin_unlock_bh(&sl->lock); spin_unlock_bh(&sl->lock);
return; return;
} }
...@@ -594,17 +583,14 @@ static void slcan_transmit(struct work_struct *work) ...@@ -594,17 +583,14 @@ static void slcan_transmit(struct work_struct *work)
*/ */
static void slcan_write_wakeup(struct tty_struct *tty) static void slcan_write_wakeup(struct tty_struct *tty)
{ {
struct slcan *sl; struct slcan *sl = (struct slcan *)tty->disc_data;
rcu_read_lock(); schedule_work(&sl->tx_work);
sl = rcu_dereference(tty->disc_data);
if (sl)
schedule_work(&sl->tx_work);
rcu_read_unlock();
} }
/* Send a can_frame to a TTY queue. */ /* Send a can_frame to a TTY queue. */
static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev) static netdev_tx_t slcan_netdev_xmit(struct sk_buff *skb,
struct net_device *dev)
{ {
struct slcan *sl = netdev_priv(dev); struct slcan *sl = netdev_priv(dev);
...@@ -623,7 +609,7 @@ static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev) ...@@ -623,7 +609,7 @@ static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
} }
netif_stop_queue(sl->dev); netif_stop_queue(sl->dev);
slc_encaps(sl, (struct can_frame *)skb->data); /* encaps & send */ slcan_encaps(sl, (struct can_frame *)skb->data); /* encaps & send */
spin_unlock(&sl->lock); spin_unlock(&sl->lock);
out: out:
...@@ -666,30 +652,26 @@ static int slcan_transmit_cmd(struct slcan *sl, const unsigned char *cmd) ...@@ -666,30 +652,26 @@ static int slcan_transmit_cmd(struct slcan *sl, const unsigned char *cmd)
} }
/* Netdevice UP -> DOWN routine */ /* Netdevice UP -> DOWN routine */
static int slc_close(struct net_device *dev) static int slcan_netdev_close(struct net_device *dev)
{ {
struct slcan *sl = netdev_priv(dev); struct slcan *sl = netdev_priv(dev);
int err; int err;
spin_lock_bh(&sl->lock); if (sl->can.bittiming.bitrate &&
if (sl->tty) { sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
if (sl->can.bittiming.bitrate && err = slcan_transmit_cmd(sl, "C\r");
sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) { if (err)
spin_unlock_bh(&sl->lock); netdev_warn(dev,
err = slcan_transmit_cmd(sl, "C\r"); "failed to send close command 'C\\r'\n");
spin_lock_bh(&sl->lock);
if (err)
netdev_warn(dev,
"failed to send close command 'C\\r'\n");
}
/* TTY discipline is running. */
clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
} }
/* TTY discipline is running. */
clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
flush_work(&sl->tx_work);
netif_stop_queue(dev); netif_stop_queue(dev);
sl->rcount = 0; sl->rcount = 0;
sl->xleft = 0; sl->xleft = 0;
spin_unlock_bh(&sl->lock);
close_candev(dev); close_candev(dev);
sl->can.state = CAN_STATE_STOPPED; sl->can.state = CAN_STATE_STOPPED;
if (sl->can.bittiming.bitrate == CAN_BITRATE_UNKNOWN) if (sl->can.bittiming.bitrate == CAN_BITRATE_UNKNOWN)
...@@ -699,15 +681,12 @@ static int slc_close(struct net_device *dev) ...@@ -699,15 +681,12 @@ static int slc_close(struct net_device *dev)
} }
/* Netdevice DOWN -> UP routine */ /* Netdevice DOWN -> UP routine */
static int slc_open(struct net_device *dev) static int slcan_netdev_open(struct net_device *dev)
{ {
struct slcan *sl = netdev_priv(dev); struct slcan *sl = netdev_priv(dev);
unsigned char cmd[SLC_MTU]; unsigned char cmd[SLCAN_MTU];
int err, s; int err, s;
if (!sl->tty)
return -ENODEV;
/* The baud rate is not set with the command /* The baud rate is not set with the command
* `ip link set <iface> type can bitrate <baud>' and therefore * `ip link set <iface> type can bitrate <baud>' and therefore
* can.bittiming.bitrate is CAN_BITRATE_UNSET (0), causing * can.bittiming.bitrate is CAN_BITRATE_UNSET (0), causing
...@@ -722,8 +701,6 @@ static int slc_open(struct net_device *dev) ...@@ -722,8 +701,6 @@ static int slc_open(struct net_device *dev)
return err; return err;
} }
sl->flags &= BIT(SLF_INUSE);
if (sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) { if (sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
for (s = 0; s < ARRAY_SIZE(slcan_bitrate_const); s++) { for (s = 0; s < ARRAY_SIZE(slcan_bitrate_const); s++) {
if (sl->can.bittiming.bitrate == slcan_bitrate_const[s]) if (sl->can.bittiming.bitrate == slcan_bitrate_const[s])
...@@ -751,10 +728,20 @@ static int slc_open(struct net_device *dev) ...@@ -751,10 +728,20 @@ static int slc_open(struct net_device *dev)
} }
} }
err = slcan_transmit_cmd(sl, "O\r"); if (sl->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
if (err) { err = slcan_transmit_cmd(sl, "L\r");
netdev_err(dev, "failed to send open command 'O\\r'\n"); if (err) {
goto cmd_transmit_failed; netdev_err(dev,
"failed to send listen-only command 'L\\r'\n");
goto cmd_transmit_failed;
}
} else {
err = slcan_transmit_cmd(sl, "O\r");
if (err) {
netdev_err(dev,
"failed to send open command 'O\\r'\n");
goto cmd_transmit_failed;
}
} }
} }
...@@ -767,24 +754,11 @@ static int slc_open(struct net_device *dev) ...@@ -767,24 +754,11 @@ static int slc_open(struct net_device *dev)
return err; return err;
} }
static void slc_dealloc(struct slcan *sl) static const struct net_device_ops slcan_netdev_ops = {
{ .ndo_open = slcan_netdev_open,
int i = sl->dev->base_addr; .ndo_stop = slcan_netdev_close,
.ndo_start_xmit = slcan_netdev_xmit,
free_candev(sl->dev); .ndo_change_mtu = can_change_mtu,
slcan_devs[i] = NULL;
}
static int slcan_change_mtu(struct net_device *dev, int new_mtu)
{
return -EINVAL;
}
static const struct net_device_ops slc_netdev_ops = {
.ndo_open = slc_open,
.ndo_stop = slc_close,
.ndo_start_xmit = slc_xmit,
.ndo_change_mtu = slcan_change_mtu,
}; };
/****************************************** /******************************************
...@@ -804,7 +778,7 @@ static void slcan_receive_buf(struct tty_struct *tty, ...@@ -804,7 +778,7 @@ static void slcan_receive_buf(struct tty_struct *tty,
{ {
struct slcan *sl = (struct slcan *)tty->disc_data; struct slcan *sl = (struct slcan *)tty->disc_data;
if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev)) if (!netif_running(sl->dev))
return; return;
/* Read the characters out of the buffer */ /* Read the characters out of the buffer */
...@@ -819,80 +793,15 @@ static void slcan_receive_buf(struct tty_struct *tty, ...@@ -819,80 +793,15 @@ static void slcan_receive_buf(struct tty_struct *tty,
} }
} }
/************************************
* slcan_open helper routines.
************************************/
/* Collect hanged up channels */
static void slc_sync(void)
{
int i;
struct net_device *dev;
struct slcan *sl;
for (i = 0; i < maxdev; i++) {
dev = slcan_devs[i];
if (!dev)
break;
sl = netdev_priv(dev);
if (sl->tty)
continue;
if (dev->flags & IFF_UP)
dev_close(dev);
}
}
/* Find a free SLCAN channel, and link in this `tty' line. */
static struct slcan *slc_alloc(void)
{
int i;
struct net_device *dev = NULL;
struct slcan *sl;
for (i = 0; i < maxdev; i++) {
dev = slcan_devs[i];
if (!dev)
break;
}
/* Sorry, too many, all slots in use */
if (i >= maxdev)
return NULL;
dev = alloc_candev(sizeof(*sl), 1);
if (!dev)
return NULL;
snprintf(dev->name, sizeof(dev->name), "slcan%d", i);
dev->netdev_ops = &slc_netdev_ops;
dev->ethtool_ops = &slcan_ethtool_ops;
dev->base_addr = i;
sl = netdev_priv(dev);
/* Initialize channel control data */
sl->magic = SLCAN_MAGIC;
sl->dev = dev;
sl->can.bitrate_const = slcan_bitrate_const;
sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);
spin_lock_init(&sl->lock);
INIT_WORK(&sl->tx_work, slcan_transmit);
init_waitqueue_head(&sl->xcmd_wait);
slcan_devs[i] = dev;
return sl;
}
/* Open the high-level part of the SLCAN channel. /* Open the high-level part of the SLCAN channel.
* This function is called by the TTY module when the * This function is called by the TTY module when the
* SLCAN line discipline is called for. Because we are * SLCAN line discipline is called for.
* sure the tty line exists, we only have to link it to
* a free SLCAN channel...
* *
* Called in process context serialized from other ldisc calls. * Called in process context serialized from other ldisc calls.
*/ */
static int slcan_open(struct tty_struct *tty) static int slcan_open(struct tty_struct *tty)
{ {
struct net_device *dev;
struct slcan *sl; struct slcan *sl;
int err; int err;
...@@ -902,72 +811,50 @@ static int slcan_open(struct tty_struct *tty) ...@@ -902,72 +811,50 @@ static int slcan_open(struct tty_struct *tty)
if (!tty->ops->write) if (!tty->ops->write)
return -EOPNOTSUPP; return -EOPNOTSUPP;
/* RTnetlink lock is misused here to serialize concurrent dev = alloc_candev(sizeof(*sl), 1);
* opens of slcan channels. There are better ways, but it is if (!dev)
* the simplest one. return -ENFILE;
*/
rtnl_lock();
/* Collect hanged up channels. */ sl = netdev_priv(dev);
slc_sync();
sl = tty->disc_data; /* Configure TTY interface */
tty->receive_room = 65536; /* We don't flow control */
sl->rcount = 0;
sl->xleft = 0;
spin_lock_init(&sl->lock);
INIT_WORK(&sl->tx_work, slcan_transmit);
init_waitqueue_head(&sl->xcmd_wait);
err = -EEXIST; /* Configure CAN metadata */
/* First make sure we're not already connected. */ sl->can.bitrate_const = slcan_bitrate_const;
if (sl && sl->magic == SLCAN_MAGIC) sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);
goto err_exit; sl->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY;
/* OK. Find a free SLCAN channel to use. */ /* Configure netdev interface */
err = -ENFILE; sl->dev = dev;
sl = slc_alloc(); dev->netdev_ops = &slcan_netdev_ops;
if (!sl) dev->ethtool_ops = &slcan_ethtool_ops;
goto err_exit;
/* Mark ldisc channel as alive */
sl->tty = tty; sl->tty = tty;
tty->disc_data = sl; tty->disc_data = sl;
if (!test_bit(SLF_INUSE, &sl->flags)) { err = register_candev(dev);
/* Perform the low-level SLCAN initialization. */ if (err) {
sl->rcount = 0; free_candev(dev);
sl->xleft = 0; pr_err("can't register candev\n");
return err;
set_bit(SLF_INUSE, &sl->flags);
rtnl_unlock();
err = register_candev(sl->dev);
if (err) {
pr_err("slcan: can't register candev\n");
goto err_free_chan;
}
} else {
rtnl_unlock();
} }
tty->receive_room = 65536; /* We don't flow control */ netdev_info(dev, "slcan on %s.\n", tty->name);
/* TTY layer expects 0 on success */ /* TTY layer expects 0 on success */
return 0; return 0;
err_free_chan:
rtnl_lock();
sl->tty = NULL;
tty->disc_data = NULL;
clear_bit(SLF_INUSE, &sl->flags);
slc_dealloc(sl);
rtnl_unlock();
return err;
err_exit:
rtnl_unlock();
/* Count references from TTY module */
return err;
} }
/* Close down a SLCAN channel. /* Close down a SLCAN channel.
* This means flushing out any pending queues, and then returning. This * This means flushing out any pending queues, and then returning. This
* call is serialized against other ldisc functions. * call is serialized against other ldisc functions.
* Once this is called, no other ldisc function of ours is entered.
* *
* We also use this method for a hangup event. * We also use this method for a hangup event.
*/ */
...@@ -975,28 +862,20 @@ static void slcan_close(struct tty_struct *tty) ...@@ -975,28 +862,20 @@ static void slcan_close(struct tty_struct *tty)
{ {
struct slcan *sl = (struct slcan *)tty->disc_data; struct slcan *sl = (struct slcan *)tty->disc_data;
/* First make sure we're connected. */ /* unregister_netdev() calls .ndo_stop() so we don't have to.
if (!sl || sl->magic != SLCAN_MAGIC || sl->tty != tty) * Our .ndo_stop() also flushes the TTY write wakeup handler,
return; * so we can safely set sl->tty = NULL after this.
*/
unregister_candev(sl->dev);
/* Mark channel as dead */
spin_lock_bh(&sl->lock); spin_lock_bh(&sl->lock);
rcu_assign_pointer(tty->disc_data, NULL); tty->disc_data = NULL;
sl->tty = NULL; sl->tty = NULL;
spin_unlock_bh(&sl->lock); spin_unlock_bh(&sl->lock);
synchronize_rcu(); netdev_info(sl->dev, "slcan off %s.\n", tty->name);
flush_work(&sl->tx_work); free_candev(sl->dev);
slc_close(sl->dev);
unregister_candev(sl->dev);
rtnl_lock();
slc_dealloc(sl);
rtnl_unlock();
}
static void slcan_hangup(struct tty_struct *tty)
{
slcan_close(tty);
} }
/* Perform I/O control on an active SLCAN channel. */ /* Perform I/O control on an active SLCAN channel. */
...@@ -1006,10 +885,6 @@ static int slcan_ioctl(struct tty_struct *tty, unsigned int cmd, ...@@ -1006,10 +885,6 @@ static int slcan_ioctl(struct tty_struct *tty, unsigned int cmd,
struct slcan *sl = (struct slcan *)tty->disc_data; struct slcan *sl = (struct slcan *)tty->disc_data;
unsigned int tmp; unsigned int tmp;
/* First make sure we're connected. */
if (!sl || sl->magic != SLCAN_MAGIC)
return -EINVAL;
switch (cmd) { switch (cmd) {
case SIOCGIFNAME: case SIOCGIFNAME:
tmp = strlen(sl->dev->name) + 1; tmp = strlen(sl->dev->name) + 1;
...@@ -1025,13 +900,12 @@ static int slcan_ioctl(struct tty_struct *tty, unsigned int cmd, ...@@ -1025,13 +900,12 @@ static int slcan_ioctl(struct tty_struct *tty, unsigned int cmd,
} }
} }
static struct tty_ldisc_ops slc_ldisc = { static struct tty_ldisc_ops slcan_ldisc = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.num = N_SLCAN, .num = N_SLCAN,
.name = "slcan", .name = KBUILD_MODNAME,
.open = slcan_open, .open = slcan_open,
.close = slcan_close, .close = slcan_close,
.hangup = slcan_hangup,
.ioctl = slcan_ioctl, .ioctl = slcan_ioctl,
.receive_buf = slcan_receive_buf, .receive_buf = slcan_receive_buf,
.write_wakeup = slcan_write_wakeup, .write_wakeup = slcan_write_wakeup,
...@@ -1041,79 +915,22 @@ static int __init slcan_init(void) ...@@ -1041,79 +915,22 @@ static int __init slcan_init(void)
{ {
int status; int status;
if (maxdev < 4) pr_info("serial line CAN interface driver\n");
maxdev = 4; /* Sanity */
pr_info("slcan: serial line CAN interface driver\n");
pr_info("slcan: %d dynamic interface channels.\n", maxdev);
slcan_devs = kcalloc(maxdev, sizeof(struct net_device *), GFP_KERNEL);
if (!slcan_devs)
return -ENOMEM;
/* Fill in our line protocol discipline, and register it */ /* Fill in our line protocol discipline, and register it */
status = tty_register_ldisc(&slc_ldisc); status = tty_register_ldisc(&slcan_ldisc);
if (status) { if (status)
pr_err("slcan: can't register line discipline\n"); pr_err("can't register line discipline\n");
kfree(slcan_devs);
}
return status; return status;
} }
static void __exit slcan_exit(void) static void __exit slcan_exit(void)
{ {
int i; /* This will only be called when all channels have been closed by
struct net_device *dev; * userspace - tty_ldisc.c takes care of the module's refcount.
struct slcan *sl;
unsigned long timeout = jiffies + HZ;
int busy = 0;
if (!slcan_devs)
return;
/* First of all: check for active disciplines and hangup them.
*/
do {
if (busy)
msleep_interruptible(100);
busy = 0;
for (i = 0; i < maxdev; i++) {
dev = slcan_devs[i];
if (!dev)
continue;
sl = netdev_priv(dev);
spin_lock_bh(&sl->lock);
if (sl->tty) {
busy++;
tty_hangup(sl->tty);
}
spin_unlock_bh(&sl->lock);
}
} while (busy && time_before(jiffies, timeout));
/* FIXME: hangup is async so we should wait when doing this second
* phase
*/ */
tty_unregister_ldisc(&slcan_ldisc);
for (i = 0; i < maxdev; i++) {
dev = slcan_devs[i];
if (!dev)
continue;
sl = netdev_priv(dev);
if (sl->tty)
netdev_err(dev, "tty discipline still running\n");
slc_close(dev);
unregister_candev(dev);
slc_dealloc(sl);
}
kfree(slcan_devs);
slcan_devs = NULL;
tty_unregister_ldisc(&slc_ldisc);
} }
module_init(slcan_init); module_init(slcan_init);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment