2 * slcan.c - serial line CAN interface driver (using tty line discipline)
4 * This file is derived from linux/drivers/net/slip/slip.c and got
5 * inspiration from linux/drivers/net/can/can327.c for the rework made
6 * on the line discipline code.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, see http://www.gnu.org/licenses/gpl.html
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43 #include <linux/module.h>
45 #include <linux/uaccess.h>
46 #include <linux/bitops.h>
47 #include <linux/string.h>
48 #include <linux/tty.h>
49 #include <linux/errno.h>
50 #include <linux/netdevice.h>
51 #include <linux/skbuff.h>
52 #include <linux/rtnetlink.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");
68 /* maximum rx buffer len: extended CAN frame with timestamp */
69 #define SLCAN_MTU (sizeof("T1111222281122334455667788EA5F\r") + 1)
71 #define SLCAN_CMD_LEN 1
72 #define SLCAN_SFF_ID_LEN 3
73 #define SLCAN_EFF_ID_LEN 8
74 #define SLCAN_STATE_LEN 1
75 #define SLCAN_STATE_BE_RXCNT_LEN 3
76 #define SLCAN_STATE_BE_TXCNT_LEN 3
77 #define SLCAN_STATE_FRAME_LEN (1 + SLCAN_CMD_LEN + \
78 SLCAN_STATE_BE_RXCNT_LEN + \
79 SLCAN_STATE_BE_TXCNT_LEN)
84 struct tty_struct *tty; /* ptr to TTY structure */
85 struct net_device *dev; /* easy for intr handling */
87 struct work_struct tx_work; /* Flushes transmit buffer */
89 /* These are pointers to the malloc()ed frame buffers. */
90 unsigned char rbuff[SLCAN_MTU]; /* receiver buffer */
91 int rcount; /* received chars counter */
92 unsigned char xbuff[SLCAN_MTU]; /* transmitter buffer*/
93 unsigned char *xhead; /* pointer to next XMIT byte */
94 int xleft; /* bytes left in XMIT queue */
96 unsigned long flags; /* Flag values/ mode etc */
97 #define SLF_ERROR 0 /* Parity, etc. error */
98 #define SLF_XCMD 1 /* Command transmission */
99 unsigned long cmd_flags; /* Command flags */
100 #define CF_ERR_RST 0 /* Reset errors on open */
101 wait_queue_head_t xcmd_wait; /* Wait queue for commands */
105 static const u32 slcan_bitrate_const[] = {
106 10000, 20000, 50000, 100000, 125000,
107 250000, 500000, 800000, 1000000
110 bool slcan_err_rst_on_open(struct net_device *ndev)
112 struct slcan *sl = netdev_priv(ndev);
114 return !!test_bit(CF_ERR_RST, &sl->cmd_flags);
117 int slcan_enable_err_rst_on_open(struct net_device *ndev, bool on)
119 struct slcan *sl = netdev_priv(ndev);
121 if (netif_running(ndev))
125 set_bit(CF_ERR_RST, &sl->cmd_flags);
127 clear_bit(CF_ERR_RST, &sl->cmd_flags);
132 /*************************************************************************
133 * SLCAN ENCAPSULATION FORMAT *
134 *************************************************************************/
136 /* A CAN frame has a can_id (11 bit standard frame format OR 29 bit extended
137 * frame format) a data length code (len) which can be from 0 to 8
138 * and up to <len> data bytes as payload.
139 * Additionally a CAN frame may become a remote transmission frame if the
140 * RTR-bit is set. This causes another ECU to send a CAN frame with the
143 * The SLCAN ASCII representation of these different frame types is:
144 * <type> <id> <dlc> <data>*
146 * Extended frames (29 bit) are defined by capital characters in the type.
147 * RTR frames are defined as 'r' types - normal frames have 't' type:
148 * t => 11 bit data frame
149 * r => 11 bit RTR frame
150 * T => 29 bit data frame
151 * R => 29 bit RTR frame
153 * The <id> is 3 (standard) or 8 (extended) bytes in ASCII Hex (base64).
154 * The <dlc> is a one byte ASCII number ('0' - '8')
155 * The <data> section has at much ASCII Hex bytes as defined by the <dlc>
159 * t1230 : can_id 0x123, len 0, no data
160 * t4563112233 : can_id 0x456, len 3, data 0x11 0x22 0x33
161 * T12ABCDEF2AA55 : extended can_id 0x12ABCDEF, len 2, data 0xAA 0x55
162 * r1230 : can_id 0x123, len 0, no data, remote transmission request
166 /*************************************************************************
167 * STANDARD SLCAN DECAPSULATION *
168 *************************************************************************/
170 /* Send one completely decapsulated can_frame to the network layer */
171 static void slcan_bump_frame(struct slcan *sl)
174 struct can_frame *cf;
177 char *cmd = sl->rbuff;
179 skb = alloc_can_skb(sl->dev, &cf);
180 if (unlikely(!skb)) {
181 sl->dev->stats.rx_dropped++;
187 cf->can_id = CAN_RTR_FLAG;
190 /* store dlc ASCII value and terminate SFF CAN ID string */
191 cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN];
192 sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN] = 0;
193 /* point to payload data behind the dlc */
194 cmd += SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN + 1;
197 cf->can_id = CAN_RTR_FLAG;
200 cf->can_id |= CAN_EFF_FLAG;
201 /* store dlc ASCII value and terminate EFF CAN ID string */
202 cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN];
203 sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN] = 0;
204 /* point to payload data behind the dlc */
205 cmd += SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN + 1;
211 if (kstrtou32(sl->rbuff + SLCAN_CMD_LEN, 16, &tmpid))
216 /* get len from sanitized ASCII value */
217 if (cf->len >= '0' && cf->len < '9')
222 /* RTR frames may have a dlc > 0 but they never have any data bytes */
223 if (!(cf->can_id & CAN_RTR_FLAG)) {
224 for (i = 0; i < cf->len; i++) {
225 tmp = hex_to_bin(*cmd++);
229 cf->data[i] = (tmp << 4);
230 tmp = hex_to_bin(*cmd++);
238 sl->dev->stats.rx_packets++;
239 if (!(cf->can_id & CAN_RTR_FLAG))
240 sl->dev->stats.rx_bytes += cf->len;
246 sl->dev->stats.rx_errors++;
250 /* A change state frame must contain state info and receive and transmit
255 * sb256256 : state bus-off: rx counter 256, tx counter 256
256 * sa057033 : state active, rx counter 57, tx counter 33
258 static void slcan_bump_state(struct slcan *sl)
260 struct net_device *dev = sl->dev;
262 struct can_frame *cf;
263 char *cmd = sl->rbuff;
265 enum can_state state, rx_state, tx_state;
269 state = CAN_STATE_ERROR_ACTIVE;
272 state = CAN_STATE_ERROR_WARNING;
275 state = CAN_STATE_ERROR_PASSIVE;
278 state = CAN_STATE_BUS_OFF;
284 if (state == sl->can.state || sl->rcount < SLCAN_STATE_FRAME_LEN)
287 cmd += SLCAN_STATE_BE_RXCNT_LEN + SLCAN_CMD_LEN + 1;
288 cmd[SLCAN_STATE_BE_TXCNT_LEN] = 0;
289 if (kstrtou32(cmd, 10, &txerr))
293 cmd -= SLCAN_STATE_BE_RXCNT_LEN;
294 if (kstrtou32(cmd, 10, &rxerr))
297 skb = alloc_can_err_skb(dev, &cf);
299 tx_state = txerr >= rxerr ? state : 0;
300 rx_state = txerr <= rxerr ? state : 0;
301 can_change_state(dev, cf, tx_state, rx_state);
303 if (state == CAN_STATE_BUS_OFF) {
306 cf->can_id |= CAN_ERR_CNT;
315 /* An error frame can contain more than one type of error.
319 * e1a : len 1, errors: ACK error
320 * e3bcO: len 3, errors: Bit0 error, CRC error, Tx overrun error
322 static void slcan_bump_err(struct slcan *sl)
324 struct net_device *dev = sl->dev;
326 struct can_frame *cf;
327 char *cmd = sl->rbuff;
328 bool rx_errors = false, tx_errors = false, rx_over_errors = false;
331 /* get len from sanitized ASCII value */
333 if (len >= '0' && len < '9')
338 if ((len + SLCAN_CMD_LEN + 1) > sl->rcount)
341 skb = alloc_can_err_skb(dev, &cf);
344 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
346 cmd += SLCAN_CMD_LEN + 1;
347 for (i = 0; i < len; i++, cmd++) {
350 netdev_dbg(dev, "ACK error\n");
353 cf->can_id |= CAN_ERR_ACK;
354 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
359 netdev_dbg(dev, "Bit0 error\n");
362 cf->data[2] |= CAN_ERR_PROT_BIT0;
366 netdev_dbg(dev, "Bit1 error\n");
369 cf->data[2] |= CAN_ERR_PROT_BIT1;
373 netdev_dbg(dev, "CRC error\n");
376 cf->data[2] |= CAN_ERR_PROT_BIT;
377 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
382 netdev_dbg(dev, "Form Error\n");
385 cf->data[2] |= CAN_ERR_PROT_FORM;
389 netdev_dbg(dev, "Rx overrun error\n");
390 rx_over_errors = true;
393 cf->can_id |= CAN_ERR_CRTL;
394 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
399 netdev_dbg(dev, "Tx overrun error\n");
402 cf->can_id |= CAN_ERR_CRTL;
403 cf->data[1] = CAN_ERR_CRTL_TX_OVERFLOW;
408 netdev_dbg(dev, "Stuff error\n");
411 cf->data[2] |= CAN_ERR_PROT_STUFF;
423 dev->stats.rx_errors++;
426 dev->stats.rx_over_errors++;
429 dev->stats.tx_errors++;
435 static void slcan_bump(struct slcan *sl)
437 switch (sl->rbuff[0]) {
445 return slcan_bump_frame(sl);
447 return slcan_bump_err(sl);
449 return slcan_bump_state(sl);
455 /* parse tty input stream */
456 static void slcan_unesc(struct slcan *sl, unsigned char s)
458 if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
459 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
465 if (!test_bit(SLF_ERROR, &sl->flags)) {
466 if (sl->rcount < SLCAN_MTU) {
467 sl->rbuff[sl->rcount++] = s;
471 sl->dev->stats.rx_over_errors++;
472 set_bit(SLF_ERROR, &sl->flags);
477 /*************************************************************************
478 * STANDARD SLCAN ENCAPSULATION *
479 *************************************************************************/
481 /* Encapsulate one can_frame and stuff into a TTY queue. */
482 static void slcan_encaps(struct slcan *sl, struct can_frame *cf)
486 unsigned char *endpos;
487 canid_t id = cf->can_id;
491 if (cf->can_id & CAN_RTR_FLAG)
492 *pos = 'R'; /* becomes 'r' in standard frame format (SFF) */
494 *pos = 'T'; /* becomes 't' in standard frame format (SSF) */
496 /* determine number of chars for the CAN-identifier */
497 if (cf->can_id & CAN_EFF_FLAG) {
499 endpos = pos + SLCAN_EFF_ID_LEN;
501 *pos |= 0x20; /* convert R/T to lower case for SFF */
503 endpos = pos + SLCAN_SFF_ID_LEN;
506 /* build 3 (SFF) or 8 (EFF) digit CAN identifier */
508 while (endpos >= pos) {
509 *endpos-- = hex_asc_upper[id & 0xf];
513 pos += (cf->can_id & CAN_EFF_FLAG) ?
514 SLCAN_EFF_ID_LEN : SLCAN_SFF_ID_LEN;
516 *pos++ = cf->len + '0';
518 /* RTR frames may have a dlc > 0 but they never have any data bytes */
519 if (!(cf->can_id & CAN_RTR_FLAG)) {
520 for (i = 0; i < cf->len; i++)
521 pos = hex_byte_pack_upper(pos, cf->data[i]);
523 sl->dev->stats.tx_bytes += cf->len;
528 /* Order of next two lines is *very* important.
529 * When we are sending a little amount of data,
530 * the transfer may be completed inside the ops->write()
531 * routine, because it's running with interrupts enabled.
532 * In this case we *never* got WRITE_WAKEUP event,
533 * if we did not request it before write operation.
534 * 14 Oct 1994 Dmitry Gorodchanin.
536 set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
537 actual = sl->tty->ops->write(sl->tty, sl->xbuff, pos - sl->xbuff);
538 sl->xleft = (pos - sl->xbuff) - actual;
539 sl->xhead = sl->xbuff + actual;
542 /* Write out any remaining transmit buffer. Scheduled when tty is writable */
543 static void slcan_transmit(struct work_struct *work)
545 struct slcan *sl = container_of(work, struct slcan, tx_work);
548 spin_lock_bh(&sl->lock);
549 /* First make sure we're connected. */
550 if (unlikely(!netif_running(sl->dev)) &&
551 likely(!test_bit(SLF_XCMD, &sl->flags))) {
552 spin_unlock_bh(&sl->lock);
556 if (sl->xleft <= 0) {
557 if (unlikely(test_bit(SLF_XCMD, &sl->flags))) {
558 clear_bit(SLF_XCMD, &sl->flags);
559 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
560 spin_unlock_bh(&sl->lock);
561 wake_up(&sl->xcmd_wait);
565 /* Now serial buffer is almost free & we can start
566 * transmission of another packet
568 sl->dev->stats.tx_packets++;
569 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
570 spin_unlock_bh(&sl->lock);
571 netif_wake_queue(sl->dev);
575 actual = sl->tty->ops->write(sl->tty, sl->xhead, sl->xleft);
578 spin_unlock_bh(&sl->lock);
581 /* Called by the driver when there's room for more data.
582 * Schedule the transmit.
584 static void slcan_write_wakeup(struct tty_struct *tty)
586 struct slcan *sl = tty->disc_data;
588 schedule_work(&sl->tx_work);
591 /* Send a can_frame to a TTY queue. */
592 static netdev_tx_t slcan_netdev_xmit(struct sk_buff *skb,
593 struct net_device *dev)
595 struct slcan *sl = netdev_priv(dev);
597 if (can_dev_dropped_skb(dev, skb))
600 spin_lock(&sl->lock);
601 if (!netif_running(dev)) {
602 spin_unlock(&sl->lock);
603 netdev_warn(dev, "xmit: iface is down\n");
607 spin_unlock(&sl->lock);
611 netif_stop_queue(sl->dev);
612 slcan_encaps(sl, (struct can_frame *)skb->data); /* encaps & send */
613 spin_unlock(&sl->lock);
615 skb_tx_timestamp(skb);
622 /******************************************
623 * Routines looking at netdevice side.
624 ******************************************/
626 static int slcan_transmit_cmd(struct slcan *sl, const unsigned char *cmd)
630 spin_lock(&sl->lock);
632 spin_unlock(&sl->lock);
636 n = scnprintf(sl->xbuff, sizeof(sl->xbuff), "%s", cmd);
637 set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
638 actual = sl->tty->ops->write(sl->tty, sl->xbuff, n);
639 sl->xleft = n - actual;
640 sl->xhead = sl->xbuff + actual;
641 set_bit(SLF_XCMD, &sl->flags);
642 spin_unlock(&sl->lock);
643 ret = wait_event_interruptible_timeout(sl->xcmd_wait,
644 !test_bit(SLF_XCMD, &sl->flags),
646 clear_bit(SLF_XCMD, &sl->flags);
647 if (ret == -ERESTARTSYS)
656 /* Netdevice UP -> DOWN routine */
657 static int slcan_netdev_close(struct net_device *dev)
659 struct slcan *sl = netdev_priv(dev);
662 if (sl->can.bittiming.bitrate &&
663 sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
664 err = slcan_transmit_cmd(sl, "C\r");
667 "failed to send close command 'C\\r'\n");
670 /* TTY discipline is running. */
671 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
672 flush_work(&sl->tx_work);
674 netif_stop_queue(dev);
678 sl->can.state = CAN_STATE_STOPPED;
679 if (sl->can.bittiming.bitrate == CAN_BITRATE_UNKNOWN)
680 sl->can.bittiming.bitrate = CAN_BITRATE_UNSET;
685 /* Netdevice DOWN -> UP routine */
686 static int slcan_netdev_open(struct net_device *dev)
688 struct slcan *sl = netdev_priv(dev);
689 unsigned char cmd[SLCAN_MTU];
692 /* The baud rate is not set with the command
693 * `ip link set <iface> type can bitrate <baud>' and therefore
694 * can.bittiming.bitrate is CAN_BITRATE_UNSET (0), causing
695 * open_candev() to fail. So let's set to a fake value.
697 if (sl->can.bittiming.bitrate == CAN_BITRATE_UNSET)
698 sl->can.bittiming.bitrate = CAN_BITRATE_UNKNOWN;
700 err = open_candev(dev);
702 netdev_err(dev, "failed to open can device\n");
706 if (sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
707 for (s = 0; s < ARRAY_SIZE(slcan_bitrate_const); s++) {
708 if (sl->can.bittiming.bitrate == slcan_bitrate_const[s])
712 /* The CAN framework has already validate the bitrate value,
713 * so we can avoid to check if `s' has been properly set.
715 snprintf(cmd, sizeof(cmd), "C\rS%d\r", s);
716 err = slcan_transmit_cmd(sl, cmd);
719 "failed to send bitrate command 'C\\rS%d\\r'\n",
721 goto cmd_transmit_failed;
724 if (test_bit(CF_ERR_RST, &sl->cmd_flags)) {
725 err = slcan_transmit_cmd(sl, "F\r");
728 "failed to send error command 'F\\r'\n");
729 goto cmd_transmit_failed;
733 if (sl->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
734 err = slcan_transmit_cmd(sl, "L\r");
737 "failed to send listen-only command 'L\\r'\n");
738 goto cmd_transmit_failed;
741 err = slcan_transmit_cmd(sl, "O\r");
744 "failed to send open command 'O\\r'\n");
745 goto cmd_transmit_failed;
750 sl->can.state = CAN_STATE_ERROR_ACTIVE;
751 netif_start_queue(dev);
759 static const struct net_device_ops slcan_netdev_ops = {
760 .ndo_open = slcan_netdev_open,
761 .ndo_stop = slcan_netdev_close,
762 .ndo_start_xmit = slcan_netdev_xmit,
763 .ndo_change_mtu = can_change_mtu,
766 /******************************************
767 * Routines looking at TTY side.
768 ******************************************/
770 /* Handle the 'receiver data ready' interrupt.
771 * This function is called by the 'tty_io' module in the kernel when
772 * a block of SLCAN data has been received, which can now be decapsulated
773 * and sent on to some IP layer for further processing. This will not
774 * be re-entered while running but other ldisc functions may be called
777 static void slcan_receive_buf(struct tty_struct *tty, const u8 *cp,
778 const u8 *fp, size_t count)
780 struct slcan *sl = tty->disc_data;
782 if (!netif_running(sl->dev))
785 /* Read the characters out of the buffer */
788 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
789 sl->dev->stats.rx_errors++;
793 slcan_unesc(sl, *cp++);
797 /* Open the high-level part of the SLCAN channel.
798 * This function is called by the TTY module when the
799 * SLCAN line discipline is called for.
801 * Called in process context serialized from other ldisc calls.
803 static int slcan_open(struct tty_struct *tty)
805 struct net_device *dev;
809 if (!capable(CAP_NET_ADMIN))
812 if (!tty->ops->write)
815 dev = alloc_candev(sizeof(*sl), 1);
819 sl = netdev_priv(dev);
821 /* Configure TTY interface */
822 tty->receive_room = 65536; /* We don't flow control */
825 spin_lock_init(&sl->lock);
826 INIT_WORK(&sl->tx_work, slcan_transmit);
827 init_waitqueue_head(&sl->xcmd_wait);
829 /* Configure CAN metadata */
830 sl->can.bitrate_const = slcan_bitrate_const;
831 sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);
832 sl->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY;
834 /* Configure netdev interface */
836 dev->netdev_ops = &slcan_netdev_ops;
837 dev->ethtool_ops = &slcan_ethtool_ops;
839 /* Mark ldisc channel as alive */
843 err = register_candev(dev);
846 pr_err("can't register candev\n");
850 netdev_info(dev, "slcan on %s.\n", tty->name);
851 /* TTY layer expects 0 on success */
855 /* Close down a SLCAN channel.
856 * This means flushing out any pending queues, and then returning. This
857 * call is serialized against other ldisc functions.
858 * Once this is called, no other ldisc function of ours is entered.
860 * We also use this method for a hangup event.
862 static void slcan_close(struct tty_struct *tty)
864 struct slcan *sl = tty->disc_data;
866 unregister_candev(sl->dev);
869 * The netdev needn't be UP (so .ndo_stop() is not called). Hence make
870 * sure this is not running before freeing it up.
872 flush_work(&sl->tx_work);
874 /* Mark channel as dead */
875 spin_lock_bh(&sl->lock);
876 tty->disc_data = NULL;
878 spin_unlock_bh(&sl->lock);
880 netdev_info(sl->dev, "slcan off %s.\n", tty->name);
881 free_candev(sl->dev);
884 /* Perform I/O control on an active SLCAN channel. */
885 static int slcan_ioctl(struct tty_struct *tty, unsigned int cmd,
888 struct slcan *sl = tty->disc_data;
893 tmp = strlen(sl->dev->name) + 1;
894 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
902 return tty_mode_ioctl(tty, cmd, arg);
906 static struct tty_ldisc_ops slcan_ldisc = {
907 .owner = THIS_MODULE,
909 .name = KBUILD_MODNAME,
911 .close = slcan_close,
912 .ioctl = slcan_ioctl,
913 .receive_buf = slcan_receive_buf,
914 .write_wakeup = slcan_write_wakeup,
917 static int __init slcan_init(void)
921 pr_info("serial line CAN interface driver\n");
923 /* Fill in our line protocol discipline, and register it */
924 status = tty_register_ldisc(&slcan_ldisc);
926 pr_err("can't register line discipline\n");
931 static void __exit slcan_exit(void)
933 /* This will only be called when all channels have been closed by
934 * userspace - tty_ldisc.c takes care of the module's refcount.
936 tty_unregister_ldisc(&slcan_ldisc);
939 module_init(slcan_init);
940 module_exit(slcan_exit);