^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * slcan.c - serial line CAN interface driver (using tty line discipline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * This file is derived from linux/drivers/net/slip/slip.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * slcan.c Author : Oliver Hartkopp <socketcan@hartkopp.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * This program is free software; you can redistribute it and/or modify it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * under the terms of the GNU General Public License as published by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Free Software Foundation; either version 2 of the License, or (at your
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * This program is distributed in the hope that it will be useful, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * You should have received a copy of the GNU General Public License along
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * with this program; if not, see http://www.gnu.org/licenses/gpl.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <linux/can.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <linux/can/skb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <linux/can/can-ml.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) MODULE_ALIAS_LDISC(N_SLCAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) MODULE_DESCRIPTION("serial line CAN interface");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define SLCAN_MAGIC 0x53CA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int maxdev = 10; /* MAX number of SLCAN channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) This can be overridden with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) insmod slcan.ko maxdev=nnn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) module_param(maxdev, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* maximum rx buffer len: extended CAN frame with timestamp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define SLC_CMD_LEN 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define SLC_SFF_ID_LEN 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define SLC_EFF_ID_LEN 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct slcan {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* Various fields. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct tty_struct *tty; /* ptr to TTY structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct net_device *dev; /* easy for intr handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct work_struct tx_work; /* Flushes transmit buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* These are pointers to the malloc()ed frame buffers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) unsigned char rbuff[SLC_MTU]; /* receiver buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int rcount; /* received chars counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) unsigned char xbuff[SLC_MTU]; /* transmitter buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unsigned char *xhead; /* pointer to next XMIT byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int xleft; /* bytes left in XMIT queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) unsigned long flags; /* Flag values/ mode etc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #define SLF_INUSE 0 /* Channel in use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #define SLF_ERROR 1 /* Parity, etc. error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static struct net_device **slcan_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * SLCAN ENCAPSULATION FORMAT *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * A CAN frame has a can_id (11 bit standard frame format OR 29 bit extended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * frame format) a data length code (can_dlc) which can be from 0 to 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * and up to <can_dlc> data bytes as payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * Additionally a CAN frame may become a remote transmission frame if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * RTR-bit is set. This causes another ECU to send a CAN frame with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * given can_id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * The SLCAN ASCII representation of these different frame types is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * <type> <id> <dlc> <data>*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * Extended frames (29 bit) are defined by capital characters in the type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * RTR frames are defined as 'r' types - normal frames have 't' type:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * t => 11 bit data frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * r => 11 bit RTR frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * T => 29 bit data frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * R => 29 bit RTR frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * The <id> is 3 (standard) or 8 (extended) bytes in ASCII Hex (base64).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * The <dlc> is a one byte ASCII number ('0' - '8')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * The <data> section has at much ASCII Hex bytes as defined by the <dlc>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * Examples:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * t1230 : can_id 0x123, can_dlc 0, no data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * t4563112233 : can_id 0x456, can_dlc 3, data 0x11 0x22 0x33
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * T12ABCDEF2AA55 : extended can_id 0x12ABCDEF, can_dlc 2, data 0xAA 0x55
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * r1230 : can_id 0x123, can_dlc 0, no data, remote transmission request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * STANDARD SLCAN DECAPSULATION *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) ************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /* Send one completely decapsulated can_frame to the network layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static void slc_bump(struct slcan *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct can_frame cf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int i, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) u32 tmpid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) char *cmd = sl->rbuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) memset(&cf, 0, sizeof(cf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) switch (*cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) case 'r':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) cf.can_id = CAN_RTR_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) case 't':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /* store dlc ASCII value and terminate SFF CAN ID string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) cf.can_dlc = sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* point to payload data behind the dlc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) cmd += SLC_CMD_LEN + SLC_SFF_ID_LEN + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) case 'R':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) cf.can_id = CAN_RTR_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) case 'T':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) cf.can_id |= CAN_EFF_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* store dlc ASCII value and terminate EFF CAN ID string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) cf.can_dlc = sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* point to payload data behind the dlc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) cmd += SLC_CMD_LEN + SLC_EFF_ID_LEN + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (kstrtou32(sl->rbuff + SLC_CMD_LEN, 16, &tmpid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) cf.can_id |= tmpid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /* get can_dlc from sanitized ASCII value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (cf.can_dlc >= '0' && cf.can_dlc < '9')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) cf.can_dlc -= '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /* RTR frames may have a dlc > 0 but they never have any data bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!(cf.can_id & CAN_RTR_FLAG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) for (i = 0; i < cf.can_dlc; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) tmp = hex_to_bin(*cmd++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (tmp < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) cf.data[i] = (tmp << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) tmp = hex_to_bin(*cmd++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (tmp < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) cf.data[i] |= tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) skb = dev_alloc_skb(sizeof(struct can_frame) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) sizeof(struct can_skb_priv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) skb->dev = sl->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) skb->protocol = htons(ETH_P_CAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) skb->pkt_type = PACKET_BROADCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) skb->ip_summed = CHECKSUM_UNNECESSARY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) can_skb_reserve(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) can_skb_prv(skb)->ifindex = sl->dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) can_skb_prv(skb)->skbcnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) skb_put_data(skb, &cf, sizeof(struct can_frame));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) sl->dev->stats.rx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) sl->dev->stats.rx_bytes += cf.can_dlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) netif_rx_ni(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* parse tty input stream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static void slcan_unesc(struct slcan *sl, unsigned char s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) (sl->rcount > 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) slc_bump(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) sl->rcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (!test_bit(SLF_ERROR, &sl->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (sl->rcount < SLC_MTU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) sl->rbuff[sl->rcount++] = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) sl->dev->stats.rx_over_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) set_bit(SLF_ERROR, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * STANDARD SLCAN ENCAPSULATION *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /* Encapsulate one can_frame and stuff into a TTY queue. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static void slc_encaps(struct slcan *sl, struct can_frame *cf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int actual, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) unsigned char *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) unsigned char *endpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) canid_t id = cf->can_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) pos = sl->xbuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (cf->can_id & CAN_RTR_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) *pos = 'R'; /* becomes 'r' in standard frame format (SFF) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) *pos = 'T'; /* becomes 't' in standard frame format (SSF) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) /* determine number of chars for the CAN-identifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (cf->can_id & CAN_EFF_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) id &= CAN_EFF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) endpos = pos + SLC_EFF_ID_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) *pos |= 0x20; /* convert R/T to lower case for SFF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) id &= CAN_SFF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) endpos = pos + SLC_SFF_ID_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /* build 3 (SFF) or 8 (EFF) digit CAN identifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) while (endpos >= pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) *endpos-- = hex_asc_upper[id & 0xf];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) id >>= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) pos += (cf->can_id & CAN_EFF_FLAG) ? SLC_EFF_ID_LEN : SLC_SFF_ID_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) *pos++ = cf->can_dlc + '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /* RTR frames may have a dlc > 0 but they never have any data bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (!(cf->can_id & CAN_RTR_FLAG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) for (i = 0; i < cf->can_dlc; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) pos = hex_byte_pack_upper(pos, cf->data[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) *pos++ = '\r';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) /* Order of next two lines is *very* important.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * When we are sending a little amount of data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * the transfer may be completed inside the ops->write()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * routine, because it's running with interrupts enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * In this case we *never* got WRITE_WAKEUP event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * if we did not request it before write operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * 14 Oct 1994 Dmitry Gorodchanin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) actual = sl->tty->ops->write(sl->tty, sl->xbuff, pos - sl->xbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) sl->xleft = (pos - sl->xbuff) - actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) sl->xhead = sl->xbuff + actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) sl->dev->stats.tx_bytes += cf->can_dlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /* Write out any remaining transmit buffer. Scheduled when tty is writable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static void slcan_transmit(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct slcan *sl = container_of(work, struct slcan, tx_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) spin_lock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /* First make sure we're connected. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (!sl->tty || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) spin_unlock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (sl->xleft <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /* Now serial buffer is almost free & we can start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * transmission of another packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) sl->dev->stats.tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) spin_unlock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) netif_wake_queue(sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) actual = sl->tty->ops->write(sl->tty, sl->xhead, sl->xleft);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) sl->xleft -= actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) sl->xhead += actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) spin_unlock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * Called by the driver when there's room for more data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * Schedule the transmit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static void slcan_write_wakeup(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) struct slcan *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) sl = rcu_dereference(tty->disc_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) schedule_work(&sl->tx_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) /* Send a can_frame to a TTY queue. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) struct slcan *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (skb->len != CAN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) spin_lock(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (!netif_running(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) spin_unlock(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (sl->tty == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) spin_unlock(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) netif_stop_queue(sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) slc_encaps(sl, (struct can_frame *) skb->data); /* encaps & send */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) spin_unlock(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) /******************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * Routines looking at netdevice side.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) ******************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /* Netdevice UP -> DOWN routine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static int slc_close(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) struct slcan *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) spin_lock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (sl->tty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /* TTY discipline is running. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) netif_stop_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) sl->rcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) sl->xleft = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) spin_unlock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) /* Netdevice DOWN -> UP routine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static int slc_open(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct slcan *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (sl->tty == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) sl->flags &= (1 << SLF_INUSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) netif_start_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) /* Hook the destructor so we can free slcan devs at the right point in time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static void slc_free_netdev(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) int i = dev->base_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) slcan_devs[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static int slcan_change_mtu(struct net_device *dev, int new_mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) static const struct net_device_ops slc_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) .ndo_open = slc_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) .ndo_stop = slc_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) .ndo_start_xmit = slc_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) .ndo_change_mtu = slcan_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) static void slc_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) dev->netdev_ops = &slc_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) dev->needs_free_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) dev->priv_destructor = slc_free_netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) dev->hard_header_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) dev->addr_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) dev->tx_queue_len = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) dev->mtu = CAN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) dev->type = ARPHRD_CAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) /* New-style flags. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) dev->flags = IFF_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) dev->features = NETIF_F_HW_CSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /******************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) Routines looking at TTY side.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) ******************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * Handle the 'receiver data ready' interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * This function is called by the 'tty_io' module in the kernel when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * a block of SLCAN data has been received, which can now be decapsulated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * and sent on to some IP layer for further processing. This will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * be re-entered while running but other ldisc functions may be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * in parallel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) static void slcan_receive_buf(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) const unsigned char *cp, char *fp, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct slcan *sl = (struct slcan *) tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) /* Read the characters out of the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) while (count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (fp && *fp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (!test_and_set_bit(SLF_ERROR, &sl->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) sl->dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) cp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) slcan_unesc(sl, *cp++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * slcan_open helper routines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) ************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) /* Collect hanged up channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static void slc_sync(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) struct slcan *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) for (i = 0; i < maxdev; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) dev = slcan_devs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (sl->tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (dev->flags & IFF_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) dev_close(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) /* Find a free SLCAN channel, and link in this `tty' line. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static struct slcan *slc_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) char name[IFNAMSIZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) struct net_device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) struct can_ml_priv *can_ml;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct slcan *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) for (i = 0; i < maxdev; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) dev = slcan_devs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) /* Sorry, too many, all slots in use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (i >= maxdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) sprintf(name, "slcan%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) size = ALIGN(sizeof(*sl), NETDEV_ALIGN) + sizeof(struct can_ml_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) dev = alloc_netdev(size, name, NET_NAME_UNKNOWN, slc_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) dev->base_addr = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) can_ml = (void *)sl + ALIGN(sizeof(*sl), NETDEV_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) can_set_ml_priv(dev, can_ml);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) /* Initialize channel control data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) sl->magic = SLCAN_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) sl->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) spin_lock_init(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) INIT_WORK(&sl->tx_work, slcan_transmit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) slcan_devs[i] = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * Open the high-level part of the SLCAN channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * This function is called by the TTY module when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * SLCAN line discipline is called for. Because we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * sure the tty line exists, we only have to link it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * a free SLCAN channel...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * Called in process context serialized from other ldisc calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) static int slcan_open(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) struct slcan *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (!capable(CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (tty->ops->write == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) /* RTnetlink lock is misused here to serialize concurrent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) opens of slcan channels. There are better ways, but it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) the simplest one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) /* Collect hanged up channels. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) slc_sync();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) sl = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) /* First make sure we're not already connected. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (sl && sl->magic == SLCAN_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) goto err_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) /* OK. Find a free SLCAN channel to use. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) err = -ENFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) sl = slc_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (sl == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) goto err_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) sl->tty = tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) tty->disc_data = sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (!test_bit(SLF_INUSE, &sl->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) /* Perform the low-level SLCAN initialization. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) sl->rcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) sl->xleft = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) set_bit(SLF_INUSE, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) err = register_netdevice(sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) goto err_free_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) /* Done. We have linked the TTY line to a channel. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) tty->receive_room = 65536; /* We don't flow control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) /* TTY layer expects 0 on success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) err_free_chan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) sl->tty = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) tty->disc_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) clear_bit(SLF_INUSE, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) slc_free_netdev(sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) /* do not call free_netdev before rtnl_unlock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) free_netdev(sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) err_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) /* Count references from TTY module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * Close down a SLCAN channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) * This means flushing out any pending queues, and then returning. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) * call is serialized against other ldisc functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) * We also use this method for a hangup event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) static void slcan_close(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) struct slcan *sl = (struct slcan *) tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) /* First make sure we're connected. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (!sl || sl->magic != SLCAN_MAGIC || sl->tty != tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) spin_lock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) rcu_assign_pointer(tty->disc_data, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) sl->tty = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) spin_unlock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) flush_work(&sl->tx_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) /* Flush network side */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) unregister_netdev(sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) /* This will complete via sl_free_netdev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) static int slcan_hangup(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) slcan_close(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) /* Perform I/O control on an active SLCAN channel. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) static int slcan_ioctl(struct tty_struct *tty, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) struct slcan *sl = (struct slcan *) tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) unsigned int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) /* First make sure we're connected. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) if (!sl || sl->magic != SLCAN_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) case SIOCGIFNAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) tmp = strlen(sl->dev->name) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) case SIOCSIFHWADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) return tty_mode_ioctl(tty, file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) static struct tty_ldisc_ops slc_ldisc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) .magic = TTY_LDISC_MAGIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) .name = "slcan",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) .open = slcan_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) .close = slcan_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) .hangup = slcan_hangup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) .ioctl = slcan_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) .receive_buf = slcan_receive_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) .write_wakeup = slcan_write_wakeup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) static int __init slcan_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (maxdev < 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) maxdev = 4; /* Sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) pr_info("slcan: serial line CAN interface driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) pr_info("slcan: %d dynamic interface channels.\n", maxdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) slcan_devs = kcalloc(maxdev, sizeof(struct net_device *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) if (!slcan_devs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) /* Fill in our line protocol discipline, and register it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) status = tty_register_ldisc(N_SLCAN, &slc_ldisc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) printk(KERN_ERR "slcan: can't register line discipline\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) kfree(slcan_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) static void __exit slcan_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) struct slcan *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) unsigned long timeout = jiffies + HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) int busy = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) if (slcan_devs == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) /* First of all: check for active disciplines and hangup them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (busy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) msleep_interruptible(100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) busy = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) for (i = 0; i < maxdev; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) dev = slcan_devs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) spin_lock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) if (sl->tty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) busy++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) tty_hangup(sl->tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) spin_unlock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) } while (busy && time_before(jiffies, timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) /* FIXME: hangup is async so we should wait when doing this second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) phase */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) for (i = 0; i < maxdev; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) dev = slcan_devs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) slcan_devs[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) if (sl->tty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) printk(KERN_ERR "%s: tty discipline still running\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) unregister_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) kfree(slcan_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) slcan_devs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) i = tty_unregister_ldisc(N_SLCAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) printk(KERN_ERR "slcan: can't unregister ldisc (err %d)\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) module_init(slcan_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) module_exit(slcan_exit);