2 * sllin.c - serial line LIN interface driver (using tty line discipline)
4 * This file is derived from linux/drivers/net/slip.c
6 * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk>
7 * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
8 * sllin.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, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307. You can also get it
23 * at http://www.gnu.org/licenses/gpl.html
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38 * Send feedback to <socketcan-users@lists.berlios.de>
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
45 #include <asm/system.h>
46 #include <linux/uaccess.h>
47 #include <linux/bitops.h>
48 #include <linux/string.h>
49 #include <linux/tty.h>
50 #include <linux/errno.h>
51 #include <linux/netdevice.h>
52 #include <linux/skbuff.h>
53 #include <linux/rtnetlink.h>
54 #include <linux/if_arp.h>
55 #include <linux/if_ether.h>
56 #include <linux/sched.h>
57 #include <linux/delay.h>
58 #include <linux/init.h>
59 #include <linux/can.h>
61 /* Should be in include/linux/tty.h */
64 static __initdata const char banner[] =
65 KERN_INFO "sllin: serial line LIN interface driver\n";
67 MODULE_ALIAS_LDISC(N_SLLIN);
68 MODULE_DESCRIPTION("serial line LIN interface");
69 MODULE_LICENSE("GPL");
70 MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
72 #define SLLIN_MAGIC 0x53CA
74 static int maxdev = 10; /* MAX number of SLLIN channels;
75 This can be overridden with
76 insmod sllin.ko maxdev=nnn */
77 module_param(maxdev, int, 0);
78 MODULE_PARM_DESC(maxdev, "Maximum number of sllin interfaces");
80 /* maximum rx buffer len: extended CAN frame with timestamp */
81 #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
87 struct tty_struct *tty; /* ptr to TTY structure */
88 struct net_device *dev; /* easy for intr handling */
91 /* These are pointers to the malloc()ed frame buffers. */
92 unsigned char rbuff[SLC_MTU]; /* receiver buffer */
93 int rcount; /* received chars counter */
94 unsigned char xbuff[SLC_MTU]; /* transmitter buffer */
95 unsigned char *xhead; /* pointer to next XMIT byte */
96 int xleft; /* bytes left in XMIT queue */
98 unsigned long flags; /* Flag values/ mode etc */
99 #define SLF_INUSE 0 /* Channel in use */
100 #define SLF_ERROR 1 /* Parity, etc. error */
102 unsigned char leased;
107 static struct net_device **sllin_devs;
110 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
112 struct ktermios old_termios;
115 mutex_lock(&tty->termios_mutex);
116 old_termios = *(tty->termios);
117 cflag = tty->termios->c_cflag;
118 tty_encode_baud_rate(tty, speed, speed);
119 if (tty->ops->set_termios)
120 tty->ops->set_termios(tty, &old_termios);
121 //priv->io.speed = speed;
122 mutex_unlock(&tty->termios_mutex);
128 /* Send one completely decapsulated can_frame to the network layer */
129 static void sll_bump(struct sllin *sl)
131 // struct sk_buff *skb;
132 // struct can_frame cf;
133 // int i, dlc_pos, tmp;
134 // unsigned long ultmp;
135 // char cmd = sl->rbuff[0];
137 // if ((cmd != 't') && (cmd != 'T') && (cmd != 'r') && (cmd != 'R'))
140 // if (cmd & 0x20) /* tiny chars 'r' 't' => standard frame format */
141 // dlc_pos = 4; /* dlc position tiiid */
143 // dlc_pos = 9; /* dlc position Tiiiiiiiid */
145 // if (!((sl->rbuff[dlc_pos] >= '0') && (sl->rbuff[dlc_pos] < '9')))
148 // cf.can_dlc = sl->rbuff[dlc_pos] - '0'; /* get can_dlc from ASCII val */
150 // sl->rbuff[dlc_pos] = 0; /* terminate can_id string */
152 // if (strict_strtoul(sl->rbuff+1, 16, &ultmp))
155 // cf.can_id = ultmp;
157 // if (!(cmd & 0x20)) /* NO tiny chars => extended frame format */
158 // cf.can_id |= CAN_EFF_FLAG;
160 // if ((cmd | 0x20) == 'r') /* RTR frame */
161 // cf.can_id |= CAN_RTR_FLAG;
163 // *(u64 *) (&cf.data) = 0; /* clear payload */
165 // for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
167 // tmp = asc2nibble(sl->rbuff[dlc_pos++]);
170 // cf.data[i] = (tmp << 4);
171 // tmp = asc2nibble(sl->rbuff[dlc_pos++]);
174 // cf.data[i] |= tmp;
178 // skb = dev_alloc_skb(sizeof(struct can_frame));
182 // skb->dev = sl->dev;
183 // skb->protocol = htons(ETH_P_CAN);
184 // skb->pkt_type = PACKET_BROADCAST;
185 // skb->ip_summed = CHECKSUM_UNNECESSARY;
186 // memcpy(skb_put(skb, sizeof(struct can_frame)),
187 // &cf, sizeof(struct can_frame));
190 // sl->dev->stats.rx_packets++;
191 // sl->dev->stats.rx_bytes += cf.can_dlc;
194 /* parse tty input stream */
195 static void sllin_unesc(struct sllin *sl, unsigned char s)
198 if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
199 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
205 if (!test_bit(SLF_ERROR, &sl->flags)) {
206 if (sl->rcount < SLC_MTU) {
207 sl->rbuff[sl->rcount++] = s;
210 sl->dev->stats.rx_over_errors++;
211 set_bit(SLF_ERROR, &sl->flags);
217 /************************************************************************
218 * STANDARD SLLIN ENCAPSULATION *
219 ************************************************************************/
221 /* Convert particular CAN frame into LIN frame and send it to TTY queue. */
222 static void sll_encaps(struct sllin *sl, struct can_frame *cf)
225 char lframe[16] = {0x00, 0x55}; /* Fake break, Sync byte */
226 struct tty_struct *tty = sl->tty;
228 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
230 /* We do care only about SFF frames */
231 if (cf->can_id & CAN_EFF_FLAG)
234 /* Send only header */
235 if (cf->can_id & CAN_RTR_FLAG) {
236 pr_debug("sllin: RTR CAN frame\n", __FUNCTION__);
237 lframe[2] = (u8)cf->can_id; /* Get one byte LIN ID */
239 sltty_change_speed(tty, 1200);
240 tty->ops->write(tty, &lframe[0], 1);
241 sltty_change_speed(tty, 2400);
242 tty->ops->write(tty, &lframe[1], 1);
243 tty->ops->write(tty, &lframe[2], 1);
245 pr_debug("sllin: non-RTR CAN frame\n", __FUNCTION__);
246 /* idx = strlen(sl->xbuff);
248 for (i = 0; i < cf->can_dlc; i++)
249 sprintf(&sl->xbuff[idx + 2*i], "%02X", cf->data[i]);
251 * Order of next two lines is *very* important.
252 * When we are sending a little amount of data,
253 * the transfer may be completed inside the ops->write()
254 * routine, because it's running with interrupts enabled.
255 * In this case we *never* got WRITE_WAKEUP event,
256 * if we did not request it before write operation.
257 * 14 Oct 1994 Dmitry Gorodchanin.
259 set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
260 actual = sl->tty->ops->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
261 sl->xleft = strlen(sl->xbuff) - actual;
262 sl->xhead = sl->xbuff + actual;
263 sl->dev->stats.tx_bytes += cf->can_dlc;
270 * Called by the driver when there's room for more data. If we have
271 * more packets to send, we send them here.
273 static void sllin_write_wakeup(struct tty_struct *tty)
276 struct sllin *sl = (struct sllin *) tty->disc_data;
278 /* First make sure we're connected. */
279 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
282 if (sl->xleft <= 0) {
283 /* Now serial buffer is almost free & we can start
284 * transmission of another packet */
285 sl->dev->stats.tx_packets++;
286 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
287 netif_wake_queue(sl->dev);
291 actual = tty->ops->write(tty, sl->xhead, sl->xleft);
296 /* Send a can_frame to a TTY queue. */
297 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
299 struct sllin *sl = netdev_priv(dev);
301 if (skb->len != sizeof(struct can_frame))
304 spin_lock(&sl->lock);
305 if (!netif_running(dev)) {
306 spin_unlock(&sl->lock);
307 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
310 if (sl->tty == NULL) {
311 spin_unlock(&sl->lock);
315 netif_stop_queue(sl->dev);
316 sll_encaps(sl, (struct can_frame *) skb->data); /* encaps & send */
317 spin_unlock(&sl->lock);
325 /******************************************
326 * Routines looking at netdevice side.
327 ******************************************/
329 /* Netdevice UP -> DOWN routine */
330 static int sll_close(struct net_device *dev)
332 struct sllin *sl = netdev_priv(dev);
334 spin_lock_bh(&sl->lock);
336 /* TTY discipline is running. */
337 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
339 netif_stop_queue(dev);
342 spin_unlock_bh(&sl->lock);
347 /* Netdevice DOWN -> UP routine */
348 static int sll_open(struct net_device *dev)
350 struct sllin *sl = netdev_priv(dev);
352 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
357 sl->flags &= (1 << SLF_INUSE);
358 netif_start_queue(dev);
362 /* Hook the destructor so we can free sllin devs at the right point in time */
363 static void sll_free_netdev(struct net_device *dev)
365 int i = dev->base_addr;
367 sllin_devs[i] = NULL;
370 static const struct net_device_ops sll_netdev_ops = {
371 .ndo_open = sll_open,
372 .ndo_stop = sll_close,
373 .ndo_start_xmit = sll_xmit,
376 static void sll_setup(struct net_device *dev)
378 dev->netdev_ops = &sll_netdev_ops;
379 dev->destructor = sll_free_netdev;
381 dev->hard_header_len = 0;
383 dev->tx_queue_len = 10;
385 dev->mtu = sizeof(struct can_frame);
386 dev->type = ARPHRD_CAN;
388 /* New-style flags. */
389 dev->flags = IFF_NOARP;
390 dev->features = NETIF_F_NO_CSUM;
393 /******************************************
394 Routines looking at TTY side.
395 ******************************************/
398 * Handle the 'receiver data ready' interrupt.
399 * This function is called by the 'tty_io' module in the kernel when
400 * a block of SLLIN data has been received, which can now be decapsulated
401 * and sent on to some IP layer for further processing. This will not
402 * be re-entered while running but other ldisc functions may be called
406 static void sllin_receive_buf(struct tty_struct *tty,
407 const unsigned char *cp, char *fp, int count)
409 struct sllin *sl = (struct sllin *) tty->disc_data;
411 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
414 /* Read the characters out of the buffer */
417 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
418 sl->dev->stats.rx_errors++;
422 sllin_unesc(sl, *cp++);
426 /************************************
427 * sllin_open helper routines.
428 ************************************/
430 /* Collect hanged up channels */
431 static void sll_sync(void)
434 struct net_device *dev;
437 for (i = 0; i < maxdev; i++) {
442 sl = netdev_priv(dev);
443 if (sl->tty || sl->leased)
445 if (dev->flags & IFF_UP)
450 /* Find a free SLLIN channel, and link in this `tty' line. */
451 static struct sllin *sll_alloc(dev_t line)
454 struct net_device *dev = NULL;
457 if (sllin_devs == NULL)
458 return NULL; /* Master array missing ! */
460 for (i = 0; i < maxdev; i++) {
467 /* Sorry, too many, all slots in use */
472 sl = netdev_priv(dev);
473 if (test_bit(SLF_INUSE, &sl->flags)) {
474 unregister_netdevice(dev);
476 sllin_devs[i] = NULL;
482 sprintf(name, "sllin%d", i);
484 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
490 sl = netdev_priv(dev);
492 /* Initialize channel control data */
493 sl->magic = SLLIN_MAGIC;
495 spin_lock_init(&sl->lock);
502 * Open the high-level part of the SLLIN channel.
503 * This function is called by the TTY module when the
504 * SLLIN line discipline is called for. Because we are
505 * sure the tty line exists, we only have to link it to
506 * a free SLLIN channel...
508 * Called in process context serialized from other ldisc calls.
511 static int sllin_open(struct tty_struct *tty)
515 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
517 if (!capable(CAP_NET_ADMIN))
520 if (tty->ops->write == NULL)
523 /* RTnetlink lock is misused here to serialize concurrent
524 opens of sllin channels. There are better ways, but it is
529 /* Collect hanged up channels. */
535 /* First make sure we're not already connected. */
536 if (sl && sl->magic == SLLIN_MAGIC)
539 /* OK. Find a free SLLIN channel to use. */
541 sl = sll_alloc(tty_devnum(tty));
547 sl->line = tty_devnum(tty);
548 sl->pid = current->pid;
550 if (!test_bit(SLF_INUSE, &sl->flags)) {
551 /* Perform the low-level SLLIN initialization. */
555 set_bit(SLF_INUSE, &sl->flags);
557 err = register_netdevice(sl->dev);
562 /* Done. We have linked the TTY line to a channel. */
564 tty->receive_room = 65536; /* We don't flow control */
566 /* TTY layer expects 0 on success */
571 tty->disc_data = NULL;
572 clear_bit(SLF_INUSE, &sl->flags);
577 /* Count references from TTY module */
582 * Close down a SLLIN channel.
583 * This means flushing out any pending queues, and then returning. This
584 * call is serialized against other ldisc functions.
586 * We also use this method for a hangup event.
589 static void sllin_close(struct tty_struct *tty)
591 struct sllin *sl = (struct sllin *) tty->disc_data;
593 /* First make sure we're connected. */
594 if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
597 tty->disc_data = NULL;
602 /* Flush network side */
603 unregister_netdev(sl->dev);
604 /* This will complete via sl_free_netdev */
607 static int sllin_hangup(struct tty_struct *tty)
613 /* Perform I/O control on an active SLLIN channel. */
614 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
615 unsigned int cmd, unsigned long arg)
617 struct sllin *sl = (struct sllin *) tty->disc_data;
620 /* First make sure we're connected. */
621 if (!sl || sl->magic != SLLIN_MAGIC)
626 tmp = strlen(sl->dev->name) + 1;
627 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
635 return tty_mode_ioctl(tty, file, cmd, arg);
639 static struct tty_ldisc_ops sll_ldisc = {
640 .owner = THIS_MODULE,
641 .magic = TTY_LDISC_MAGIC,
644 .close = sllin_close,
645 .hangup = sllin_hangup,
646 .ioctl = sllin_ioctl,
647 .receive_buf = sllin_receive_buf,
648 .write_wakeup = sllin_write_wakeup,
651 static int __init sllin_init(void)
656 maxdev = 4; /* Sanity */
659 printk(KERN_INFO "sllin: %d dynamic interface channels.\n", maxdev);
661 sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
663 printk(KERN_ERR "sllin: can't allocate sllin device array!\n");
667 /* Fill in our line protocol discipline, and register it */
668 status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
670 printk(KERN_ERR "sllin: can't register line discipline\n");
676 static void __exit sllin_exit(void)
679 struct net_device *dev;
681 unsigned long timeout = jiffies + HZ;
684 if (sllin_devs == NULL)
687 /* First of all: check for active disciplines and hangup them.
691 msleep_interruptible(100);
694 for (i = 0; i < maxdev; i++) {
698 sl = netdev_priv(dev);
699 spin_lock_bh(&sl->lock);
704 spin_unlock_bh(&sl->lock);
706 } while (busy && time_before(jiffies, timeout));
708 /* FIXME: hangup is async so we should wait when doing this second
711 for (i = 0; i < maxdev; i++) {
715 sllin_devs[i] = NULL;
717 sl = netdev_priv(dev);
719 printk(KERN_ERR "%s: tty discipline still running\n",
721 /* Intentionally leak the control block. */
722 dev->destructor = NULL;
725 unregister_netdev(dev);
731 i = tty_unregister_ldisc(N_SLLIN);
733 printk(KERN_ERR "sllin: can't unregister ldisc (err %d)\n", i);
736 module_init(sllin_init);
737 module_exit(sllin_exit);