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>
60 #include <linux/kthread.h>
62 /* 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 buffer len to store whole LIN message*/
82 #define SLLIN_DATA_MAX 8
83 #define SLLIN_BUFF_LEN (1 /*break*/ + 1 /*sync*/ + 1 /*ID*/ + \
84 SLLIN_DATA_MAX + 1 /*checksum*/)
85 #define SLLIN_BUFF_BREAK 0
86 #define SLLIN_BUFF_SYNC 1
87 #define SLLIN_BUFF_ID 2
88 #define SLLIN_BUFF_DATA 3
94 SLSTATE_RESPONSE_WAIT,
95 SLSTATE_RESPONSE_SENT,
101 /* Various fields. */
102 struct tty_struct *tty; /* ptr to TTY structure */
103 struct net_device *dev; /* easy for intr handling */
106 /* LIN message buffer and actual processed data counts */
107 unsigned char rx_buff[SLLIN_BUFF_LEN]; /* LIN Rx buffer */
108 unsigned char tx_buff[SLLIN_BUFF_LEN]; /* LIN Tx buffer */
109 int rx_expect; /* expected number of Rx chars */
110 int rx_lim; /* maximum Rx chars for ID */
111 int rx_cnt; /* message buffer Rx fill level */
112 int tx_lim; /* actual limit of bytes to Tx */
113 int tx_cnt; /* number of already Tx bytes */
114 char lin_master; /* node is a master node */
115 int lin_baud; /* LIN baudrate */
116 int lin_state; /* state */
117 int id_to_send; /* there is ID to be sent */
119 unsigned long flags; /* Flag values/ mode etc */
120 #define SLF_INUSE 0 /* Channel in use */
121 #define SLF_ERROR 1 /* Parity, etc. error */
122 #define SLF_RXEVENT 2 /* Rx wake event */
123 #define SLF_TXEVENT 3 /* Tx wake event */
124 #define SLF_MSGEVENT 4 /* CAN message to sent */
126 unsigned char leased;
128 struct task_struct *kwthread;
129 wait_queue_head_t kwt_wq;
132 static struct net_device **sllin_devs;
134 const unsigned char sllin_id_parity_table[] = {
135 0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
136 0x00,0x40,0xc0,0x80,0x40,0x00,0x80,0xc0,
137 0x40,0x00,0x80,0xc0,0x00,0x40,0xc0,0x80,
138 0xc0,0x80,0x00,0x40,0x80,0xc0,0x40,0x00,
139 0x00,0x40,0xc0,0x80,0x40,0x00,0x80,0xc0,
140 0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
141 0xc0,0x80,0x00,0x40,0x80,0xc0,0x40,0x00,
142 0x40,0x00,0x80,0xc0,0x00,0x40,0xc0,0x80
145 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
147 struct ktermios old_termios;
150 mutex_lock(&tty->termios_mutex);
152 old_termios = *(tty->termios);
153 cflag = tty->termios->c_cflag;
154 cflag &= ~(CBAUD | CIBAUD);
156 tty->termios->c_cflag = cflag;
158 tty_encode_baud_rate(tty, speed, speed);
160 if (tty->ops->set_termios)
161 tty->ops->set_termios(tty, &old_termios);
162 //priv->io.speed = speed;
163 mutex_unlock(&tty->termios_mutex);
169 /* Send one completely decapsulated can_frame to the network layer */
170 static void sll_bump(struct sllin *sl)
172 // struct sk_buff *skb;
173 // struct can_frame cf;
174 // int i, dlc_pos, tmp;
175 // unsigned long ultmp;
176 // char cmd = sl->rbuff[0];
178 // if ((cmd != 't') && (cmd != 'T') && (cmd != 'r') && (cmd != 'R'))
181 // if (cmd & 0x20) /* tiny chars 'r' 't' => standard frame format */
182 // dlc_pos = 4; /* dlc position tiiid */
184 // dlc_pos = 9; /* dlc position Tiiiiiiiid */
186 // if (!((sl->rbuff[dlc_pos] >= '0') && (sl->rbuff[dlc_pos] < '9')))
189 // cf.can_dlc = sl->rbuff[dlc_pos] - '0'; /* get can_dlc from ASCII val */
191 // sl->rbuff[dlc_pos] = 0; /* terminate can_id string */
193 // if (strict_strtoul(sl->rbuff+1, 16, &ultmp))
196 // cf.can_id = ultmp;
198 // if (!(cmd & 0x20)) /* NO tiny chars => extended frame format */
199 // cf.can_id |= CAN_EFF_FLAG;
201 // if ((cmd | 0x20) == 'r') /* RTR frame */
202 // cf.can_id |= CAN_RTR_FLAG;
204 // *(u64 *) (&cf.data) = 0; /* clear payload */
206 // for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
208 // tmp = asc2nibble(sl->rbuff[dlc_pos++]);
211 // cf.data[i] = (tmp << 4);
212 // tmp = asc2nibble(sl->rbuff[dlc_pos++]);
215 // cf.data[i] |= tmp;
219 // skb = dev_alloc_skb(sizeof(struct can_frame));
223 // skb->dev = sl->dev;
224 // skb->protocol = htons(ETH_P_CAN);
225 // skb->pkt_type = PACKET_BROADCAST;
226 // skb->ip_summed = CHECKSUM_UNNECESSARY;
227 // memcpy(skb_put(skb, sizeof(struct can_frame)),
228 // &cf, sizeof(struct can_frame));
231 // sl->dev->stats.rx_packets++;
232 // sl->dev->stats.rx_bytes += cf.can_dlc;
236 /************************************************************************
237 * STANDARD SLLIN ENCAPSULATION *
238 ************************************************************************/
240 /* Convert particular CAN frame into LIN frame and send it to TTY queue. */
241 static void sll_encaps(struct sllin *sl, struct can_frame *cf)
244 char lframe[16] = {0x00, 0x55}; /* Fake break, Sync byte */
245 struct tty_struct *tty = sl->tty;
247 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
249 /* We do care only about SFF frames */
250 if (cf->can_id & CAN_EFF_FLAG)
253 /* Send only header */
254 if (cf->can_id & CAN_RTR_FLAG) {
255 pr_debug("sllin: %s() RTR CAN frame\n", __FUNCTION__);
256 lframe[2] = (u8)cf->can_id; /* Get one byte LIN ID */
258 sltty_change_speed(tty, sl->lin_baud * 2 / 3);
259 tty->ops->write(tty, &lframe[0], 1);
260 sltty_change_speed(tty, sl->lin_baud);
261 tty->ops->write(tty, &lframe[1], 1);
262 tty->ops->write(tty, &lframe[2], 1);
264 pr_debug("sllin: %s() non-RTR CAN frame\n", __FUNCTION__);
265 /* idx = strlen(sl->xbuff);
267 for (i = 0; i < cf->can_dlc; i++)
268 sprintf(&sl->xbuff[idx + 2*i], "%02X", cf->data[i]);
270 * Order of next two lines is *very* important.
271 * When we are sending a little amount of data,
272 * the transfer may be completed inside the ops->write()
273 * routine, because it's running with interrupts enabled.
274 * In this case we *never* got WRITE_WAKEUP event,
275 * if we did not request it before write operation.
276 * 14 Oct 1994 Dmitry Gorodchanin.
278 set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
279 actual = sl->tty->ops->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
280 sl->xleft = strlen(sl->xbuff) - actual;
281 sl->xhead = sl->xbuff + actual;
282 sl->dev->stats.tx_bytes += cf->can_dlc;
289 * Called by the driver when there's room for more data. If we have
290 * more packets to send, we send them here.
292 static void sllin_write_wakeup(struct tty_struct *tty)
296 struct sllin *sl = (struct sllin *) tty->disc_data;
298 /* First make sure we're connected. */
299 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
302 if (sl->lin_state != SLSTATE_BREAK_SENT)
303 remains = sl->tx_lim - sl->tx_cnt;
305 remains = SLLIN_BUFF_BREAK + 1 - sl->tx_cnt;
308 actual = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, sl->tx_cnt - sl->tx_lim);
309 sl->tx_cnt += actual;
311 if (sl->tx_cnt < sl->tx_lim) {
312 printk(KERN_INFO "sllin_write_wakeup sent %d, remains %d, waiting\n",
313 sl->tx_cnt, sl->tx_lim - sl->tx_cnt);
318 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
319 set_bit(SLF_TXEVENT, &sl->flags);
320 wake_up(&sl->kwt_wq);
322 printk(KERN_INFO "sllin_write_wakeup sent %d, wakeup\n", sl->tx_cnt);
325 /* Send a can_frame to a TTY queue. */
326 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
328 struct sllin *sl = netdev_priv(dev);
330 if (skb->len != sizeof(struct can_frame))
333 spin_lock(&sl->lock);
334 if (!netif_running(dev)) {
335 spin_unlock(&sl->lock);
336 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
339 if (sl->tty == NULL) {
340 spin_unlock(&sl->lock);
344 netif_stop_queue(sl->dev);
345 sll_encaps(sl, (struct can_frame *) skb->data); /* encaps & send */
346 spin_unlock(&sl->lock);
354 /******************************************
355 * Routines looking at netdevice side.
356 ******************************************/
358 /* Netdevice UP -> DOWN routine */
359 static int sll_close(struct net_device *dev)
361 struct sllin *sl = netdev_priv(dev);
363 spin_lock_bh(&sl->lock);
365 /* TTY discipline is running. */
366 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
368 netif_stop_queue(dev);
371 spin_unlock_bh(&sl->lock);
376 /* Netdevice DOWN -> UP routine */
377 static int sll_open(struct net_device *dev)
379 struct sllin *sl = netdev_priv(dev);
381 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
386 sl->flags &= (1 << SLF_INUSE);
387 netif_start_queue(dev);
391 /* Hook the destructor so we can free sllin devs at the right point in time */
392 static void sll_free_netdev(struct net_device *dev)
394 int i = dev->base_addr;
396 sllin_devs[i] = NULL;
399 static const struct net_device_ops sll_netdev_ops = {
400 .ndo_open = sll_open,
401 .ndo_stop = sll_close,
402 .ndo_start_xmit = sll_xmit,
405 static void sll_setup(struct net_device *dev)
407 dev->netdev_ops = &sll_netdev_ops;
408 dev->destructor = sll_free_netdev;
410 dev->hard_header_len = 0;
412 dev->tx_queue_len = 10;
414 dev->mtu = sizeof(struct can_frame);
415 dev->type = ARPHRD_CAN;
417 /* New-style flags. */
418 dev->flags = IFF_NOARP;
419 dev->features = NETIF_F_NO_CSUM;
422 /******************************************
423 Routines looking at TTY side.
424 ******************************************/
427 * Handle the 'receiver data ready' interrupt.
428 * This function is called by the 'tty_io' module in the kernel when
429 * a block of SLLIN data has been received, which can now be decapsulated
430 * and sent on to some IP layer for further processing. This will not
431 * be re-entered while running but other ldisc functions may be called
435 static void sllin_receive_buf(struct tty_struct *tty,
436 const unsigned char *cp, char *fp, int count)
438 struct sllin *sl = (struct sllin *) tty->disc_data;
440 printk(KERN_INFO "sllin_receive_buf invoked\n");
442 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
445 /* Read the characters out of the buffer */
448 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
449 sl->dev->stats.rx_errors++;
450 printk(KERN_INFO "sllin_receive_buf char 0x%02x ignored due marker 0x%02x, flags 0x%lx\n",
451 *cp, *(fp-1), sl->flags);
456 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
457 sl->rx_buff[sl->rx_cnt++] = *cp++;
461 if(sl->rx_cnt >= sl->rx_expect) {
462 set_bit(SLF_RXEVENT, &sl->flags);
463 wake_up(&sl->kwt_wq);
464 printk(KERN_INFO "sllin_receive_buf count %d, wakeup\n", sl->rx_cnt);
466 printk(KERN_INFO "sllin_receive_buf count %d, waiting\n", sl->rx_cnt);
470 /*****************************************
471 * sllin message helper routines
472 *****************************************/
474 int sllin_setup_msg(struct sllin *sl, int mode, int id,
475 unsigned char *data, int len)
484 sl->tx_buff[SLLIN_BUFF_BREAK] = 0;
485 sl->tx_buff[SLLIN_BUFF_SYNC] = 0x55;
486 sl->tx_buff[SLLIN_BUFF_ID] = id | sllin_id_parity_table[id];
487 sl->tx_lim = SLLIN_BUFF_DATA;
489 if ((data != NULL) && len) {
494 memcpy(sl->tx_buff + SLLIN_BUFF_DATA, data, len);
495 /* compute data parity there */
496 for (i = SLLIN_BUFF_DATA; i < sl->tx_lim; i++) {
497 csum += sl->tx_buff[i];
502 sl->tx_buff[sl->tx_lim++] = csum;
505 sl->rx_lim += len + 1;
511 int sllin_send_tx_buff(struct sllin *sl)
513 struct tty_struct *tty = sl->tty;
517 if (sl->lin_state != SLSTATE_BREAK_SENT)
518 remains = sl->tx_lim - sl->tx_cnt;
523 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
531 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
532 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
534 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
542 printk(KERN_INFO "sllin_send_tx_buff sent %d, remains %d\n",
543 sl->tx_cnt, remains);
548 int sllin_send_break(struct sllin *sl)
550 struct tty_struct *tty = sl->tty;
551 unsigned long break_baud = sl->lin_baud;
554 //break_baud = (break_baud * 8) / 14;
557 sltty_change_speed(tty, break_baud);
559 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
561 sl->lin_state = SLSTATE_BREAK_SENT;
563 res = sllin_send_tx_buff(sl);
565 sl->lin_state = SLSTATE_IDLE;
572 /*****************************************
573 * sllin_kwthread - kernel worker thread
574 *****************************************/
576 int sllin_kwthread(void *ptr)
578 struct sllin *sl = (struct sllin *)ptr;
579 struct tty_struct *tty = sl->tty;
582 printk(KERN_INFO "sllin: sllin_kwthread started.\n");
584 clear_bit(SLF_ERROR, &sl->flags);
586 sltty_change_speed(tty, sl->lin_baud);
588 sllin_setup_msg(sl, 0, 0x33, NULL, 0);
591 while (!kthread_should_stop()) {
593 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
595 if(sllin_send_break(sl)<0) {
596 /* error processing */
601 wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
602 test_bit(SLF_RXEVENT, &sl->flags) ||
603 test_bit(SLF_TXEVENT, &sl->flags));
605 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
606 printk(KERN_INFO "sllin_kthread RXEVENT \n");
609 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
610 printk(KERN_INFO "sllin_kthread TXEVENT \n");
613 switch (sl->lin_state) {
614 case SLSTATE_BREAK_SENT:
615 if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
618 res = sltty_change_speed(tty, sl->lin_baud);
620 sllin_send_tx_buff(sl);
622 sl->lin_state = SLSTATE_ID_SENT;
625 case SLSTATE_ID_SENT:
627 sl->lin_state = SLSTATE_IDLE;
633 /* sll_bump(sl); send packet to the network layer */
635 /* sl->dev->stats.tx_packets++; send frames statistic */
636 /* netif_wake_queue(sl->dev); allow next Tx packet arrival */
640 printk(KERN_INFO "sllin: sllin_kwthread stopped.\n");
646 /************************************
647 * sllin_open helper routines.
648 ************************************/
650 /* Collect hanged up channels */
651 static void sll_sync(void)
654 struct net_device *dev;
657 for (i = 0; i < maxdev; i++) {
662 sl = netdev_priv(dev);
663 if (sl->tty || sl->leased)
665 if (dev->flags & IFF_UP)
670 /* Find a free SLLIN channel, and link in this `tty' line. */
671 static struct sllin *sll_alloc(dev_t line)
674 struct net_device *dev = NULL;
677 if (sllin_devs == NULL)
678 return NULL; /* Master array missing ! */
680 for (i = 0; i < maxdev; i++) {
687 /* Sorry, too many, all slots in use */
692 sl = netdev_priv(dev);
693 if (test_bit(SLF_INUSE, &sl->flags)) {
694 unregister_netdevice(dev);
696 sllin_devs[i] = NULL;
702 sprintf(name, "sllin%d", i);
704 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
710 sl = netdev_priv(dev);
712 /* Initialize channel control data */
713 sl->magic = SLLIN_MAGIC;
715 spin_lock_init(&sl->lock);
722 * Open the high-level part of the SLLIN channel.
723 * This function is called by the TTY module when the
724 * SLLIN line discipline is called for. Because we are
725 * sure the tty line exists, we only have to link it to
726 * a free SLLIN channel...
728 * Called in process context serialized from other ldisc calls.
731 static int sllin_open(struct tty_struct *tty)
735 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
737 if (!capable(CAP_NET_ADMIN))
740 if (tty->ops->write == NULL)
743 /* RTnetlink lock is misused here to serialize concurrent
744 opens of sllin channels. There are better ways, but it is
749 /* Collect hanged up channels. */
755 /* First make sure we're not already connected. */
756 if (sl && sl->magic == SLLIN_MAGIC)
759 /* OK. Find a free SLLIN channel to use. */
761 sl = sll_alloc(tty_devnum(tty));
767 sl->line = tty_devnum(tty);
769 if (!test_bit(SLF_INUSE, &sl->flags)) {
770 /* Perform the low-level SLLIN initialization. */
779 sl->lin_state = SLSTATE_IDLE;
781 set_bit(SLF_INUSE, &sl->flags);
783 init_waitqueue_head(&sl->kwt_wq);
784 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
785 if (sl->kwthread == NULL)
788 err = register_netdevice(sl->dev);
790 goto err_free_chan_and_thread;
793 /* Done. We have linked the TTY line to a channel. */
795 tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
797 /* TTY layer expects 0 on success */
800 err_free_chan_and_thread:
801 kthread_stop(sl->kwthread);
806 tty->disc_data = NULL;
807 clear_bit(SLF_INUSE, &sl->flags);
812 /* Count references from TTY module */
817 * Close down a SLLIN channel.
818 * This means flushing out any pending queues, and then returning. This
819 * call is serialized against other ldisc functions.
821 * We also use this method for a hangup event.
824 static void sllin_close(struct tty_struct *tty)
826 struct sllin *sl = (struct sllin *) tty->disc_data;
828 /* First make sure we're connected. */
829 if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
832 kthread_stop(sl->kwthread);
835 tty->disc_data = NULL;
840 /* Flush network side */
841 unregister_netdev(sl->dev);
842 /* This will complete via sl_free_netdev */
845 static int sllin_hangup(struct tty_struct *tty)
851 /* Perform I/O control on an active SLLIN channel. */
852 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
853 unsigned int cmd, unsigned long arg)
855 struct sllin *sl = (struct sllin *) tty->disc_data;
858 /* First make sure we're connected. */
859 if (!sl || sl->magic != SLLIN_MAGIC)
864 tmp = strlen(sl->dev->name) + 1;
865 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
873 return tty_mode_ioctl(tty, file, cmd, arg);
877 static struct tty_ldisc_ops sll_ldisc = {
878 .owner = THIS_MODULE,
879 .magic = TTY_LDISC_MAGIC,
882 .close = sllin_close,
883 .hangup = sllin_hangup,
884 .ioctl = sllin_ioctl,
885 .receive_buf = sllin_receive_buf,
886 .write_wakeup = sllin_write_wakeup,
889 static int __init sllin_init(void)
894 maxdev = 4; /* Sanity */
897 printk(KERN_INFO "sllin: %d dynamic interface channels.\n", maxdev);
899 sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
901 printk(KERN_ERR "sllin: can't allocate sllin device array!\n");
905 /* Fill in our line protocol discipline, and register it */
906 status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
908 printk(KERN_ERR "sllin: can't register line discipline\n");
914 static void __exit sllin_exit(void)
917 struct net_device *dev;
919 unsigned long timeout = jiffies + HZ;
922 if (sllin_devs == NULL)
925 /* First of all: check for active disciplines and hangup them.
929 msleep_interruptible(100);
932 for (i = 0; i < maxdev; i++) {
936 sl = netdev_priv(dev);
937 spin_lock_bh(&sl->lock);
942 spin_unlock_bh(&sl->lock);
944 } while (busy && time_before(jiffies, timeout));
946 /* FIXME: hangup is async so we should wait when doing this second
949 for (i = 0; i < maxdev; i++) {
953 sllin_devs[i] = NULL;
955 sl = netdev_priv(dev);
957 printk(KERN_ERR "%s: tty discipline still running\n",
959 /* Intentionally leak the control block. */
960 dev->destructor = NULL;
963 unregister_netdev(dev);
969 i = tty_unregister_ldisc(N_SLLIN);
971 printk(KERN_ERR "sllin: can't unregister ldisc (err %d)\n", i);
974 module_init(sllin_init);
975 module_exit(sllin_exit);