2 * slcan.c - serial line CAN interface driver (using tty line discipline)
4 * This file is derived from linux/drivers/net/slip/slip.c
6 * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk>
7 * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
8 * slcan.c Author : Oliver Hartkopp <socketcan@hartkopp.net>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see http://www.gnu.org/licenses/gpl.html
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
41 #include <linux/uaccess.h>
42 #include <linux/bitops.h>
43 #include <linux/string.h>
44 #include <linux/tty.h>
45 #include <linux/errno.h>
46 #include <linux/netdevice.h>
47 #include <linux/skbuff.h>
48 #include <linux/rtnetlink.h>
49 #include <linux/if_arp.h>
50 #include <linux/if_ether.h>
51 #include <linux/sched.h>
52 #include <linux/delay.h>
53 #include <linux/init.h>
54 #include <linux/kernel.h>
55 #include <linux/workqueue.h>
56 #include <linux/can.h>
57 #include <linux/can/dev.h>
58 #include <linux/can/skb.h>
62 MODULE_ALIAS_LDISC(N_SLCAN);
63 MODULE_DESCRIPTION("serial line CAN interface");
64 MODULE_LICENSE("GPL");
65 MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
67 #define SLCAN_MAGIC 0x53CA
69 static int maxdev = 10; /* MAX number of SLCAN channels;
70 * This can be overridden with
71 * insmod slcan.ko maxdev=nnn
73 module_param(maxdev, int, 0);
74 MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
76 /* maximum rx buffer len: extended CAN frame with timestamp */
77 #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
80 #define SLC_SFF_ID_LEN 3
81 #define SLC_EFF_ID_LEN 8
82 #define SLC_STATE_LEN 1
83 #define SLC_STATE_BE_RXCNT_LEN 3
84 #define SLC_STATE_BE_TXCNT_LEN 3
85 #define SLC_STATE_FRAME_LEN (1 + SLC_CMD_LEN + SLC_STATE_BE_RXCNT_LEN + \
86 SLC_STATE_BE_TXCNT_LEN)
92 struct tty_struct *tty; /* ptr to TTY structure */
93 struct net_device *dev; /* easy for intr handling */
95 struct work_struct tx_work; /* Flushes transmit buffer */
97 /* These are pointers to the malloc()ed frame buffers. */
98 unsigned char rbuff[SLC_MTU]; /* receiver buffer */
99 int rcount; /* received chars counter */
100 unsigned char xbuff[SLC_MTU]; /* transmitter buffer */
101 unsigned char *xhead; /* pointer to next XMIT byte */
102 int xleft; /* bytes left in XMIT queue */
104 unsigned long flags; /* Flag values/ mode etc */
105 #define SLF_INUSE 0 /* Channel in use */
106 #define SLF_ERROR 1 /* Parity, etc. error */
107 #define SLF_XCMD 2 /* Command transmission */
108 unsigned long cmd_flags; /* Command flags */
109 #define CF_ERR_RST 0 /* Reset errors on open */
110 wait_queue_head_t xcmd_wait; /* Wait queue for commands */
114 static struct net_device **slcan_devs;
116 static const u32 slcan_bitrate_const[] = {
117 10000, 20000, 50000, 100000, 125000,
118 250000, 500000, 800000, 1000000
121 bool slcan_err_rst_on_open(struct net_device *ndev)
123 struct slcan *sl = netdev_priv(ndev);
125 return !!test_bit(CF_ERR_RST, &sl->cmd_flags);
128 int slcan_enable_err_rst_on_open(struct net_device *ndev, bool on)
130 struct slcan *sl = netdev_priv(ndev);
132 if (netif_running(ndev))
136 set_bit(CF_ERR_RST, &sl->cmd_flags);
138 clear_bit(CF_ERR_RST, &sl->cmd_flags);
143 /*************************************************************************
144 * SLCAN ENCAPSULATION FORMAT *
145 *************************************************************************/
147 /* A CAN frame has a can_id (11 bit standard frame format OR 29 bit extended
148 * frame format) a data length code (len) which can be from 0 to 8
149 * and up to <len> data bytes as payload.
150 * Additionally a CAN frame may become a remote transmission frame if the
151 * RTR-bit is set. This causes another ECU to send a CAN frame with the
154 * The SLCAN ASCII representation of these different frame types is:
155 * <type> <id> <dlc> <data>*
157 * Extended frames (29 bit) are defined by capital characters in the type.
158 * RTR frames are defined as 'r' types - normal frames have 't' type:
159 * t => 11 bit data frame
160 * r => 11 bit RTR frame
161 * T => 29 bit data frame
162 * R => 29 bit RTR frame
164 * The <id> is 3 (standard) or 8 (extended) bytes in ASCII Hex (base64).
165 * The <dlc> is a one byte ASCII number ('0' - '8')
166 * The <data> section has at much ASCII Hex bytes as defined by the <dlc>
170 * t1230 : can_id 0x123, len 0, no data
171 * t4563112233 : can_id 0x456, len 3, data 0x11 0x22 0x33
172 * T12ABCDEF2AA55 : extended can_id 0x12ABCDEF, len 2, data 0xAA 0x55
173 * r1230 : can_id 0x123, len 0, no data, remote transmission request
177 /*************************************************************************
178 * STANDARD SLCAN DECAPSULATION *
179 *************************************************************************/
181 /* Send one completely decapsulated can_frame to the network layer */
182 static void slc_bump_frame(struct slcan *sl)
185 struct can_frame *cf;
188 char *cmd = sl->rbuff;
190 skb = alloc_can_skb(sl->dev, &cf);
191 if (unlikely(!skb)) {
192 sl->dev->stats.rx_dropped++;
198 cf->can_id = CAN_RTR_FLAG;
201 /* store dlc ASCII value and terminate SFF CAN ID string */
202 cf->len = sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN];
203 sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN] = 0;
204 /* point to payload data behind the dlc */
205 cmd += SLC_CMD_LEN + SLC_SFF_ID_LEN + 1;
208 cf->can_id = CAN_RTR_FLAG;
211 cf->can_id |= CAN_EFF_FLAG;
212 /* store dlc ASCII value and terminate EFF CAN ID string */
213 cf->len = sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN];
214 sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN] = 0;
215 /* point to payload data behind the dlc */
216 cmd += SLC_CMD_LEN + SLC_EFF_ID_LEN + 1;
222 if (kstrtou32(sl->rbuff + SLC_CMD_LEN, 16, &tmpid))
227 /* get len from sanitized ASCII value */
228 if (cf->len >= '0' && cf->len < '9')
233 /* RTR frames may have a dlc > 0 but they never have any data bytes */
234 if (!(cf->can_id & CAN_RTR_FLAG)) {
235 for (i = 0; i < cf->len; i++) {
236 tmp = hex_to_bin(*cmd++);
240 cf->data[i] = (tmp << 4);
241 tmp = hex_to_bin(*cmd++);
249 sl->dev->stats.rx_packets++;
250 if (!(cf->can_id & CAN_RTR_FLAG))
251 sl->dev->stats.rx_bytes += cf->len;
257 sl->dev->stats.rx_errors++;
261 /* A change state frame must contain state info and receive and transmit
266 * sb256256 : state bus-off: rx counter 256, tx counter 256
267 * sa057033 : state active, rx counter 57, tx counter 33
269 static void slc_bump_state(struct slcan *sl)
271 struct net_device *dev = sl->dev;
273 struct can_frame *cf;
274 char *cmd = sl->rbuff;
276 enum can_state state, rx_state, tx_state;
280 state = CAN_STATE_ERROR_ACTIVE;
283 state = CAN_STATE_ERROR_WARNING;
286 state = CAN_STATE_ERROR_PASSIVE;
289 state = CAN_STATE_BUS_OFF;
295 if (state == sl->can.state || sl->rcount < SLC_STATE_FRAME_LEN)
298 cmd += SLC_STATE_BE_RXCNT_LEN + SLC_CMD_LEN + 1;
299 cmd[SLC_STATE_BE_TXCNT_LEN] = 0;
300 if (kstrtou32(cmd, 10, &txerr))
304 cmd -= SLC_STATE_BE_RXCNT_LEN;
305 if (kstrtou32(cmd, 10, &rxerr))
308 skb = alloc_can_err_skb(dev, &cf);
316 tx_state = txerr >= rxerr ? state : 0;
317 rx_state = txerr <= rxerr ? state : 0;
318 can_change_state(dev, cf, tx_state, rx_state);
320 if (state == CAN_STATE_BUS_OFF)
327 /* An error frame can contain more than one type of error.
331 * e1a : len 1, errors: ACK error
332 * e3bcO: len 3, errors: Bit0 error, CRC error, Tx overrun error
334 static void slc_bump_err(struct slcan *sl)
336 struct net_device *dev = sl->dev;
338 struct can_frame *cf;
339 char *cmd = sl->rbuff;
340 bool rx_errors = false, tx_errors = false, rx_over_errors = false;
343 /* get len from sanitized ASCII value */
345 if (len >= '0' && len < '9')
350 if ((len + SLC_CMD_LEN + 1) > sl->rcount)
353 skb = alloc_can_err_skb(dev, &cf);
356 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
358 cmd += SLC_CMD_LEN + 1;
359 for (i = 0; i < len; i++, cmd++) {
362 netdev_dbg(dev, "ACK error\n");
365 cf->can_id |= CAN_ERR_ACK;
366 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
371 netdev_dbg(dev, "Bit0 error\n");
374 cf->data[2] |= CAN_ERR_PROT_BIT0;
378 netdev_dbg(dev, "Bit1 error\n");
381 cf->data[2] |= CAN_ERR_PROT_BIT1;
385 netdev_dbg(dev, "CRC error\n");
388 cf->data[2] |= CAN_ERR_PROT_BIT;
389 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
394 netdev_dbg(dev, "Form Error\n");
397 cf->data[2] |= CAN_ERR_PROT_FORM;
401 netdev_dbg(dev, "Rx overrun error\n");
402 rx_over_errors = true;
405 cf->can_id |= CAN_ERR_CRTL;
406 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
411 netdev_dbg(dev, "Tx overrun error\n");
414 cf->can_id |= CAN_ERR_CRTL;
415 cf->data[1] = CAN_ERR_CRTL_TX_OVERFLOW;
420 netdev_dbg(dev, "Stuff error\n");
423 cf->data[2] |= CAN_ERR_PROT_STUFF;
435 dev->stats.rx_errors++;
438 dev->stats.rx_over_errors++;
441 dev->stats.tx_errors++;
447 static void slc_bump(struct slcan *sl)
449 switch (sl->rbuff[0]) {
457 return slc_bump_frame(sl);
459 return slc_bump_err(sl);
461 return slc_bump_state(sl);
467 /* parse tty input stream */
468 static void slcan_unesc(struct slcan *sl, unsigned char s)
470 if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
471 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
477 if (!test_bit(SLF_ERROR, &sl->flags)) {
478 if (sl->rcount < SLC_MTU) {
479 sl->rbuff[sl->rcount++] = s;
482 sl->dev->stats.rx_over_errors++;
483 set_bit(SLF_ERROR, &sl->flags);
489 /*************************************************************************
490 * STANDARD SLCAN ENCAPSULATION *
491 *************************************************************************/
493 /* Encapsulate one can_frame and stuff into a TTY queue. */
494 static void slc_encaps(struct slcan *sl, struct can_frame *cf)
498 unsigned char *endpos;
499 canid_t id = cf->can_id;
503 if (cf->can_id & CAN_RTR_FLAG)
504 *pos = 'R'; /* becomes 'r' in standard frame format (SFF) */
506 *pos = 'T'; /* becomes 't' in standard frame format (SSF) */
508 /* determine number of chars for the CAN-identifier */
509 if (cf->can_id & CAN_EFF_FLAG) {
511 endpos = pos + SLC_EFF_ID_LEN;
513 *pos |= 0x20; /* convert R/T to lower case for SFF */
515 endpos = pos + SLC_SFF_ID_LEN;
518 /* build 3 (SFF) or 8 (EFF) digit CAN identifier */
520 while (endpos >= pos) {
521 *endpos-- = hex_asc_upper[id & 0xf];
525 pos += (cf->can_id & CAN_EFF_FLAG) ? SLC_EFF_ID_LEN : SLC_SFF_ID_LEN;
527 *pos++ = cf->len + '0';
529 /* RTR frames may have a dlc > 0 but they never have any data bytes */
530 if (!(cf->can_id & CAN_RTR_FLAG)) {
531 for (i = 0; i < cf->len; i++)
532 pos = hex_byte_pack_upper(pos, cf->data[i]);
534 sl->dev->stats.tx_bytes += cf->len;
539 /* Order of next two lines is *very* important.
540 * When we are sending a little amount of data,
541 * the transfer may be completed inside the ops->write()
542 * routine, because it's running with interrupts enabled.
543 * In this case we *never* got WRITE_WAKEUP event,
544 * if we did not request it before write operation.
545 * 14 Oct 1994 Dmitry Gorodchanin.
547 set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
548 actual = sl->tty->ops->write(sl->tty, sl->xbuff, pos - sl->xbuff);
549 sl->xleft = (pos - sl->xbuff) - actual;
550 sl->xhead = sl->xbuff + actual;
553 /* Write out any remaining transmit buffer. Scheduled when tty is writable */
554 static void slcan_transmit(struct work_struct *work)
556 struct slcan *sl = container_of(work, struct slcan, tx_work);
559 spin_lock_bh(&sl->lock);
560 /* First make sure we're connected. */
561 if (!sl->tty || sl->magic != SLCAN_MAGIC ||
562 (unlikely(!netif_running(sl->dev)) &&
563 likely(!test_bit(SLF_XCMD, &sl->flags)))) {
564 spin_unlock_bh(&sl->lock);
568 if (sl->xleft <= 0) {
569 if (unlikely(test_bit(SLF_XCMD, &sl->flags))) {
570 clear_bit(SLF_XCMD, &sl->flags);
571 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
572 spin_unlock_bh(&sl->lock);
573 wake_up(&sl->xcmd_wait);
577 /* Now serial buffer is almost free & we can start
578 * transmission of another packet
580 sl->dev->stats.tx_packets++;
581 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
582 spin_unlock_bh(&sl->lock);
583 netif_wake_queue(sl->dev);
587 actual = sl->tty->ops->write(sl->tty, sl->xhead, sl->xleft);
590 spin_unlock_bh(&sl->lock);
593 /* Called by the driver when there's room for more data.
594 * Schedule the transmit.
596 static void slcan_write_wakeup(struct tty_struct *tty)
601 sl = rcu_dereference(tty->disc_data);
603 schedule_work(&sl->tx_work);
607 /* Send a can_frame to a TTY queue. */
608 static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
610 struct slcan *sl = netdev_priv(dev);
612 if (can_dropped_invalid_skb(dev, skb))
615 spin_lock(&sl->lock);
616 if (!netif_running(dev)) {
617 spin_unlock(&sl->lock);
618 netdev_warn(dev, "xmit: iface is down\n");
621 if (sl->tty == NULL) {
622 spin_unlock(&sl->lock);
626 netif_stop_queue(sl->dev);
627 slc_encaps(sl, (struct can_frame *) skb->data); /* encaps & send */
628 spin_unlock(&sl->lock);
635 /******************************************
636 * Routines looking at netdevice side.
637 ******************************************/
639 static int slcan_transmit_cmd(struct slcan *sl, const unsigned char *cmd)
643 spin_lock(&sl->lock);
645 spin_unlock(&sl->lock);
649 n = scnprintf(sl->xbuff, sizeof(sl->xbuff), "%s", cmd);
650 set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
651 actual = sl->tty->ops->write(sl->tty, sl->xbuff, n);
652 sl->xleft = n - actual;
653 sl->xhead = sl->xbuff + actual;
654 set_bit(SLF_XCMD, &sl->flags);
655 spin_unlock(&sl->lock);
656 ret = wait_event_interruptible_timeout(sl->xcmd_wait,
657 !test_bit(SLF_XCMD, &sl->flags),
659 clear_bit(SLF_XCMD, &sl->flags);
660 if (ret == -ERESTARTSYS)
669 /* Netdevice UP -> DOWN routine */
670 static int slc_close(struct net_device *dev)
672 struct slcan *sl = netdev_priv(dev);
675 spin_lock_bh(&sl->lock);
677 if (sl->can.bittiming.bitrate &&
678 sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
679 spin_unlock_bh(&sl->lock);
680 err = slcan_transmit_cmd(sl, "C\r");
681 spin_lock_bh(&sl->lock);
684 "failed to send close command 'C\\r'\n");
687 /* TTY discipline is running. */
688 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
690 netif_stop_queue(dev);
692 sl->can.state = CAN_STATE_STOPPED;
693 if (sl->can.bittiming.bitrate == CAN_BITRATE_UNKNOWN)
694 sl->can.bittiming.bitrate = CAN_BITRATE_UNSET;
698 spin_unlock_bh(&sl->lock);
703 /* Netdevice DOWN -> UP routine */
704 static int slc_open(struct net_device *dev)
706 struct slcan *sl = netdev_priv(dev);
707 unsigned char cmd[SLC_MTU];
713 /* The baud rate is not set with the command
714 * `ip link set <iface> type can bitrate <baud>' and therefore
715 * can.bittiming.bitrate is CAN_BITRATE_UNSET (0), causing
716 * open_candev() to fail. So let's set to a fake value.
718 if (sl->can.bittiming.bitrate == CAN_BITRATE_UNSET)
719 sl->can.bittiming.bitrate = CAN_BITRATE_UNKNOWN;
721 err = open_candev(dev);
723 netdev_err(dev, "failed to open can device\n");
727 sl->flags &= BIT(SLF_INUSE);
729 if (sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
730 for (s = 0; s < ARRAY_SIZE(slcan_bitrate_const); s++) {
731 if (sl->can.bittiming.bitrate == slcan_bitrate_const[s])
735 /* The CAN framework has already validate the bitrate value,
736 * so we can avoid to check if `s' has been properly set.
738 snprintf(cmd, sizeof(cmd), "C\rS%d\r", s);
739 err = slcan_transmit_cmd(sl, cmd);
742 "failed to send bitrate command 'C\\rS%d\\r'\n",
744 goto cmd_transmit_failed;
747 if (test_bit(CF_ERR_RST, &sl->cmd_flags)) {
748 err = slcan_transmit_cmd(sl, "F\r");
751 "failed to send error command 'F\\r'\n");
752 goto cmd_transmit_failed;
756 err = slcan_transmit_cmd(sl, "O\r");
758 netdev_err(dev, "failed to send open command 'O\\r'\n");
759 goto cmd_transmit_failed;
763 sl->can.state = CAN_STATE_ERROR_ACTIVE;
764 netif_start_queue(dev);
772 static void slc_dealloc(struct slcan *sl)
774 int i = sl->dev->base_addr;
776 free_candev(sl->dev);
777 slcan_devs[i] = NULL;
780 static int slcan_change_mtu(struct net_device *dev, int new_mtu)
785 static const struct net_device_ops slc_netdev_ops = {
786 .ndo_open = slc_open,
787 .ndo_stop = slc_close,
788 .ndo_start_xmit = slc_xmit,
789 .ndo_change_mtu = slcan_change_mtu,
792 /******************************************
793 * Routines looking at TTY side.
794 ******************************************/
796 /* Handle the 'receiver data ready' interrupt.
797 * This function is called by the 'tty_io' module in the kernel when
798 * a block of SLCAN data has been received, which can now be decapsulated
799 * and sent on to some IP layer for further processing. This will not
800 * be re-entered while running but other ldisc functions may be called
803 static void slcan_receive_buf(struct tty_struct *tty,
804 const unsigned char *cp, const char *fp,
807 struct slcan *sl = (struct slcan *) tty->disc_data;
809 if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
812 /* Read the characters out of the buffer */
815 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
816 sl->dev->stats.rx_errors++;
820 slcan_unesc(sl, *cp++);
824 /************************************
825 * slcan_open helper routines.
826 ************************************/
828 /* Collect hanged up channels */
829 static void slc_sync(void)
832 struct net_device *dev;
835 for (i = 0; i < maxdev; i++) {
840 sl = netdev_priv(dev);
843 if (dev->flags & IFF_UP)
848 /* Find a free SLCAN channel, and link in this `tty' line. */
849 static struct slcan *slc_alloc(void)
852 struct net_device *dev = NULL;
855 for (i = 0; i < maxdev; i++) {
862 /* Sorry, too many, all slots in use */
866 dev = alloc_candev(sizeof(*sl), 1);
870 snprintf(dev->name, sizeof(dev->name), "slcan%d", i);
871 dev->netdev_ops = &slc_netdev_ops;
873 slcan_set_ethtool_ops(dev);
874 sl = netdev_priv(dev);
876 /* Initialize channel control data */
877 sl->magic = SLCAN_MAGIC;
879 sl->can.bitrate_const = slcan_bitrate_const;
880 sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);
881 spin_lock_init(&sl->lock);
882 INIT_WORK(&sl->tx_work, slcan_transmit);
883 init_waitqueue_head(&sl->xcmd_wait);
889 /* Open the high-level part of the SLCAN channel.
890 * This function is called by the TTY module when the
891 * SLCAN line discipline is called for. Because we are
892 * sure the tty line exists, we only have to link it to
893 * a free SLCAN channel...
895 * Called in process context serialized from other ldisc calls.
897 static int slcan_open(struct tty_struct *tty)
902 if (!capable(CAP_NET_ADMIN))
905 if (tty->ops->write == NULL)
908 /* RTnetlink lock is misused here to serialize concurrent
909 * opens of slcan channels. There are better ways, but it is
914 /* Collect hanged up channels. */
920 /* First make sure we're not already connected. */
921 if (sl && sl->magic == SLCAN_MAGIC)
924 /* OK. Find a free SLCAN channel to use. */
933 if (!test_bit(SLF_INUSE, &sl->flags)) {
934 /* Perform the low-level SLCAN initialization. */
938 set_bit(SLF_INUSE, &sl->flags);
941 err = register_candev(sl->dev);
943 pr_err("slcan: can't register candev\n");
950 tty->receive_room = 65536; /* We don't flow control */
952 /* TTY layer expects 0 on success */
958 tty->disc_data = NULL;
959 clear_bit(SLF_INUSE, &sl->flags);
967 /* Count references from TTY module */
971 /* Close down a SLCAN channel.
972 * This means flushing out any pending queues, and then returning. This
973 * call is serialized against other ldisc functions.
975 * We also use this method for a hangup event.
977 static void slcan_close(struct tty_struct *tty)
979 struct slcan *sl = (struct slcan *) tty->disc_data;
981 /* First make sure we're connected. */
982 if (!sl || sl->magic != SLCAN_MAGIC || sl->tty != tty)
985 spin_lock_bh(&sl->lock);
986 rcu_assign_pointer(tty->disc_data, NULL);
988 spin_unlock_bh(&sl->lock);
991 flush_work(&sl->tx_work);
994 unregister_candev(sl->dev);
1000 static void slcan_hangup(struct tty_struct *tty)
1005 /* Perform I/O control on an active SLCAN channel. */
1006 static int slcan_ioctl(struct tty_struct *tty, unsigned int cmd,
1009 struct slcan *sl = (struct slcan *) tty->disc_data;
1012 /* First make sure we're connected. */
1013 if (!sl || sl->magic != SLCAN_MAGIC)
1018 tmp = strlen(sl->dev->name) + 1;
1019 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1027 return tty_mode_ioctl(tty, cmd, arg);
1031 static struct tty_ldisc_ops slc_ldisc = {
1032 .owner = THIS_MODULE,
1036 .close = slcan_close,
1037 .hangup = slcan_hangup,
1038 .ioctl = slcan_ioctl,
1039 .receive_buf = slcan_receive_buf,
1040 .write_wakeup = slcan_write_wakeup,
1043 static int __init slcan_init(void)
1048 maxdev = 4; /* Sanity */
1050 pr_info("slcan: serial line CAN interface driver\n");
1051 pr_info("slcan: %d dynamic interface channels.\n", maxdev);
1053 slcan_devs = kcalloc(maxdev, sizeof(struct net_device *), GFP_KERNEL);
1057 /* Fill in our line protocol discipline, and register it */
1058 status = tty_register_ldisc(&slc_ldisc);
1060 pr_err("slcan: can't register line discipline\n");
1066 static void __exit slcan_exit(void)
1069 struct net_device *dev;
1071 unsigned long timeout = jiffies + HZ;
1074 if (slcan_devs == NULL)
1077 /* First of all: check for active disciplines and hangup them.
1081 msleep_interruptible(100);
1084 for (i = 0; i < maxdev; i++) {
1085 dev = slcan_devs[i];
1088 sl = netdev_priv(dev);
1089 spin_lock_bh(&sl->lock);
1092 tty_hangup(sl->tty);
1094 spin_unlock_bh(&sl->lock);
1096 } while (busy && time_before(jiffies, timeout));
1098 /* FIXME: hangup is async so we should wait when doing this second
1102 for (i = 0; i < maxdev; i++) {
1103 dev = slcan_devs[i];
1107 sl = netdev_priv(dev);
1109 netdev_err(dev, "tty discipline still running\n");
1113 unregister_candev(dev);
1120 tty_unregister_ldisc(&slc_ldisc);
1123 module_init(slcan_init);
1124 module_exit(slcan_exit);