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 */
65 static __initdata const char banner[] =
66 KERN_INFO "sllin: serial line LIN interface driver\n";
68 MODULE_ALIAS_LDISC(N_SLLIN);
69 MODULE_DESCRIPTION("serial line LIN interface");
70 MODULE_LICENSE("GPL");
71 MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
73 #define SLLIN_MAGIC 0x53CA
75 static int maxdev = 10; /* MAX number of SLLIN channels;
76 This can be overridden with
77 insmod sllin.ko maxdev=nnn */
78 module_param(maxdev, int, 0);
79 MODULE_PARM_DESC(maxdev, "Maximum number of sllin interfaces");
81 /* maximum rx buffer len: extended CAN frame with timestamp */
82 #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
88 struct tty_struct *tty; /* ptr to TTY structure */
89 struct net_device *dev; /* easy for intr handling */
92 /* These are pointers to the malloc()ed frame buffers. */
93 unsigned char rbuff[SLC_MTU]; /* receiver buffer */
94 int rcount; /* received chars counter */
95 unsigned char xbuff[SLC_MTU]; /* transmitter buffer */
96 unsigned char *xhead; /* pointer to next XMIT byte */
97 int xleft; /* bytes left in XMIT queue */
99 unsigned long flags; /* Flag values/ mode etc */
100 #define SLF_INUSE 0 /* Channel in use */
101 #define SLF_ERROR 1 /* Parity, etc. error */
103 unsigned char leased;
108 static struct net_device **sllin_devs;
111 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
113 struct ktermios old_termios;
116 mutex_lock(&tty->termios_mutex);
117 old_termios = *(tty->termios);
118 cflag = tty->termios->c_cflag;
119 tty_encode_baud_rate(tty, speed, speed);
120 if (tty->ops->set_termios)
121 tty->ops->set_termios(tty, &old_termios);
122 //priv->io.speed = speed;
123 mutex_unlock(&tty->termios_mutex);
129 /* Send one completely decapsulated can_frame to the network layer */
130 static void sll_bump(struct sllin *sl)
132 // struct sk_buff *skb;
133 // struct can_frame cf;
134 // int i, dlc_pos, tmp;
135 // unsigned long ultmp;
136 // char cmd = sl->rbuff[0];
138 // if ((cmd != 't') && (cmd != 'T') && (cmd != 'r') && (cmd != 'R'))
141 // if (cmd & 0x20) /* tiny chars 'r' 't' => standard frame format */
142 // dlc_pos = 4; /* dlc position tiiid */
144 // dlc_pos = 9; /* dlc position Tiiiiiiiid */
146 // if (!((sl->rbuff[dlc_pos] >= '0') && (sl->rbuff[dlc_pos] < '9')))
149 // cf.can_dlc = sl->rbuff[dlc_pos] - '0'; /* get can_dlc from ASCII val */
151 // sl->rbuff[dlc_pos] = 0; /* terminate can_id string */
153 // if (strict_strtoul(sl->rbuff+1, 16, &ultmp))
156 // cf.can_id = ultmp;
158 // if (!(cmd & 0x20)) /* NO tiny chars => extended frame format */
159 // cf.can_id |= CAN_EFF_FLAG;
161 // if ((cmd | 0x20) == 'r') /* RTR frame */
162 // cf.can_id |= CAN_RTR_FLAG;
164 // *(u64 *) (&cf.data) = 0; /* clear payload */
166 // for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
168 // tmp = asc2nibble(sl->rbuff[dlc_pos++]);
171 // cf.data[i] = (tmp << 4);
172 // tmp = asc2nibble(sl->rbuff[dlc_pos++]);
175 // cf.data[i] |= tmp;
179 // skb = dev_alloc_skb(sizeof(struct can_frame));
183 // skb->dev = sl->dev;
184 // skb->protocol = htons(ETH_P_CAN);
185 // skb->pkt_type = PACKET_BROADCAST;
186 // skb->ip_summed = CHECKSUM_UNNECESSARY;
187 // memcpy(skb_put(skb, sizeof(struct can_frame)),
188 // &cf, sizeof(struct can_frame));
191 // sl->dev->stats.rx_packets++;
192 // sl->dev->stats.rx_bytes += cf.can_dlc;
195 /* parse tty input stream */
196 static void sllin_unesc(struct sllin *sl, unsigned char s)
199 if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
200 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
206 if (!test_bit(SLF_ERROR, &sl->flags)) {
207 if (sl->rcount < SLC_MTU) {
208 sl->rbuff[sl->rcount++] = s;
211 sl->dev->stats.rx_over_errors++;
212 set_bit(SLF_ERROR, &sl->flags);
218 /************************************************************************
219 * STANDARD SLLIN ENCAPSULATION *
220 ************************************************************************/
222 /* Convert particular CAN frame into LIN frame and send it to TTY queue. */
223 static void sll_encaps(struct sllin *sl, struct can_frame *cf)
226 char lframe[16] = {0x00, 0x55}; /* Fake break, Sync byte */
227 struct tty_struct *tty = sl->tty;
229 /* We do care only about SFF frames */
230 if (cf->can_id & CAN_EFF_FLAG)
233 /* Send only header */
234 if (cf->can_id & CAN_RTR_FLAG) {
235 lframe[2] = (u8)cf->can_id; /* Get one byte LIN ID */
237 sltty_change_speed(tty, 1200);
238 tty->ops->write(tty, &lframe[0], 1);
239 sltty_change_speed(tty, 2400);
240 tty->ops->write(tty, &lframe[1], 1);
241 tty->ops->write(tty, &lframe[2], 1);
243 /* idx = strlen(sl->xbuff);
245 for (i = 0; i < cf->can_dlc; i++)
246 sprintf(&sl->xbuff[idx + 2*i], "%02X", cf->data[i]);
248 * Order of next two lines is *very* important.
249 * When we are sending a little amount of data,
250 * the transfer may be completed inside the ops->write()
251 * routine, because it's running with interrupts enabled.
252 * In this case we *never* got WRITE_WAKEUP event,
253 * if we did not request it before write operation.
254 * 14 Oct 1994 Dmitry Gorodchanin.
256 set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
257 actual = sl->tty->ops->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
258 sl->xleft = strlen(sl->xbuff) - actual;
259 sl->xhead = sl->xbuff + actual;
260 sl->dev->stats.tx_bytes += cf->can_dlc;
267 * Called by the driver when there's room for more data. If we have
268 * more packets to send, we send them here.
270 static void sllin_write_wakeup(struct tty_struct *tty)
273 struct sllin *sl = (struct sllin *) tty->disc_data;
275 /* First make sure we're connected. */
276 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
279 if (sl->xleft <= 0) {
280 /* Now serial buffer is almost free & we can start
281 * transmission of another packet */
282 sl->dev->stats.tx_packets++;
283 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
284 netif_wake_queue(sl->dev);
288 actual = tty->ops->write(tty, sl->xhead, sl->xleft);
293 /* Send a can_frame to a TTY queue. */
294 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
296 struct sllin *sl = netdev_priv(dev);
298 if (skb->len != sizeof(struct can_frame))
301 spin_lock(&sl->lock);
302 if (!netif_running(dev)) {
303 spin_unlock(&sl->lock);
304 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
307 if (sl->tty == NULL) {
308 spin_unlock(&sl->lock);
312 netif_stop_queue(sl->dev);
313 sll_encaps(sl, (struct can_frame *) skb->data); /* encaps & send */
314 spin_unlock(&sl->lock);
322 /******************************************
323 * Routines looking at netdevice side.
324 ******************************************/
326 /* Netdevice UP -> DOWN routine */
327 static int sll_close(struct net_device *dev)
329 struct sllin *sl = netdev_priv(dev);
331 spin_lock_bh(&sl->lock);
333 /* TTY discipline is running. */
334 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
336 netif_stop_queue(dev);
339 spin_unlock_bh(&sl->lock);
344 /* Netdevice DOWN -> UP routine */
345 static int sll_open(struct net_device *dev)
347 struct sllin *sl = netdev_priv(dev);
352 sl->flags &= (1 << SLF_INUSE);
353 netif_start_queue(dev);
357 /* Hook the destructor so we can free sllin devs at the right point in time */
358 static void sll_free_netdev(struct net_device *dev)
360 int i = dev->base_addr;
362 sllin_devs[i] = NULL;
365 static const struct net_device_ops sll_netdev_ops = {
366 .ndo_open = sll_open,
367 .ndo_stop = sll_close,
368 .ndo_start_xmit = sll_xmit,
371 static void sll_setup(struct net_device *dev)
373 dev->netdev_ops = &sll_netdev_ops;
374 dev->destructor = sll_free_netdev;
376 dev->hard_header_len = 0;
378 dev->tx_queue_len = 10;
380 dev->mtu = sizeof(struct can_frame);
381 dev->type = ARPHRD_CAN;
383 /* New-style flags. */
384 dev->flags = IFF_NOARP;
385 dev->features = NETIF_F_NO_CSUM;
388 /******************************************
389 Routines looking at TTY side.
390 ******************************************/
393 * Handle the 'receiver data ready' interrupt.
394 * This function is called by the 'tty_io' module in the kernel when
395 * a block of SLLIN data has been received, which can now be decapsulated
396 * and sent on to some IP layer for further processing. This will not
397 * be re-entered while running but other ldisc functions may be called
401 static void sllin_receive_buf(struct tty_struct *tty,
402 const unsigned char *cp, char *fp, int count)
404 struct sllin *sl = (struct sllin *) tty->disc_data;
406 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
409 /* Read the characters out of the buffer */
412 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
413 sl->dev->stats.rx_errors++;
417 sllin_unesc(sl, *cp++);
421 /************************************
422 * sllin_open helper routines.
423 ************************************/
425 /* Collect hanged up channels */
426 static void sll_sync(void)
429 struct net_device *dev;
432 for (i = 0; i < maxdev; i++) {
437 sl = netdev_priv(dev);
438 if (sl->tty || sl->leased)
440 if (dev->flags & IFF_UP)
445 /* Find a free SLLIN channel, and link in this `tty' line. */
446 static struct sllin *sll_alloc(dev_t line)
449 struct net_device *dev = NULL;
452 if (sllin_devs == NULL)
453 return NULL; /* Master array missing ! */
455 for (i = 0; i < maxdev; i++) {
462 /* Sorry, too many, all slots in use */
467 sl = netdev_priv(dev);
468 if (test_bit(SLF_INUSE, &sl->flags)) {
469 unregister_netdevice(dev);
471 sllin_devs[i] = NULL;
477 sprintf(name, "sllin%d", i);
479 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
485 sl = netdev_priv(dev);
487 /* Initialize channel control data */
488 sl->magic = SLLIN_MAGIC;
490 spin_lock_init(&sl->lock);
497 * Open the high-level part of the SLLIN channel.
498 * This function is called by the TTY module when the
499 * SLLIN line discipline is called for. Because we are
500 * sure the tty line exists, we only have to link it to
501 * a free SLLIN channel...
503 * Called in process context serialized from other ldisc calls.
506 static int sllin_open(struct tty_struct *tty)
511 if (!capable(CAP_NET_ADMIN))
514 if (tty->ops->write == NULL)
517 /* RTnetlink lock is misused here to serialize concurrent
518 opens of sllin channels. There are better ways, but it is
523 /* Collect hanged up channels. */
529 /* First make sure we're not already connected. */
530 if (sl && sl->magic == SLLIN_MAGIC)
533 /* OK. Find a free SLLIN channel to use. */
535 sl = sll_alloc(tty_devnum(tty));
541 sl->line = tty_devnum(tty);
542 sl->pid = current->pid;
544 if (!test_bit(SLF_INUSE, &sl->flags)) {
545 /* Perform the low-level SLLIN initialization. */
549 set_bit(SLF_INUSE, &sl->flags);
551 err = register_netdevice(sl->dev);
556 /* Done. We have linked the TTY line to a channel. */
558 tty->receive_room = 65536; /* We don't flow control */
560 /* TTY layer expects 0 on success */
565 tty->disc_data = NULL;
566 clear_bit(SLF_INUSE, &sl->flags);
571 /* Count references from TTY module */
576 * Close down a SLLIN channel.
577 * This means flushing out any pending queues, and then returning. This
578 * call is serialized against other ldisc functions.
580 * We also use this method for a hangup event.
583 static void sllin_close(struct tty_struct *tty)
585 struct sllin *sl = (struct sllin *) tty->disc_data;
587 /* First make sure we're connected. */
588 if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
591 tty->disc_data = NULL;
596 /* Flush network side */
597 unregister_netdev(sl->dev);
598 /* This will complete via sl_free_netdev */
601 static int sllin_hangup(struct tty_struct *tty)
607 /* Perform I/O control on an active SLLIN channel. */
608 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
609 unsigned int cmd, unsigned long arg)
611 struct sllin *sl = (struct sllin *) tty->disc_data;
614 /* First make sure we're connected. */
615 if (!sl || sl->magic != SLLIN_MAGIC)
620 tmp = strlen(sl->dev->name) + 1;
621 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
629 return tty_mode_ioctl(tty, file, cmd, arg);
633 static struct tty_ldisc_ops sll_ldisc = {
634 .owner = THIS_MODULE,
635 .magic = TTY_LDISC_MAGIC,
638 .close = sllin_close,
639 .hangup = sllin_hangup,
640 .ioctl = sllin_ioctl,
641 .receive_buf = sllin_receive_buf,
642 .write_wakeup = sllin_write_wakeup,
645 static int __init sllin_init(void)
650 maxdev = 4; /* Sanity */
653 printk(KERN_INFO "sllin: %d dynamic interface channels.\n", maxdev);
655 sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
657 printk(KERN_ERR "sllin: can't allocate sllin device array!\n");
661 /* Fill in our line protocol discipline, and register it */
662 status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
664 printk(KERN_ERR "sllin: can't register line discipline\n");
670 static void __exit sllin_exit(void)
673 struct net_device *dev;
675 unsigned long timeout = jiffies + HZ;
678 if (sllin_devs == NULL)
681 /* First of all: check for active disciplines and hangup them.
685 msleep_interruptible(100);
688 for (i = 0; i < maxdev; i++) {
692 sl = netdev_priv(dev);
693 spin_lock_bh(&sl->lock);
698 spin_unlock_bh(&sl->lock);
700 } while (busy && time_before(jiffies, timeout));
702 /* FIXME: hangup is async so we should wait when doing this second
705 for (i = 0; i < maxdev; i++) {
709 sllin_devs[i] = NULL;
711 sl = netdev_priv(dev);
713 printk(KERN_ERR "%s: tty discipline still running\n",
715 /* Intentionally leak the control block. */
716 dev->destructor = NULL;
719 unregister_netdev(dev);
725 i = tty_unregister_ldisc(N_SLLIN);
727 printk(KERN_ERR "sllin: can't unregister ldisc (err %d)\n", i);
730 module_init(sllin_init);
731 module_exit(sllin_exit);