Commit 52f9ac85 authored by Dario Binacchi's avatar Dario Binacchi Committed by Marc Kleine-Budde

can: slcan: allow to send commands to the adapter

This is a preparation patch for the upcoming support to change the
bitrate via ip tool, reset the adapter error states via the ethtool API
and, more generally, send commands to the adapter.

Since the close command (i. e. "C\r") will be sent in the ndo_stop()
where netif_running() returns false, a new flag bit (i. e. SLF_XCMD) for
serial transmission has to be added.

Link: https://lore.kernel.org/all/20220628163137.413025-7-dario.binacchi@amarulasolutions.comSigned-off-by: default avatarDario Binacchi <dario.binacchi@amarulasolutions.com>
Tested-by: default avatarJeroen Hofstee <jhofstee@victronenergy.com>
Signed-off-by: default avatarMarc Kleine-Budde <mkl@pengutronix.de>
parent c4e54b06
...@@ -97,6 +97,9 @@ struct slcan { ...@@ -97,6 +97,9 @@ struct slcan {
unsigned long flags; /* Flag values/ mode etc */ unsigned long flags; /* Flag values/ mode etc */
#define SLF_INUSE 0 /* Channel in use */ #define SLF_INUSE 0 /* Channel in use */
#define SLF_ERROR 1 /* Parity, etc. error */ #define SLF_ERROR 1 /* Parity, etc. error */
#define SLF_XCMD 2 /* Command transmission */
wait_queue_head_t xcmd_wait; /* Wait queue for commands */
/* transmission */
}; };
static struct net_device **slcan_devs; static struct net_device **slcan_devs;
...@@ -314,12 +317,22 @@ static void slcan_transmit(struct work_struct *work) ...@@ -314,12 +317,22 @@ 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 || !netif_running(sl->dev)) { if (!sl->tty || sl->magic != SLCAN_MAGIC ||
(unlikely(!netif_running(sl->dev)) &&
likely(!test_bit(SLF_XCMD, &sl->flags)))) {
spin_unlock_bh(&sl->lock); spin_unlock_bh(&sl->lock);
return; return;
} }
if (sl->xleft <= 0) { if (sl->xleft <= 0) {
if (unlikely(test_bit(SLF_XCMD, &sl->flags))) {
clear_bit(SLF_XCMD, &sl->flags);
clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
spin_unlock_bh(&sl->lock);
wake_up(&sl->xcmd_wait);
return;
}
/* Now serial buffer is almost free & we can start /* Now serial buffer is almost free & we can start
* transmission of another packet */ * transmission of another packet */
sl->dev->stats.tx_packets++; sl->dev->stats.tx_packets++;
...@@ -383,6 +396,36 @@ static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev) ...@@ -383,6 +396,36 @@ static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
* Routines looking at netdevice side. * Routines looking at netdevice side.
******************************************/ ******************************************/
static int slcan_transmit_cmd(struct slcan *sl, const unsigned char *cmd)
{
int ret, actual, n;
spin_lock(&sl->lock);
if (!sl->tty) {
spin_unlock(&sl->lock);
return -ENODEV;
}
n = snprintf(sl->xbuff, sizeof(sl->xbuff), "%s", cmd);
set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
actual = sl->tty->ops->write(sl->tty, sl->xbuff, n);
sl->xleft = n - actual;
sl->xhead = sl->xbuff + actual;
set_bit(SLF_XCMD, &sl->flags);
spin_unlock(&sl->lock);
ret = wait_event_interruptible_timeout(sl->xcmd_wait,
!test_bit(SLF_XCMD, &sl->flags),
HZ);
clear_bit(SLF_XCMD, &sl->flags);
if (ret == -ERESTARTSYS)
return ret;
if (ret == 0)
return -ETIMEDOUT;
return 0;
}
/* Netdevice UP -> DOWN routine */ /* Netdevice UP -> DOWN routine */
static int slc_close(struct net_device *dev) static int slc_close(struct net_device *dev)
{ {
...@@ -540,6 +583,7 @@ static struct slcan *slc_alloc(void) ...@@ -540,6 +583,7 @@ static struct slcan *slc_alloc(void)
sl->dev = dev; sl->dev = dev;
spin_lock_init(&sl->lock); spin_lock_init(&sl->lock);
INIT_WORK(&sl->tx_work, slcan_transmit); INIT_WORK(&sl->tx_work, slcan_transmit);
init_waitqueue_head(&sl->xcmd_wait);
slcan_devs[i] = dev; slcan_devs[i] = dev;
return sl; return sl;
......
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