2 * slcan.c - serial line CAN interface driver (using tty line discipline)
4 * This file is derived from linux/drivers/net/slip.c
6 * Therefore it has the same (strange?) behaviour not to unregister the
7 * netdevice when detaching the tty. Is there any better solution?
9 * Do not try to attach, detach and re-attach a tty for this reason ...
11 * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk>
12 * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
13 * slcan.c Author : Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
15 * Copyright (c) 2007-2009 Volkswagen Group Electronic Research
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2 of the License, or (at your
20 * option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307. You can also get it
30 * at http://www.gnu.org/licenses/gpl.html
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
45 * Send feedback to <socketcan-users@lists.berlios.de>
49 #include <linux/version.h>
50 #include <linux/module.h>
51 #include <linux/moduleparam.h>
53 #include <asm/system.h>
54 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17)
55 #include <linux/uaccess.h>
57 #include <asm/uaccess.h>
59 #include <linux/bitops.h>
60 #include <linux/string.h>
62 #include <linux/interrupt.h>
64 #include <linux/tty.h>
65 #include <linux/errno.h>
66 #include <linux/netdevice.h>
67 #include <linux/etherdevice.h>
68 #include <linux/skbuff.h>
69 #include <linux/rtnetlink.h>
70 #include <linux/if_arp.h>
71 #include <linux/if_ether.h>
72 #include <linux/if_slip.h>
73 #include <linux/delay.h>
74 #include <linux/init.h>
76 #include <linux/can.h>
78 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
81 static __initdata const char banner[] =
82 KERN_INFO "slcan: serial line CAN interface driver\n";
84 MODULE_ALIAS_LDISC(N_SLCAN);
85 MODULE_DESCRIPTION("serial line CAN interface");
86 MODULE_LICENSE("GPL");
87 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
89 #ifdef CONFIG_CAN_DEBUG_DEVICES
91 module_param(debug, int, S_IRUGO);
92 #define DBG(args...) (debug & 1 ? \
93 (printk(KERN_DEBUG "slcan %s: ", __func__), \
100 #error Your kernel does not support tty line discipline N_SLCAN
103 * As there is currently no line discipline N_SLCAN in the mainstream kernel
104 * you will have to modify two kernel includes & recompile the kernel.
106 * Add this in include/asm/termios.h after the definition of N_HCI:
109 * Increment NR_LDICS in include/linux/tty.h from 16 to 17
111 * NEW: Since Kernel 2.6.21 you only have to change include/linux/tty.h
113 * HACK for precompiled Kernels:
115 * In order to use the slcan driver without rebuilding the kernel, the slcan
116 * driver must be compiled to use an existing line discipline.
117 * The N_MOUSE line discipline is documented to be free for custom use and
118 * using it *should* not cause any side effect.
120 * Then, before compiling the slcan driver, add a -DN_SLCAN=N_MOUSE
121 * compilation option in its Makefile. The slcan_attach tool must(!!) also be
122 * rebuild to use the right value for N_SLCAN. This workaround will allow
123 * to use the slcan driver with an existing kernel.
126 #define SLC_CHECK_TRANSMIT
127 #define SLCAN_MAGIC 0x53CA
129 static int maxdev = 10; /* MAX number of SLCAN channels;
130 This can be overridden with
131 insmod slcan.ko maxdev=nnn */
132 module_param(maxdev, int, 0);
133 MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
135 /* maximum rx buffer len: extended CAN frame with timestamp */
136 #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
141 /* Various fields. */
142 struct tty_struct *tty; /* ptr to TTY structure */
143 struct net_device *dev; /* easy for intr handling */
146 /* These are pointers to the malloc()ed frame buffers. */
147 unsigned char rbuff[SLC_MTU]; /* receiver buffer */
148 int rcount; /* received chars counter */
149 unsigned char xbuff[SLC_MTU]; /* transmitter buffer */
150 unsigned char *xhead; /* pointer to next XMIT byte */
151 int xleft; /* bytes left in XMIT queue */
153 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
154 /* SLCAN interface statistics. */
155 struct net_device_stats stats;
158 unsigned long flags; /* Flag values/ mode etc */
159 #define SLF_INUSE 0 /* Channel in use */
160 #define SLF_ERROR 1 /* Parity, etc. error */
162 unsigned char leased;
167 static struct net_device **slcan_devs;
170 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
171 /* Netdevice get statistics request */
172 static struct net_device_stats *slc_get_stats(struct net_device *dev)
174 struct slcan *sl = netdev_priv(dev);
180 /************************************************************************
181 * SLCAN ENCAPSULATION FORMAT *
182 ************************************************************************/
185 * A CAN frame has a can_id (11 bit standard frame format OR 29 bit extended
186 * frame format) a data length code (can_dlc) which can be from 0 to 8
187 * and up to <can_dlc> data bytes as payload.
188 * Additionally a CAN frame may become a remote transmission frame if the
189 * RTR-bit is set. This causes another ECU to send a CAN frame with the
192 * The SLCAN ASCII representation of these different frame types is:
193 * <type> <id> <dlc> <data>*
195 * Extended frames (29 bit) are defined by capital characters in the type.
196 * RTR frames are defined as 'r' types - normal frames have 't' type:
197 * t => 11 bit data frame
198 * r => 11 bit RTR frame
199 * T => 29 bit data frame
200 * R => 29 bit RTR frame
202 * The <id> is 3 (standard) or 8 (extended) bytes in ASCII Hex (base64).
203 * The <dlc> is a one byte ASCII number ('0' - '8')
204 * The <data> section has at much ASCII Hex bytes as defined by the <dlc>
208 * t1230 : can_id 0x123, can_dlc 0, no data
209 * t4563112233 : can_id 0x456, can_dlc 3, data 0x11 0x22 0x33
210 * T12ABCDEF2AA55 : extended can_id 0x12ABCDEF, can_dlc 2, data 0xAA 0x55
211 * r1230 : can_id 0x123, can_dlc 0, no data, remote transmission request
215 /************************************************************************
216 * STANDARD SLCAN DECAPSULATION *
217 ************************************************************************/
219 static int asc2nibble(char c)
222 if ((c >= '0') && (c <= '9'))
225 if ((c >= 'A') && (c <= 'F'))
228 if ((c >= 'a') && (c <= 'f'))
231 return 16; /* error */
234 /* Send one completely decapsulated can_frame to the network layer */
235 static void slc_bump(struct slcan *sl)
237 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
238 struct net_device_stats *stats = slc_get_stats(sl->dev);
240 struct net_device_stats *stats = &sl->dev->stats;
246 char cmd = sl->rbuff[0];
248 if ((cmd != 't') && (cmd != 'T') && (cmd != 'r') && (cmd != 'R'))
251 if (cmd & 0x20) /* tiny chars 'r' 't' => standard frame format */
252 dlc_pos = 4; /* dlc position tiiid */
254 dlc_pos = 9; /* dlc position Tiiiiiiiid */
256 if (!((sl->rbuff[dlc_pos] >= '0') && (sl->rbuff[dlc_pos] < '9')))
259 cf.can_dlc = sl->rbuff[dlc_pos] - '0'; /* get can_dlc from ASCII val */
261 sl->rbuff[dlc_pos] = 0; /* terminate can_id string */
263 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
264 ultmp = simple_strtoul(sl->rbuff+1, NULL, 16);
266 if (strict_strtoul(sl->rbuff+1, 16, &ultmp))
271 if (!(cmd & 0x20)) /* NO tiny chars => extended frame format */
272 cf.can_id |= CAN_EFF_FLAG;
274 if ((cmd | 0x20) == 'r') /* RTR frame */
275 cf.can_id |= CAN_RTR_FLAG;
277 *(u64 *) (&cf.data) = 0; /* clear payload */
279 for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
281 tmp = asc2nibble(sl->rbuff[dlc_pos++]);
284 cf.data[i] = (tmp << 4);
285 tmp = asc2nibble(sl->rbuff[dlc_pos++]);
292 skb = dev_alloc_skb(sizeof(struct can_frame));
297 skb->protocol = htons(ETH_P_CAN);
298 skb->pkt_type = PACKET_BROADCAST;
299 skb->ip_summed = CHECKSUM_UNNECESSARY;
300 memcpy(skb_put(skb, sizeof(struct can_frame)),
301 &cf, sizeof(struct can_frame));
304 sl->dev->last_rx = jiffies;
306 stats->rx_bytes += cf.can_dlc;
309 /* parse tty input stream */
310 static void slcan_unesc(struct slcan *sl, unsigned char s)
312 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
313 struct net_device_stats *stats = slc_get_stats(sl->dev);
315 struct net_device_stats *stats = &sl->dev->stats;
318 if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
319 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
325 if (!test_bit(SLF_ERROR, &sl->flags)) {
326 if (sl->rcount < SLC_MTU) {
327 sl->rbuff[sl->rcount++] = s;
330 stats->rx_over_errors++;
331 set_bit(SLF_ERROR, &sl->flags);
337 /************************************************************************
338 * STANDARD SLCAN ENCAPSULATION *
339 ************************************************************************/
341 /* Encapsulate one can_frame and stuff into a TTY queue. */
342 static void slc_encaps(struct slcan *sl, struct can_frame *cf)
344 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
345 struct net_device_stats *stats = slc_get_stats(sl->dev);
347 struct net_device_stats *stats = &sl->dev->stats;
352 if (cf->can_id & CAN_RTR_FLAG)
353 cmd = 'R'; /* becomes 'r' in standard frame format */
355 cmd = 'T'; /* becomes 't' in standard frame format */
357 if (cf->can_id & CAN_EFF_FLAG)
358 sprintf(sl->xbuff, "%c%08X%d", cmd,
359 cf->can_id & CAN_EFF_MASK, cf->can_dlc);
361 sprintf(sl->xbuff, "%c%03X%d", cmd | 0x20,
362 cf->can_id & CAN_SFF_MASK, cf->can_dlc);
364 idx = strlen(sl->xbuff);
366 for (i = 0; i < cf->can_dlc; i++)
367 sprintf(&sl->xbuff[idx + 2*i], "%02X", cf->data[i]);
369 DBG("ASCII frame = '%s'\n", sl->xbuff);
371 strcat(sl->xbuff, "\r"); /* add terminating character */
373 /* Order of next two lines is *very* important.
374 * When we are sending a little amount of data,
375 * the transfer may be completed inside driver.write()
376 * routine, because it's running with interrupts enabled.
377 * In this case we *never* got WRITE_WAKEUP event,
378 * if we did not request it before write operation.
379 * 14 Oct 1994 Dmitry Gorodchanin.
381 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
382 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
383 actual = sl->tty->driver->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
385 actual = sl->tty->ops->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
387 #ifdef SLC_CHECK_TRANSMIT
388 sl->dev->trans_start = jiffies;
390 sl->xleft = strlen(sl->xbuff) - actual;
391 sl->xhead = sl->xbuff + actual;
392 stats->tx_bytes += cf->can_dlc;
396 * Called by the driver when there's room for more data. If we have
397 * more packets to send, we send them here.
399 static void slcan_write_wakeup(struct tty_struct *tty)
402 struct slcan *sl = (struct slcan *) tty->disc_data;
403 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
404 struct net_device_stats *stats = slc_get_stats(sl->dev);
406 struct net_device_stats *stats = &sl->dev->stats;
409 /* First make sure we're connected. */
410 if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
413 if (sl->xleft <= 0) {
414 /* Now serial buffer is almost free & we can start
415 * transmission of another packet */
417 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
418 netif_wake_queue(sl->dev);
422 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
423 actual = tty->driver->write(tty, sl->xhead, sl->xleft);
425 actual = tty->ops->write(tty, sl->xhead, sl->xleft);
431 static void slc_tx_timeout(struct net_device *dev)
433 struct slcan *sl = netdev_priv(dev);
435 spin_lock(&sl->lock);
437 if (netif_queue_stopped(dev)) {
438 if (!netif_running(dev))
441 /* May be we must check transmitter timeout here ?
442 * 14 Oct 1994 Dmitry Gorodchanin.
444 #ifdef SLC_CHECK_TRANSMIT
445 if (time_before(jiffies, dev->trans_start + 20 * HZ)) {
446 /* 20 sec timeout not reached */
449 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
450 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
451 (sl->tty->driver->chars_in_buffer(sl->tty) || sl->xleft)
453 (tty_chars_in_buffer(sl->tty) || sl->xleft)
455 ? "bad line quality" : "driver error");
457 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
458 netif_wake_queue(sl->dev);
462 spin_unlock(&sl->lock);
466 /******************************************
467 * Routines looking at netdevice side.
468 ******************************************/
470 /* Send a can_frame to a TTY queue. */
471 static int slc_xmit(struct sk_buff *skb, struct net_device *dev)
473 struct slcan *sl = netdev_priv(dev);
475 if (skb->len != sizeof(struct can_frame))
478 spin_lock(&sl->lock);
479 if (!netif_running(dev)) {
480 spin_unlock(&sl->lock);
481 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
485 if (sl->tty == NULL) {
486 spin_unlock(&sl->lock);
490 netif_stop_queue(sl->dev);
491 slc_encaps(sl, (struct can_frame *) skb->data); /* encaps & send */
492 spin_unlock(&sl->lock);
500 /* Netdevice UP -> DOWN routine */
501 static int slc_close(struct net_device *dev)
503 struct slcan *sl = netdev_priv(dev);
505 spin_lock_bh(&sl->lock);
507 /* TTY discipline is running. */
508 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
510 netif_stop_queue(dev);
513 spin_unlock_bh(&sl->lock);
518 /* Netdevice DOWN -> UP routine */
519 static int slc_open(struct net_device *dev)
521 struct slcan *sl = netdev_priv(dev);
526 sl->flags &= (1 << SLF_INUSE);
527 netif_start_queue(dev);
531 /* Netdevice register callback */
532 static void slc_setup(struct net_device *dev)
534 dev->open = slc_open;
535 dev->destructor = free_netdev;
536 dev->stop = slc_close;
537 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
538 dev->get_stats = slc_get_stats;
540 dev->hard_start_xmit = slc_xmit;
542 dev->hard_header_len = 0;
544 dev->tx_queue_len = 10;
546 dev->mtu = sizeof(struct can_frame);
547 dev->type = ARPHRD_CAN;
548 #ifdef SLC_CHECK_TRANSMIT
549 dev->tx_timeout = slc_tx_timeout;
550 dev->watchdog_timeo = 20*HZ;
553 /* New-style flags. */
554 dev->flags = IFF_NOARP;
555 dev->features = NETIF_F_NO_CSUM;
558 /******************************************
559 * Routines looking at TTY side.
560 ******************************************/
562 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
563 static int slcan_receive_room(struct tty_struct *tty)
565 return 65536; /* We can handle an infinite amount of data. :-) */
570 * Handle the 'receiver data ready' interrupt.
571 * This function is called by the 'tty_io' module in the kernel when
572 * a block of SLCAN data has been received, which can now be decapsulated
573 * and sent on to some IP layer for further processing. This will not
574 * be re-entered while running but other ldisc functions may be called
578 static void slcan_receive_buf(struct tty_struct *tty,
579 const unsigned char *cp, char *fp, int count)
581 struct slcan *sl = (struct slcan *) tty->disc_data;
582 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
583 struct net_device_stats *stats = slc_get_stats(sl->dev);
585 struct net_device_stats *stats = &sl->dev->stats;
588 if (!sl || sl->magic != SLCAN_MAGIC ||
589 !netif_running(sl->dev))
592 /* Read the characters out of the buffer */
595 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
600 slcan_unesc(sl, *cp++);
604 /************************************
605 * slcan_open helper routines.
606 ************************************/
608 /* Collect hanged up channels */
610 static void slc_sync(void)
613 struct net_device *dev;
616 for (i = 0; i < maxdev; i++) {
621 sl = netdev_priv(dev);
622 if (sl->tty || sl->leased)
624 if (dev->flags&IFF_UP)
630 /* Find a free SLCAN channel, and link in this `tty' line. */
631 static struct slcan *slc_alloc(dev_t line)
636 struct net_device *dev = NULL;
639 if (slcan_devs == NULL)
640 return NULL; /* Master array missing ! */
642 for (i = 0; i < maxdev; i++) {
647 sl = netdev_priv(dev);
649 if (sl->line != line)
654 /* Clear ESCAPE & ERROR flags */
655 sl->flags &= (1 << SLF_INUSE);
662 if (current->pid == sl->pid) {
663 if (sl->line == line && score < 3) {
674 if (sl->line == line && score < 1) {
689 sl = netdev_priv(dev);
690 sl->flags &= (1 << SLF_INUSE);
695 /* Sorry, too many, all slots in use */
700 sl = netdev_priv(dev);
701 if (test_bit(SLF_INUSE, &sl->flags)) {
702 unregister_netdevice(dev);
704 slcan_devs[i] = NULL;
710 sprintf(name, "slc%d", i);
712 dev = alloc_netdev(sizeof(*sl), name, slc_setup);
718 sl = netdev_priv(dev);
720 /* Initialize channel control data */
721 sl->magic = SLCAN_MAGIC;
723 spin_lock_init(&sl->lock);
730 * Open the high-level part of the SLCAN channel.
731 * This function is called by the TTY module when the
732 * SLCAN line discipline is called for. Because we are
733 * sure the tty line exists, we only have to link it to
734 * a free SLCAN channel...
736 * Called in process context serialized from other ldisc calls.
739 static int slcan_open(struct tty_struct *tty)
744 if (!capable(CAP_NET_ADMIN))
747 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,25)
748 if (tty->ops->write == NULL)
752 /* RTnetlink lock is misused here to serialize concurrent
753 opens of slcan channels. There are better ways, but it is
758 /* Collect hanged up channels. */
761 sl = (struct slcan *) tty->disc_data;
764 /* First make sure we're not already connected. */
765 if (sl && sl->magic == SLCAN_MAGIC)
768 /* OK. Find a free SLCAN channel to use. */
770 sl = slc_alloc(tty_devnum(tty));
776 sl->line = tty_devnum(tty);
777 sl->pid = current->pid;
779 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
780 /* FIXME: already done before we were called - seems this can go */
781 if (tty->driver->flush_buffer)
782 tty->driver->flush_buffer(tty);
785 if (!test_bit(SLF_INUSE, &sl->flags)) {
786 /* Perform the low-level SLCAN initialization. */
790 set_bit(SLF_INUSE, &sl->flags);
792 err = register_netdevice(sl->dev);
797 /* Done. We have linked the TTY line to a channel. */
800 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
801 tty->receive_room = 65536; /* We don't flow control */
804 return sl->dev->base_addr;
808 tty->disc_data = NULL;
809 clear_bit(SLF_INUSE, &sl->flags);
814 /* Count references from TTY module */
820 FIXME: 1,2 are fixed 3 was never true anyway.
822 Let me to blame a bit.
823 1. TTY module calls this funstion on soft interrupt.
824 2. TTY module calls this function WITH MASKED INTERRUPTS!
825 3. TTY module does not notify us about line discipline
828 Seems, now it is clean. The solution is to consider netdevice and
829 line discipline sides as two independent threads.
831 By-product (not desired): slc? does not feel hangups and remains open.
832 It is supposed, that user level program (dip, diald, slattach...)
833 will catch SIGHUP and make the rest of work.
835 I see no way to make more with current tty code. --ANK
839 * Close down a SLCAN channel.
840 * This means flushing out any pending queues, and then returning. This
841 * call is serialized against other ldisc functions.
843 static void slcan_close(struct tty_struct *tty)
845 struct slcan *sl = (struct slcan *) tty->disc_data;
847 /* First make sure we're connected. */
848 if (!sl || sl->magic != SLCAN_MAGIC || sl->tty != tty)
851 tty->disc_data = NULL;
856 /* Count references from TTY module */
859 /* Perform I/O control on an active SLCAN channel. */
860 static int slcan_ioctl(struct tty_struct *tty, struct file *file,
861 unsigned int cmd, unsigned long arg)
863 struct slcan *sl = (struct slcan *) tty->disc_data;
866 /* First make sure we're connected. */
867 if (!sl || sl->magic != SLCAN_MAGIC)
872 tmp = strlen(sl->dev->name) + 1;
873 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
880 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
881 /* Allow stty to read, but not set, the serial port */
884 return n_tty_ioctl(tty, file, cmd, arg);
890 return tty_mode_ioctl(tty, file, cmd, arg);
895 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
896 static struct tty_ldisc slc_ldisc = {
898 static struct tty_ldisc_ops slc_ldisc = {
900 .owner = THIS_MODULE,
901 .magic = TTY_LDISC_MAGIC,
904 .close = slcan_close,
905 .ioctl = slcan_ioctl,
906 .receive_buf = slcan_receive_buf,
907 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
908 .receive_room = slcan_receive_room,
910 .write_wakeup = slcan_write_wakeup,
913 /************************************
914 * general slcan module init/exit
915 ************************************/
917 static int __init slcan_init(void)
922 maxdev = 4; /* Sanity */
925 printk(KERN_INFO "slcan: %d dynamic interface channels.\n", maxdev);
927 slcan_devs = kmalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
929 printk(KERN_ERR "slcan: can't allocate slcan device array!\n");
933 /* Clear the pointer array, we allocate devices when we need them */
934 memset(slcan_devs, 0, sizeof(struct net_device *)*maxdev);
936 /* Fill in our line protocol discipline, and register it */
937 status = tty_register_ldisc(N_SLCAN, &slc_ldisc);
939 printk(KERN_ERR "slcan: can't register line discipline\n");
945 static void __exit slcan_exit(void)
948 struct net_device *dev;
950 unsigned long timeout = jiffies + HZ;
953 if (slcan_devs == NULL)
956 /* First of all: check for active disciplines and hangup them.
960 msleep_interruptible(100);
963 for (i = 0; i < maxdev; i++) {
967 sl = netdev_priv(dev);
968 spin_lock_bh(&sl->lock);
973 spin_unlock_bh(&sl->lock);
975 } while (busy && time_before(jiffies, timeout));
978 for (i = 0; i < maxdev; i++) {
982 slcan_devs[i] = NULL;
984 sl = netdev_priv(dev);
986 printk(KERN_ERR "%s: tty discipline still running\n",
988 /* Intentionally leak the control block. */
989 dev->destructor = NULL;
992 unregister_netdev(dev);
998 i = tty_unregister_ldisc(N_SLCAN);
1000 printk(KERN_ERR "slcan: can't unregister ldisc (err %d)\n", i);
1003 module_init(slcan_init);
1004 module_exit(slcan_exit);