]> rtime.felk.cvut.cz Git - linux-lin.git/blob - sllin/sllin.c
Added some debugging functions.
[linux-lin.git] / sllin / sllin.c
1 /*
2  * sllin.c - serial line LIN interface driver (using tty line discipline)
3  *
4  * This file is derived from linux/drivers/net/slip.c
5  *
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>
9  *
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.
14  *
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.
19  *
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
24  *
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
36  * DAMAGE.
37  *
38  * Send feedback to <socketcan-users@lists.berlios.de>
39  *
40  */
41
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44
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
61 /* Should be in include/linux/tty.h */
62 #define N_SLLIN         25
63
64 static __initdata const char banner[] =
65         KERN_INFO "sllin: serial line LIN interface driver\n";
66
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>");
71
72 #define SLLIN_MAGIC 0x53CA
73
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");
79
80 /* maximum rx buffer len: extended CAN frame with timestamp */
81 #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
82
83 struct sllin {
84         int                     magic;
85
86         /* Various fields. */
87         struct tty_struct       *tty;           /* ptr to TTY structure      */
88         struct net_device       *dev;           /* easy for intr handling    */
89         spinlock_t              lock;
90
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  */
97
98         unsigned long           flags;          /* Flag values/ mode etc     */
99 #define SLF_INUSE               0               /* Channel in use            */
100 #define SLF_ERROR               1               /* Parity, etc. error        */
101
102         unsigned char           leased;
103         dev_t                   line;
104         pid_t                   pid;
105 };
106
107 static struct net_device **sllin_devs;
108
109
110 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
111 {
112         struct ktermios old_termios;
113         int cflag;
114
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);
123
124         return 0;
125 }
126
127
128 /* Send one completely decapsulated can_frame to the network layer */
129 static void sll_bump(struct sllin *sl)
130 {
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];
136 //
137 //      if ((cmd != 't') && (cmd != 'T') && (cmd != 'r') && (cmd != 'R'))
138 //              return;
139 //
140 //      if (cmd & 0x20) /* tiny chars 'r' 't' => standard frame format */
141 //              dlc_pos = 4; /* dlc position tiiid */
142 //      else
143 //              dlc_pos = 9; /* dlc position Tiiiiiiiid */
144 //
145 //      if (!((sl->rbuff[dlc_pos] >= '0') && (sl->rbuff[dlc_pos] < '9')))
146 //              return;
147 //
148 //      cf.can_dlc = sl->rbuff[dlc_pos] - '0'; /* get can_dlc from ASCII val */
149 //
150 //      sl->rbuff[dlc_pos] = 0; /* terminate can_id string */
151 //
152 //      if (strict_strtoul(sl->rbuff+1, 16, &ultmp))
153 //              return;
154 //
155 //      cf.can_id = ultmp;
156 //
157 //      if (!(cmd & 0x20)) /* NO tiny chars => extended frame format */
158 //              cf.can_id |= CAN_EFF_FLAG;
159 //
160 //      if ((cmd | 0x20) == 'r') /* RTR frame */
161 //              cf.can_id |= CAN_RTR_FLAG;
162 //
163 //      *(u64 *) (&cf.data) = 0; /* clear payload */
164 //
165 //      for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
166 //
167 //              tmp = asc2nibble(sl->rbuff[dlc_pos++]);
168 //              if (tmp > 0x0F)
169 //                      return;
170 //              cf.data[i] = (tmp << 4);
171 //              tmp = asc2nibble(sl->rbuff[dlc_pos++]);
172 //              if (tmp > 0x0F)
173 //                      return;
174 //              cf.data[i] |= tmp;
175 //      }
176 //
177 //
178 //      skb = dev_alloc_skb(sizeof(struct can_frame));
179 //      if (!skb)
180 //              return;
181 //
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));
188 //      netif_rx(skb);
189 //
190 //      sl->dev->stats.rx_packets++;
191 //      sl->dev->stats.rx_bytes += cf.can_dlc;
192 }
193
194 /* parse tty input stream */
195 static void sllin_unesc(struct sllin *sl, unsigned char s)
196 {
197
198         if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
199                 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
200                     (sl->rcount > 4))  {
201                         sll_bump(sl);
202                 }
203                 sl->rcount = 0;
204         } else {
205                 if (!test_bit(SLF_ERROR, &sl->flags))  {
206                         if (sl->rcount < SLC_MTU)  {
207                                 sl->rbuff[sl->rcount++] = s;
208                                 return;
209                         } else {
210                                 sl->dev->stats.rx_over_errors++;
211                                 set_bit(SLF_ERROR, &sl->flags);
212                         }
213                 }
214         }
215 }
216
217  /************************************************************************
218   *                     STANDARD SLLIN ENCAPSULATION                     *
219   ************************************************************************/
220
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)
223 {
224         int actual, idx, i;
225         char lframe[16] = {0x00, 0x55}; /* Fake break, Sync byte */
226         struct tty_struct *tty = sl->tty;
227
228         pr_debug("sllin: %s() invoked\n", __FUNCTION__);
229
230         /* We do care only about SFF frames */
231         if (cf->can_id & CAN_EFF_FLAG)
232                 return;
233
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 */
238
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);
244         } else {
245                 pr_debug("sllin: non-RTR CAN frame\n", __FUNCTION__);
246                 /*      idx = strlen(sl->xbuff);
247
248                         for (i = 0; i < cf->can_dlc; i++)
249                         sprintf(&sl->xbuff[idx + 2*i], "%02X", cf->data[i]);
250
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.
258
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;
264                  */
265         }
266
267 }
268
269 /*
270  * Called by the driver when there's room for more data.  If we have
271  * more packets to send, we send them here.
272  */
273 static void sllin_write_wakeup(struct tty_struct *tty)
274 {
275         int actual;
276         struct sllin *sl = (struct sllin *) tty->disc_data;
277
278         /* First make sure we're connected. */
279         if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
280                 return;
281
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);
288                 return;
289         }
290
291         actual = tty->ops->write(tty, sl->xhead, sl->xleft);
292         sl->xleft -= actual;
293         sl->xhead += actual;
294 }
295
296 /* Send a can_frame to a TTY queue. */
297 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
298 {
299         struct sllin *sl = netdev_priv(dev);
300
301         if (skb->len != sizeof(struct can_frame))
302                 goto out;
303
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);
308                 goto out;
309         }
310         if (sl->tty == NULL) {
311                 spin_unlock(&sl->lock);
312                 goto out;
313         }
314
315         netif_stop_queue(sl->dev);
316         sll_encaps(sl, (struct can_frame *) skb->data); /* encaps & send */
317         spin_unlock(&sl->lock);
318
319 out:
320         kfree_skb(skb);
321         return NETDEV_TX_OK;
322 }
323
324
325 /******************************************
326  *   Routines looking at netdevice side.
327  ******************************************/
328
329 /* Netdevice UP -> DOWN routine */
330 static int sll_close(struct net_device *dev)
331 {
332         struct sllin *sl = netdev_priv(dev);
333
334         spin_lock_bh(&sl->lock);
335         if (sl->tty) {
336                 /* TTY discipline is running. */
337                 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
338         }
339         netif_stop_queue(dev);
340         sl->rcount   = 0;
341         sl->xleft    = 0;
342         spin_unlock_bh(&sl->lock);
343
344         return 0;
345 }
346
347 /* Netdevice DOWN -> UP routine */
348 static int sll_open(struct net_device *dev)
349 {
350         struct sllin *sl = netdev_priv(dev);
351
352         pr_debug("sllin: %s() invoked\n", __FUNCTION__);
353
354         if (sl->tty == NULL)
355                 return -ENODEV;
356
357         sl->flags &= (1 << SLF_INUSE);
358         netif_start_queue(dev);
359         return 0;
360 }
361
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)
364 {
365         int i = dev->base_addr;
366         free_netdev(dev);
367         sllin_devs[i] = NULL;
368 }
369
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,
374 };
375
376 static void sll_setup(struct net_device *dev)
377 {
378         dev->netdev_ops         = &sll_netdev_ops;
379         dev->destructor         = sll_free_netdev;
380
381         dev->hard_header_len    = 0;
382         dev->addr_len           = 0;
383         dev->tx_queue_len       = 10;
384
385         dev->mtu                = sizeof(struct can_frame);
386         dev->type               = ARPHRD_CAN;
387
388         /* New-style flags. */
389         dev->flags              = IFF_NOARP;
390         dev->features           = NETIF_F_NO_CSUM;
391 }
392
393 /******************************************
394   Routines looking at TTY side.
395  ******************************************/
396
397 /*
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
403  * in parallel
404  */
405
406 static void sllin_receive_buf(struct tty_struct *tty,
407                               const unsigned char *cp, char *fp, int count)
408 {
409         struct sllin *sl = (struct sllin *) tty->disc_data;
410
411         if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
412                 return;
413
414         /* Read the characters out of the buffer */
415         while (count--) {
416                 if (fp && *fp++) {
417                         if (!test_and_set_bit(SLF_ERROR, &sl->flags))
418                                 sl->dev->stats.rx_errors++;
419                         cp++;
420                         continue;
421                 }
422                 sllin_unesc(sl, *cp++);
423         }
424 }
425
426 /************************************
427  *  sllin_open helper routines.
428  ************************************/
429
430 /* Collect hanged up channels */
431 static void sll_sync(void)
432 {
433         int i;
434         struct net_device *dev;
435         struct sllin      *sl;
436
437         for (i = 0; i < maxdev; i++) {
438                 dev = sllin_devs[i];
439                 if (dev == NULL)
440                         break;
441
442                 sl = netdev_priv(dev);
443                 if (sl->tty || sl->leased)
444                         continue;
445                 if (dev->flags & IFF_UP)
446                         dev_close(dev);
447         }
448 }
449
450 /* Find a free SLLIN channel, and link in this `tty' line. */
451 static struct sllin *sll_alloc(dev_t line)
452 {
453         int i;
454         struct net_device *dev = NULL;
455         struct sllin       *sl;
456
457         if (sllin_devs == NULL)
458                 return NULL;    /* Master array missing ! */
459
460         for (i = 0; i < maxdev; i++) {
461                 dev = sllin_devs[i];
462                 if (dev == NULL)
463                         break;
464
465         }
466
467         /* Sorry, too many, all slots in use */
468         if (i >= maxdev)
469                 return NULL;
470
471         if (dev) {
472                 sl = netdev_priv(dev);
473                 if (test_bit(SLF_INUSE, &sl->flags)) {
474                         unregister_netdevice(dev);
475                         dev = NULL;
476                         sllin_devs[i] = NULL;
477                 }
478         }
479
480         if (!dev) {
481                 char name[IFNAMSIZ];
482                 sprintf(name, "sllin%d", i);
483
484                 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
485                 if (!dev)
486                         return NULL;
487                 dev->base_addr  = i;
488         }
489
490         sl = netdev_priv(dev);
491
492         /* Initialize channel control data */
493         sl->magic = SLLIN_MAGIC;
494         sl->dev = dev;
495         spin_lock_init(&sl->lock);
496         sllin_devs[i] = dev;
497
498         return sl;
499 }
500
501 /*
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...
507  *
508  * Called in process context serialized from other ldisc calls.
509  */
510
511 static int sllin_open(struct tty_struct *tty)
512 {
513         struct sllin *sl;
514         int err;
515         pr_debug("sllin: %s() invoked\n", __FUNCTION__);
516
517         if (!capable(CAP_NET_ADMIN))
518                 return -EPERM;
519
520         if (tty->ops->write == NULL)
521                 return -EOPNOTSUPP;
522
523         /* RTnetlink lock is misused here to serialize concurrent
524            opens of sllin channels. There are better ways, but it is
525            the simplest one.
526          */
527         rtnl_lock();
528
529         /* Collect hanged up channels. */
530         sll_sync();
531
532         sl = tty->disc_data;
533
534         err = -EEXIST;
535         /* First make sure we're not already connected. */
536         if (sl && sl->magic == SLLIN_MAGIC)
537                 goto err_exit;
538
539         /* OK.  Find a free SLLIN channel to use. */
540         err = -ENFILE;
541         sl = sll_alloc(tty_devnum(tty));
542         if (sl == NULL)
543                 goto err_exit;
544
545         sl->tty = tty;
546         tty->disc_data = sl;
547         sl->line = tty_devnum(tty);
548         sl->pid = current->pid;
549
550         if (!test_bit(SLF_INUSE, &sl->flags)) {
551                 /* Perform the low-level SLLIN initialization. */
552                 sl->rcount   = 0;
553                 sl->xleft    = 0;
554
555                 set_bit(SLF_INUSE, &sl->flags);
556
557                 err = register_netdevice(sl->dev);
558                 if (err)
559                         goto err_free_chan;
560         }
561
562         /* Done.  We have linked the TTY line to a channel. */
563         rtnl_unlock();
564         tty->receive_room = 65536;      /* We don't flow control */
565
566         /* TTY layer expects 0 on success */
567         return 0;
568
569 err_free_chan:
570         sl->tty = NULL;
571         tty->disc_data = NULL;
572         clear_bit(SLF_INUSE, &sl->flags);
573
574 err_exit:
575         rtnl_unlock();
576
577         /* Count references from TTY module */
578         return err;
579 }
580
581 /*
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.
585  *
586  * We also use this method for a hangup event.
587  */
588
589 static void sllin_close(struct tty_struct *tty)
590 {
591         struct sllin *sl = (struct sllin *) tty->disc_data;
592
593         /* First make sure we're connected. */
594         if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
595                 return;
596
597         tty->disc_data = NULL;
598         sl->tty = NULL;
599         if (!sl->leased)
600                 sl->line = 0;
601
602         /* Flush network side */
603         unregister_netdev(sl->dev);
604         /* This will complete via sl_free_netdev */
605 }
606
607 static int sllin_hangup(struct tty_struct *tty)
608 {
609         sllin_close(tty);
610         return 0;
611 }
612
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)
616 {
617         struct sllin *sl = (struct sllin *) tty->disc_data;
618         unsigned int tmp;
619
620         /* First make sure we're connected. */
621         if (!sl || sl->magic != SLLIN_MAGIC)
622                 return -EINVAL;
623
624         switch (cmd) {
625         case SIOCGIFNAME:
626                 tmp = strlen(sl->dev->name) + 1;
627                 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
628                         return -EFAULT;
629                 return 0;
630
631         case SIOCSIFHWADDR:
632                 return -EINVAL;
633
634         default:
635                 return tty_mode_ioctl(tty, file, cmd, arg);
636         }
637 }
638
639 static struct tty_ldisc_ops sll_ldisc = {
640         .owner          = THIS_MODULE,
641         .magic          = TTY_LDISC_MAGIC,
642         .name           = "sllin",
643         .open           = sllin_open,
644         .close          = sllin_close,
645         .hangup         = sllin_hangup,
646         .ioctl          = sllin_ioctl,
647         .receive_buf    = sllin_receive_buf,
648         .write_wakeup   = sllin_write_wakeup,
649 };
650
651 static int __init sllin_init(void)
652 {
653         int status;
654
655         if (maxdev < 4)
656                 maxdev = 4; /* Sanity */
657
658         printk(banner);
659         printk(KERN_INFO "sllin: %d dynamic interface channels.\n", maxdev);
660
661         sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
662         if (!sllin_devs) {
663                 printk(KERN_ERR "sllin: can't allocate sllin device array!\n");
664                 return -ENOMEM;
665         }
666
667         /* Fill in our line protocol discipline, and register it */
668         status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
669         if (status)  {
670                 printk(KERN_ERR "sllin: can't register line discipline\n");
671                 kfree(sllin_devs);
672         }
673         return status;
674 }
675
676 static void __exit sllin_exit(void)
677 {
678         int i;
679         struct net_device *dev;
680         struct sllin *sl;
681         unsigned long timeout = jiffies + HZ;
682         int busy = 0;
683
684         if (sllin_devs == NULL)
685                 return;
686
687         /* First of all: check for active disciplines and hangup them.
688          */
689         do {
690                 if (busy)
691                         msleep_interruptible(100);
692
693                 busy = 0;
694                 for (i = 0; i < maxdev; i++) {
695                         dev = sllin_devs[i];
696                         if (!dev)
697                                 continue;
698                         sl = netdev_priv(dev);
699                         spin_lock_bh(&sl->lock);
700                         if (sl->tty) {
701                                 busy++;
702                                 tty_hangup(sl->tty);
703                         }
704                         spin_unlock_bh(&sl->lock);
705                 }
706         } while (busy && time_before(jiffies, timeout));
707
708         /* FIXME: hangup is async so we should wait when doing this second
709            phase */
710
711         for (i = 0; i < maxdev; i++) {
712                 dev = sllin_devs[i];
713                 if (!dev)
714                         continue;
715                 sllin_devs[i] = NULL;
716
717                 sl = netdev_priv(dev);
718                 if (sl->tty) {
719                         printk(KERN_ERR "%s: tty discipline still running\n",
720                                dev->name);
721                         /* Intentionally leak the control block. */
722                         dev->destructor = NULL;
723                 }
724
725                 unregister_netdev(dev);
726         }
727
728         kfree(sllin_devs);
729         sllin_devs = NULL;
730
731         i = tty_unregister_ldisc(N_SLLIN);
732         if (i)
733                 printk(KERN_ERR "sllin: can't unregister ldisc (err %d)\n", i);
734 }
735
736 module_init(sllin_init);
737 module_exit(sllin_exit);